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