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