╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const ui = @import("../../ui.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const tr = xit.tree; 7 const df = xit.diff; 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const Key = xitui.input.Key; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 const inp = @import("../input.zig"); 15 16 // how many lines of a file's content one window shows before a "next" link. 17 const file_page = 5000; 18 19 // budget for the total bytes of file content preloaded (windowed) per page, 20 // so selecting a preloaded file shows its contents without a reload. 21 const max_preloaded_bytes = 2 * 1024 * 1024; 22 23 // one entry in the directory currently being viewed. 24 pub const Entry = struct { 25 name: []const u8, 26 is_dir: bool, 27 // a file's content window split into lines (each without its trailing 28 // newline); empty for directories and for binary or empty files. 29 lines: []const []const u8 = &.{}, 30 // true when xit's line iterator flagged the file as binary, so the detail 31 // pane shows a placeholder rather than its bytes. 32 is_binary: bool = false, 33 // the line index this window starts at (0 = the first window). non-zero only 34 // for the route's selected file, paginated by the url's `line`. 35 window_start: usize = 0, 36 // whether more lines exist after this window. 37 has_more: bool = false, 38 // whether this file's content was read; unloaded files load via their 39 // "a:" link. 40 loaded: bool = false, 41 }; 42 43 // where links and clone commands for this repository are rooted. 44 location: ui.RoutablePage.RepoLocation, 45 // the resolved ref this listing is read at (the default branch when the route 46 // didn't name one), so the view's directory links stay pinned to it. 47 ref_or_oid: ui.RoutablePage.RefOrOid, 48 ref_or_oid_value: []const u8, 49 // the directory being viewed, relative to the repo root ("" at the root). 50 dir: []const u8, 51 // a file within `dir` to start selected (so its contents show and the url 52 // carries it), or null to fall back to a README / the first row. set when the 53 // route's path named a file rather than a directory. 54 selected_file: ?[]const u8 = null, 55 entries: []const Entry, 56 // the "viewing <ref> <value>" banner shown above the listing. 57 header: Header, 58 59 const Self = @This(); 60 61 // build the listing for an opened repo. generic over the repo's backend and 62 // hash kind so the tree/diff types it threads through match the repo's opts. 63 // reads with the arena's backing allocator (transient); the listing is built 64 // into the page arena so it outlives the repo handle. 65 pub fn init( 66 comptime repo_kind: rp.RepoKind, 67 comptime repo_opts: rp.RepoOpts(repo_kind), 68 arena: *std.heap.ArenaAllocator, 69 repo: *rp.Repo(repo_kind, repo_opts), 70 io: std.Io, 71 gpa: std.mem.Allocator, 72 location: ui.RoutablePage.RepoLocation, 73 requested_ref_or_oid: ?ui.RoutablePage.RefOrOid, 74 requested_value: []const u8, 75 path: []const u8, 76 line: usize, 77 ) !Self { 78 const aa = arena.allocator(); 79 80 // resolve the requested ref (or the default branch) to the commit oid whose 81 // tree we list. a ref the route named explicitly that doesn't resolve is a 82 // bad url (NotFound -> 404); the default-branch path falls through to empty. 83 const resolved = (try ui.ResolvedRefOrOid(repo_kind, repo_opts).init(repo, io, aa, requested_ref_or_oid, requested_value)) orelse { 84 if (requested_ref_or_oid != null) return error.NotFound; 85 return emptyResult(aa, location, .branch, requested_value, path); 86 }; 87 88 // read just the viewed directory of that commit's tree. building the 89 // read-only state mirrors what repo.status does internally, but for an 90 // arbitrary commit rather than HEAD. a path that doesn't exist in the tree 91 // is a bad url (404). 92 var moment = repo.core.latestMoment() catch return emptyResult(aa, location, resolved.ref_or_oid, resolved.value, path); 93 const state = rp.Repo(repo_kind, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 94 var tree_dir = tr.TreeDir(repo_kind, repo_opts).init(state, io, gpa, &resolved.oid, path) catch |err| switch (err) { 95 error.TreeEntryNotFound => return error.NotFound, 96 else => return emptyResult(aa, location, resolved.ref_or_oid, resolved.value, path), 97 }; 98 defer tree_dir.deinit(); 99 100 // when `path` named a file, the listing is its parent directory with that 101 // file selected. 102 var dir = path; 103 var selected_file: ?[]const u8 = null; 104 if (tree_dir.file_name != null) { 105 const slash = std.mem.lastIndexOfScalar(u8, path, '/'); 106 selected_file = try aa.dupe(u8, if (slash) |s| path[s + 1 ..] else path); 107 dir = if (slash) |s| path[0..s] else ""; 108 } 109 110 // tree objects store their entries in name order, so the listing is 111 // already sorted. group directories before files, keeping that order 112 // within each group, and dupe into the page arena. file contents are read 113 // here (into the page arena) so the detail pane can show them without 114 // another lookup. 115 const entries = try aa.alloc(Entry, tree_dir.entries.count()); 116 var i: usize = 0; 117 var preloaded_bytes: usize = 0; 118 for ([_]bool{ true, false }) |want_dir| { 119 for (tree_dir.entries.keys(), tree_dir.entries.values()) |name, tree_entry| { 120 if (tree_entry.isTree() != want_dir) continue; 121 var entry: Entry = .{ .name = try aa.dupe(u8, name), .is_dir = want_dir }; 122 if (!want_dir) { 123 // only the route's selected file paginates; the rest show 124 // their first window (for the in-page detail when selected). 125 const is_selected = if (selected_file) |sf| std.mem.eql(u8, sf, name) else false; 126 // the README always loads because it's the default selection. 127 if (preloaded_bytes < max_preloaded_bytes or is_selected or isReadme(name)) { 128 const file_path = try childDir(aa, dir, name); 129 // the url's `line` is 1-based (0 = unset); the window 130 // index is 0-based. 131 const window_start = if (is_selected) line -| 1 else 0; 132 const content = readFileContent(repo_kind, repo_opts, state, io, gpa, aa, file_path, tree_entry, window_start) catch 133 FileContent{ .lines = &.{} }; 134 for (content.lines) |l| preloaded_bytes += l.len; 135 entry.lines = content.lines; 136 entry.is_binary = content.is_binary; 137 entry.window_start = window_start; 138 entry.has_more = content.has_more; 139 entry.loaded = true; 140 } 141 } 142 entries[i] = entry; 143 i += 1; 144 } 145 } 146 147 return .{ 148 .location = try location.dupe(aa), 149 .ref_or_oid = resolved.ref_or_oid, 150 .ref_or_oid_value = resolved.value, 151 .dir = try aa.dupe(u8, dir), 152 .selected_file = selected_file, 153 .entries = entries, 154 .header = try Header.init(aa, resolved.ref_or_oid, resolved.value), 155 }; 156 } 157 158 const FileContent = struct { 159 lines: []const []const u8, 160 is_binary: bool = false, 161 has_more: bool = false, 162 }; 163 164 // read the window [start, start+file_page) of a committed file's contents at 165 // `tree_entry` into `arena`-owned lines, flagging whether more follow. xit's 166 // line iterator flags binary files (its source becomes `.binary`), in which case 167 // we report no lines and let the view show a placeholder. 168 fn readFileContent( 169 comptime repo_kind: rp.RepoKind, 170 comptime repo_opts: rp.RepoOpts(repo_kind), 171 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 172 io: std.Io, 173 gpa: std.mem.Allocator, 174 arena: std.mem.Allocator, 175 path: []const u8, 176 tree_entry: tr.TreeEntry(repo_opts.hash), 177 start: usize, 178 ) !FileContent { 179 var line_iter = try df.LineIterator(repo_kind, repo_opts).initFromTree(state, io, gpa, path, tree_entry); 180 defer line_iter.deinit(); 181 if (line_iter.source == .binary) return .{ .lines = &.{}, .is_binary = true }; 182 183 // init reads through the file to validate it (and buffer it in memory), so 184 // the line count is known and a window can be sliced out of the buffer. 185 const total = line_iter.count(); 186 const end = @min(start +| file_page, total); 187 188 var lines: std.ArrayList([]const u8) = .empty; 189 var i = start; 190 while (i < end) : (i += 1) { 191 const line = try line_iter.get(i); 192 defer line_iter.free(line); 193 try lines.append(arena, try arena.dupe(u8, line)); 194 } 195 return .{ 196 .lines = try lines.toOwnedSlice(arena), 197 .has_more = end < total, 198 }; 199 } 200 201 // an empty listing pinned to a ref, for the wasm / no-repo / unresolved paths. 202 pub fn emptyResult(aa: std.mem.Allocator, location: ui.RoutablePage.RepoLocation, ref_or_oid: ui.RoutablePage.RefOrOid, ref_or_oid_value: []const u8, dir: []const u8) !Self { 203 return .{ 204 .location = try location.dupe(aa), 205 .ref_or_oid = ref_or_oid, 206 .ref_or_oid_value = try aa.dupe(u8, ref_or_oid_value), 207 .dir = try aa.dupe(u8, dir), 208 .entries = &.{}, 209 .header = try Header.init(aa, ref_or_oid, ref_or_oid_value), 210 }; 211 } 212 213 pub const View = struct { 214 // a vertical stack: the "viewing <ref>" banner on top, then a horizontal 215 // split with the file/directory list on the left and a detail pane on the 216 // right showing the selected file's contents. 217 box: wgt.Box(ui.Widget), // vert: [header_index] = banner, [content_index] = split 218 data: *const Self, 219 session: *ui.Session, 220 // the list row whose contents the detail pane currently shows. 221 shown_index: ?usize, 222 223 const header_index: usize = 0; 224 const content_index: usize = 1; 225 // indices within the content box (the horizontal split). 226 const list_index: usize = 0; 227 const detail_index: usize = 1; 228 // indices within the detail pane frame (nav box above the content scroll). 229 const detail_nav_index: usize = 0; 230 const detail_scroll_index: usize = 1; 231 const list_max_width: usize = 35; 232 const detail_min_width: usize = 40; 233 // a list row's height: one text line inside its (hidden) border. 234 const row_height: usize = 3; 235 236 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 237 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 238 errdefer outer.deinit(allocator); 239 240 // the ref banner at the top. 241 { 242 var header_view = try Header.View.init(allocator, &data.header, session, data.location); 243 errdefer header_view.deinit(allocator); 244 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_files_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 245 } 246 247 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 248 errdefer box.deinit(allocator); 249 250 // the directory listing on the left (one focusable row each). 251 { 252 var list_scroll = blk: { 253 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 254 errdefer list_box.deinit(allocator); 255 256 // link kinds are borrowed by the rows, so they live in the page 257 // arena (as long as this page's widget tree). row labels are 258 // copied by their text boxes. 259 const aa = session.page_arena.allocator(); 260 if (data.dir.len != 0) { 261 try addRow(allocator, &list_box, "..", try dirLink(session.page_arena, data, parentDir(data.dir))); 262 } 263 for (data.entries) |entry| { 264 const path = try childDir(aa, data.dir, entry.name); 265 const label = if (entry.is_dir) try std.fmt.allocPrint(aa, "{s}/", .{entry.name}) else entry.name; 266 const link = if (entry.is_dir) 267 try dirLink(session.page_arena, data, path) 268 else 269 try fileLink(session.page_arena, data, path, entry.loaded, lineNumber(entry.window_start)); 270 try addRow(allocator, &list_box, label, link); 271 } 272 // select the file the route named, else prefer a README so its 273 // contents greet the visitor in the detail pane, else the first 274 // row. a non-root directory has a leading ".." row, so the entry 275 // index is offset by one there. 276 var sel: usize = 0; 277 if (list_box.children.count() > 0) { 278 const base: usize = if (data.dir.len != 0) 1 else 0; 279 const entry_idx = selectedFileIndex(data) orelse readmeIndex(data.entries); 280 sel = if (entry_idx) |i| i + base else 0; 281 list_box.getFocus().child_id = list_box.children.keys()[sel]; 282 } 283 var sc = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 284 // start scrolled so the selected row begins at the top of the 285 // viewport. rows are a fixed height, so no layout pass is 286 // needed; a too-far offset near the end of the list is clamped 287 // in build (the browser clamps its own scrolling natively). 288 sc.y = @intCast(sel * row_height); 289 break :blk sc; 290 }; 291 errdefer list_scroll.deinit(allocator); 292 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 293 } 294 295 // the detail pane on the right — a "next" nav box above a scroll of the 296 // selected file's content window. both are repopulated per selection by 297 // populateDetail. 298 { 299 var detail_outer = blk: { 300 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 301 errdefer frame.deinit(allocator); 302 303 { 304 var nav_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 305 errdefer nav_box.deinit(allocator); 306 try frame.children.put(allocator, nav_box.getFocus().id, .{ .widget = .{ .box = nav_box }, .rect = null, .min_size = null }); 307 } 308 { 309 // the content scroll wrapped in a bordered box; build sets the 310 // border single normally and double when the content is focused 311 // (the content box itself is borderless). 312 var scroll_frame = blk2: { 313 var detail_scroll = blk3: { 314 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 315 errdefer detail_inner.deinit(allocator); 316 break :blk3 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }); 317 }; 318 errdefer detail_scroll.deinit(allocator); 319 var sf = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .single, .rounded_corners = true, .direction = .vert }); 320 errdefer sf.deinit(allocator); 321 // the wrapper's selected child is its scroll, so the focus 322 // chain reaches the content box (populateDetail points the 323 // scroll's inner box at it). 324 sf.getFocus().child_id = detail_scroll.getFocus().id; 325 try sf.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 326 break :blk2 sf; 327 }; 328 errdefer scroll_frame.deinit(allocator); 329 // entering the detail pane lands on the content scroll (focus 330 // recovery descends the frame's selected child). the nav links 331 // above are reached by scrolling to the top, then up. 332 frame.getFocus().child_id = scroll_frame.getFocus().id; 333 // shrink, so the fill scroll yields the nav box's row 334 // instead of consuming the frame's whole height. 335 try frame.children.put(allocator, scroll_frame.getFocus().id, .{ .widget = .{ .box = scroll_frame }, .rect = null, .min_size = null, .flex = .shrink }); 336 } 337 break :blk frame; 338 }; 339 errdefer detail_outer.deinit(allocator); 340 detail_outer.getFocus().mode = .mouse; 341 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 342 } 343 344 box.getFocus().child_id = box.children.keys()[list_index]; 345 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 346 347 // focus lives in the split; the banner isn't focusable. 348 outer.getFocus().child_id = outer.children.keys()[content_index]; 349 return .{ .box = outer, .data = data, .session = session, .shown_index = null }; 350 } 351 352 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 353 var row = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 354 errdefer row.deinit(allocator); 355 row.getFocus().mode = .all; 356 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 357 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 358 } 359 360 // the selected file's contents as a single focusable multi-line text box. 361 // it's borderless — the border lives on the surrounding scroll frame, which 362 // doubles when this content is focused. a non-empty `link` makes activating 363 // the box follow it, like addRow. 364 fn addContentBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 365 _ = self; 366 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = null, .wrap_kind = .none }); 367 errdefer tb.deinit(allocator); 368 tb.getFocus().mode = .all; 369 if (link.len != 0) tb.getFocus().kind = .{ .custom = link }; 370 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 371 } 372 373 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 374 self.box.deinit(allocator); 375 } 376 377 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 378 return &self.box.children.values()[content_index].widget.box; 379 } 380 381 fn header(self: *View) *Header.View { 382 return &self.box.children.values()[header_index].widget.repo_files_header; 383 } 384 385 fn headerActive(self: *View) bool { 386 return self.box.getFocus().child_id == self.header().getFocus().id; 387 } 388 389 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 390 return &self.contentBox().children.values()[list_index].widget.scroll; 391 } 392 393 fn listBox(self: *View) *wgt.Box(ui.Widget) { 394 return &self.listScroll().child.box; 395 } 396 397 fn detailOuter(self: *View) *wgt.Box(ui.Widget) { 398 return &self.contentBox().children.values()[detail_index].widget.box; 399 } 400 401 fn navBox(self: *View) *wgt.Box(ui.Widget) { 402 return &self.detailOuter().children.values()[detail_nav_index].widget.box; 403 } 404 405 // the bordered box wrapping the content scroll (border toggles with focus). 406 fn detailScrollFrame(self: *View) *wgt.Box(ui.Widget) { 407 return &self.detailOuter().children.values()[detail_scroll_index].widget.box; 408 } 409 410 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 411 return &self.detailScrollFrame().children.values()[0].widget.scroll; 412 } 413 414 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 415 return &self.detailScroll().child.box; 416 } 417 418 fn detailActive(self: *View) bool { 419 const content = self.contentBox(); 420 const cid = content.getFocus().child_id orelse return false; 421 return content.children.getIndex(cid) == detail_index; 422 } 423 424 // the selected list row's index, or null when nothing is focused. 425 fn selectedRowIndex(self: *View) ?usize { 426 const lb = self.listBox(); 427 const cid = lb.getFocus().child_id orelse return null; 428 return lb.children.getIndex(cid); 429 } 430 431 // the entry the selected row points at, or null for the ".." row (which 432 // isn't an entry). directory rows return their entry too, though they carry 433 // no contents. 434 fn selectedEntry(self: *View) ?Entry { 435 const idx = self.selectedRowIndex() orelse return null; 436 const base: usize = if (self.data.dir.len != 0) 1 else 0; // skip the ".." row 437 if (idx < base) return null; 438 const e = idx - base; 439 if (e >= self.data.entries.len) return null; 440 return self.data.entries[e]; 441 } 442 443 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 444 self.clearGrid(); 445 446 // swap the detail pane to the selected entry when the selection changes. 447 try self.refreshDetail(allocator); 448 449 // the selected list row shows a border (the focused TextBox upgrades it 450 // to a double border itself); the rest stay borderless. 451 const lb = self.listBox(); 452 for (lb.children.keys(), lb.children.values()) |id, *child| { 453 switch (child.widget) { 454 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 455 else => {}, 456 } 457 } 458 459 // the content scroll's border is single normally and double when the 460 // content is focused (the content box itself is borderless). 461 const has_content = self.detailInner().children.count() > 0; 462 const content_focused = if (root_focus.grandchild_id) |g| self.detailInner().children.contains(g) else false; 463 self.detailScrollFrame().options.border_style = if (!has_content) null else if (content_focused) .double else .single; 464 465 // cap the list at list_max_width only while the detail pane fits beside 466 // it. the box drops the detail pane when the width can't hold both 467 // minimums, so when it's that narrow we lift the cap and let the list 468 // fill the whole width. 469 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 470 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 471 472 // stretch the detail pane and its bordered scroll frame across the rest 473 // of the width so the pane fills the area rather than shrinking to its 474 // content, like the refs view's columns. the content scroll inside keeps 475 // its natural width: forcing it to the full inner width would make it one 476 // column too wide whenever the vertical scrollbar appears, leaving a 477 // phantom horizontal scroll. (height fills via the scroll clipping to the 478 // viewport.) 479 if (constraint.max_size.width) |w| { 480 const detail_w = if (both_panes_fit) w - list_max_width else w; 481 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_w, .height = null }; 482 self.detailOuter().children.values()[detail_scroll_index].min_size = .{ .width = detail_w, .height = null }; 483 } else { 484 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 485 self.detailOuter().children.values()[detail_scroll_index].min_size = null; 486 } 487 488 // crossing panes only re-selects the content box's child (focusList / 489 // focusDetail); when the window is too narrow to show both, the pane that 490 // was holding focus is dropped here. the framework recovers focus after 491 // the top-level build by re-deriving it down the selected-child chain, so 492 // there's nothing to fix up afterward. 493 try self.box.build(allocator, constraint, root_focus); 494 495 // clamp the list's initial offset (a selected row near the end of the 496 // list) against this build's grids, for the next frame. initRoot's 497 // pre-sizing build is unbounded — its full-content "viewport" would 498 // clamp the offset to zero, so only a bounded build counts. 499 if (constraint.max_size.height != null) self.listScroll().clampToContent(); 500 } 501 502 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 503 const cur = self.selectedRowIndex(); 504 if (std.meta.eql(cur, self.shown_index)) return; 505 try self.populateDetail(allocator); 506 self.shown_index = cur; 507 } 508 509 fn populateDetail(self: *View, allocator: std.mem.Allocator) !void { 510 const nav_box = self.navBox(); 511 const inner = self.detailInner(); 512 513 for (nav_box.children.values()) |*child| child.widget.deinit(allocator); 514 nav_box.children.clearAndFree(allocator); 515 nav_box.getFocus().child_id = null; 516 for (inner.children.values()) |*child| child.widget.deinit(allocator); 517 inner.children.clearAndFree(allocator); 518 inner.getFocus().child_id = null; 519 520 // only files have contents to show; directories and the ".." row leave 521 // the pane empty. 522 if (self.selectedEntry()) |entry| { 523 if (!entry.is_dir) { 524 // the window links sit above the scroll, so they stay put 525 // while the content scrolls. 526 if (entry.window_start > 0) try self.addNavLink(allocator, nav_box, "scroll to top", entry, 0); 527 if (entry.has_more) try self.addNavLink(allocator, nav_box, "next lines →", entry, lineNumber(entry.window_start + file_page)); 528 if (!entry.loaded) { 529 // the placeholder carries the file's "a:" link, so enter 530 // (or a click) here loads the file like on its list row. 531 const path = try childDir(self.session.page_arena.allocator(), self.data.dir, entry.name); 532 const link = try fileLink(self.session.page_arena, self.data, path, false, 0); 533 try self.addContentBox(allocator, inner, "(press enter to load this file)", link); 534 } else if (entry.is_binary) { 535 try self.addContentBox(allocator, inner, "(binary file)", ""); 536 } else { 537 const text = try numberedContent(self.session.page_arena.allocator(), entry.lines, entry.window_start); 538 try self.addContentBox(allocator, inner, text, ""); 539 } 540 } 541 } 542 // point each box at its first child so focus can descend into it. 543 if (nav_box.children.count() > 0) nav_box.getFocus().child_id = nav_box.children.keys()[0]; 544 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 545 546 // reserve the nav row's height (the shrink scroll frame yields it). 547 self.detailOuter().children.values()[detail_nav_index].min_size = 548 if (nav_box.children.count() > 0) .{ .width = null, .height = row_height } else null; 549 550 // reset the scroll to the top for the newly-shown file: directly on the 551 // terminal (the wasm offset), and via a version bump on the web (so the 552 // renderer's scroll id changes and JS drops the preserved position). 553 const sc = self.detailScroll(); 554 sc.x = 0; 555 sc.y = 0; 556 sc.getFocus().version +%= 1; 557 } 558 559 // a focusable "next" link above the content. it's an `a:` link to the 560 // selected file at `target_start`, so activating it (the host follows the 561 // link) reloads the page on the next window. 562 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_start: usize) !void { 563 const page_arena = self.session.page_arena; 564 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 565 const route = self.data.location.filesRoute(self.data.ref_or_oid, self.data.ref_or_oid_value, path, target_start) orelse return error.RouteTooLong; 566 const link = try std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 567 var tb = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 568 errdefer tb.deinit(allocator); 569 tb.getFocus().mode = .all; 570 tb.getFocus().kind = .{ .custom = link }; 571 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 572 } 573 574 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 575 if (self.headerActive()) { 576 if (key == .arrow_down) { 577 root_focus.setFocus(self.contentBox().getFocus().id); 578 } else { 579 try self.header().input(allocator, key, root_focus); 580 } 581 return; 582 } 583 if (key == .arrow_up and self.contentAtTop() and self.header().focusCloneUrl(root_focus)) return; 584 if (self.detailActive()) { 585 try self.detailInput(key, root_focus); 586 } else { 587 try self.listInput(key, root_focus); 588 } 589 } 590 591 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 592 // up/down (and the scroll wheel) move the selection; page up/down jump a 593 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 594 // directory (or "..") row are turned into navigation by the host 595 // (crossPageLink) before reaching here. 596 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 597 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 598 return; 599 } 600 switch (key) { 601 .enter, .arrow_right => try self.focusDetail(root_focus), 602 else => {}, 603 } 604 } 605 606 // whether focus is on the content box (vs. a "previous"/"next" nav link). 607 fn focusOnContent(self: *View, root_focus: *Focus) bool { 608 const g = root_focus.grandchild_id orelse return false; 609 return self.detailInner().children.contains(g); 610 } 611 612 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 613 const sc = self.detailScroll(); 614 const on_content = self.focusOnContent(root_focus); 615 // the "next" link sits above the content scroll. on the link, left returns 616 // to the file list and down drops into the content. in the content, the 617 // terminal scrolls by offset and reaching the top then pressing up crosses 618 // back up to the link; on the web vertical scrolling is the browser's job 619 // so up just crosses to the link. 620 switch (key) { 621 .arrow_left => { 622 if (on_content) { 623 if (sc.x > 0) { 624 sc.x -= 1; 625 self.detailScroll().clampToContent(); 626 } else try self.focusList(root_focus); 627 } else if (!self.moveNav(root_focus, -1)) { 628 try self.focusList(root_focus); 629 } 630 }, 631 .arrow_right => { 632 if (on_content) { 633 sc.x += 1; 634 self.detailScroll().clampToContent(); 635 } else _ = self.moveNav(root_focus, 1); 636 }, 637 .arrow_up => { 638 if (on_content) { 639 if (self.session.is_terminal and sc.y > 0) { 640 sc.y -= 1; 641 self.detailScroll().clampToContent(); 642 } else self.focusNav(root_focus); // cross up to the links 643 } 644 }, 645 .arrow_down => { 646 if (on_content) { 647 if (self.session.is_terminal) { 648 sc.y += 1; 649 self.detailScroll().clampToContent(); 650 } 651 } else self.focusContent(root_focus); // links -> content 652 }, 653 .page_up => if (on_content and self.session.is_terminal) { 654 sc.y -= 10; 655 self.detailScroll().clampToContent(); 656 }, 657 .page_down => if (on_content and self.session.is_terminal) { 658 sc.y += 10; 659 self.detailScroll().clampToContent(); 660 }, 661 .home => if (on_content and self.session.is_terminal) { 662 sc.y = 0; 663 self.detailScroll().clampToContent(); 664 }, 665 .end => if (on_content and self.session.is_terminal) { 666 sc.y = std.math.maxInt(isize); 667 self.detailScroll().clampToContent(); 668 }, 669 .mouse => |mouse| switch (mouse.action) { 670 .scroll => |dir| if (on_content and self.session.is_terminal) { 671 sc.y += if (dir == .up) -5 else 5; 672 self.detailScroll().clampToContent(); 673 }, 674 else => {}, 675 }, 676 else => {}, 677 } 678 } 679 680 // focus the first (left-most) nav link; a no-op when there are none, leaving 681 // focus on the content. 682 fn focusNav(self: *View, root_focus: *Focus) void { 683 const nav_box = self.navBox(); 684 if (nav_box.children.count() == 0) return; 685 root_focus.setFocus(nav_box.children.keys()[0]); 686 } 687 688 fn focusContent(self: *View, root_focus: *Focus) void { 689 const inner = self.detailInner(); 690 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 691 root_focus.setFocus(id); 692 } 693 694 // move focus `delta` rows within the nav box; returns false (without moving) 695 // when that would step off either end. 696 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 697 const nav_box = self.navBox(); 698 const keys = nav_box.children.keys(); 699 const cur_id = nav_box.getFocus().child_id orelse return false; 700 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 701 const target = cur + delta; 702 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 703 root_focus.setFocus(keys[@intCast(target)]); 704 return true; 705 } 706 707 // enter the detail pane, landing on the "next" link when there is one (so 708 // right-arrow reaches it), else on the content. a directory or ".." row has 709 // neither, so there's nothing to enter. selecting the frame's child (then 710 // focusing the frame) also handles the too-narrow case where the pane isn't 711 // laid out yet — focus lands inside it after the next build. 712 fn focusDetail(self: *View, root_focus: *Focus) !void { 713 const frame = self.detailOuter(); 714 if (self.navBox().children.count() > 0) { 715 frame.getFocus().child_id = self.navBox().getFocus().id; 716 } else if (self.detailInner().children.count() > 0) { 717 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 718 } else return; 719 root_focus.setFocus(frame.getFocus().id); 720 } 721 722 // return to the list. 723 fn focusList(self: *View, root_focus: *Focus) !void { 724 root_focus.setFocus(self.listScroll().getFocus().id); 725 } 726 727 pub fn clearGrid(self: *View) void { 728 self.box.clearGrid(); 729 } 730 731 pub fn getGrid(self: View) ?Grid { 732 return self.box.getGrid(); 733 } 734 735 pub fn getFocus(self: *View) *Focus { 736 return self.box.getFocus(); 737 } 738 739 fn contentAtTop(self: *View) bool { 740 if (self.detailActive()) { 741 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 742 return self.navBox().children.count() == 0 and 743 (!self.session.is_terminal or self.detailScroll().y == 0); 744 } 745 const lb = self.listBox(); 746 const cid = lb.getFocus().child_id orelse return true; 747 return lb.children.getIndex(cid) == 0; 748 } 749 750 // only the clone-url row sits directly below the repository header. 751 pub fn getSelectedIndex(self: *View) ?usize { 752 if (self.headerActive()) return 0; 753 return if (self.header().hasCloneUrl() or !self.contentAtTop()) 1 else 0; 754 } 755 756 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 757 return self.header().focusCloneUrl(root_focus); 758 } 759 }; 760 761 // join `lines` into one string, each prefixed with its 1-based file line number 762 // (right-aligned in a column wide enough for the last number, then a space), 763 // like the diffs view. `start` is the window's 0-based first line, so numbers 764 // continue across paginated windows. 765 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 766 if (lines.len == 0) return ""; 767 const width = std.fmt.count("{d}", .{start + lines.len}); 768 const indent = try arena.alloc(u8, width); 769 @memset(indent, ' '); 770 771 var out: std.ArrayList(u8) = .empty; 772 for (lines, 0..) |line, i| { 773 if (i != 0) try out.append(arena, '\n'); 774 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 775 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 776 try out.appendSlice(arena, num_str); 777 try out.append(arena, ' '); 778 try out.appendSlice(arena, line); 779 } 780 return out.toOwnedSlice(arena); 781 } 782 783 // the index of the "README"/"README.md" file entry (case-insensitive), or null 784 // if none. used to pick the default selection at the repo root. 785 fn readmeIndex(entries: []const Entry) ?usize { 786 for (entries, 0..) |entry, i| { 787 if (!entry.is_dir and isReadme(entry.name)) return i; 788 } 789 return null; 790 } 791 792 fn isReadme(name: []const u8) bool { 793 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 794 } 795 796 // the index of the entry the route named (data.selected_file), or null if none. 797 fn selectedFileIndex(data: *const Self) ?usize { 798 const name = data.selected_file orelse return null; 799 for (data.entries, 0..) |entry, i| { 800 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 801 } 802 return null; 803 } 804 805 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 806 // following it navigates to that directory's listing. 807 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 808 const route = data.location.filesRoute(data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 809 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 810 } 811 812 // a link to the file's route. a preloaded file gets an "ai:" link 813 // (crossPageLink ignores it, so a click selects the row in place and shows its 814 // contents); an unloaded file gets an "a:" link, so activating it reloads the 815 // page with that file selected and its content read. 816 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 817 const route = data.location.filesRoute(data.ref_or_oid, data.ref_or_oid_value, path, line) orelse return error.RouteTooLong; 818 const prefix = if (loaded) "ai:" else "a:"; 819 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 820 } 821 822 // the url `line` value for a content window: its first line's 1-based number, 823 // except the first window, which serializes as unset (0). 824 fn lineNumber(window_start: usize) usize { 825 return if (window_start == 0) 0 else window_start + 1; 826 } 827 828 // `dir`/`name`, or just `name` at the root. 829 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 830 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 831 } 832 833 // `dir` with its last segment removed ("" when `dir` has a single segment). 834 fn parentDir(dir: []const u8) []const u8 { 835 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 836 return dir[0..slash]; 837 } 838 839 // the "<ref_or_oid> <value>" banner shown above the listing. 840 pub const Header = struct { 841 content: []const u8, 842 843 // `value` arrives url-encoded, so decode it for display. 844 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 845 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 846 return .{ 847 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 848 }; 849 } 850 851 pub const View = struct { 852 box: wgt.Box(ui.Widget), 853 data: *const Header, 854 855 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !Header.View { 856 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 857 errdefer box.deinit(allocator); 858 859 if (!session.data.is_local) { 860 if (try ui.widget.CopyableText.initClone(allocator, session, location)) |value| { 861 var clone_url = value; 862 errdefer clone_url.deinit(allocator); 863 const min_width = clone_url.minWidth(); 864 box.getFocus().child_id = clone_url.getFocus().id; 865 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 866 } 867 } 868 869 var spacer = try ui.widget.Spacer.init(allocator); 870 errdefer spacer.deinit(allocator); 871 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 872 873 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 874 errdefer text_box.deinit(allocator); 875 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 876 877 return .{ .box = box, .data = data }; 878 } 879 880 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 881 self.box.deinit(allocator); 882 } 883 884 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 885 self.clearGrid(); 886 try self.box.build(allocator, constraint, root_focus); 887 } 888 889 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 890 const clone_url = self.cloneUrl() orelse return; 891 try clone_url.input(allocator, key, root_focus); 892 } 893 894 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 895 const clone_url = self.cloneUrl() orelse return false; 896 root_focus.setFocus(clone_url.getFocus().id); 897 return true; 898 } 899 900 pub fn hasCloneUrl(self: *Header.View) bool { 901 return self.cloneUrl() != null; 902 } 903 904 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 905 for (self.box.children.values()) |*child| switch (child.widget) { 906 .copyable_text => |*copyable_text| return copyable_text, 907 else => {}, 908 }; 909 return null; 910 } 911 912 pub fn clearGrid(self: *Header.View) void { 913 self.box.clearGrid(); 914 } 915 916 pub fn getGrid(self: Header.View) ?Grid { 917 return self.box.getGrid(); 918 } 919 920 pub fn getFocus(self: *Header.View) *Focus { 921 return self.box.getFocus(); 922 } 923 }; 924 }; 925