╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
317 else 318 message.items; 319 return .{ std.mem.trim(u8, text, " \t\r\n"), byte_truncated or preview_end != null }; 320 } 321 322 /// drop any incomplete UTF data from the end of a byte array 323 fn trimIncompleteCodepoint(bytes: []const u8) []const u8 { 324 var i = bytes.len; 325 while (i > 0 and bytes.len - i < 4) { 326 i -= 1; 327 const byte = bytes[i]; 328 if (byte & 0b1100_0000 == 0b1000_0000) continue; // continuation byte 329 const seq_len = std.unicode.utf8ByteSequenceLength(byte) catch return bytes; 330 return if (i + seq_len > bytes.len) bytes[0..i] else bytes; 331 } 332 return bytes; 333 } 334 335 // a focusable, word-wrapping box holding a commit message. `bottom_label` 336 // marks a message the page only shows part of ("" when it's whole). 337 fn messageBox(allocator: std.mem.Allocator, message: []const u8, bottom_label: []const u8) !wgt.TextBox { 338 var tb = try wgt.TextBox.init(allocator, message, .{ 339 .border = .single, 340 .round_corners = true, 341 .wrap_kind = .word, 342 .top_label = .{ .text = " message " }, 343 .bottom_label = .{ .text = bottom_label }, 344 .detect_links = true, 345 }); 346 tb.getFocus().mode = .all; 347 return tb; 348 } 349 350 // the first line of a commit message, for the one-row list entries. 351 fn firstLine(message: []const u8) []const u8 { 352 const nl = std.mem.indexOfScalar(u8, message, '\n'); 353 return if (nl) |i| std.mem.trimEnd(u8, message[0..i], " \t\r") else message; 354 } 355 356 pub const View = struct { 357 // a vertical stack: the clone url row on top, then a horizontal 358 // split with the commit list on the left and a pane on the right 359 // showing the selected commit's details. 360 box: wgt.Box(ui.Widget), // vert: [header_index] = clone url row, [content_index] = split 361 data: *const Self, 362 session: *ui.Session, 363 // what the pane was last filled for: the commit (index into data.commits), 364 // and whether the window was too narrow to show the list beside it 365 shown: ?Shown = null, 366 // the detail's way back to a list that isn't shown beside it 367 back_id: ?usize = null, 368 369 const Shown = struct { index: usize, narrow: bool }; 370 371 // the sub-header leads the box where there is one; local mode has none 372 const header_index: usize = 0; 373 // indices within the content box (the horizontal split). 374 const list_index: usize = 0; 375 const detail_index: usize = 1; 376 const list_max_width: usize = 35; 377 const detail_min_width: usize = 40; 378 379 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 380 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 381 errdefer outer.deinit(allocator); 382 383 if (data.no_commits) { 384 var center = try ui.widget.initNoCommits(allocator, session, data.handle); 385 errdefer center.deinit(allocator); 386 outer.getFocus().child_id = center.getFocus().id; 387 try outer.children.put(allocator, center.getFocus().id, .{ .widget = .{ .center = center }, .rect = null, .min_size = null }); 388 return .{ .box = outer, .data = data, .session = session }; 389 } 390 391 // the search box and the clone url at the top. local mode has neither. 392 if (session.data.host_kind == .server) { 393 var header_view = try ui.widget.SearchHeader.init(allocator, session, data.handle, " search ", "search", data.search, data.search_available); 394 errdefer header_view.deinit(allocator); 395 // a row taller than the header, leaving a blank line beneath it 396 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .search_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 4 } }); 397 } 398 399 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 400 errdefer box.deinit(allocator); 401 402 // the commit list (one focusable row each), plus a "next" link 403 { 404 var list_scroll = blk: { 405 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 406 errdefer list_box.deinit(allocator); 407 for (data.commits, 0..) |commit, index| { 408 // an in-page "ai:" anchor so a commit row is clickable with 409 // js off (the browser follows it, rooting the list there); 410 // with wasm the click just selects it and swaps the pane. 411 try addRow(allocator, &list_box, firstLine(commit.message), try commitRowLink(session.page_arena, data, commit, index == 0 and data.message), if (commit.merge) "(merge)" else ""); 412 } 413 if (data.next_start) |next| { 414 try addRow(allocator, &list_box, "next →", try nextPageLink(session.page_arena, data, next), ""); 415 } 416 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 417 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 418 }; 419 errdefer list_scroll.deinit(allocator); 420 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 } }); 421 } 422 423 // the detail pane: a frame around a scroll of its rows 424 { 425 var detail_outer = blk: { 426 var detail_scroll = blk2: { 427 var rows = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 428 errdefer rows.deinit(allocator); 429 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = rows }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 430 }; 431 errdefer detail_scroll.deinit(allocator); 432 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border = .hidden, .direction = .vert }); 433 errdefer frame.deinit(allocator); 434 // the frame's selected child is its scroll, so the focus chain 435 // reaches a row (populateDetail points the scroll's inner box at 436 // one), letting focus recovery descend into the pane after 437 // it's laid out beside a too-narrow list. 438 frame.getFocus().child_id = detail_scroll.getFocus().id; 439 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 440 break :blk frame; 441 }; 442 errdefer detail_outer.deinit(allocator); 443 detail_outer.getFocus().mode = .mouse; 444 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 445 } 446 447 // the page opens on the list, unless the url names a commit 448 box.getFocus().child_id = box.children.keys()[if (data.ref_or_oid == .object) detail_index else list_index]; 449 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 450 451 // focus lives in the split, except that search results start in the 452 // search box so the query can be refined right away. 453 outer.getFocus().child_id = outer.children.keys()[if (data.search != null) header_index else outer.children.count() - 1]; 454 455 return .{ .box = outer, .data = data, .session = session }; 456 } 457 458 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8, bottom_label: []const u8) !void { 459 var row = try wgt.TextBox.init(allocator, label, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = bottom_label } }); 460 errdefer row.deinit(allocator); 461 row.getFocus().mode = .all; 462 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 463 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 464 } 465 466 // a focusable row following `link`, returning its focus id 467 fn addLink(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !usize { 468 var tb = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 469 errdefer tb.deinit(allocator); 470 tb.getFocus().mode = .all; 471 tb.getFocus().kind = .{ .custom = link }; 472 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 473 return tb.getFocus().id; 474 } 475 476 // how much of the message a pane's box holds: a truncated preview links to 477 // the pane holding the whole thing, which is that link's destination. 478 const MessageBox = enum { preview, whole }; 479 480 // the selected commit's message. 481 fn addMessageBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), commit: Commit, kind: MessageBox) !void { 482 // a truncated message keeps its box even with no whole line to show, 483 // since the box is what links to the whole thing. 484 if (commit.message.len == 0 and !commit.message_truncated) return; 485 const cut_short = commit.message_truncated and kind == .preview; 486 var tb = try messageBox(allocator, commit.message, if (cut_short) " click or press enter to see more " else ""); 487 errdefer tb.deinit(allocator); 488 if (cut_short) tb.getFocus().kind = .{ .custom = try messageLink(self.session.page_arena, self.data, commit.oid) }; 489 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 490 } 491 492 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 493 self.box.deinit(allocator); 494 } 495 496 // the split is last, after the sub-header where there is one 497 fn contentIndex(self: *View) usize { 498 return self.box.children.count() - 1; 499 } 500 501 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 502 return &self.box.children.values()[self.contentIndex()].widget.box; 503 } 504 505 fn header(self: *View) ?*ui.widget.SearchHeader { 506 switch (self.box.children.values()[header_index].widget) { 507 .search_header => |*header_view| return header_view, 508 else => return null, 509 } 510 } 511 512 fn headerActive(self: *View) bool { 513 const header_view = self.header() orelse return false; 514 return self.box.getFocus().child_id == header_view.getFocus().id; 515 } 516 517 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 518 return &self.contentBox().children.values()[list_index].widget.scroll; 519 } 520 521 fn listBox(self: *View) *wgt.Box(ui.Widget) { 522 return &self.listScroll().child.box; 523 } 524 525 fn detailOuter(self: *View) *wgt.Box(ui.Widget) { 526 return &self.contentBox().children.values()[detail_index].widget.box; 527 } 528 529 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 530 return &self.detailOuter().children.values()[0].widget.scroll; 531 } 532 533 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 534 return &self.detailScroll().child.box; 535 } 536 537 fn detailActive(self: *View) bool { 538 const content = self.contentBox(); 539 const cid = content.getFocus().child_id orelse return false; 540 return content.children.getIndex(cid) == detail_index; 541 } 542 543 // the selected commit's index, or null when the "next" row is selected. 544 fn selectedCommitIndex(self: *View) ?usize { 545 const lb = self.listBox(); 546 const cid = lb.getFocus().child_id orelse return null; 547 const idx = lb.children.getIndex(cid) orelse return null; 548 if (idx >= self.data.commits.len) return null; 549 return idx; 550 } 551 552 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 553 self.clearGrid(); 554 if (self.data.no_commits) return self.box.build(allocator, constraint, root_focus); 555 556 // whether the detail pane fits beside the list, which decides the list 557 // cap below and whether the detail carries a way back to the list 558 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 559 560 // swap the detail pane to the selected commit when it changes. 561 try self.refreshDetail(allocator, !both_panes_fit); 562 563 // the selected list row shows a border (the focused TextBox upgrades it 564 // to a double border itself); the rest stay borderless. 565 const lb = self.listBox(); 566 for (lb.children.keys(), lb.children.values()) |id, *child| { 567 switch (child.widget) { 568 .text_box => |*tb| ui.widget.markSelected(tb, lb.getFocus().child_id == id), 569 else => {}, 570 } 571 } 572 573 // cap the list at list_max_width only while the detail pane fits beside it. 574 // the box drops the pane when the width can't hold both minimums, so when 575 // it's that narrow we lift the cap and let the list fill the whole width. 576 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 577 578 // stretch the detail pane across the rest of the width so it fills the area 579 // rather than shrinking to its content; its scroll fills the pane. when 580 // too narrow for both, it fills the whole width. 581 const detail_width: usize = if (constraint.max_size.width) |w| 582 (if (both_panes_fit) w - list_max_width else w) 583 else 584 detail_min_width; 585 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 586 587 // the web bounds the layout to the browser viewport like the terminal; 588 // each Scroll's web-native mode hands its full content to a real 589 // scrollable element, so we no longer build unbounded here. 590 // 591 // crossing panes only re-selects the content box's child (focusDetail / 592 // focusList); when the window is too narrow to show both, the pane that 593 // held focus is dropped here. the framework recovers focus after the 594 // top-level build by re-deriving it down the selected-child chain, so 595 // there's nothing to fix up afterward. 596 try self.box.build(allocator, constraint, root_focus); 597 } 598 599 fn refreshDetail(self: *View, allocator: std.mem.Allocator, narrow: bool) !void { 600 const next: Shown = .{ .index = self.selectedCommitIndex() orelse return, .narrow = narrow }; 601 if (self.shown) |shown| if (std.meta.eql(shown, next)) return; 602 try self.populateDetail(allocator, next); 603 self.shown = next; 604 } 605 606 fn populateDetail(self: *View, allocator: std.mem.Allocator, next: Shown) !void { 607 const sel = next.index; 608 const commit = self.data.commits[sel]; 609 const inner = self.detailInner(); 610 611 for (inner.children.values()) |*child| child.widget.deinit(allocator); 612 inner.children.clearAndFree(allocator); 613 inner.getFocus().child_id = null; 614 self.back_id = null; 615 616 // with the list hidden beside it, the detail leads with the way back. 617 // an in-page link to this very page, so both hosts hand its press and 618 // enter to input instead of navigating. 619 if (next.narrow) { 620 const pa = self.session.page_arena; 621 const link = try pa.allocator().print("ai:{s}", .{try self.session.data.current_page.toUrl(pa)}); 622 self.back_id = try addLink(allocator, inner, "← back to commit list", link); 623 } 624 625 if (sel == 0 and self.data.message) { 626 _ = try addLink(allocator, inner, "← back to commit", try commitsLink(self.session.page_arena, self.data, commit.oid)); 627 try self.addMessageBox(allocator, inner, commit, .whole); 628 } else { 629 const pa = self.session.page_arena; 630 { 631 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 632 errdefer row.deinit(allocator); 633 const diff_route = self.data.handle.location.commitDiffRoute(commit.oid, commit.parent_oid) orelse return error.RouteTooLong; 634 _ = try addLink(allocator, &row, "view diff", try pa.allocator().print("a:{s}", .{try diff_route.toUrl(pa)})); 635 _ = try addLink(allocator, &row, "view files at this commit", try filesObjectLink(pa, self.data.handle.location, commit.oid)); 636 row.getFocus().child_id = row.children.keys()[0]; 637 try inner.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 638 } 639 if (commit.merge) if (self.data.handle.location.commitsMergeRoute(commit.oid)) |route| { 640 _ = try addLink(allocator, inner, "view commits from this merge", try pa.allocator().print("a:{s}", .{try route.toUrl(pa)})); 641 }; 642 { 643 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 644 errdefer row.deinit(allocator); 645 if (commit.author != .unknown) { 646 var tb = try ui.authorBox(allocator, pa, commit.author); 647 errdefer tb.deinit(allocator); 648 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 649 } 650 if (commit.committer != .unknown) { 651 var tb = try ui.authorBox(allocator, pa, commit.committer); 652 errdefer tb.deinit(allocator); 653 tb.options.top_label.text = " committer "; 654 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 655 } 656 const timestamp = try ui.timestamp.format(pa.allocator(), commit.timestamp, try self.session.nowSeconds()); 657 var tb = try wgt.TextBox.init(allocator, timestamp.relative, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = timestamp.exact } }); 658 errdefer tb.deinit(allocator); 659 tb.getFocus().mode = .all; 660 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 661 row.getFocus().child_id = row.children.keys()[0]; 662 try inner.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 663 } 664 if (commit.stats) |stats| { 665 var text: std.Io.Writer.Allocating = .init(allocator); 666 defer text.deinit(); 667 try text.writer.print("lines changed: {d}", .{stats.lines_added + stats.lines_changed}); 668 if (stats.lines_removed != 0) try text.writer.print("\nlines removed: {d}", .{stats.lines_removed}); 669 const bytes_added = stats.bytes_added >= stats.bytes_removed; 670 const bytes = if (bytes_added) stats.bytes_added - stats.bytes_removed else stats.bytes_removed - stats.bytes_added; 671 try text.writer.print("\nbytes {s}: {d}\nfiles changed: {d}", .{ 672 if (bytes_added) "added" else "removed", bytes, stats.files_added + stats.files_changed, 673 }); 674 if (stats.files_removed != 0) try text.writer.print("\nfiles removed: {d}", .{stats.files_removed}); 675 var tb = try wgt.TextBox.init(allocator, text.written(), .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " stats " } }); 676 errdefer tb.deinit(allocator); 677 tb.getFocus().mode = .all; 678 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 679 } 680 try self.addMessageBox(allocator, inner, commit, .preview); 681 } 682 683 // point the pane at its first row so focus recovery can land here. 684 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 685 686 // reset the scroll to the top for the newly-shown commit: directly on the 687 // terminal (the wasm offset), and via a version bump on the web (so the 688 // renderer's scroll id changes and JS drops the preserved position). 689 const sc = self.detailScroll(); 690 sc.x = 0; 691 sc.y = 0; 692 sc.getFocus().version +%= 1; 693 } 694 695 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 696 if (self.data.no_commits) return self.box.input(allocator, key, root_focus); 697 // scrolling crosses the sub header boundary the same way arrows do 698 const direction = inp.vertDirection(key); 699 if (self.headerActive()) { 700 // active means there is one 701 const header_view = self.header() orelse unreachable; 702 if (direction == .down) { 703 root_focus.setFocus(self.contentBox().getFocus().id); 704 return; 705 } 706 if (key == .enter) if (try header_view.submittedText(allocator)) |text| { 707 defer allocator.free(text); 708 return self.submit(text); 709 }; 710 return header_view.input(allocator, key, root_focus); 711 } 712 if (direction == .up and self.contentAtTop() and self.focusHeader(root_focus)) return; 713 if (self.detailActive()) { 714 // the way back to the list, by enter or a click 715 if (self.back_id) |id| { 716 if (inp.activated(root_focus, id, key)) return self.focusList(root_focus); 717 } 718 const inner = self.detailInner(); 719 if (inp.rowDelta(key, @intCast(inner.children.count()))) |delta| { 720 ui.widget.moveRowFocus(inner, self.detailScroll(), root_focus, delta); 721 } else if (key == .arrow_left) { 722 if (!ui.widget.moveInSelectedRow(inner, root_focus, false)) self.focusList(root_focus); 723 } else if (key == .arrow_right) { 724 _ = ui.widget.moveInSelectedRow(inner, root_focus, true); 725 } 726 } else { 727 try self.listInput(allocator, key, root_focus); 728 } 729 } 730 731 // navigate to the typed query's results, pinned to the list's ref. an 732 // empty query leaves the results for the plain log. 733 fn submit(self: *View, text: []const u8) !void { 734 if (text.len == 0 and self.data.search == null) return; 735 // an object view or a base-bounded list searches the default branch, so the url says so 736 const route = if (self.data.default_branch.len != 0) 737 self.data.handle.location.commitsRoute(.branch, self.data.default_branch, "") 738 else 739 self.data.handle.location.commitsRoute(self.data.ref_or_oid, self.data.ref_or_oid_value, self.data.base_oid); 740 try self.session.navigate((route orelse return).withSearch(text) orelse return); 741 } 742 743 fn listInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 744 // up/down (and the scroll wheel) move the selection a row; page up/down 745 // jump a fixed amount. right/Enter cross into the detail pane. Enter/clicks 746 // on the "next" row become navigation in the host before reaching here. 747 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 748 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 749 return; 750 } 751 switch (key) { 752 .enter => if (self.selectedCommitIndex() != null) 753 self.focusDetail(root_focus) 754 else if (self.data.next_start) |next| { 755 if (pageRoute(self.data, next)) |route| try self.session.navigate(route); 756 }, 757 .arrow_right => self.focusDetail(root_focus), 758 // when the window is too narrow to lay out the detail pane beside 759 // the list, a click on a row opens it like enter. the row was just 760 // selected, so the detail is swapped to it ahead of the build. 761 .mouse => |mouse| if (self.contentBox().children.values()[detail_index].rect == null and ui.widget.clickOnSelectedRow(self.listBox(), root_focus, mouse)) { 762 try self.refreshDetail(allocator, true); 763 self.focusDetail(root_focus); 764 }, 765 else => {}, 766 } 767 } 768 769 // enter the detail pane. the host arrives here on right-arrow or Enter from the 770 // list. a pane with no rows can't be entered. setFocus handles the too-narrow 771 // case where the pane isn't laid out yet (it gets selected, then focused after 772 // the next build, landing on its remembered row). 773 fn focusDetail(self: *View, root_focus: *Focus) void { 774 if (self.detailInner().children.count() == 0) return; 775 root_focus.setFocus(self.detailOuter().getFocus().id); 776 } 777 778 // return to the list. 779 fn focusList(self: *View, root_focus: *Focus) void { 780 root_focus.setFocus(self.listScroll().getFocus().id); 781 } 782 783 pub fn clearGrid(self: *View) void { 784 self.box.clearGrid(); 785 } 786 787 pub fn getGrid(self: View) ?Grid { 788 return self.box.getGrid(); 789 } 790 791 pub fn getFocus(self: *View) *Focus { 792 return self.box.getFocus(); 793 } 794 795 fn contentAtTop(self: *View) bool { 796 if (self.detailActive()) { 797 const inner = self.detailInner(); 798 const cid = inner.getFocus().child_id orelse return true; 799 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 800 return first_focused and self.detailScroll().y == 0; 801 } 802 const lb = self.listBox(); 803 const cid = lb.getFocus().child_id orelse return true; 804 return lb.children.getIndex(cid) == 0; 805 } 806 807 // only the header's own row sits directly below the repository header. 808 pub fn atTop(self: *View) bool { 809 if (self.data.no_commits) return true; 810 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 811 return self.headerActive() or (!focusable and self.contentAtTop()); 812 } 813 814 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 815 const header_view = self.header() orelse return false; 816 return header_view.focusHeader(root_focus); 817 } 818 }; 819 820 // the "a:" navigation link for the commits page walking from commit `oid` within 821 // `data.handle.location`. 822 fn commitsLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 823 const route = data.handle.location.commitsRoute(.object, oid, data.base_oid) orelse return error.RouteTooLong; 824 const url = try route.toUrl(page_arena); 825 return page_arena.allocator().print("a:{s}", .{url}); 826 } 827 828 // this list's page starting at `oid`: a repo's branch or tag keeps its ref (and 829 // its query) and moves `from` along, so the search box survives paging. a fork 830 // or an object view roots the page at the commit itself. 831 fn pageRoute(data: *const Self, oid: []const u8) ?ui.RoutablePage { 832 if (data.handle.location != .repo or data.ref_or_oid == .object) { 833 return data.handle.location.commitsRoute(.object, oid, data.base_oid); 834 } 835 const route = data.handle.location.commitsRoute(data.ref_or_oid, data.ref_or_oid_value, data.base_oid) orelse return null; 836 const paged = route.withFrom(oid) orelse return null; 837 return paged.withSearch(data.search orelse return paged); 838 } 839 840 fn nextPageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 841 const route = pageRoute(data, oid) orelse return error.RouteTooLong; 842 const url = try route.toUrl(page_arena); 843 return page_arena.allocator().print("a:{s}", .{url}); 844 } 845 846 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 847 // directory, within `location`. 848 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 849 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 850 const url = try route.toUrl(page_arena); 851 return page_arena.allocator().print("a:{s}", .{url}); 852 } 853 854 // the in-page "ai:" anchor for selecting a commit, showing its whole message 855 // when `message` is set. the href is only followed with js off. 856 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, commit: Commit, message: bool) ![]const u8 { 857 // a result roots the same results page at its commit, so the query 858 // survives following the row 859 if (data.search != null) { 860 const route = pageRoute(data, commit.oid) orelse return error.RouteTooLong; 861 return page_arena.allocator().print("ai:{s}", .{try route.toUrl(page_arena)}); 862 } 863 const route = (if (message) 864 data.handle.location.commitMessageRoute(.object, commit.oid, data.base_oid) 865 else 866 data.handle.location.commitsRoute(.object, commit.oid, data.base_oid)) orelse return error.RouteTooLong; 867 const url = try route.toUrl(page_arena); 868 return page_arena.allocator().print("ai:{s}", .{url}); 869 } 870 871 // the "a:" link to the page showing `oid`'s message on its own. 872 fn messageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 873 const route = data.handle.location.commitMessageRoute(.object, oid, data.base_oid) orelse return error.RouteTooLong; 874 const url = try route.toUrl(page_arena); 875 return page_arena.allocator().print("a:{s}", .{url}); 876 } 877