╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
510 const max_width = constraint.max_size.width orelse 80; 511 if (max_width == 0) return; 512 513 // lay the items out left to right, wrapping to a new row when the next 514 // one wouldn't fit. 515 self.rects.clearRetainingCapacity(); 516 var x: usize = 0; 517 var y: usize = 0; 518 var row_height: usize = 0; 519 var total_width: usize = 0; 520 for (self.text_boxes.items) |*tb| { 521 try tb.build(allocator, .{ 522 .min_size = .{ .width = null, .height = null }, 523 .max_size = .{ .width = max_width, .height = null }, 524 }, root_focus); 525 const tb_grid_maybe = tb.getGrid(); 526 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 527 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 528 if (x > 0 and x + w > max_width) { 529 x = 0; 530 y += row_height; 531 row_height = 0; 532 } 533 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 534 if (h > row_height) row_height = h; 535 x += w; 536 if (x > total_width) total_width = x; 537 } 538 539 const total_height = y + row_height; 540 if (total_width == 0 or total_height == 0) return; 541 542 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 543 errdefer grid.deinit(); 544 545 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 546 const tb_grid = tb.getGrid() orelse continue; 547 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 548 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 549 } 550 551 self.grid = grid; 552 } 553 554 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 555 _ = self; 556 _ = allocator; 557 _ = key; 558 _ = root_focus; 559 } 560 561 pub fn clearGrid(self: *TagFlow) void { 562 if (self.grid) |*grid| { 563 grid.deinit(); 564 self.grid = null; 565 } 566 for (self.text_boxes.items) |*tb| tb.clearGrid(); 567 } 568 569 pub fn getGrid(self: TagFlow) ?Grid { 570 return self.grid; 571 } 572 573 pub fn getFocus(self: *TagFlow) *Focus { 574 return self.focus; 575 } 576 577 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 578 for (self.text_boxes.items, 0..) |tb, i| { 579 if (tb.focus.id == focus_id) return i; 580 } 581 return null; 582 } 583 584 // the first item of the row below (when `downward`) or above, or null at 585 // the flow's edge. items are laid out in order, so y is nondecreasing. 586 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 587 const rects = self.rects.items; 588 if (index >= rects.len) return null; 589 const cur_y = rects[index].y; 590 if (downward) { 591 for (rects[index + 1 ..], index + 1..) |r, i| { 592 if (r.y > cur_y) return i; 593 } 594 return null; 595 } 596 // walk the earlier rows; the last one visited is the row above. 597 var prev_first: ?usize = null; 598 var i: usize = 0; 599 while (i < rects.len and rects[i].y < cur_y) { 600 prev_first = i; 601 const y = rects[i].y; 602 while (i < rects.len and rects[i].y == y) i += 1; 603 } 604 return prev_first; 605 } 606 }; 607 608 // an invisible widget that fills the horizontal space granted by its parent. 609 // used inside Box(horiz) with a min_size so the box reserves space for the 610 // children that follow, pushing them to the right. 611 pub const Spacer = struct { 612 focus: *Focus, 613 grid: ?Grid, 614 615 pub fn init(allocator: std.mem.Allocator) !Spacer { 616 return .{ 617 .focus = try Focus.create(allocator, .container), 618 .grid = null, 619 }; 620 } 621 622 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 623 self.focus.destroy(allocator); 624 if (self.grid) |*grid| { 625 grid.deinit(); 626 self.grid = null; 627 } 628 } 629 630 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 631 _ = root_focus; 632 self.clearGrid(); 633 const width = constraint.max_size.width orelse return; 634 if (width == 0) return; 635 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 636 } 637 638 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 639 _ = self; 640 _ = allocator; 641 _ = key; 642 _ = root_focus; 643 } 644 645 pub fn clearGrid(self: *Spacer) void { 646 if (self.grid) |*grid| { 647 grid.deinit(); 648 self.grid = null; 649 } 650 } 651 652 pub fn getGrid(self: Spacer) ?Grid { 653 return self.grid; 654 } 655 656 pub fn getFocus(self: *Spacer) *Focus { 657 return self.focus; 658 } 659 }; 660 661 // a one-row, non-focusable status bar showing the current page's url 662 pub const Footer = struct { 663 focus: *Focus, 664 grid: ?Grid, 665 session: *ui.Session, 666 arena: std.heap.ArenaAllocator, 667 url: []const u8 = "", 668 url_width: usize = 0, 669 670 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 671 return .{ 672 .focus = try Focus.create(allocator, .container), 673 .grid = null, 674 .session = session, 675 .arena = std.heap.ArenaAllocator.init(allocator), 676 }; 677 } 678 679 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 680 self.focus.destroy(allocator); 681 if (self.grid) |*grid| grid.deinit(); 682 self.arena.deinit(); 683 } 684 685 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 686 _ = root_focus; 687 self.clearGrid(); 688 const width = constraint.max_size.width orelse return; 689 if (width == 0) return; 690 691 _ = self.arena.reset(.retain_capacity); 692 self.url = ""; 693 self.url_width = 0; 694 const aa = self.arena.allocator(); 695 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 696 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 697 self.url = text; 698 699 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 700 errdefer grid.deinit(); 701 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 702 var i: usize = 0; 703 while (utf8.nextCodepoint()) |ch| { 704 const rune_width = xitui.width.cellWidth(ch); 705 if (i + rune_width > width) break; 706 try grid.setRune(i, 0, ch); 707 i += rune_width; 708 } 709 self.url_width = i; 710 self.grid = grid; 711 } 712 713 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 714 _ = allocator; 715 switch (key) { 716 .mouse => |mouse| { 717 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 718 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 719 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 720 }, 721 else => {}, 722 } 723 } 724 725 pub fn clearGrid(self: *Footer) void { 726 if (self.grid) |*grid| { 727 grid.deinit(); 728 self.grid = null; 729 } 730 } 731 732 pub fn getGrid(self: Footer) ?Grid { 733 return self.grid; 734 } 735 736 pub fn getFocus(self: *Footer) *Focus { 737 return self.focus; 738 } 739 }; 740 741 // a single-child wrapper that builds the child at its natural size and 742 // positions its grid in the middle of the area granted by the parent 743 // a bordered label, indented past the content it precedes 744 pub const SectionLabel = struct { 745 box: wgt.Box(Widget), 746 747 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 748 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 749 errdefer box.deinit(allocator); 750 751 { 752 var gap = try wgt.Text.init(allocator, " "); 753 errdefer gap.deinit(allocator); 754 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 755 } 756 757 { 758 var tb = try wgt.TextBox.init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 759 errdefer tb.deinit(allocator); 760 tb.getFocus().mode = .all; 761 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 762 } 763 764 box.getFocus().child_id = box.children.keys()[1]; 765 return .{ .box = box }; 766 } 767 768 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 769 self.box.deinit(allocator); 770 } 771 772 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 773 try self.box.build(allocator, constraint, root_focus); 774 } 775 776 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 777 _ = self; 778 _ = allocator; 779 _ = key; 780 _ = root_focus; 781 } 782 783 pub fn clearGrid(self: *SectionLabel) void { 784 self.box.clearGrid(); 785 } 786 787 pub fn getGrid(self: SectionLabel) ?Grid { 788 return self.box.getGrid(); 789 } 790 791 pub fn getFocus(self: *SectionLabel) *Focus { 792 return self.box.getFocus(); 793 } 794 }; 795 796 // a submit button, indented past its form's content. the web form POSTs 797 // the button's form; terminal hosts detect presses through `buttonId`. 798 pub const SubmitButton = struct { 799 box: wgt.Box(Widget), 800 801 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 802 return initLabeled(allocator, "submit"); 803 } 804 805 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 806 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 807 errdefer box.deinit(allocator); 808 809 { 810 var gap = try wgt.Text.init(allocator, " "); 811 errdefer gap.deinit(allocator); 812 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 813 } 814 815 { 816 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 817 errdefer button.deinit(allocator); 818 button.getFocus().mode = .all; 819 // the renderer distinguishes plain clickables from buttons that 820 // should POST to a server route by this kind. 821 button.getFocus().kind = .{ .custom = "submit" }; 822 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 823 } 824 825 box.getFocus().child_id = box.children.keys()[1]; 826 return .{ .box = box }; 827 } 828 829 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 830 self.box.deinit(allocator); 831 } 832 833 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 834 const button = &self.box.children.values()[1]; 835 try button.widget.text_box.setContent(allocator, label); 836 button.min_size = .{ .width = label.len + 2, .height = null }; 837 } 838 839 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 840 try self.box.build(allocator, constraint, root_focus); 841 } 842 843 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 844 _ = self; 845 _ = allocator; 846 _ = key; 847 _ = root_focus; 848 } 849 850 pub fn clearGrid(self: *SubmitButton) void { 851 self.box.clearGrid(); 852 } 853 854 pub fn getGrid(self: SubmitButton) ?Grid { 855 return self.box.getGrid(); 856 } 857 858 pub fn getFocus(self: *SubmitButton) *Focus { 859 return self.box.getFocus(); 860 } 861 862 // the button's focus id, for click hit-testing 863 pub fn buttonId(self: *const SubmitButton) usize { 864 return self.box.children.keys()[1]; 865 } 866 }; 867 868 pub const Center = struct { 869 focus: *Focus, 870 grid: ?Grid, 871 child: *Widget, 872 873 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 874 const child = try allocator.create(Widget); 875 errdefer allocator.destroy(child); 876 child.* = child_widget; 877 return .{ 878 .focus = try Focus.create(allocator, .container), 879 .grid = null, 880 .child = child, 881 }; 882 } 883 884 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 885 self.focus.destroy(allocator); 886 if (self.grid) |*grid| { 887 grid.deinit(); 888 self.grid = null; 889 } 890 self.child.deinit(allocator); 891 allocator.destroy(self.child); 892 } 893 894 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 895 self.clearGrid(); 896 self.getFocus().clear(); 897 898 // build the child without forcing it to fill min_size; it sizes 899 // itself to its content within the available max. 900 try self.child.build(allocator, .{ 901 .min_size = .{ .width = null, .height = null }, 902 .max_size = constraint.max_size, 903 }, root_focus); 904 905 const child_grid = self.child.getGrid() orelse return; 906 907 // prefer max when given; otherwise grow to max(min, child) so that 908 // when only a min is set (e.g. the wasm path passes viewport rows 909 // as min_height with no max), we can still vertically center while 910 // letting taller content extend past the min. 911 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 912 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 913 if (width == 0 or height == 0) return; 914 915 const offset_x: usize = (width -| child_grid.size.width) / 2; 916 const offset_y: usize = (height -| child_grid.size.height) / 2; 917 918 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 919 errdefer grid.deinit(); 920 try grid.drawGrid(child_grid, offset_x, offset_y); 921 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 922 self.getFocus().child_id = self.child.getFocus().id; 923 924 self.grid = grid; 925 } 926 927 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 928 try self.child.input(allocator, key, root_focus); 929 } 930 931 pub fn clearGrid(self: *Center) void { 932 if (self.grid) |*grid| { 933 grid.deinit(); 934 self.grid = null; 935 } 936 self.child.clearGrid(); 937 } 938 939 pub fn getGrid(self: Center) ?Grid { 940 return self.grid; 941 } 942 943 pub fn getFocus(self: *Center) *Focus { 944 return self.focus; 945 } 946 }; 947 948 // renders truecolor ANSI art 949 pub const AnsiArt = struct { 950 focus: *Focus, 951 grid: ?Grid, 952 content: []const u8, 953 954 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 955 return .{ 956 .focus = try Focus.create(allocator, .container), 957 .grid = null, 958 .content = content, 959 }; 960 } 961 962 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 963 self.focus.destroy(allocator); 964 if (self.grid) |*grid| { 965 grid.deinit(); 966 self.grid = null; 967 } 968 } 969 970 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 971 _ = root_focus; 972 self.clearGrid(); 973 974 // parse into rows of cells, then copy into a rectangular grid 975 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 976 defer { 977 for (rows.items) |*row| row.deinit(allocator); 978 rows.deinit(allocator); 979 } 980 var row: std.ArrayList(Grid.Cell) = .empty; 981 errdefer row.deinit(allocator); 982 983 var style: Grid.Style = .{}; 984 var width: usize = 0; 985 const content = self.content; 986 var i: usize = 0; 987 while (i < content.len) { 988 const byte = content[i]; 989 if (byte == '\n') { 990 width = @max(width, row.items.len); 991 try rows.append(allocator, row); 992 row = .empty; 993 i += 1; 994 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 995 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 996 var j = i + 2; 997 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 998 if (j >= content.len) { 999 i = content.len; // malformed trailing escape; stop 1000 } else { 1001 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1002 i = j + 1; 1003 } 1004 } else { 1005 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1006 const end = @min(content.len, i + len); 1007 const rune = content[i..end]; 1008 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1009 // a foreground color does not make a space visible. only an 1010 // effective background makes it opaque; foreground state often 1011 // remains active through the padding at the end of a row. 1012 const effective_background = if (style.inverted) style.fg else style.bg; 1013 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1014 try row.append(allocator, .{ 1015 .rune = if (transparent) null else codepoint, 1016 .style = style, 1017 }); 1018 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1019 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1020 } 1021 i = end; 1022 } 1023 } 1024 // a trailing row with content but no closing newline 1025 if (row.items.len > 0) { 1026 width = @max(width, row.items.len); 1027 try rows.append(allocator, row); 1028 } else { 1029 row.deinit(allocator); 1030 } 1031 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1032 1033 const height = rows.items.len; 1034 if (width == 0 or height == 0) return; 1035 1036 const clamped_w = @min(width, constraint.max_size.width orelse width); 1037 const clamped_h = @min(height, constraint.max_size.height orelse height); 1038 1039 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1040 errdefer grid.deinit(); 1041 for (rows.items[0..clamped_h], 0..) |r, y| { 1042 const n = @min(r.items.len, clamped_w); 1043 for (r.items[0..n], 0..) |cell, x| { 1044 if (cell.continuation) continue; 1045 (try grid.cell(x, y)).style = cell.style; 1046 try grid.setRune(x, y, cell.rune); 1047 } 1048 } 1049 self.grid = grid; 1050 } 1051 1052 pub fn clearGrid(self: *AnsiArt) void { 1053 if (self.grid) |*grid| { 1054 grid.deinit(); 1055 self.grid = null; 1056 } 1057 } 1058 1059 pub fn getGrid(self: AnsiArt) ?Grid { 1060 return self.grid; 1061 } 1062 1063 pub fn getFocus(self: *AnsiArt) *Focus { 1064 return self.focus; 1065 } 1066 1067 fn applySgr(style: *Grid.Style, params: []const u8) void { 1068 var nums: [16]u32 = undefined; 1069 var n: usize = 0; 1070 var it = std.mem.splitScalar(u8, params, ';'); 1071 while (it.next()) |tok| { 1072 if (n >= nums.len) break; 1073 // an empty parameter (e.g. bare "\x1b[m") means 0 1074 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1075 n += 1; 1076 } 1077 if (n == 0) { 1078 style.* = .{}; 1079 return; 1080 } 1081 var i: usize = 0; 1082 while (i < n) : (i += 1) { 1083 switch (nums[i]) { 1084 0 => style.* = .{}, 1085 7 => style.inverted = true, 1086 27 => style.inverted = false, 1087 39 => style.fg = null, 1088 49 => style.bg = null, 1089 38, 48 => { 1090 // truecolor form: 38;2;r;g;b — anything else is ignored 1091 if (i + 4 < n and nums[i + 1] == 2) { 1092 const c = Grid.Color{ 1093 .r = @truncate(nums[i + 2]), 1094 .g = @truncate(nums[i + 3]), 1095 .b = @truncate(nums[i + 4]), 1096 }; 1097 if (nums[i] == 38) style.fg = c else style.bg = c; 1098 i += 4; 1099 } 1100 }, 1101 else => {}, 1102 } 1103 } 1104 } 1105 }; 1106 1107 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1108 pub const AnsiBackground = struct { 1109 grid: ?Grid, 1110 child: *Widget, 1111 art: AnsiArt, 1112 session: *ui.Session, 1113 1114 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1115 var cw = child_widget; 1116 const child = allocator.create(Widget) catch |e| { 1117 cw.deinit(allocator); 1118 return e; 1119 }; 1120 child.* = cw; 1121 errdefer { 1122 child.deinit(allocator); 1123 allocator.destroy(child); 1124 } 1125 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1126 } 1127 1128 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1129 if (self.grid) |*grid| grid.deinit(); 1130 self.art.deinit(allocator); 1131 self.child.deinit(allocator); 1132 allocator.destroy(self.child); 1133 } 1134 1135 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1136 self.clearGrid(); 1137 1138 // make the wrapped page fill the whole available area 1139 try self.child.build(allocator, .{ 1140 .min_size = .{ 1141 .width = constraint.max_size.width orelse constraint.min_size.width, 1142 .height = constraint.max_size.height orelse constraint.min_size.height, 1143 }, 1144 .max_size = constraint.max_size, 1145 }, root_focus); 1146 1147 if (!self.session.data.enable_ansi) return; 1148 const foreground = self.child.getGrid() orelse return; 1149 self.art.content = self.session.data.ansi_art; 1150 try self.buildArt(allocator, foreground.size, root_focus); 1151 if (!self.session.is_terminal) return; 1152 const art_grid = self.art.getGrid() orelse return; 1153 self.grid = try artBehind(allocator, foreground, art_grid); 1154 } 1155 1156 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1157 try self.child.input(allocator, key, root_focus); 1158 } 1159 1160 pub fn clearGrid(self: *AnsiBackground) void { 1161 if (self.grid) |*grid| { 1162 grid.deinit(); 1163 self.grid = null; 1164 } 1165 self.child.clearGrid(); 1166 self.art.clearGrid(); 1167 } 1168 1169 pub fn getGrid(self: AnsiBackground) ?Grid { 1170 return self.grid orelse self.child.getGrid(); 1171 } 1172 1173 pub fn getFocus(self: *AnsiBackground) *Focus { 1174 return self.child.getFocus(); 1175 } 1176 1177 // a foreground cell counts as blank if it's empty or a bare space without styling 1178 fn cellIsBlank(cell: Grid.Cell) bool { 1179 if (cell.continuation) return false; 1180 if (cell.rune) |rune| { 1181 return rune == ' ' and 1182 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1183 } 1184 return true; 1185 } 1186 1187 // the art is dimmed to this fraction of its brightness so foreground text 1188 // stays legible over it 1189 const art_brightness = 35; // percent 1190 1191 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1192 const c = color orelse return null; 1193 return .{ 1194 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1195 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1196 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1197 }; 1198 } 1199 1200 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1201 try self.art.build(allocator, .{ 1202 .min_size = .{ .width = null, .height = null }, 1203 .max_size = .{ .width = size.width, .height = size.height }, 1204 }, root_focus); 1205 if (self.art.grid) |*grid| { 1206 for (grid.cells) |*cell| { 1207 cell.style.fg = dimColor(cell.style.fg); 1208 cell.style.bg = dimColor(cell.style.bg); 1209 } 1210 } 1211 } 1212 1213 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1214 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1215 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1216 // snap such channels clear of that range so the backdrop doesn't blink or 1217 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1218 // actually render truecolor. 1219 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1220 const c = color orelse return null; 1221 const snap = struct { 1222 fn f(v: u8) u8 { 1223 return if (v >= 1 and v <= 9) 10 else v; 1224 } 1225 }.f; 1226 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1227 } 1228 1229 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1230 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1231 dst.style.bg = background_maybe; 1232 // change unstyled text to contrast with the art behind it 1233 if (dst.style.fg == null) { 1234 if (background_maybe) |background| { 1235 // use near-black and near-white, which is easier on the eyes 1236 // while retaining high contrast 1237 const luminance = (@as(u32, background.r) * 299 + 1238 @as(u32, background.g) * 587 + 1239 @as(u32, background.b) * 114) / 1000; 1240 dst.style.fg = if (luminance >= 128) 1241 .{ .r = 16, .g = 16, .b = 16 } 1242 else 1243 .{ .r = 240, .g = 240, .b = 240 }; 1244 } 1245 } 1246 } 1247 1248 // composites ANSI art behind a foreground grid 1249 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1250 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1251 errdefer out.deinit(); 1252 1253 const anchor_x = foreground.size.width -| art_grid.size.width; 1254 for (0..art_grid.size.height) |y| { 1255 for (0..art_grid.size.width) |x| { 1256 const src = (try art_grid.cell(x, y)).*; 1257 if (src.rune == null) continue; 1258 const dst = try out.cell(anchor_x + x, y); 1259 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1260 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1261 // blank cells take the art; occupied cells take its background 1262 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1263 dst.style = src.style; 1264 dst.style.fg = sgrSafe(dst.style.fg); 1265 dst.style.bg = sgrSafe(dst.style.bg); 1266 try out.setRune(anchor_x + x, y, src.rune); 1267 } else { 1268 applyArtBackground(dst, src); 1269 if (next_dst) |next| applyArtBackground(next, src); 1270 } 1271 } 1272 } 1273 return out; 1274 } 1275 }; 1276 1277 pub const CopyableText = struct { 1278 pub const Choice = struct { 1279 selector: []const u8 = "", 1280 text: []const u8, 1281 copyable_text: ?[]const u8 = null, 1282 label: []const u8 = "", 1283 bottom_label: []const u8 = "", 1284 }; 1285 1286 box: wgt.Box(Widget), 1287 session: *ui.Session, 1288 choices: []Choice, 1289 selected: usize = 0, 1290 1291 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1292 if (choices.len == 0) return error.MissingCopyableText; 1293 1294 const owned_choices = try allocator.dupe(Choice, choices); 1295 errdefer allocator.free(owned_choices); 1296 1297 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1298 errdefer box.deinit(allocator); 1299 1300 if (choices.len > 1) for (choices) |choice| { 1301 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 1302 errdefer selector_box.deinit(allocator); 1303 selector_box.getFocus().mode = .all; 1304 try box.children.put(allocator, selector_box.getFocus().id, .{ .widget = .{ .text_box = selector_box }, .rect = null, .min_size = .{ .width = choice.selector.len + 2, .height = 3 } }); 1305 }; 1306 1307 var text_input = try wgt.TextInput.init(allocator, .{ 1308 .border_style = .single, 1309 .rounded_corners = true, 1310 .label = choices[0].label, 1311 .bottom_label = choices[0].bottom_label, 1312 .read_only = true, 1313 .render_content = session.is_terminal, 1314 .visible_width = null, 1315 }); 1316 errdefer text_input.deinit(allocator); 1317 text_input.getFocus().mode = .all; 1318 try text_input.setContent(allocator, choices[0].text); 1319 box.getFocus().child_id = text_input.getFocus().id; 1320 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1321 1322 return .{ .box = box, .session = session, .choices = owned_choices }; 1323 } 1324 1325 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1326 const aa = session.page_arena.allocator(); 1327 var choices: [2]Choice = undefined; 1328 var count: usize = 0; 1329 switch (location) { 1330 .repo => |identity| { 1331 if (session.data.git_http_port) |port| { 1332 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1333 choices[count] = .{ 1334 .selector = "http", 1335 .text = url, 1336 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1337 .label = " clone ", 1338 }; 1339 count += 1; 1340 } 1341 if (session.data.git_ssh_port) |port| { 1342 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1343 choices[count] = .{ 1344 .selector = "ssh", 1345 .text = url, 1346 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1347 .label = " clone ", 1348 }; 1349 count += 1; 1350 } 1351 }, 1352 .fork => |f| if (session.data.git_ssh_port) |port| { 1353 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1354 choices[count] = .{ 1355 .text = url, 1356 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1357 .label = " clone ", 1358 }; 1359 count += 1; 1360 }, 1361 } 1362 if (count == 0) return null; 1363 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1364 } 1365 1366 pub fn minWidth(self: *const CopyableText) usize { 1367 var width = maxWidth(self.choices) + 2; 1368 if (self.choices.len > 1) { 1369 for (self.choices) |choice| width += choice.selector.len + 2; 1370 } 1371 return width; 1372 } 1373 1374 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1375 self.box.deinit(allocator); 1376 allocator.free(self.choices); 1377 } 1378 1379 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1380 self.clearGrid(); 1381 1382 if (self.choices.len > 1) { 1383 if (root_focus.grandchild_id) |id| { 1384 for (0..self.choices.len) |i| { 1385 if (id != self.selector(i).getFocus().id) continue; 1386 if (i != self.selected) try self.select(allocator, i); 1387 root_focus.setFocus(self.textInput().getFocus().id); 1388 break; 1389 } 1390 } 1391 1392 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1393 for (0..self.choices.len) |i| { 1394 self.selector(i).options.border_style = if (i == self.selected) 1395 (if (focused) .double else .single) 1396 else 1397 .hidden; 1398 } 1399 } 1400 self.textInput().options.border_style = .single; 1401 self.textInput().cursor = 0; 1402 self.textInput().scroll_offset = 0; 1403 try self.box.build(allocator, constraint, root_focus); 1404 1405 if (!self.session.is_terminal) { 1406 const aa = self.session.arena.allocator(); 1407 const id = self.textInput().getFocus().id; 1408 try self.session.text_inputs.put(aa, id, self.textInput()); 1409 } 1410 } 1411 1412 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1413 switch (key) { 1414 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1415 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1416 .enter => if (self.session.is_terminal) self.show(), 1417 .mouse => |mouse| { 1418 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1419 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1420 try self.select(allocator, i); 1421 root_focus.setFocus(self.textInput().getFocus().id); 1422 return; 1423 }; 1424 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1425 }, 1426 else => {}, 1427 } 1428 } 1429 1430 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1431 self.selected = selected; 1432 const choice = self.choices[selected]; 1433 const text_input = self.textInput(); 1434 text_input.options.label = choice.label; 1435 text_input.options.bottom_label = choice.bottom_label; 1436 try text_input.setContent(allocator, choice.text); 1437 } 1438 1439 fn show(self: *CopyableText) void { 1440 const choice = self.choices[self.selected]; 1441 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1442 } 1443 1444 fn textInput(self: *CopyableText) *wgt.TextInput { 1445 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1446 } 1447 1448 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1449 return &self.box.children.values()[index].widget.text_box; 1450 } 1451 1452 fn maxWidth(choices: []const Choice) usize { 1453 var width: usize = 0; 1454 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1455 return width; 1456 } 1457 1458 pub fn clearGrid(self: *CopyableText) void { 1459 self.box.clearGrid(); 1460 } 1461 1462 pub fn getGrid(self: CopyableText) ?Grid { 1463 return self.box.getGrid(); 1464 } 1465 1466 pub fn getFocus(self: *CopyableText) *Focus { 1467 return self.box.getFocus(); 1468 } 1469 }; 1470