╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
342 .min_size = .{ .width = new_tab_label.len + 2, .height = null }, 343 }); 344 } else if (session.data.host_kind == .server) { 345 var text_box = try wgt.TextBox.init(allocator, new_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = new_user_tab_bottom_label } }); 346 errdefer text_box.deinit(allocator); 347 text_box.getFocus().mode = .all; 348 text_box.getFocus().kind = .{ .custom = new_user_link }; 349 try tab_ids.put(allocator, text_box.getFocus().id, {}); 350 if (current_tag == .repo_user_new) selected_tab = text_box.getFocus().id; 351 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 352 .widget = .{ .text_box = text_box }, 353 .rect = null, 354 .min_size = .{ .width = new_tab_label.len + 2, .height = null }, 355 }); 356 } 357 358 // user tab. local mode has no users, so it has none. 359 if (session.data.host_kind == .server) { 360 const label = ui.UserSettings.tabLabel(session); 361 const count_label = try ui.UserSettings.tabBottomLabel(session); 362 var text_box = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = count_label } }); 363 errdefer text_box.deinit(allocator); 364 text_box.getFocus().mode = .all; 365 text_box.getFocus().kind = .{ .custom = user_link }; 366 try tab_ids.put(allocator, text_box.getFocus().id, {}); 367 if (current_tag == .repo_user) selected_tab = text_box.getFocus().id; 368 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 369 .widget = .{ .text_box = text_box }, 370 .rect = null, 371 .min_size = .{ .width = @max(label.len, count_label.len) + 2, .height = null }, 372 }); 373 } 374 375 // quit tab 376 if (session.is_terminal) { 377 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 378 errdefer text_box.deinit(allocator); 379 text_box.getFocus().mode = .all; 380 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 381 try tab_ids.put(allocator, text_box.getFocus().id, {}); 382 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 383 .widget = .{ .text_box = text_box }, 384 .rect = null, 385 // the label is a single column; +2 for the border 386 .min_size = .{ .width = 1 + 2, .height = null }, 387 }); 388 } 389 390 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 391 const title_id = title_box.getFocus().id; 392 const tabs_id = tabs_box.getFocus().id; 393 try box.children.put(allocator, title_id, .{ 394 .widget = .{ .box = title_box }, 395 .rect = null, 396 .min_size = null, 397 }); 398 title_box_owned = true; 399 // a blank row between the title and the tabs, shown only when they wrap 400 const gap_id = blk: { 401 var gap = try wgt.Text.init(allocator, " "); 402 errdefer gap.deinit(allocator); 403 const id = gap.getFocus().id; 404 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 405 break :blk id; 406 }; 407 try box.children.put(allocator, tabs_id, .{ 408 .widget = .{ .box = tabs_box }, 409 .rect = null, 410 .min_size = null, 411 }); 412 tabs_box_owned = true; 413 box.getFocus().child_id = tabs_id; 414 415 return .{ 416 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 417 .tab_ids = tab_ids, 418 .tabs_id = tabs_id, 419 .gap_id = gap_id, 420 .first_group_width = first_group_width, 421 .session = session, 422 }; 423 } 424 425 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 426 self.scroll.deinit(allocator); 427 self.tab_ids.deinit(allocator); 428 } 429 430 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 431 self.clearGrid(); 432 const box = &self.scroll.child.box; 433 const back_visible = self.session.back == .available; 434 ui.widget.setBackButtonVisible(box, back_visible); 435 const tabs_child = self.tabsChild(); 436 const tabs_box = &tabs_child.widget.box; 437 438 // only the selected tab shows its border 439 const selected_tab = tabs_box.getFocus().child_id; 440 var tabs_width: usize = 0; 441 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 442 const tb: ?*wgt.TextBox = switch (child.widget) { 443 .text_box => |*x| x, 444 else => null, 445 }; 446 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 447 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 448 } 449 450 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 451 // the outer box's hidden border occupies two columns. keep the title 452 // and tabs together if they fit; otherwise tab strip on its own row. 453 const content_width = if (viewport_width) |width| width -| 2 else null; 454 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 455 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 456 box.options.direction = if (wrap) .vert else .horiz; 457 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 458 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 459 460 var scroll_constraint = constraint; 461 scroll_constraint.min_size.width = viewport_width; 462 try self.scroll.build(allocator, scroll_constraint, root_focus); 463 } 464 465 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 466 _ = allocator; 467 const current_tab = self.currentTabIndex() orelse return; 468 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 469 const tab_id = self.tab_ids.keys()[new_tab]; 470 root_focus.setFocus(tab_id); 471 const tabs_child = self.tabsChild(); 472 const tabs_rect = tabs_child.rect orelse return; 473 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 474 var rect = tab.rect orelse return; 475 rect.x += tabs_rect.x; 476 rect.y += tabs_rect.y; 477 self.scroll.scrollToRect(rect); 478 } 479 } 480 481 pub fn clearGrid(self: *View) void { 482 self.scroll.clearGrid(); 483 } 484 485 pub fn getGrid(self: View) ?Grid { 486 return self.scroll.getGrid(); 487 } 488 489 pub fn getFocus(self: *View) *Focus { 490 return self.scroll.getFocus(); 491 } 492 493 pub fn getSelectedIndex(self: View) ?usize { 494 return self.currentTabIndex(); 495 } 496 497 fn currentTabIndex(self: View) ?usize { 498 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 499 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 500 return self.tab_ids.getIndex(child_id); 501 } 502 503 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 504 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 505 } 506 }; 507