╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
352 } 353 354 // the diff pane — a frame around a scroll of the hunks 355 { 356 var diff_outer = blk: { 357 var diff_scroll = try Diff.View.initEmpty(allocator, session); 358 errdefer diff_scroll.deinit(allocator); 359 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 360 errdefer frame.deinit(allocator); 361 // the frame's selected child is its scroll, so the focus chain 362 // reaches a hunk (populateDiff points the scroll's inner box at 363 // one), letting focus recovery descend into the diff pane after 364 // it's laid out beside a too-narrow list. 365 frame.getFocus().child_id = diff_scroll.getFocus().id; 366 try frame.children.put(allocator, diff_scroll.getFocus().id, .{ .widget = .{ .diff_view = diff_scroll }, .rect = null, .min_size = null }); 367 break :blk frame; 368 }; 369 errdefer diff_outer.deinit(allocator); 370 diff_outer.getFocus().mode = .mouse; 371 try box.children.put(allocator, diff_outer.getFocus().id, .{ .widget = .{ .box = diff_outer }, .rect = null, .min_size = .{ .width = diff_min_width, .height = null } }); 372 } 373 374 box.getFocus().child_id = box.children.keys()[list_index]; 375 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 376 377 // focus lives in the split; the banner isn't focusable. 378 outer.getFocus().child_id = outer.children.keys()[content_index]; 379 380 return .{ 381 .box = outer, 382 .data = data, 383 .session = session, 384 .diffed_index = null, 385 }; 386 } 387 388 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 389 var row = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 390 errdefer row.deinit(allocator); 391 row.getFocus().mode = .all; 392 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 393 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 394 } 395 396 // a focusable window-navigation row ("previous"/"next"). it's a link to this 397 // commit's route at `target_start`, so activating it (the host follows the 398 // "a:" link) reloads the page on the adjacent window — same on TUI and web. 399 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, oid: []const u8, target_start: usize, path: []const u8) !void { 400 const link = try commitsLink(self.session.page_arena, self.data.location, oid, target_start, path); 401 var tb = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 402 errdefer tb.deinit(allocator); 403 tb.getFocus().mode = .all; 404 tb.getFocus().kind = .{ .custom = link }; 405 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 406 } 407 408 // a focusable row for the top of the diff pane linking to the files tab at 409 // this commit's tree, so its files are always viewable as of this object. 410 fn addViewFilesLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), oid: []const u8) !void { 411 const link = try filesObjectLink(self.session.page_arena, self.data.location, oid); 412 var tb = try wgt.TextBox.init(allocator, "view files at this commit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 413 errdefer tb.deinit(allocator); 414 tb.getFocus().mode = .all; 415 tb.getFocus().kind = .{ .custom = link }; 416 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 417 } 418 419 // how much of the message a pane's box holds: a truncated preview links to 420 // the pane holding the whole thing, which is that link's destination. 421 const MessageBox = enum { preview, whole }; 422 423 // the selected commit's message. 424 fn addMessageBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), commit: Commit, kind: MessageBox) !void { 425 // a truncated message keeps its box even with no whole line to show, 426 // since the box is what links to the whole thing. 427 if (commit.message.len == 0 and !commit.message_truncated) return; 428 const cut_short = commit.message_truncated and kind == .preview; 429 var tb = try messageBox(allocator, commit.message, if (cut_short) " click or press enter to see more " else ""); 430 errdefer tb.deinit(allocator); 431 if (cut_short) tb.getFocus().kind = .{ .custom = try messageLink(self.session.page_arena, self.data.location, commit.oid) }; 432 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 433 } 434 435 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 436 self.box.deinit(allocator); 437 } 438 439 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 440 return &self.box.children.values()[content_index].widget.box; 441 } 442 443 fn header(self: *View) *Header.View { 444 return &self.box.children.values()[header_index].widget.repo_commits_header; 445 } 446 447 fn headerActive(self: *View) bool { 448 return self.box.getFocus().child_id == self.header().getFocus().id; 449 } 450 451 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 452 return &self.contentBox().children.values()[list_index].widget.scroll; 453 } 454 455 fn listBox(self: *View) *wgt.Box(ui.Widget) { 456 return &self.listScroll().child.box; 457 } 458 459 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 460 return &self.contentBox().children.values()[diff_index].widget.box; 461 } 462 463 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 464 return &self.diffOuter().children.values()[0].widget.diff_view.scroll; 465 } 466 467 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 468 return &self.diffScroll().child.box; 469 } 470 471 fn diffActive(self: *View) bool { 472 const content = self.contentBox(); 473 const cid = content.getFocus().child_id orelse return false; 474 return content.children.getIndex(cid) == diff_index; 475 } 476 477 // what the pane shows for the commit at `sel`. the page's content only 478 // applies to the commit it walks from; the rest show their plain diff. 479 fn paneContent(self: *View, sel: usize) Self.Content { 480 return if (sel == 0) self.data.content else .{ .diff = .{} }; 481 } 482 483 // the selected commit's index, or null when the "next" row is selected. 484 fn selectedCommitIndex(self: *View) ?usize { 485 const lb = self.listBox(); 486 const cid = lb.getFocus().child_id orelse return null; 487 const idx = lb.children.getIndex(cid) orelse return null; 488 if (idx >= self.data.commits.len) return null; 489 return idx; 490 } 491 492 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 493 self.clearGrid(); 494 495 // swap the diff pane to the selected commit when it changes. 496 try self.refreshDiff(allocator); 497 498 // the selected list row shows a border (the focused TextBox upgrades it 499 // to a double border itself); the rest stay borderless. 500 const lb = self.listBox(); 501 for (lb.children.keys(), lb.children.values()) |id, *child| { 502 switch (child.widget) { 503 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 504 else => {}, 505 } 506 } 507 508 // cap the list at list_max_width only while the diff pane fits beside it. 509 // the box drops the diff when the width can't hold both minimums, so when 510 // it's that narrow we lift the cap and let the list fill the whole width. 511 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 512 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 513 514 // stretch the diff pane across the rest of the width so it fills the area 515 // rather than shrinking to its content; its scroll fills the pane. when 516 // too narrow for both, it fills the whole width. 517 const diff_width: usize = if (constraint.max_size.width) |w| 518 (if (both_panes_fit) w - list_max_width else w) 519 else 520 diff_min_width; 521 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_width, .height = null }; 522 523 // the message is the pane's only wrapping row, and wrapping needs a 524 // bounded width, which the pane's scroll doesn't grant (it scrolls 525 // horizontally too). cap it to the pane, leaving room for the border 526 // and the scrollbar column. re-capped each build so it tracks resizes. 527 for (self.diffInner().children.values()) |*child| switch (child.widget) { 528 .text_box => |text_box| if (text_box.options.wrap_kind == .word) { 529 child.max_size = .{ .width = diff_width -| 3, .height = null }; 530 }, 531 else => {}, 532 }; 533 534 // the web bounds the layout to the browser viewport like the terminal; 535 // each Scroll's web-native mode hands its full content to a real 536 // scrollable element, so we no longer build unbounded here. 537 // 538 // crossing panes only re-selects the content box's child (focusDiff / 539 // focusList); when the window is too narrow to show both, the pane that 540 // held focus is dropped here. the framework recovers focus after the 541 // top-level build by re-deriving it down the selected-child chain, so 542 // there's nothing to fix up afterward. 543 try self.box.build(allocator, constraint, root_focus); 544 } 545 546 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 547 const sel = self.selectedCommitIndex() orelse return; 548 if (self.diffed_index) |d| if (d == sel) return; 549 try self.populateDiff(allocator, sel); 550 self.diffed_index = sel; 551 } 552 553 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 554 const commit = self.data.commits[sel]; 555 const inner = self.diffInner(); 556 557 for (inner.children.values()) |*child| child.widget.deinit(allocator); 558 inner.children.clearAndFree(allocator); 559 inner.getFocus().child_id = null; 560 561 switch (self.paneContent(sel)) { 562 .message => { 563 try self.addNavLink(allocator, inner, "← back to diff", commit.oid, 0, ""); 564 try self.addMessageBox(allocator, inner, commit, .whole); 565 }, 566 .diff => |d| { 567 if (d.path.len == 0) { 568 try self.addMessageBox(allocator, inner, commit, .preview); 569 if (commit.author != .unknown) { 570 var tb = try ui.authorBox(allocator, self.session.page_arena, commit.author); 571 errdefer tb.deinit(allocator); 572 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 573 } 574 if (commit.stats) |stats| { 575 var text: std.Io.Writer.Allocating = .init(allocator); 576 defer text.deinit(); 577 try text.writer.print("lines changed: {d}", .{stats.lines_added + stats.lines_changed}); 578 if (stats.lines_removed != 0) try text.writer.print("\nlines removed: {d}", .{stats.lines_removed}); 579 const bytes_added = stats.bytes_added >= stats.bytes_removed; 580 const bytes = if (bytes_added) stats.bytes_added - stats.bytes_removed else stats.bytes_removed - stats.bytes_added; 581 try text.writer.print("\nbytes {s}: {d}\nfiles changed: {d}", .{ 582 if (bytes_added) "added" else "removed", bytes, stats.files_added + stats.files_changed, 583 }); 584 if (stats.files_removed != 0) try text.writer.print("\nfiles removed: {d}", .{stats.files_removed}); 585 var tb = try wgt.TextBox.init(allocator, text.written(), .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " stats " }); 586 errdefer tb.deinit(allocator); 587 tb.getFocus().mode = .all; 588 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 589 } 590 try self.addViewFilesLink(allocator, inner, commit.oid); 591 } 592 593 try (Diff{ 594 .route = .{ .commit = .{ .location = self.data.location, .oid = commit.oid } }, 595 .path = d.path, 596 .window = commit.window, 597 }).appendWindow(allocator, self.session, inner); 598 }, 599 } 600 601 // point the pane at its first row so focus recovery can land here. 602 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 603 604 // reset the scroll to the top for the newly-shown commit: directly on the 605 // terminal (the wasm offset), and via a version bump on the web (so the 606 // renderer's scroll id changes and JS drops the preserved position). 607 const sc = self.diffScroll(); 608 sc.x = 0; 609 sc.y = 0; 610 sc.getFocus().version +%= 1; 611 } 612 613 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 614 if (self.headerActive()) { 615 if (key == .arrow_down) { 616 root_focus.setFocus(self.contentBox().getFocus().id); 617 } else { 618 try self.header().input(allocator, key, root_focus); 619 } 620 return; 621 } 622 if (key == .arrow_up and self.contentAtTop() and self.header().focusCloneUrl(root_focus)) return; 623 if (self.diffActive()) { 624 if (key == .arrow_left and self.diffScroll().x == 0) { 625 self.focusList(root_focus); 626 } else { 627 try self.diffOuter().children.values()[0].widget.diff_view.input(allocator, key, root_focus); 628 } 629 } else { 630 try self.listInput(key, root_focus); 631 } 632 } 633 634 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 635 // up/down (and the scroll wheel) move the selection a row; page up/down 636 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 637 // on the "next" row become navigation in the host before reaching here. 638 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 639 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 640 return; 641 } 642 switch (key) { 643 .enter => if (self.selectedCommitIndex() != null) 644 self.focusDiff(root_focus) 645 else if (self.data.next_start) |next| { 646 if (self.data.location.commitsRoute(.object, next, 0, "")) |route| 647 try self.session.navigate(route); 648 }, 649 .arrow_right => self.focusDiff(root_focus), 650 else => {}, 651 } 652 } 653 654 // enter the diff pane. the host arrives here on right-arrow or Enter from the 655 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 656 // case where the pane isn't laid out yet (it gets selected, then focused after 657 // the next build, landing on its remembered hunk). 658 fn focusDiff(self: *View, root_focus: *Focus) void { 659 if (self.diffInner().children.count() == 0) return; 660 root_focus.setFocus(self.diffOuter().getFocus().id); 661 } 662 663 // return to the list. 664 fn focusList(self: *View, root_focus: *Focus) void { 665 root_focus.setFocus(self.listScroll().getFocus().id); 666 } 667 668 pub fn clearGrid(self: *View) void { 669 self.box.clearGrid(); 670 } 671 672 pub fn getGrid(self: View) ?Grid { 673 return self.box.getGrid(); 674 } 675 676 pub fn getFocus(self: *View) *Focus { 677 return self.box.getFocus(); 678 } 679 680 fn contentAtTop(self: *View) bool { 681 if (self.diffActive()) { 682 const inner = self.diffInner(); 683 const cid = inner.getFocus().child_id orelse return true; 684 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 685 return first_focused and self.diffScroll().y == 0; 686 } 687 const lb = self.listBox(); 688 const cid = lb.getFocus().child_id orelse return true; 689 return lb.children.getIndex(cid) == 0; 690 } 691 692 // only the clone-url row sits directly below the repository header. 693 pub fn atTop(self: *View) bool { 694 return self.headerActive() or (!self.header().hasCloneUrl() and self.contentAtTop()); 695 } 696 697 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 698 return self.header().focusCloneUrl(root_focus); 699 } 700 }; 701 702 // the "a:" navigation link for the commits page walking from commit `oid` within 703 // `location`, windowing the selected commit's diff from hunk `start`, filtered 704 // to `path` ("" = every file). 705 fn commitsLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 706 const route = location.commitsRoute(.object, oid, start, path) orelse return error.RouteTooLong; 707 const url = try route.toUrl(page_arena); 708 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 709 } 710 711 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 712 // directory, within `location`. 713 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 714 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 715 const url = try route.toUrl(page_arena); 716 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 717 } 718 719 // the in-page "ai:" anchor for selecting a commit with its current pane content 720 // and window. the href is only followed with js off. 721 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, commit: Commit, content: Content) ![]const u8 { 722 const route = (switch (content) { 723 .message => location.commitMessageRoute(.object, commit.oid), 724 .diff => |diff| location.commitsRoute(.object, commit.oid, commit.window.start, diff.path), 725 }) orelse return error.RouteTooLong; 726 const url = try route.toUrl(page_arena); 727 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 728 } 729 730 // the "a:" link to the page showing `oid`'s message on its own. 731 fn messageLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 732 const route = location.commitMessageRoute(.object, oid) orelse return error.RouteTooLong; 733 const url = try route.toUrl(page_arena); 734 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 735 } 736 737 // the "<ref_or_oid> <value>" banner shown above the log. 738 pub const Header = struct { 739 content: []const u8, 740 741 // `value` arrives url-encoded, so decode it for display. 742 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 743 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 744 return .{ 745 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 746 }; 747 } 748 749 pub const View = struct { 750 box: wgt.Box(ui.Widget), 751 data: *const Header, 752 753 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !Header.View { 754 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 755 errdefer box.deinit(allocator); 756 757 if (session.data.host_kind == .server) { 758 if (try ui.widget.CopyableText.initClone(allocator, session, location)) |value| { 759 var clone_url = value; 760 errdefer clone_url.deinit(allocator); 761 const min_width = clone_url.minWidth(); 762 box.getFocus().child_id = clone_url.getFocus().id; 763 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 764 } 765 } 766 767 var spacer = try ui.widget.Spacer.init(allocator); 768 errdefer spacer.deinit(allocator); 769 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 770 771 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 772 errdefer text_box.deinit(allocator); 773 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 774 775 return .{ .box = box, .data = data }; 776 } 777 778 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 779 self.box.deinit(allocator); 780 } 781 782 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 783 self.clearGrid(); 784 try self.box.build(allocator, constraint, root_focus); 785 } 786 787 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 788 const clone_url = self.cloneUrl() orelse return; 789 try clone_url.input(allocator, key, root_focus); 790 } 791 792 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 793 const clone_url = self.cloneUrl() orelse return false; 794 root_focus.setFocus(clone_url.getFocus().id); 795 return true; 796 } 797 798 pub fn hasCloneUrl(self: *Header.View) bool { 799 return self.cloneUrl() != null; 800 } 801 802 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 803 for (self.box.children.values()) |*child| switch (child.widget) { 804 .copyable_text => |*copyable_text| return copyable_text, 805 else => {}, 806 }; 807 return null; 808 } 809 810 pub fn clearGrid(self: *Header.View) void { 811 self.box.clearGrid(); 812 } 813 814 pub fn getGrid(self: Header.View) ?Grid { 815 return self.box.getGrid(); 816 } 817 818 pub fn getFocus(self: *Header.View) *Focus { 819 return self.box.getFocus(); 820 } 821 }; 822 }; 823