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