╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
611 // terminal (the wasm offset), and via a version bump on the web (so the 612 // renderer's scroll id changes and JS drops the preserved position). 613 const sc = self.diffScroll(); 614 sc.x = 0; 615 sc.y = 0; 616 sc.getFocus().version +%= 1; 617 } 618 619 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 620 _ = allocator; 621 if (self.diffActive()) { 622 try self.diffInput(key, root_focus); 623 } else { 624 try self.listInput(key, root_focus); 625 } 626 } 627 628 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 629 // up/down (and the scroll wheel) move the selection a row; page up/down 630 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 631 // on the "next" row become navigation in the host before reaching here. 632 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 633 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 634 return; 635 } 636 switch (key) { 637 .enter => if (self.selectedCommitIndex() != null) 638 try self.focusDiff(root_focus) 639 else if (self.data.next_start) |next| { 640 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, next, 0, "")) |route| 641 try self.session.navigate(route); 642 }, 643 .arrow_right => try self.focusDiff(root_focus), 644 else => {}, 645 } 646 } 647 648 fn diffInput(self: *View, key: Key, root_focus: *Focus) !void { 649 const sc = self.diffScroll(); 650 switch (key) { 651 // left scrolls horizontally, then leaves for the list once flush left. 652 .arrow_left => { 653 if (sc.x > 0) { 654 sc.x -= 1; 655 self.diffScroll().clampToContent(); 656 } else try self.focusList(root_focus); 657 }, 658 .arrow_right => { 659 sc.x += 1; 660 self.diffScroll().clampToContent(); 661 }, 662 .arrow_up => try self.moveDiff(root_focus, -1), 663 .arrow_down => try self.moveDiff(root_focus, 1), 664 .page_up => try self.pageDiff(root_focus, -10), 665 .page_down => try self.pageDiff(root_focus, 10), 666 .home => try self.jumpDiff(root_focus, false), 667 .end => try self.jumpDiff(root_focus, true), 668 // Enter / a click on a "previous"/"next" row follow its "a:" link in 669 // the host (it reloads the adjacent window), so they don't reach here. 670 .mouse => |mouse| switch (mouse.action) { 671 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 672 else => {}, 673 }, 674 else => {}, 675 } 676 } 677 678 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 679 // line count unless there is a new hunk visible (in which case it is a 680 // hunk count). on the web `delta` is always a hunk count. 681 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 682 const inner = self.diffInner(); 683 const keys = inner.children.keys(); 684 if (keys.len == 0) return; 685 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 686 const target = @as(isize, @intCast(cur)) + delta; 687 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 688 689 if (self.session.is_terminal) { 690 if (in_range and self.hunkVisible(@intCast(target))) { 691 root_focus.setFocus(keys[@intCast(target)]); 692 return; 693 } 694 695 const sc = self.diffScroll(); 696 sc.y += delta * 5; // magnify because it's a line count 697 self.diffScroll().clampToContent(); 698 if (in_range and self.hunkVisible(@intCast(target))) { 699 root_focus.setFocus(keys[@intCast(target)]); 700 } 701 } else { 702 if (in_range) { 703 root_focus.setFocus(keys[@intCast(target)]); 704 } 705 } 706 } 707 708 // page a fixed number of lines, then focus the leading visible hunk 709 // (bottom-most when paging down, top-most when paging up). in the 710 // terminal, `delta` is a line count, and on the web it's a hunk count. 711 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 712 if (self.session.is_terminal) { 713 const sc = self.diffScroll(); 714 sc.y += delta * 5; // magnify because it's a line count 715 self.diffScroll().clampToContent(); 716 try self.focusVisible(root_focus, delta > 0); 717 } else { 718 const inner = self.diffInner(); 719 const keys = inner.children.keys(); 720 if (keys.len == 0) return; 721 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 722 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 723 root_focus.setFocus(keys[target]); 724 } 725 } 726 727 // jump to the first or last hunk. on the web the browser scrolls to the 728 // focused hunk; on the terminal pin the scroll to the top/bottom too. 729 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 730 const inner = self.diffInner(); 731 const keys = inner.children.keys(); 732 if (keys.len == 0) return; 733 if (self.session.is_terminal) { 734 const sc = self.diffScroll(); 735 sc.y = if (to_end) std.math.maxInt(isize) else 0; 736 self.diffScroll().clampToContent(); 737 } 738 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 739 } 740 741 // whether hunk `index` is at least partly within the diff viewport, per the 742 // last build's layout rects (content space) and the current scroll offset. 743 fn hunkVisible(self: *View, index: usize) bool { 744 const inner = self.diffInner(); 745 const sc = self.diffScroll(); 746 const vp = sc.grid orelse return false; 747 const r = inner.children.values()[index].rect orelse return false; 748 const top = sc.y; 749 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 750 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 751 } 752 753 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 754 // the bottom-most visible (for downward motion), else the top-most. 755 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 756 const inner = self.diffInner(); 757 const sc = self.diffScroll(); 758 const vp = sc.grid orelse return; 759 const top = sc.y; 760 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 761 var chosen: ?usize = null; 762 for (inner.children.keys(), inner.children.values()) |id, *child| { 763 const r = child.rect orelse continue; // content-space layout rect 764 const r_top = r.y; 765 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 766 if (r_bot <= top or r_top >= bottom) continue; // not visible 767 chosen = id; 768 if (!prefer_last) break; // first visible 769 } 770 if (chosen) |id| root_focus.setFocus(id); 771 } 772 773 // enter the diff pane. the host arrives here on right-arrow or Enter from the 774 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 775 // case where the pane isn't laid out yet (it gets selected, then focused after 776 // the next build, landing on its remembered hunk). 777 fn focusDiff(self: *View, root_focus: *Focus) !void { 778 if (self.diffInner().children.count() == 0) return; 779 root_focus.setFocus(self.diffOuter().getFocus().id); 780 } 781 782 // return to the list. 783 fn focusList(self: *View, root_focus: *Focus) !void { 784 root_focus.setFocus(self.listScroll().getFocus().id); 785 } 786 787 pub fn clearGrid(self: *View) void { 788 self.box.clearGrid(); 789 } 790 791 pub fn getGrid(self: View) ?Grid { 792 return self.box.getGrid(); 793 } 794 795 pub fn getFocus(self: *View) *Focus { 796 return self.box.getFocus(); 797 } 798 799 // for the parent's "scroll up at the top jumps to the header" check. in the 800 // diff pane that means the first hunk is focused and the diff can't scroll up 801 // any further; otherwise up-arrow stays in the pane (scrolling or moving 802 // between hunks). when the list holds focus, report its selection directly. 803 pub fn getSelectedIndex(self: *View) ?usize { 804 if (self.diffActive()) { 805 const inner = self.diffInner(); 806 const cid = inner.getFocus().child_id orelse return 1; 807 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 808 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 809 } 810 const lb = self.listBox(); 811 const cid = lb.getFocus().child_id orelse return null; 812 return lb.children.getIndex(cid); 813 } 814 }; 815 816 // the "a:" navigation link for the commits page walking from commit `oid` within 817 // `identity`, windowing the selected commit's diff from hunk `start`, filtered 818 // to `path` ("" = every file). 819 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 820 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, start, path) orelse return error.RouteTooLong; 821 const url = try route.toUrl(page_arena); 822 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 823 } 824 825 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 826 // directory, within `identity` ("owner/name"). 827 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 828 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 829 const url = try route.toUrl(page_arena); 830 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 831 } 832 833 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 834 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 835 // in-page (crossPageLink ignores it); the href is only followed with js off. 836 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, path: []const u8) ![]const u8 { 837 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0, path) orelse return error.RouteTooLong; 838 const url = try route.toUrl(page_arena); 839 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 840 } 841 842 // the "viewing <ref_or_oid> <value>" banner shown above the log. 843 pub const Header = struct { 844 content: []const u8, 845 846 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 847 return .{ 848 .content = try std.fmt.allocPrint(aa, "viewing {s} {s}", .{ @tagName(ref_or_oid), value }), 849 }; 850 } 851 852 pub const View = struct { 853 box: wgt.Box(ui.Widget), 854 data: *const Header, 855 856 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session) !Header.View { 857 _ = session; 858 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 859 errdefer box.deinit(allocator); 860 861 var text_box = try wgt.TextBox(ui.Widget).init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 862 errdefer text_box.deinit(allocator); 863 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 864 865 return .{ .box = box, .data = data }; 866 } 867 868 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 869 self.box.deinit(allocator); 870 } 871 872 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 873 self.clearGrid(); 874 try self.box.build(allocator, constraint, root_focus); 875 } 876 877 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 878 _ = self; 879 _ = allocator; 880 _ = key; 881 _ = root_focus; 882 } 883 884 pub fn clearGrid(self: *Header.View) void { 885 self.box.clearGrid(); 886 } 887 888 pub fn getGrid(self: Header.View) ?Grid { 889 return self.box.getGrid(); 890 } 891 892 pub fn getFocus(self: *Header.View) *Focus { 893 return self.box.getFocus(); 894 } 895 }; 896 }; 897