╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("../ui.zig"); 3 const xit = @import("xit"); 4 const xitui = xit.xitui; 5 const wgt = xitui.widget; 6 const layout = xitui.layout; 7 const Key = xitui.input.Key; 8 const Grid = xitui.grid.Grid; 9 const Focus = xitui.focus.Focus; 10 const inp = @import("input.zig"); 11 12 pub const thread = @import("widget/thread.zig"); 13 14 pub const Widget = union(enum) { 15 text: wgt.Text(Widget), 16 box: wgt.Box(Widget), 17 text_box: wgt.TextBox(Widget), 18 text_input: wgt.TextInput(Widget), 19 scroll: wgt.Scroll(Widget), 20 stack: wgt.Stack(Widget), 21 flow_box: FlowBox, 22 flow_box_scroll: FlowBox.Scroll, 23 tag_flow: TagFlow, 24 spacer: Spacer, 25 center: Center, 26 section_label: SectionLabel, 27 submit_button: SubmitButton, 28 background: AnsiBackground, 29 home: ui.Home.View, 30 user: ui.User.View, 31 repo: ui.Repo.View, 32 quit: ui.Quit.View, 33 unauthorized: ui.Unauthorized.View, 34 title: ui.Title.View, 35 sub_title: ui.SubTitle.View, 36 home_header: ui.Home.Header.View, 37 user_header: ui.User.Header.View, 38 repo_header: ui.Repo.Header.View, 39 repo_files_header: ui.Repo.Files.Header.View, 40 repo_commits_header: ui.Repo.Commits.Header.View, 41 repo_issues_header: ui.Repo.Issues.Header, 42 repo_patches_header: ui.Repo.Patches.Header, 43 repo_discussions_header: ui.Repo.Discussions.Header, 44 repo_files: ui.Repo.Files.View, 45 repo_commits: ui.Repo.Commits.View, 46 repo_refs: ui.Repo.Refs.View, 47 repo_issues: ui.Repo.Issues.View, 48 repo_patches: ui.Repo.Patches.View, 49 repo_discussions: ui.Repo.Discussions.View, 50 repo_events_header: ui.Repo.Events.Header.View, 51 repo_events: ui.Repo.Events.View, 52 repo_comment: ui.Repo.Comment.Item, 53 copyable_text: CopyableText, 54 home_users: ui.Home.Users.View, 55 home_repos: ui.Home.Repos.View, 56 auth_tab: ui.Home.Header.AuthTab.View, 57 home_settings: ui.Home.Settings.View, 58 home_auth: ui.Home.Auth.View, 59 footer: Footer, 60 61 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 62 switch (self.*) { 63 inline else => |*case| case.deinit(allocator), 64 } 65 } 66 67 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 68 switch (self.*) { 69 inline else => |*case| try case.build(allocator, constraint, root_focus), 70 } 71 } 72 73 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 74 switch (self.*) { 75 inline else => |*case| try case.input(allocator, key, root_focus), 76 } 77 } 78 79 pub fn clearGrid(self: *Widget) void { 80 switch (self.*) { 81 inline else => |*case| case.clearGrid(), 82 } 83 } 84 85 pub fn getGrid(self: Widget) ?Grid { 86 switch (self) { 87 inline else => |*case| return case.getGrid(), 88 } 89 } 90 91 pub fn getFocus(self: *Widget) *Focus { 92 switch (self.*) { 93 inline else => |*case| return case.getFocus(), 94 } 95 } 96 }; 97 98 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 99 // `scroll` to keep it visible. 100 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 101 const keys = box.children.keys(); 102 if (keys.len == 0) return; 103 const cur_id = box.getFocus().child_id orelse return; 104 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 105 const last: isize = @intCast(keys.len - 1); 106 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 107 if (next == @as(usize, @intCast(cur))) return; 108 root_focus.setFocus(keys[next]); 109 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 110 } 111 112 pub const FlowBox = struct { 113 focus: *Focus, 114 grid: ?Grid, 115 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 116 // backs each link item's `.custom` focus kind (e.g. "a:/user/foo"). reset 117 // wholesale on each setItems so there's no per-string ownership to track. 118 arena: std.heap.ArenaAllocator, 119 // column count from the last build — FlowBox.Scroll.input uses it so arrow 120 // up/down can step by a row's worth of items. 121 last_cols: usize, 122 options: Options, 123 124 const border_rows: usize = 2; 125 126 pub const Options = struct { 127 cell_width: usize = 40, 128 cell_height: usize = 3, 129 }; 130 131 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 132 return .{ 133 .focus = try Focus.create(allocator, .container), 134 .grid = null, 135 .text_boxes = .empty, 136 .arena = std.heap.ArenaAllocator.init(allocator), 137 .last_cols = 1, 138 .options = options, 139 }; 140 } 141 142 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 143 self.focus.destroy(allocator); 144 if (self.grid) |*grid| { 145 grid.deinit(); 146 self.grid = null; 147 } 148 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 149 self.text_boxes.deinit(allocator); 150 self.arena.deinit(); 151 } 152 153 // one flow item: its display text plus an optional link. a non-empty `link` 154 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 155 // the web renderer turns into an anchor. 156 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 157 158 // the text box copies the item text; links are copied into the arena, so the 159 // caller's slices needn't outlive this call. 160 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 161 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 162 self.text_boxes.clearAndFree(allocator); 163 164 // the old text boxes and their borrowed links are gone now, so the link 165 // arena can be reclaimed. 166 _ = self.arena.reset(.retain_capacity); 167 const aa = self.arena.allocator(); 168 169 self.focus.clear(); 170 self.focus.child_id = null; 171 172 for (items) |item| { 173 var text_box = try wgt.TextBox(Widget).init(allocator, item.text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 174 errdefer text_box.deinit(allocator); 175 text_box.getFocus().focusable = true; 176 177 if (item.link.len > 0) { 178 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 179 } 180 181 try self.text_boxes.append(allocator, text_box); 182 } 183 184 if (self.text_boxes.items.len > 0) { 185 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 186 } 187 } 188 189 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 190 self.clearGrid(); 191 self.focus.clear(); 192 193 const cell_width = self.options.cell_width; 194 const max_width = constraint.max_size.width orelse cell_width; 195 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 196 self.last_cols = cols; 197 198 const count = self.text_boxes.items.len; 199 if (count == 0) return; 200 const slot_height = self.options.cell_height + border_rows; 201 if (slot_height == 0) return; 202 203 // build at the slot size so every text box fits a single grid cell 204 for (self.text_boxes.items) |*tb| { 205 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 206 try tb.build(allocator, .{ 207 .min_size = .{ .width = cell_width, .height = null }, 208 .max_size = .{ .width = cell_width, .height = slot_height }, 209 }, root_focus); 210 } 211 212 const rows = (count + cols - 1) / cols; 213 const content_height = rows * slot_height; 214 const total_width = cols * cell_width; 215 if (total_width == 0 or content_height == 0) return; 216 217 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 218 errdefer grid.deinit(); 219 220 for (self.text_boxes.items, 0..) |*tb, i| { 221 const tb_grid = tb.getGrid() orelse continue; 222 const col = i % cols; 223 const row = i / cols; 224 const cell_x = col * cell_width; 225 const cell_y = row * slot_height; 226 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 227 try grid.drawGrid(tb_grid, cell_x, cell_y); 228 } 229 230 self.grid = grid; 231 } 232 233 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 234 _ = self; 235 _ = allocator; 236 _ = key; 237 _ = root_focus; 238 } 239 240 pub fn clearGrid(self: *FlowBox) void { 241 if (self.grid) |*grid| { 242 grid.deinit(); 243 self.grid = null; 244 } 245 for (self.text_boxes.items) |*tb| tb.clearGrid(); 246 } 247 248 pub fn getGrid(self: FlowBox) ?Grid { 249 return self.grid; 250 } 251 252 pub fn getFocus(self: *FlowBox) *Focus { 253 return self.focus; 254 } 255 256 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 257 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 258 const slot_height = self.options.cell_height + border_rows; 259 const col = index % self.last_cols; 260 const row = index / self.last_cols; 261 return .{ 262 .x = @intCast(col * self.options.cell_width), 263 .y = @intCast(row * slot_height), 264 .size = .{ .width = self.options.cell_width, .height = slot_height }, 265 }; 266 } 267 268 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 269 for (self.text_boxes.items, 0..) |tb, i| { 270 if (tb.box.focus.id == focus_id) return i; 271 } 272 return null; 273 } 274 275 pub const Scroll = struct { 276 scroll: wgt.Scroll(Widget), 277 278 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 279 var layout_inner = try FlowBox.init(allocator, options); 280 errdefer layout_inner.deinit(allocator); 281 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 282 errdefer scroll.deinit(allocator); 283 return .{ .scroll = scroll }; 284 } 285 286 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 287 self.scroll.deinit(allocator); 288 } 289 290 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 291 self.scroll.x = 0; 292 self.scroll.y = 0; 293 try self.inner().setItems(allocator, items); 294 } 295 296 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 297 try self.scroll.build(allocator, constraint, root_focus); 298 } 299 300 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 301 _ = allocator; 302 const in = self.inner(); 303 const child_id = in.focus.child_id orelse return; 304 const current_index = in.indexOfFocusId(child_id) orelse return; 305 const count = in.text_boxes.items.len; 306 if (count == 0) return; 307 const cols = in.last_cols; 308 const slot_height = in.options.cell_height + FlowBox.border_rows; 309 310 var index = current_index; 311 switch (key) { 312 .arrow_up => index -|= cols, 313 .arrow_down => if (index + cols < count) { 314 index += cols; 315 }, 316 .arrow_left => index -|= 1, 317 .arrow_right => if (index + 1 < count) { 318 index += 1; 319 }, 320 .home => index = 0, 321 .end => index = count - 1, 322 .page_up => { 323 if (self.scroll.grid) |grid| if (slot_height > 0) { 324 const rows_per_page = grid.size.height / slot_height; 325 index -|= rows_per_page * cols; 326 }; 327 }, 328 .page_down => { 329 if (self.scroll.grid) |grid| if (slot_height > 0) { 330 const rows_per_page = grid.size.height / slot_height; 331 index = @min(index + rows_per_page * cols, count - 1); 332 }; 333 }, 334 // scroll wheel moves the focused cell by a full row so the 335 // viewport (via scrollToRect) follows in row-sized steps, 336 // matching how a scroll wheel feels in a grid view. 337 .mouse => |mouse| switch (mouse.action) { 338 .scroll => |dir| switch (dir) { 339 .up => index -|= cols, 340 .down => if (index + cols < count) { 341 index += cols; 342 }, 343 }, 344 else => {}, 345 }, 346 else => {}, 347 } 348 349 if (index != current_index) { 350 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 351 if (in.cellRect(index)) |rect| { 352 self.scroll.scrollToRect(rect); 353 } 354 } 355 } 356 357 pub fn clearGrid(self: *Scroll) void { 358 self.scroll.clearGrid(); 359 } 360 361 pub fn getGrid(self: Scroll) ?Grid { 362 return self.scroll.getGrid(); 363 } 364 365 pub fn getFocus(self: *Scroll) *Focus { 366 return self.scroll.getFocus(); 367 } 368 369 pub fn getSelectedIndex(self: Scroll) ?usize { 370 const in = self.scroll.child.flow_box; 371 const child_id = in.focus.child_id orelse return null; 372 return in.indexOfFocusId(child_id); 373 } 374 375 fn inner(self: *Scroll) *FlowBox { 376 return &self.scroll.child.flow_box; 377 } 378 }; 379 }; 380 381 // a left-to-right flow of variable-width focusable items that wraps at the 382 // available width, like words in a paragraph. unlike FlowBox there is no cell 383 // grid: each item is as wide as its text. selection movement is driven by the 384 // owning view via indexOfFocusId/verticalNeighbor. 385 pub const TagFlow = struct { 386 focus: *Focus, 387 grid: ?Grid, 388 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 389 // last build's item rects (content space), for vertical navigation and 390 // scroll-into-view. 391 rects: std.ArrayList(layout.IRect), 392 // backs each link item's `.custom` focus kind. 393 arena: std.heap.ArenaAllocator, 394 395 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 396 397 pub fn init(allocator: std.mem.Allocator) !TagFlow { 398 return .{ 399 .focus = try Focus.create(allocator, .container), 400 .grid = null, 401 .text_boxes = .empty, 402 .rects = .empty, 403 .arena = std.heap.ArenaAllocator.init(allocator), 404 }; 405 } 406 407 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 408 self.focus.destroy(allocator); 409 if (self.grid) |*grid| { 410 grid.deinit(); 411 self.grid = null; 412 } 413 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 414 self.text_boxes.deinit(allocator); 415 self.rects.deinit(allocator); 416 self.arena.deinit(); 417 } 418 419 // the text box copies the item text; links are copied into the arena, so the 420 // caller's slices needn't outlive this call. 421 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 422 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 423 self.text_boxes.clearAndFree(allocator); 424 425 _ = self.arena.reset(.retain_capacity); 426 const aa = self.arena.allocator(); 427 428 self.focus.clear(); 429 self.focus.child_id = null; 430 431 for (items) |item| { 432 var text_box = try wgt.TextBox(Widget).init(allocator, item.text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 433 errdefer text_box.deinit(allocator); 434 text_box.getFocus().focusable = true; 435 436 if (item.link.len > 0) { 437 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 438 } 439 440 try self.text_boxes.append(allocator, text_box); 441 } 442 443 if (self.text_boxes.items.len > 0) { 444 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 445 } 446 } 447 448 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 449 self.clearGrid(); 450 self.focus.clear(); 451 452 if (self.text_boxes.items.len == 0) return; 453 const max_width = constraint.max_size.width orelse 80; 454 if (max_width == 0) return; 455 456 // lay the items out left to right, wrapping to a new row when the next 457 // one wouldn't fit. 458 self.rects.clearRetainingCapacity(); 459 var x: usize = 0; 460 var y: usize = 0; 461 var row_height: usize = 0; 462 var total_width: usize = 0; 463 for (self.text_boxes.items) |*tb| { 464 try tb.build(allocator, .{ 465 .min_size = .{ .width = null, .height = null }, 466 .max_size = .{ .width = max_width, .height = null }, 467 }, root_focus); 468 const tb_grid_maybe = tb.getGrid(); 469 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 470 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 471 if (x > 0 and x + w > max_width) { 472 x = 0; 473 y += row_height; 474 row_height = 0; 475 } 476 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 477 if (h > row_height) row_height = h; 478 x += w; 479 if (x > total_width) total_width = x; 480 } 481 482 const total_height = y + row_height; 483 if (total_width == 0 or total_height == 0) return; 484 485 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 486 errdefer grid.deinit(); 487 488 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 489 const tb_grid = tb.getGrid() orelse continue; 490 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 491 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 492 } 493 494 self.grid = grid; 495 } 496 497 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 498 _ = self; 499 _ = allocator; 500 _ = key; 501 _ = root_focus; 502 } 503 504 pub fn clearGrid(self: *TagFlow) void { 505 if (self.grid) |*grid| { 506 grid.deinit(); 507 self.grid = null; 508 } 509 for (self.text_boxes.items) |*tb| tb.clearGrid(); 510 } 511 512 pub fn getGrid(self: TagFlow) ?Grid { 513 return self.grid; 514 } 515 516 pub fn getFocus(self: *TagFlow) *Focus { 517 return self.focus; 518 } 519 520 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 521 for (self.text_boxes.items, 0..) |tb, i| { 522 if (tb.box.focus.id == focus_id) return i; 523 } 524 return null; 525 } 526 527 // the first item of the row below (when `downward`) or above, or null at 528 // the flow's edge. items are laid out in order, so y is nondecreasing. 529 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 530 const rects = self.rects.items; 531 if (index >= rects.len) return null; 532 const cur_y = rects[index].y; 533 if (downward) { 534 for (rects[index + 1 ..], index + 1..) |r, i| { 535 if (r.y > cur_y) return i; 536 } 537 return null; 538 } 539 // walk the earlier rows; the last one visited is the row above. 540 var prev_first: ?usize = null; 541 var i: usize = 0; 542 while (i < rects.len and rects[i].y < cur_y) { 543 prev_first = i; 544 const y = rects[i].y; 545 while (i < rects.len and rects[i].y == y) i += 1; 546 } 547 return prev_first; 548 } 549 }; 550 551 // an invisible widget that fills the horizontal space granted by its parent. 552 // used inside Box(horiz) with a min_size so the box reserves space for the 553 // children that follow, pushing them to the right. 554 pub const Spacer = struct { 555 focus: *Focus, 556 grid: ?Grid, 557 558 pub fn init(allocator: std.mem.Allocator) !Spacer { 559 return .{ 560 .focus = try Focus.create(allocator, .container), 561 .grid = null, 562 }; 563 } 564 565 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 566 self.focus.destroy(allocator); 567 if (self.grid) |*grid| { 568 grid.deinit(); 569 self.grid = null; 570 } 571 } 572 573 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 574 _ = root_focus; 575 self.clearGrid(); 576 const width = constraint.max_size.width orelse return; 577 if (width == 0) return; 578 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 579 } 580 581 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 582 _ = self; 583 _ = allocator; 584 _ = key; 585 _ = root_focus; 586 } 587 588 pub fn clearGrid(self: *Spacer) void { 589 if (self.grid) |*grid| { 590 grid.deinit(); 591 self.grid = null; 592 } 593 } 594 595 pub fn getGrid(self: Spacer) ?Grid { 596 return self.grid; 597 } 598 599 pub fn getFocus(self: *Spacer) *Focus { 600 return self.focus; 601 } 602 }; 603 604 // a one-row, non-focusable status bar showing the current page's url 605 pub const Footer = struct { 606 focus: *Focus, 607 grid: ?Grid, 608 session: *ui.Session, 609 arena: std.heap.ArenaAllocator, 610 url: []const u8 = "", 611 url_width: usize = 0, 612 613 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 614 return .{ 615 .focus = try Focus.create(allocator, .container), 616 .grid = null, 617 .session = session, 618 .arena = std.heap.ArenaAllocator.init(allocator), 619 }; 620 } 621 622 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 623 self.focus.destroy(allocator); 624 if (self.grid) |*grid| grid.deinit(); 625 self.arena.deinit(); 626 } 627 628 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 629 _ = root_focus; 630 self.clearGrid(); 631 const width = constraint.max_size.width orelse return; 632 if (width == 0) return; 633 634 _ = self.arena.reset(.retain_capacity); 635 self.url = ""; 636 self.url_width = 0; 637 const aa = self.arena.allocator(); 638 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 639 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 640 self.url = text; 641 642 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 643 errdefer grid.deinit(); 644 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 645 var i: usize = 0; 646 while (utf8.nextCodepointSlice()) |ch| { 647 if (i == width) break; 648 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 649 i += 1; 650 } 651 self.url_width = i; 652 self.grid = grid; 653 } 654 655 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 656 _ = allocator; 657 switch (key) { 658 .mouse => |mouse| { 659 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 660 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 661 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 662 }, 663 else => {}, 664 } 665 } 666 667 pub fn clearGrid(self: *Footer) void { 668 if (self.grid) |*grid| { 669 grid.deinit(); 670 self.grid = null; 671 } 672 } 673 674 pub fn getGrid(self: Footer) ?Grid { 675 return self.grid; 676 } 677 678 pub fn getFocus(self: *Footer) *Focus { 679 return self.focus; 680 } 681 }; 682 683 // a single-child wrapper that builds the child at its natural size and 684 // positions its grid in the middle of the area granted by the parent 685 // a bordered label, indented past the content it precedes 686 pub const SectionLabel = struct { 687 box: wgt.Box(Widget), 688 689 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 690 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 691 errdefer box.deinit(allocator); 692 693 { 694 var gap = try wgt.Text(Widget).init(allocator, " "); 695 errdefer gap.deinit(allocator); 696 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 697 } 698 699 { 700 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 701 errdefer tb.deinit(allocator); 702 tb.getFocus().focusable = true; 703 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 704 } 705 706 box.getFocus().child_id = box.children.keys()[1]; 707 return .{ .box = box }; 708 } 709 710 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 711 self.box.deinit(allocator); 712 } 713 714 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 715 try self.box.build(allocator, constraint, root_focus); 716 } 717 718 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 719 _ = self; 720 _ = allocator; 721 _ = key; 722 _ = root_focus; 723 } 724 725 pub fn clearGrid(self: *SectionLabel) void { 726 self.box.clearGrid(); 727 } 728 729 pub fn getGrid(self: SectionLabel) ?Grid { 730 return self.box.getGrid(); 731 } 732 733 pub fn getFocus(self: *SectionLabel) *Focus { 734 return self.box.getFocus(); 735 } 736 }; 737 738 // a submit button, indented past its form's content. the web overlay POSTs 739 // the button's form; terminal hosts detect presses through `buttonId`. 740 pub const SubmitButton = struct { 741 box: wgt.Box(Widget), 742 743 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 744 return initLabeled(allocator, "submit"); 745 } 746 747 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 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(Widget).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 button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 759 errdefer button.deinit(allocator); 760 button.getFocus().focusable = true; 761 // the renderer distinguishes plain clickables from buttons that 762 // should POST to a server route by this kind. 763 button.getFocus().kind = .{ .custom = "submit" }; 764 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 765 } 766 767 box.getFocus().child_id = box.children.keys()[1]; 768 return .{ .box = box }; 769 } 770 771 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 772 self.box.deinit(allocator); 773 } 774 775 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 776 try self.box.build(allocator, constraint, root_focus); 777 } 778 779 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 780 _ = self; 781 _ = allocator; 782 _ = key; 783 _ = root_focus; 784 } 785 786 pub fn clearGrid(self: *SubmitButton) void { 787 self.box.clearGrid(); 788 } 789 790 pub fn getGrid(self: SubmitButton) ?Grid { 791 return self.box.getGrid(); 792 } 793 794 pub fn getFocus(self: *SubmitButton) *Focus { 795 return self.box.getFocus(); 796 } 797 798 // the button's focus id, for click hit-testing 799 pub fn buttonId(self: *const SubmitButton) usize { 800 return self.box.children.keys()[1]; 801 } 802 }; 803 804 pub const Center = struct { 805 focus: *Focus, 806 grid: ?Grid, 807 child: *Widget, 808 809 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 810 const child = try allocator.create(Widget); 811 errdefer allocator.destroy(child); 812 child.* = child_widget; 813 return .{ 814 .focus = try Focus.create(allocator, .container), 815 .grid = null, 816 .child = child, 817 }; 818 } 819 820 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 821 self.focus.destroy(allocator); 822 if (self.grid) |*grid| { 823 grid.deinit(); 824 self.grid = null; 825 } 826 self.child.deinit(allocator); 827 allocator.destroy(self.child); 828 } 829 830 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 831 self.clearGrid(); 832 self.getFocus().clear(); 833 834 // build the child without forcing it to fill min_size; it sizes 835 // itself to its content within the available max. 836 try self.child.build(allocator, .{ 837 .min_size = .{ .width = null, .height = null }, 838 .max_size = constraint.max_size, 839 }, root_focus); 840 841 const child_grid = self.child.getGrid() orelse return; 842 843 // prefer max when given; otherwise grow to max(min, child) so that 844 // when only a min is set (e.g. the wasm path passes viewport rows 845 // as min_height with no max), we can still vertically center while 846 // letting taller content extend past the min. 847 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; 848 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; 849 if (width == 0 or height == 0) return; 850 851 const offset_x: usize = (width -| child_grid.size.width) / 2; 852 const offset_y: usize = (height -| child_grid.size.height) / 2; 853 854 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 855 errdefer grid.deinit(); 856 try grid.drawGrid(child_grid, offset_x, offset_y); 857 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 858 self.getFocus().child_id = self.child.getFocus().id; 859 860 self.grid = grid; 861 } 862 863 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 864 try self.child.input(allocator, key, root_focus); 865 } 866 867 pub fn clearGrid(self: *Center) void { 868 if (self.grid) |*grid| { 869 grid.deinit(); 870 self.grid = null; 871 } 872 self.child.clearGrid(); 873 } 874 875 pub fn getGrid(self: Center) ?Grid { 876 return self.grid; 877 } 878 879 pub fn getFocus(self: *Center) *Focus { 880 return self.focus; 881 } 882 }; 883 884 // renders truecolor ANSI art 885 pub const AnsiArt = struct { 886 focus: *Focus, 887 grid: ?Grid, 888 content: []const u8, 889 890 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 891 return .{ 892 .focus = try Focus.create(allocator, .container), 893 .grid = null, 894 .content = content, 895 }; 896 } 897 898 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 899 self.focus.destroy(allocator); 900 if (self.grid) |*grid| { 901 grid.deinit(); 902 self.grid = null; 903 } 904 } 905 906 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 907 _ = root_focus; 908 self.clearGrid(); 909 910 // parse into rows of cells, then copy into a rectangular grid 911 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 912 defer { 913 for (rows.items) |*row| row.deinit(allocator); 914 rows.deinit(allocator); 915 } 916 var row: std.ArrayList(Grid.Cell) = .empty; 917 errdefer row.deinit(allocator); 918 919 var style: Grid.Style = .{}; 920 var width: usize = 0; 921 const content = self.content; 922 var i: usize = 0; 923 while (i < content.len) { 924 const byte = content[i]; 925 if (byte == '\n') { 926 width = @max(width, row.items.len); 927 try rows.append(allocator, row); 928 row = .empty; 929 i += 1; 930 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 931 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 932 var j = i + 2; 933 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 934 if (j >= content.len) { 935 i = content.len; // malformed trailing escape; stop 936 } else { 937 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 938 i = j + 1; 939 } 940 } else { 941 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 942 const end = @min(content.len, i + len); 943 const rune = content[i..end]; 944 // a foreground color does not make a space visible. only an 945 // effective background makes it opaque; foreground state often 946 // remains active through the padding at the end of a row. 947 const effective_background = if (style.inverted) style.fg else style.bg; 948 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 949 try row.append(allocator, .{ 950 .rune = if (transparent) null else rune, 951 .style = style, 952 }); 953 i = end; 954 } 955 } 956 // a trailing row with content but no closing newline 957 if (row.items.len > 0) { 958 width = @max(width, row.items.len); 959 try rows.append(allocator, row); 960 } else { 961 row.deinit(allocator); 962 } 963 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 964 965 const height = rows.items.len; 966 if (width == 0 or height == 0) return; 967 968 const clamped_w = @min(width, constraint.max_size.width orelse width); 969 const clamped_h = @min(height, constraint.max_size.height orelse height); 970 971 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 972 errdefer grid.deinit(); 973 for (rows.items[0..clamped_h], 0..) |r, y| { 974 const n = @min(r.items.len, clamped_w); 975 for (r.items[0..n], 0..) |cell, x| { 976 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 977 } 978 } 979 self.grid = grid; 980 } 981 982 pub fn clearGrid(self: *AnsiArt) void { 983 if (self.grid) |*grid| { 984 grid.deinit(); 985 self.grid = null; 986 } 987 } 988 989 pub fn getGrid(self: AnsiArt) ?Grid { 990 return self.grid; 991 } 992 993 pub fn getFocus(self: *AnsiArt) *Focus { 994 return self.focus; 995 } 996 997 fn applySgr(style: *Grid.Style, params: []const u8) void { 998 var nums: [16]u32 = undefined; 999 var n: usize = 0; 1000 var it = std.mem.splitScalar(u8, params, ';'); 1001 while (it.next()) |tok| { 1002 if (n >= nums.len) break; 1003 // an empty parameter (e.g. bare "\x1b[m") means 0 1004 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1005 n += 1; 1006 } 1007 if (n == 0) { 1008 style.* = .{}; 1009 return; 1010 } 1011 var i: usize = 0; 1012 while (i < n) : (i += 1) { 1013 switch (nums[i]) { 1014 0 => style.* = .{}, 1015 7 => style.inverted = true, 1016 27 => style.inverted = false, 1017 39 => style.fg = null, 1018 49 => style.bg = null, 1019 38, 48 => { 1020 // truecolor form: 38;2;r;g;b — anything else is ignored 1021 if (i + 4 < n and nums[i + 1] == 2) { 1022 const c = Grid.Color{ 1023 .r = @truncate(nums[i + 2]), 1024 .g = @truncate(nums[i + 3]), 1025 .b = @truncate(nums[i + 4]), 1026 }; 1027 if (nums[i] == 38) style.fg = c else style.bg = c; 1028 i += 4; 1029 } 1030 }, 1031 else => {}, 1032 } 1033 } 1034 } 1035 }; 1036 1037 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1038 pub const AnsiBackground = struct { 1039 grid: ?Grid, 1040 child: *Widget, 1041 art: AnsiArt, 1042 session: *ui.Session, 1043 1044 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1045 var cw = child_widget; 1046 const child = allocator.create(Widget) catch |e| { 1047 cw.deinit(allocator); 1048 return e; 1049 }; 1050 child.* = cw; 1051 errdefer { 1052 child.deinit(allocator); 1053 allocator.destroy(child); 1054 } 1055 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1056 } 1057 1058 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1059 if (self.grid) |*grid| grid.deinit(); 1060 self.art.deinit(allocator); 1061 self.child.deinit(allocator); 1062 allocator.destroy(self.child); 1063 } 1064 1065 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1066 self.clearGrid(); 1067 1068 // make the wrapped page fill the whole available area 1069 try self.child.build(allocator, .{ 1070 .min_size = .{ 1071 .width = constraint.max_size.width orelse constraint.min_size.width, 1072 .height = constraint.max_size.height orelse constraint.min_size.height, 1073 }, 1074 .max_size = constraint.max_size, 1075 }, root_focus); 1076 1077 if (!self.session.data.enable_ansi) return; 1078 const foreground = self.child.getGrid() orelse return; 1079 self.art.content = self.session.data.ansi_art; 1080 try self.buildArt(allocator, foreground.size, root_focus); 1081 if (!self.session.is_terminal) return; 1082 const art_grid = self.art.getGrid() orelse return; 1083 self.grid = try artBehind(allocator, foreground, art_grid); 1084 } 1085 1086 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1087 try self.child.input(allocator, key, root_focus); 1088 } 1089 1090 pub fn clearGrid(self: *AnsiBackground) void { 1091 if (self.grid) |*grid| { 1092 grid.deinit(); 1093 self.grid = null; 1094 } 1095 self.child.clearGrid(); 1096 self.art.clearGrid(); 1097 } 1098 1099 pub fn getGrid(self: AnsiBackground) ?Grid { 1100 return self.grid orelse self.child.getGrid(); 1101 } 1102 1103 pub fn getFocus(self: *AnsiBackground) *Focus { 1104 return self.child.getFocus(); 1105 } 1106 1107 // a foreground cell counts as blank if it's empty or a bare space without styling 1108 fn cellIsBlank(cell: Grid.Cell) bool { 1109 if (cell.rune) |rune| { 1110 return std.mem.eql(u8, rune, " ") and 1111 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1112 } 1113 return true; 1114 } 1115 1116 // the art is dimmed to this fraction of its brightness so foreground text 1117 // stays legible over it 1118 const art_brightness = 35; // percent 1119 1120 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1121 const c = color orelse return null; 1122 return .{ 1123 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1124 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1125 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1126 }; 1127 } 1128 1129 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1130 try self.art.build(allocator, .{ 1131 .min_size = .{ .width = null, .height = null }, 1132 .max_size = .{ .width = size.width, .height = size.height }, 1133 }, root_focus); 1134 if (self.art.grid) |*grid| { 1135 for (grid.cells.items) |*cell| { 1136 cell.style.fg = dimColor(cell.style.fg); 1137 cell.style.bg = dimColor(cell.style.bg); 1138 } 1139 } 1140 } 1141 1142 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1143 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1144 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1145 // snap such channels clear of that range so the backdrop doesn't blink or 1146 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1147 // actually render truecolor. 1148 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1149 const c = color orelse return null; 1150 const snap = struct { 1151 fn f(v: u8) u8 { 1152 return if (v >= 1 and v <= 9) 10 else v; 1153 } 1154 }.f; 1155 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1156 } 1157 1158 // composites ANSI art behind a foreground grid 1159 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1160 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1161 errdefer out.deinit(); 1162 1163 const anchor_x = foreground.size.width -| art_grid.size.width; 1164 for (0..art_grid.size.height) |y| { 1165 for (0..art_grid.size.width) |x| { 1166 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1167 if (src.rune == null) continue; 1168 const idx = try out.cells.at(.{ y, anchor_x + x }); 1169 const dst = &out.cells.items[idx]; 1170 // blank cells take the art; occupied cells take its background 1171 if (cellIsBlank(dst.*)) { 1172 dst.* = src; 1173 dst.style.fg = sgrSafe(dst.style.fg); 1174 dst.style.bg = sgrSafe(dst.style.bg); 1175 } else { 1176 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1177 dst.style.bg = background_maybe; 1178 // change unstyled text to contrast with the art behind it 1179 if (dst.style.fg == null) { 1180 if (background_maybe) |background| { 1181 // use near-black and near-white, which is easier on the eyes 1182 // while retaining high contrast 1183 const luminance = (@as(u32, background.r) * 299 + 1184 @as(u32, background.g) * 587 + 1185 @as(u32, background.b) * 114) / 1000; 1186 dst.style.fg = if (luminance >= 128) 1187 .{ .r = 16, .g = 16, .b = 16 } 1188 else 1189 .{ .r = 240, .g = 240, .b = 240 }; 1190 } 1191 } 1192 } 1193 } 1194 } 1195 return out; 1196 } 1197 }; 1198 1199 pub const CopyableText = struct { 1200 pub const Choice = struct { 1201 selector: []const u8 = "", 1202 text: []const u8, 1203 copyable_text: ?[]const u8 = null, 1204 label: []const u8 = "", 1205 bottom_label: []const u8 = "", 1206 }; 1207 1208 box: wgt.Box(Widget), 1209 session: *ui.Session, 1210 choices: []Choice, 1211 selected: usize = 0, 1212 1213 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1214 if (choices.len == 0) return error.MissingCopyableText; 1215 1216 const owned_choices = try allocator.dupe(Choice, choices); 1217 errdefer allocator.free(owned_choices); 1218 1219 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1220 errdefer box.deinit(allocator); 1221 1222 if (choices.len > 1) for (choices) |choice| { 1223 var selector_box = try wgt.TextBox(Widget).init(allocator, choice.selector, .{ .border_style = .hidden, .wrap_kind = .none }); 1224 errdefer selector_box.deinit(allocator); 1225 selector_box.getFocus().focusable = true; 1226 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 } }); 1227 }; 1228 1229 var text_input = try wgt.TextInput(Widget).init(allocator, .{ 1230 .border_style = .single, 1231 .label = choices[0].label, 1232 .bottom_label = choices[0].bottom_label, 1233 .read_only = true, 1234 .render_content = session.is_terminal, 1235 .visible_width = null, 1236 }); 1237 errdefer text_input.deinit(allocator); 1238 text_input.getFocus().focusable = true; 1239 try text_input.setContent(allocator, choices[0].text); 1240 box.getFocus().child_id = text_input.getFocus().id; 1241 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1242 1243 return .{ .box = box, .session = session, .choices = owned_choices }; 1244 } 1245 1246 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, identity: []const u8) !CopyableText { 1247 const aa = session.page_arena.allocator(); 1248 var choices: [2]Choice = undefined; 1249 var count: usize = 0; 1250 if (session.data.git_http_port) |port| { 1251 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1252 choices[count] = .{ 1253 .selector = "http", 1254 .text = url, 1255 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1256 .label = " clone ", 1257 }; 1258 count += 1; 1259 } 1260 if (session.data.git_ssh_port) |port| { 1261 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1262 choices[count] = .{ 1263 .selector = "ssh", 1264 .text = url, 1265 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1266 .label = " clone ", 1267 }; 1268 count += 1; 1269 } 1270 if (count == 0) return error.MissingGitPort; 1271 return init(allocator, session, choices[0..count]); 1272 } 1273 1274 pub fn minWidth(self: *const CopyableText) usize { 1275 var width = maxWidth(self.choices) + 2; 1276 if (self.choices.len > 1) { 1277 for (self.choices) |choice| width += choice.selector.len + 2; 1278 } 1279 return width; 1280 } 1281 1282 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1283 self.box.deinit(allocator); 1284 allocator.free(self.choices); 1285 } 1286 1287 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1288 self.clearGrid(); 1289 1290 if (self.choices.len > 1) { 1291 if (root_focus.grandchild_id) |id| { 1292 for (0..self.choices.len) |i| { 1293 if (id != self.selector(i).getFocus().id) continue; 1294 if (i != self.selected) try self.select(allocator, i); 1295 root_focus.setFocus(self.textInput().getFocus().id); 1296 break; 1297 } 1298 } 1299 1300 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1301 for (0..self.choices.len) |i| { 1302 self.selector(i).options.border_style = if (i == self.selected) 1303 (if (focused) .double else .single) 1304 else 1305 .hidden; 1306 } 1307 } 1308 self.textInput().options.border_style = .single; 1309 self.textInput().cursor = 0; 1310 self.textInput().scroll_offset = 0; 1311 try self.box.build(allocator, constraint, root_focus); 1312 1313 if (!self.session.is_terminal) { 1314 const aa = self.session.arena.allocator(); 1315 const id = self.textInput().getFocus().id; 1316 try self.session.text_inputs.put(aa, id, self.textInput()); 1317 try self.session.input_values.put(aa, id, self.choices[self.selected].text); 1318 } 1319 } 1320 1321 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1322 switch (key) { 1323 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1324 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1325 .enter => if (self.session.is_terminal) self.show(), 1326 .mouse => |mouse| { 1327 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1328 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1329 try self.select(allocator, i); 1330 root_focus.setFocus(self.textInput().getFocus().id); 1331 return; 1332 }; 1333 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1334 }, 1335 else => {}, 1336 } 1337 } 1338 1339 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1340 self.selected = selected; 1341 const choice = self.choices[selected]; 1342 const text_input = self.textInput(); 1343 text_input.options.label = choice.label; 1344 text_input.options.bottom_label = choice.bottom_label; 1345 try text_input.setContent(allocator, choice.text); 1346 } 1347 1348 fn show(self: *CopyableText) void { 1349 const choice = self.choices[self.selected]; 1350 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1351 } 1352 1353 fn textInput(self: *CopyableText) *wgt.TextInput(Widget) { 1354 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1355 } 1356 1357 fn selector(self: *CopyableText, index: usize) *wgt.TextBox(Widget) { 1358 return &self.box.children.values()[index].widget.text_box; 1359 } 1360 1361 fn maxWidth(choices: []const Choice) usize { 1362 var width: usize = 0; 1363 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1364 return width; 1365 } 1366 1367 pub fn clearGrid(self: *CopyableText) void { 1368 self.box.clearGrid(); 1369 } 1370 1371 pub fn getGrid(self: CopyableText) ?Grid { 1372 return self.box.getGrid(); 1373 } 1374 1375 pub fn getFocus(self: *CopyableText) *Focus { 1376 return self.box.getFocus(); 1377 } 1378 }; 1379