╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("../../ui.zig"); 3 const xit = @import("xit"); 4 const xitui = xit.xitui; 5 const wgt = xitui.widget; 6 const layout = xitui.layout; 7 const inp = xitui.input; 8 const Grid = xitui.grid.Grid; 9 const Focus = xitui.focus.Focus; 10 11 pub const AuthTab = @import("./../AuthTab.zig"); 12 13 title: ui.Title, 14 auth_tab: AuthTab, 15 16 const Self = @This(); 17 18 pub fn init(arena: *std.heap.ArenaAllocator) !Self { 19 return .{ 20 .title = try ui.Title.init(arena, "haxy"), 21 .auth_tab = AuthTab.init(), 22 }; 23 } 24 25 pub const View = struct { 26 box: wgt.Box(ui.Widget), 27 data: *const Self, 28 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 29 30 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 31 var box = wgt.Box(ui.Widget).init(.{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 32 errdefer box.deinit(allocator); 33 34 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 35 errdefer tab_ids.deinit(allocator); 36 37 // title sits to the left of the tabs 38 { 39 var title_view = try ui.Title.View.init(allocator, &data.title); 40 errdefer title_view.deinit(allocator); 41 try box.children.put(allocator, title_view.getFocus().id, .{ 42 .widget = .{ .title = title_view }, 43 .rect = null, 44 .min_size = .{ .width = data.title.width, .height = null }, 45 }); 46 } 47 48 // spacer 49 { 50 var text = wgt.Text(ui.Widget).init(" "); 51 errdefer text.deinit(allocator); 52 try box.children.put(allocator, text.getFocus().id, .{ 53 .widget = .{ .text = text }, 54 .rect = null, 55 .min_size = .{ .width = 1, .height = null }, 56 }); 57 } 58 59 // tabs 60 for ( 61 [_][]const u8{ "users", "repos", "", "settings", "" }, 62 [_][]const u8{ "a:/users", "a:/repos", "", "a:/settings", "a:/auth" }, 63 ) |name, focus_name| { 64 // spacer 65 if (focus_name.len == 0) { 66 var spacer = ui.Spacer.init(); 67 errdefer spacer.deinit(allocator); 68 try box.children.put(allocator, spacer.getFocus().id, .{ 69 .widget = .{ .spacer = spacer }, 70 .rect = null, 71 .min_size = .{ .width = 1, .height = null }, 72 }); 73 } 74 // auth 75 else if (std.mem.eql(u8, "a:/auth", focus_name)) { 76 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 77 errdefer auth_tab.deinit(allocator); 78 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 79 try box.children.put(allocator, auth_tab.getFocus().id, .{ 80 .widget = .{ .auth_tab = auth_tab }, 81 .rect = null, 82 .min_size = .{ .width = AuthTab.min_width, .height = null }, 83 }); 84 } 85 // other tabs 86 else { 87 var text_box = try wgt.TextBox(ui.Widget).init(allocator, name, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 88 errdefer text_box.deinit(allocator); 89 text_box.getFocus().focusable = true; 90 text_box.getFocus().kind = .{ .custom = focus_name }; 91 try tab_ids.put(allocator, text_box.getFocus().id, {}); 92 try box.children.put(allocator, text_box.getFocus().id, .{ 93 .widget = .{ .text_box = text_box }, 94 .rect = null, 95 .min_size = .{ .width = name.len + 2, .height = null }, 96 }); 97 } 98 } 99 100 // quit tab 101 if (session.is_terminal) { 102 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 103 errdefer text_box.deinit(allocator); 104 text_box.getFocus().focusable = true; 105 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 106 try tab_ids.put(allocator, text_box.getFocus().id, {}); 107 try box.children.put(allocator, text_box.getFocus().id, .{ 108 .widget = .{ .text_box = text_box }, 109 .rect = null, 110 // the label is a single column; +2 for the border 111 .min_size = .{ .width = 1 + 2, .height = null }, 112 }); 113 } 114 115 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 116 // initial selected tab 117 self.getFocus().child_id = self.tab_ids.keys()[ 118 switch (session.data.current_page) { 119 .home_users => 0, 120 .home_repos => 1, 121 .home_settings => 2, 122 .home_auth => 3, 123 else => 0, 124 } 125 ]; 126 return self; 127 } 128 129 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 130 self.box.deinit(allocator); 131 self.tab_ids.deinit(allocator); 132 } 133 134 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 135 self.clearGrid(); 136 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 137 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 138 .text_box => |*x| x, 139 .auth_tab => |*at| &at.text_box, 140 else => null, 141 }; 142 if (tb) |t| { 143 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 144 } 145 } 146 try self.box.build(allocator, constraint, root_focus); 147 } 148 149 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 150 _ = allocator; 151 const current_tab = self.currentTabIndex() orelse return; 152 var new_tab = current_tab; 153 switch (key) { 154 .arrow_left => new_tab -|= 1, 155 .arrow_right => if (new_tab + 1 < self.tab_ids.count()) { 156 new_tab += 1; 157 }, 158 else => {}, 159 } 160 if (new_tab != current_tab) { 161 try root_focus.setFocus(self.tab_ids.keys()[new_tab]); 162 } 163 } 164 165 pub fn clearGrid(self: *View) void { 166 self.box.clearGrid(); 167 } 168 169 pub fn getGrid(self: View) ?Grid { 170 return self.box.getGrid(); 171 } 172 173 pub fn getFocus(self: *View) *Focus { 174 return self.box.getFocus(); 175 } 176 177 pub fn getSelectedIndex(self: View) ?usize { 178 return self.currentTabIndex(); 179 } 180 181 fn currentTabIndex(self: View) ?usize { 182 const child_id = self.box.focus.child_id orelse return null; 183 return self.tab_ids.getIndex(child_id); 184 } 185 }; 186