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