╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
264 }}, 0); 265 } else null else null; 266 errdefer if (remote_maybe) |*remote| remote.deinit(allocator); 267 268 { 269 var text_widget: Widget = blk: { 270 var text_box = try wgt.TextBox.init(allocator, "no commits yet", .{ .border_style = null, .wrap_kind = .none }); 271 errdefer text_box.deinit(allocator); 272 break :blk if (remote_maybe != null) .{ .center = try Center.init(allocator, .{ .text_box = text_box }) } else .{ .text_box = text_box }; 273 }; 274 errdefer text_widget.deinit(allocator); 275 // over the remote widget, the text centers across its width 276 const size: ?layout.MaybeSize = if (remote_maybe) |*remote| .{ .width = remote.minWidth(), .height = 1 } else null; 277 try box.children.put(allocator, text_widget.getFocus().id, .{ .widget = text_widget, .rect = null, .min_size = size, .max_size = size }); 278 } 279 280 if (remote_maybe) |*remote| { 281 { 282 var gap = try wgt.Text.init(allocator, " "); 283 errdefer gap.deinit(allocator); 284 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 285 } 286 const min_width = remote.minWidth(); 287 box.getFocus().child_id = remote.getFocus().id; 288 try box.children.put(allocator, remote.getFocus().id, .{ .widget = .{ .copyable_text = remote.* }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 289 // the box owns it now 290 remote_maybe = null; 291 } 292 293 return Center.init(allocator, .{ .box = box }); 294 } 295 296 pub const FlowBox = struct { 297 focus: *Focus, 298 grid: ?Grid, 299 text_boxes: std.ArrayList(wgt.TextBox), 300 // backs each link item's `.custom` focus kind (e.g. "a:/user/foo"). reset 301 // wholesale on each setItems so there's no per-string ownership to track. 302 arena: std.heap.ArenaAllocator, 303 // column count from the last build — FlowBox.Scroll.input uses it so arrow 304 // up/down can step by a row's worth of items. 305 last_cols: usize, 306 options: Options, 307 308 const border_rows: usize = 2; 309 310 pub const Options = struct { 311 cell_width: usize = 40, 312 cell_height: usize = 3, 313 }; 314 315 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 316 return .{ 317 .focus = try Focus.create(allocator, .container), 318 .grid = null, 319 .text_boxes = .empty, 320 .arena = std.heap.ArenaAllocator.init(allocator), 321 .last_cols = 1, 322 .options = options, 323 }; 324 } 325 326 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 327 self.focus.destroy(allocator); 328 if (self.grid) |*grid| { 329 grid.deinit(); 330 self.grid = null; 331 } 332 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 333 self.text_boxes.deinit(allocator); 334 self.arena.deinit(); 335 } 336 337 // one flow item: its display text plus an optional link and bottom label. a 338 // non-empty `link` sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, 339 // which the web renderer turns into an anchor. 340 pub const Item = struct { text: []const u8, link: []const u8 = "", bottom_label: []const u8 = "" }; 341 342 // the text box copies the item text; links and bottom labels are copied into 343 // the arena, so the caller's slices needn't outlive this call. 344 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 345 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 346 self.text_boxes.clearAndFree(allocator); 347 348 // the old text boxes and their borrowed links are gone now, so the link 349 // arena can be reclaimed. 350 _ = self.arena.reset(.retain_capacity); 351 const aa = self.arena.allocator(); 352 353 self.focus.clear(); 354 self.focus.child_id = null; 355 356 for (items) |item| { 357 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = try aa.dupe(u8, item.bottom_label) }); 358 errdefer text_box.deinit(allocator); 359 text_box.getFocus().mode = .all; 360 361 if (item.link.len > 0) { 362 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 363 } 364 365 try self.text_boxes.append(allocator, text_box); 366 } 367 368 if (self.text_boxes.items.len > 0) { 369 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 370 } 371 } 372 373 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 374 self.clearGrid(); 375 self.focus.clear(); 376 377 const cell_width = self.options.cell_width; 378 const max_width = constraint.max_size.width orelse cell_width; 379 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 380 self.last_cols = cols; 381 382 const count = self.text_boxes.items.len; 383 if (count == 0) return; 384 const slot_height = self.options.cell_height + border_rows; 385 if (slot_height == 0) return; 386 387 // build at the slot size so every text box fits a single grid cell 388 for (self.text_boxes.items) |*tb| { 389 tb.options.border_style = if (root_focus.grandchild_id == tb.getFocus().id) .double else .hidden; 390 try tb.build(allocator, .{ 391 .min_size = .{ .width = cell_width, .height = null }, 392 .max_size = .{ .width = cell_width, .height = slot_height }, 393 }, root_focus); 394 } 395 396 const rows = (count + cols - 1) / cols; 397 const content_height = rows * slot_height; 398 const total_width = cols * cell_width; 399 if (total_width == 0 or content_height == 0) return; 400 401 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 402 errdefer grid.deinit(); 403 404 for (self.text_boxes.items, 0..) |*tb, i| { 405 const tb_grid = tb.getGrid() orelse continue; 406 const col = i % cols; 407 const row = i / cols; 408 const cell_x = col * cell_width; 409 const cell_y = row * slot_height; 410 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 411 try grid.drawGrid(tb_grid, cell_x, cell_y); 412 } 413 414 self.grid = grid; 415 } 416 417 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 418 _ = self; 419 _ = allocator; 420 _ = key; 421 _ = root_focus; 422 } 423 424 pub fn clearGrid(self: *FlowBox) void { 425 if (self.grid) |*grid| { 426 grid.deinit(); 427 self.grid = null; 428 } 429 for (self.text_boxes.items) |*tb| tb.clearGrid(); 430 } 431 432 pub fn getGrid(self: FlowBox) ?Grid { 433 return self.grid; 434 } 435 436 pub fn getFocus(self: *FlowBox) *Focus { 437 return self.focus; 438 } 439 440 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 441 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 442 const slot_height = self.options.cell_height + border_rows; 443 const col = index % self.last_cols; 444 const row = index / self.last_cols; 445 return .{ 446 .x = @intCast(col * self.options.cell_width), 447 .y = @intCast(row * slot_height), 448 .size = .{ .width = self.options.cell_width, .height = slot_height }, 449 }; 450 } 451 452 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 453 for (self.text_boxes.items, 0..) |tb, i| { 454 if (tb.focus.id == focus_id) return i; 455 } 456 return null; 457 } 458 459 pub const Scroll = struct { 460 scroll: wgt.Scroll(Widget), 461 462 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 463 var layout_inner = try FlowBox.init(allocator, options); 464 errdefer layout_inner.deinit(allocator); 465 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 466 errdefer scroll.deinit(allocator); 467 return .{ .scroll = scroll }; 468 } 469 470 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 471 self.scroll.deinit(allocator); 472 } 473 474 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 475 self.scroll.x = 0; 476 self.scroll.y = 0; 477 try self.inner().setItems(allocator, items); 478 } 479 480 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 481 try self.scroll.build(allocator, constraint, root_focus); 482 } 483 484 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 485 _ = allocator; 486 const in = self.inner(); 487 const child_id = in.focus.child_id orelse return; 488 const current_index = in.indexOfFocusId(child_id) orelse return; 489 const count = in.text_boxes.items.len; 490 if (count == 0) return; 491 const cols = in.last_cols; 492 const slot_height = in.options.cell_height + FlowBox.border_rows; 493 494 var index = current_index; 495 switch (key) { 496 .arrow_up => index -|= cols, 497 .arrow_down => if (index + cols < count) { 498 index += cols; 499 }, 500 .arrow_left => index -|= 1, 501 .arrow_right => if (index + 1 < count) { 502 index += 1; 503 }, 504 .home => index = 0, 505 .end => index = count - 1, 506 .page_up => { 507 if (self.scroll.grid) |grid| if (slot_height > 0) { 508 const rows_per_page = grid.size.height / slot_height; 509 index -|= rows_per_page * cols; 510 }; 511 }, 512 .page_down => { 513 if (self.scroll.grid) |grid| if (slot_height > 0) { 514 const rows_per_page = grid.size.height / slot_height; 515 index = @min(index + rows_per_page * cols, count - 1); 516 }; 517 }, 518 // scroll wheel moves the focused cell by a full row so the 519 // viewport (via scrollToRect) follows in row-sized steps, 520 // matching how a scroll wheel feels in a grid view. 521 .mouse => |mouse| switch (mouse.action) { 522 .scroll => |dir| switch (dir) { 523 .up => index -|= cols, 524 .down => if (index + cols < count) { 525 index += cols; 526 }, 527 }, 528 else => {}, 529 }, 530 else => {}, 531 } 532 533 if (index != current_index) { 534 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 535 if (in.cellRect(index)) |rect| { 536 self.scroll.scrollToRect(rect); 537 } 538 } 539 } 540 541 pub fn clearGrid(self: *Scroll) void { 542 self.scroll.clearGrid(); 543 } 544 545 pub fn getGrid(self: Scroll) ?Grid { 546 return self.scroll.getGrid(); 547 } 548 549 pub fn getFocus(self: *Scroll) *Focus { 550 return self.scroll.getFocus(); 551 } 552 553 pub fn isEmpty(self: Scroll) bool { 554 return self.scroll.child.flow_box.text_boxes.items.len == 0; 555 } 556 557 pub fn atTop(self: Scroll) bool { 558 const in = self.scroll.child.flow_box; 559 const child_id = in.focus.child_id orelse return false; 560 return in.indexOfFocusId(child_id) == 0; 561 } 562 563 fn inner(self: *Scroll) *FlowBox { 564 return &self.scroll.child.flow_box; 565 } 566 }; 567 }; 568 569 // a left-to-right flow of variable-width focusable items that wraps at the 570 // available width, like words in a paragraph. unlike FlowBox there is no cell 571 // grid: each item is as wide as its text. selection movement is driven by the 572 // owning view via indexOfFocusId/verticalNeighbor. 573 pub const WordFlow = struct { 574 focus: *Focus, 575 grid: ?Grid, 576 text_boxes: std.ArrayList(wgt.TextBox), 577 // last build's item rects (content space), for vertical navigation and 578 // scroll-into-view. 579 rects: std.ArrayList(layout.IRect), 580 // backs each link item's `.custom` focus kind. 581 arena: std.heap.ArenaAllocator, 582 583 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 584 585 pub fn init(allocator: std.mem.Allocator) !WordFlow { 586 return .{ 587 .focus = try Focus.create(allocator, .container), 588 .grid = null, 589 .text_boxes = .empty, 590 .rects = .empty, 591 .arena = std.heap.ArenaAllocator.init(allocator), 592 }; 593 } 594 595 pub fn deinit(self: *WordFlow, allocator: std.mem.Allocator) void { 596 self.focus.destroy(allocator); 597 if (self.grid) |*grid| { 598 grid.deinit(); 599 self.grid = null; 600 } 601 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 602 self.text_boxes.deinit(allocator); 603 self.rects.deinit(allocator); 604 self.arena.deinit(); 605 } 606 607 // the text box copies the item text; links are copied into the arena, so the 608 // caller's slices needn't outlive this call. 609 pub fn setItems(self: *WordFlow, allocator: std.mem.Allocator, items: []const Item) !void { 610 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 611 self.text_boxes.clearAndFree(allocator); 612 613 _ = self.arena.reset(.retain_capacity); 614 const aa = self.arena.allocator(); 615 616 self.focus.clear(); 617 self.focus.child_id = null; 618 619 for (items) |item| { 620 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 621 errdefer text_box.deinit(allocator); 622 text_box.getFocus().mode = .all; 623 624 if (item.link.len > 0) { 625 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 626 } 627 628 try self.text_boxes.append(allocator, text_box); 629 } 630 631 if (self.text_boxes.items.len > 0) { 632 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 633 } 634 } 635 636 pub fn build(self: *WordFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 637 self.clearGrid(); 638 self.focus.clear(); 639 640 if (self.text_boxes.items.len == 0) return; 641 const max_width = constraint.max_size.width orelse 80; 642 if (max_width == 0) return; 643 644 // lay the items out left to right, wrapping to a new row when the next 645 // one wouldn't fit. 646 self.rects.clearRetainingCapacity(); 647 var x: usize = 0; 648 var y: usize = 0; 649 var row_height: usize = 0; 650 var total_width: usize = 0; 651 for (self.text_boxes.items) |*tb| { 652 try tb.build(allocator, .{ 653 .min_size = .{ .width = null, .height = null }, 654 .max_size = .{ .width = max_width, .height = null }, 655 }, root_focus); 656 const tb_grid_maybe = tb.getGrid(); 657 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 658 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 659 if (x > 0 and x + w > max_width) { 660 x = 0; 661 y += row_height; 662 row_height = 0; 663 } 664 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 665 if (h > row_height) row_height = h; 666 x += w; 667 if (x > total_width) total_width = x; 668 } 669 670 const total_height = y + row_height; 671 if (total_width == 0 or total_height == 0) return; 672 673 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 674 errdefer grid.deinit(); 675 676 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 677 const tb_grid = tb.getGrid() orelse continue; 678 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 679 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 680 } 681 682 self.grid = grid; 683 } 684 685 pub fn input(self: *WordFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 686 _ = self; 687 _ = allocator; 688 _ = key; 689 _ = root_focus; 690 } 691 692 pub fn clearGrid(self: *WordFlow) void { 693 if (self.grid) |*grid| { 694 grid.deinit(); 695 self.grid = null; 696 } 697 for (self.text_boxes.items) |*tb| tb.clearGrid(); 698 } 699 700 pub fn getGrid(self: WordFlow) ?Grid { 701 return self.grid; 702 } 703 704 pub fn getFocus(self: *WordFlow) *Focus { 705 return self.focus; 706 } 707 708 pub fn indexOfFocusId(self: WordFlow, focus_id: usize) ?usize { 709 for (self.text_boxes.items, 0..) |tb, i| { 710 if (tb.focus.id == focus_id) return i; 711 } 712 return null; 713 } 714 715 // the first item of the row below (when `downward`) or above, or null at 716 // the flow's edge. items are laid out in order, so y is nondecreasing. 717 pub fn rowStep(self: WordFlow, index: usize, downward: bool) ?usize { 718 const rects = self.rects.items; 719 if (index >= rects.len) return null; 720 const cur_y = rects[index].y; 721 if (downward) { 722 for (rects[index + 1 ..], index + 1..) |r, i| { 723 if (r.y > cur_y) return i; 724 } 725 return null; 726 } 727 // walk the earlier rows; the last one visited is the row above. 728 var prev_first: ?usize = null; 729 var i: usize = 0; 730 while (i < rects.len and rects[i].y < cur_y) { 731 prev_first = i; 732 const y = rects[i].y; 733 while (i < rects.len and rects[i].y == y) i += 1; 734 } 735 return prev_first; 736 } 737 }; 738 739 // a word-wrapped paragraph of styled runs. each line a link touches is a 740 // focusable piece, and a focused link draws all its pieces inverted. 741 pub const RichText = struct { 742 focus: *Focus, 743 grid: ?Grid, 744 // backs the decoded content and the link targets 745 arena: std.heap.ArenaAllocator, 746 runes: []const u21, 747 // each rune's span 748 span_of: []const usize, 749 styles: []const Grid.Style, 750 // each span's link index, null outside a link 751 span_links: []const ?usize, 752 links: []const []const u8, 753 // each link's piece focuses in line order, kept across builds so a link 754 // keeps its ids when the text rewraps 755 link_focuses: []std.ArrayList(*Focus), 756 lines: std.ArrayList(wgt.Line), 757 // scratch for each build's link pieces 758 pieces: std.ArrayList(Piece), 759 760 pub const Span = struct { 761 text: []const u8, 762 style: Grid.Style = .{}, 763 // the focus kind a click or enter follows; empty for plain text 764 link: []const u8 = "", 765 }; 766 767 const Piece = struct { 768 link: usize, 769 rect: layout.URect, 770 }; 771 772 pub fn init(allocator: std.mem.Allocator, spans: []const Span) !RichText { 773 var arena = std.heap.ArenaAllocator.init(allocator); 774 errdefer arena.deinit(); 775 const aa = arena.allocator(); 776 777 var runes: std.ArrayList(u21) = .empty; 778 var span_of: std.ArrayList(usize) = .empty; 779 const styles = try aa.alloc(Grid.Style, spans.len); 780 const span_links = try aa.alloc(?usize, spans.len); 781 var links: std.ArrayList([]const u8) = .empty; 782 for (spans, 0..) |span, i| { 783 styles[i] = span.style; 784 span_links[i] = null; 785 if (span.link.len > 0) { 786 // adjacent spans with the same target are one link 787 const same = i > 0 and spans[i - 1].link.len > 0 and std.mem.eql(u8, spans[i - 1].link, span.link); 788 if (!same) try links.append(aa, try aa.dupe(u8, span.link)); 789 span_links[i] = links.items.len - 1; 790 } 791 var utf8 = (try std.unicode.Utf8View.init(span.text)).iterator(); 792 while (utf8.nextCodepoint()) |codepoint| { 793 try runes.append(aa, codepoint); 794 try span_of.append(aa, i); 795 } 796 } 797 798 const link_focuses = try aa.alloc(std.ArrayList(*Focus), links.items.len); 799 @memset(link_focuses, .empty); 800 801 return .{ 802 .focus = try Focus.create(allocator, .container), 803 .grid = null, 804 .arena = arena, 805 .runes = runes.items, 806 .span_of = span_of.items, 807 .styles = styles, 808 .span_links = span_links, 809 .links = links.items, 810 .link_focuses = link_focuses, 811 .lines = .empty, 812 .pieces = .empty, 813 }; 814 } 815 816 pub fn deinit(self: *RichText, allocator: std.mem.Allocator) void { 817 self.focus.destroy(allocator); 818 self.clearGrid(); 819 for (self.link_focuses) |*focuses| { 820 for (focuses.items) |focus| focus.destroy(allocator); 821 focuses.deinit(allocator); 822 } 823 self.pieces.deinit(allocator); 824 self.lines.deinit(allocator); 825 self.arena.deinit(); 826 } 827 828 // the url a terminal can open for a link: raw links to absolute urls 829 fn terminalLink(link: []const u8) ?[]const u8 { 830 if (!std.mem.startsWith(u8, link, ui.raw_link_prefix)) return null; 831 const url = link[ui.raw_link_prefix.len..]; 832 return if (std.mem.startsWith(u8, url, "/")) null else url; 833 } 834 835 // the link one of whose pieces has focus id `id` 836 fn linkOf(self: *const RichText, id: ?usize) ?usize { 837 for (self.link_focuses, 0..) |focuses, l| { 838 for (focuses.items) |focus| if (focus.id == id) return l; 839 } 840 return null; 841 } 842 843 pub fn build(self: *RichText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 844 self.clearGrid(); 845 self.focus.clear(); 846 const max_width = constraint.max_size.width; 847 if (max_width == 0 or constraint.max_size.height == 0) return; 848 const focused_link = self.linkOf(root_focus.grandchild_id); 849 850 try wgt.wrapLines(allocator, &self.lines, self.runes, .word, max_width); 851 // trailing spaces are dropped so a wrapped link doesn't highlight them 852 for (self.lines.items) |*line| { 853 while (line.end > line.start and self.runes[line.end - 1] == ' ') { 854 line.end -= 1; 855 line.width -= 1; 856 } 857 } 858 const visible = @min(self.lines.items.len, constraint.max_size.height orelse self.lines.items.len); 859 var width: usize = 1; 860 for (self.lines.items[0..visible]) |line| width = @max(width, line.width); 861 if (max_width) |w| width = @min(width, w); 862 863 var grid = try Grid.init(allocator, .{ .width = width, .height = visible }); 864 errdefer grid.deinit(); 865 self.pieces.clearRetainingCapacity(); 866 for (self.lines.items[0..visible], 0..) |line, y| { 867 var x: usize = 0; 868 for (self.runes[line.start..line.end], line.start..) |rune, i| { 869 const rune_width = xitui.width.cellWidth(rune); 870 if (x + rune_width > width) break; 871 const link = self.span_links[self.span_of[i]]; 872 const cell = try grid.cell(x, y); 873 cell.style = self.styles[self.span_of[i]]; 874 if (link) |l| { 875 cell.link = terminalLink(self.links[l]); 876 if (l == focused_link) cell.style.inverted = true; 877 // extend this line's piece of the link, or start one 878 const items = self.pieces.items; 879 if (items.len > 0 and items[items.len - 1].link == l and items[items.len - 1].rect.y == y) { 880 items[items.len - 1].rect.size.width += rune_width; 881 } else { 882 try self.pieces.append(allocator, .{ .link = l, .rect = .{ .x = x, .y = y, .size = .{ .width = rune_width, .height = 1 } } }); 883 } 884 } 885 try grid.setRune(x, y, rune); 886 x += rune_width; 887 } 888 } 889 890 // a link's pieces are consecutive, since its runes are 891 var piece_index: usize = 0; 892 for (self.pieces.items, 0..) |piece, i| { 893 piece_index = if (i > 0 and self.pieces.items[i - 1].link == piece.link) piece_index + 1 else 0; 894 const focuses = &self.link_focuses[piece.link]; 895 if (piece_index == focuses.items.len) { 896 const focus = try Focus.create(allocator, .{ .custom = self.links[piece.link] }); 897 errdefer focus.destroy(allocator); 898 focus.mode = .all; 899 try focuses.append(allocator, focus); 900 } 901 try self.focus.addChild(allocator, focuses.items[piece_index], piece.rect.size, piece.rect.x, piece.rect.y); 902 } 903 904 // focus rests on a link's first piece, which stays while the link shows 905 if (focused_link) |l| if (self.link_focuses[l].items.len > 0) { 906 const first = self.link_focuses[l].items[0].id; 907 if (self.focus.children.contains(first)) { 908 root_focus.grandchild_id = first; 909 self.focus.child_id = first; 910 } 911 }; 912 913 self.grid = grid; 914 } 915 916 pub fn input(self: *RichText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 917 _ = self; 918 _ = allocator; 919 _ = key; 920 _ = root_focus; 921 } 922 923 pub fn clearGrid(self: *RichText) void { 924 if (self.grid) |*grid| { 925 grid.deinit(); 926 self.grid = null; 927 } 928 } 929 930 pub fn getGrid(self: RichText) ?Grid { 931 return self.grid; 932 } 933 934 pub fn getFocus(self: *RichText) *Focus { 935 return self.focus; 936 } 937 }; 938 939 // an invisible widget that fills the horizontal space granted by its parent. 940 // used inside Box(horiz) with a min_size so the box reserves space for the 941 // children that follow, pushing them to the right. 942 pub const Spacer = struct { 943 focus: *Focus, 944 grid: ?Grid, 945 946 pub fn init(allocator: std.mem.Allocator) !Spacer { 947 return .{ 948 .focus = try Focus.create(allocator, .container), 949 .grid = null, 950 }; 951 } 952 953 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 954 self.focus.destroy(allocator); 955 if (self.grid) |*grid| { 956 grid.deinit(); 957 self.grid = null; 958 } 959 } 960 961 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 962 _ = root_focus; 963 self.clearGrid(); 964 const width = constraint.max_size.width orelse return; 965 if (width == 0) return; 966 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 967 } 968 969 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 970 _ = self; 971 _ = allocator; 972 _ = key; 973 _ = root_focus; 974 } 975 976 pub fn clearGrid(self: *Spacer) void { 977 if (self.grid) |*grid| { 978 grid.deinit(); 979 self.grid = null; 980 } 981 } 982 983 pub fn getGrid(self: Spacer) ?Grid { 984 return self.grid; 985 } 986 987 pub fn getFocus(self: *Spacer) *Focus { 988 return self.focus; 989 } 990 }; 991 992 // a sub-header's search box: a labeled text input whose enter the owning view 993 // turns into a navigation. shared by the files, commits and thread tabs. 994 pub const SearchBox = struct { 995 text_input: wgt.TextInput, 996 session: *ui.Session, 997 998 // the room the box asks its row for 999 pub const min_size: layout.MaybeSize = .{ .width = 20, .height = 3 }; 1000 1001 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, label: []const u8, name: []const u8, text_value: ?[]const u8) !SearchBox { 1002 var text_input = try wgt.TextInput.init(allocator, .{ .label = label, .name = name, .round_corners = true, .visible_width = 18, .render_content = session.is_terminal }); 1003 errdefer text_input.deinit(allocator); 1004 text_input.getFocus().mode = .all; 1005 if (text_value) |value| try text_input.setContent(allocator, value); 1006 return .{ .text_input = text_input, .session = session }; 1007 } 1008 1009 pub fn deinit(self: *SearchBox, allocator: std.mem.Allocator) void { 1010 self.text_input.deinit(allocator); 1011 } 1012 1013 pub fn build(self: *SearchBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1014 self.text_input.options.bottom_label = if (root_focus.grandchild_id == self.getFocus().id) " press enter " else ""; 1015 try self.text_input.build(allocator, constraint, root_focus); 1016 if (!self.session.is_terminal) { 1017 try self.session.text_inputs.put(self.session.arena.allocator(), self.getFocus().id, &self.text_input); 1018 } 1019 } 1020 1021 pub fn input(self: *SearchBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1022 try self.text_input.input(allocator, key, root_focus); 1023 } 1024 1025 // the typed text; the caller owns it 1026 pub fn text(self: *SearchBox, allocator: std.mem.Allocator) ![]u8 { 1027 return self.text_input.text(allocator); 1028 } 1029 1030 // whether the cursor sits past the last character, where right-arrow leaves 1031 // the box rather than moving within it 1032 pub fn atEnd(self: *SearchBox) bool { 1033 return self.text_input.cursor == self.text_input.content.items.len; 1034 } 1035 1036 pub fn clearGrid(self: *SearchBox) void { 1037 self.text_input.clearGrid(); 1038 } 1039 1040 pub fn getGrid(self: SearchBox) ?Grid { 1041 return self.text_input.getGrid(); 1042 } 1043 1044 pub fn getFocus(self: *SearchBox) *Focus { 1045 return self.text_input.getFocus(); 1046 } 1047 }; 1048 1049 // a row of bordered options posted as one form field. the selection is the 1050 // focused option, so a click selects on the web too. 1051 pub const Radio = struct { 1052 box: wgt.Box(Widget), 1053 name: []const u8, 1054 values: []const []const u8, 1055 1056 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, name: []const u8, values: []const []const u8, initial: []const u8) !Radio { 1057 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1058 errdefer box.deinit(allocator); 1059 for (values) |value| { 1060 var option = try wgt.TextBox.init(allocator, value, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1061 errdefer option.deinit(allocator); 1062 option.getFocus().mode = .all; 1063 option.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), ui.radio_prefix ++ "{s}={s}", .{ name, value }) }; 1064 if (std.mem.eql(u8, value, initial)) box.getFocus().child_id = option.getFocus().id; 1065 try box.children.put(allocator, option.getFocus().id, .{ .widget = .{ .text_box = option }, .rect = null, .min_size = .{ .width = try xitui.width.displayWidth(value) + 2, .height = 3 } }); 1066 } 1067 return .{ .box = box, .name = name, .values = values }; 1068 } 1069 1070 pub fn deinit(self: *Radio, allocator: std.mem.Allocator) void { 1071 self.box.deinit(allocator); 1072 } 1073 1074 // the focused option's value 1075 pub fn selected(self: *Radio) []const u8 { 1076 const index = self.box.children.getIndex(self.box.getFocus().child_id orelse unreachable) orelse unreachable; 1077 return self.values[index]; 1078 } 1079 1080 pub fn build(self: *Radio, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1081 self.clearGrid(); 1082 const selected_id = self.box.getFocus().child_id; 1083 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1084 child.widget.text_box.options.border_style = if (id == selected_id) .single else .hidden; 1085 } 1086 try self.box.build(allocator, constraint, root_focus); 1087 } 1088 1089 pub fn input(self: *Radio, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1090 _ = allocator; 1091 const ids = self.box.children.keys(); 1092 const index = self.box.children.getIndex(self.box.getFocus().child_id orelse return) orelse return; 1093 switch (key) { 1094 .arrow_left => if (index > 0) root_focus.setFocus(ids[index - 1]), 1095 .arrow_right => if (index + 1 < ids.len) root_focus.setFocus(ids[index + 1]), 1096 else => {}, 1097 } 1098 } 1099 1100 pub fn clearGrid(self: *Radio) void { 1101 self.box.clearGrid(); 1102 } 1103 1104 pub fn getGrid(self: Radio) ?Grid { 1105 return self.box.getGrid(); 1106 } 1107 1108 pub fn getFocus(self: *Radio) *Focus { 1109 return self.box.getFocus(); 1110 } 1111 }; 1112 1113 // a tab's sub-header: an optional search box on the left, then a spacer, then 1114 // the clone url on the right. the owning view turns a submitted query into a 1115 // route, so route building stays with the tab that knows its own url. 1116 pub const SearchHeader = struct { 1117 box: wgt.Box(Widget), 1118 session: *ui.Session, 1119 1120 pub fn init( 1121 allocator: std.mem.Allocator, 1122 session: *ui.Session, 1123 handle: ui.RepoHandle, 1124 label: []const u8, 1125 name: []const u8, 1126 text: ?[]const u8, 1127 show_input: bool, 1128 ) !SearchHeader { 1129 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1130 errdefer box.deinit(allocator); 1131 1132 // the search box only appears where an index can serve it. 1133 if (show_input) { 1134 var search_box = try SearchBox.init(allocator, session, label, name, text); 1135 errdefer search_box.deinit(allocator); 1136 box.getFocus().child_id = search_box.getFocus().id; 1137 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 1138 } 1139 1140 // the spacer pushes the clone url to the right edge. 1141 { 1142 var spacer = try Spacer.init(allocator); 1143 errdefer spacer.deinit(allocator); 1144 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1145 } 1146 1147 if (session.data.host_kind == .server) { 1148 if (try CopyableText.initClone(allocator, session, handle)) |value| { 1149 var clone_url = value; 1150 errdefer clone_url.deinit(allocator); 1151 const min_width = clone_url.minWidth(); 1152 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 1153 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 1154 } 1155 } 1156 1157 return .{ .box = box, .session = session }; 1158 } 1159 1160 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 1161 self.box.deinit(allocator); 1162 } 1163 1164 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1165 self.clearGrid(); 1166 try self.box.build(allocator, constraint, root_focus); 1167 } 1168 1169 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1170 const current = self.box.getFocus().child_id orelse return; 1171 if (self.searchBox()) |search_box| { 1172 if (current == search_box.getFocus().id) { 1173 // tab, and right-arrow past the last character, step to the 1174 // clone url beside the box 1175 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1176 if (self.cloneUrl()) |clone_url| { 1177 root_focus.setFocus(clone_url.getFocus().id); 1178 return; 1179 } 1180 } 1181 return search_box.input(allocator, key, root_focus); 1182 } 1183 } 1184 const clone_url = self.cloneUrl() orelse return; 1185 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1186 if (self.searchBox()) |search_box| { 1187 root_focus.setFocus(search_box.getFocus().id); 1188 return; 1189 } 1190 } 1191 try clone_url.input(allocator, key, root_focus); 1192 } 1193 1194 // the typed query when enter should submit it: the box exists and holds 1195 // focus. the caller owns the returned text. 1196 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1197 const search_box = self.searchBox() orelse return null; 1198 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 1199 return try search_box.text(allocator); 1200 } 1201 1202 // enter the header, preferring the search box. 1203 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1204 if (self.searchBox()) |search_box| { 1205 root_focus.setFocus(search_box.getFocus().id); 1206 return true; 1207 } 1208 const clone_url = self.cloneUrl() orelse return false; 1209 root_focus.setFocus(clone_url.getFocus().id); 1210 return true; 1211 } 1212 1213 pub fn hasFocusable(self: *SearchHeader) bool { 1214 return self.cloneUrl() != null or self.searchBox() != null; 1215 } 1216 1217 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1218 for (self.box.children.values()) |*child| switch (child.widget) { 1219 .copyable_text => |*copyable_text| return copyable_text, 1220 else => {}, 1221 }; 1222 return null; 1223 } 1224 1225 fn searchBox(self: *SearchHeader) ?*SearchBox { 1226 for (self.box.children.values()) |*child| switch (child.widget) { 1227 .search_box => |*search_box| return search_box, 1228 else => {}, 1229 }; 1230 return null; 1231 } 1232 1233 pub fn clearGrid(self: *SearchHeader) void { 1234 self.box.clearGrid(); 1235 } 1236 1237 pub fn getGrid(self: SearchHeader) ?Grid { 1238 return self.box.getGrid(); 1239 } 1240 1241 pub fn getFocus(self: *SearchHeader) *Focus { 1242 return self.box.getFocus(); 1243 } 1244 }; 1245 1246 // a one-row, non-focusable status bar showing the current page's url 1247 pub const Footer = struct { 1248 focus: *Focus, 1249 grid: ?Grid, 1250 session: *ui.Session, 1251 arena: std.heap.ArenaAllocator, 1252 url: []const u8 = "", 1253 url_width: usize = 0, 1254 1255 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1256 return .{ 1257 .focus = try Focus.create(allocator, .container), 1258 .grid = null, 1259 .session = session, 1260 .arena = std.heap.ArenaAllocator.init(allocator), 1261 }; 1262 } 1263 1264 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1265 self.focus.destroy(allocator); 1266 if (self.grid) |*grid| grid.deinit(); 1267 self.arena.deinit(); 1268 } 1269 1270 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1271 _ = root_focus; 1272 self.clearGrid(); 1273 const width = constraint.max_size.width orelse return; 1274 if (width == 0) return; 1275 1276 _ = self.arena.reset(.retain_capacity); 1277 self.url = ""; 1278 self.url_width = 0; 1279 const aa = self.arena.allocator(); 1280 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1281 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1282 self.url = text; 1283 1284 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1285 errdefer grid.deinit(); 1286 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1287 var i: usize = 0; 1288 while (utf8.nextCodepoint()) |ch| { 1289 const rune_width = xitui.width.cellWidth(ch); 1290 if (i + rune_width > width) break; 1291 // a terminal hyperlink to the page 1292 (try grid.cell(i, 0)).link = text; 1293 try grid.setRune(i, 0, ch); 1294 i += rune_width; 1295 } 1296 self.url_width = i; 1297 self.grid = grid; 1298 } 1299 1300 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1301 _ = allocator; 1302 switch (key) { 1303 .mouse => |mouse| { 1304 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1305 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1306 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1307 }, 1308 else => {}, 1309 } 1310 } 1311 1312 pub fn clearGrid(self: *Footer) void { 1313 if (self.grid) |*grid| { 1314 grid.deinit(); 1315 self.grid = null; 1316 } 1317 } 1318 1319 pub fn getGrid(self: Footer) ?Grid { 1320 return self.grid; 1321 } 1322 1323 pub fn getFocus(self: *Footer) *Focus { 1324 return self.focus; 1325 } 1326 }; 1327 1328 // a single-child wrapper that builds the child at its natural size and 1329 // positions its grid in the middle of the area granted by the parent 1330 // a bordered label, indented past the content it precedes 1331 pub const SectionLabel = struct { 1332 box: wgt.Box(Widget), 1333 1334 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1335 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1336 errdefer box.deinit(allocator); 1337 1338 { 1339 var gap = try wgt.Text.init(allocator, " "); 1340 errdefer gap.deinit(allocator); 1341 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1342 } 1343 1344 { 1345 var tb = try wgt.TextBox.init(allocator, content, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1346 errdefer tb.deinit(allocator); 1347 tb.getFocus().mode = .all; 1348 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1349 } 1350 1351 box.getFocus().child_id = box.children.keys()[1]; 1352 return .{ .box = box }; 1353 } 1354 1355 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1356 self.box.deinit(allocator); 1357 } 1358 1359 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1360 try self.box.build(allocator, constraint, root_focus); 1361 } 1362 1363 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1364 _ = self; 1365 _ = allocator; 1366 _ = key; 1367 _ = root_focus; 1368 } 1369 1370 pub fn clearGrid(self: *SectionLabel) void { 1371 self.box.clearGrid(); 1372 } 1373 1374 pub fn getGrid(self: SectionLabel) ?Grid { 1375 return self.box.getGrid(); 1376 } 1377 1378 pub fn getFocus(self: *SectionLabel) *Focus { 1379 return self.box.getFocus(); 1380 } 1381 }; 1382 1383 // a submit button, indented past its form's content. the web form POSTs 1384 // the button's form; terminal hosts detect presses through `buttonId`. 1385 pub const SubmitButton = struct { 1386 box: wgt.Box(Widget), 1387 1388 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1389 return initLabeled(allocator, "submit"); 1390 } 1391 1392 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1393 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1394 errdefer box.deinit(allocator); 1395 1396 { 1397 var gap = try wgt.Text.init(allocator, " "); 1398 errdefer gap.deinit(allocator); 1399 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1400 } 1401 1402 { 1403 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1404 errdefer button.deinit(allocator); 1405 button.getFocus().mode = .all; 1406 // the renderer distinguishes plain clickables from buttons that 1407 // should POST to a server route by this kind. 1408 button.getFocus().kind = .{ .custom = "submit" }; 1409 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1410 } 1411 1412 box.getFocus().child_id = box.children.keys()[1]; 1413 return .{ .box = box }; 1414 } 1415 1416 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1417 self.box.deinit(allocator); 1418 } 1419 1420 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1421 const button = &self.box.children.values()[1]; 1422 try button.widget.text_box.setContent(allocator, label); 1423 button.min_size = .{ .width = label.len + 2, .height = null }; 1424 } 1425 1426 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1427 try self.box.build(allocator, constraint, root_focus); 1428 } 1429 1430 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1431 _ = self; 1432 _ = allocator; 1433 _ = key; 1434 _ = root_focus; 1435 } 1436 1437 pub fn clearGrid(self: *SubmitButton) void { 1438 self.box.clearGrid(); 1439 } 1440 1441 pub fn getGrid(self: SubmitButton) ?Grid { 1442 return self.box.getGrid(); 1443 } 1444 1445 pub fn getFocus(self: *SubmitButton) *Focus { 1446 return self.box.getFocus(); 1447 } 1448 1449 // the button's focus id, for click hit-testing 1450 pub fn buttonId(self: *const SubmitButton) usize { 1451 return self.box.children.keys()[1]; 1452 } 1453 }; 1454 1455 pub const Center = struct { 1456 focus: *Focus, 1457 grid: ?Grid, 1458 child: *Widget, 1459 1460 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1461 const child = try allocator.create(Widget); 1462 errdefer allocator.destroy(child); 1463 child.* = child_widget; 1464 return .{ 1465 .focus = try Focus.create(allocator, .container), 1466 .grid = null, 1467 .child = child, 1468 }; 1469 } 1470 1471 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1472 self.focus.destroy(allocator); 1473 if (self.grid) |*grid| { 1474 grid.deinit(); 1475 self.grid = null; 1476 } 1477 self.child.deinit(allocator); 1478 allocator.destroy(self.child); 1479 } 1480 1481 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1482 self.clearGrid(); 1483 self.getFocus().clear(); 1484 1485 // build the child without forcing it to fill min_size; it sizes 1486 // itself to its content within the available max. 1487 try self.child.build(allocator, .{ 1488 .min_size = .{ .width = null, .height = null }, 1489 .max_size = constraint.max_size, 1490 }, root_focus); 1491 1492 const child_grid = self.child.getGrid() orelse return; 1493 1494 // prefer max when given; otherwise grow to max(min, child) so that 1495 // when only a min is set (e.g. the wasm path passes viewport rows 1496 // as min_height with no max), we can still vertically center while 1497 // letting taller content extend past the min. 1498 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; 1499 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; 1500 if (width == 0 or height == 0) return; 1501 1502 const offset_x: usize = (width -| child_grid.size.width) / 2; 1503 const offset_y: usize = (height -| child_grid.size.height) / 2; 1504 1505 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1506 errdefer grid.deinit(); 1507 try grid.drawGrid(child_grid, offset_x, offset_y); 1508 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1509 self.getFocus().child_id = self.child.getFocus().id; 1510 1511 self.grid = grid; 1512 } 1513 1514 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1515 try self.child.input(allocator, key, root_focus); 1516 } 1517 1518 pub fn clearGrid(self: *Center) void { 1519 if (self.grid) |*grid| { 1520 grid.deinit(); 1521 self.grid = null; 1522 } 1523 self.child.clearGrid(); 1524 } 1525 1526 pub fn getGrid(self: Center) ?Grid { 1527 return self.grid; 1528 } 1529 1530 pub fn getFocus(self: *Center) *Focus { 1531 return self.focus; 1532 } 1533 }; 1534 1535 // renders truecolor ANSI art 1536 pub const AnsiArt = struct { 1537 focus: *Focus, 1538 grid: ?Grid, 1539 content: []const u8, 1540 1541 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1542 return .{ 1543 .focus = try Focus.create(allocator, .container), 1544 .grid = null, 1545 .content = content, 1546 }; 1547 } 1548 1549 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1550 self.focus.destroy(allocator); 1551 if (self.grid) |*grid| { 1552 grid.deinit(); 1553 self.grid = null; 1554 } 1555 } 1556 1557 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1558 _ = root_focus; 1559 self.clearGrid(); 1560 1561 // parse into rows of cells, then copy into a rectangular grid 1562 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1563 defer { 1564 for (rows.items) |*row| row.deinit(allocator); 1565 rows.deinit(allocator); 1566 } 1567 var row: std.ArrayList(Grid.Cell) = .empty; 1568 errdefer row.deinit(allocator); 1569 1570 var style: Grid.Style = .{}; 1571 var width: usize = 0; 1572 const content = self.content; 1573 var i: usize = 0; 1574 while (i < content.len) { 1575 const byte = content[i]; 1576 if (byte == '\n') { 1577 width = @max(width, row.items.len); 1578 try rows.append(allocator, row); 1579 row = .empty; 1580 i += 1; 1581 } else if (byte == '\r') { 1582 // git may checkout the ansi art with crlf line endings on windows 1583 i += 1; 1584 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1585 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1586 var j = i + 2; 1587 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1588 if (j >= content.len) { 1589 i = content.len; // malformed trailing escape; stop 1590 } else { 1591 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1592 i = j + 1; 1593 } 1594 } else { 1595 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1596 const end = @min(content.len, i + len); 1597 const rune = content[i..end]; 1598 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1599 // a foreground color does not make a space visible. only an 1600 // effective background makes it opaque; foreground state often 1601 // remains active through the padding at the end of a row. 1602 const effective_background = if (style.inverted) style.fg else style.bg; 1603 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1604 try row.append(allocator, .{ 1605 .rune = if (transparent) null else codepoint, 1606 .style = style, 1607 }); 1608 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1609 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1610 } 1611 i = end; 1612 } 1613 } 1614 // a trailing row with content but no closing newline 1615 if (row.items.len > 0) { 1616 width = @max(width, row.items.len); 1617 try rows.append(allocator, row); 1618 } else { 1619 row.deinit(allocator); 1620 } 1621 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1622 1623 const height = rows.items.len; 1624 if (width == 0 or height == 0) return; 1625 1626 const clamped_w = @min(width, constraint.max_size.width orelse width); 1627 const clamped_h = @min(height, constraint.max_size.height orelse height); 1628 1629 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1630 errdefer grid.deinit(); 1631 for (rows.items[0..clamped_h], 0..) |r, y| { 1632 const n = @min(r.items.len, clamped_w); 1633 for (r.items[0..n], 0..) |cell, x| { 1634 if (cell.continuation) continue; 1635 (try grid.cell(x, y)).style = cell.style; 1636 try grid.setRune(x, y, cell.rune); 1637 } 1638 } 1639 self.grid = grid; 1640 } 1641 1642 pub fn clearGrid(self: *AnsiArt) void { 1643 if (self.grid) |*grid| { 1644 grid.deinit(); 1645 self.grid = null; 1646 } 1647 } 1648 1649 pub fn getGrid(self: AnsiArt) ?Grid { 1650 return self.grid; 1651 } 1652 1653 pub fn getFocus(self: *AnsiArt) *Focus { 1654 return self.focus; 1655 } 1656 1657 fn applySgr(style: *Grid.Style, params: []const u8) void { 1658 var nums: [16]u32 = undefined; 1659 var n: usize = 0; 1660 var it = std.mem.splitScalar(u8, params, ';'); 1661 while (it.next()) |tok| { 1662 if (n >= nums.len) break; 1663 // an empty parameter (e.g. bare "\x1b[m") means 0 1664 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1665 n += 1; 1666 } 1667 if (n == 0) { 1668 style.* = .{}; 1669 return; 1670 } 1671 var i: usize = 0; 1672 while (i < n) : (i += 1) { 1673 switch (nums[i]) { 1674 0 => style.* = .{}, 1675 7 => style.inverted = true, 1676 27 => style.inverted = false, 1677 39 => style.fg = null, 1678 49 => style.bg = null, 1679 38, 48 => { 1680 // truecolor form: 38;2;r;g;b — anything else is ignored 1681 if (i + 4 < n and nums[i + 1] == 2) { 1682 const c = Grid.Color{ .rgb = .{ 1683 .r = @truncate(nums[i + 2]), 1684 .g = @truncate(nums[i + 3]), 1685 .b = @truncate(nums[i + 4]), 1686 } }; 1687 if (nums[i] == 38) style.fg = c else style.bg = c; 1688 i += 4; 1689 } 1690 }, 1691 else => {}, 1692 } 1693 } 1694 } 1695 }; 1696 1697 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1698 pub const AnsiBackground = struct { 1699 grid: ?Grid, 1700 child: *Widget, 1701 art: AnsiArt, 1702 session: *ui.Session, 1703 1704 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1705 var cw = child_widget; 1706 const child = allocator.create(Widget) catch |e| { 1707 cw.deinit(allocator); 1708 return e; 1709 }; 1710 child.* = cw; 1711 errdefer { 1712 child.deinit(allocator); 1713 allocator.destroy(child); 1714 } 1715 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1716 } 1717 1718 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1719 if (self.grid) |*grid| grid.deinit(); 1720 self.art.deinit(allocator); 1721 self.child.deinit(allocator); 1722 allocator.destroy(self.child); 1723 } 1724 1725 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1726 self.clearGrid(); 1727 1728 // make the wrapped page fill the whole available area 1729 try self.child.build(allocator, .{ 1730 .min_size = .{ 1731 .width = constraint.max_size.width orelse constraint.min_size.width, 1732 .height = constraint.max_size.height orelse constraint.min_size.height, 1733 }, 1734 .max_size = constraint.max_size, 1735 }, root_focus); 1736 1737 const foreground = self.child.getGrid() orelse return; 1738 if (!self.session.color) return; 1739 // terminals show the art only once they report a background to fade it toward 1740 const backdrop: ?Grid.Color.Rgb = if (self.session.is_terminal) self.session.terminal_background orelse return else null; 1741 self.art.content = self.session.data.ansi_art; 1742 try self.art.build(allocator, .{ 1743 .min_size = .{ .width = null, .height = null }, 1744 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1745 }, root_focus); 1746 // the web fades the art with css opacity instead 1747 const art_backdrop = backdrop orelse return; 1748 if (self.art.grid) |*art_grid| { 1749 for (art_grid.cells) |*cell| { 1750 cell.style.fg = fadeColor(cell.style.fg, art_backdrop); 1751 cell.style.bg = fadeColor(cell.style.bg, art_backdrop); 1752 } 1753 self.grid = try artBehind(allocator, foreground, art_grid.*); 1754 } 1755 } 1756 1757 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1758 try self.child.input(allocator, key, root_focus); 1759 } 1760 1761 pub fn clearGrid(self: *AnsiBackground) void { 1762 if (self.grid) |*grid| { 1763 grid.deinit(); 1764 self.grid = null; 1765 } 1766 self.child.clearGrid(); 1767 self.art.clearGrid(); 1768 } 1769 1770 pub fn getGrid(self: AnsiBackground) ?Grid { 1771 return self.grid orelse self.child.getGrid(); 1772 } 1773 1774 pub fn getFocus(self: *AnsiBackground) *Focus { 1775 return self.child.getFocus(); 1776 } 1777 1778 // a foreground cell counts as blank if it's empty or a bare space without styling 1779 fn cellIsBlank(cell: Grid.Cell) bool { 1780 if (cell.continuation) return false; 1781 if (cell.rune) |rune| { 1782 return rune == ' ' and 1783 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1784 } 1785 return true; 1786 } 1787 1788 // the art is faded toward the background at this opacity so foreground 1789 // text stays legible over it 1790 pub const art_opacity = 25; // percent 1791 1792 // the art parser only emits truecolor, so a palette color never occurs 1793 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1794 const c = color orelse return null; 1795 return switch (c) { 1796 .rgb => |v| v, 1797 else => null, 1798 }; 1799 } 1800 1801 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 1802 const v = artRgb(color) orelse return color; 1803 const blend = struct { 1804 fn f(c: u8, b: u8) u8 { 1805 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 1806 } 1807 }.f; 1808 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 1809 } 1810 1811 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1812 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1813 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1814 // snap such channels clear of that range so the backdrop doesn't blink or 1815 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1816 // actually render truecolor. 1817 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1818 const v = artRgb(color) orelse return color; 1819 const snap = struct { 1820 fn f(c: u8) u8 { 1821 return if (c >= 1 and c <= 9) 10 else c; 1822 } 1823 }.f; 1824 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1825 } 1826 1827 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1828 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1829 dst.style.bg = background_maybe; 1830 // change unstyled text to contrast with the art behind it 1831 if (dst.style.fg == null) { 1832 if (artRgb(background_maybe)) |v| { 1833 // use near-black and near-white, which is easier on the eyes 1834 // while retaining high contrast 1835 const luminance = (@as(u32, v.r) * 299 + 1836 @as(u32, v.g) * 587 + 1837 @as(u32, v.b) * 114) / 1000; 1838 dst.style.fg = if (luminance >= 128) 1839 .{ .rgb = .{ .r = 16, .g = 16, .b = 16 } } 1840 else 1841 .{ .rgb = .{ .r = 240, .g = 240, .b = 240 } }; 1842 } 1843 } 1844 } 1845 1846 // composites ANSI art behind a foreground grid 1847 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1848 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1849 errdefer out.deinit(); 1850 1851 const anchor_x = foreground.size.width -| art_grid.size.width; 1852 for (0..art_grid.size.height) |y| { 1853 for (0..art_grid.size.width) |x| { 1854 const src = (try art_grid.cell(x, y)).*; 1855 if (src.rune == null) continue; 1856 const dst = try out.cell(anchor_x + x, y); 1857 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1858 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1859 // blank cells take the art; occupied cells take its background 1860 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1861 dst.style = src.style; 1862 dst.style.fg = sgrSafe(dst.style.fg); 1863 dst.style.bg = sgrSafe(dst.style.bg); 1864 try out.setRune(anchor_x + x, y, src.rune); 1865 } else { 1866 applyArtBackground(dst, src); 1867 if (next_dst) |next| applyArtBackground(next, src); 1868 } 1869 } 1870 } 1871 return out; 1872 } 1873 }; 1874 1875 pub const CopyableText = struct { 1876 pub const Choice = struct { 1877 selector: []const u8 = "", 1878 text: []const u8, 1879 copyable_text: ?[]const u8 = null, 1880 label: []const u8 = "", 1881 bottom_label: []const u8 = "", 1882 }; 1883 1884 box: wgt.Box(Widget), 1885 session: *ui.Session, 1886 choices: []Choice, 1887 selected: usize = 0, 1888 1889 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1890 if (choices.len == 0) return error.MissingCopyableText; 1891 1892 const owned_choices = try allocator.dupe(Choice, choices); 1893 errdefer allocator.free(owned_choices); 1894 1895 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1896 errdefer box.deinit(allocator); 1897 1898 if (choices.len > 1) for (choices) |choice| { 1899 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1900 errdefer selector_box.deinit(allocator); 1901 selector_box.getFocus().mode = .all; 1902 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 } }); 1903 }; 1904 1905 var text_input = try wgt.TextInput.init(allocator, .{ 1906 .border_style = .single, 1907 .round_corners = true, 1908 .label = choices[initial].label, 1909 .bottom_label = choices[initial].bottom_label, 1910 .read_only = true, 1911 .render_content = session.is_terminal, 1912 .visible_width = null, 1913 }); 1914 errdefer text_input.deinit(allocator); 1915 text_input.getFocus().mode = .all; 1916 try text_input.setContent(allocator, choices[initial].text); 1917 box.getFocus().child_id = text_input.getFocus().id; 1918 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1919 1920 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 1921 } 1922 1923 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 1924 const aa = session.page_arena.allocator(); 1925 var choices: [2]Choice = undefined; 1926 var count: usize = 0; 1927 var ssh_index: ?usize = null; 1928 switch (handle.location) { 1929 .repo => |identity| { 1930 if (session.data.git_http_port) |port| { 1931 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/{s}", .{ port, identity }); 1932 choices[count] = .{ 1933 .selector = "http", 1934 .text = url, 1935 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1936 .label = " clone ", 1937 }; 1938 count += 1; 1939 } 1940 if (session.data.git_ssh_port) |port| { 1941 ssh_index = count; 1942 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/{s}", .{ port, identity }); 1943 choices[count] = .{ 1944 .selector = "ssh", 1945 .text = url, 1946 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1947 .label = " clone ", 1948 }; 1949 count += 1; 1950 } 1951 }, 1952 .fork => |f| if (session.data.git_ssh_port) |port| { 1953 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 1954 choices[count] = .{ 1955 .text = url, 1956 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1957 .label = " clone ", 1958 }; 1959 count += 1; 1960 }, 1961 } 1962 if (count == 0) return null; 1963 // a viewer who can push starts on the ssh url 1964 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 1965 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 1966 } 1967 1968 pub fn minWidth(self: *const CopyableText) usize { 1969 var width = maxWidth(self.choices) + 2; 1970 if (self.choices.len > 1) { 1971 for (self.choices) |choice| width += choice.selector.len + 2; 1972 } 1973 return width; 1974 } 1975 1976 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1977 self.box.deinit(allocator); 1978 allocator.free(self.choices); 1979 } 1980 1981 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1982 self.clearGrid(); 1983 1984 if (self.choices.len > 1) { 1985 if (root_focus.grandchild_id) |id| { 1986 for (0..self.choices.len) |i| { 1987 if (id != self.selector(i).getFocus().id) continue; 1988 if (i != self.selected) try self.select(allocator, i); 1989 root_focus.setFocus(self.textInput().getFocus().id); 1990 break; 1991 } 1992 } 1993 1994 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1995 for (0..self.choices.len) |i| { 1996 self.selector(i).options.border_style = if (i == self.selected) 1997 (if (focused) .double else .single) 1998 else 1999 .hidden; 2000 } 2001 } 2002 self.textInput().options.border_style = .single; 2003 self.textInput().cursor = 0; 2004 self.textInput().scroll_offset = 0; 2005 try self.box.build(allocator, constraint, root_focus); 2006 2007 if (!self.session.is_terminal) { 2008 const aa = self.session.arena.allocator(); 2009 const id = self.textInput().getFocus().id; 2010 try self.session.text_inputs.put(aa, id, self.textInput()); 2011 } 2012 } 2013 2014 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2015 switch (key) { 2016 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2017 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2018 .enter => if (self.session.is_terminal) self.show(), 2019 .mouse => |mouse| { 2020 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2021 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2022 try self.select(allocator, i); 2023 root_focus.setFocus(self.textInput().getFocus().id); 2024 return; 2025 }; 2026 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2027 }, 2028 else => {}, 2029 } 2030 } 2031 2032 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2033 self.selected = selected; 2034 const choice = self.choices[selected]; 2035 const text_input = self.textInput(); 2036 text_input.options.label = choice.label; 2037 text_input.options.bottom_label = choice.bottom_label; 2038 try text_input.setContent(allocator, choice.text); 2039 } 2040 2041 fn show(self: *CopyableText) void { 2042 const choice = self.choices[self.selected]; 2043 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2044 } 2045 2046 fn textInput(self: *CopyableText) *wgt.TextInput { 2047 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2048 } 2049 2050 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2051 return &self.box.children.values()[index].widget.text_box; 2052 } 2053 2054 fn maxWidth(choices: []const Choice) usize { 2055 var width: usize = 0; 2056 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2057 return width; 2058 } 2059 2060 pub fn clearGrid(self: *CopyableText) void { 2061 self.box.clearGrid(); 2062 } 2063 2064 pub fn getGrid(self: CopyableText) ?Grid { 2065 return self.box.getGrid(); 2066 } 2067 2068 pub fn getFocus(self: *CopyableText) *Focus { 2069 return self.box.getFocus(); 2070 } 2071 }; 2072