╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
319 return .{ 320 .location = try location.dupe(aa), 321 .ref_or_oid = ref_or_oid, 322 .ref_or_oid_value = try aa.dupe(u8, ref_or_oid_value), 323 .dir = try aa.dupe(u8, dir), 324 .entries = &.{}, 325 }; 326 } 327 328 pub const View = struct { 329 // a vertical stack: the search box and clone url on top, then a horizontal 330 // split with the file/directory list on the left and a detail pane on the 331 // right showing the selected file's contents. 332 box: wgt.Box(ui.Widget), // vert: [header_index] = header row, [content_index] = split 333 data: *const Self, 334 session: *ui.Session, 335 // the list row whose contents the detail pane currently shows. 336 shown_index: ?usize, 337 338 // the sub-header leads the box where there is one; local mode has none 339 const header_index: usize = 0; 340 // indices within the content box (the horizontal split). 341 const list_index: usize = 0; 342 const detail_index: usize = 1; 343 // indices within the detail pane frame (nav box above the content scroll). 344 const detail_nav_index: usize = 0; 345 const detail_scroll_index: usize = 1; 346 const list_max_width: usize = 35; 347 const detail_min_width: usize = 40; 348 // a list row's height: one text line inside its (hidden) border. 349 const row_height: usize = 3; 350 351 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 352 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 353 errdefer outer.deinit(allocator); 354 355 // the search box and the clone url at the top. local mode has neither. 356 if (session.data.host_kind == .server) { 357 var header_view = try ui.widget.SearchHeader.init(allocator, session, data.location, " find file ", "find", data.find, data.find_available); 358 errdefer header_view.deinit(allocator); 359 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .search_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 360 } 361 362 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 363 errdefer box.deinit(allocator); 364 365 // the directory listing on the left (one focusable row each). 366 { 367 var list_scroll = blk: { 368 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 369 errdefer list_box.deinit(allocator); 370 371 // link kinds are borrowed by the rows, so they live in the page 372 // arena (as long as this page's widget tree). row labels are 373 // copied by their text boxes. 374 const aa = session.page_arena.allocator(); 375 if (data.dir.len != 0) { 376 try addRow(allocator, &list_box, "..", "", try dirLink(session.page_arena, data, parentDir(data.dir))); 377 } 378 for (data.entries) |entry| { 379 const path = try childDir(aa, data.dir, entry.name); 380 // a search result shows its file name, with its directory 381 // in the bottom label. 382 const label = if (data.find != null) 383 std.fs.path.basenamePosix(entry.name) 384 else if (entry.is_dir) 385 try std.fmt.allocPrint(aa, "{s}/", .{entry.name}) 386 else 387 entry.name; 388 const bottom_label = if (data.find != null) try resultDirLabel(aa, entry.name) else ""; 389 const link = if (entry.is_dir) 390 try dirLink(session.page_arena, data, path) 391 else 392 try fileLink(session.page_arena, data, path, entry.loaded, lineNumber(entry.window_start)); 393 try addRow(allocator, &list_box, label, bottom_label, link); 394 } 395 if (data.capped) try addRow(allocator, &list_box, "(list truncated)", "", ""); 396 // select the file the route named, else prefer a README so its 397 // contents greet the visitor in the detail pane, else the first 398 // row. a non-root directory has a leading ".." row, so the entry 399 // index is offset by one there. search results have no README 400 // default: the term decides what matters. 401 var sel: usize = 0; 402 if (list_box.children.count() > 0) { 403 const base: usize = if (data.dir.len != 0) 1 else 0; 404 const entry_idx = selectedFileIndex(data) orelse if (data.find == null) readmeIndex(data.entries) else null; 405 sel = if (entry_idx) |i| i + base else 0; 406 list_box.getFocus().child_id = list_box.children.keys()[sel]; 407 } 408 var sc = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 409 // start scrolled so the selected row begins at the top of the 410 // viewport. rows are a fixed height, so no layout pass is 411 // needed; a too-far offset near the end of the list is clamped 412 // in build (the browser clamps its own scrolling natively). 413 sc.y = @intCast(sel * row_height); 414 break :blk sc; 415 }; 416 errdefer list_scroll.deinit(allocator); 417 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 } }); 418 } 419 420 // the detail pane on the right — a "next" nav box above a scroll of the 421 // selected file's content window. both are repopulated per selection by 422 // populateDetail. 423 { 424 var detail_outer = blk: { 425 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 426 errdefer frame.deinit(allocator); 427 428 { 429 var nav_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 430 errdefer nav_box.deinit(allocator); 431 try frame.children.put(allocator, nav_box.getFocus().id, .{ .widget = .{ .box = nav_box }, .rect = null, .min_size = null }); 432 } 433 { 434 // the content scroll wrapped in a bordered box; build sets the 435 // border single normally and double when the content is focused 436 // (the content box itself is borderless). 437 var scroll_frame = blk2: { 438 var detail_scroll = blk3: { 439 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 440 errdefer detail_inner.deinit(allocator); 441 break :blk3 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }); 442 }; 443 errdefer detail_scroll.deinit(allocator); 444 var sf = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .single, .rounded_corners = true, .direction = .vert }); 445 errdefer sf.deinit(allocator); 446 // the wrapper's selected child is its scroll, so the focus 447 // chain reaches the content box (populateDetail points the 448 // scroll's inner box at it). 449 sf.getFocus().child_id = detail_scroll.getFocus().id; 450 try sf.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 451 break :blk2 sf; 452 }; 453 errdefer scroll_frame.deinit(allocator); 454 // entering the detail pane lands on the content scroll (focus 455 // recovery descends the frame's selected child). the nav links 456 // above are reached by scrolling to the top, then up. 457 frame.getFocus().child_id = scroll_frame.getFocus().id; 458 // shrink, so the fill scroll yields the nav box's row 459 // instead of consuming the frame's whole height. 460 try frame.children.put(allocator, scroll_frame.getFocus().id, .{ .widget = .{ .box = scroll_frame }, .rect = null, .min_size = null, .flex = .shrink }); 461 } 462 break :blk frame; 463 }; 464 errdefer detail_outer.deinit(allocator); 465 detail_outer.getFocus().mode = .mouse; 466 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 467 } 468 469 box.getFocus().child_id = box.children.keys()[list_index]; 470 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 471 472 // focus lives in the split, except that search results start in the 473 // search box so the term can be refined right away. 474 outer.getFocus().child_id = outer.children.keys()[if (data.find != null) header_index else outer.children.count() - 1]; 475 return .{ .box = outer, .data = data, .session = session, .shown_index = null }; 476 } 477 478 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, bottom_label: []const u8, link: []const u8) !void { 479 var row = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none, .bottom_label = bottom_label }); 480 errdefer row.deinit(allocator); 481 row.getFocus().mode = .all; 482 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 483 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 484 } 485 486 // the selected file's contents as a single focusable multi-line text box. 487 // it's borderless — the border lives on the surrounding scroll frame, which 488 // doubles when this content is focused. a non-empty `link` makes activating 489 // the box follow it, like addRow. 490 fn addContentBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 491 _ = self; 492 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = null, .wrap_kind = .none }); 493 errdefer tb.deinit(allocator); 494 tb.getFocus().mode = .all; 495 if (link.len != 0) tb.getFocus().kind = .{ .custom = link }; 496 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 497 } 498 499 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 500 self.box.deinit(allocator); 501 } 502 503 // the split is last, after the sub-header where there is one 504 fn contentIndex(self: *View) usize { 505 return self.box.children.count() - 1; 506 } 507 508 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 509 return &self.box.children.values()[self.contentIndex()].widget.box; 510 } 511 512 fn header(self: *View) ?*ui.widget.SearchHeader { 513 switch (self.box.children.values()[header_index].widget) { 514 .search_header => |*header_view| return header_view, 515 else => return null, 516 } 517 } 518 519 fn headerActive(self: *View) bool { 520 const header_view = self.header() orelse return false; 521 return self.box.getFocus().child_id == header_view.getFocus().id; 522 } 523 524 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 525 return &self.contentBox().children.values()[list_index].widget.scroll; 526 } 527 528 fn listBox(self: *View) *wgt.Box(ui.Widget) { 529 return &self.listScroll().child.box; 530 } 531 532 fn detailOuter(self: *View) *wgt.Box(ui.Widget) { 533 return &self.contentBox().children.values()[detail_index].widget.box; 534 } 535 536 fn navBox(self: *View) *wgt.Box(ui.Widget) { 537 return &self.detailOuter().children.values()[detail_nav_index].widget.box; 538 } 539 540 // the bordered box wrapping the content scroll (border toggles with focus). 541 fn detailScrollFrame(self: *View) *wgt.Box(ui.Widget) { 542 return &self.detailOuter().children.values()[detail_scroll_index].widget.box; 543 } 544 545 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 546 return &self.detailScrollFrame().children.values()[0].widget.scroll; 547 } 548 549 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 550 return &self.detailScroll().child.box; 551 } 552 553 fn detailActive(self: *View) bool { 554 const content = self.contentBox(); 555 const cid = content.getFocus().child_id orelse return false; 556 return content.children.getIndex(cid) == detail_index; 557 } 558 559 // the selected list row's index, or null when nothing is focused. 560 fn selectedRowIndex(self: *View) ?usize { 561 const lb = self.listBox(); 562 const cid = lb.getFocus().child_id orelse return null; 563 return lb.children.getIndex(cid); 564 } 565 566 // the entry the selected row points at, or null for the ".." row (which 567 // isn't an entry). directory rows return their entry too, though they carry 568 // no contents. 569 fn selectedEntry(self: *View) ?Entry { 570 const idx = self.selectedRowIndex() orelse return null; 571 const base: usize = if (self.data.dir.len != 0) 1 else 0; // skip the ".." row 572 if (idx < base) return null; 573 const e = idx - base; 574 if (e >= self.data.entries.len) return null; 575 return self.data.entries[e]; 576 } 577 578 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 579 self.clearGrid(); 580 581 // swap the detail pane to the selected entry when the selection changes. 582 try self.refreshDetail(allocator); 583 584 // the selected list row shows a border (the focused TextBox upgrades it 585 // to a double border itself); the rest stay borderless. 586 const lb = self.listBox(); 587 for (lb.children.keys(), lb.children.values()) |id, *child| { 588 switch (child.widget) { 589 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 590 else => {}, 591 } 592 } 593 594 // the content scroll's border is single normally and double when the 595 // content is focused (the content box itself is borderless). 596 const has_content = self.detailInner().children.count() > 0; 597 const content_focused = if (root_focus.grandchild_id) |g| self.detailInner().children.contains(g) else false; 598 self.detailScrollFrame().options.border_style = if (!has_content) null else if (content_focused) .double else .single; 599 600 // cap the list at list_max_width only while the detail pane fits beside 601 // it. the box drops the detail pane when the width can't hold both 602 // minimums, so when it's that narrow we lift the cap and let the list 603 // fill the whole width. 604 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 605 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 606 607 // stretch the detail pane and its bordered scroll frame across the rest 608 // of the width so the pane fills the area rather than shrinking to its 609 // content, like the refs view's columns. the content scroll inside keeps 610 // its natural width: forcing it to the full inner width would make it one 611 // column too wide whenever the vertical scrollbar appears, leaving a 612 // phantom horizontal scroll. (height fills via the scroll clipping to the 613 // viewport.) 614 if (constraint.max_size.width) |w| { 615 const detail_w = if (both_panes_fit) w - list_max_width else w; 616 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_w, .height = null }; 617 self.detailOuter().children.values()[detail_scroll_index].min_size = .{ .width = detail_w, .height = null }; 618 } else { 619 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 620 self.detailOuter().children.values()[detail_scroll_index].min_size = null; 621 } 622 623 // crossing panes only re-selects the content box's child (focusList / 624 // focusDetail); when the window is too narrow to show both, the pane that 625 // was holding focus is dropped here. the framework recovers focus after 626 // the top-level build by re-deriving it down the selected-child chain, so 627 // there's nothing to fix up afterward. 628 try self.box.build(allocator, constraint, root_focus); 629 630 // clamp the list's initial offset (a selected row near the end of the 631 // list) against this build's grids, for the next frame. initRoot's 632 // pre-sizing build is unbounded — its full-content "viewport" would 633 // clamp the offset to zero, so only a bounded build counts. 634 if (constraint.max_size.height != null) self.listScroll().clampToContent(); 635 } 636 637 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 638 const cur = self.selectedRowIndex(); 639 if (std.meta.eql(cur, self.shown_index)) return; 640 try self.populateDetail(allocator); 641 self.shown_index = cur; 642 } 643 644 fn populateDetail(self: *View, allocator: std.mem.Allocator) !void { 645 const nav_box = self.navBox(); 646 const inner = self.detailInner(); 647 648 for (nav_box.children.values()) |*child| child.widget.deinit(allocator); 649 nav_box.children.clearAndFree(allocator); 650 nav_box.getFocus().child_id = null; 651 for (inner.children.values()) |*child| child.widget.deinit(allocator); 652 inner.children.clearAndFree(allocator); 653 inner.getFocus().child_id = null; 654 655 // only files have contents to show; directories and the ".." row leave 656 // the pane empty. 657 if (self.selectedEntry()) |entry| { 658 if (!entry.is_dir) { 659 // the window links sit above the scroll, so they stay put 660 // while the content scrolls. 661 if (entry.window_start > 0) try self.addNavLink(allocator, nav_box, "scroll to top", entry, 0); 662 if (entry.has_more) try self.addNavLink(allocator, nav_box, "next lines →", entry, lineNumber(entry.window_start + file_page)); 663 if (!entry.loaded) { 664 // the placeholder carries the file's "a:" link, so enter 665 // (or a click) here loads the file like on its list row. 666 const path = try childDir(self.session.page_arena.allocator(), self.data.dir, entry.name); 667 const link = try fileLink(self.session.page_arena, self.data, path, false, 0); 668 try self.addContentBox(allocator, inner, "(press enter to load this file)", link); 669 } else if (entry.is_binary) { 670 try self.addContentBox(allocator, inner, "(binary file)", ""); 671 } else { 672 const text = try numberedContent(self.session.page_arena.allocator(), entry.lines, entry.window_start); 673 try self.addContentBox(allocator, inner, text, ""); 674 } 675 } 676 } 677 // point each box at its first child so focus can descend into it. 678 if (nav_box.children.count() > 0) nav_box.getFocus().child_id = nav_box.children.keys()[0]; 679 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 680 681 // reserve the nav row's height (the shrink scroll frame yields it). 682 self.detailOuter().children.values()[detail_nav_index].min_size = 683 if (nav_box.children.count() > 0) .{ .width = null, .height = row_height } else null; 684 685 // reset the scroll to the top for the newly-shown file: directly on the 686 // terminal (the wasm offset), and via a version bump on the web (so the 687 // renderer's scroll id changes and JS drops the preserved position). 688 const sc = self.detailScroll(); 689 sc.x = 0; 690 sc.y = 0; 691 sc.getFocus().version +%= 1; 692 } 693 694 // a focusable "next" link above the content. it's an `a:` link to the 695 // selected file at `target_start`, so activating it (the host follows the 696 // link) reloads the page on the next window. 697 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_start: usize) !void { 698 const page_arena = self.session.page_arena; 699 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 700 const route = self.data.filesRoute(path, target_start) orelse return error.RouteTooLong; 701 const link = try std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 702 var tb = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 703 errdefer tb.deinit(allocator); 704 tb.getFocus().mode = .all; 705 tb.getFocus().kind = .{ .custom = link }; 706 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 707 } 708 709 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 710 // scrolling crosses the sub header boundary the same way arrows do 711 const direction = inp.vertDirection(key); 712 if (self.headerActive()) { 713 // active means there is one 714 const header_view = self.header() orelse unreachable; 715 if (direction == .down) { 716 root_focus.setFocus(self.contentBox().getFocus().id); 717 return; 718 } 719 if (key == .enter) if (try header_view.submittedText(allocator)) |term| { 720 defer allocator.free(term); 721 return self.submit(term); 722 }; 723 return header_view.input(allocator, key, root_focus); 724 } 725 if (direction == .up and self.contentAtTop() and self.focusHeader(root_focus)) return; 726 if (self.detailActive()) { 727 try self.detailInput(key, root_focus); 728 } else { 729 try self.listInput(key, root_focus); 730 } 731 } 732 733 // navigate to the typed term's results, pinned to the listing's ref. an 734 // empty term leaves the results for the plain listing. 735 fn submit(self: *View, term: []const u8) !void { 736 if (term.len == 0 and self.data.find == null) return; 737 const route = self.data.filesRoute("", 0) orelse return; 738 try self.session.navigate(route.withFind(term) orelse return); 739 } 740 741 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 742 // up/down (and the scroll wheel) move the selection; page up/down jump a 743 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 744 // directory (or "..") row are turned into navigation by the host 745 // (crossPageLink) before reaching here. 746 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 747 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 748 return; 749 } 750 switch (key) { 751 .enter, .arrow_right => try self.focusDetail(root_focus), 752 else => {}, 753 } 754 } 755 756 // whether focus is on the content box (vs. a "previous"/"next" nav link). 757 fn focusOnContent(self: *View, root_focus: *Focus) bool { 758 const g = root_focus.grandchild_id orelse return false; 759 return self.detailInner().children.contains(g); 760 } 761 762 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 763 const sc = self.detailScroll(); 764 const on_content = self.focusOnContent(root_focus); 765 // the "next" link sits above the content scroll. on the link, left returns 766 // to the file list and down drops into the content. in the content, the 767 // terminal scrolls by offset and reaching the top then pressing up crosses 768 // back up to the link; on the web vertical scrolling is the browser's job 769 // so up just crosses to the link. 770 switch (key) { 771 .arrow_left => { 772 if (on_content) { 773 if (sc.x > 0) { 774 sc.x -= 1; 775 self.detailScroll().clampToContent(); 776 } else try self.focusList(root_focus); 777 } else if (!self.moveNav(root_focus, -1)) { 778 try self.focusList(root_focus); 779 } 780 }, 781 .arrow_right => { 782 if (on_content) { 783 sc.x += 1; 784 self.detailScroll().clampToContent(); 785 } else _ = self.moveNav(root_focus, 1); 786 }, 787 .arrow_up => { 788 if (on_content) { 789 if (self.session.is_terminal and sc.y > 0) { 790 sc.y -= 1; 791 self.detailScroll().clampToContent(); 792 } else self.focusNav(root_focus); // cross up to the links 793 } 794 }, 795 .arrow_down => { 796 if (on_content) { 797 if (self.session.is_terminal) { 798 sc.y += 1; 799 self.detailScroll().clampToContent(); 800 } 801 } else self.focusContent(root_focus); // links -> content 802 }, 803 .page_up => if (on_content and self.session.is_terminal) { 804 sc.y -= 10; 805 self.detailScroll().clampToContent(); 806 }, 807 .page_down => if (on_content and self.session.is_terminal) { 808 sc.y += 10; 809 self.detailScroll().clampToContent(); 810 }, 811 .home => if (on_content and self.session.is_terminal) { 812 sc.y = 0; 813 self.detailScroll().clampToContent(); 814 }, 815 .end => if (on_content and self.session.is_terminal) { 816 sc.y = std.math.maxInt(isize); 817 self.detailScroll().clampToContent(); 818 }, 819 .mouse => |mouse| switch (mouse.action) { 820 .scroll => |dir| if (on_content and self.session.is_terminal) { 821 sc.y += if (dir == .up) -5 else 5; 822 self.detailScroll().clampToContent(); 823 }, 824 else => {}, 825 }, 826 else => {}, 827 } 828 } 829 830 // focus the first (left-most) nav link; a no-op when there are none, leaving 831 // focus on the content. 832 fn focusNav(self: *View, root_focus: *Focus) void { 833 const nav_box = self.navBox(); 834 if (nav_box.children.count() == 0) return; 835 root_focus.setFocus(nav_box.children.keys()[0]); 836 } 837 838 fn focusContent(self: *View, root_focus: *Focus) void { 839 const inner = self.detailInner(); 840 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 841 root_focus.setFocus(id); 842 } 843 844 // move focus `delta` rows within the nav box; returns false (without moving) 845 // when that would step off either end. 846 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 847 const nav_box = self.navBox(); 848 const keys = nav_box.children.keys(); 849 const cur_id = nav_box.getFocus().child_id orelse return false; 850 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 851 const target = cur + delta; 852 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 853 root_focus.setFocus(keys[@intCast(target)]); 854 return true; 855 } 856 857 // enter the detail pane, landing on the "next" link when there is one (so 858 // right-arrow reaches it), else on the content. a directory or ".." row has 859 // neither, so there's nothing to enter. selecting the frame's child (then 860 // focusing the frame) also handles the too-narrow case where the pane isn't 861 // laid out yet — focus lands inside it after the next build. 862 fn focusDetail(self: *View, root_focus: *Focus) !void { 863 const frame = self.detailOuter(); 864 if (self.navBox().children.count() > 0) { 865 frame.getFocus().child_id = self.navBox().getFocus().id; 866 } else if (self.detailInner().children.count() > 0) { 867 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 868 } else return; 869 root_focus.setFocus(frame.getFocus().id); 870 } 871 872 // return to the list. 873 fn focusList(self: *View, root_focus: *Focus) !void { 874 root_focus.setFocus(self.listScroll().getFocus().id); 875 } 876 877 pub fn clearGrid(self: *View) void { 878 self.box.clearGrid(); 879 } 880 881 pub fn getGrid(self: View) ?Grid { 882 return self.box.getGrid(); 883 } 884 885 pub fn getFocus(self: *View) *Focus { 886 return self.box.getFocus(); 887 } 888 889 fn contentAtTop(self: *View) bool { 890 if (self.detailActive()) { 891 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 892 return self.navBox().children.count() == 0 and 893 (!self.session.is_terminal or self.detailScroll().y == 0); 894 } 895 const lb = self.listBox(); 896 const cid = lb.getFocus().child_id orelse return true; 897 return lb.children.getIndex(cid) == 0; 898 } 899 900 // only the header's own row sits directly below the repository header. 901 pub fn atTop(self: *View) bool { 902 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 903 return self.headerActive() or (!focusable and self.contentAtTop()); 904 } 905 906 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 907 const header_view = self.header() orelse return false; 908 return header_view.focusHeader(root_focus); 909 } 910 }; 911 912 // join `lines` into one string, each prefixed with its 1-based file line number 913 // (right-aligned in a column wide enough for the last number, then a space), 914 // like the diffs view. `start` is the window's 0-based first line, so numbers 915 // continue across paginated windows. 916 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 917 if (lines.len == 0) return ""; 918 const width = std.fmt.count("{d}", .{start + lines.len}); 919 const indent = try arena.alloc(u8, width); 920 @memset(indent, ' '); 921 922 var out: std.ArrayList(u8) = .empty; 923 for (lines, 0..) |line, i| { 924 if (i != 0) try out.append(arena, '\n'); 925 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 926 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 927 try out.appendSlice(arena, num_str); 928 try out.append(arena, ' '); 929 try out.appendSlice(arena, line); 930 } 931 return out.toOwnedSlice(arena); 932 } 933 934 // the index of the "README"/"README.md" file entry (case-insensitive), or null 935 // if none. used to pick the default selection at the repo root. 936 fn readmeIndex(entries: []const Entry) ?usize { 937 for (entries, 0..) |entry, i| { 938 if (!entry.is_dir and isReadme(entry.name)) return i; 939 } 940 return null; 941 } 942 943 fn isReadme(name: []const u8) bool { 944 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 945 } 946 947 // the index of the entry the route named (data.selected_file), or null if none. 948 fn selectedFileIndex(data: *const Self) ?usize { 949 const name = data.selected_file orelse return null; 950 for (data.entries, 0..) |entry, i| { 951 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 952 } 953 return null; 954 } 955 956 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 957 // following it navigates to that directory's listing. 958 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 959 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 960 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 961 } 962 963 // a link to the file's route. a preloaded file gets an "ai:" link 964 // (crossPageLink ignores it, so a click selects the row in place and shows its 965 // contents); an unloaded file gets an "a:" link, so activating it reloads the 966 // page with that file selected and its content read. 967 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 968 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 969 const prefix = if (loaded) "ai:" else "a:"; 970 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 971 } 972 973 // the url `line` value for a content window: its first line's 1-based number, 974 // except the first window, which serializes as unset (0). 975 fn lineNumber(window_start: usize) usize { 976 return if (window_start == 0) 0 else window_start + 1; 977 } 978 979 // `dir`/`name`, or just `name` at the root. 980 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 981 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 982 } 983 984 // `dir` with its last segment removed ("" when `dir` has a single segment). 985 fn parentDir(dir: []const u8) []const u8 { 986 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 987 return dir[0..slash]; 988 } 989 990 // a search result's bottom label: the directory it sits in, empty at the root. 991 // a directory too wide for the list column is clipped from the left. 992 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 993 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 994 const dir = path[0..slash]; 995 // the row's border and the label's padding take two columns each 996 const max_width = View.list_max_width - 4; 997 var width = try xitui.width.displayWidth(dir); 998 if (width <= max_width) return std.fmt.allocPrint(aa, " {s} ", .{dir}); 999 1000 const ellipsis = ".."; 1001 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1002 var start: usize = 0; 1003 while (width > max_width - ellipsis.len) { 1004 const slice = iter.nextCodepointSlice() orelse break; 1005 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1006 start += slice.len; 1007 } 1008 return std.fmt.allocPrint(aa, " " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1009 } 1010