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