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