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