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