╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
347 .min_size = null, 348 }); 349 title_box_owned = true; 350 try box.children.put(allocator, tabs_id, .{ 351 .widget = .{ .box = tabs_box }, 352 .rect = null, 353 .min_size = null, 354 }); 355 tabs_box_owned = true; 356 box.getFocus().child_id = tabs_id; 357 358 return .{ 359 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 360 .tab_ids = tab_ids, 361 .tabs_id = tabs_id, 362 .first_group_width = first_group_width, 363 .session = session, 364 }; 365 } 366 367 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 368 self.scroll.deinit(allocator); 369 self.tab_ids.deinit(allocator); 370 } 371 372 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 373 self.clearGrid(); 374 const box = &self.scroll.child.box; 375 const back_visible = self.session.back == .available; 376 ui.widget.setBackButtonVisible(box, back_visible); 377 const tabs_child = self.tabsChild(); 378 const tabs_box = &tabs_child.widget.box; 379 380 // only the selected tab shows its border 381 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 382 var tabs_width: usize = 0; 383 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 384 const tb: ?*wgt.TextBox = switch (child.widget) { 385 .text_box => |*x| x, 386 .auth_tab => |*at| blk: { 387 // the label tracks login state per frame, so the width must too 388 child.min_size = .{ .width = at.minWidth(), .height = null }; 389 break :blk &at.text_box; 390 }, 391 else => null, 392 }; 393 if (tb) |t| { 394 t.options.border_style = if (selected_tab == id) .single else .hidden; 395 } 396 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 397 } 398 399 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 400 // the outer box's hidden border occupies two columns. keep the title 401 // and tabs together if they fit; otherwise tab strip on its own row. 402 const content_width = if (viewport_width) |width| width -| 2 else null; 403 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 404 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 405 box.options.direction = if (wrap) .vert else .horiz; 406 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 407 408 var scroll_constraint = constraint; 409 scroll_constraint.min_size.width = viewport_width; 410 try self.scroll.build(allocator, scroll_constraint, root_focus); 411 } 412 413 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 414 _ = allocator; 415 const current_tab = self.currentTabIndex() orelse return; 416 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 417 const tab_id = self.tab_ids.keys()[new_tab]; 418 root_focus.setFocus(tab_id); 419 const tabs_child = self.tabsChild(); 420 const tabs_rect = tabs_child.rect orelse return; 421 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 422 var rect = tab.rect orelse return; 423 rect.x += tabs_rect.x; 424 rect.y += tabs_rect.y; 425 self.scroll.scrollToRect(rect); 426 } 427 } 428 429 pub fn clearGrid(self: *View) void { 430 self.scroll.clearGrid(); 431 } 432 433 pub fn getGrid(self: View) ?Grid { 434 return self.scroll.getGrid(); 435 } 436 437 pub fn getFocus(self: *View) *Focus { 438 return self.scroll.getFocus(); 439 } 440 441 pub fn getSelectedIndex(self: View) ?usize { 442 return self.currentTabIndex(); 443 } 444 445 fn currentTabIndex(self: View) ?usize { 446 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 447 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 448 return self.tab_ids.getIndex(child_id); 449 } 450 451 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 452 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 453 } 454 }; 455