╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
411 tabs_box_owned = true; 412 box.getFocus().child_id = tabs_id; 413 414 return .{ 415 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 416 .tab_ids = tab_ids, 417 .tabs_id = tabs_id, 418 .gap_id = gap_id, 419 .first_group_width = first_group_width, 420 .session = session, 421 }; 422 } 423 424 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 425 self.scroll.deinit(allocator); 426 self.tab_ids.deinit(allocator); 427 } 428 429 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 430 self.clearGrid(); 431 const box = &self.scroll.child.box; 432 const back_visible = self.session.back == .available; 433 ui.widget.setBackButtonVisible(box, back_visible); 434 const tabs_child = self.tabsChild(); 435 const tabs_box = &tabs_child.widget.box; 436 437 // only the selected tab shows its border 438 const selected_tab = tabs_box.getFocus().child_id; 439 var tabs_width: usize = 0; 440 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 441 const tb: ?*wgt.TextBox = switch (child.widget) { 442 .text_box => |*x| x, 443 else => null, 444 }; 445 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 446 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 447 } 448 449 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 450 // the outer box's hidden border occupies two columns. keep the title 451 // and tabs together if they fit; otherwise tab strip on its own row. 452 const content_width = if (viewport_width) |width| width -| 2 else null; 453 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 454 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 455 box.options.direction = if (wrap) .vert else .horiz; 456 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 457 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 458 459 var scroll_constraint = constraint; 460 scroll_constraint.min_size.width = viewport_width; 461 try self.scroll.build(allocator, scroll_constraint, root_focus); 462 } 463 464 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 465 _ = allocator; 466 const current_tab = self.currentTabIndex() orelse return; 467 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 468 const tab_id = self.tab_ids.keys()[new_tab]; 469 root_focus.setFocus(tab_id); 470 const tabs_child = self.tabsChild(); 471 const tabs_rect = tabs_child.rect orelse return; 472 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 473 var rect = tab.rect orelse return; 474 rect.x += tabs_rect.x; 475 rect.y += tabs_rect.y; 476 self.scroll.scrollToRect(rect); 477 } 478 } 479 480 pub fn clearGrid(self: *View) void { 481 self.scroll.clearGrid(); 482 } 483 484 pub fn getGrid(self: View) ?Grid { 485 return self.scroll.getGrid(); 486 } 487 488 pub fn getFocus(self: *View) *Focus { 489 return self.scroll.getFocus(); 490 } 491 492 pub fn getSelectedIndex(self: View) ?usize { 493 return self.currentTabIndex(); 494 } 495 496 fn currentTabIndex(self: View) ?usize { 497 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 498 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 499 return self.tab_ids.getIndex(child_id); 500 } 501 502 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 503 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 504 } 505 }; 506