╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
70 const bottom_label = try ui.clippedBottomLabel(try aa.alloc(u8, ui.clipped_bottom_label_max_len), ref_name); 71 const bottom_label_width = try xitui.width.displayWidth(bottom_label); 72 const commits_label = if (commit_count) |count| try std.fmt.allocPrint(aa, "commits ({d})", .{count}) else "commits"; 73 const commits_label_width = try xitui.width.displayWidth(commits_label); 74 var first_group_width = try data.title.width(); 75 76 try ui.widget.addBackButton(allocator, &title_box, session); 77 78 // the user's name (local mode has no user pages to link to) 79 if (session.data.host_kind == .server) { 80 var text_buf: [evt.User.name_max_len + 1]u8 = undefined; 81 const text = try std.fmt.bufPrint(&text_buf, "{s}/", .{data.owner_name}); 82 const link = try std.fmt.allocPrint(aa, "a:/user/{s}", .{data.owner_name}); 83 84 var text_box = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .wrap_kind = .none }); 85 errdefer text_box.deinit(allocator); 86 text_box.getFocus().mode = .all; 87 text_box.getFocus().kind = .{ .custom = link }; 88 first_group_width += try xitui.width.displayWidth(text) + 2; 89 try title_box.children.put(allocator, text_box.getFocus().id, .{ 90 .widget = .{ .text_box = text_box }, 91 .rect = null, 92 .min_size = null, 93 }); 94 } 95 96 // local routes carry no identity, so their urls come out elided 97 const identity = if (session.data.host_kind == .local) "" else try std.fmt.allocPrint(aa, "{s}/{s}", .{ data.owner_name, data.name }); 98 99 // title links to the repo's files root (the bare route, so it resolves 100 // to the default branch). 101 { 102 const files_root_route = ui.RoutablePage.repoFilesRoute(identity, null, "", "", 0) orelse return error.RouteTooLong; 103 const title_link = try std.fmt.allocPrint(aa, "a:{s}", .{try files_root_route.toUrl(session.page_arena)}); 104 var title_view = try ui.Title.View.init(allocator, &data.title); 105 errdefer title_view.deinit(allocator); 106 title_view.getFocus().mode = .all; 107 title_view.getFocus().kind = .{ .custom = title_link }; 108 try title_box.children.put(allocator, title_view.getFocus().id, .{ 109 .widget = .{ .title = title_view }, 110 .rect = null, 111 .min_size = null, 112 }); 113 } 114 115 // spacer 116 { 117 var text = try wgt.Text.init(allocator, " "); 118 errdefer text.deinit(allocator); 119 try tabs_box.children.put(allocator, text.getFocus().id, .{ 120 .widget = .{ .text = text }, 121 .rect = null, 122 .min_size = .{ .width = 1, .height = null }, 123 }); 124 } 125 126 // every tab link is repo-scoped so selecting one stays on this page — 127 // switching its stack and updating the url — instead of navigating to 128 // the global settings or auth pages. the "ai:" prefix makes each an 129 // in-page anchor: crossPageLink ignores it so a wasm click just switches 130 // tabs (the page already holds every tab's content), while the href is 131 // still followed with js off. the files tab routes through the shared 132 // helper so the /repo/.../files url format lives in one place. 133 // both tabs link to the ref/oid this page is viewing, so switching tabs 134 // keeps the same ref (the files tab opens at its root directory). 135 const current_page = session.data.current_page; 136 const current_tag = std.meta.activeTag(current_page); 137 const files_route = ui.RoutablePage.repoFilesRoute(identity, data.ref_or_oid, data.ref_or_oid_value, "", 0) orelse return error.RouteTooLong; 138 const files_link = try ui.inPageTabLink(session, files_route, current_tag == .repo_files); 139 const commits_route = ui.RoutablePage.repoCommitsRoute(identity, data.ref_or_oid, data.ref_or_oid_value, 0, "") orelse return error.RouteTooLong; 140 const commits_link = try ui.inPageTabLink(session, commits_route, current_tag == .repo_commits); 141 const refs_route = ui.RoutablePage.repoRefsRoute(identity, .branch, "") orelse return error.RouteTooLong; 142 const refs_link = try ui.inPageTabLink(session, refs_route, current_tag == .repo_refs); 143 const issues_route = ui.RoutablePage.repoIssuesRoute(identity, .open, data.issues_tag, "") orelse return error.RouteTooLong; 144 const issues_link = try ui.inPageTabLink(session, issues_route, current_tag == .repo_issues); 145 const patches_route = ui.RoutablePage.repoPatchesRoute(identity, .open, data.patches_tag, "") orelse return error.RouteTooLong; 146 const patches_link = try ui.inPageTabLink(session, patches_route, current_tag == .repo_patches); 147 const discussions_route = ui.RoutablePage.repoDiscussionsRoute(identity, data.discussions_tag, "") orelse return error.RouteTooLong; 148 const discussions_link = try ui.inPageTabLink(session, discussions_route, current_tag == .repo_discussions); 149 const events_route = ui.RoutablePage.repoEventsRoute(identity, .active, null, "") orelse return error.RouteTooLong; 150 const events_link = try ui.inPageTabLink(session, events_route, current_tag == .repo_events); 151 const settings_route = ui.RoutablePage{ .repo_settings = Array.from(identity) orelse return error.RouteTooLong }; 152 const settings_link = try ui.inPageTabLink(session, settings_route, current_tag == .repo_settings); 153 const auth_route = ui.RoutablePage{ .repo_auth = Array.from(identity) orelse return error.RouteTooLong }; 154 const auth_link = try ui.inPageTabLink(session, auth_route, current_tag == .repo_auth); 155 156 // the tab matching the current page is focused initially; matching by 157 // link (rather than position) keeps this robust to tab changes. 158 var selected_tab: ?usize = null; 159 160 // files tab 161 { 162 var text_box = try wgt.TextBox.init(allocator, "files", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .bottom_label = bottom_label }); 163 errdefer text_box.deinit(allocator); 164 text_box.getFocus().mode = .all; 165 text_box.getFocus().kind = .{ .custom = files_link }; 166 try tab_ids.put(allocator, text_box.getFocus().id, {}); 167 if (current_tag == .repo_files) selected_tab = text_box.getFocus().id; 168 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 169 .widget = .{ .text_box = text_box }, 170 .rect = null, 171 .min_size = .{ .width = @max("files".len, bottom_label_width) + 2, .height = null }, 172 }); 173 } 174 175 // commits tab 176 { 177 var text_box = try wgt.TextBox.init(allocator, commits_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .bottom_label = bottom_label }); 178 errdefer text_box.deinit(allocator); 179 text_box.getFocus().mode = .all; 180 text_box.getFocus().kind = .{ .custom = commits_link }; 181 try tab_ids.put(allocator, text_box.getFocus().id, {}); 182 if (current_tag == .repo_commits) selected_tab = text_box.getFocus().id; 183 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 184 .widget = .{ .text_box = text_box }, 185 .rect = null, 186 .min_size = .{ .width = @max(commits_label_width, bottom_label_width) + 2, .height = null }, 187 }); 188 } 189 190 // refs tab 191 { 192 var text_box = try wgt.TextBox.init(allocator, "refs", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 193 errdefer text_box.deinit(allocator); 194 text_box.getFocus().mode = .all; 195 text_box.getFocus().kind = .{ .custom = refs_link }; 196 try tab_ids.put(allocator, text_box.getFocus().id, {}); 197 if (current_tag == .repo_refs) 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 = "refs".len + 2, .height = null }, 202 }); 203 } 204 205 // issues tab 206 { 207 var text_box = try wgt.TextBox.init(allocator, "issues", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 208 errdefer text_box.deinit(allocator); 209 text_box.getFocus().mode = .all; 210 text_box.getFocus().kind = .{ .custom = issues_link }; 211 try tab_ids.put(allocator, text_box.getFocus().id, {}); 212 if (current_tag == .repo_issues) selected_tab = 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 .min_size = .{ .width = "issues".len + 2, .height = null }, 217 }); 218 } 219 220 // patches tab 221 { 222 var text_box = try wgt.TextBox.init(allocator, "patches", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 223 errdefer text_box.deinit(allocator); 224 text_box.getFocus().mode = .all; 225 text_box.getFocus().kind = .{ .custom = patches_link }; 226 try tab_ids.put(allocator, text_box.getFocus().id, {}); 227 if (current_tag == .repo_patches) selected_tab = text_box.getFocus().id; 228 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 229 .widget = .{ .text_box = text_box }, 230 .rect = null, 231 .min_size = .{ .width = "patches".len + 2, .height = null }, 232 }); 233 } 234 235 // discussions tab 236 { 237 var text_box = try wgt.TextBox.init(allocator, "discussions", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 238 errdefer text_box.deinit(allocator); 239 text_box.getFocus().mode = .all; 240 text_box.getFocus().kind = .{ .custom = discussions_link }; 241 try tab_ids.put(allocator, text_box.getFocus().id, {}); 242 if (current_tag == .repo_discussions) selected_tab = text_box.getFocus().id; 243 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 244 .widget = .{ .text_box = text_box }, 245 .rect = null, 246 .min_size = .{ .width = "discussions".len + 2, .height = null }, 247 }); 248 } 249 250 // events 251 { 252 var text_box = try wgt.TextBox.init(allocator, "events", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 253 errdefer text_box.deinit(allocator); 254 text_box.getFocus().mode = .all; 255 text_box.getFocus().kind = .{ .custom = events_link }; 256 try tab_ids.put(allocator, text_box.getFocus().id, {}); 257 if (current_tag == .repo_events) selected_tab = text_box.getFocus().id; 258 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 259 .widget = .{ .text_box = text_box }, 260 .rect = null, 261 .min_size = .{ .width = "events".len + 2, .height = null }, 262 }); 263 } 264 265 // spacer pushes settings + auth to the right 266 { 267 var spacer = try ui.widget.Spacer.init(allocator); 268 errdefer spacer.deinit(allocator); 269 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ 270 .widget = .{ .spacer = spacer }, 271 .rect = null, 272 .min_size = null, 273 .flex = .grow, 274 }); 275 } 276 277 // settings tab. settings are account preferences, so it needs a login 278 // (which also rules out local mode, which has no accounts). 279 if (session.data.user_id != null) { 280 var text_box = try wgt.TextBox.init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 281 errdefer text_box.deinit(allocator); 282 text_box.getFocus().mode = .all; 283 text_box.getFocus().kind = .{ .custom = settings_link }; 284 try tab_ids.put(allocator, text_box.getFocus().id, {}); 285 if (current_tag == .repo_settings) selected_tab = text_box.getFocus().id; 286 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 287 .widget = .{ .text_box = text_box }, 288 .rect = null, 289 .min_size = .{ .width = "settings".len + 2, .height = null }, 290 }); 291 } 292 293 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 294 // link; repoint its instance at this repo's auth route so it stays on 295 // this page. local mode has no accounts, so it has no auth tab. 296 if (session.data.host_kind == .server) { 297 var auth_tab = try AuthTab.View.init(allocator, session); 298 errdefer auth_tab.deinit(allocator); 299 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 300 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 301 if (current_tag == .repo_auth) selected_tab = auth_tab.getFocus().id; 302 try tabs_box.children.put(allocator, auth_tab.getFocus().id, .{ 303 .widget = .{ .auth_tab = auth_tab }, 304 .rect = null, 305 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 306 }); 307 } 308 309 // quit tab 310 if (session.is_terminal) { 311 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 312 errdefer text_box.deinit(allocator); 313 text_box.getFocus().mode = .all; 314 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 315 try tab_ids.put(allocator, text_box.getFocus().id, {}); 316 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 317 .widget = .{ .text_box = text_box }, 318 .rect = null, 319 // the label is a single column; +2 for the border 320 .min_size = .{ .width = 1 + 2, .height = null }, 321 }); 322 } 323 324 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 325 const title_id = title_box.getFocus().id; 326 const tabs_id = tabs_box.getFocus().id; 327 try box.children.put(allocator, title_id, .{ 328 .widget = .{ .box = title_box }, 329 .rect = null, 330 .min_size = null, 331 }); 332 title_box_owned = true; 333 try box.children.put(allocator, tabs_id, .{ 334 .widget = .{ .box = tabs_box }, 335 .rect = null, 336 .min_size = null, 337 }); 338 tabs_box_owned = true; 339 box.getFocus().child_id = tabs_id; 340 341 return .{ 342 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 343 .tab_ids = tab_ids, 344 .tabs_id = tabs_id, 345 .first_group_width = first_group_width, 346 .session = session, 347 }; 348 } 349 350 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 351 self.scroll.deinit(allocator); 352 self.tab_ids.deinit(allocator); 353 } 354 355 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 356 self.clearGrid(); 357 const box = &self.scroll.child.box; 358 const back_visible = self.session.back == .available; 359 ui.widget.setBackButtonVisible(box, back_visible); 360 const tabs_child = self.tabsChild(); 361 const tabs_box = &tabs_child.widget.box; 362 363 // only the selected tab shows its border 364 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 365 var tabs_width: usize = 0; 366 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 367 const tb: ?*wgt.TextBox = switch (child.widget) { 368 .text_box => |*x| x, 369 .auth_tab => |*at| blk: { 370 // the label tracks login state per frame, so the width must too 371 child.min_size = .{ .width = at.minWidth(), .height = null }; 372 break :blk &at.text_box; 373 }, 374 else => null, 375 }; 376 if (tb) |t| { 377 t.options.border_style = if (selected_tab == id) .single else .hidden; 378 } 379 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 380 } 381 382 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 383 // the outer box's hidden border occupies two columns. keep the title 384 // and tabs together if they fit; otherwise tab strip on its own row. 385 const content_width = if (viewport_width) |width| width -| 2 else null; 386 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 387 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 388 box.options.direction = if (wrap) .vert else .horiz; 389 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 390 391 var scroll_constraint = constraint; 392 scroll_constraint.min_size.width = viewport_width; 393 try self.scroll.build(allocator, scroll_constraint, root_focus); 394 } 395 396 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 397 _ = allocator; 398 const current_tab = self.currentTabIndex() orelse return; 399 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 400 const tab_id = self.tab_ids.keys()[new_tab]; 401 root_focus.setFocus(tab_id); 402 const tabs_child = self.tabsChild(); 403 const tabs_rect = tabs_child.rect orelse return; 404 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 405 var rect = tab.rect orelse return; 406 rect.x += tabs_rect.x; 407 rect.y += tabs_rect.y; 408 self.scroll.scrollToRect(rect); 409 } 410 } 411 412 pub fn clearGrid(self: *View) void { 413 self.scroll.clearGrid(); 414 } 415 416 pub fn getGrid(self: View) ?Grid { 417 return self.scroll.getGrid(); 418 } 419 420 pub fn getFocus(self: *View) *Focus { 421 return self.scroll.getFocus(); 422 } 423 424 pub fn getSelectedIndex(self: View) ?usize { 425 return self.currentTabIndex(); 426 } 427 428 fn currentTabIndex(self: View) ?usize { 429 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 430 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 431 return self.tab_ids.getIndex(child_id); 432 } 433 434 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 435 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 436 } 437 }; 438