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