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