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