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