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