╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 = try wgt.Box(ui.Widget).init(allocator, .{ .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 // shrink the title when there is not enough space 42 try box.children.put(allocator, title_view.getFocus().id, .{ 43 .widget = .{ .title = title_view }, 44 .rect = null, 45 .min_size = null, 46 .shrink = true, 47 }); 48 } 49 50 // spacer 51 { 52 var text = try wgt.Text(ui.Widget).init(allocator, " "); 53 errdefer text.deinit(allocator); 54 try box.children.put(allocator, text.getFocus().id, .{ 55 .widget = .{ .text = text }, 56 .rect = null, 57 .min_size = .{ .width = 1, .height = null }, 58 }); 59 } 60 61 // the tab matching the current page is focused initially; matching by 62 // link (rather than position) keeps this robust to tab changes. 63 const current_link: []const u8 = switch (session.data.current_page) { 64 .home_repos => "ai:/repos", 65 .home_settings => "ai:/settings", 66 .home_auth => "ai:/auth", 67 else => "ai:/users", 68 }; 69 var selected_tab: ?usize = null; 70 71 // tabs 72 for ( 73 [_][]const u8{ "users", "repos", "", "settings", "" }, 74 [_][]const u8{ "ai:/users", "ai:/repos", "", "ai:/settings", "ai:/auth" }, 75 ) |name, focus_name| { 76 // spacer 77 if (focus_name.len == 0) { 78 var spacer = try ui.Spacer.init(allocator); 79 errdefer spacer.deinit(allocator); 80 try box.children.put(allocator, spacer.getFocus().id, .{ 81 .widget = .{ .spacer = spacer }, 82 .rect = null, 83 .min_size = null, 84 }); 85 } 86 // auth 87 else if (std.mem.eql(u8, "ai:/auth", focus_name)) { 88 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 89 errdefer auth_tab.deinit(allocator); 90 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 91 if (std.mem.eql(u8, focus_name, current_link)) selected_tab = auth_tab.getFocus().id; 92 try box.children.put(allocator, auth_tab.getFocus().id, .{ 93 .widget = .{ .auth_tab = auth_tab }, 94 .rect = null, 95 .min_size = .{ .width = AuthTab.min_width, .height = null }, 96 }); 97 } 98 // other tabs 99 else { 100 var text_box = try wgt.TextBox(ui.Widget).init(allocator, name, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 101 errdefer text_box.deinit(allocator); 102 text_box.getFocus().focusable = true; 103 text_box.getFocus().kind = .{ .custom = focus_name }; 104 try tab_ids.put(allocator, text_box.getFocus().id, {}); 105 if (std.mem.eql(u8, focus_name, current_link)) selected_tab = text_box.getFocus().id; 106 try box.children.put(allocator, text_box.getFocus().id, .{ 107 .widget = .{ .text_box = text_box }, 108 .rect = null, 109 .min_size = .{ .width = name.len + 2, .height = null }, 110 }); 111 } 112 } 113 114 // quit tab 115 if (session.is_terminal) { 116 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 117 errdefer text_box.deinit(allocator); 118 text_box.getFocus().focusable = true; 119 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 120 try tab_ids.put(allocator, text_box.getFocus().id, {}); 121 try box.children.put(allocator, text_box.getFocus().id, .{ 122 .widget = .{ .text_box = text_box }, 123 .rect = null, 124 // the label is a single column; +2 for the border 125 .min_size = .{ .width = 1 + 2, .height = null }, 126 }); 127 } 128 129 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 130 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 131 return self; 132 } 133 134 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 135 self.box.deinit(allocator); 136 self.tab_ids.deinit(allocator); 137 } 138 139 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 140 self.clearGrid(); 141 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 142 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 143 .text_box => |*x| x, 144 .auth_tab => |*at| &at.text_box, 145 else => null, 146 }; 147 if (tb) |t| { 148 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 149 } 150 } 151 try self.box.build(allocator, constraint, root_focus); 152 } 153 154 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 155 _ = allocator; 156 const current_tab = self.currentTabIndex() orelse return; 157 var new_tab = current_tab; 158 switch (key) { 159 .arrow_left => new_tab -|= 1, 160 .arrow_right => if (new_tab + 1 < self.tab_ids.count()) { 161 new_tab += 1; 162 }, 163 else => {}, 164 } 165 if (new_tab != current_tab) { 166 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 167 } 168 } 169 170 pub fn clearGrid(self: *View) void { 171 self.box.clearGrid(); 172 } 173 174 pub fn getGrid(self: View) ?Grid { 175 return self.box.getGrid(); 176 } 177 178 pub fn getFocus(self: *View) *Focus { 179 return self.box.getFocus(); 180 } 181 182 pub fn getSelectedIndex(self: View) ?usize { 183 return self.currentTabIndex(); 184 } 185 186 fn currentTabIndex(self: View) ?usize { 187 const child_id = self.box.focus.child_id orelse return null; 188 return self.tab_ids.getIndex(child_id); 189 } 190 }; 191