╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
60 .rect = null, 61 .min_size = .{ .width = 1, .height = null }, 62 }); 63 } 64 65 // every tab link is user-scoped so selecting one stays on this page — 66 // switching its stack and updating the url — instead of navigating to 67 // the global settings or auth pages. the "ai:" prefix makes each an 68 // in-page anchor: crossPageLink ignores it so a wasm click just switches 69 // tabs (the page already holds every tab's content), while the href is 70 // still followed with js off. allocated in the page arena so the focus 71 // kinds that borrow them live as long as this page's widget tree. 72 const aa = session.page_arena.allocator(); 73 const current_page = session.data.current_page; 74 const current_tag = std.meta.activeTag(current_page); 75 const current_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try current_page.toUrl(session.page_arena)}); 76 const repos_link = if (current_tag == .user_repos) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}", .{data.name}); 77 const forks_link = if (current_tag == .user_forks) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/forks", .{data.name}); 78 const settings_link = if (current_tag == .user_settings) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/settings", .{data.name}); 79 const auth_link = if (current_tag == .user_auth) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/auth", .{data.name}); 80 81 // the tab matching the current page is focused initially; matching by 82 // link (rather than position) keeps this robust to tab changes. 83 const selected_link: []const u8 = switch (current_page) { 84 .user_settings => settings_link, 85 .user_auth => auth_link, 86 .user_forks => forks_link, 87 else => repos_link, 88 }; 89 var selected_tab: ?usize = null; 90 91 // repos tab 92 { 93 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "repos", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 94 errdefer text_box.deinit(allocator); 95 text_box.getFocus().focusable = true; 96 text_box.getFocus().kind = .{ .custom = repos_link }; 97 try tab_ids.put(allocator, text_box.getFocus().id, {}); 98 if (std.mem.eql(u8, repos_link, selected_link)) selected_tab = text_box.getFocus().id; 99 try box.children.put(allocator, text_box.getFocus().id, .{ 100 .widget = .{ .text_box = text_box }, 101 .rect = null, 102 .min_size = .{ .width = "repos".len + 2, .height = null }, 103 }); 104 } 105 106 // forks tab 107 { 108 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "forks", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 109 errdefer text_box.deinit(allocator); 110 text_box.getFocus().focusable = true; 111 text_box.getFocus().kind = .{ .custom = forks_link }; 112 try tab_ids.put(allocator, text_box.getFocus().id, {}); 113 if (std.mem.eql(u8, forks_link, selected_link)) selected_tab = text_box.getFocus().id; 114 try box.children.put(allocator, text_box.getFocus().id, .{ 115 .widget = .{ .text_box = text_box }, 116 .rect = null, 117 .min_size = .{ .width = "forks".len + 2, .height = null }, 118 }); 119 } 120 121 // spacer pushes settings + auth to the right 122 { 123 var spacer = try ui.widget.Spacer.init(allocator); 124 errdefer spacer.deinit(allocator); 125 try box.children.put(allocator, spacer.getFocus().id, .{ 126 .widget = .{ .spacer = spacer }, 127 .rect = null, 128 .min_size = null, 129 .flex = .grow, 130 }); 131 } 132 133 // settings tab. settings are account preferences, so it needs a login. 134 if (session.data.user_id != null) { 135 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 136 errdefer text_box.deinit(allocator); 137 text_box.getFocus().focusable = true; 138 text_box.getFocus().kind = .{ .custom = settings_link }; 139 try tab_ids.put(allocator, text_box.getFocus().id, {}); 140 if (std.mem.eql(u8, settings_link, selected_link)) selected_tab = text_box.getFocus().id; 141 try box.children.put(allocator, text_box.getFocus().id, .{ 142 .widget = .{ .text_box = text_box }, 143 .rect = null, 144 .min_size = .{ .width = "settings".len + 2, .height = null }, 145 }); 146 } 147 148 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 149 // link; repoint its instance at this user's auth route so it stays local. 150 { 151 var auth_tab = try AuthTab.View.init(allocator, session); 152 errdefer auth_tab.deinit(allocator); 153 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 154 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 155 if (std.mem.eql(u8, auth_link, selected_link)) selected_tab = auth_tab.getFocus().id; 156 try box.children.put(allocator, auth_tab.getFocus().id, .{ 157 .widget = .{ .auth_tab = auth_tab }, 158 .rect = null, 159 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 160 }); 161 } 162 163 // quit tab 164 if (session.is_terminal) { 165 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 166 errdefer text_box.deinit(allocator); 167 text_box.getFocus().focusable = true; 168 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 169 try tab_ids.put(allocator, text_box.getFocus().id, {}); 170 try box.children.put(allocator, text_box.getFocus().id, .{ 171 .widget = .{ .text_box = text_box }, 172 .rect = null, 173 // the label is a single column; +2 for the border 174 .min_size = .{ .width = 1 + 2, .height = null }, 175 }); 176 } 177 178 var self = View{ 179 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 180 .tab_ids = tab_ids, 181 }; 182 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 183 return self; 184 } 185 186 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 187 self.scroll.deinit(allocator); 188 self.tab_ids.deinit(allocator); 189 } 190 191 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 192 self.clearGrid(); 193 // only the selected tab shows its border 194 const box = &self.scroll.child.box; 195 for (box.children.keys(), box.children.values()) |id, *child| { 196 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 197 .text_box => |*x| x, 198 .auth_tab => |*at| blk: { 199 // the label tracks login state per frame, so the width must too 200 child.min_size = .{ .width = at.minWidth(), .height = null }; 201 break :blk &at.text_box; 202 }, 203 else => null, 204 }; 205 if (tb) |t| { 206 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 207 } 208 } 209 var scroll_constraint = constraint; 210 scroll_constraint.min_size.width = constraint.max_size.width orelse constraint.min_size.width; 211 try self.scroll.build(allocator, scroll_constraint, root_focus); 212 } 213 214 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 215 _ = allocator; 216 const current_tab = self.currentTabIndex() orelse return; 217 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 218 const tab_id = self.tab_ids.keys()[new_tab]; 219 root_focus.setFocus(tab_id); 220 const tab = self.scroll.child.box.children.get(tab_id) orelse return; 221 if (tab.rect) |rect| self.scroll.scrollToRect(rect); 222 } 223 } 224 225 pub fn clearGrid(self: *View) void { 226 self.scroll.clearGrid(); 227 } 228 229 pub fn getGrid(self: View) ?Grid { 230 return self.scroll.getGrid(); 231 } 232 233 pub fn getFocus(self: *View) *Focus { 234 return self.scroll.getFocus(); 235 } 236 237 pub fn getSelectedIndex(self: View) ?usize { 238 return self.currentTabIndex(); 239 } 240 241 fn currentTabIndex(self: View) ?usize { 242 const child_id = self.scroll.child.box.focus.child_id orelse return null; 243 return self.tab_ids.getIndex(child_id); 244 } 245 }; 246