╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
634 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 635 // up/down (and the scroll wheel) move the selection a row; page up/down 636 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 637 // on the "next" row become navigation in the host before reaching here. 638 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 639 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 640 return; 641 } 642 switch (key) { 643 .enter => if (self.selectedCommitIndex() != null) 644 self.focusDiff(root_focus) 645 else if (self.data.next_start) |next| { 646 if (self.data.location.commitsRoute(.object, next, 0, "")) |route| 647 try self.session.navigate(route); 648 }, 649 .arrow_right => self.focusDiff(root_focus), 650 else => {}, 651 } 652 } 653 654 // enter the diff pane. the host arrives here on right-arrow or Enter from the 655 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 656 // case where the pane isn't laid out yet (it gets selected, then focused after 657 // the next build, landing on its remembered hunk). 658 fn focusDiff(self: *View, root_focus: *Focus) void { 659 if (self.diffInner().children.count() == 0) return; 660 root_focus.setFocus(self.diffOuter().getFocus().id); 661 } 662 663 // return to the list. 664 fn focusList(self: *View, root_focus: *Focus) void { 665 root_focus.setFocus(self.listScroll().getFocus().id); 666 } 667 668 pub fn clearGrid(self: *View) void { 669 self.box.clearGrid(); 670 } 671 672 pub fn getGrid(self: View) ?Grid { 673 return self.box.getGrid(); 674 } 675 676 pub fn getFocus(self: *View) *Focus { 677 return self.box.getFocus(); 678 } 679 680 fn contentAtTop(self: *View) bool { 681 if (self.diffActive()) { 682 const inner = self.diffInner(); 683 const cid = inner.getFocus().child_id orelse return true; 684 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 685 return first_focused and self.diffScroll().y == 0; 686 } 687 const lb = self.listBox(); 688 const cid = lb.getFocus().child_id orelse return true; 689 return lb.children.getIndex(cid) == 0; 690 } 691 692 // only the clone-url row sits directly below the repository header. 693 pub fn atTop(self: *View) bool { 694 return self.headerActive() or (!self.header().hasCloneUrl() and self.contentAtTop()); 695 } 696 697 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 698 return self.header().focusCloneUrl(root_focus); 699 } 700 }; 701 702 // the "a:" navigation link for the commits page walking from commit `oid` within 703 // `location`, windowing the selected commit's diff from hunk `start`, filtered 704 // to `path` ("" = every file). 705 fn commitsLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 706 const route = location.commitsRoute(.object, oid, start, path) orelse return error.RouteTooLong; 707 const url = try route.toUrl(page_arena); 708 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 709 } 710 711 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 712 // directory, within `location`. 713 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 714 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 715 const url = try route.toUrl(page_arena); 716 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 717 } 718 719 // the in-page "ai:" anchor for selecting a commit with its current pane content 720 // and window. the href is only followed with js off. 721 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, commit: Commit, content: Content) ![]const u8 { 722 const route = (switch (content) { 723 .message => location.commitMessageRoute(.object, commit.oid), 724 .diff => |diff| location.commitsRoute(.object, commit.oid, commit.window.start, diff.path), 725 }) orelse return error.RouteTooLong; 726 const url = try route.toUrl(page_arena); 727 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 728 } 729 730 // the "a:" link to the page showing `oid`'s message on its own. 731 fn messageLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 732 const route = location.commitMessageRoute(.object, oid) orelse return error.RouteTooLong; 733 const url = try route.toUrl(page_arena); 734 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 735 } 736 737 // the "<ref_or_oid> <value>" banner shown above the log. 738 pub const Header = struct { 739 content: []const u8, 740 741 // `value` arrives url-encoded, so decode it for display. 742 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 743 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 744 return .{ 745 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 746 }; 747 } 748 749 pub const View = struct { 750 box: wgt.Box(ui.Widget), 751 data: *const Header, 752 753 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !Header.View { 754 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 755 errdefer box.deinit(allocator); 756 757 if (!session.data.is_local) { 758 if (try ui.widget.CopyableText.initClone(allocator, session, location)) |value| { 759 var clone_url = value; 760 errdefer clone_url.deinit(allocator); 761 const min_width = clone_url.minWidth(); 762 box.getFocus().child_id = clone_url.getFocus().id; 763 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 764 } 765 } 766 767 var spacer = try ui.widget.Spacer.init(allocator); 768 errdefer spacer.deinit(allocator); 769 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 770 771 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 772 errdefer text_box.deinit(allocator); 773 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 774 775 return .{ .box = box, .data = data }; 776 } 777 778 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 779 self.box.deinit(allocator); 780 } 781 782 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 783 self.clearGrid(); 784 try self.box.build(allocator, constraint, root_focus); 785 } 786 787 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 788 const clone_url = self.cloneUrl() orelse return; 789 try clone_url.input(allocator, key, root_focus); 790 } 791 792 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 793 const clone_url = self.cloneUrl() orelse return false; 794 root_focus.setFocus(clone_url.getFocus().id); 795 return true; 796 } 797 798 pub fn hasCloneUrl(self: *Header.View) bool { 799 return self.cloneUrl() != null; 800 } 801 802 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 803 for (self.box.children.values()) |*child| switch (child.widget) { 804 .copyable_text => |*copyable_text| return copyable_text, 805 else => {}, 806 }; 807 return null; 808 } 809 810 pub fn clearGrid(self: *Header.View) void { 811 self.box.clearGrid(); 812 } 813 814 pub fn getGrid(self: Header.View) ?Grid { 815 return self.box.getGrid(); 816 } 817 818 pub fn getFocus(self: *Header.View) *Focus { 819 return self.box.getFocus(); 820 } 821 }; 822 }; 823