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