╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
359 { 360 var list_scroll = blk: { 361 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 362 errdefer list_box.deinit(allocator); 363 for (data.commits) |commit| { 364 // an in-page "ai:" anchor so a commit row is clickable with 365 // js off (the browser follows it, rooting the list there); 366 // with wasm the click just selects it and swaps the diff pane. 367 try addRow(allocator, &list_box, commit.message, try commitRowLink(session.page_arena, data.identity, commit.oid)); 368 } 369 if (data.next_start) |next| { 370 try addRow(allocator, &list_box, "next →", try commitsLink(session.page_arena, data.identity, next, 0)); 371 } 372 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 373 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 374 }; 375 errdefer list_scroll.deinit(allocator); 376 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 377 } 378 379 // the diff pane — a frame around a scroll of the hunks 380 { 381 var diff_outer = blk: { 382 var diff_scroll = blk2: { 383 var diff_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 384 errdefer diff_inner.deinit(allocator); 385 // fill the pane (content top-left, scroll bars pinned to the 386 // edges) rather than shrinking to the diff content. 387 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = diff_inner }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }); 388 }; 389 errdefer diff_scroll.deinit(allocator); 390 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 391 errdefer frame.deinit(allocator); 392 // the frame's selected child is its scroll, so the focus chain 393 // reaches a hunk (populateDiff points the scroll's inner box at 394 // one), letting focus recovery descend into the diff pane after 395 // it's laid out beside a too-narrow list. 396 frame.getFocus().child_id = diff_scroll.getFocus().id; 397 try frame.children.put(allocator, diff_scroll.getFocus().id, .{ .widget = .{ .scroll = diff_scroll }, .rect = null, .min_size = null }); 398 break :blk frame; 399 }; 400 errdefer diff_outer.deinit(allocator); 401 try box.children.put(allocator, diff_outer.getFocus().id, .{ .widget = .{ .box = diff_outer }, .rect = null, .min_size = .{ .width = diff_min_width, .height = null } }); 402 } 403 404 box.getFocus().child_id = box.children.keys()[list_index]; 405 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 406 407 // focus lives in the split; the banner isn't focusable. 408 outer.getFocus().child_id = outer.children.keys()[content_index]; 409 410 return .{ 411 .box = outer, 412 .data = data, 413 .session = session, 414 .diffed_index = null, 415 }; 416 } 417 418 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 419 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 420 errdefer row.deinit(allocator); 421 row.getFocus().focusable = true; 422 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 423 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 424 } 425 426 // one hunk as a focusable multi-line text box. its lines sit flush (the 427 // inner Text rows have no border); the box's hidden border reserves the 428 // space the double border occupies when focused, so focusing doesn't shift 429 // layout. `lines` are joined into the page arena (text_box borrows it). 430 fn addHunkBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), lines: []const []const u8) !void { 431 if (lines.len == 0) return; 432 const text = try std.mem.join(self.session.page_arena.allocator(), "\n", lines); 433 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 434 errdefer tb.deinit(allocator); 435 tb.getFocus().focusable = true; 436 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 437 } 438 439 // a focusable file-path label shown above the first hunk of each file. 440 fn addPathBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), path: []const u8) !void { 441 _ = self; 442 var tb = try wgt.TextBox(ui.Widget).init(allocator, path, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 443 errdefer tb.deinit(allocator); 444 tb.getFocus().focusable = true; 445 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 446 } 447 448 // a focusable window-navigation row ("previous"/"next"). it's a link to this 449 // commit's route at `target_after`, so activating it (the host follows the 450 // "a:" link) reloads the page on the adjacent window — same on TUI and web. 451 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, oid: []const u8, target_after: usize) !void { 452 const link = try commitsLink(self.session.page_arena, self.data.identity, oid, target_after); 453 var tb = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 454 errdefer tb.deinit(allocator); 455 tb.getFocus().focusable = true; 456 tb.getFocus().kind = .{ .custom = link }; 457 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 458 } 459 460 // a focusable row for the top of the diff pane linking to the files tab at 461 // this commit's tree, so its files are always viewable as of this object. 462 fn addViewFilesLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), oid: []const u8) !void { 463 const link = try filesObjectLink(self.session.page_arena, self.data.identity, oid); 464 var tb = try wgt.TextBox(ui.Widget).init(allocator, "view files at this commit", .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 465 errdefer tb.deinit(allocator); 466 tb.getFocus().focusable = true; 467 tb.getFocus().kind = .{ .custom = link }; 468 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 469 } 470 471 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 472 self.box.deinit(allocator); 473 } 474 475 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 476 return &self.box.children.values()[content_index].widget.box; 477 } 478 479 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 480 return &self.contentBox().children.values()[list_index].widget.scroll; 481 } 482 483 fn listBox(self: *View) *wgt.Box(ui.Widget) { 484 return &self.listScroll().child.box; 485 } 486 487 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 488 return &self.contentBox().children.values()[diff_index].widget.box; 489 } 490 491 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 492 return &self.diffOuter().children.values()[0].widget.scroll; 493 } 494 495 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 496 return &self.diffScroll().child.box; 497 } 498 499 fn diffActive(self: *View) bool { 500 const content = self.contentBox(); 501 const cid = content.getFocus().child_id orelse return false; 502 return content.children.getIndex(cid) == diff_index; 503 } 504 505 // the selected commit's index, or null when the "next" row is selected. 506 fn selectedCommitIndex(self: *View) ?usize { 507 const lb = self.listBox(); 508 const cid = lb.getFocus().child_id orelse return null; 509 const idx = lb.children.getIndex(cid) orelse return null; 510 if (idx >= self.data.commits.len) return null; 511 return idx; 512 } 513 514 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 515 self.clearGrid(); 516 517 // swap the diff pane to the selected commit when it changes. 518 try self.refreshDiff(allocator); 519 520 // mirror the focused commit into the url so it updates as the selection 521 // moves, but only while focus is inside this view (the list or diff). when 522 // focus sits on the header tab, use the page's base /commits route. 523 if (root_focus.grandchild_id) |g| { 524 if (self.box.getFocus().children.contains(g)) { 525 if (self.selectedCommitIndex()) |sel| { 526 // mirror the commit's current diff window so the url stays 527 // linkable (0 for any commit but the windowed start one). 528 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, self.data.commits[sel].oid, self.data.commits[sel].window_start)) |route| 529 self.session.data.current_page = route; 530 } 531 } 532 } 533 534 // the selected list row shows a border (the focused TextBox upgrades it 535 // to a double border itself); the rest stay borderless. 536 const lb = self.listBox(); 537 for (lb.children.keys(), lb.children.values()) |id, *child| { 538 switch (child.widget) { 539 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 540 else => {}, 541 } 542 } 543 544 // the selected hunk (or the "load more" row) shows a single border (the 545 // focused TextBox upgrades it to a double border itself, so it stays 546 // single when focus is on the commit list); the rest keep their 547 // space-reserving hidden border. 548 const inner = self.diffInner(); 549 for (inner.children.keys(), inner.children.values()) |id, *child| { 550 switch (child.widget) { 551 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 552 else => {}, 553 } 554 } 555 556 // cap the list at list_max_width only while the diff pane fits beside it. 557 // the box drops the diff when the width can't hold both minimums, so when 558 // it's that narrow we lift the cap and let the list fill the whole width. 559 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 560 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 561 562 // stretch the diff pane across the rest of the width so it fills the area 563 // rather than shrinking to its content; its scroll fills the pane. when 564 // too narrow for both, it fills the whole width. 565 if (constraint.max_size.width) |w| { 566 self.contentBox().children.values()[diff_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 567 } else { 568 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_min_width, .height = null }; 569 } 570 571 // the web bounds the layout to the browser viewport like the terminal; 572 // each Scroll's web-native mode hands its full content to a real 573 // scrollable element, so we no longer build unbounded here. 574 // 575 // crossing panes only re-selects the content box's child (focusDiff / 576 // focusList); when the window is too narrow to show both, the pane that 577 // held focus is dropped here. the framework recovers focus after the 578 // top-level build by re-deriving it down the selected-child chain, so 579 // there's nothing to fix up afterward. 580 try self.box.build(allocator, constraint, root_focus); 581 } 582 583 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 584 const sel = self.selectedCommitIndex() orelse return; 585 if (self.diffed_index) |d| if (d == sel) return; 586 try self.populateDiff(allocator, sel); 587 self.diffed_index = sel; 588 } 589 590 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 591 const commit = self.data.commits[sel]; 592 const inner = self.diffInner(); 593 594 for (inner.children.values()) |*child| child.widget.deinit(allocator); 595 inner.children.clearAndFree(allocator); 596 inner.getFocus().child_id = null; 597 598 // the first row always links to viewing the repo's files at this commit. 599 try self.addViewFilesLink(allocator, inner, commit.oid); 600 601 // a "previous" row and a "next" row at the bottom reload the page on the 602 // adjacent diff window. 603 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page); 604 for (commit.hunks) |hunk| { 605 if (hunk.path) |path| try self.addPathBox(allocator, inner, path); 606 try self.addHunkBox(allocator, inner, hunk.lines); 607 } 608 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page); 609 610 // point the pane at its first row so focus recovery can land here. 611 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 612 613 // reset the scroll to the top for the newly-shown commit: directly on the 614 // terminal (the wasm offset), and via a version bump on the web (so the 615 // renderer's scroll id changes and JS drops the preserved position). 616 const sc = self.diffScroll(); 617 sc.x = 0; 618 sc.y = 0; 619 sc.getFocus().version +%= 1; 620 } 621 622 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 623 _ = allocator; 624 if (self.diffActive()) { 625 try self.diffInput(key, root_focus); 626 } else { 627 try self.listInput(key, root_focus); 628 } 629 } 630 631 fn listInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 632 // up/down (and the scroll wheel) move the selection a row; page up/down 633 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 634 // on the "next" row become navigation in the host before reaching here. 635 switch (key) { 636 .enter => if (self.selectedCommitIndex() != null) 637 try self.focusDiff(root_focus) 638 else if (self.data.next_start) |next| { 639 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, next, 0)) |route| 640 try self.session.navigate(route); 641 }, 642 .arrow_right => try self.focusDiff(root_focus), 643 .arrow_down => try self.moveSelection(root_focus, 1), 644 .arrow_up => try self.moveSelection(root_focus, -1), 645 .page_down => try self.moveSelection(root_focus, 10), 646 .page_up => try self.moveSelection(root_focus, -10), 647 .end => try self.moveSelection(root_focus, @intCast(self.listBox().children.count())), 648 .home => try self.moveSelection(root_focus, -@as(isize, @intCast(self.listBox().children.count()))), 649 .mouse => |mouse| switch (mouse.action) { 650 .scroll => |dir| try self.moveSelection(root_focus, if (dir == .up) -1 else 1), 651 else => {}, 652 }, 653 else => {}, 654 } 655 } 656 657 fn diffInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 658 const sc = self.diffScroll(); 659 switch (key) { 660 // left scrolls horizontally, then leaves for the list once flush left. 661 .arrow_left => { 662 if (sc.x > 0) { 663 sc.x -= 1; 664 self.clampDiffScroll(); 665 } else try self.focusList(root_focus); 666 }, 667 .arrow_right => { 668 sc.x += 1; 669 self.clampDiffScroll(); 670 }, 671 .arrow_up => try self.moveDiff(root_focus, -1), 672 .arrow_down => try self.moveDiff(root_focus, 1), 673 .page_up => try self.pageDiff(root_focus, -10), 674 .page_down => try self.pageDiff(root_focus, 10), 675 .home => try self.jumpDiff(root_focus, false), 676 .end => try self.jumpDiff(root_focus, true), 677 // Enter / a click on a "previous"/"next" row follow its "a:" link in 678 // the host (it reloads the adjacent window), so they don't reach here. 679 .mouse => |mouse| switch (mouse.action) { 680 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 681 else => {}, 682 }, 683 else => {}, 684 } 685 } 686 687 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 688 // line count unless there is a new hunk visible (in which case it is a 689 // hunk count). on the web `delta` is always a hunk count. 690 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 691 const inner = self.diffInner(); 692 const keys = inner.children.keys(); 693 if (keys.len == 0) return; 694 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 695 const target = @as(isize, @intCast(cur)) + delta; 696 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 697 698 if (self.session.is_terminal) { 699 if (in_range and self.hunkVisible(@intCast(target))) { 700 root_focus.setFocus(keys[@intCast(target)]); 701 return; 702 } 703 704 const sc = self.diffScroll(); 705 sc.y += delta * 5; // magnify because it's a line count 706 self.clampDiffScroll(); 707 if (in_range and self.hunkVisible(@intCast(target))) { 708 root_focus.setFocus(keys[@intCast(target)]); 709 } 710 } else { 711 if (in_range) { 712 root_focus.setFocus(keys[@intCast(target)]); 713 } 714 } 715 } 716 717 // page a fixed number of lines, then focus the leading visible hunk 718 // (bottom-most when paging down, top-most when paging up). in the 719 // terminal, `delta` is a line count, and on the web it's a hunk count. 720 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 721 if (self.session.is_terminal) { 722 const sc = self.diffScroll(); 723 sc.y += delta * 5; // magnify because it's a line count 724 self.clampDiffScroll(); 725 try self.focusVisible(root_focus, delta > 0); 726 } else { 727 const inner = self.diffInner(); 728 const keys = inner.children.keys(); 729 if (keys.len == 0) return; 730 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 731 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 732 root_focus.setFocus(keys[target]); 733 } 734 } 735 736 // jump to the first or last hunk. on the web the browser scrolls to the 737 // focused hunk; on the terminal pin the scroll to the top/bottom too. 738 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 739 const inner = self.diffInner(); 740 const keys = inner.children.keys(); 741 if (keys.len == 0) return; 742 if (self.session.is_terminal) { 743 const sc = self.diffScroll(); 744 sc.y = if (to_end) std.math.maxInt(isize) else 0; 745 self.clampDiffScroll(); 746 } 747 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 748 } 749 750 // whether hunk `index` is at least partly within the diff viewport, per the 751 // last build's layout rects (content space) and the current scroll offset. 752 fn hunkVisible(self: *View, index: usize) bool { 753 const inner = self.diffInner(); 754 const sc = self.diffScroll(); 755 const vp = sc.grid orelse return false; 756 const r = inner.children.values()[index].rect orelse return false; 757 const top = sc.y; 758 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 759 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 760 } 761 762 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 763 // the bottom-most visible (for downward motion), else the top-most. 764 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 765 const inner = self.diffInner(); 766 const sc = self.diffScroll(); 767 const vp = sc.grid orelse return; 768 const top = sc.y; 769 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 770 var chosen: ?usize = null; 771 for (inner.children.keys(), inner.children.values()) |id, *child| { 772 const r = child.rect orelse continue; // content-space layout rect 773 const r_top = r.y; 774 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 775 if (r_bot <= top or r_top >= bottom) continue; // not visible 776 chosen = id; 777 if (!prefer_last) break; // first visible 778 } 779 if (chosen) |id| root_focus.setFocus(id); 780 } 781 782 // keep the diff scroll within its content, using the last build's grids. the 783 // scroll bar's reserved column/row isn't part of the content viewport, so 784 // exclude it (as scrollToRect does) or the last column/row stays unreachable. 785 fn clampDiffScroll(self: *View) void { 786 const sc = self.diffScroll(); 787 const vp = sc.grid orelse return; 788 const content = sc.child.box.grid orelse return; 789 const view_w = vp.size.width - sc.bar_w; 790 const view_h = vp.size.height - sc.bar_h; 791 const max_y: isize = if (content.size.height > view_h) @intCast(content.size.height - view_h) else 0; 792 const max_x: isize = if (content.size.width > view_w) @intCast(content.size.width - view_w) else 0; 793 sc.y = std.math.clamp(sc.y, 0, max_y); 794 sc.x = std.math.clamp(sc.x, 0, max_x); 795 } 796 797 // enter the diff pane. the host arrives here on right-arrow or Enter from the 798 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 799 // case where the pane isn't laid out yet (it gets selected, then focused after 800 // the next build, landing on its remembered hunk). 801 fn focusDiff(self: *View, root_focus: *Focus) !void { 802 if (self.diffInner().children.count() == 0) return; 803 root_focus.setFocus(self.diffOuter().getFocus().id); 804 } 805 806 // return to the list. 807 fn focusList(self: *View, root_focus: *Focus) !void { 808 root_focus.setFocus(self.listScroll().getFocus().id); 809 } 810 811 fn moveSelection(self: *View, root_focus: *Focus, delta: isize) !void { 812 const lb = self.listBox(); 813 const keys = lb.children.keys(); 814 if (keys.len == 0) return; 815 const cur_id = lb.getFocus().child_id orelse return; 816 const cur: isize = @intCast(lb.children.getIndex(cur_id) orelse return); 817 const last: isize = @intCast(keys.len - 1); 818 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 819 if (next == @as(usize, @intCast(cur))) return; 820 root_focus.setFocus(keys[next]); 821 if (lb.children.values()[next].rect) |rect| self.listScroll().scrollToRect(rect); 822 } 823 824 pub fn clearGrid(self: *View) void { 825 self.box.clearGrid(); 826 } 827 828 pub fn getGrid(self: View) ?Grid { 829 return self.box.getGrid(); 830 } 831 832 pub fn getFocus(self: *View) *Focus { 833 return self.box.getFocus(); 834 } 835 836 // for the parent's "scroll up at the top jumps to the header" check. in the 837 // diff pane that means the first hunk is focused and the diff can't scroll up 838 // any further; otherwise up-arrow stays in the pane (scrolling or moving 839 // between hunks). when the list holds focus, report its selection directly. 840 pub fn getSelectedIndex(self: *View) ?usize { 841 if (self.diffActive()) { 842 const inner = self.diffInner(); 843 const cid = inner.getFocus().child_id orelse return 1; 844 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 845 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 846 } 847 const lb = self.listBox(); 848 const cid = lb.getFocus().child_id orelse return null; 849 return lb.children.getIndex(cid); 850 } 851 }; 852 853 // the "a:" navigation link for the commits page walking from commit `oid` within 854 // `identity`, showing the selected commit's first `after` hunks (0 = default). 855 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, after: usize) ![]const u8 { 856 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, after) orelse return error.RouteTooLong; 857 const url = try route.urlAlloc(page_arena); 858 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 859 } 860 861 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 862 // directory, within `identity` ("owner/name"). 863 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 864 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 865 const url = try route.urlAlloc(page_arena); 866 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 867 } 868 869 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 870 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 871 // in-page (crossPageLink ignores it); the href is only followed with js off. 872 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 873 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0) orelse return error.RouteTooLong; 874 const url = try route.urlAlloc(page_arena); 875 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 876 } 877