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