╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
402 403 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 404 return &self.diffOuter().children.values()[0].widget.scroll; 405 } 406 407 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 408 return &self.diffScroll().child.box; 409 } 410 411 fn diffActive(self: *View) bool { 412 const cid = self.box.getFocus().child_id orelse return false; 413 return self.box.children.getIndex(cid) == diff_index; 414 } 415 416 // the selected commit's index, or null when the "next" row is selected. 417 fn selectedCommitIndex(self: *View) ?usize { 418 const lb = self.listBox(); 419 const cid = lb.getFocus().child_id orelse return null; 420 const idx = lb.children.getIndex(cid) orelse return null; 421 if (idx >= self.data.commits.len) return null; 422 return idx; 423 } 424 425 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 426 self.clearGrid(); 427 428 // swap the diff pane to the selected commit when it changes. 429 try self.refreshDiff(allocator); 430 431 // mirror the focused commit into the url so it updates as the selection 432 // moves, but only while focus is inside this view (the list or diff). when 433 // focus sits on the header tab, use the page's base /commits route. 434 if (root_focus.grandchild_id) |g| { 435 if (self.box.getFocus().children.contains(g)) { 436 if (self.selectedCommitIndex()) |sel| { 437 // mirror the commit's current diff window so the url stays 438 // linkable (0 for any commit but the windowed start one). 439 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, self.data.commits[sel].oid, self.data.commits[sel].window_start)) |route| 440 self.session.data.current_page = route; 441 } 442 } 443 } 444 445 // the selected list row shows a border (the focused TextBox upgrades it 446 // to a double border itself); the rest stay borderless. 447 const lb = self.listBox(); 448 for (lb.children.keys(), lb.children.values()) |id, *child| { 449 switch (child.widget) { 450 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 451 else => {}, 452 } 453 } 454 455 // the selected hunk (or the "load more" row) shows a single border (the 456 // focused TextBox upgrades it to a double border itself, so it stays 457 // single when focus is on the commit list); the rest keep their 458 // space-reserving hidden border. 459 const inner = self.diffInner(); 460 for (inner.children.keys(), inner.children.values()) |id, *child| { 461 switch (child.widget) { 462 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 463 else => {}, 464 } 465 } 466 467 // cap the list at list_max_width only while the diff pane fits beside it. 468 // the box drops the diff when the width can't hold both minimums, so when 469 // it's that narrow we lift the cap and let the list fill the whole width. 470 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 471 self.box.children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 472 473 // the web bounds the layout to the browser viewport like the terminal; 474 // each Scroll's web-native mode hands its full content to a real 475 // scrollable element, so we no longer build unbounded here. 476 try self.box.build(allocator, constraint, root_focus); 477 478 // if the diff pane is selected but focus is still elsewhere in this view, 479 // the pane was too narrow to lay out when focus crossed over. it's laid 480 // out now, so focus its hunk. 481 if (self.diffActive() and inner.children.count() > 0) { 482 if (root_focus.grandchild_id) |g| { 483 const in_view = self.box.getFocus().children.contains(g); 484 const in_diff = inner.children.contains(g); 485 if (in_view and !in_diff) 486 try root_focus.setFocus(inner.getFocus().child_id orelse inner.children.keys()[0]); 487 } 488 } 489 } 490 491 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 492 const sel = self.selectedCommitIndex() orelse return; 493 if (self.diffed_index) |d| if (d == sel) return; 494 try self.populateDiff(allocator, sel); 495 self.diffed_index = sel; 496 } 497 498 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 499 const commit = self.data.commits[sel]; 500 const inner = self.diffInner(); 501 502 for (inner.children.values()) |*child| child.widget.deinit(allocator); 503 inner.children.clearAndFree(allocator); 504 inner.getFocus().child_id = null; 505 506 // a "previous" row at the top and a "next" row at the bottom reload the 507 // page on the adjacent diff window. 508 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page); 509 for (commit.hunks) |hunk| { 510 if (hunk.path) |path| try self.addPathBox(allocator, inner, path); 511 try self.addHunkBox(allocator, inner, hunk.lines); 512 } 513 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page); 514 515 // reset the scroll to the top for the newly-shown commit: directly on the 516 // terminal (the wasm offset), and via a version bump on the web (so the 517 // renderer's scroll id changes and JS drops the preserved position). 518 const sc = self.diffScroll(); 519 sc.x = 0; 520 sc.y = 0; 521 sc.getFocus().version +%= 1; 522 } 523 524 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 525 _ = allocator; 526 if (self.diffActive()) { 527 try self.diffInput(key, root_focus); 528 } else { 529 try self.listInput(key, root_focus); 530 } 531 } 532 533 fn listInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 534 // up/down (and the scroll wheel) move the selection a row; page up/down 535 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 536 // on the "next" row become navigation in the host before reaching here. 537 switch (key) { 538 .enter => if (self.selectedCommitIndex() != null) 539 try self.focusDiff(root_focus) 540 else if (self.data.next_start) |next| { 541 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, next, 0)) |route| 542 try self.session.navigate(route); 543 }, 544 .arrow_right => try self.focusDiff(root_focus), 545 .arrow_down => try self.moveSelection(root_focus, 1), 546 .arrow_up => try self.moveSelection(root_focus, -1), 547 .page_down => try self.moveSelection(root_focus, 10), 548 .page_up => try self.moveSelection(root_focus, -10), 549 .end => try self.moveSelection(root_focus, @intCast(self.listBox().children.count())), 550 .home => try self.moveSelection(root_focus, -@as(isize, @intCast(self.listBox().children.count()))), 551 .mouse => |mouse| switch (mouse.action) { 552 .scroll => |dir| try self.moveSelection(root_focus, if (dir == .up) -1 else 1), 553 else => {}, 554 }, 555 else => {}, 556 } 557 } 558 559 fn diffInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 560 const sc = self.diffScroll(); 561 switch (key) { 562 // left scrolls horizontally, then leaves for the list once flush left. 563 .arrow_left => { 564 if (sc.x > 0) { 565 sc.x -= 1; 566 self.clampDiffScroll(); 567 } else try self.focusList(root_focus); 568 }, 569 .arrow_right => { 570 sc.x += 1; 571 self.clampDiffScroll(); 572 }, 573 .arrow_up => try self.moveDiff(root_focus, -1), 574 .arrow_down => try self.moveDiff(root_focus, 1), 575 .page_up => try self.pageDiff(root_focus, -10), 576 .page_down => try self.pageDiff(root_focus, 10), 577 .home => try self.jumpDiff(root_focus, false), 578 .end => try self.jumpDiff(root_focus, true), 579 // Enter / a click on a "previous"/"next" row follow its "a:" link in 580 // the host (it reloads the adjacent window), so they don't reach here. 581 .mouse => |mouse| switch (mouse.action) { 582 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 583 else => {}, 584 }, 585 else => {}, 586 } 587 } 588 589 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 590 // line count unless there is a new hunk visible (in which case it is a 591 // hunk count). on the web `delta` is always a hunk count. 592 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 593 const inner = self.diffInner(); 594 const keys = inner.children.keys(); 595 if (keys.len == 0) return; 596 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 597 const target = @as(isize, @intCast(cur)) + delta; 598 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 599 600 if (self.session.is_terminal) { 601 if (in_range and self.hunkVisible(@intCast(target))) { 602 try root_focus.setFocus(keys[@intCast(target)]); 603 return; 604 } 605 606 const sc = self.diffScroll(); 607 sc.y += delta * 5; // magnify because it's a line count 608 self.clampDiffScroll(); 609 if (in_range and self.hunkVisible(@intCast(target))) { 610 try root_focus.setFocus(keys[@intCast(target)]); 611 } 612 } else { 613 if (in_range) { 614 try root_focus.setFocus(keys[@intCast(target)]); 615 } 616 } 617 } 618 619 // page a fixed number of lines, then focus the leading visible hunk 620 // (bottom-most when paging down, top-most when paging up). in the 621 // terminal, `delta` is a line count, and on the web it's a hunk count. 622 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 623 if (self.session.is_terminal) { 624 const sc = self.diffScroll(); 625 sc.y += delta * 5; // magnify because it's a line count 626 self.clampDiffScroll(); 627 try self.focusVisible(root_focus, delta > 0); 628 } else { 629 const inner = self.diffInner(); 630 const keys = inner.children.keys(); 631 if (keys.len == 0) return; 632 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 633 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 634 try root_focus.setFocus(keys[target]); 635 } 636 } 637 638 // jump to the first or last hunk. on the web the browser scrolls to the 639 // focused hunk; on the terminal pin the scroll to the top/bottom too. 640 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 641 const inner = self.diffInner(); 642 const keys = inner.children.keys(); 643 if (keys.len == 0) return; 644 if (self.session.is_terminal) { 645 const sc = self.diffScroll(); 646 sc.y = if (to_end) std.math.maxInt(isize) else 0; 647 self.clampDiffScroll(); 648 } 649 try root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 650 } 651 652 // whether hunk `index` is at least partly within the diff viewport, per the 653 // last build's layout rects (content space) and the current scroll offset. 654 fn hunkVisible(self: *View, index: usize) bool { 655 const inner = self.diffInner(); 656 const sc = self.diffScroll(); 657 const vp = sc.grid orelse return false; 658 const r = inner.children.values()[index].rect orelse return false; 659 const top = sc.y; 660 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 661 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 662 } 663 664 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 665 // the bottom-most visible (for downward motion), else the top-most. 666 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 667 const inner = self.diffInner(); 668 const sc = self.diffScroll(); 669 const vp = sc.grid orelse return; 670 const top = sc.y; 671 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 672 var chosen: ?usize = null; 673 for (inner.children.keys(), inner.children.values()) |id, *child| { 674 const r = child.rect orelse continue; // content-space layout rect 675 const r_top = r.y; 676 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 677 if (r_bot <= top or r_top >= bottom) continue; // not visible 678 chosen = id; 679 if (!prefer_last) break; // first visible 680 } 681 if (chosen) |id| try root_focus.setFocus(id); 682 } 683 684 // keep the diff scroll within its content, using the last build's grids. the 685 // scroll bar's reserved column/row isn't part of the content viewport, so 686 // exclude it (as scrollToRect does) or the last column/row stays unreachable. 687 fn clampDiffScroll(self: *View) void { 688 const sc = self.diffScroll(); 689 const vp = sc.grid orelse return; 690 const content = sc.child.box.grid orelse return; 691 const view_w = vp.size.width - sc.bar_w; 692 const view_h = vp.size.height - sc.bar_h; 693 const max_y: isize = if (content.size.height > view_h) @intCast(content.size.height - view_h) else 0; 694 const max_x: isize = if (content.size.width > view_w) @intCast(content.size.width - view_w) else 0; 695 sc.y = std.math.clamp(sc.y, 0, max_y); 696 sc.x = std.math.clamp(sc.x, 0, max_x); 697 } 698 699 // enter the diff pane by focusing the hunk that was focused last (the box's 700 // child_id remembers it across the trip to the commit list); fall back to the 701 // first hunk on a fresh diff. the host arrives here on right-arrow or Enter 702 // from the list. a diff with no hunks can't be entered. 703 fn focusDiff(self: *View, root_focus: *Focus) !void { 704 const inner = self.diffInner(); 705 if (inner.children.count() == 0) return; 706 const target = inner.getFocus().child_id orelse inner.children.keys()[0]; 707 try root_focus.setFocus(target); 708 if (root_focus.grandchild_id == target) return; 709 // the diff pane wasn't laid out last build (too narrow to show beside 710 // the list), so its hunks aren't in the focus tree yet. select the pane 711 // at the box level, and the hunk will be focused after the next build. 712 self.box.getFocus().child_id = self.diffOuter().getFocus().id; 713 } 714 715 fn focusList(self: *View, root_focus: *Focus) !void { 716 const lb = self.listBox(); 717 const id = lb.getFocus().child_id orelse (if (lb.children.count() > 0) lb.children.keys()[0] else return); 718 try root_focus.setFocus(id); 719 } 720 721 fn moveSelection(self: *View, root_focus: *Focus, delta: isize) !void { 722 const lb = self.listBox(); 723 const keys = lb.children.keys(); 724 if (keys.len == 0) return; 725 const cur_id = lb.getFocus().child_id orelse return; 726 const cur: isize = @intCast(lb.children.getIndex(cur_id) orelse return); 727 const last: isize = @intCast(keys.len - 1); 728 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 729 if (next == @as(usize, @intCast(cur))) return; 730 try root_focus.setFocus(keys[next]); 731 if (lb.children.values()[next].rect) |rect| self.listScroll().scrollToRect(rect); 732 } 733 734 pub fn clearGrid(self: *View) void { 735 self.box.clearGrid(); 736 } 737 738 pub fn getGrid(self: View) ?Grid { 739 return self.box.getGrid(); 740 } 741 742 pub fn getFocus(self: *View) *Focus { 743 return self.box.getFocus(); 744 } 745 746 // for the parent's "scroll up at the top jumps to the header" check. in the 747 // diff pane that means the first hunk is focused and the diff can't scroll up 748 // any further; otherwise up-arrow stays in the pane (scrolling or moving 749 // between hunks). when the list holds focus, report its selection directly. 750 pub fn getSelectedIndex(self: *View) ?usize { 751 if (self.diffActive()) { 752 const inner = self.diffInner(); 753 const cid = inner.getFocus().child_id orelse return 1; 754 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 755 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 756 } 757 const lb = self.listBox(); 758 const cid = lb.getFocus().child_id orelse return null; 759 return lb.children.getIndex(cid); 760 } 761 }; 762 763 // the "a:" navigation link for the commits page starting at `start_oid` within 764 // `identity`, showing the selected commit's first `after` hunks (0 = default). 765 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start_oid: []const u8, after: usize) ![]const u8 { 766 const route = ui.RoutablePage.repoCommitsRoute(identity, start_oid, after) orelse return error.RouteTooLong; 767 const url = try route.urlAlloc(page_arena); 768 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 769 } 770