╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
538 const link = try commitsLink(self.session.page_arena, self.data.location, oid, 0, path); 539 var tb = try wgt.TextBox.init(allocator, path, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 540 errdefer tb.deinit(allocator); 541 tb.getFocus().mode = .all; 542 tb.getFocus().kind = .{ .custom = link }; 543 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 544 } 545 546 // a focusable window-navigation row ("previous"/"next"). it's a link to this 547 // commit's route at `target_start`, so activating it (the host follows the 548 // "a:" link) reloads the page on the adjacent window — same on TUI and web. 549 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, oid: []const u8, target_start: usize, path: []const u8) !void { 550 const link = try commitsLink(self.session.page_arena, self.data.location, oid, target_start, path); 551 var tb = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 552 errdefer tb.deinit(allocator); 553 tb.getFocus().mode = .all; 554 tb.getFocus().kind = .{ .custom = link }; 555 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 556 } 557 558 // a focusable row for the top of the diff pane linking to the files tab at 559 // this commit's tree, so its files are always viewable as of this object. 560 fn addViewFilesLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), oid: []const u8) !void { 561 const link = try filesObjectLink(self.session.page_arena, self.data.location, oid); 562 var tb = try wgt.TextBox.init(allocator, "view files at this commit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 563 errdefer tb.deinit(allocator); 564 tb.getFocus().mode = .all; 565 tb.getFocus().kind = .{ .custom = link }; 566 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 567 } 568 569 // how much of the message a pane's box holds: a truncated preview links to 570 // the pane holding the whole thing, which is that link's destination. 571 const MessageBox = enum { preview, whole }; 572 573 // the selected commit's message. 574 fn addMessageBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), commit: Commit, kind: MessageBox) !void { 575 // a truncated message keeps its box even with no whole line to show, 576 // since the box is what links to the whole thing. 577 if (commit.message.len == 0 and !commit.message_truncated) return; 578 const cut_short = commit.message_truncated and kind == .preview; 579 var tb = try messageBox(allocator, commit.message, if (cut_short) " click or press enter to see more " else ""); 580 errdefer tb.deinit(allocator); 581 if (cut_short) tb.getFocus().kind = .{ .custom = try messageLink(self.session.page_arena, self.data.location, commit.oid) }; 582 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 583 } 584 585 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 586 self.box.deinit(allocator); 587 } 588 589 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 590 return &self.box.children.values()[content_index].widget.box; 591 } 592 593 fn header(self: *View) *Header.View { 594 return &self.box.children.values()[header_index].widget.repo_commits_header; 595 } 596 597 fn headerActive(self: *View) bool { 598 return self.box.getFocus().child_id == self.header().getFocus().id; 599 } 600 601 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 602 return &self.contentBox().children.values()[list_index].widget.scroll; 603 } 604 605 fn listBox(self: *View) *wgt.Box(ui.Widget) { 606 return &self.listScroll().child.box; 607 } 608 609 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 610 return &self.contentBox().children.values()[diff_index].widget.box; 611 } 612 613 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 614 return &self.diffOuter().children.values()[0].widget.scroll; 615 } 616 617 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 618 return &self.diffScroll().child.box; 619 } 620 621 fn diffActive(self: *View) bool { 622 const content = self.contentBox(); 623 const cid = content.getFocus().child_id orelse return false; 624 return content.children.getIndex(cid) == diff_index; 625 } 626 627 // what the pane shows for the commit at `sel`. the page's content only 628 // applies to the commit it walks from; the rest show their plain diff. 629 fn paneContent(self: *View, sel: usize) Self.Content { 630 return if (sel == 0) self.data.content else .{ .diff = .{} }; 631 } 632 633 // the selected commit's index, or null when the "next" row is selected. 634 fn selectedCommitIndex(self: *View) ?usize { 635 const lb = self.listBox(); 636 const cid = lb.getFocus().child_id orelse return null; 637 const idx = lb.children.getIndex(cid) orelse return null; 638 if (idx >= self.data.commits.len) return null; 639 return idx; 640 } 641 642 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 643 self.clearGrid(); 644 645 // swap the diff pane to the selected commit when it changes. 646 try self.refreshDiff(allocator); 647 648 // the selected list row shows a border (the focused TextBox upgrades it 649 // to a double border itself); the rest stay borderless. 650 const lb = self.listBox(); 651 for (lb.children.keys(), lb.children.values()) |id, *child| { 652 switch (child.widget) { 653 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 654 else => {}, 655 } 656 } 657 658 // cap the list at list_max_width only while the diff pane fits beside it. 659 // the box drops the diff when the width can't hold both minimums, so when 660 // it's that narrow we lift the cap and let the list fill the whole width. 661 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 662 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 663 664 // stretch the diff pane across the rest of the width so it fills the area 665 // rather than shrinking to its content; its scroll fills the pane. when 666 // too narrow for both, it fills the whole width. 667 const diff_width: usize = if (constraint.max_size.width) |w| 668 (if (both_panes_fit) w - list_max_width else w) 669 else 670 diff_min_width; 671 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_width, .height = null }; 672 673 // the message is the pane's only wrapping row, and wrapping needs a 674 // bounded width, which the pane's scroll doesn't grant (it scrolls 675 // horizontally too). cap it to the pane, leaving room for the border 676 // and the scrollbar column. re-capped each build so it tracks resizes. 677 for (self.diffInner().children.values()) |*child| switch (child.widget) { 678 .text_box => |text_box| if (text_box.options.wrap_kind == .word) { 679 child.max_size = .{ .width = diff_width -| 3, .height = null }; 680 }, 681 else => {}, 682 }; 683 684 // the web bounds the layout to the browser viewport like the terminal; 685 // each Scroll's web-native mode hands its full content to a real 686 // scrollable element, so we no longer build unbounded here. 687 // 688 // crossing panes only re-selects the content box's child (focusDiff / 689 // focusList); when the window is too narrow to show both, the pane that 690 // held focus is dropped here. the framework recovers focus after the 691 // top-level build by re-deriving it down the selected-child chain, so 692 // there's nothing to fix up afterward. 693 try self.box.build(allocator, constraint, root_focus); 694 } 695 696 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 697 const sel = self.selectedCommitIndex() orelse return; 698 if (self.diffed_index) |d| if (d == sel) return; 699 try self.populateDiff(allocator, sel); 700 self.diffed_index = sel; 701 } 702 703 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 704 const commit = self.data.commits[sel]; 705 const inner = self.diffInner(); 706 707 for (inner.children.values()) |*child| child.widget.deinit(allocator); 708 inner.children.clearAndFree(allocator); 709 inner.getFocus().child_id = null; 710 711 if (commit.stats) |stats| { 712 const text = try std.fmt.allocPrint(self.session.page_arena.allocator(), "+{d}, -{d}", .{ stats.lines_added, stats.lines_removed }); 713 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " lines changed " }); 714 errdefer tb.deinit(allocator); 715 tb.getFocus().mode = .all; 716 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 717 } 718 719 switch (self.paneContent(sel)) { 720 .message => { 721 try self.addNavLink(allocator, inner, "← back to diff", commit.oid, 0, ""); 722 try self.addMessageBox(allocator, inner, commit, .whole); 723 }, 724 .diff => |d| { 725 if (d.path.len != 0) { 726 // the file's path, then a row returning to this commit's unfiltered diff. 727 var label = try wgt.TextBox.init(allocator, d.path, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 728 errdefer label.deinit(allocator); 729 label.getFocus().mode = .all; 730 try inner.children.put(allocator, label.getFocus().id, .{ .widget = .{ .text_box = label }, .rect = null, .min_size = null }); 731 try self.addNavLink(allocator, inner, "← all files", commit.oid, 0, ""); 732 } else { 733 try self.addMessageBox(allocator, inner, commit, .preview); 734 if (commit.author != .unknown) { 735 var tb = try ui.authorBox(allocator, self.session.page_arena, commit.author); 736 errdefer tb.deinit(allocator); 737 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 738 } 739 try self.addViewFilesLink(allocator, inner, commit.oid); 740 } 741 742 // a "previous" row and a "next" row at the bottom reload the page 743 // on the adjacent diff window. 744 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page, d.path); 745 for (commit.hunks) |hunk| { 746 if (d.path.len == 0) if (hunk.path) |hunk_path| try self.addPathBox(allocator, inner, hunk_path, commit.oid); 747 try self.addHunkBox(allocator, inner, hunk.lines); 748 } 749 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page, d.path); 750 }, 751 } 752 753 // point the pane at its first row so focus recovery can land here. 754 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 755 756 // reset the scroll to the top for the newly-shown commit: directly on the 757 // terminal (the wasm offset), and via a version bump on the web (so the 758 // renderer's scroll id changes and JS drops the preserved position). 759 const sc = self.diffScroll(); 760 sc.x = 0; 761 sc.y = 0; 762 sc.getFocus().version +%= 1; 763 } 764 765 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 766 if (self.headerActive()) { 767 if (key == .arrow_down) { 768 root_focus.setFocus(self.contentBox().getFocus().id); 769 } else { 770 try self.header().input(allocator, key, root_focus); 771 } 772 return; 773 } 774 if (key == .arrow_up and self.contentAtTop() and self.header().focusCloneUrl(root_focus)) return; 775 if (self.diffActive()) { 776 try self.diffInput(key, root_focus); 777 } else { 778 try self.listInput(key, root_focus); 779 } 780 } 781 782 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 783 // up/down (and the scroll wheel) move the selection a row; page up/down 784 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 785 // on the "next" row become navigation in the host before reaching here. 786 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 787 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 788 return; 789 } 790 switch (key) { 791 .enter => if (self.selectedCommitIndex() != null) 792 try self.focusDiff(root_focus) 793 else if (self.data.next_start) |next| { 794 if (self.data.location.commitsRoute(.object, next, 0, "")) |route| 795 try self.session.navigate(route); 796 }, 797 .arrow_right => try self.focusDiff(root_focus), 798 else => {}, 799 } 800 } 801 802 fn diffInput(self: *View, key: Key, root_focus: *Focus) !void { 803 const sc = self.diffScroll(); 804 switch (key) { 805 // left scrolls horizontally, then leaves for the list once flush left. 806 .arrow_left => { 807 if (sc.x > 0) { 808 sc.x -= 1; 809 self.diffScroll().clampToContent(); 810 } else try self.focusList(root_focus); 811 }, 812 .arrow_right => { 813 sc.x += 1; 814 self.diffScroll().clampToContent(); 815 }, 816 .arrow_up => try self.moveDiff(root_focus, -1), 817 .arrow_down => try self.moveDiff(root_focus, 1), 818 .page_up => try self.pageDiff(root_focus, -10), 819 .page_down => try self.pageDiff(root_focus, 10), 820 .home => try self.jumpDiff(root_focus, false), 821 .end => try self.jumpDiff(root_focus, true), 822 // Enter / a click on a "previous"/"next" row follow its "a:" link in 823 // the host (it reloads the adjacent window), so they don't reach here. 824 .mouse => |mouse| switch (mouse.action) { 825 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 826 else => {}, 827 }, 828 else => {}, 829 } 830 } 831 832 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 833 // line count unless there is a new hunk visible (in which case it is a 834 // hunk count). on the web `delta` is always a hunk count. 835 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 836 const inner = self.diffInner(); 837 const keys = inner.children.keys(); 838 if (keys.len == 0) return; 839 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 840 const target = @as(isize, @intCast(cur)) + delta; 841 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 842 843 if (self.session.is_terminal) { 844 if (in_range and self.hunkVisible(@intCast(target))) { 845 root_focus.setFocus(keys[@intCast(target)]); 846 return; 847 } 848 849 const sc = self.diffScroll(); 850 sc.y += delta * 5; // magnify because it's a line count 851 self.diffScroll().clampToContent(); 852 if (in_range and self.hunkVisible(@intCast(target))) { 853 root_focus.setFocus(keys[@intCast(target)]); 854 } 855 } else { 856 if (in_range) { 857 root_focus.setFocus(keys[@intCast(target)]); 858 } 859 } 860 } 861 862 // page a fixed number of lines, then focus the leading visible hunk 863 // (bottom-most when paging down, top-most when paging up). in the 864 // terminal, `delta` is a line count, and on the web it's a hunk count. 865 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 866 if (self.session.is_terminal) { 867 const sc = self.diffScroll(); 868 sc.y += delta * 5; // magnify because it's a line count 869 self.diffScroll().clampToContent(); 870 try self.focusVisible(root_focus, delta > 0); 871 } else { 872 const inner = self.diffInner(); 873 const keys = inner.children.keys(); 874 if (keys.len == 0) return; 875 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 876 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 877 root_focus.setFocus(keys[target]); 878 } 879 } 880 881 // jump to the first or last hunk. on the web the browser scrolls to the 882 // focused hunk; on the terminal pin the scroll to the top/bottom too. 883 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 884 const inner = self.diffInner(); 885 const keys = inner.children.keys(); 886 if (keys.len == 0) return; 887 if (self.session.is_terminal) { 888 const sc = self.diffScroll(); 889 sc.y = if (to_end) std.math.maxInt(isize) else 0; 890 self.diffScroll().clampToContent(); 891 } 892 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 893 } 894 895 // whether hunk `index` is at least partly within the diff viewport, per the 896 // last build's layout rects (content space) and the current scroll offset. 897 fn hunkVisible(self: *View, index: usize) bool { 898 const inner = self.diffInner(); 899 const sc = self.diffScroll(); 900 const vp = sc.grid orelse return false; 901 const r = inner.children.values()[index].rect orelse return false; 902 const top = sc.y; 903 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 904 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 905 } 906 907 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 908 // the bottom-most visible (for downward motion), else the top-most. 909 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 910 const inner = self.diffInner(); 911 const sc = self.diffScroll(); 912 const vp = sc.grid orelse return; 913 const top = sc.y; 914 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 915 var chosen: ?usize = null; 916 for (inner.children.keys(), inner.children.values()) |id, *child| { 917 const r = child.rect orelse continue; // content-space layout rect 918 const r_top = r.y; 919 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 920 if (r_bot <= top or r_top >= bottom) continue; // not visible 921 chosen = id; 922 if (!prefer_last) break; // first visible 923 } 924 if (chosen) |id| root_focus.setFocus(id); 925 } 926 927 // enter the diff pane. the host arrives here on right-arrow or Enter from the 928 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 929 // case where the pane isn't laid out yet (it gets selected, then focused after 930 // the next build, landing on its remembered hunk). 931 fn focusDiff(self: *View, root_focus: *Focus) !void { 932 if (self.diffInner().children.count() == 0) return; 933 root_focus.setFocus(self.diffOuter().getFocus().id); 934 } 935 936 // return to the list. 937 fn focusList(self: *View, root_focus: *Focus) !void { 938 root_focus.setFocus(self.listScroll().getFocus().id); 939 } 940 941 pub fn clearGrid(self: *View) void { 942 self.box.clearGrid(); 943 } 944 945 pub fn getGrid(self: View) ?Grid { 946 return self.box.getGrid(); 947 } 948 949 pub fn getFocus(self: *View) *Focus { 950 return self.box.getFocus(); 951 } 952 953 fn contentAtTop(self: *View) bool { 954 if (self.diffActive()) { 955 const inner = self.diffInner(); 956 const cid = inner.getFocus().child_id orelse return true; 957 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 958 return first_focused and self.diffScroll().y == 0; 959 } 960 const lb = self.listBox(); 961 const cid = lb.getFocus().child_id orelse return true; 962 return lb.children.getIndex(cid) == 0; 963 } 964 965 // only the clone-url row sits directly below the repository header. 966 pub fn atTop(self: *View) bool { 967 return self.headerActive() or (!self.header().hasCloneUrl() and self.contentAtTop()); 968 } 969 970 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 971 return self.header().focusCloneUrl(root_focus); 972 } 973 }; 974 975 // the "a:" navigation link for the commits page walking from commit `oid` within 976 // `location`, windowing the selected commit's diff from hunk `start`, filtered 977 // to `path` ("" = every file). 978 fn commitsLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 979 const route = location.commitsRoute(.object, oid, start, path) orelse return error.RouteTooLong; 980 const url = try route.toUrl(page_arena); 981 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 982 } 983 984 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 985 // directory, within `location`. 986 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 987 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 988 const url = try route.toUrl(page_arena); 989 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 990 } 991 992 // the in-page "ai:" anchor for selecting a commit with its current pane content 993 // and window. the href is only followed with js off. 994 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, commit: Commit, content: Content) ![]const u8 { 995 const route = (switch (content) { 996 .message => location.commitMessageRoute(.object, commit.oid), 997 .diff => |diff| location.commitsRoute(.object, commit.oid, commit.window_start, diff.path), 998 }) orelse return error.RouteTooLong; 999 const url = try route.toUrl(page_arena); 1000 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1001 } 1002 1003 // the "a:" link to the page showing `oid`'s message on its own. 1004 fn messageLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 1005 const route = location.commitMessageRoute(.object, oid) orelse return error.RouteTooLong; 1006 const url = try route.toUrl(page_arena); 1007 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1008 } 1009 1010 // the "<ref_or_oid> <value>" banner shown above the log. 1011 pub const Header = struct { 1012 content: []const u8, 1013 1014 // `value` arrives url-encoded, so decode it for display. 1015 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 1016 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 1017 return .{ 1018 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 1019 }; 1020 } 1021 1022 pub const View = struct { 1023 box: wgt.Box(ui.Widget), 1024 data: *const Header, 1025 1026 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !Header.View { 1027 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1028 errdefer box.deinit(allocator); 1029 1030 if (!session.data.is_local) { 1031 if (try ui.widget.CopyableText.initClone(allocator, session, location)) |value| { 1032 var clone_url = value; 1033 errdefer clone_url.deinit(allocator); 1034 const min_width = clone_url.minWidth(); 1035 box.getFocus().child_id = clone_url.getFocus().id; 1036 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 1037 } 1038 } 1039 1040 var spacer = try ui.widget.Spacer.init(allocator); 1041 errdefer spacer.deinit(allocator); 1042 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1043 1044 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 1045 errdefer text_box.deinit(allocator); 1046 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 1047 1048 return .{ .box = box, .data = data }; 1049 } 1050 1051 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 1052 self.box.deinit(allocator); 1053 } 1054 1055 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1056 self.clearGrid(); 1057 try self.box.build(allocator, constraint, root_focus); 1058 } 1059 1060 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1061 const clone_url = self.cloneUrl() orelse return; 1062 try clone_url.input(allocator, key, root_focus); 1063 } 1064 1065 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 1066 const clone_url = self.cloneUrl() orelse return false; 1067 root_focus.setFocus(clone_url.getFocus().id); 1068 return true; 1069 } 1070 1071 pub fn hasCloneUrl(self: *Header.View) bool { 1072 return self.cloneUrl() != null; 1073 } 1074 1075 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 1076 for (self.box.children.values()) |*child| switch (child.widget) { 1077 .copyable_text => |*copyable_text| return copyable_text, 1078 else => {}, 1079 }; 1080 return null; 1081 } 1082 1083 pub fn clearGrid(self: *Header.View) void { 1084 self.box.clearGrid(); 1085 } 1086 1087 pub fn getGrid(self: Header.View) ?Grid { 1088 return self.box.getGrid(); 1089 } 1090 1091 pub fn getFocus(self: *Header.View) *Focus { 1092 return self.box.getFocus(); 1093 } 1094 }; 1095 }; 1096