╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
92 } 93 94 // every tab link is user-scoped so selecting one stays on this page — 95 // switching its stack and updating the url — instead of navigating to 96 // the global new repo or auth pages. the "ai:" prefix makes each an 97 // in-page anchor: crossPageLink ignores it so a wasm click just switches 98 // tabs (the page already holds every tab's content), while the href is 99 // still followed with js off. allocated in the page arena so the focus 100 // kinds that borrow them live as long as this page's widget tree. 101 const aa = session.page_arena.allocator(); 102 const current_page = session.data.current_page; 103 const current_tag = std.meta.activeTag(current_page); 104 const current_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try current_page.toUrl(session.page_arena)}); 105 const repos_link = if (current_tag == .user_repos) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}", .{data.name}); 106 const forks_link = if (current_tag == .user_forks) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/forks", .{data.name}); 107 const new_repo_link = if (current_tag == .user_new_repo) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/new-repo", .{data.name}); 108 const new_user_link = if (current_tag == .user_new_user) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/new-user", .{data.name}); 109 const auth_link = if (current_tag == .user_auth) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/auth", .{data.name}); 110 111 // the tab matching the current page is focused initially; matching by 112 // link (rather than position) keeps this robust to tab changes. 113 const selected_link: []const u8 = switch (current_page) { 114 .user_new_repo => new_repo_link, 115 .user_new_user => new_user_link, 116 .user_auth => auth_link, 117 .user_forks => forks_link, 118 else => repos_link, 119 }; 120 var selected_tab: ?usize = null; 121 122 // repos tab 123 { 124 var text_box = try wgt.TextBox.init(allocator, repos_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 125 errdefer text_box.deinit(allocator); 126 text_box.getFocus().mode = .all; 127 text_box.getFocus().kind = .{ .custom = repos_link }; 128 try tab_ids.put(allocator, text_box.getFocus().id, {}); 129 if (std.mem.eql(u8, repos_link, selected_link)) selected_tab = text_box.getFocus().id; 130 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 131 .widget = .{ .text_box = text_box }, 132 .rect = null, 133 .min_size = .{ .width = repos_tab_label.len + 2, .height = null }, 134 }); 135 } 136 137 // forks tab 138 { 139 var text_box = try wgt.TextBox.init(allocator, forks_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 140 errdefer text_box.deinit(allocator); 141 text_box.getFocus().mode = .all; 142 text_box.getFocus().kind = .{ .custom = forks_link }; 143 try tab_ids.put(allocator, text_box.getFocus().id, {}); 144 if (std.mem.eql(u8, forks_link, selected_link)) selected_tab = text_box.getFocus().id; 145 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 146 .widget = .{ .text_box = text_box }, 147 .rect = null, 148 .min_size = .{ .width = forks_tab_label.len + 2, .height = null }, 149 }); 150 } 151 152 // spacer pushes new repo, new user, and auth to the right 153 { 154 var spacer = try ui.widget.Spacer.init(allocator); 155 errdefer spacer.deinit(allocator); 156 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ 157 .widget = .{ .spacer = spacer }, 158 .rect = null, 159 .min_size = null, 160 .flex = .grow, 161 }); 162 } 163 164 // new repo tab with a login, new user tab without one 165 if (session.data.user_id != null) { 166 var text_box = try wgt.TextBox.init(allocator, new_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = new_repo_tab_bottom_label }); 167 errdefer text_box.deinit(allocator); 168 text_box.getFocus().mode = .all; 169 text_box.getFocus().kind = .{ .custom = new_repo_link }; 170 try tab_ids.put(allocator, text_box.getFocus().id, {}); 171 if (std.mem.eql(u8, new_repo_link, selected_link)) selected_tab = text_box.getFocus().id; 172 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 173 .widget = .{ .text_box = text_box }, 174 .rect = null, 175 .min_size = .{ .width = @max(new_tab_label.len, new_repo_tab_bottom_label.len) + 2, .height = null }, 176 }); 177 } else { 178 var text_box = try wgt.TextBox.init(allocator, new_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = new_user_tab_bottom_label }); 179 errdefer text_box.deinit(allocator); 180 text_box.getFocus().mode = .all; 181 text_box.getFocus().kind = .{ .custom = new_user_link }; 182 try tab_ids.put(allocator, text_box.getFocus().id, {}); 183 if (std.mem.eql(u8, new_user_link, selected_link)) selected_tab = text_box.getFocus().id; 184 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 185 .widget = .{ .text_box = text_box }, 186 .rect = null, 187 .min_size = .{ .width = @max(new_tab_label.len, new_user_tab_bottom_label.len) + 2, .height = null }, 188 }); 189 } 190 191 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 192 // link; repoint its instance at this user's auth route so it stays local. 193 { 194 var auth_tab = try AuthTab.View.init(allocator, session); 195 errdefer auth_tab.deinit(allocator); 196 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 197 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 198 if (std.mem.eql(u8, auth_link, selected_link)) selected_tab = auth_tab.getFocus().id; 199 try tabs_box.children.put(allocator, auth_tab.getFocus().id, .{ 200 .widget = .{ .auth_tab = auth_tab }, 201 .rect = null, 202 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 203 }); 204 } 205 206 // quit tab 207 if (session.is_terminal) { 208 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 209 errdefer text_box.deinit(allocator); 210 text_box.getFocus().mode = .all; 211 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 212 try tab_ids.put(allocator, text_box.getFocus().id, {}); 213 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 214 .widget = .{ .text_box = text_box }, 215 .rect = null, 216 // the label is a single column; +2 for the border 217 .min_size = .{ .width = 1 + 2, .height = null }, 218 }); 219 } 220 221 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 222 const title_id = title_box.getFocus().id; 223 const tabs_id = tabs_box.getFocus().id; 224 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 225 title_box_owned = true; 226 // a blank row between the title and the tabs, shown only when they wrap 227 const gap_id = blk: { 228 var gap = try wgt.Text.init(allocator, " "); 229 errdefer gap.deinit(allocator); 230 const id = gap.getFocus().id; 231 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 232 break :blk id; 233 }; 234 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 235 tabs_box_owned = true; 236 box.getFocus().child_id = tabs_id; 237 238 return .{ 239 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 240 .tab_ids = tab_ids, 241 .tabs_id = tabs_id, 242 .gap_id = gap_id, 243 .first_group_width = first_group_width, 244 .session = session, 245 }; 246 } 247 248 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 249 self.scroll.deinit(allocator); 250 self.tab_ids.deinit(allocator); 251 } 252 253 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 254 self.clearGrid(); 255 const box = &self.scroll.child.box; 256 const back_visible = self.session.back == .available; 257 ui.widget.setBackButtonVisible(box, back_visible); 258 const tabs_child = self.tabsChild(); 259 const tabs_box = &tabs_child.widget.box; 260 261 // only the selected tab shows its border 262 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 263 var tabs_width: usize = 0; 264 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 265 const tb: ?*wgt.TextBox = switch (child.widget) { 266 .text_box => |*x| x, 267 .auth_tab => |*at| blk: { 268 // the label tracks login state per frame, so the width must too 269 child.min_size = .{ .width = at.minWidth(), .height = null }; 270 break :blk &at.text_box; 271 }, 272 else => null, 273 }; 274 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 275 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 276 } 277 278 // the outer box's hidden border occupies two columns. keep the title 279 // and tabs together if they fit; otherwise tab strip on its own row. 280 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 281 const content_width = if (viewport_width) |width| width -| 2 else null; 282 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 283 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 284 box.options.direction = if (wrap) .vert else .horiz; 285 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 286 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 287 288 var scroll_constraint = constraint; 289 scroll_constraint.min_size.width = viewport_width; 290 try self.scroll.build(allocator, scroll_constraint, root_focus); 291 } 292 293 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 294 _ = allocator; 295 const current_tab = self.currentTabIndex() orelse return; 296 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 297 const tab_id = self.tab_ids.keys()[new_tab]; 298 root_focus.setFocus(tab_id); 299 const tabs_child = self.tabsChild(); 300 const tabs_rect = tabs_child.rect orelse return; 301 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 302 var rect = tab.rect orelse return; 303 rect.x += tabs_rect.x; 304 rect.y += tabs_rect.y; 305 self.scroll.scrollToRect(rect); 306 } 307 } 308 309 pub fn clearGrid(self: *View) void { 310 self.scroll.clearGrid(); 311 } 312 313 pub fn getGrid(self: View) ?Grid { 314 return self.scroll.getGrid(); 315 } 316 317 pub fn getFocus(self: *View) *Focus { 318 return self.scroll.getFocus(); 319 } 320 321 pub fn getSelectedIndex(self: View) ?usize { 322 return self.currentTabIndex(); 323 } 324 325 fn currentTabIndex(self: View) ?usize { 326 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 327 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 328 return self.tab_ids.getIndex(child_id); 329 } 330 331 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 332 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 333 } 334 }; 335