╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Undo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
519 520 // the fixed rows are filled first, since adding the ones below 521 // invalidates every pointer into the box 522 { 523 const button = &details.children.values()[0].widget.text_box; 524 try button.setContent(allocator, if (item.index == 0) "initial state cannot be undone" else if (item.index == self.data.count - 1) "undo this" else "undo this and all above it"); 525 button.getFocus().kind = if (enabled) .{ .custom = "submit" } else .text_box; 526 } 527 const timestamp = try ui.timestamp.format(self.session.page_arena.allocator(), item.timestamp, try self.session.nowSeconds()); 528 try details.children.values()[1].widget.text_box.setContent(allocator, timestamp.relative); 529 details.children.values()[1].widget.text_box.options.bottom_label.text = timestamp.exact; 530 531 // some actions put buttons where the description would go 532 if (item.buttons.len == 0) { 533 try addLabeled(allocator, details, " description ", item.description, null); 534 } else for (item.buttons) |detail_button| { 535 try addLabeled(allocator, details, detail_button.label, detail_button.text, detail_button.link); 536 } 537 if (failure) |message| try addText(allocator, details, try self.session.page_arena.allocator().print("error: {s}", .{message}), null, .single); 538 self.shown_failure = failure; 539 details.getFocus().kind = if (enabled) .{ .custom = item.form } else .container; 540 details.getFocus().child_id = details.children.keys()[0]; 541 self.detailScroll().x = 0; 542 self.detailScroll().y = 0; 543 self.detailScroll().getFocus().version +%= 1; 544 ui.widget.setListBackLink(self.detailFrame(), item.link); 545 self.detailed_index = index; 546 } 547 548 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 549 self.clearGrid(); 550 // the empty message stands in for the list 551 if (self.data.isEmpty(self.session)) return try self.box.build(allocator, constraint, root_focus); 552 try self.refreshDetail(allocator); 553 const rows = &self.listScroll().child.box; 554 if (self.data.items.len == 0) { 555 // an empty list only shows to report its failure 556 const failure = self.session.data.undo_failure orelse self.data.failure orelse unreachable; 557 if (!std.meta.eql(self.shown_failure, failure)) { 558 try rows.children.values()[0].widget.text_box.setContent(allocator, try self.session.page_arena.allocator().print("error: {s}", .{failure})); 559 self.shown_failure = failure; 560 } 561 } 562 for (rows.children.keys(), rows.children.values()) |id, *child| ui.widget.markSelected(&child.widget.text_box, rows.getFocus().child_id == id); 563 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 564 ui.widget.showListBack(self.detailFrame(), !both_fit); 565 self.contentBox().children.values()[0].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 566 const width = if (constraint.max_size.width) |value| if (both_fit) value - list_max_width else value else detail_min_width; 567 self.contentBox().children.values()[1].min_size = .{ .width = width, .height = null }; 568 for (self.detailScroll().child.box.children.values()) |*child| child.max_size = .{ .width = width -| 2, .height = null }; 569 try self.box.build(allocator, constraint, root_focus); 570 } 571 572 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 573 // scrolling crosses the sub header boundary the same way the arrow keys do 574 const direction = inp.vertDirection(key); 575 if (self.headerActive()) { 576 // active means there is one 577 const copyable_text = self.header() orelse unreachable; 578 if (direction == .down) { 579 // the empty message has nothing to focus 580 if (!self.data.isEmpty(self.session)) root_focus.setFocus(self.contentBox().getFocus().id); 581 return; 582 } 583 return copyable_text.input(allocator, key, root_focus); 584 } 585 // the empty message has nothing to focus 586 if (self.data.isEmpty(self.session)) return; 587 // the way back to the list, by enter or a click 588 if (self.detailActive()) if (ui.widget.listBackId(self.detailFrame())) |id| { 589 if (inp.activated(root_focus, id, key)) return self.focusList(root_focus); 590 if (root_focus.grandchild_id == id) return switch (direction) { 591 // without a sub header, atTop hands it to the page's tabs 592 .up => _ = self.focusHeader(root_focus), 593 .down => root_focus.setFocus(self.detailScroll().getFocus().id), 594 .none => if (key == .arrow_left) self.focusList(root_focus), 595 }; 596 if (direction == .up and self.contentAtTop()) return root_focus.setFocus(id); 597 }; 598 if (direction == .up and self.contentAtTop() and self.focusHeader(root_focus)) return; 599 try self.refreshDetail(allocator); 600 if (self.detailActive()) { 601 const details = &self.detailScroll().child.box; 602 if (self.selected()) |index| { 603 if (inp.activated(root_focus, details.children.keys()[0], key) and self.data.handle.canUndo() and self.data.items[index].index > 0) { 604 self.requestUndo(ui.RoutablePage.repoUndoRoute(self.data.identity, self.data.items[index].index) orelse return error.RouteTooLong); 605 return; 606 } 607 } 608 if (inp.rowDelta(key, @intCast(details.children.count()))) |delta| { 609 ui.widget.moveRowFocus(details, self.detailScroll(), root_focus, delta); 610 } else if (key == .arrow_left) { 611 self.focusList(root_focus); 612 } 613 } else if (inp.rowDelta(key, @intCast(self.listScroll().child.box.children.count()))) |delta| { 614 ui.widget.moveRowFocus(&self.listScroll().child.box, self.listScroll(), root_focus, delta); 615 } else switch (key) { 616 .arrow_right, .enter => if (self.selected() != null) self.focusDetail(root_focus), 617 // when the window is too narrow to lay out the detail pane beside 618 // the list, a click on a row opens it like enter. the detail was 619 // swapped to the just-selected row above. 620 .mouse => |mouse| if (self.selected() != null and self.contentBox().children.values()[1].rect == null and ui.widget.clickOnSelectedRow(&self.listScroll().child.box, root_focus, mouse)) { 621 self.focusDetail(root_focus); 622 }, 623 else => {}, 624 } 625 } 626 627 // enter the detail pane, landing on the way back to the list when it shows 628 fn focusDetail(self: *View, root_focus: *Focus) void { 629 if (ui.widget.listBackId(self.detailFrame()) != null) return ui.widget.focusListBack(self.detailFrame(), root_focus); 630 root_focus.setFocus(self.detailScroll().getFocus().id); 631 } 632 633 fn focusList(self: *View, root_focus: *Focus) void { 634 root_focus.setFocus(self.listScroll().getFocus().id); 635 } 636 637 fn contentAtTop(self: *View) bool { 638 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 639 const rows = &scroll.child.box; 640 const id = rows.getFocus().child_id orelse return true; 641 return rows.children.getIndex(id) == 0 and scroll.y == 0; 642 } 643 644 // moving up from the sub header returns to the page's tabs, as does moving 645 // up from the top of the content where there is no sub header 646 pub fn atTop(self: *View) bool { 647 if (self.header() != null) return self.headerActive(); 648 if (self.data.isEmpty(self.session)) return true; 649 // the way back to the list sits above the detail while it shows 650 if (self.detailActive()) if (ui.widget.listBackId(self.detailFrame())) |id| return self.detailFrame().getFocus().child_id == id; 651 return self.contentAtTop(); 652 } 653 654 // arriving from the page's tabs lands on the sub header, not the content 655 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 656 const copyable_text = self.header() orelse return false; 657 root_focus.setFocus(copyable_text.getFocus().id); 658 return true; 659 } 660 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 661 self.box.deinit(allocator); 662 } 663 pub fn clearGrid(self: *View) void { 664 self.box.clearGrid(); 665 } 666 pub fn getGrid(self: View) ?Grid { 667 return self.box.getGrid(); 668 } 669 pub fn getFocus(self: *View) *Focus { 670 return self.box.getFocus(); 671 } 672 }; 673