╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
297 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 298 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 299 if (current_tag == .repo_auth) selected_tab = auth_tab.getFocus().id; 300 try tabs_box.children.put(allocator, auth_tab.getFocus().id, .{ 301 .widget = .{ .auth_tab = auth_tab }, 302 .rect = null, 303 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 304 }); 305 } 306 307 // quit tab 308 if (session.is_terminal) { 309 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 310 errdefer text_box.deinit(allocator); 311 text_box.getFocus().focusable = true; 312 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 313 try tab_ids.put(allocator, text_box.getFocus().id, {}); 314 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 315 .widget = .{ .text_box = text_box }, 316 .rect = null, 317 // the label is a single column; +2 for the border 318 .min_size = .{ .width = 1 + 2, .height = null }, 319 }); 320 } 321 322 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 323 const title_id = title_box.getFocus().id; 324 const tabs_id = tabs_box.getFocus().id; 325 try box.children.put(allocator, title_id, .{ 326 .widget = .{ .box = title_box }, 327 .rect = null, 328 .min_size = null, 329 }); 330 title_box_owned = true; 331 try box.children.put(allocator, tabs_id, .{ 332 .widget = .{ .box = tabs_box }, 333 .rect = null, 334 .min_size = null, 335 }); 336 tabs_box_owned = true; 337 box.getFocus().child_id = tabs_id; 338 339 return .{ 340 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 341 .data = data, 342 .tab_ids = tab_ids, 343 .tabs_id = tabs_id, 344 .first_group_width = first_group_width, 345 }; 346 } 347 348 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 349 self.scroll.deinit(allocator); 350 self.tab_ids.deinit(allocator); 351 } 352 353 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 354 self.clearGrid(); 355 const box = &self.scroll.child.box; 356 const tabs_child = self.tabsChild(); 357 const tabs_box = &tabs_child.widget.box; 358 359 // only the selected tab shows its border 360 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 361 var tabs_width: usize = 0; 362 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 363 const tb: ?*wgt.TextBox = switch (child.widget) { 364 .text_box => |*x| x, 365 .auth_tab => |*at| blk: { 366 // the label tracks login state per frame, so the width must too 367 child.min_size = .{ .width = at.minWidth(), .height = null }; 368 break :blk &at.text_box; 369 }, 370 else => null, 371 }; 372 if (tb) |t| { 373 t.options.border_style = if (selected_tab == id) .single else .hidden; 374 } 375 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 376 } 377 378 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 379 // the outer box's hidden border occupies two columns. keep the title 380 // and tabs together if they fit; otherwise tab strip on its own row. 381 const content_width = if (viewport_width) |width| width -| 2 else null; 382 const wrap = if (content_width) |width| self.first_group_width + tabs_width > width else false; 383 box.options.direction = if (wrap) .vert else .horiz; 384 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 385 386 var scroll_constraint = constraint; 387 scroll_constraint.min_size.width = viewport_width; 388 try self.scroll.build(allocator, scroll_constraint, root_focus); 389 } 390 391 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 392 _ = allocator; 393 const current_tab = self.currentTabIndex() orelse return; 394 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 395 const tab_id = self.tab_ids.keys()[new_tab]; 396 root_focus.setFocus(tab_id); 397 const tabs_child = self.tabsChild(); 398 const tabs_rect = tabs_child.rect orelse return; 399 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 400 var rect = tab.rect orelse return; 401 rect.x += tabs_rect.x; 402 rect.y += tabs_rect.y; 403 self.scroll.scrollToRect(rect); 404 } 405 } 406 407 pub fn clearGrid(self: *View) void { 408 self.scroll.clearGrid(); 409 } 410 411 pub fn getGrid(self: View) ?Grid { 412 return self.scroll.getGrid(); 413 } 414 415 pub fn getFocus(self: *View) *Focus { 416 return self.scroll.getFocus(); 417 } 418 419 pub fn getSelectedIndex(self: View) ?usize { 420 return self.currentTabIndex(); 421 } 422 423 fn currentTabIndex(self: View) ?usize { 424 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 425 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 426 return self.tab_ids.getIndex(child_id); 427 } 428 429 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 430 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 431 } 432 }; 433