╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
486 487 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 488 const sel = self.selectedCommitIndex() orelse return; 489 if (self.diffed_index) |d| if (d == sel) return; 490 try self.populateDiff(allocator, sel); 491 self.diffed_index = sel; 492 } 493 494 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 495 const commit = self.data.commits[sel]; 496 const inner = self.diffInner(); 497 498 for (inner.children.values()) |*child| child.widget.deinit(allocator); 499 inner.children.clearAndFree(allocator); 500 inner.getFocus().child_id = null; 501 502 for (commit.hunks) |hunk| { 503 if (hunk.path) |path| try self.addPathBox(allocator, inner, path); 504 try self.addHunkBox(allocator, inner, hunk.lines); 505 } 506 // the load-more link reloads showing one more page of this commit's hunks. 507 if (commit.has_more) try self.addLoadMore(allocator, inner, commit.oid, commit.shown_hunks + diff_page); 508 509 const sc = self.diffScroll(); 510 sc.x = 0; 511 sc.y = 0; 512 } 513 514 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 515 _ = allocator; 516 if (self.diffActive()) { 517 try self.diffInput(key, root_focus); 518 } else { 519 try self.listInput(key, root_focus); 520 } 521 } 522 523 fn listInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 524 // up/down (and the scroll wheel) move the selection a row; page up/down 525 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 526 // on the "next" row become navigation in the host before reaching here. 527 switch (key) { 528 .arrow_right, .enter => try self.focusDiff(root_focus), 529 .arrow_down => try self.moveSelection(root_focus, 1), 530 .arrow_up => try self.moveSelection(root_focus, -1), 531 .page_down => try self.moveSelection(root_focus, page_rows), 532 .page_up => try self.moveSelection(root_focus, -page_rows), 533 .mouse => |mouse| switch (mouse.action) { 534 .scroll => |dir| try self.moveSelection(root_focus, if (dir == .up) -1 else 1), 535 else => {}, 536 }, 537 else => {}, 538 } 539 } 540 541 fn diffInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 542 const sc = self.diffScroll(); 543 switch (key) { 544 // left scrolls horizontally, then leaves for the list once flush left. 545 .arrow_left => { 546 if (sc.x > 0) { 547 sc.x -= 1; 548 self.clampDiffScroll(); 549 } else try self.focusList(root_focus); 550 }, 551 .arrow_right => { 552 sc.x += 1; 553 self.clampDiffScroll(); 554 }, 555 .arrow_up => try self.moveDiff(root_focus, -1), 556 .arrow_down => try self.moveDiff(root_focus, 1), 557 .page_up => try self.pageDiff(root_focus, -page_rows), 558 .page_down => try self.pageDiff(root_focus, page_rows), 559 // Enter / a click on the "load more" row follow its "a:" link in the 560 // host (it reloads the page with more hunks), so they don't reach here. 561 .mouse => |mouse| switch (mouse.action) { 562 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 563 else => {}, 564 }, 565 else => {}, 566 } 567 } 568 569 // move focus one hunk in `dir` (+1 down, -1 up). if the adjacent hunk is 570 // already visible, just move focus to it — don't scroll. only when it isn't 571 // visible yet do we scroll one line toward it (and focus it once it appears), 572 // so scrolling never skips past an on-screen hunk. when nothing fits to 573 // scroll (e.g. the web, where the browser scrolls the page), the clamp keeps 574 // the offset put and focus simply steps to the adjacent hunk. 575 fn moveDiff(self: *View, root_focus: *Focus, dir: isize) !void { 576 const inner = self.diffInner(); 577 const keys = inner.children.keys(); 578 if (keys.len == 0) return; 579 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 580 const target = @as(isize, @intCast(cur)) + dir; 581 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 582 583 if (in_range and self.hunkVisible(@intCast(target))) { 584 try root_focus.setFocus(keys[@intCast(target)]); 585 return; 586 } 587 588 const sc = self.diffScroll(); 589 sc.y += dir; 590 self.clampDiffScroll(); 591 if (in_range and self.hunkVisible(@intCast(target))) 592 try root_focus.setFocus(keys[@intCast(target)]); 593 } 594 595 // page a fixed number of lines, then focus the leading visible hunk 596 // (bottom-most when paging down, top-most when paging up). 597 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 598 const sc = self.diffScroll(); 599 sc.y += delta; 600 self.clampDiffScroll(); 601 try self.focusVisible(root_focus, delta > 0); 602 } 603 604 // whether hunk `index` is at least partly within the diff viewport, per the 605 // last build's layout rects (content space) and the current scroll offset. 606 fn hunkVisible(self: *View, index: usize) bool { 607 const inner = self.diffInner(); 608 const sc = self.diffScroll(); 609 const vp = sc.grid orelse return false; 610 const r = inner.children.values()[index].rect orelse return false; 611 const top = sc.y; 612 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 613 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 614 } 615 616 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 617 // the bottom-most visible (for downward motion), else the top-most. 618 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 619 const inner = self.diffInner(); 620 const sc = self.diffScroll(); 621 const vp = sc.grid orelse return; 622 const top = sc.y; 623 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 624 var chosen: ?usize = null; 625 for (inner.children.keys(), inner.children.values()) |id, *child| { 626 const r = child.rect orelse continue; // content-space layout rect 627 const r_top = r.y; 628 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 629 if (r_bot <= top or r_top >= bottom) continue; // not visible 630 chosen = id; 631 if (!prefer_last) break; // first visible 632 } 633 if (chosen) |id| try root_focus.setFocus(id); 634 } 635 636 // keep the diff scroll within its content, using the last build's grids. the 637 // scroll bar's reserved column/row isn't part of the content viewport, so 638 // exclude it (as scrollToRect does) or the last column/row stays unreachable. 639 fn clampDiffScroll(self: *View) void { 640 const sc = self.diffScroll(); 641 const vp = sc.grid orelse return; 642 const content = sc.child.box.grid orelse return; 643 const view_w = vp.size.width - sc.bar_w; 644 const view_h = vp.size.height - sc.bar_h; 645 const max_y: isize = if (content.size.height > view_h) @intCast(content.size.height - view_h) else 0; 646 const max_x: isize = if (content.size.width > view_w) @intCast(content.size.width - view_w) else 0; 647 sc.y = std.math.clamp(sc.y, 0, max_y); 648 sc.x = std.math.clamp(sc.x, 0, max_x); 649 } 650 651 // enter the diff pane by focusing the hunk that was focused last (the box's 652 // child_id remembers it across the trip to the commit list); fall back to the 653 // first hunk on a fresh diff. the host arrives here on right-arrow or Enter 654 // from the list. a diff with no hunks can't be entered. 655 fn focusDiff(self: *View, root_focus: *Focus) !void { 656 const inner = self.diffInner(); 657 if (inner.children.count() == 0) return; 658 const target = inner.getFocus().child_id orelse inner.children.keys()[0]; 659 try root_focus.setFocus(target); 660 if (root_focus.grandchild_id == target) return; 661 // the diff pane wasn't laid out last build (too narrow to show beside 662 // the list), so its hunks aren't in the focus tree yet. select the pane 663 // at the box level, and the hunk will be focused after the next build. 664 self.box.getFocus().child_id = self.diffOuter().getFocus().id; 665 } 666 667 fn focusList(self: *View, root_focus: *Focus) !void { 668 const lb = self.listBox(); 669 const id = lb.getFocus().child_id orelse (if (lb.children.count() > 0) lb.children.keys()[0] else return); 670 try root_focus.setFocus(id); 671 } 672 673 fn moveSelection(self: *View, root_focus: *Focus, delta: isize) !void { 674 const lb = self.listBox(); 675 const keys = lb.children.keys(); 676 if (keys.len == 0) return; 677 const cur_id = lb.getFocus().child_id orelse return; 678 const cur: isize = @intCast(lb.children.getIndex(cur_id) orelse return); 679 const last: isize = @intCast(keys.len - 1); 680 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 681 if (next == @as(usize, @intCast(cur))) return; 682 try root_focus.setFocus(keys[next]); 683 if (lb.children.values()[next].rect) |rect| self.listScroll().scrollToRect(rect); 684 } 685 686 pub fn clearGrid(self: *View) void { 687 self.box.clearGrid(); 688 } 689 690 pub fn getGrid(self: View) ?Grid { 691 return self.box.getGrid(); 692 } 693 694 pub fn getFocus(self: *View) *Focus { 695 return self.box.getFocus(); 696 } 697 698 // for the parent's "scroll up at the top jumps to the header" check. in the 699 // diff pane that means the first hunk is focused and the diff can't scroll up 700 // any further; otherwise up-arrow stays in the pane (scrolling or moving 701 // between hunks). when the list holds focus, report its selection directly. 702 pub fn getSelectedIndex(self: *View) ?usize { 703 if (self.diffActive()) { 704 const inner = self.diffInner(); 705 const cid = inner.getFocus().child_id orelse return 1; 706 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 707 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 708 } 709 const lb = self.listBox(); 710 const cid = lb.getFocus().child_id orelse return null; 711 return lb.children.getIndex(cid); 712 } 713 }; 714 715 // the "a:" navigation link for the commits page starting at `start_oid` within 716 // `identity`, showing the selected commit's first `after` hunks (0 = default). 717 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start_oid: []const u8, after: usize) ![]const u8 { 718 const route = ui.RoutablePage.repoCommitsRoute(identity, start_oid, after) orelse return error.RouteTooLong; 719 const url = try route.urlAlloc(page_arena); 720 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 721 } 722