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