╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
367 }, 368 } 369 } 370 const aa = arena.allocator(); 371 break :blk .{ 372 try Files.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 373 Changes{ .commits = try Commits.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, commits_base_oid) }, 374 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 375 try Discussions.emptyResult(aa, repo_identity.identity, discussions_label, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 376 try Issues.emptyResult(aa, repo_identity.identity, issues_label, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 377 try Patches.emptyResult(aa, repo_identity.identity, patches_label, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 378 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 379 }; 380 }; 381 if (route == .repo_undo and undo_data == null) return error.NotFound; 382 if ((route == .repo_settings or route == .repo_roles) and settings == null) return error.NotFound; 383 if (undo_data) |*undo| undo.clear = undo_clear; 384 discussions.repo_source = source; 385 issues.repo_source = source; 386 patches.repo_source = source; 387 patches.repo_id = repo_id_maybe; 388 389 return .{ 390 // use the files tab's resolved ref for the header 391 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, discussions.label, issues.label, patches.label), 392 .repo = repo, 393 .handle = handle, 394 .files = files, 395 .changes = changes, 396 .refs = refs, 397 .discussions = discussions, 398 .issues = issues, 399 .patches = patches, 400 .events = events, 401 .undo = undo_data, 402 .settings = settings, 403 .quit = Quit.init(), 404 }; 405 } 406 407 // the viewer a tab's comment bar sees, which is none below the tab's role 408 fn tabViewer(handle: ui.RepoHandle, repo: evt.Repo.Record, kind: evt.EventKind) ?ui.Viewer { 409 const viewer = handle.viewer orelse return null; 410 const tab_role = repo.event.threadRole(kind) orelse return null; 411 return if (viewer.role.atLeast(tab_role)) viewer else null; 412 } 413 414 pub const View = struct { 415 box: wgt.Box(ui.Widget), 416 417 const header_index: usize = 0; 418 const stack_index: usize = 1; 419 420 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 421 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 422 errdefer box.deinit(allocator); 423 424 // build the header first so we can grab the files-tab id for the user 425 // view (it focuses there after login). 426 { 427 var header_view = try Header.View.init(allocator, data, session); 428 errdefer header_view.deinit(allocator); 429 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 430 } 431 432 { 433 var stack = try wgt.Stack(ui.Widget).init(allocator); 434 errdefer stack.deinit(allocator); 435 436 // files — the default tab: the current directory's listing. 437 { 438 var files_view = try Files.View.init(allocator, &data.files, session); 439 errdefer files_view.deinit(allocator); 440 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 441 } 442 443 switch (data.changes) { 444 .diff => |*diff| { 445 var diff_view = try Diff.View.init(allocator, diff, session); 446 errdefer diff_view.deinit(allocator); 447 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 448 }, 449 .commits => |*commits| { 450 var commits_view = try Commits.View.init(allocator, commits, session); 451 errdefer commits_view.deinit(allocator); 452 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 453 }, 454 } 455 456 // refs — the repo's branches and tags. 457 { 458 var refs_view = try Refs.View.init(allocator, &data.refs, session); 459 errdefer refs_view.deinit(allocator); 460 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 461 } 462 463 // discussions and their comment permalinks. 464 if (data.repo.event.discuss_role != null) { 465 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 466 errdefer discussions_view.deinit(allocator); 467 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 468 } 469 470 // issues — the repo's issue tracker and comment permalinks. 471 if (data.repo.event.issue_role != null) { 472 var issues_view = try Issues.View.init(allocator, &data.issues, session); 473 errdefer issues_view.deinit(allocator); 474 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 475 } 476 477 // patches and their comment permalinks. 478 if (data.repo.event.patch_role != null) { 479 var patches_view = try Patches.View.init(allocator, &data.patches, session); 480 errdefer patches_view.deinit(allocator); 481 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 482 } 483 484 if (data.handle.canWrite()) { 485 var events_view = try Events.View.init(allocator, &data.events, session); 486 errdefer events_view.deinit(allocator); 487 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 488 } 489 490 if (data.undo) |*undo| { 491 var undo_view = try Undo.View.init(allocator, undo, session); 492 errdefer undo_view.deinit(allocator); 493 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 494 } 495 496 if (data.settings) |*settings| { 497 var settings_view = try Settings.View.init(allocator, settings, session); 498 errdefer settings_view.deinit(allocator); 499 try stack.children.put(allocator, settings_view.getFocus().id, .{ .repo_settings = settings_view }); 500 } 501 502 // the header shows new repo with a login, and new user without one 503 // and user only outside local mode, so keep the stack's children 504 // 1:1 with the tabs by skipping the same views. 505 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.owner_name, data.header.name }); 506 if (session.data.user_id != null) { 507 const route = ui.RoutablePage{ .repo_repo_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 508 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 509 errdefer new_repo_view.deinit(allocator); 510 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 511 } else if (session.data.host_kind == .server) { 512 const route = ui.RoutablePage{ .repo_user_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 513 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 514 errdefer new_user_view.deinit(allocator); 515 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 516 } 517 518 if (session.data.host_kind == .server) { 519 var user_view = try ui.UserSettings.initView(allocator, session); 520 errdefer user_view.deinit(allocator); 521 try stack.children.put(allocator, user_view.getFocus().id, user_view); 522 } 523 524 if (session.is_terminal) { 525 var quit_view = try Quit.View.init(allocator, session); 526 errdefer quit_view.deinit(allocator); 527 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 528 } 529 530 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 531 } 532 533 var self = View{ .box = box }; 534 // a page opens on its tabs, except that search results open in the 535 // tab's own search box. 536 const results = data.files.find != null or 537 (data.changes == .commits and data.changes.commits.search != null) or 538 data.discussions.search != null or data.issues.search != null or data.patches.search != null; 539 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 540 return self; 541 } 542 543 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 544 self.box.deinit(allocator); 545 } 546 547 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 548 self.clearGrid(); 549 const header = &self.box.children.values()[header_index].widget.repo_header; 550 const stack = &self.box.children.values()[stack_index].widget.stack; 551 552 // each header tab maps 1:1 to a stack child by position 553 if (header.getSelectedIndex()) |index| 554 stack.getFocus().child_id = stack.children.keys()[index]; 555 try self.box.build(allocator, constraint, root_focus); 556 } 557 558 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 559 const stack = &self.box.children.values()[stack_index].widget.stack; 560 if (self.getFocus().child_id) |child_id| { 561 if (self.box.children.getIndex(child_id)) |current_index| { 562 const child = &self.box.children.values()[current_index].widget; 563 var index = current_index; 564 565 const direction = inp.vertDirection(key); 566 567 switch (direction) { 568 .up => { 569 switch (child.*) { 570 .repo_header => { 571 try child.input(allocator, key, root_focus); 572 }, 573 .stack => { 574 if (child.stack.getSelected()) |selected_widget| { 575 if (selected_widget.atTop(root_focus)) { 576 index = header_index; 577 } else { 578 try child.input(allocator, key, root_focus); 579 } 580 } 581 }, 582 else => {}, 583 } 584 }, 585 .down => { 586 switch (child.*) { 587 .repo_header => { 588 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 589 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 590 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 591 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 592 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 593 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 594 .repo_settings => |*v| if (v.focusHeader(root_focus)) return, 595 else => {}, 596 }; 597 index = stack_index; 598 }, 599 .stack => { 600 try child.input(allocator, key, root_focus); 601 }, 602 else => {}, 603 } 604 }, 605 .none => { 606 try child.input(allocator, key, root_focus); 607 }, 608 } 609 610 if (index != current_index) { 611 root_focus.setFocus(self.box.children.keys()[index]); 612 } 613 } 614 } 615 } 616 617 pub fn clearGrid(self: *View) void { 618 self.box.clearGrid(); 619 } 620 621 pub fn getGrid(self: View) ?Grid { 622 return self.box.getGrid(); 623 } 624 625 pub fn getFocus(self: *View) *Focus { 626 return self.box.getFocus(); 627 } 628 }; 629