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