╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
536 537 // stretch the diff pane across the rest of the width so it fills the area 538 // rather than shrinking to its content; its scroll fills the pane. when 539 // too narrow for both, it fills the whole width. 540 if (constraint.max_size.width) |w| { 541 self.contentBox().children.values()[diff_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 542 } else { 543 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_min_width, .height = null }; 544 } 545 546 // the web bounds the layout to the browser viewport like the terminal; 547 // each Scroll's web-native mode hands its full content to a real 548 // scrollable element, so we no longer build unbounded here. 549 // 550 // crossing panes only re-selects the content box's child (focusDiff / 551 // focusList); when the window is too narrow to show both, the pane that 552 // held focus is dropped here. the framework recovers focus after the 553 // top-level build by re-deriving it down the selected-child chain, so 554 // there's nothing to fix up afterward. 555 try self.box.build(allocator, constraint, root_focus); 556 } 557 558 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 559 const sel = self.selectedCommitIndex() orelse return; 560 if (self.diffed_index) |d| if (d == sel) return; 561 try self.populateDiff(allocator, sel); 562 self.diffed_index = sel; 563 } 564 565 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 566 const commit = self.data.commits[sel]; 567 const inner = self.diffInner(); 568 569 for (inner.children.values()) |*child| child.widget.deinit(allocator); 570 inner.children.clearAndFree(allocator); 571 inner.getFocus().child_id = null; 572 573 // the first row always links to viewing the repo's files at this commit. 574 try self.addViewFilesLink(allocator, inner, commit.oid); 575 576 // a "previous" row and a "next" row at the bottom reload the page on the 577 // adjacent diff window. 578 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page); 579 for (commit.hunks) |hunk| { 580 if (hunk.path) |path| try self.addPathBox(allocator, inner, path); 581 try self.addHunkBox(allocator, inner, hunk.lines); 582 } 583 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page); 584 585 // point the pane at its first row so focus recovery can land here. 586 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 587 588 // reset the scroll to the top for the newly-shown commit: directly on the 589 // terminal (the wasm offset), and via a version bump on the web (so the 590 // renderer's scroll id changes and JS drops the preserved position). 591 const sc = self.diffScroll(); 592 sc.x = 0; 593 sc.y = 0; 594 sc.getFocus().version +%= 1; 595 } 596 597 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 598 _ = allocator; 599 if (self.diffActive()) { 600 try self.diffInput(key, root_focus); 601 } else { 602 try self.listInput(key, root_focus); 603 } 604 } 605 606 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 607 // up/down (and the scroll wheel) move the selection a row; page up/down 608 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 609 // on the "next" row become navigation in the host before reaching here. 610 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 611 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 612 return; 613 } 614 switch (key) { 615 .enter => if (self.selectedCommitIndex() != null) 616 try self.focusDiff(root_focus) 617 else if (self.data.next_start) |next| { 618 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, next, 0)) |route| 619 try self.session.navigate(route); 620 }, 621 .arrow_right => try self.focusDiff(root_focus), 622 else => {}, 623 } 624 } 625 626 fn diffInput(self: *View, key: Key, root_focus: *Focus) !void { 627 const sc = self.diffScroll(); 628 switch (key) { 629 // left scrolls horizontally, then leaves for the list once flush left. 630 .arrow_left => { 631 if (sc.x > 0) { 632 sc.x -= 1; 633 self.diffScroll().clampToContent(); 634 } else try self.focusList(root_focus); 635 }, 636 .arrow_right => { 637 sc.x += 1; 638 self.diffScroll().clampToContent(); 639 }, 640 .arrow_up => try self.moveDiff(root_focus, -1), 641 .arrow_down => try self.moveDiff(root_focus, 1), 642 .page_up => try self.pageDiff(root_focus, -10), 643 .page_down => try self.pageDiff(root_focus, 10), 644 .home => try self.jumpDiff(root_focus, false), 645 .end => try self.jumpDiff(root_focus, true), 646 // Enter / a click on a "previous"/"next" row follow its "a:" link in 647 // the host (it reloads the adjacent window), so they don't reach here. 648 .mouse => |mouse| switch (mouse.action) { 649 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 650 else => {}, 651 }, 652 else => {}, 653 } 654 } 655 656 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 657 // line count unless there is a new hunk visible (in which case it is a 658 // hunk count). on the web `delta` is always a hunk count. 659 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 660 const inner = self.diffInner(); 661 const keys = inner.children.keys(); 662 if (keys.len == 0) return; 663 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 664 const target = @as(isize, @intCast(cur)) + delta; 665 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 666 667 if (self.session.is_terminal) { 668 if (in_range and self.hunkVisible(@intCast(target))) { 669 root_focus.setFocus(keys[@intCast(target)]); 670 return; 671 } 672 673 const sc = self.diffScroll(); 674 sc.y += delta * 5; // magnify because it's a line count 675 self.diffScroll().clampToContent(); 676 if (in_range and self.hunkVisible(@intCast(target))) { 677 root_focus.setFocus(keys[@intCast(target)]); 678 } 679 } else { 680 if (in_range) { 681 root_focus.setFocus(keys[@intCast(target)]); 682 } 683 } 684 } 685 686 // page a fixed number of lines, then focus the leading visible hunk 687 // (bottom-most when paging down, top-most when paging up). in the 688 // terminal, `delta` is a line count, and on the web it's a hunk count. 689 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 690 if (self.session.is_terminal) { 691 const sc = self.diffScroll(); 692 sc.y += delta * 5; // magnify because it's a line count 693 self.diffScroll().clampToContent(); 694 try self.focusVisible(root_focus, delta > 0); 695 } else { 696 const inner = self.diffInner(); 697 const keys = inner.children.keys(); 698 if (keys.len == 0) return; 699 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 700 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 701 root_focus.setFocus(keys[target]); 702 } 703 } 704 705 // jump to the first or last hunk. on the web the browser scrolls to the 706 // focused hunk; on the terminal pin the scroll to the top/bottom too. 707 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 708 const inner = self.diffInner(); 709 const keys = inner.children.keys(); 710 if (keys.len == 0) return; 711 if (self.session.is_terminal) { 712 const sc = self.diffScroll(); 713 sc.y = if (to_end) std.math.maxInt(isize) else 0; 714 self.diffScroll().clampToContent(); 715 } 716 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 717 } 718 719 // whether hunk `index` is at least partly within the diff viewport, per the 720 // last build's layout rects (content space) and the current scroll offset. 721 fn hunkVisible(self: *View, index: usize) bool { 722 const inner = self.diffInner(); 723 const sc = self.diffScroll(); 724 const vp = sc.grid orelse return false; 725 const r = inner.children.values()[index].rect orelse return false; 726 const top = sc.y; 727 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 728 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 729 } 730 731 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 732 // the bottom-most visible (for downward motion), else the top-most. 733 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 734 const inner = self.diffInner(); 735 const sc = self.diffScroll(); 736 const vp = sc.grid orelse return; 737 const top = sc.y; 738 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 739 var chosen: ?usize = null; 740 for (inner.children.keys(), inner.children.values()) |id, *child| { 741 const r = child.rect orelse continue; // content-space layout rect 742 const r_top = r.y; 743 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 744 if (r_bot <= top or r_top >= bottom) continue; // not visible 745 chosen = id; 746 if (!prefer_last) break; // first visible 747 } 748 if (chosen) |id| root_focus.setFocus(id); 749 } 750 751 // enter the diff pane. the host arrives here on right-arrow or Enter from the 752 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 753 // case where the pane isn't laid out yet (it gets selected, then focused after 754 // the next build, landing on its remembered hunk). 755 fn focusDiff(self: *View, root_focus: *Focus) !void { 756 if (self.diffInner().children.count() == 0) return; 757 root_focus.setFocus(self.diffOuter().getFocus().id); 758 } 759 760 // return to the list. 761 fn focusList(self: *View, root_focus: *Focus) !void { 762 root_focus.setFocus(self.listScroll().getFocus().id); 763 } 764 765 pub fn clearGrid(self: *View) void { 766 self.box.clearGrid(); 767 } 768 769 pub fn getGrid(self: View) ?Grid { 770 return self.box.getGrid(); 771 } 772 773 pub fn getFocus(self: *View) *Focus { 774 return self.box.getFocus(); 775 } 776 777 // for the parent's "scroll up at the top jumps to the header" check. in the 778 // diff pane that means the first hunk is focused and the diff can't scroll up 779 // any further; otherwise up-arrow stays in the pane (scrolling or moving 780 // between hunks). when the list holds focus, report its selection directly. 781 pub fn getSelectedIndex(self: *View) ?usize { 782 if (self.diffActive()) { 783 const inner = self.diffInner(); 784 const cid = inner.getFocus().child_id orelse return 1; 785 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 786 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 787 } 788 const lb = self.listBox(); 789 const cid = lb.getFocus().child_id orelse return null; 790 return lb.children.getIndex(cid); 791 } 792 }; 793 794 // the "a:" navigation link for the commits page walking from commit `oid` within 795 // `identity`, showing the selected commit's first `after` hunks (0 = default). 796 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, after: usize) ![]const u8 { 797 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, after) orelse return error.RouteTooLong; 798 const url = try route.urlAlloc(page_arena); 799 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 800 } 801 802 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 803 // directory, within `identity` ("owner/name"). 804 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 805 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 806 const url = try route.urlAlloc(page_arena); 807 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 808 } 809 810 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 811 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 812 // in-page (crossPageLink ignores it); the href is only followed with js off. 813 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 814 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0) orelse return error.RouteTooLong; 815 const url = try route.urlAlloc(page_arena); 816 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 817 } 818