╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
620 } 621 622 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 623 _ = allocator; 624 if (self.diffActive()) { 625 try self.diffInput(key, root_focus); 626 } else { 627 try self.listInput(key, root_focus); 628 } 629 } 630 631 fn listInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 632 // up/down (and the scroll wheel) move the selection a row; page up/down 633 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 634 // on the "next" row become navigation in the host before reaching here. 635 switch (key) { 636 .enter => if (self.selectedCommitIndex() != null) 637 try self.focusDiff(root_focus) 638 else if (self.data.next_start) |next| { 639 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, next, 0)) |route| 640 try self.session.navigate(route); 641 }, 642 .arrow_right => try self.focusDiff(root_focus), 643 .arrow_down => try self.moveSelection(root_focus, 1), 644 .arrow_up => try self.moveSelection(root_focus, -1), 645 .page_down => try self.moveSelection(root_focus, 10), 646 .page_up => try self.moveSelection(root_focus, -10), 647 .end => try self.moveSelection(root_focus, @intCast(self.listBox().children.count())), 648 .home => try self.moveSelection(root_focus, -@as(isize, @intCast(self.listBox().children.count()))), 649 .mouse => |mouse| switch (mouse.action) { 650 .scroll => |dir| try self.moveSelection(root_focus, if (dir == .up) -1 else 1), 651 else => {}, 652 }, 653 else => {}, 654 } 655 } 656 657 fn diffInput(self: *View, key: inp.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.clampDiffScroll(); 665 } else try self.focusList(root_focus); 666 }, 667 .arrow_right => { 668 sc.x += 1; 669 self.clampDiffScroll(); 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.clampDiffScroll(); 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.clampDiffScroll(); 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.clampDiffScroll(); 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 // keep the diff scroll within its content, using the last build's grids. the 783 // scroll bar's reserved column/row isn't part of the content viewport, so 784 // exclude it (as scrollToRect does) or the last column/row stays unreachable. 785 fn clampDiffScroll(self: *View) void { 786 const sc = self.diffScroll(); 787 const vp = sc.grid orelse return; 788 const content = sc.child.box.grid orelse return; 789 const view_w = vp.size.width - sc.bar_w; 790 const view_h = vp.size.height - sc.bar_h; 791 const max_y: isize = if (content.size.height > view_h) @intCast(content.size.height - view_h) else 0; 792 const max_x: isize = if (content.size.width > view_w) @intCast(content.size.width - view_w) else 0; 793 sc.y = std.math.clamp(sc.y, 0, max_y); 794 sc.x = std.math.clamp(sc.x, 0, max_x); 795 } 796 797 // enter the diff pane. the host arrives here on right-arrow or Enter from the 798 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 799 // case where the pane isn't laid out yet (it gets selected, then focused after 800 // the next build, landing on its remembered hunk). 801 fn focusDiff(self: *View, root_focus: *Focus) !void { 802 if (self.diffInner().children.count() == 0) return; 803 root_focus.setFocus(self.diffOuter().getFocus().id); 804 } 805 806 // return to the list. 807 fn focusList(self: *View, root_focus: *Focus) !void { 808 root_focus.setFocus(self.listScroll().getFocus().id); 809 } 810 811 fn moveSelection(self: *View, root_focus: *Focus, delta: isize) !void { 812 const lb = self.listBox(); 813 const keys = lb.children.keys(); 814 if (keys.len == 0) return; 815 const cur_id = lb.getFocus().child_id orelse return; 816 const cur: isize = @intCast(lb.children.getIndex(cur_id) orelse return); 817 const last: isize = @intCast(keys.len - 1); 818 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 819 if (next == @as(usize, @intCast(cur))) return; 820 root_focus.setFocus(keys[next]); 821 if (lb.children.values()[next].rect) |rect| self.listScroll().scrollToRect(rect); 822 } 823 824 pub fn clearGrid(self: *View) void { 825 self.box.clearGrid(); 826 } 827 828 pub fn getGrid(self: View) ?Grid { 829 return self.box.getGrid(); 830 } 831 832 pub fn getFocus(self: *View) *Focus { 833 return self.box.getFocus(); 834 } 835 836 // for the parent's "scroll up at the top jumps to the header" check. in the 837 // diff pane that means the first hunk is focused and the diff can't scroll up 838 // any further; otherwise up-arrow stays in the pane (scrolling or moving 839 // between hunks). when the list holds focus, report its selection directly. 840 pub fn getSelectedIndex(self: *View) ?usize { 841 if (self.diffActive()) { 842 const inner = self.diffInner(); 843 const cid = inner.getFocus().child_id orelse return 1; 844 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 845 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 846 } 847 const lb = self.listBox(); 848 const cid = lb.getFocus().child_id orelse return null; 849 return lb.children.getIndex(cid); 850 } 851 }; 852 853 // the "a:" navigation link for the commits page walking from commit `oid` within 854 // `identity`, showing the selected commit's first `after` hunks (0 = default). 855 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, after: usize) ![]const u8 { 856 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, after) orelse return error.RouteTooLong; 857 const url = try route.urlAlloc(page_arena); 858 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 859 } 860 861 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 862 // directory, within `identity` ("owner/name"). 863 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 864 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 865 const url = try route.urlAlloc(page_arena); 866 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 867 } 868 869 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 870 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 871 // in-page (crossPageLink ignores it); the href is only followed with js off. 872 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 873 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0) orelse return error.RouteTooLong; 874 const url = try route.urlAlloc(page_arena); 875 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 876 } 877