╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
450 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 451 self.box.deinit(allocator); 452 } 453 454 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 455 return &self.box.children.values()[content_index].widget.box; 456 } 457 458 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 459 return &self.contentBox().children.values()[list_index].widget.scroll; 460 } 461 462 fn listBox(self: *View) *wgt.Box(ui.Widget) { 463 return &self.listScroll().child.box; 464 } 465 466 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 467 return &self.contentBox().children.values()[diff_index].widget.box; 468 } 469 470 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 471 return &self.diffOuter().children.values()[0].widget.scroll; 472 } 473 474 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 475 return &self.diffScroll().child.box; 476 } 477 478 fn diffActive(self: *View) bool { 479 const content = self.contentBox(); 480 const cid = content.getFocus().child_id orelse return false; 481 return content.children.getIndex(cid) == diff_index; 482 } 483 484 // the selected commit's index, or null when the "next" row is selected. 485 fn selectedCommitIndex(self: *View) ?usize { 486 const lb = self.listBox(); 487 const cid = lb.getFocus().child_id orelse return null; 488 const idx = lb.children.getIndex(cid) orelse return null; 489 if (idx >= self.data.commits.len) return null; 490 return idx; 491 } 492 493 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 494 self.clearGrid(); 495 496 // swap the diff pane to the selected commit when it changes. 497 try self.refreshDiff(allocator); 498 499 // mirror the focused commit into the url so it updates as the selection 500 // moves, but only while focus is inside this view (the list or diff). when 501 // focus sits on the header tab, use the page's base /commits route. 502 if (root_focus.grandchild_id) |g| { 503 if (self.box.getFocus().children.contains(g)) { 504 if (self.selectedCommitIndex()) |sel| { 505 // mirror the commit's current diff window so the url stays 506 // linkable (0 for any commit but the windowed start one). 507 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, self.data.commits[sel].oid, self.data.commits[sel].window_start)) |route| 508 self.session.data.current_page = route; 509 } 510 } 511 } 512 513 // the selected list row shows a border (the focused TextBox upgrades it 514 // to a double border itself); the rest stay borderless. 515 const lb = self.listBox(); 516 for (lb.children.keys(), lb.children.values()) |id, *child| { 517 switch (child.widget) { 518 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 519 else => {}, 520 } 521 } 522 523 // the selected hunk (or the "load more" row) shows a single border (the 524 // focused TextBox upgrades it to a double border itself, so it stays 525 // single when focus is on the commit list); the rest keep their 526 // space-reserving hidden border. 527 const inner = self.diffInner(); 528 for (inner.children.keys(), inner.children.values()) |id, *child| { 529 switch (child.widget) { 530 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 531 else => {}, 532 } 533 } 534 535 // cap the list at list_max_width only while the diff pane fits beside it. 536 // the box drops the diff when the width can't hold both minimums, so when 537 // it's that narrow we lift the cap and let the list fill the whole width. 538 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 539 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 540 541 // stretch the diff pane across the rest of the width so it fills the area 542 // rather than shrinking to its content; its scroll fills the pane. when 543 // too narrow for both, it fills the whole width. 544 if (constraint.max_size.width) |w| { 545 self.contentBox().children.values()[diff_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 546 } else { 547 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_min_width, .height = null }; 548 } 549 550 // the web bounds the layout to the browser viewport like the terminal; 551 // each Scroll's web-native mode hands its full content to a real 552 // scrollable element, so we no longer build unbounded here. 553 // 554 // crossing panes only re-selects the content box's child (focusDiff / 555 // focusList); when the window is too narrow to show both, the pane that 556 // held focus is dropped here. the framework recovers focus after the 557 // top-level build by re-deriving it down the selected-child chain, so 558 // there's nothing to fix up afterward. 559 try self.box.build(allocator, constraint, root_focus); 560 } 561 562 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 563 const sel = self.selectedCommitIndex() orelse return; 564 if (self.diffed_index) |d| if (d == sel) return; 565 try self.populateDiff(allocator, sel); 566 self.diffed_index = sel; 567 } 568 569 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 570 const commit = self.data.commits[sel]; 571 const inner = self.diffInner(); 572 573 for (inner.children.values()) |*child| child.widget.deinit(allocator); 574 inner.children.clearAndFree(allocator); 575 inner.getFocus().child_id = null; 576 577 // the first row always links to viewing the repo's files at this commit. 578 try self.addViewFilesLink(allocator, inner, commit.oid); 579 580 // a "previous" row and a "next" row at the bottom reload the page on the 581 // adjacent diff window. 582 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page); 583 for (commit.hunks) |hunk| { 584 if (hunk.path) |path| try self.addPathBox(allocator, inner, path); 585 try self.addHunkBox(allocator, inner, hunk.lines); 586 } 587 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page); 588 589 // point the pane at its first row so focus recovery can land here. 590 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 591 592 // reset the scroll to the top for the newly-shown commit: directly on the 593 // terminal (the wasm offset), and via a version bump on the web (so the 594 // renderer's scroll id changes and JS drops the preserved position). 595 const sc = self.diffScroll(); 596 sc.x = 0; 597 sc.y = 0; 598 sc.getFocus().version +%= 1; 599 } 600 601 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 602 _ = allocator; 603 if (self.diffActive()) { 604 try self.diffInput(key, root_focus); 605 } else { 606 try self.listInput(key, root_focus); 607 } 608 } 609 610 fn listInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 611 // up/down (and the scroll wheel) move the selection a row; page up/down 612 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 613 // on the "next" row become navigation in the host before reaching here. 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 .arrow_down => try self.moveSelection(root_focus, 1), 623 .arrow_up => try self.moveSelection(root_focus, -1), 624 .page_down => try self.moveSelection(root_focus, 10), 625 .page_up => try self.moveSelection(root_focus, -10), 626 .end => try self.moveSelection(root_focus, @intCast(self.listBox().children.count())), 627 .home => try self.moveSelection(root_focus, -@as(isize, @intCast(self.listBox().children.count()))), 628 .mouse => |mouse| switch (mouse.action) { 629 .scroll => |dir| try self.moveSelection(root_focus, if (dir == .up) -1 else 1), 630 else => {}, 631 }, 632 else => {}, 633 } 634 } 635 636 fn diffInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 637 const sc = self.diffScroll(); 638 switch (key) { 639 // left scrolls horizontally, then leaves for the list once flush left. 640 .arrow_left => { 641 if (sc.x > 0) { 642 sc.x -= 1; 643 self.clampDiffScroll(); 644 } else try self.focusList(root_focus); 645 }, 646 .arrow_right => { 647 sc.x += 1; 648 self.clampDiffScroll(); 649 }, 650 .arrow_up => try self.moveDiff(root_focus, -1), 651 .arrow_down => try self.moveDiff(root_focus, 1), 652 .page_up => try self.pageDiff(root_focus, -10), 653 .page_down => try self.pageDiff(root_focus, 10), 654 .home => try self.jumpDiff(root_focus, false), 655 .end => try self.jumpDiff(root_focus, true), 656 // Enter / a click on a "previous"/"next" row follow its "a:" link in 657 // the host (it reloads the adjacent window), so they don't reach here. 658 .mouse => |mouse| switch (mouse.action) { 659 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 660 else => {}, 661 }, 662 else => {}, 663 } 664 } 665 666 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 667 // line count unless there is a new hunk visible (in which case it is a 668 // hunk count). on the web `delta` is always a hunk count. 669 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 670 const inner = self.diffInner(); 671 const keys = inner.children.keys(); 672 if (keys.len == 0) return; 673 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 674 const target = @as(isize, @intCast(cur)) + delta; 675 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 676 677 if (self.session.is_terminal) { 678 if (in_range and self.hunkVisible(@intCast(target))) { 679 root_focus.setFocus(keys[@intCast(target)]); 680 return; 681 } 682 683 const sc = self.diffScroll(); 684 sc.y += delta * 5; // magnify because it's a line count 685 self.clampDiffScroll(); 686 if (in_range and self.hunkVisible(@intCast(target))) { 687 root_focus.setFocus(keys[@intCast(target)]); 688 } 689 } else { 690 if (in_range) { 691 root_focus.setFocus(keys[@intCast(target)]); 692 } 693 } 694 } 695 696 // page a fixed number of lines, then focus the leading visible hunk 697 // (bottom-most when paging down, top-most when paging up). in the 698 // terminal, `delta` is a line count, and on the web it's a hunk count. 699 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 700 if (self.session.is_terminal) { 701 const sc = self.diffScroll(); 702 sc.y += delta * 5; // magnify because it's a line count 703 self.clampDiffScroll(); 704 try self.focusVisible(root_focus, delta > 0); 705 } else { 706 const inner = self.diffInner(); 707 const keys = inner.children.keys(); 708 if (keys.len == 0) return; 709 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 710 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 711 root_focus.setFocus(keys[target]); 712 } 713 } 714 715 // jump to the first or last hunk. on the web the browser scrolls to the 716 // focused hunk; on the terminal pin the scroll to the top/bottom too. 717 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 718 const inner = self.diffInner(); 719 const keys = inner.children.keys(); 720 if (keys.len == 0) return; 721 if (self.session.is_terminal) { 722 const sc = self.diffScroll(); 723 sc.y = if (to_end) std.math.maxInt(isize) else 0; 724 self.clampDiffScroll(); 725 } 726 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 727 } 728 729 // whether hunk `index` is at least partly within the diff viewport, per the 730 // last build's layout rects (content space) and the current scroll offset. 731 fn hunkVisible(self: *View, index: usize) bool { 732 const inner = self.diffInner(); 733 const sc = self.diffScroll(); 734 const vp = sc.grid orelse return false; 735 const r = inner.children.values()[index].rect orelse return false; 736 const top = sc.y; 737 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 738 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 739 } 740 741 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 742 // the bottom-most visible (for downward motion), else the top-most. 743 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 744 const inner = self.diffInner(); 745 const sc = self.diffScroll(); 746 const vp = sc.grid orelse return; 747 const top = sc.y; 748 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 749 var chosen: ?usize = null; 750 for (inner.children.keys(), inner.children.values()) |id, *child| { 751 const r = child.rect orelse continue; // content-space layout rect 752 const r_top = r.y; 753 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 754 if (r_bot <= top or r_top >= bottom) continue; // not visible 755 chosen = id; 756 if (!prefer_last) break; // first visible 757 } 758 if (chosen) |id| root_focus.setFocus(id); 759 } 760 761 // keep the diff scroll within its content, using the last build's grids. the 762 // scroll bar's reserved column/row isn't part of the content viewport, so 763 // exclude it (as scrollToRect does) or the last column/row stays unreachable. 764 fn clampDiffScroll(self: *View) void { 765 const sc = self.diffScroll(); 766 const vp = sc.grid orelse return; 767 const content = sc.child.box.grid orelse return; 768 const view_w = vp.size.width - sc.bar_w; 769 const view_h = vp.size.height - sc.bar_h; 770 const max_y: isize = if (content.size.height > view_h) @intCast(content.size.height - view_h) else 0; 771 const max_x: isize = if (content.size.width > view_w) @intCast(content.size.width - view_w) else 0; 772 sc.y = std.math.clamp(sc.y, 0, max_y); 773 sc.x = std.math.clamp(sc.x, 0, max_x); 774 } 775 776 // enter the diff pane. the host arrives here on right-arrow or Enter from the 777 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 778 // case where the pane isn't laid out yet (it gets selected, then focused after 779 // the next build, landing on its remembered hunk). 780 fn focusDiff(self: *View, root_focus: *Focus) !void { 781 if (self.diffInner().children.count() == 0) return; 782 root_focus.setFocus(self.diffOuter().getFocus().id); 783 } 784 785 // return to the list. 786 fn focusList(self: *View, root_focus: *Focus) !void { 787 root_focus.setFocus(self.listScroll().getFocus().id); 788 } 789 790 fn moveSelection(self: *View, root_focus: *Focus, delta: isize) !void { 791 const lb = self.listBox(); 792 const keys = lb.children.keys(); 793 if (keys.len == 0) return; 794 const cur_id = lb.getFocus().child_id orelse return; 795 const cur: isize = @intCast(lb.children.getIndex(cur_id) orelse return); 796 const last: isize = @intCast(keys.len - 1); 797 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 798 if (next == @as(usize, @intCast(cur))) return; 799 root_focus.setFocus(keys[next]); 800 if (lb.children.values()[next].rect) |rect| self.listScroll().scrollToRect(rect); 801 } 802 803 pub fn clearGrid(self: *View) void { 804 self.box.clearGrid(); 805 } 806 807 pub fn getGrid(self: View) ?Grid { 808 return self.box.getGrid(); 809 } 810 811 pub fn getFocus(self: *View) *Focus { 812 return self.box.getFocus(); 813 } 814 815 // for the parent's "scroll up at the top jumps to the header" check. in the 816 // diff pane that means the first hunk is focused and the diff can't scroll up 817 // any further; otherwise up-arrow stays in the pane (scrolling or moving 818 // between hunks). when the list holds focus, report its selection directly. 819 pub fn getSelectedIndex(self: *View) ?usize { 820 if (self.diffActive()) { 821 const inner = self.diffInner(); 822 const cid = inner.getFocus().child_id orelse return 1; 823 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 824 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 825 } 826 const lb = self.listBox(); 827 const cid = lb.getFocus().child_id orelse return null; 828 return lb.children.getIndex(cid); 829 } 830 }; 831 832 // the "a:" navigation link for the commits page walking from commit `oid` within 833 // `identity`, showing the selected commit's first `after` hunks (0 = default). 834 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, after: usize) ![]const u8 { 835 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, after) orelse return error.RouteTooLong; 836 const url = try route.urlAlloc(page_arena); 837 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 838 } 839 840 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 841 // directory, within `identity` ("owner/name"). 842 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 843 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 844 const url = try route.urlAlloc(page_arena); 845 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 846 } 847 848 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 849 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 850 // in-page (crossPageLink ignores it); the href is only followed with js off. 851 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 852 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0) orelse return error.RouteTooLong; 853 const url = try route.urlAlloc(page_arena); 854 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 855 } 856