╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
469 box.getFocus().child_id = box.children.keys()[list_index]; 470 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 471 472 // focus lives in the split; the banner isn't focusable. 473 outer.getFocus().child_id = outer.children.keys()[content_index]; 474 475 return .{ 476 .box = outer, 477 .data = data, 478 .session = session, 479 .diffed_index = null, 480 }; 481 } 482 483 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 484 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 485 errdefer row.deinit(allocator); 486 row.getFocus().focusable = true; 487 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 488 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 489 } 490 491 // one hunk as a focusable multi-line text box. its lines sit flush (the 492 // inner Text rows have no border). `lines` are joined into the page arena 493 // (text_box borrows it). 494 fn addHunkBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), lines: []const []const u8) !void { 495 if (lines.len == 0) return; 496 const text = try std.mem.join(self.session.page_arena.allocator(), "\n", lines); 497 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 498 errdefer tb.deinit(allocator); 499 tb.getFocus().focusable = true; 500 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 501 } 502 503 // a focusable file-path label shown above the first hunk of each file. it 504 // links to this page filtered to that file, so activating it navigates 505 // there. 506 fn addPathBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), path: []const u8, oid: []const u8) !void { 507 const link = try commitsLink(self.session.page_arena, self.data.identity, oid, 0, path); 508 var tb = try wgt.TextBox(ui.Widget).init(allocator, path, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 509 errdefer tb.deinit(allocator); 510 tb.getFocus().focusable = true; 511 tb.getFocus().kind = .{ .custom = link }; 512 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 513 } 514 515 // a focusable window-navigation row ("previous"/"next"). it's a link to this 516 // commit's route at `target_start`, so activating it (the host follows the 517 // "a:" link) reloads the page on the adjacent window — same on TUI and web. 518 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, oid: []const u8, target_start: usize, path: []const u8) !void { 519 const link = try commitsLink(self.session.page_arena, self.data.identity, oid, target_start, path); 520 var tb = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 521 errdefer tb.deinit(allocator); 522 tb.getFocus().focusable = true; 523 tb.getFocus().kind = .{ .custom = link }; 524 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 525 } 526 527 // a focusable row for the top of the diff pane linking to the files tab at 528 // this commit's tree, so its files are always viewable as of this object. 529 fn addViewFilesLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), oid: []const u8) !void { 530 const link = try filesObjectLink(self.session.page_arena, self.data.identity, oid); 531 var tb = try wgt.TextBox(ui.Widget).init(allocator, "view files at this commit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 532 errdefer tb.deinit(allocator); 533 tb.getFocus().focusable = true; 534 tb.getFocus().kind = .{ .custom = link }; 535 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 536 } 537 538 // how much of the message a pane's box holds: a truncated preview links to 539 // the pane holding the whole thing, which is that link's destination. 540 const MessageBox = enum { preview, whole }; 541 542 // the selected commit's message. 543 fn addMessageBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), commit: Commit, kind: MessageBox) !void { 544 // a truncated message keeps its box even with no whole line to show, 545 // since the box is what links to the whole thing. 546 if (commit.message.len == 0 and !commit.message_truncated) return; 547 const cut_short = commit.message_truncated and kind == .preview; 548 var tb = try messageBox(allocator, commit.message, if (cut_short) " click or press enter to see more " else ""); 549 errdefer tb.deinit(allocator); 550 if (cut_short) tb.getFocus().kind = .{ .custom = try messageLink(self.session.page_arena, self.data.identity, commit.oid) }; 551 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 552 } 553 554 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 555 self.box.deinit(allocator); 556 } 557 558 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 559 return &self.box.children.values()[content_index].widget.box; 560 } 561 562 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 563 return &self.contentBox().children.values()[list_index].widget.scroll; 564 } 565 566 fn listBox(self: *View) *wgt.Box(ui.Widget) { 567 return &self.listScroll().child.box; 568 } 569 570 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 571 return &self.contentBox().children.values()[diff_index].widget.box; 572 } 573 574 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 575 return &self.diffOuter().children.values()[0].widget.scroll; 576 } 577 578 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 579 return &self.diffScroll().child.box; 580 } 581 582 fn diffActive(self: *View) bool { 583 const content = self.contentBox(); 584 const cid = content.getFocus().child_id orelse return false; 585 return content.children.getIndex(cid) == diff_index; 586 } 587 588 // what the pane shows for the commit at `sel`. the page's content only 589 // applies to the commit it walks from; the rest show their plain diff. 590 fn paneContent(self: *View, sel: usize) Self.Content { 591 return if (sel == 0) self.data.content else .{ .diff = .{} }; 592 } 593 594 // the selected commit's index, or null when the "next" row is selected. 595 fn selectedCommitIndex(self: *View) ?usize { 596 const lb = self.listBox(); 597 const cid = lb.getFocus().child_id orelse return null; 598 const idx = lb.children.getIndex(cid) orelse return null; 599 if (idx >= self.data.commits.len) return null; 600 return idx; 601 } 602 603 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 604 self.clearGrid(); 605 606 // swap the diff pane to the selected commit when it changes. 607 try self.refreshDiff(allocator); 608 609 // mirror the focused commit into the url so it updates as the selection 610 // moves, but only while focus is inside this view (the list or diff). when 611 // focus sits on the header tab, use the page's base /commits route. 612 if (root_focus.grandchild_id) |g| { 613 if (self.box.getFocus().children.contains(g)) { 614 if (self.selectedCommitIndex()) |sel| { 615 const oid = self.data.commits[sel].oid; 616 const window_start = self.data.commits[sel].window_start; 617 // mirror the commit's current diff window so the url stays 618 // linkable (0 for any commit but the windowed start one). 619 // selecting another commit leaves the message behind, like 620 // it leaves the path filter behind. 621 const mirrored = switch (self.paneContent(sel)) { 622 .message => ui.RoutablePage.repoCommitMessageRoute(self.data.identity, .object, oid), 623 .diff => |d| ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, oid, window_start, d.path), 624 }; 625 if (mirrored) |route| self.session.data.current_page = route; 626 } 627 } 628 } 629 630 // the selected list row shows a border (the focused TextBox upgrades it 631 // to a double border itself); the rest stay borderless. 632 const lb = self.listBox(); 633 for (lb.children.keys(), lb.children.values()) |id, *child| { 634 switch (child.widget) { 635 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 636 else => {}, 637 } 638 } 639 640 // cap the list at list_max_width only while the diff pane fits beside it. 641 // the box drops the diff when the width can't hold both minimums, so when 642 // it's that narrow we lift the cap and let the list fill the whole width. 643 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 644 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 645 646 // stretch the diff pane across the rest of the width so it fills the area 647 // rather than shrinking to its content; its scroll fills the pane. when 648 // too narrow for both, it fills the whole width. 649 const diff_width: usize = if (constraint.max_size.width) |w| 650 (if (both_panes_fit) w - list_max_width else w) 651 else 652 diff_min_width; 653 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_width, .height = null }; 654 655 // the message is the pane's only wrapping row, and wrapping needs a 656 // bounded width, which the pane's scroll doesn't grant (it scrolls 657 // horizontally too). cap it to the pane, leaving room for the border 658 // and the scrollbar column. re-capped each build so it tracks resizes. 659 for (self.diffInner().children.values()) |*child| switch (child.widget) { 660 .text_box => |text_box| if (text_box.options.wrap_kind == .word) { 661 child.max_size = .{ .width = diff_width -| 3, .height = null }; 662 }, 663 else => {}, 664 }; 665 666 // the web bounds the layout to the browser viewport like the terminal; 667 // each Scroll's web-native mode hands its full content to a real 668 // scrollable element, so we no longer build unbounded here. 669 // 670 // crossing panes only re-selects the content box's child (focusDiff / 671 // focusList); when the window is too narrow to show both, the pane that 672 // held focus is dropped here. the framework recovers focus after the 673 // top-level build by re-deriving it down the selected-child chain, so 674 // there's nothing to fix up afterward. 675 try self.box.build(allocator, constraint, root_focus); 676 } 677 678 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 679 const sel = self.selectedCommitIndex() orelse return; 680 if (self.diffed_index) |d| if (d == sel) return; 681 try self.populateDiff(allocator, sel); 682 self.diffed_index = sel; 683 } 684 685 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 686 const commit = self.data.commits[sel]; 687 const inner = self.diffInner(); 688 689 for (inner.children.values()) |*child| child.widget.deinit(allocator); 690 inner.children.clearAndFree(allocator); 691 inner.getFocus().child_id = null; 692 693 switch (self.paneContent(sel)) { 694 .message => { 695 try self.addNavLink(allocator, inner, "← back to diff", commit.oid, 0, ""); 696 try self.addMessageBox(allocator, inner, commit, .whole); 697 }, 698 .diff => |d| { 699 if (d.path.len != 0) { 700 // the file's path, then a row returning to this commit's unfiltered diff. 701 var label = try wgt.TextBox(ui.Widget).init(allocator, d.path, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 702 errdefer label.deinit(allocator); 703 label.getFocus().focusable = true; 704 try inner.children.put(allocator, label.getFocus().id, .{ .widget = .{ .text_box = label }, .rect = null, .min_size = null }); 705 try self.addNavLink(allocator, inner, "← all files", commit.oid, 0, ""); 706 } else { 707 try self.addMessageBox(allocator, inner, commit, .preview); 708 if (commit.author != .unknown) { 709 var tb = try ui.authorBox(allocator, self.session.page_arena, commit.author); 710 errdefer tb.deinit(allocator); 711 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 712 } 713 try self.addViewFilesLink(allocator, inner, commit.oid); 714 } 715 716 // a "previous" row and a "next" row at the bottom reload the page 717 // on the adjacent diff window. 718 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page, d.path); 719 for (commit.hunks) |hunk| { 720 if (d.path.len == 0) if (hunk.path) |hunk_path| try self.addPathBox(allocator, inner, hunk_path, commit.oid); 721 try self.addHunkBox(allocator, inner, hunk.lines); 722 } 723 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page, d.path); 724 }, 725 } 726 727 // point the pane at its first row so focus recovery can land here. 728 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 729 730 // reset the scroll to the top for the newly-shown commit: directly on the 731 // terminal (the wasm offset), and via a version bump on the web (so the 732 // renderer's scroll id changes and JS drops the preserved position). 733 const sc = self.diffScroll(); 734 sc.x = 0; 735 sc.y = 0; 736 sc.getFocus().version +%= 1; 737 } 738 739 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 740 _ = allocator; 741 if (self.diffActive()) { 742 try self.diffInput(key, root_focus); 743 } else { 744 try self.listInput(key, root_focus); 745 } 746 } 747 748 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 749 // up/down (and the scroll wheel) move the selection a row; page up/down 750 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 751 // on the "next" row become navigation in the host before reaching here. 752 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 753 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 754 return; 755 } 756 switch (key) { 757 .enter => if (self.selectedCommitIndex() != null) 758 try self.focusDiff(root_focus) 759 else if (self.data.next_start) |next| { 760 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, next, 0, "")) |route| 761 try self.session.navigate(route); 762 }, 763 .arrow_right => try self.focusDiff(root_focus), 764 else => {}, 765 } 766 } 767 768 fn diffInput(self: *View, key: Key, root_focus: *Focus) !void { 769 const sc = self.diffScroll(); 770 switch (key) { 771 // left scrolls horizontally, then leaves for the list once flush left. 772 .arrow_left => { 773 if (sc.x > 0) { 774 sc.x -= 1; 775 self.diffScroll().clampToContent(); 776 } else try self.focusList(root_focus); 777 }, 778 .arrow_right => { 779 sc.x += 1; 780 self.diffScroll().clampToContent(); 781 }, 782 .arrow_up => try self.moveDiff(root_focus, -1), 783 .arrow_down => try self.moveDiff(root_focus, 1), 784 .page_up => try self.pageDiff(root_focus, -10), 785 .page_down => try self.pageDiff(root_focus, 10), 786 .home => try self.jumpDiff(root_focus, false), 787 .end => try self.jumpDiff(root_focus, true), 788 // Enter / a click on a "previous"/"next" row follow its "a:" link in 789 // the host (it reloads the adjacent window), so they don't reach here. 790 .mouse => |mouse| switch (mouse.action) { 791 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 792 else => {}, 793 }, 794 else => {}, 795 } 796 } 797 798 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 799 // line count unless there is a new hunk visible (in which case it is a 800 // hunk count). on the web `delta` is always a hunk count. 801 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 802 const inner = self.diffInner(); 803 const keys = inner.children.keys(); 804 if (keys.len == 0) return; 805 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 806 const target = @as(isize, @intCast(cur)) + delta; 807 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 808 809 if (self.session.is_terminal) { 810 if (in_range and self.hunkVisible(@intCast(target))) { 811 root_focus.setFocus(keys[@intCast(target)]); 812 return; 813 } 814 815 const sc = self.diffScroll(); 816 sc.y += delta * 5; // magnify because it's a line count 817 self.diffScroll().clampToContent(); 818 if (in_range and self.hunkVisible(@intCast(target))) { 819 root_focus.setFocus(keys[@intCast(target)]); 820 } 821 } else { 822 if (in_range) { 823 root_focus.setFocus(keys[@intCast(target)]); 824 } 825 } 826 } 827 828 // page a fixed number of lines, then focus the leading visible hunk 829 // (bottom-most when paging down, top-most when paging up). in the 830 // terminal, `delta` is a line count, and on the web it's a hunk count. 831 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 832 if (self.session.is_terminal) { 833 const sc = self.diffScroll(); 834 sc.y += delta * 5; // magnify because it's a line count 835 self.diffScroll().clampToContent(); 836 try self.focusVisible(root_focus, delta > 0); 837 } else { 838 const inner = self.diffInner(); 839 const keys = inner.children.keys(); 840 if (keys.len == 0) return; 841 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 842 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 843 root_focus.setFocus(keys[target]); 844 } 845 } 846 847 // jump to the first or last hunk. on the web the browser scrolls to the 848 // focused hunk; on the terminal pin the scroll to the top/bottom too. 849 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 850 const inner = self.diffInner(); 851 const keys = inner.children.keys(); 852 if (keys.len == 0) return; 853 if (self.session.is_terminal) { 854 const sc = self.diffScroll(); 855 sc.y = if (to_end) std.math.maxInt(isize) else 0; 856 self.diffScroll().clampToContent(); 857 } 858 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 859 } 860 861 // whether hunk `index` is at least partly within the diff viewport, per the 862 // last build's layout rects (content space) and the current scroll offset. 863 fn hunkVisible(self: *View, index: usize) bool { 864 const inner = self.diffInner(); 865 const sc = self.diffScroll(); 866 const vp = sc.grid orelse return false; 867 const r = inner.children.values()[index].rect orelse return false; 868 const top = sc.y; 869 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 870 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 871 } 872 873 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 874 // the bottom-most visible (for downward motion), else the top-most. 875 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 876 const inner = self.diffInner(); 877 const sc = self.diffScroll(); 878 const vp = sc.grid orelse return; 879 const top = sc.y; 880 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 881 var chosen: ?usize = null; 882 for (inner.children.keys(), inner.children.values()) |id, *child| { 883 const r = child.rect orelse continue; // content-space layout rect 884 const r_top = r.y; 885 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 886 if (r_bot <= top or r_top >= bottom) continue; // not visible 887 chosen = id; 888 if (!prefer_last) break; // first visible 889 } 890 if (chosen) |id| root_focus.setFocus(id); 891 } 892 893 // enter the diff pane. the host arrives here on right-arrow or Enter from the 894 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 895 // case where the pane isn't laid out yet (it gets selected, then focused after 896 // the next build, landing on its remembered hunk). 897 fn focusDiff(self: *View, root_focus: *Focus) !void { 898 if (self.diffInner().children.count() == 0) return; 899 root_focus.setFocus(self.diffOuter().getFocus().id); 900 } 901 902 // return to the list. 903 fn focusList(self: *View, root_focus: *Focus) !void { 904 root_focus.setFocus(self.listScroll().getFocus().id); 905 } 906 907 pub fn clearGrid(self: *View) void { 908 self.box.clearGrid(); 909 } 910 911 pub fn getGrid(self: View) ?Grid { 912 return self.box.getGrid(); 913 } 914 915 pub fn getFocus(self: *View) *Focus { 916 return self.box.getFocus(); 917 } 918 919 // for the parent's "scroll up at the top jumps to the header" check. in the 920 // diff pane that means the first hunk is focused and the diff can't scroll up 921 // any further; otherwise up-arrow stays in the pane (scrolling or moving 922 // between hunks). when the list holds focus, report its selection directly. 923 pub fn getSelectedIndex(self: *View) ?usize { 924 if (self.diffActive()) { 925 const inner = self.diffInner(); 926 const cid = inner.getFocus().child_id orelse return 1; 927 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 928 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 929 } 930 const lb = self.listBox(); 931 const cid = lb.getFocus().child_id orelse return null; 932 return lb.children.getIndex(cid); 933 } 934 }; 935 936 // the "a:" navigation link for the commits page walking from commit `oid` within 937 // `identity`, windowing the selected commit's diff from hunk `start`, filtered 938 // to `path` ("" = every file). 939 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 940 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, start, path) orelse return error.RouteTooLong; 941 const url = try route.toUrl(page_arena); 942 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 943 } 944 945 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 946 // directory, within `identity` ("owner/name"). 947 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 948 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 949 const url = try route.toUrl(page_arena); 950 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 951 } 952 953 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 954 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 955 // in-page (crossPageLink ignores it); the href is only followed with js off. 956 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, path: []const u8) ![]const u8 { 957 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0, path) orelse return error.RouteTooLong; 958 const url = try route.toUrl(page_arena); 959 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 960 } 961 962 // the "a:" link to the page showing `oid`'s message on its own. 963 fn messageLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 964 const route = ui.RoutablePage.repoCommitMessageRoute(identity, .object, oid) orelse return error.RouteTooLong; 965 const url = try route.toUrl(page_arena); 966 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 967 } 968 969 // the "viewing <ref_or_oid> <value>" banner shown above the log. 970 pub const Header = struct { 971 content: []const u8, 972 973 // `value` arrives url-encoded, so decode it for display. 974 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 975 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 976 return .{ 977 .content = try std.fmt.allocPrint(aa, "viewing {s} {s}", .{ @tagName(ref_or_oid), decoded }), 978 }; 979 } 980 981 pub const View = struct { 982 box: wgt.Box(ui.Widget), 983 data: *const Header, 984 985 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session) !Header.View { 986 _ = session; 987 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 988 errdefer box.deinit(allocator); 989 990 var text_box = try wgt.TextBox(ui.Widget).init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 991 errdefer text_box.deinit(allocator); 992 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 993 994 return .{ .box = box, .data = data }; 995 } 996 997 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 998 self.box.deinit(allocator); 999 } 1000 1001 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1002 self.clearGrid(); 1003 try self.box.build(allocator, constraint, root_focus); 1004 } 1005 1006 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1007 _ = self; 1008 _ = allocator; 1009 _ = key; 1010 _ = root_focus; 1011 } 1012 1013 pub fn clearGrid(self: *Header.View) void { 1014 self.box.clearGrid(); 1015 } 1016 1017 pub fn getGrid(self: Header.View) ?Grid { 1018 return self.box.getGrid(); 1019 } 1020 1021 pub fn getFocus(self: *Header.View) *Focus { 1022 return self.box.getFocus(); 1023 } 1024 }; 1025 }; 1026