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