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