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