╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
148 const current_page = session.data.current_page; 149 const current_tag = std.meta.activeTag(current_page); 150 const files_route = page.files.filesRoute("", 0) orelse return error.RouteTooLong; 151 const files_link = try ui.inPageTabLink(session, files_route, current_tag == .repo_files); 152 const changes_route = (switch (page.changes) { 153 .diff => |d| d.route.page(d.window.start, d.path), 154 .commits => |c| ui.RoutablePage.repoCommitsRoute(identity, data.ref_or_oid, data.ref_or_oid_value, 0, "", c.base_oid), 155 }) orelse return error.RouteTooLong; 156 const changes_tag = std.meta.activeTag(changes_route); 157 const changes_link = try ui.inPageTabLink(session, changes_route, current_tag == changes_tag); 158 const refs_route = ui.RoutablePage.repoRefsRoute(identity, .branch, "") orelse return error.RouteTooLong; 159 const refs_link = try ui.inPageTabLink(session, refs_route, current_tag == .repo_refs); 160 const issues_route = ui.RoutablePage.repoIssuesRoute(identity, .open, data.issues_tag, "") orelse return error.RouteTooLong; 161 const issues_link = try ui.inPageTabLink(session, issues_route, current_tag == .repo_issues); 162 const patches_route = ui.RoutablePage.repoPatchesRoute(identity, .open, data.patches_tag, "") orelse return error.RouteTooLong; 163 const patches_link = try ui.inPageTabLink(session, patches_route, current_tag == .repo_patches); 164 const discussions_route = ui.RoutablePage.repoDiscussionsRoute(identity, data.discussions_tag, "") orelse return error.RouteTooLong; 165 const discussions_link = try ui.inPageTabLink(session, discussions_route, current_tag == .repo_discussions); 166 const events_route = ui.RoutablePage.repoEventsRoute(identity, .active, null, "", null) orelse return error.RouteTooLong; 167 const events_link = try ui.inPageTabLink(session, events_route, current_tag == .repo_events); 168 const settings_route = ui.RoutablePage{ .repo_settings = Array.from(identity) orelse return error.RouteTooLong }; 169 const settings_link = try ui.inPageTabLink(session, settings_route, current_tag == .repo_settings); 170 const auth_route = ui.RoutablePage{ .repo_auth = Array.from(identity) orelse return error.RouteTooLong }; 171 const auth_link = try ui.inPageTabLink(session, auth_route, current_tag == .repo_auth); 172 173 // the tab matching the current page is focused initially; matching by 174 // link (rather than position) keeps this robust to tab changes. 175 var selected_tab: ?usize = null; 176 177 // files tab 178 { 179 var text_box = try wgt.TextBox.init(allocator, files_tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .bottom_label = bottom_label }); 180 errdefer text_box.deinit(allocator); 181 text_box.getFocus().mode = .all; 182 text_box.getFocus().kind = .{ .custom = files_link }; 183 try tab_ids.put(allocator, text_box.getFocus().id, {}); 184 if (current_tag == .repo_files) selected_tab = text_box.getFocus().id; 185 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 186 .widget = .{ .text_box = text_box }, 187 .rect = null, 188 .min_size = .{ .width = @max(files_tab_label.len, bottom_label_width) + 2, .height = null }, 189 }); 190 } 191 192 // commits, or the comparison selected by a diff / patchrev url 193 { 194 var text_box = try wgt.TextBox.init(allocator, changes_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .bottom_label = bottom_label }); 195 errdefer text_box.deinit(allocator); 196 text_box.getFocus().mode = .all; 197 text_box.getFocus().kind = .{ .custom = changes_link }; 198 try tab_ids.put(allocator, text_box.getFocus().id, {}); 199 if (current_tag == changes_tag) selected_tab = text_box.getFocus().id; 200 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 201 .widget = .{ .text_box = text_box }, 202 .rect = null, 203 .min_size = .{ .width = @max(changes_label_width, bottom_label_width) + 2, .height = null }, 204 }); 205 } 206 207 // refs tab 208 { 209 var text_box = try wgt.TextBox.init(allocator, refs_tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 210 errdefer text_box.deinit(allocator); 211 text_box.getFocus().mode = .all; 212 text_box.getFocus().kind = .{ .custom = refs_link }; 213 try tab_ids.put(allocator, text_box.getFocus().id, {}); 214 if (current_tag == .repo_refs) selected_tab = text_box.getFocus().id; 215 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 216 .widget = .{ .text_box = text_box }, 217 .rect = null, 218 .min_size = .{ .width = refs_tab_label.len + 2, .height = null }, 219 }); 220 } 221 222 // issues tab 223 { 224 var text_box = try wgt.TextBox.init(allocator, issues_tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 225 errdefer text_box.deinit(allocator); 226 text_box.getFocus().mode = .all; 227 text_box.getFocus().kind = .{ .custom = issues_link }; 228 try tab_ids.put(allocator, text_box.getFocus().id, {}); 229 if (current_tag == .repo_issues) selected_tab = text_box.getFocus().id; 230 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 231 .widget = .{ .text_box = text_box }, 232 .rect = null, 233 .min_size = .{ .width = issues_tab_label.len + 2, .height = null }, 234 }); 235 } 236 237 // patches tab 238 { 239 var text_box = try wgt.TextBox.init(allocator, patches_tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 240 errdefer text_box.deinit(allocator); 241 text_box.getFocus().mode = .all; 242 text_box.getFocus().kind = .{ .custom = patches_link }; 243 try tab_ids.put(allocator, text_box.getFocus().id, {}); 244 if (current_tag == .repo_patches) selected_tab = text_box.getFocus().id; 245 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 246 .widget = .{ .text_box = text_box }, 247 .rect = null, 248 .min_size = .{ .width = patches_tab_label.len + 2, .height = null }, 249 }); 250 } 251 252 // discuss tab 253 { 254 var text_box = try wgt.TextBox.init(allocator, discuss_tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 255 errdefer text_box.deinit(allocator); 256 text_box.getFocus().mode = .all; 257 text_box.getFocus().kind = .{ .custom = discussions_link }; 258 try tab_ids.put(allocator, text_box.getFocus().id, {}); 259 if (current_tag == .repo_discussions) selected_tab = text_box.getFocus().id; 260 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 261 .widget = .{ .text_box = text_box }, 262 .rect = null, 263 .min_size = .{ .width = discuss_tab_label.len + 2, .height = null }, 264 }); 265 } 266 267 // events, which a server only shows to whoever it shows undo to 268 if (session.data.host_kind == .local or page.undo != null) { 269 var text_box = try wgt.TextBox.init(allocator, events_tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 270 errdefer text_box.deinit(allocator); 271 text_box.getFocus().mode = .all; 272 text_box.getFocus().kind = .{ .custom = events_link }; 273 try tab_ids.put(allocator, text_box.getFocus().id, {}); 274 if (current_tag == .repo_events) selected_tab = text_box.getFocus().id; 275 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 276 .widget = .{ .text_box = text_box }, 277 .rect = null, 278 .min_size = .{ .width = events_tab_label.len + 2, .height = null }, 279 }); 280 } 281 282 if (page.undo != null) { 283 const route = ui.RoutablePage.repoUndoRoute(identity, null) orelse return error.RouteTooLong; 284 var text_box = try wgt.TextBox.init(allocator, "undo", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 285 errdefer text_box.deinit(allocator); 286 text_box.getFocus().mode = .all; 287 text_box.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, current_tag == .repo_undo) }; 288 try tab_ids.put(allocator, text_box.getFocus().id, {}); 289 if (current_tag == .repo_undo) selected_tab = text_box.getFocus().id; 290 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 291 .widget = .{ .text_box = text_box }, 292 .rect = null, 293 .min_size = .{ .width = 6, .height = null }, 294 }); 295 } 296 297 // spacer pushes settings + auth to the right 298 { 299 var spacer = try ui.widget.Spacer.init(allocator); 300 errdefer spacer.deinit(allocator); 301 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ 302 .widget = .{ .spacer = spacer }, 303 .rect = null, 304 .min_size = null, 305 .flex = .grow, 306 }); 307 } 308 309 // settings tab. settings are account preferences, so it needs a login 310 // (which also rules out local mode, which has no accounts). 311 if (session.data.user_id != null) { 312 var text_box = try wgt.TextBox.init(allocator, settings_tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 313 errdefer text_box.deinit(allocator); 314 text_box.getFocus().mode = .all; 315 text_box.getFocus().kind = .{ .custom = settings_link }; 316 try tab_ids.put(allocator, text_box.getFocus().id, {}); 317 if (current_tag == .repo_settings) selected_tab = text_box.getFocus().id; 318 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 319 .widget = .{ .text_box = text_box }, 320 .rect = null, 321 .min_size = .{ .width = settings_tab_label.len + 2, .height = null }, 322 }); 323 } 324 325 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 326 // link; repoint its instance at this repo's auth route so it stays on 327 // this page. local mode has no accounts, so it has no auth tab. 328 if (session.data.host_kind == .server) { 329 var auth_tab = try AuthTab.View.init(allocator, session); 330 errdefer auth_tab.deinit(allocator); 331 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 332 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 333 if (current_tag == .repo_auth) selected_tab = auth_tab.getFocus().id; 334 try tabs_box.children.put(allocator, auth_tab.getFocus().id, .{ 335 .widget = .{ .auth_tab = auth_tab }, 336 .rect = null, 337 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 338 }); 339 } 340 341 // quit tab 342 if (session.is_terminal) { 343 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 344 errdefer text_box.deinit(allocator); 345 text_box.getFocus().mode = .all; 346 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 347 try tab_ids.put(allocator, text_box.getFocus().id, {}); 348 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 349 .widget = .{ .text_box = text_box }, 350 .rect = null, 351 // the label is a single column; +2 for the border 352 .min_size = .{ .width = 1 + 2, .height = null }, 353 }); 354 } 355 356 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 357 const title_id = title_box.getFocus().id; 358 const tabs_id = tabs_box.getFocus().id; 359 try box.children.put(allocator, title_id, .{ 360 .widget = .{ .box = title_box }, 361 .rect = null, 362 .min_size = null, 363 }); 364 title_box_owned = true; 365 try box.children.put(allocator, tabs_id, .{ 366 .widget = .{ .box = tabs_box }, 367 .rect = null, 368 .min_size = null, 369 }); 370 tabs_box_owned = true; 371 box.getFocus().child_id = tabs_id; 372 373 return .{ 374 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 375 .tab_ids = tab_ids, 376 .tabs_id = tabs_id, 377 .first_group_width = first_group_width, 378 .session = session, 379 }; 380 } 381 382 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 383 self.scroll.deinit(allocator); 384 self.tab_ids.deinit(allocator); 385 } 386 387 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 388 self.clearGrid(); 389 const box = &self.scroll.child.box; 390 const back_visible = self.session.back == .available; 391 ui.widget.setBackButtonVisible(box, back_visible); 392 const tabs_child = self.tabsChild(); 393 const tabs_box = &tabs_child.widget.box; 394 395 // only the selected tab shows its border 396 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 397 var tabs_width: usize = 0; 398 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 399 const tb: ?*wgt.TextBox = switch (child.widget) { 400 .text_box => |*x| x, 401 .auth_tab => |*at| blk: { 402 // the label tracks login state per frame, so the width must too 403 child.min_size = .{ .width = at.minWidth(), .height = null }; 404 break :blk &at.text_box; 405 }, 406 else => null, 407 }; 408 if (tb) |t| { 409 t.options.border_style = if (selected_tab == id) .single else .hidden; 410 } 411 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 412 } 413 414 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 415 // the outer box's hidden border occupies two columns. keep the title 416 // and tabs together if they fit; otherwise tab strip on its own row. 417 const content_width = if (viewport_width) |width| width -| 2 else null; 418 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 419 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 420 box.options.direction = if (wrap) .vert else .horiz; 421 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 422 423 var scroll_constraint = constraint; 424 scroll_constraint.min_size.width = viewport_width; 425 try self.scroll.build(allocator, scroll_constraint, root_focus); 426 } 427 428 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 429 _ = allocator; 430 const current_tab = self.currentTabIndex() orelse return; 431 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 432 const tab_id = self.tab_ids.keys()[new_tab]; 433 root_focus.setFocus(tab_id); 434 const tabs_child = self.tabsChild(); 435 const tabs_rect = tabs_child.rect orelse return; 436 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 437 var rect = tab.rect orelse return; 438 rect.x += tabs_rect.x; 439 rect.y += tabs_rect.y; 440 self.scroll.scrollToRect(rect); 441 } 442 } 443 444 pub fn clearGrid(self: *View) void { 445 self.scroll.clearGrid(); 446 } 447 448 pub fn getGrid(self: View) ?Grid { 449 return self.scroll.getGrid(); 450 } 451 452 pub fn getFocus(self: *View) *Focus { 453 return self.scroll.getFocus(); 454 } 455 456 pub fn getSelectedIndex(self: View) ?usize { 457 return self.currentTabIndex(); 458 } 459 460 fn currentTabIndex(self: View) ?usize { 461 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 462 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 463 return self.tab_ids.getIndex(child_id); 464 } 465 466 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 467 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 468 } 469 }; 470