╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
569 // rather than shrinking to its content; its scroll fills the pane. when 570 // too narrow for both, it fills the whole width. 571 if (constraint.max_size.width) |w| { 572 self.contentBox().children.values()[diff_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 573 } else { 574 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_min_width, .height = null }; 575 } 576 577 // the web bounds the layout to the browser viewport like the terminal; 578 // each Scroll's web-native mode hands its full content to a real 579 // scrollable element, so we no longer build unbounded here. 580 // 581 // crossing panes only re-selects the content box's child (focusDiff / 582 // focusList); when the window is too narrow to show both, the pane that 583 // held focus is dropped here. the framework recovers focus after the 584 // top-level build by re-deriving it down the selected-child chain, so 585 // there's nothing to fix up afterward. 586 try self.box.build(allocator, constraint, root_focus); 587 } 588 589 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 590 const sel = self.selectedCommitIndex() orelse return; 591 if (self.diffed_index) |d| if (d == sel) return; 592 try self.populateDiff(allocator, sel); 593 self.diffed_index = sel; 594 } 595 596 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 597 const commit = self.data.commits[sel]; 598 const inner = self.diffInner(); 599 600 for (inner.children.values()) |*child| child.widget.deinit(allocator); 601 inner.children.clearAndFree(allocator); 602 inner.getFocus().child_id = null; 603 604 // the first row always links to viewing the repo's files at this commit. 605 try self.addViewFilesLink(allocator, inner, commit.oid); 606 607 // a "previous" row and a "next" row at the bottom reload the page on the 608 // adjacent diff window. 609 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page); 610 for (commit.hunks) |hunk| { 611 if (hunk.path) |path| try self.addPathBox(allocator, inner, path); 612 try self.addHunkBox(allocator, inner, hunk.lines); 613 } 614 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page); 615 616 // point the pane at its first row so focus recovery can land here. 617 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 618 619 // reset the scroll to the top for the newly-shown commit: directly on the 620 // terminal (the wasm offset), and via a version bump on the web (so the 621 // renderer's scroll id changes and JS drops the preserved position). 622 const sc = self.diffScroll(); 623 sc.x = 0; 624 sc.y = 0; 625 sc.getFocus().version +%= 1; 626 } 627 628 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 629 _ = allocator; 630 if (self.diffActive()) { 631 try self.diffInput(key, root_focus); 632 } else { 633 try self.listInput(key, root_focus); 634 } 635 } 636 637 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 638 // up/down (and the scroll wheel) move the selection a row; page up/down 639 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 640 // on the "next" row become navigation in the host before reaching here. 641 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 642 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 643 return; 644 } 645 switch (key) { 646 .enter => if (self.selectedCommitIndex() != null) 647 try self.focusDiff(root_focus) 648 else if (self.data.next_start) |next| { 649 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, next, 0)) |route| 650 try self.session.navigate(route); 651 }, 652 .arrow_right => try self.focusDiff(root_focus), 653 else => {}, 654 } 655 } 656 657 fn diffInput(self: *View, key: Key, root_focus: *Focus) !void { 658 const sc = self.diffScroll(); 659 switch (key) { 660 // left scrolls horizontally, then leaves for the list once flush left. 661 .arrow_left => { 662 if (sc.x > 0) { 663 sc.x -= 1; 664 self.diffScroll().clampToContent(); 665 } else try self.focusList(root_focus); 666 }, 667 .arrow_right => { 668 sc.x += 1; 669 self.diffScroll().clampToContent(); 670 }, 671 .arrow_up => try self.moveDiff(root_focus, -1), 672 .arrow_down => try self.moveDiff(root_focus, 1), 673 .page_up => try self.pageDiff(root_focus, -10), 674 .page_down => try self.pageDiff(root_focus, 10), 675 .home => try self.jumpDiff(root_focus, false), 676 .end => try self.jumpDiff(root_focus, true), 677 // Enter / a click on a "previous"/"next" row follow its "a:" link in 678 // the host (it reloads the adjacent window), so they don't reach here. 679 .mouse => |mouse| switch (mouse.action) { 680 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 681 else => {}, 682 }, 683 else => {}, 684 } 685 } 686 687 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 688 // line count unless there is a new hunk visible (in which case it is a 689 // hunk count). on the web `delta` is always a hunk count. 690 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 691 const inner = self.diffInner(); 692 const keys = inner.children.keys(); 693 if (keys.len == 0) return; 694 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 695 const target = @as(isize, @intCast(cur)) + delta; 696 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 697 698 if (self.session.is_terminal) { 699 if (in_range and self.hunkVisible(@intCast(target))) { 700 root_focus.setFocus(keys[@intCast(target)]); 701 return; 702 } 703 704 const sc = self.diffScroll(); 705 sc.y += delta * 5; // magnify because it's a line count 706 self.diffScroll().clampToContent(); 707 if (in_range and self.hunkVisible(@intCast(target))) { 708 root_focus.setFocus(keys[@intCast(target)]); 709 } 710 } else { 711 if (in_range) { 712 root_focus.setFocus(keys[@intCast(target)]); 713 } 714 } 715 } 716 717 // page a fixed number of lines, then focus the leading visible hunk 718 // (bottom-most when paging down, top-most when paging up). in the 719 // terminal, `delta` is a line count, and on the web it's a hunk count. 720 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 721 if (self.session.is_terminal) { 722 const sc = self.diffScroll(); 723 sc.y += delta * 5; // magnify because it's a line count 724 self.diffScroll().clampToContent(); 725 try self.focusVisible(root_focus, delta > 0); 726 } else { 727 const inner = self.diffInner(); 728 const keys = inner.children.keys(); 729 if (keys.len == 0) return; 730 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 731 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 732 root_focus.setFocus(keys[target]); 733 } 734 } 735 736 // jump to the first or last hunk. on the web the browser scrolls to the 737 // focused hunk; on the terminal pin the scroll to the top/bottom too. 738 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 739 const inner = self.diffInner(); 740 const keys = inner.children.keys(); 741 if (keys.len == 0) return; 742 if (self.session.is_terminal) { 743 const sc = self.diffScroll(); 744 sc.y = if (to_end) std.math.maxInt(isize) else 0; 745 self.diffScroll().clampToContent(); 746 } 747 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 748 } 749 750 // whether hunk `index` is at least partly within the diff viewport, per the 751 // last build's layout rects (content space) and the current scroll offset. 752 fn hunkVisible(self: *View, index: usize) bool { 753 const inner = self.diffInner(); 754 const sc = self.diffScroll(); 755 const vp = sc.grid orelse return false; 756 const r = inner.children.values()[index].rect orelse return false; 757 const top = sc.y; 758 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 759 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 760 } 761 762 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 763 // the bottom-most visible (for downward motion), else the top-most. 764 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 765 const inner = self.diffInner(); 766 const sc = self.diffScroll(); 767 const vp = sc.grid orelse return; 768 const top = sc.y; 769 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 770 var chosen: ?usize = null; 771 for (inner.children.keys(), inner.children.values()) |id, *child| { 772 const r = child.rect orelse continue; // content-space layout rect 773 const r_top = r.y; 774 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 775 if (r_bot <= top or r_top >= bottom) continue; // not visible 776 chosen = id; 777 if (!prefer_last) break; // first visible 778 } 779 if (chosen) |id| root_focus.setFocus(id); 780 } 781 782 // enter the diff pane. the host arrives here on right-arrow or Enter from the 783 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 784 // case where the pane isn't laid out yet (it gets selected, then focused after 785 // the next build, landing on its remembered hunk). 786 fn focusDiff(self: *View, root_focus: *Focus) !void { 787 if (self.diffInner().children.count() == 0) return; 788 root_focus.setFocus(self.diffOuter().getFocus().id); 789 } 790 791 // return to the list. 792 fn focusList(self: *View, root_focus: *Focus) !void { 793 root_focus.setFocus(self.listScroll().getFocus().id); 794 } 795 796 pub fn clearGrid(self: *View) void { 797 self.box.clearGrid(); 798 } 799 800 pub fn getGrid(self: View) ?Grid { 801 return self.box.getGrid(); 802 } 803 804 pub fn getFocus(self: *View) *Focus { 805 return self.box.getFocus(); 806 } 807 808 // for the parent's "scroll up at the top jumps to the header" check. in the 809 // diff pane that means the first hunk is focused and the diff can't scroll up 810 // any further; otherwise up-arrow stays in the pane (scrolling or moving 811 // between hunks). when the list holds focus, report its selection directly. 812 pub fn getSelectedIndex(self: *View) ?usize { 813 if (self.diffActive()) { 814 const inner = self.diffInner(); 815 const cid = inner.getFocus().child_id orelse return 1; 816 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 817 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 818 } 819 const lb = self.listBox(); 820 const cid = lb.getFocus().child_id orelse return null; 821 return lb.children.getIndex(cid); 822 } 823 }; 824 825 // the "a:" navigation link for the commits page walking from commit `oid` within 826 // `identity`, showing the selected commit's first `after` hunks (0 = default). 827 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, after: usize) ![]const u8 { 828 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, after) orelse return error.RouteTooLong; 829 const url = try route.urlAlloc(page_arena); 830 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 831 } 832 833 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 834 // directory, within `identity` ("owner/name"). 835 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 836 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 837 const url = try route.urlAlloc(page_arena); 838 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 839 } 840 841 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 842 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 843 // in-page (crossPageLink ignores it); the href is only followed with js off. 844 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 845 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0) orelse return error.RouteTooLong; 846 const url = try route.urlAlloc(page_arena); 847 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 848 } 849