╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 .flex = .grow, 111 }); 112 } 113 114 // settings tab. settings are account preferences, so it needs a login. 115 if (session.data.user_id != null) { 116 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .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 = "ai:/settings" }; 120 try tab_ids.put(allocator, text_box.getFocus().id, {}); 121 if (std.mem.eql(u8, "ai:/settings", current_link)) selected_tab = text_box.getFocus().id; 122 try box.children.put(allocator, text_box.getFocus().id, .{ 123 .widget = .{ .text_box = text_box }, 124 .rect = null, 125 .min_size = .{ .width = "settings".len + 2, .height = null }, 126 }); 127 } 128 129 // auth tab (login / logout) 130 { 131 var auth_tab = try AuthTab.View.init(allocator, session); 132 errdefer auth_tab.deinit(allocator); 133 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 134 if (std.mem.eql(u8, "ai:/auth", current_link)) selected_tab = auth_tab.getFocus().id; 135 try box.children.put(allocator, auth_tab.getFocus().id, .{ 136 .widget = .{ .auth_tab = auth_tab }, 137 .rect = null, 138 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 139 }); 140 } 141 142 // quit tab 143 if (session.is_terminal) { 144 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 145 errdefer text_box.deinit(allocator); 146 text_box.getFocus().focusable = true; 147 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 148 try tab_ids.put(allocator, text_box.getFocus().id, {}); 149 try box.children.put(allocator, text_box.getFocus().id, .{ 150 .widget = .{ .text_box = text_box }, 151 .rect = null, 152 // the label is a single column; +2 for the border 153 .min_size = .{ .width = 1 + 2, .height = null }, 154 }); 155 } 156 157 var self = View{ 158 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 159 .data = data, 160 .tab_ids = tab_ids, 161 }; 162 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 163 return self; 164 } 165 166 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 167 self.scroll.deinit(allocator); 168 self.tab_ids.deinit(allocator); 169 } 170 171 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 172 self.clearGrid(); 173 const box = &self.scroll.child.box; 174 for (box.children.keys(), box.children.values()) |id, *child| { 175 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 176 .text_box => |*x| x, 177 .auth_tab => |*at| blk: { 178 // the label tracks login state per frame, so the width must too 179 child.min_size = .{ .width = at.minWidth(), .height = null }; 180 break :blk &at.text_box; 181 }, 182 else => null, 183 }; 184 if (tb) |t| { 185 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 186 } 187 } 188 var scroll_constraint = constraint; 189 scroll_constraint.min_size.width = constraint.max_size.width orelse constraint.min_size.width; 190 try self.scroll.build(allocator, scroll_constraint, root_focus); 191 } 192 193 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 194 _ = allocator; 195 const current_tab = self.currentTabIndex() orelse return; 196 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 197 const tab_id = self.tab_ids.keys()[new_tab]; 198 root_focus.setFocus(tab_id); 199 const tab = self.scroll.child.box.children.get(tab_id) orelse return; 200 if (tab.rect) |rect| self.scroll.scrollToRect(rect); 201 } 202 } 203 204 pub fn clearGrid(self: *View) void { 205 self.scroll.clearGrid(); 206 } 207 208 pub fn getGrid(self: View) ?Grid { 209 return self.scroll.getGrid(); 210 } 211 212 pub fn getFocus(self: *View) *Focus { 213 return self.scroll.getFocus(); 214 } 215 216 pub fn getSelectedIndex(self: View) ?usize { 217 return self.currentTabIndex(); 218 } 219 220 fn currentTabIndex(self: View) ?usize { 221 const child_id = self.scroll.child.box.focus.child_id orelse return null; 222 return self.tab_ids.getIndex(child_id); 223 } 224 }; 225