╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
393 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 394 return &self.diffScroll().child.box; 395 } 396 397 fn diffActive(self: *View) bool { 398 const cid = self.box.getFocus().child_id orelse return false; 399 return self.box.children.getIndex(cid) == diff_index; 400 } 401 402 // the selected commit's index, or null when the "next" row is selected. 403 fn selectedCommitIndex(self: *View) ?usize { 404 const lb = self.listBox(); 405 const cid = lb.getFocus().child_id orelse return null; 406 const idx = lb.children.getIndex(cid) orelse return null; 407 if (idx >= self.data.commits.len) return null; 408 return idx; 409 } 410 411 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 412 self.clearGrid(); 413 414 // swap the diff pane to the selected commit when it changes. 415 try self.refreshDiff(allocator); 416 417 // mirror the focused commit into the url so it updates as the selection 418 // moves, but only while focus is inside this view (the list or diff). when 419 // focus sits on the header tab, use the page's base /commits route. 420 if (root_focus.grandchild_id) |g| { 421 if (self.box.getFocus().children.contains(g)) { 422 if (self.selectedCommitIndex()) |sel| { 423 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, self.data.commits[sel].oid)) |route| 424 self.session.data.current_page = route; 425 } 426 } 427 } 428 429 // the selected list row shows a border (the focused TextBox upgrades it 430 // to a double border itself); the rest stay borderless. 431 const lb = self.listBox(); 432 for (lb.children.keys(), lb.children.values()) |id, *child| { 433 switch (child.widget) { 434 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 435 else => {}, 436 } 437 } 438 439 // the selected hunk (or the "load more" row) shows a single border (the 440 // focused TextBox upgrades it to a double border itself, so it stays 441 // single when focus is on the commit list); the rest keep their 442 // space-reserving hidden border. 443 const inner = self.diffInner(); 444 for (inner.children.keys(), inner.children.values()) |id, *child| { 445 switch (child.widget) { 446 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 447 else => {}, 448 } 449 } 450 451 // web browsers provide scrolling, so let every widget grow to 452 // its natural size instead of clipping inside the diff scroll 453 const build_constraint = if (self.session.is_terminal) constraint else layout.Constraint{ 454 .min_size = .{ .width = null, .height = null }, 455 .max_size = .{ .width = null, .height = null }, 456 }; 457 try self.box.build(allocator, build_constraint, root_focus); 458 459 // if the diff pane is selected but focus is still elsewhere in this view, 460 // the pane was too narrow to lay out when focus crossed over. it's laid 461 // out now, so focus its hunk. 462 if (self.diffActive() and inner.children.count() > 0) { 463 if (root_focus.grandchild_id) |g| { 464 const in_view = self.box.getFocus().children.contains(g); 465 const in_diff = inner.children.contains(g); 466 if (in_view and !in_diff) 467 try root_focus.setFocus(inner.getFocus().child_id orelse inner.children.keys()[0]); 468 } 469 } 470 } 471 472 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 473 const sel = self.selectedCommitIndex() orelse return; 474 if (self.diffed_index) |d| if (d == sel) return; 475 try self.populateDiff(allocator, sel); 476 self.diffed_index = sel; 477 } 478 479 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 480 const commit = self.data.commits[sel]; 481 const inner = self.diffInner(); 482 483 for (inner.children.values()) |*child| child.widget.deinit(allocator); 484 inner.children.clearAndFree(allocator); 485 inner.getFocus().child_id = null; 486 self.load_more_id = null; 487 488 for (commit.hunks) |hunk| try self.addHunkBox(allocator, inner, hunk.lines); 489 if (commit.has_more) try self.addLoadMore(allocator, inner, commit.oid, commit.shown_hunks); 490 self.shown_hunks = commit.shown_hunks; 491 self.has_more = commit.has_more; 492 493 const sc = self.diffScroll(); 494 sc.x = 0; 495 sc.y = 0; 496 } 497 498 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 499 if (self.diffActive()) { 500 try self.diffInput(allocator, key, root_focus); 501 } else { 502 try self.listInput(key, root_focus); 503 } 504 } 505 506 fn listInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 507 // up/down (and the scroll wheel) move the selection a row; page up/down 508 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 509 // on the "next" row become navigation in the host before reaching here. 510 switch (key) { 511 .arrow_right, .enter => try self.focusDiff(root_focus), 512 .arrow_down => try self.moveSelection(root_focus, 1), 513 .arrow_up => try self.moveSelection(root_focus, -1), 514 .page_down => try self.moveSelection(root_focus, page_rows), 515 .page_up => try self.moveSelection(root_focus, -page_rows), 516 .mouse => |mouse| switch (mouse.action) { 517 .scroll => |dir| try self.moveSelection(root_focus, if (dir == .up) -1 else 1), 518 else => {}, 519 }, 520 else => {}, 521 } 522 } 523 524 fn diffInput(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 525 const sc = self.diffScroll(); 526 switch (key) { 527 // left scrolls horizontally, then leaves for the list once flush left. 528 .arrow_left => { 529 if (sc.x > 0) { 530 sc.x -= 1; 531 self.clampDiffScroll(); 532 } else try self.focusList(root_focus); 533 }, 534 .arrow_right => { 535 sc.x += 1; 536 self.clampDiffScroll(); 537 }, 538 .arrow_up => try self.moveDiff(root_focus, -1), 539 .arrow_down => try self.moveDiff(root_focus, 1), 540 .page_up => try self.pageDiff(root_focus, -page_rows), 541 .page_down => try self.pageDiff(root_focus, page_rows), 542 .enter => if (self.focusedIsLoadMore(root_focus)) try self.loadMore(allocator, root_focus), 543 .mouse => |mouse| switch (mouse.action) { 544 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 545 .press => |btn| if (btn == .left and self.focusedIsLoadMore(root_focus)) try self.loadMore(allocator, root_focus), 546 else => {}, 547 }, 548 else => {}, 549 } 550 } 551 552 // move focus one hunk in `dir` (+1 down, -1 up). if the adjacent hunk is 553 // already visible, just move focus to it — don't scroll. only when it isn't 554 // visible yet do we scroll one line toward it (and focus it once it appears), 555 // so scrolling never skips past an on-screen hunk. when nothing fits to 556 // scroll (e.g. the web, where the browser scrolls the page), the clamp keeps 557 // the offset put and focus simply steps to the adjacent hunk. 558 fn moveDiff(self: *View, root_focus: *Focus, dir: isize) !void { 559 const inner = self.diffInner(); 560 const keys = inner.children.keys(); 561 if (keys.len == 0) return; 562 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 563 const target = @as(isize, @intCast(cur)) + dir; 564 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 565 566 if (in_range and self.hunkVisible(@intCast(target))) { 567 try root_focus.setFocus(keys[@intCast(target)]); 568 return; 569 } 570 571 const sc = self.diffScroll(); 572 sc.y += dir; 573 self.clampDiffScroll(); 574 if (in_range and self.hunkVisible(@intCast(target))) 575 try root_focus.setFocus(keys[@intCast(target)]); 576 } 577 578 // page a fixed number of lines, then focus the leading visible hunk 579 // (bottom-most when paging down, top-most when paging up). 580 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 581 const sc = self.diffScroll(); 582 sc.y += delta; 583 self.clampDiffScroll(); 584 try self.focusVisible(root_focus, delta > 0); 585 } 586 587 // whether hunk `index` is at least partly within the diff viewport, per the 588 // last build's layout rects (content space) and the current scroll offset. 589 fn hunkVisible(self: *View, index: usize) bool { 590 const inner = self.diffInner(); 591 const sc = self.diffScroll(); 592 const vp = sc.grid orelse return false; 593 const r = inner.children.values()[index].rect orelse return false; 594 const top = sc.y; 595 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 596 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 597 } 598 599 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 600 // the bottom-most visible (for downward motion), else the top-most. 601 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 602 const inner = self.diffInner(); 603 const sc = self.diffScroll(); 604 const vp = sc.grid orelse return; 605 const top = sc.y; 606 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 607 var chosen: ?usize = null; 608 for (inner.children.keys(), inner.children.values()) |id, *child| { 609 const r = child.rect orelse continue; // content-space layout rect 610 const r_top = r.y; 611 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 612 if (r_bot <= top or r_top >= bottom) continue; // not visible 613 chosen = id; 614 if (!prefer_last) break; // first visible 615 } 616 if (chosen) |id| try root_focus.setFocus(id); 617 } 618 619 fn focusedIsLoadMore(self: *View, root_focus: *Focus) bool { 620 const id = self.load_more_id orelse return false; 621 return root_focus.grandchild_id == id; 622 } 623 624 // keep the diff scroll within its content, using the last build's grids. the 625 // scroll bar's reserved column/row isn't part of the content viewport, so 626 // exclude it (as scrollToRect does) or the last column/row stays unreachable. 627 fn clampDiffScroll(self: *View) void { 628 const sc = self.diffScroll(); 629 const vp = sc.grid orelse return; 630 const content = sc.child.box.grid orelse return; 631 const view_w = vp.size.width - sc.bar_w; 632 const view_h = vp.size.height - sc.bar_h; 633 const max_y: isize = if (content.size.height > view_h) @intCast(content.size.height - view_h) else 0; 634 const max_x: isize = if (content.size.width > view_w) @intCast(content.size.width - view_w) else 0; 635 sc.y = std.math.clamp(sc.y, 0, max_y); 636 sc.x = std.math.clamp(sc.x, 0, max_x); 637 } 638 639 // enter the diff pane by focusing the hunk that was focused last (the box's 640 // child_id remembers it across the trip to the commit list); fall back to the 641 // first hunk on a fresh diff. the host arrives here on right-arrow or Enter 642 // from the list. a diff with no hunks can't be entered. 643 fn focusDiff(self: *View, root_focus: *Focus) !void { 644 const inner = self.diffInner(); 645 if (inner.children.count() == 0) return; 646 const target = inner.getFocus().child_id orelse inner.children.keys()[0]; 647 try root_focus.setFocus(target); 648 if (root_focus.grandchild_id == target) return; 649 // the diff pane wasn't laid out last build (too narrow to show beside 650 // the list), so its hunks aren't in the focus tree yet. select the pane 651 // at the box level, and the hunk will be focused after the next build. 652 self.box.getFocus().child_id = self.diffOuter().getFocus().id; 653 } 654 655 fn focusList(self: *View, root_focus: *Focus) !void { 656 const lb = self.listBox(); 657 const id = lb.getFocus().child_id orelse (if (lb.children.count() > 0) lb.children.keys()[0] else return); 658 try root_focus.setFocus(id); 659 } 660 661 fn moveSelection(self: *View, root_focus: *Focus, delta: isize) !void { 662 const lb = self.listBox(); 663 const keys = lb.children.keys(); 664 if (keys.len == 0) return; 665 const cur_id = lb.getFocus().child_id orelse return; 666 const cur: isize = @intCast(lb.children.getIndex(cur_id) orelse return); 667 const last: isize = @intCast(keys.len - 1); 668 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 669 if (next == @as(usize, @intCast(cur))) return; 670 try root_focus.setFocus(keys[next]); 671 if (lb.children.values()[next].rect) |rect| self.listScroll().scrollToRect(rect); 672 } 673 674 // render the next budget of hunks for the displayed commit and append them. 675 // the TUI reopens the repo here; the wasm path has no repo and the web host 676 // drives "load more" via the GET endpoint instead, so the repo code is 677 // comptime-elided from the wasm build. 678 fn loadMore(self: *View, allocator: std.mem.Allocator, root_focus: *Focus) !void { 679 // the wasm build has no repo; it drives "load more" via the GET endpoint. 680 if (!builtin.cpu.arch.isWasm()) try self.loadMoreNative(allocator, root_focus); 681 } 682 683 fn loadMoreNative(self: *View, allocator: std.mem.Allocator, root_focus: *Focus) !void { 684 const io = self.session.io orelse return; 685 const repos_dir = self.session.repos_dir orelse return; 686 const sel = self.diffed_index orelse return; 687 const commit = self.data.commits[sel]; 688 if (commit.oid.len != xit.hash.hexLen(.sha1)) return; 689 690 const aa = self.session.page_arena.allocator(); 691 const gpa = self.session.page_arena.child_allocator; 692 const repo_path = try std.fs.path.join(aa, &.{ repos_dir, self.data.repo_dir_hex }); 693 var repo = rp.Repo(.xit, .{}).open(io, gpa, .{ .path = repo_path }) catch return; 694 defer repo.deinit(io, gpa); 695 696 var oid: [xit.hash.hexLen(.sha1)]u8 = undefined; 697 @memcpy(&oid, commit.oid); 698 const rendered = renderCommitDiff(io, gpa, aa, &repo, oid, self.shown_hunks, diff_budget) catch return; 699 700 const inner = self.diffInner(); 701 if (self.load_more_id) |id| { 702 if (inner.children.fetchOrderedRemove(id)) |kv| { 703 var w = kv.value.widget; 704 w.deinit(allocator); 705 } 706 self.load_more_id = null; 707 } 708 // focus the last already-shown hunk (the one just above where "load 709 // more" was). it's already in the focus tree from the last build, so the 710 // focus lands; the freshly-appended hunks aren't in the tree until the 711 // next build, so focusing one here would silently no-op. the user scrolls 712 // down into the new hunks from here. 713 const shown = inner.children.count(); // old hunk count ("load more" removed) 714 for (rendered.hunks) |hunk| try self.addHunkBox(allocator, inner, hunk.lines); 715 self.shown_hunks = rendered.shown_hunks; 716 self.has_more = rendered.has_more; 717 if (rendered.has_more) try self.addLoadMore(allocator, inner, commit.oid, rendered.shown_hunks); 718 if (shown > 0) try root_focus.setFocus(inner.children.keys()[shown - 1]); 719 } 720 721 pub fn clearGrid(self: *View) void { 722 self.box.clearGrid(); 723 } 724 725 pub fn getGrid(self: View) ?Grid { 726 return self.box.getGrid(); 727 } 728 729 pub fn getFocus(self: *View) *Focus { 730 return self.box.getFocus(); 731 } 732 733 // for the parent's "scroll up at the top jumps to the header" check. in the 734 // diff pane that means the first hunk is focused and the diff can't scroll up 735 // any further; otherwise up-arrow stays in the pane (scrolling or moving 736 // between hunks). when the list holds focus, report its selection directly. 737 pub fn getSelectedIndex(self: *View) ?usize { 738 if (self.diffActive()) { 739 const inner = self.diffInner(); 740 const cid = inner.getFocus().child_id orelse return 1; 741 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 742 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 743 } 744 const lb = self.listBox(); 745 const cid = lb.getFocus().child_id orelse return null; 746 return lb.children.getIndex(cid); 747 } 748 }; 749 750 // the "a:" link for the commits page starting at `start_oid` within `identity`. 751 fn commitsPageLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start_oid: []const u8) ![]const u8 { 752 const route = ui.RoutablePage.repoCommitsRoute(identity, start_oid) orelse return error.RouteTooLong; 753 const url = try route.urlAlloc(page_arena); 754 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 755 } 756