╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
17 const Self = @This(); 18 19 pub fn init(arena: *std.heap.ArenaAllocator) !Self { 20 return .{ 21 .title = try ui.Title.init(arena, "haxy"), 22 .auth_tab = AuthTab.init(), 23 }; 24 } 25 26 pub const View = struct { 27 box: wgt.Box(ui.Widget), 28 data: *const Self, 29 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 30 31 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 32 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 33 errdefer box.deinit(allocator); 34 35 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 36 errdefer tab_ids.deinit(allocator); 37 38 // title sits to the left of the tabs 39 { 40 var title_view = try ui.Title.View.init(allocator, &data.title); 41 errdefer title_view.deinit(allocator); 42 // shrink the title when there is not enough space 43 try box.children.put(allocator, title_view.getFocus().id, .{ 44 .widget = .{ .title = title_view }, 45 .rect = null, 46 .min_size = null, 47 .shrink = true, 48 }); 49 } 50 51 // spacer 52 { 53 var text = try wgt.Text(ui.Widget).init(allocator, " "); 54 errdefer text.deinit(allocator); 55 try box.children.put(allocator, text.getFocus().id, .{ 56 .widget = .{ .text = text }, 57 .rect = null, 58 .min_size = .{ .width = 1, .height = null }, 59 }); 60 } 61 62 // the tab matching the current page is focused initially; matching by 63 // link (rather than position) keeps this robust to tab changes. 64 const current_link: []const u8 = switch (session.data.current_page) { 65 .home_users => "ai:/users", 66 .home_settings => "ai:/settings", 67 .home_auth => "ai:/auth", 68 else => "ai:/repos", 69 }; 70 var selected_tab: ?usize = null; 71 72 // repos tab 73 { 74 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "repos", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 75 errdefer text_box.deinit(allocator); 76 text_box.getFocus().focusable = true; 77 text_box.getFocus().kind = .{ .custom = "ai:/repos" }; 78 try tab_ids.put(allocator, text_box.getFocus().id, {}); 79 if (std.mem.eql(u8, "ai:/repos", current_link)) selected_tab = text_box.getFocus().id; 80 try box.children.put(allocator, text_box.getFocus().id, .{ 81 .widget = .{ .text_box = text_box }, 82 .rect = null, 83 .min_size = .{ .width = "repos".len + 2, .height = null }, 84 }); 85 } 86 87 // users tab 88 { 89 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "users", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 90 errdefer text_box.deinit(allocator); 91 text_box.getFocus().focusable = true; 92 text_box.getFocus().kind = .{ .custom = "ai:/users" }; 93 try tab_ids.put(allocator, text_box.getFocus().id, {}); 94 if (std.mem.eql(u8, "ai:/users", current_link)) selected_tab = text_box.getFocus().id; 95 try box.children.put(allocator, text_box.getFocus().id, .{ 96 .widget = .{ .text_box = text_box }, 97 .rect = null, 98 .min_size = .{ .width = "users".len + 2, .height = null }, 99 }); 100 } 101 102 // spacer pushes settings + auth to the right 103 { 104 var spacer = try ui.Spacer.init(allocator); 105 errdefer spacer.deinit(allocator); 106 try box.children.put(allocator, spacer.getFocus().id, .{ 107 .widget = .{ .spacer = spacer }, 108 .rect = null, 109 .min_size = null, 110 }); 111 } 112 113 // settings tab. settings are account preferences, so it needs a login. 114 if (session.data.user_id != null) { 115 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 116 errdefer text_box.deinit(allocator); 117 text_box.getFocus().focusable = true; 118 text_box.getFocus().kind = .{ .custom = "ai:/settings" }; 119 try tab_ids.put(allocator, text_box.getFocus().id, {}); 120 if (std.mem.eql(u8, "ai:/settings", current_link)) selected_tab = text_box.getFocus().id; 121 try box.children.put(allocator, text_box.getFocus().id, .{ 122 .widget = .{ .text_box = text_box }, 123 .rect = null, 124 .min_size = .{ .width = "settings".len + 2, .height = null }, 125 }); 126 } 127 128 // auth tab (login / logout) 129 { 130 var auth_tab = try AuthTab.View.init(allocator, session); 131 errdefer auth_tab.deinit(allocator); 132 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 133 if (std.mem.eql(u8, "ai:/auth", current_link)) selected_tab = auth_tab.getFocus().id; 134 try box.children.put(allocator, auth_tab.getFocus().id, .{ 135 .widget = .{ .auth_tab = auth_tab }, 136 .rect = null, 137 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 138 }); 139 } 140 141 // quit tab 142 if (session.is_terminal) { 143 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 144 errdefer text_box.deinit(allocator); 145 text_box.getFocus().focusable = true; 146 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 147 try tab_ids.put(allocator, text_box.getFocus().id, {}); 148 try box.children.put(allocator, text_box.getFocus().id, .{ 149 .widget = .{ .text_box = text_box }, 150 .rect = null, 151 // the label is a single column; +2 for the border 152 .min_size = .{ .width = 1 + 2, .height = null }, 153 }); 154 } 155 156 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 157 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 158 return self; 159 } 160 161 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 162 self.box.deinit(allocator); 163 self.tab_ids.deinit(allocator); 164 } 165 166 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 167 self.clearGrid(); 168 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 169 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 170 .text_box => |*x| x, 171 .auth_tab => |*at| blk: { 172 // the label tracks login state per frame, so the width must too 173 child.min_size = .{ .width = at.minWidth(), .height = null }; 174 break :blk &at.text_box; 175 }, 176 else => null, 177 }; 178 if (tb) |t| { 179 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 180 } 181 } 182 try self.box.build(allocator, constraint, root_focus); 183 } 184 185 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 186 _ = allocator; 187 const current_tab = self.currentTabIndex() orelse return; 188 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 189 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 190 } 191 } 192 193 pub fn clearGrid(self: *View) void { 194 self.box.clearGrid(); 195 } 196 197 pub fn getGrid(self: View) ?Grid { 198 return self.box.getGrid(); 199 } 200 201 pub fn getFocus(self: *View) *Focus { 202 return self.box.getFocus(); 203 } 204 205 pub fn getSelectedIndex(self: View) ?usize { 206 return self.currentTabIndex(); 207 } 208 209 fn currentTabIndex(self: View) ?usize { 210 const child_id = self.box.focus.child_id orelse return null; 211 return self.tab_ids.getIndex(child_id); 212 } 213 }; 214