╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
237 .min_size = .{ .width = issues_tab_label.len + 2, .height = null }, 238 }); 239 } 240 241 // patches tab 242 { 243 var text_box = try wgt.TextBox.init(allocator, patches_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 244 errdefer text_box.deinit(allocator); 245 text_box.getFocus().mode = .all; 246 text_box.getFocus().kind = .{ .custom = patches_link }; 247 try tab_ids.put(allocator, text_box.getFocus().id, {}); 248 if (current_tag == .repo_patches) selected_tab = text_box.getFocus().id; 249 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 250 .widget = .{ .text_box = text_box }, 251 .rect = null, 252 .min_size = .{ .width = patches_tab_label.len + 2, .height = null }, 253 }); 254 } 255 256 // discuss tab 257 { 258 var text_box = try wgt.TextBox.init(allocator, discuss_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 259 errdefer text_box.deinit(allocator); 260 text_box.getFocus().mode = .all; 261 text_box.getFocus().kind = .{ .custom = discussions_link }; 262 try tab_ids.put(allocator, text_box.getFocus().id, {}); 263 if (current_tag == .repo_discussions) selected_tab = text_box.getFocus().id; 264 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 265 .widget = .{ .text_box = text_box }, 266 .rect = null, 267 .min_size = .{ .width = discuss_tab_label.len + 2, .height = null }, 268 }); 269 } 270 271 // events, which a server only shows to whoever it shows undo to 272 if (page.handle.canWrite()) { 273 var text_box = try wgt.TextBox.init(allocator, events_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 274 errdefer text_box.deinit(allocator); 275 text_box.getFocus().mode = .all; 276 text_box.getFocus().kind = .{ .custom = events_link }; 277 try tab_ids.put(allocator, text_box.getFocus().id, {}); 278 if (current_tag == .repo_events) selected_tab = text_box.getFocus().id; 279 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 280 .widget = .{ .text_box = text_box }, 281 .rect = null, 282 .min_size = .{ .width = events_tab_label.len + 2, .height = null }, 283 }); 284 } 285 286 if (page.undo != null) { 287 const route = ui.RoutablePage.repoUndoRoute(identity, null) orelse return error.RouteTooLong; 288 var text_box = try wgt.TextBox.init(allocator, "undo", .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 289 errdefer text_box.deinit(allocator); 290 text_box.getFocus().mode = .all; 291 text_box.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, current_tag == .repo_undo) }; 292 try tab_ids.put(allocator, text_box.getFocus().id, {}); 293 if (current_tag == .repo_undo) selected_tab = text_box.getFocus().id; 294 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 295 .widget = .{ .text_box = text_box }, 296 .rect = null, 297 .min_size = .{ .width = 6, .height = null }, 298 }); 299 } 300 301 // spacer pushes new repo, new user, and auth to the right 302 { 303 var spacer = try ui.widget.Spacer.init(allocator); 304 errdefer spacer.deinit(allocator); 305 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ 306 .widget = .{ .spacer = spacer }, 307 .rect = null, 308 .min_size = null, 309 .flex = .grow, 310 }); 311 } 312 313 // new repo tab with a login, new user tab without one outside local mode 314 if (session.data.user_id != null) { 315 var text_box = try wgt.TextBox.init(allocator, new_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = new_repo_tab_bottom_label }); 316 errdefer text_box.deinit(allocator); 317 text_box.getFocus().mode = .all; 318 text_box.getFocus().kind = .{ .custom = new_repo_link }; 319 try tab_ids.put(allocator, text_box.getFocus().id, {}); 320 if (current_tag == .repo_new_repo) selected_tab = text_box.getFocus().id; 321 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 322 .widget = .{ .text_box = text_box }, 323 .rect = null, 324 .min_size = .{ .width = new_tab_label.len + 2, .height = null }, 325 }); 326 } else if (session.data.host_kind == .server) { 327 var text_box = try wgt.TextBox.init(allocator, new_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = new_user_tab_bottom_label }); 328 errdefer text_box.deinit(allocator); 329 text_box.getFocus().mode = .all; 330 text_box.getFocus().kind = .{ .custom = new_user_link }; 331 try tab_ids.put(allocator, text_box.getFocus().id, {}); 332 if (current_tag == .repo_new_user) selected_tab = text_box.getFocus().id; 333 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 334 .widget = .{ .text_box = text_box }, 335 .rect = null, 336 .min_size = .{ .width = new_tab_label.len + 2, .height = null }, 337 }); 338 } 339 340 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 341 // link; repoint its instance at this repo's auth route so it stays on 342 // this page. local mode has no accounts, so it has no auth tab. 343 if (session.data.host_kind == .server) { 344 var auth_tab = try AuthTab.View.init(allocator, session); 345 errdefer auth_tab.deinit(allocator); 346 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 347 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 348 if (current_tag == .repo_auth) selected_tab = auth_tab.getFocus().id; 349 try tabs_box.children.put(allocator, auth_tab.getFocus().id, .{ 350 .widget = .{ .auth_tab = auth_tab }, 351 .rect = null, 352 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 353 }); 354 } 355 356 // quit tab 357 if (session.is_terminal) { 358 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 359 errdefer text_box.deinit(allocator); 360 text_box.getFocus().mode = .all; 361 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 362 try tab_ids.put(allocator, text_box.getFocus().id, {}); 363 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 364 .widget = .{ .text_box = text_box }, 365 .rect = null, 366 // the label is a single column; +2 for the border 367 .min_size = .{ .width = 1 + 2, .height = null }, 368 }); 369 } 370 371 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 372 const title_id = title_box.getFocus().id; 373 const tabs_id = tabs_box.getFocus().id; 374 try box.children.put(allocator, title_id, .{ 375 .widget = .{ .box = title_box }, 376 .rect = null, 377 .min_size = null, 378 }); 379 title_box_owned = true; 380 // a blank row between the title and the tabs, shown only when they wrap 381 const gap_id = blk: { 382 var gap = try wgt.Text.init(allocator, " "); 383 errdefer gap.deinit(allocator); 384 const id = gap.getFocus().id; 385 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 386 break :blk id; 387 }; 388 try box.children.put(allocator, tabs_id, .{ 389 .widget = .{ .box = tabs_box }, 390 .rect = null, 391 .min_size = null, 392 }); 393 tabs_box_owned = true; 394 box.getFocus().child_id = tabs_id; 395 396 return .{ 397 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 398 .tab_ids = tab_ids, 399 .tabs_id = tabs_id, 400 .gap_id = gap_id, 401 .first_group_width = first_group_width, 402 .session = session, 403 }; 404 } 405 406 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 407 self.scroll.deinit(allocator); 408 self.tab_ids.deinit(allocator); 409 } 410 411 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 412 self.clearGrid(); 413 const box = &self.scroll.child.box; 414 const back_visible = self.session.back == .available; 415 ui.widget.setBackButtonVisible(box, back_visible); 416 const tabs_child = self.tabsChild(); 417 const tabs_box = &tabs_child.widget.box; 418 419 // only the selected tab shows its border 420 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 421 var tabs_width: usize = 0; 422 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 423 const tb: ?*wgt.TextBox = switch (child.widget) { 424 .text_box => |*x| x, 425 .auth_tab => |*at| blk: { 426 // the label tracks login state per frame, so the width must too 427 child.min_size = .{ .width = at.minWidth(), .height = null }; 428 break :blk &at.text_box; 429 }, 430 else => null, 431 }; 432 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 433 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 434 } 435 436 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 437 // the outer box's hidden border occupies two columns. keep the title 438 // and tabs together if they fit; otherwise tab strip on its own row. 439 const content_width = if (viewport_width) |width| width -| 2 else null; 440 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 441 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 442 box.options.direction = if (wrap) .vert else .horiz; 443 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 444 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 445 446 var scroll_constraint = constraint; 447 scroll_constraint.min_size.width = viewport_width; 448 try self.scroll.build(allocator, scroll_constraint, root_focus); 449 } 450 451 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 452 _ = allocator; 453 const current_tab = self.currentTabIndex() orelse return; 454 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 455 const tab_id = self.tab_ids.keys()[new_tab]; 456 root_focus.setFocus(tab_id); 457 const tabs_child = self.tabsChild(); 458 const tabs_rect = tabs_child.rect orelse return; 459 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 460 var rect = tab.rect orelse return; 461 rect.x += tabs_rect.x; 462 rect.y += tabs_rect.y; 463 self.scroll.scrollToRect(rect); 464 } 465 } 466 467 pub fn clearGrid(self: *View) void { 468 self.scroll.clearGrid(); 469 } 470 471 pub fn getGrid(self: View) ?Grid { 472 return self.scroll.getGrid(); 473 } 474 475 pub fn getFocus(self: *View) *Focus { 476 return self.scroll.getFocus(); 477 } 478 479 pub fn getSelectedIndex(self: View) ?usize { 480 return self.currentTabIndex(); 481 } 482 483 fn currentTabIndex(self: View) ?usize { 484 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 485 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 486 return self.tab_ids.getIndex(child_id); 487 } 488 489 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 490 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 491 } 492 }; 493