╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
353 }); 354 } 355 356 // user tab. local mode has no users, so it has none. 357 if (session.data.host_kind == .server) { 358 const label = ui.UserSettings.tabLabel(session); 359 const count_label = try ui.UserSettings.tabBottomLabel(session); 360 // the viewer's role in this repo, when they can push to it 361 const role_label: []const u8 = if (page.handle.viewer) |viewer| switch (viewer.role) { 362 .owner => "(owner)", 363 .write => "(member)", 364 .read => "", 365 } else ""; 366 var text_box = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = role_label }, .bottom_label = count_label }); 367 errdefer text_box.deinit(allocator); 368 text_box.getFocus().mode = .all; 369 text_box.getFocus().kind = .{ .custom = user_link }; 370 try tab_ids.put(allocator, text_box.getFocus().id, {}); 371 if (current_tag == .repo_user) selected_tab = text_box.getFocus().id; 372 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 373 .widget = .{ .text_box = text_box }, 374 .rect = null, 375 .min_size = .{ .width = @max(label.len, count_label.text.len, role_label.len) + 2, .height = null }, 376 }); 377 } 378 379 // quit tab 380 if (session.is_terminal) { 381 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 382 errdefer text_box.deinit(allocator); 383 text_box.getFocus().mode = .all; 384 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 385 try tab_ids.put(allocator, text_box.getFocus().id, {}); 386 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 387 .widget = .{ .text_box = text_box }, 388 .rect = null, 389 // the label is a single column; +2 for the border 390 .min_size = .{ .width = 1 + 2, .height = null }, 391 }); 392 } 393 394 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 395 const title_id = title_box.getFocus().id; 396 const tabs_id = tabs_box.getFocus().id; 397 try box.children.put(allocator, title_id, .{ 398 .widget = .{ .box = title_box }, 399 .rect = null, 400 .min_size = null, 401 }); 402 title_box_owned = true; 403 // a blank row between the title and the tabs, shown only when they wrap 404 const gap_id = blk: { 405 var gap = try wgt.Text.init(allocator, " "); 406 errdefer gap.deinit(allocator); 407 const id = gap.getFocus().id; 408 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 409 break :blk id; 410 }; 411 try box.children.put(allocator, tabs_id, .{ 412 .widget = .{ .box = tabs_box }, 413 .rect = null, 414 .min_size = null, 415 }); 416 tabs_box_owned = true; 417 box.getFocus().child_id = tabs_id; 418 419 return .{ 420 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 421 .tab_ids = tab_ids, 422 .tabs_id = tabs_id, 423 .gap_id = gap_id, 424 .first_group_width = first_group_width, 425 .session = session, 426 }; 427 } 428 429 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 430 self.scroll.deinit(allocator); 431 self.tab_ids.deinit(allocator); 432 } 433 434 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 435 self.clearGrid(); 436 const box = &self.scroll.child.box; 437 const back_visible = self.session.back == .available; 438 ui.widget.setBackButtonVisible(box, back_visible); 439 const tabs_child = self.tabsChild(); 440 const tabs_box = &tabs_child.widget.box; 441 442 // only the selected tab shows its border 443 const selected_tab = tabs_box.getFocus().child_id; 444 var tabs_width: usize = 0; 445 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 446 const tb: ?*wgt.TextBox = switch (child.widget) { 447 .text_box => |*x| x, 448 else => null, 449 }; 450 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 451 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 452 } 453 454 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 455 // the outer box's hidden border occupies two columns. keep the title 456 // and tabs together if they fit; otherwise tab strip on its own row. 457 const content_width = if (viewport_width) |width| width -| 2 else null; 458 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 459 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 460 box.options.direction = if (wrap) .vert else .horiz; 461 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 462 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 463 464 var scroll_constraint = constraint; 465 scroll_constraint.min_size.width = viewport_width; 466 try self.scroll.build(allocator, scroll_constraint, root_focus); 467 } 468 469 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 470 _ = allocator; 471 const current_tab = self.currentTabIndex() orelse return; 472 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 473 const tab_id = self.tab_ids.keys()[new_tab]; 474 root_focus.setFocus(tab_id); 475 const tabs_child = self.tabsChild(); 476 const tabs_rect = tabs_child.rect orelse return; 477 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 478 var rect = tab.rect orelse return; 479 rect.x += tabs_rect.x; 480 rect.y += tabs_rect.y; 481 self.scroll.scrollToRect(rect); 482 } 483 } 484 485 pub fn clearGrid(self: *View) void { 486 self.scroll.clearGrid(); 487 } 488 489 pub fn getGrid(self: View) ?Grid { 490 return self.scroll.getGrid(); 491 } 492 493 pub fn getFocus(self: *View) *Focus { 494 return self.scroll.getFocus(); 495 } 496 497 pub fn getSelectedIndex(self: View) ?usize { 498 return self.currentTabIndex(); 499 } 500 501 fn currentTabIndex(self: View) ?usize { 502 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 503 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 504 return self.tab_ids.getIndex(child_id); 505 } 506 507 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 508 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 509 } 510 }; 511