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