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