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