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