╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
483 } 484 485 fn diffActive(self: *View) bool { 486 const content = self.contentBox(); 487 const cid = content.getFocus().child_id orelse return false; 488 return content.children.getIndex(cid) == diff_index; 489 } 490 491 // the selected commit's index, or null when the "next" row is selected. 492 fn selectedCommitIndex(self: *View) ?usize { 493 const lb = self.listBox(); 494 const cid = lb.getFocus().child_id orelse return null; 495 const idx = lb.children.getIndex(cid) orelse return null; 496 if (idx >= self.data.commits.len) return null; 497 return idx; 498 } 499 500 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 501 self.clearGrid(); 502 503 // swap the diff pane to the selected commit when it changes. 504 try self.refreshDiff(allocator); 505 506 // mirror the focused commit into the url so it updates as the selection 507 // moves, but only while focus is inside this view (the list or diff). when 508 // focus sits on the header tab, use the page's base /commits route. 509 if (root_focus.grandchild_id) |g| { 510 if (self.box.getFocus().children.contains(g)) { 511 if (self.selectedCommitIndex()) |sel| { 512 // mirror the commit's current diff window so the url stays 513 // linkable (0 for any commit but the windowed start one). 514 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, self.data.commits[sel].oid, self.data.commits[sel].window_start, if (sel == 0) self.data.path else "")) |route| 515 self.session.data.current_page = route; 516 } 517 } 518 } 519 520 // the selected list row shows a border (the focused TextBox upgrades it 521 // to a double border itself); the rest stay borderless. 522 const lb = self.listBox(); 523 for (lb.children.keys(), lb.children.values()) |id, *child| { 524 switch (child.widget) { 525 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 526 else => {}, 527 } 528 } 529 530 // the selected hunk (or a window-navigation row) shows a single border 531 // (the focused TextBox upgrades it to a double border itself, so it 532 // stays single when focus is on the commit list); the rest keep their 533 // space-reserving hidden border. 534 const inner = self.diffInner(); 535 for (inner.children.keys(), inner.children.values()) |id, *child| { 536 switch (child.widget) { 537 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 538 else => {}, 539 } 540 } 541 542 // cap the list at list_max_width only while the diff pane fits beside it. 543 // the box drops the diff when the width can't hold both minimums, so when 544 // it's that narrow we lift the cap and let the list fill the whole width. 545 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 546 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 547 548 // stretch the diff pane across the rest of the width so it fills the area 549 // rather than shrinking to its content; its scroll fills the pane. when 550 // too narrow for both, it fills the whole width. 551 if (constraint.max_size.width) |w| { 552 self.contentBox().children.values()[diff_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 553 } else { 554 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_min_width, .height = null }; 555 } 556 557 // the web bounds the layout to the browser viewport like the terminal; 558 // each Scroll's web-native mode hands its full content to a real 559 // scrollable element, so we no longer build unbounded here. 560 // 561 // crossing panes only re-selects the content box's child (focusDiff / 562 // focusList); when the window is too narrow to show both, the pane that 563 // held focus is dropped here. the framework recovers focus after the 564 // top-level build by re-deriving it down the selected-child chain, so 565 // there's nothing to fix up afterward. 566 try self.box.build(allocator, constraint, root_focus); 567 } 568 569 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 570 const sel = self.selectedCommitIndex() orelse return; 571 if (self.diffed_index) |d| if (d == sel) return; 572 try self.populateDiff(allocator, sel); 573 self.diffed_index = sel; 574 } 575 576 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 577 const commit = self.data.commits[sel]; 578 // the path filter only applies to the top commit (the walk root). 579 const path = if (sel == 0) self.data.path else ""; 580 const inner = self.diffInner(); 581 582 for (inner.children.values()) |*child| child.widget.deinit(allocator); 583 inner.children.clearAndFree(allocator); 584 inner.getFocus().child_id = null; 585 586 if (path.len != 0) { 587 // the file's path, then a row returning to this commit's unfiltered diff. 588 var label = try wgt.TextBox(ui.Widget).init(allocator, path, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 589 errdefer label.deinit(allocator); 590 label.getFocus().focusable = true; 591 try inner.children.put(allocator, label.getFocus().id, .{ .widget = .{ .text_box = label }, .rect = null, .min_size = null }); 592 try self.addNavLink(allocator, inner, "← all files", commit.oid, 0, ""); 593 } else { 594 // the first row always links to viewing the repo's files at this commit. 595 try self.addViewFilesLink(allocator, inner, commit.oid); 596 } 597 598 // a "previous" row and a "next" row at the bottom reload the page on the 599 // adjacent diff window. 600 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page, path); 601 for (commit.hunks) |hunk| { 602 if (path.len == 0) if (hunk.path) |hunk_path| try self.addPathBox(allocator, inner, hunk_path, commit.oid); 603 try self.addHunkBox(allocator, inner, hunk.lines); 604 } 605 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page, path); 606 607 // point the pane at its first row so focus recovery can land here. 608 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 609 610 // reset the scroll to the top for the newly-shown commit: directly on the 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