╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
482 483 // the viewer a tab's comment bar sees, which is none below the tab's role 484 fn tabViewer(handle: ui.RepoHandle, repo: evt.Repo.Record, kind: evt.EventKind) ?ui.Viewer { 485 const viewer = handle.viewer orelse return null; 486 const tab_role = repo.event.threadRole(kind) orelse return null; 487 return if (viewer.role.atLeast(tab_role)) viewer else null; 488 } 489 490 pub const View = struct { 491 box: wgt.Box(ui.Widget), 492 493 const header_index: usize = 0; 494 const stack_index: usize = 1; 495 496 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 497 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 498 errdefer box.deinit(allocator); 499 500 // build the header first so we can grab the files-tab id for the user 501 // view (it focuses there after login). 502 { 503 var header_view = try Header.View.init(allocator, data, session); 504 errdefer header_view.deinit(allocator); 505 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 506 } 507 508 { 509 var stack = try wgt.Stack(ui.Widget).init(allocator); 510 errdefer stack.deinit(allocator); 511 512 // files — the default tab: the current directory's listing. 513 { 514 var files_view = try Files.View.init(allocator, &data.files, session); 515 errdefer files_view.deinit(allocator); 516 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 517 } 518 519 switch (data.changes) { 520 .diff => |*diff| { 521 var diff_view = try Diff.View.init(allocator, diff, session); 522 errdefer diff_view.deinit(allocator); 523 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 524 }, 525 .commits => |*commits| { 526 var commits_view = try Commits.View.init(allocator, commits, session); 527 errdefer commits_view.deinit(allocator); 528 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 529 }, 530 } 531 532 // refs — the repo's branches and tags. 533 { 534 var refs_view = try Refs.View.init(allocator, &data.refs, session); 535 errdefer refs_view.deinit(allocator); 536 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 537 } 538 539 // discussions and their comment permalinks. 540 if (data.repo.event.discuss_role != null) { 541 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 542 errdefer discussions_view.deinit(allocator); 543 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 544 } 545 546 // issues — the repo's issue tracker and comment permalinks. 547 if (data.repo.event.issue_role != null) { 548 var issues_view = try Issues.View.init(allocator, &data.issues, session); 549 errdefer issues_view.deinit(allocator); 550 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 551 } 552 553 // patches and their comment permalinks. 554 if (data.repo.event.patch_role != null) { 555 var patches_view = try Patches.View.init(allocator, &data.patches, session); 556 errdefer patches_view.deinit(allocator); 557 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 558 } 559 560 if (data.handle.canWrite()) { 561 var events_view = try Events.View.init(allocator, &data.events, session); 562 errdefer events_view.deinit(allocator); 563 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 564 } 565 566 if (data.undo) |*undo| { 567 var undo_view = try Undo.View.init(allocator, undo, session); 568 errdefer undo_view.deinit(allocator); 569 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 570 } 571 572 if (data.settings) |*settings| { 573 var settings_view = try Settings.View.init(allocator, settings, session); 574 errdefer settings_view.deinit(allocator); 575 try stack.children.put(allocator, settings_view.getFocus().id, .{ .repo_settings = settings_view }); 576 } 577 578 // the header shows new repo with a login, and new user without one 579 // and user only outside local mode, so keep the stack's children 580 // 1:1 with the tabs by skipping the same views. 581 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.owner_name, data.header.name }); 582 if (session.data.user_id != null) { 583 const route = ui.RoutablePage{ .repo_repo_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 584 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 585 errdefer new_repo_view.deinit(allocator); 586 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 587 } else if (session.data.host_kind == .server) { 588 const route = ui.RoutablePage{ .repo_user_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 589 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 590 errdefer new_user_view.deinit(allocator); 591 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 592 } 593 594 if (session.data.host_kind == .server) { 595 const route = ui.RoutablePage{ .repo_user = .{ .name = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong } }; 596 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, route); 597 errdefer user_view.deinit(allocator); 598 try stack.children.put(allocator, user_view.getFocus().id, user_view); 599 } 600 601 if (session.is_terminal) { 602 var quit_view = try Quit.View.init(allocator, session); 603 errdefer quit_view.deinit(allocator); 604 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 605 } 606 607 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 608 } 609 610 var self = View{ .box = box }; 611 // a page opens on its tabs, except that search results open in the 612 // tab's own search box. 613 const results = data.files.find != null or 614 (data.changes == .commits and data.changes.commits.search != null) or 615 data.discussions.search != null or data.issues.search != null or data.patches.search != null; 616 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 617 return self; 618 } 619 620 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 621 self.box.deinit(allocator); 622 } 623 624 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 625 self.clearGrid(); 626 const header = &self.box.children.values()[header_index].widget.repo_header; 627 const stack = &self.box.children.values()[stack_index].widget.stack; 628 629 // each header tab maps 1:1 to a stack child by position 630 if (header.getSelectedIndex()) |index| 631 stack.getFocus().child_id = stack.children.keys()[index]; 632 try self.box.build(allocator, constraint, root_focus); 633 } 634 635 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 636 const stack = &self.box.children.values()[stack_index].widget.stack; 637 if (self.getFocus().child_id) |child_id| { 638 if (self.box.children.getIndex(child_id)) |current_index| { 639 const child = &self.box.children.values()[current_index].widget; 640 var index = current_index; 641 642 const direction = inp.vertDirection(key); 643 644 switch (direction) { 645 .up => { 646 switch (child.*) { 647 .repo_header => { 648 try child.input(allocator, key, root_focus); 649 }, 650 .stack => { 651 if (child.stack.getSelected()) |selected_widget| { 652 if (selected_widget.atTop(root_focus)) { 653 index = header_index; 654 } else { 655 try child.input(allocator, key, root_focus); 656 } 657 } 658 }, 659 else => {}, 660 } 661 }, 662 .down => { 663 switch (child.*) { 664 .repo_header => { 665 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 666 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 667 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 668 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 669 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 670 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 671 .repo_settings => |*v| if (v.focusHeader(root_focus)) return, 672 .user_notifications => |*v| if (v.focusHeader(root_focus)) return, 673 else => {}, 674 }; 675 index = stack_index; 676 }, 677 .stack => { 678 try child.input(allocator, key, root_focus); 679 }, 680 else => {}, 681 } 682 }, 683 .none => { 684 try child.input(allocator, key, root_focus); 685 }, 686 } 687 688 if (index != current_index) { 689 root_focus.setFocus(self.box.children.keys()[index]); 690 } 691 } 692 } 693 } 694 695 pub fn clearGrid(self: *View) void { 696 self.box.clearGrid(); 697 } 698 699 pub fn getGrid(self: View) ?Grid { 700 return self.box.getGrid(); 701 } 702 703 pub fn getFocus(self: *View) *Focus { 704 return self.box.getFocus(); 705 } 706 }; 707