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