╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
825 }, 826 .arrow_up => self.exit = .header, 827 .arrow_down => _ = self.moveVertical(root_focus, true), 828 .enter => if (on_primary) try self.primaryAction(allocator), 829 .mouse => |mouse| if (on_primary and inp.leftClickOn(root_focus, child_id, mouse)) try self.primaryAction(allocator), 830 else => {}, 831 } 832 } 833 834 fn primaryAction(self: *This, allocator: std.mem.Allocator) !void { 835 try self.toggleStatus(allocator); 836 } 837 838 fn descriptionInput(self: *This, key: Key, root_focus: *Focus) void { 839 const detail_scroll = &self.scroll; 840 switch (key) { 841 .arrow_left => self.exit = .list, 842 .arrow_up => { 843 if (!self.session.is_terminal) { 844 _ = self.moveVertical(root_focus, false); 845 return; 846 } 847 if (self.focusAdjacentVisible(root_focus, false)) return; 848 const before = detail_scroll.y; 849 detail_scroll.y -= 1; 850 detail_scroll.clampToContent(); 851 if (detail_scroll.y == before) _ = self.moveVertical(root_focus, false); 852 }, 853 .arrow_down => { 854 if (!self.session.is_terminal) { 855 _ = self.moveVertical(root_focus, true); 856 return; 857 } 858 if (self.focusAdjacentVisible(root_focus, true)) return; 859 const before = detail_scroll.y; 860 detail_scroll.y += 1; 861 detail_scroll.clampToContent(); 862 if (detail_scroll.y == before) _ = self.moveVertical(root_focus, true); 863 }, 864 else => {}, 865 } 866 } 867 868 fn commentInput(self: *This, key: Key, root_focus: *Focus) void { 869 switch (key) { 870 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 871 self.exit = .list; 872 }, 873 .arrow_right => _ = self.moveHorizontal(root_focus, true), 874 .arrow_up => _ = self.moveVertical(root_focus, false), 875 .arrow_down => _ = self.moveVertical(root_focus, true), 876 else => {}, 877 } 878 } 879 880 fn pageDetail(self: *This, root_focus: *Focus, delta: isize) void { 881 if (!self.session.is_terminal) return; 882 const detail_scroll = &self.scroll; 883 detail_scroll.y += delta; 884 detail_scroll.clampToContent(); 885 self.focusVisible(root_focus, delta > 0); 886 } 887 888 fn jump(self: *This, root_focus: *Focus, to_end: bool) void { 889 if (self.session.is_terminal) { 890 const detail_scroll = &self.scroll; 891 detail_scroll.y = if (to_end) std.math.maxInt(isize) else 0; 892 detail_scroll.clampToContent(); 893 self.focusVisible(root_focus, to_end); 894 } else { 895 const focus = self.inner().getFocus(); 896 var chosen: ?usize = null; 897 for (focus.children.keys(), focus.children.values()) |id, child| { 898 if (!child.focus.focusable) continue; 899 chosen = id; 900 if (!to_end) break; 901 } 902 if (chosen) |id| root_focus.setFocus(id); 903 } 904 } 905 906 fn rectVisible(self: *This, rect: layout.IRect) bool { 907 const detail_scroll = &self.scroll; 908 const viewport = detail_scroll.grid orelse return false; 909 const top = detail_scroll.y; 910 const bottom = top + @as(isize, @intCast(viewport.size.height - detail_scroll.bar_h)); 911 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 912 } 913 914 fn focusVisible(self: *This, root_focus: *Focus, last: bool) void { 915 const inner_box = self.inner(); 916 var offset: usize = 0; 917 while (offset < inner_box.children.count()) : (offset += 1) { 918 const child_index = if (last) inner_box.children.count() - 1 - offset else offset; 919 if (self.focusVisibleChild(child_index, root_focus, last)) return; 920 } 921 } 922 923 fn focusVisibleChild(self: *This, child_index: usize, root_focus: *Focus, last: bool) bool { 924 const inner_box = self.inner(); 925 const child = &inner_box.children.values()[child_index]; 926 const child_rect = child.rect orelse return false; 927 var chosen: ?usize = null; 928 if (child.widget.getFocus().focusable and self.rectVisible(child_rect)) { 929 const id = inner_box.children.keys()[child_index]; 930 chosen = id; 931 if (!last) { 932 root_focus.setFocus(id); 933 return true; 934 } 935 } 936 937 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 938 if (!focus_child.focus.focusable) continue; 939 const rect: layout.IRect = .{ 940 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 941 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 942 .size = focus_child.rect.size, 943 }; 944 if (!self.rectVisible(rect)) continue; 945 chosen = id; 946 if (!last) break; 947 } 948 if (chosen) |id| { 949 root_focus.setFocus(id); 950 return true; 951 } 952 return false; 953 } 954 955 fn focusAdjacentVisible(self: *This, root_focus: *Focus, down: bool) bool { 956 const inner_box = self.inner(); 957 var next = self.focusedChild(root_focus) orelse return false; 958 while (true) { 959 if (down) { 960 next += 1; 961 if (next >= inner_box.children.count()) return false; 962 } else { 963 if (next == 0) return false; 964 next -= 1; 965 } 966 if (self.focusVisibleChild(next, root_focus, !down)) { 967 self.focusChild(next, !down, root_focus); 968 return true; 969 } 970 } 971 } 972 973 fn focusedChild(self: *This, root_focus: *Focus) ?usize { 974 const inner_box = self.inner(); 975 var id = root_focus.grandchild_id orelse return null; 976 while (inner_box.getFocus().children.get(id)) |child| { 977 if (child.parent_id == inner_box.getFocus().id) return inner_box.children.getIndex(id); 978 id = child.parent_id; 979 } 980 return null; 981 } 982 983 fn childFocusable(self: *This, child_index: usize) bool { 984 const focus = self.inner().children.values()[child_index].widget.getFocus(); 985 if (focus.focusable) return true; 986 for (focus.children.values()) |item| { 987 if (item.focus.focusable) return true; 988 } 989 return false; 990 } 991 992 fn focusChild(self: *This, child_index: usize, last: bool, root_focus: *Focus) void { 993 const inner_box = self.inner(); 994 const child = &inner_box.children.values()[child_index]; 995 var target = inner_box.children.keys()[child_index]; 996 var rect = child.rect orelse return; 997 switch (child.widget) { 998 .repo_comment => |*comment| { 999 if (comment.rowRect(last)) |row_rect| { 1000 rect.x += row_rect.x; 1001 rect.y += row_rect.y; 1002 rect.size = row_rect.size; 1003 } 1004 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1005 }, 1006 .box => |*box| if (box.children.count() > 0) { 1007 target = if (child_index == tool_row_index) 1008 box.getFocus().child_id orelse return 1009 else 1010 box.children.keys()[if (last) box.children.count() - 1 else 0]; 1011 root_focus.setFocus(target); 1012 }, 1013 else => root_focus.setFocus(target), 1014 } 1015 const detail_scroll = &self.scroll; 1016 if (detail_scroll.grid) |viewport| { 1017 const viewport_height = viewport.size.height - detail_scroll.bar_h; 1018 if (rect.size.height > viewport_height) { 1019 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1020 detail_scroll.y = std.math.clamp(detail_scroll.y, rect.y, bottom_aligned); 1021 detail_scroll.clampToContent(); 1022 return; 1023 } 1024 } 1025 detail_scroll.scrollToRect(rect); 1026 } 1027 1028 fn moveVertical(self: *This, root_focus: *Focus, down: bool) bool { 1029 const inner_box = self.inner(); 1030 const current = self.focusedChild(root_focus) orelse return false; 1031 if (inner_box.children.values()[current].widget == .repo_comment) { 1032 const comment = &inner_box.children.values()[current].widget.repo_comment; 1033 if (down and !comment.bodyFocused()) { 1034 self.focusChild(current, true, root_focus); 1035 return true; 1036 } 1037 if (!down and comment.bodyFocused()) { 1038 self.focusChild(current, false, root_focus); 1039 return true; 1040 } 1041 } 1042 1043 var next = current; 1044 while (true) { 1045 if (down) { 1046 next += 1; 1047 if (next >= inner_box.children.count()) return false; 1048 } else { 1049 if (next == 0) return false; 1050 next -= 1; 1051 } 1052 if (!self.childFocusable(next)) continue; 1053 self.focusChild(next, !down, root_focus); 1054 return true; 1055 } 1056 } 1057 1058 fn moveHorizontal(self: *This, root_focus: *Focus, right: bool) bool { 1059 const inner_box = self.inner(); 1060 const current = self.focusedChild(root_focus) orelse return false; 1061 const child = &inner_box.children.values()[current]; 1062 if (child.widget == .repo_comment) { 1063 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1064 if (moved) if (child.rect) |rect| self.scroll.scrollToRect(rect); 1065 return moved; 1066 } 1067 const row = switch (child.widget) { 1068 .box => |*box| box, 1069 else => return false, 1070 }; 1071 const selected = row.getFocus().child_id orelse return false; 1072 const selected_index = row.children.getIndex(selected) orelse return false; 1073 const target = if (right) selected_index + 1 else selected_index -| 1; 1074 if (target == selected_index or target >= row.children.count()) return false; 1075 root_focus.setFocus(row.children.keys()[target]); 1076 if (child.rect) |rect| self.scroll.scrollToRect(rect); 1077 return true; 1078 } 1079 1080 fn tagsInput(self: *This, child_index: usize, tags: *TagFlow, key: Key, root_focus: *Focus) void { 1081 const child_id = tags.focus.child_id orelse return; 1082 const cur = tags.indexOfFocusId(child_id) orelse return; 1083 const count = tags.text_boxes.items.len; 1084 switch (key) { 1085 .arrow_left => if (cur > 0) self.focusTag(child_index, tags, root_focus, cur - 1) else { 1086 self.exit = .list; 1087 }, 1088 .arrow_right => if (cur + 1 < count) self.focusTag(child_index, tags, root_focus, cur + 1), 1089 .arrow_up => if (tags.rowStep(cur, false)) |index| { 1090 self.focusTag(child_index, tags, root_focus, index); 1091 } else { 1092 _ = self.moveVertical(root_focus, false); 1093 }, 1094 .arrow_down => if (tags.rowStep(cur, true)) |index| { 1095 self.focusTag(child_index, tags, root_focus, index); 1096 } else { 1097 _ = self.moveVertical(root_focus, true); 1098 }, 1099 .home => self.focusTag(child_index, tags, root_focus, 0), 1100 .end => self.focusTag(child_index, tags, root_focus, count - 1), 1101 else => {}, 1102 } 1103 } 1104 1105 fn focusTag(self: *This, child_index: usize, tags: *TagFlow, root_focus: *Focus, item: usize) void { 1106 root_focus.setFocus(tags.text_boxes.items[item].getFocus().id); 1107 if (self.session.is_terminal and item < tags.rects.items.len) { 1108 if (self.inner().children.values()[child_index].rect) |flow_rect| { 1109 var rect = tags.rects.items[item]; 1110 rect.x += flow_rect.x; 1111 rect.y += flow_rect.y; 1112 self.scroll.scrollToRect(rect); 1113 } 1114 } 1115 } 1116 1117 fn toolRowFocused(self: *This, root_focus: *Focus) bool { 1118 return self.focusedChild(root_focus) == tool_row_index; 1119 } 1120 1121 fn focusToolRow(self: *This, root_focus: *Focus) void { 1122 const child_id = self.toolRow().getFocus().child_id orelse { 1123 self.exit = .header; 1124 return; 1125 }; 1126 root_focus.setFocus(child_id); 1127 if (self.inner().children.values()[tool_row_index].rect) |rect| self.scroll.scrollToRect(rect); 1128 } 1129 1130 pub fn focusFirst(self: *This, root_focus: ?*Focus) bool { 1131 const inner_box = self.inner(); 1132 for (tool_row_index + 1..inner_box.children.count()) |child_index| { 1133 if (!self.childFocusable(child_index)) continue; 1134 if (root_focus) |focus| 1135 self.focusChild(child_index, false, focus) 1136 else 1137 inner_box.getFocus().child_id = inner_box.children.keys()[child_index]; 1138 return true; 1139 } 1140 return false; 1141 } 1142 1143 pub fn atTop(self: *This, root_focus: *Focus) bool { 1144 const focused = self.focusedChild(root_focus) orelse return false; 1145 for (0..self.inner().children.count()) |child_index| { 1146 if (!self.childFocusable(child_index)) continue; 1147 return focused == child_index; 1148 } 1149 return false; 1150 } 1151 1152 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 1153 if (comptime wasm) return; 1154 1155 if (comptime supports_drafts) { 1156 const entry = self.entry orelse return; 1157 if (event_kind == kind and entryDraft(entry)) { 1158 try Data.removeDraft(self.session, allocator, &id); 1159 const route = draftsRoute(self.data.identity) orelse return; 1160 try self.session.navigate(route); 1161 return; 1162 } 1163 } 1164 1165 const io = self.session.io orelse return; 1166 const source = self.data.repo_source orelse return; 1167 const author = (try self.session.eventAuthor()) orelse return; 1168 1169 switch (source.repo_kind) { 1170 inline else => |repo_kind| { 1171 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1172 defer any_repo.deinit(io, allocator); 1173 switch (any_repo) { 1174 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 1175 } 1176 }, 1177 } 1178 1179 const id_hex = std.fmt.bytesToHex(id, .lower); 1180 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 1181 try self.session.navigate(route); 1182 } 1183 1184 fn toggleStatus(self: *This, allocator: std.mem.Allocator) !void { 1185 if (comptime (!has_status or wasm)) return; 1186 const entry = self.entry orelse return; 1187 const io = self.session.io orelse return; 1188 const source = self.data.repo_source orelse return; 1189 const author = (try self.session.eventAuthor()) orelse return; 1190 const status = (statusChange(entryStatus(entry)) orelse return).status; 1191 const id = try evt.parseEventId(entry.id); 1192 1193 switch (source.repo_kind) { 1194 inline else => |repo_kind| { 1195 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1196 defer any_repo.deinit(io, allocator); 1197 switch (any_repo) { 1198 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id, .{ .status = status }, author), 1199 } 1200 }, 1201 } 1202 1203 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 1204 try self.session.navigate(route); 1205 } 1206 }; 1207 } 1208 1209 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 1210 const Self = Data; 1211 const HeaderType = Data.Header; 1212 const Window = Data.Window; 1213 const Entry = Data.Entry; 1214 const Event = Data.Event; 1215 const Status = Data.Status; 1216 const has_status = @hasField(Event, "status"); 1217 const supports_conflicts = Event.merge_policy == .field_conflicts; 1218 const supports_drafts = @hasField(Self, "drafts"); 1219 const supports_merge = @hasDecl(Data, "mergeRoute"); 1220 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 1221 const ViewKind = Data.ViewKind; 1222 const thread_name = Data.thread_name; 1223 const DetailType = Data.Detail; 1224 1225 return struct { 1226 const This = @This(); 1227 // a vertical box: the header tabs on top, then a stack holding a 1228 // master-detail split (thread list + description pane) per status list, 1229 // plus the tags view. 1230 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 1231 data: *const Self, 1232 session: *ui.Session, 1233 1234 const header_index: usize = 0; 1235 const stack_index: usize = 1; 1236 // indices within the stack, 1:1 with the header tabs. 1237 const status_count = @typeInfo(Status).@"enum".fields.len; 1238 const tags_view_index: usize = status_count; 1239 // the new-thread form, or the edit, comment, or resolve form when the page was 1240 // loaded at one of their urls. 1241 const form_view_index: usize = tags_view_index + 1; 1242 // the conflicts split; the tab and stack child exist only when the repo 1243 // has conflicted threads. 1244 const drafts_view_index: usize = form_view_index + 1; 1245 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 1246 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 1247 // indices within a split (the horizontal box inside the stack). 1248 const list_index: usize = 0; 1249 const detail_index: usize = 1; 1250 const list_max_width: usize = 35; 1251 const detail_min_width: usize = 40; 1252 // indices within the thread form. 1253 const title_field_index: usize = 0; 1254 const tags_field_index: usize = 1; 1255 const description_field_index: usize = 2; 1256 const target_branch_field_index: usize = 3; 1257 const submit_field_index: usize = 3 + @as(usize, @intFromBool(supports_drafts)); 1258 const comment_author_field_index: usize = 0; 1259 const comment_body_field_index: usize = 1; 1260 const comment_submit_field_index: usize = 2; 1261 1262 pub fn viewIndex(view: ViewKind) usize { 1263 const name = @tagName(view); 1264 if (std.mem.eql(u8, name, "tags")) return tags_view_index; 1265 if (std.mem.eql(u8, name, "new") or std.mem.eql(u8, name, "edit") or std.mem.eql(u8, name, "publish") or std.mem.eql(u8, name, "merge") or std.mem.eql(u8, name, "new_comment") or std.mem.eql(u8, name, "edit_comment") or std.mem.eql(u8, name, "remove") or std.mem.eql(u8, name, "resolve")) return form_view_index; 1266 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 1267 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 1268 if (std.mem.eql(u8, name, "description")) unreachable; 1269 inline for (@typeInfo(Status).@"enum".fields, 0..) |field, index| { 1270 if (std.mem.eql(u8, name, field.name)) return index; 1271 } 1272 unreachable; 1273 } 1274 1275 const Confirmation = enum { remove, publish, merge }; 1276 1277 fn confirmation(view: ViewKind) ?Confirmation { 1278 if (view == .remove) return .remove; 1279 if (supports_drafts and std.mem.eql(u8, @tagName(view), "publish")) return .publish; 1280 if (supports_merge and std.mem.eql(u8, @tagName(view), "merge")) return .merge; 1281 return null; 1282 } 1283 1284 // the status a status split lists; the conflicts split has none (its 1285 // threads carry their own). 1286 fn splitStatus(index: usize) Status { 1287 return @enumFromInt(index); 1288 } 1289 1290 fn entryConflicted(entry: Entry) bool { 1291 return if (supports_conflicts) entry.conflicted else false; 1292 } 1293 1294 fn entryStatus(entry: Entry) Status { 1295 if (comptime has_status) return Data.statusKind(entry.record.event); 1296 return @enumFromInt(0); 1297 } 1298 1299 fn entryDraft(entry: Entry) bool { 1300 return if (supports_drafts) entry.draft else false; 1301 } 1302 1303 fn listRoute(identity: []const u8, status: Status, tag: []const u8, selected: []const u8) ?ui.RoutablePage { 1304 return Data.listRoute(identity, status, tag, selected); 1305 } 1306 1307 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 1308 return if (supports_drafts) Data.draftsRoute(identity) else null; 1309 } 1310 1311 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1312 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 1313 } 1314 1315 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1316 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 1317 } 1318 1319 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1320 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 1321 } 1322 1323 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 1324 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 1325 } 1326 1327 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8, id: []const u8) ![]const u8 { 1328 const route = listRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1329 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1330 } 1331 1332 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 1333 const selected = std.mem.eql(u8, id, data.selected_id); 1334 const route = (if (selected and data.description_page) 1335 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 1336 else if (selected) 1337 if (data.comment_page) |page| 1338 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 1339 else 1340 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 1341 else 1342 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 1343 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 1344 } 1345 1346 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 1347 if (supports_conflicts and status_maybe == null and !drafts) { 1348 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 1349 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1350 } 1351 if (drafts and id.len == 0) { 1352 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 1353 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1354 } 1355 return listLink(page_arena, data.identity, status_maybe orelse @enumFromInt(0), data.tag, id); 1356 } 1357 1358 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8) ![]const u8 { 1359 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1360 return listLink(page_arena, identity, status, encoded, ""); 1361 } 1362 1363 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 1364 var outer = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1365 errdefer outer.deinit(allocator); 1366 1367 // the tabs at the top. 1368 { 1369 var hdr = try Data.initHeader(allocator, session, data); 1370 errdefer hdr.deinit(allocator); 1371 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 1372 } 1373 1374 // the stack enters `outer` before its children enter it, so an error 1375 // frees each child exactly once. 1376 { 1377 var stack = try wgt.Stack(Widget).init(allocator); 1378 errdefer stack.deinit(allocator); 1379 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1380 } 1381 const stack = &outer.children.values()[stack_index].widget.stack; 1382 1383 inline for (@typeInfo(Status).@"enum".fields) |field| { 1384 var split = try initSplit(allocator, session, data, @enumFromInt(field.value), false); 1385 errdefer split.deinit(allocator); 1386 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1387 } 1388 1389 // the tags view 1390 { 1391 var tf = try TagFlow.init(allocator); 1392 errdefer tf.deinit(allocator); 1393 var items: std.ArrayList(TagFlow.Item) = .empty; 1394 defer items.deinit(allocator); 1395 // when filtered, the first item clears the filter 1396 if (data.tag.len != 0) 1397 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @enumFromInt(0), "", "") }); 1398 for (data.tags) |tag| 1399 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, @enumFromInt(0), tag) }); 1400 try tf.setItems(allocator, items.items); 1401 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 1402 } 1403 1404 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1405 // its place, prefilled with the selected thread's content. a 1406 // logged-out session can't create events, so the unauthorized view 1407 // stands in. 1408 if (session.data.is_local or session.data.user_id != null) { 1409 if (confirmation(data.view)) |confirmation_kind| { 1410 const aa = session.page_arena.allocator(); 1411 const route = switch (confirmation_kind) { 1412 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1413 .publish => publishRoute(data.identity, data.selected_id), 1414 .merge => mergeRoute(data.identity, data.selected_id), 1415 } orelse return error.RouteTooLong; 1416 const page_url = try route.toUrl(session.page_arena); 1417 const action = try std.fmt.allocPrint(aa, "form:{s}", .{page_url}); 1418 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1419 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1420 var buttons: [2]ConfirmationButton = undefined; 1421 buttons[0] = .{ .label = switch (confirmation_kind) { 1422 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1423 .publish => "publish patch", 1424 .merge => "merge patch", 1425 } }; 1426 var button_count: usize = 1; 1427 if (confirmation_kind == .merge) { 1428 if (comptime supports_merge) { 1429 buttons[1] = .{ 1430 .label = "squash and merge patch", 1431 .action = try std.fmt.allocPrint(aa, "{s}{s}/squash", .{ ui.submit_action_prefix, page_url }), 1432 }; 1433 button_count += 1; 1434 } else unreachable; 1435 } 1436 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1437 errdefer center.deinit(allocator); 1438 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1439 } else if (data.view == .new_comment or data.view == .edit_comment) { 1440 const editing = data.view == .edit_comment; 1441 const route = if (editing) 1442 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1443 else 1444 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1445 const page_url = try route.toUrl(session.page_arena); 1446 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 1447 const page = data.comment_page; 1448 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1449 const parent_route = if (editing) 1450 if (top_level) 1451 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1452 else if (page) |p| 1453 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1454 else 1455 null 1456 else if (page) |p| 1457 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1458 else 1459 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 1460 const parent_author: ui.Author = if (editing) 1461 if (top_level) 1462 if (data.selectedThread()) |entry| entry.author else .unknown 1463 else if (page) |p| 1464 p.selected.parent_author orelse .unknown 1465 else 1466 .unknown 1467 else if (page) |p| 1468 p.selected.author 1469 else if (data.selectedThread()) |entry| 1470 entry.author 1471 else 1472 .unknown; 1473 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 1474 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 1475 errdefer form.deinit(allocator); 1476 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1477 } else if (supports_conflicts and data.view == .resolve) { 1478 // on the web the page grows to the form's height and the 1479 // browser scrolls it; the terminal scrolls the form itself 1480 var form_widget: Widget = blk: { 1481 var form = try initResolveForm(allocator, session, data); 1482 errdefer form.deinit(allocator); 1483 break :blk if (session.is_terminal) 1484 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 1485 else 1486 .{ .box = form }; 1487 }; 1488 errdefer form_widget.deinit(allocator); 1489 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 1490 } else { 1491 const aa = session.page_arena.allocator(); 1492 const route = if (data.view == .edit) 1493 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 1494 else 1495 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 1496 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 1497 const record = if (data.view == .edit) 1498 (if (data.selectedThread()) |entry| &entry.record else null) 1499 else 1500 null; 1501 var form = try initThreadForm(allocator, session, data, action, record); 1502 errdefer form.deinit(allocator); 1503 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1504 } 1505 } else { 1506 var message = try ui.Unauthorized.View.init(allocator); 1507 errdefer message.deinit(allocator); 1508 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 1509 } 1510 1511 if (supports_drafts) { 1512 var split = try initSplit(allocator, session, data, null, true); 1513 errdefer split.deinit(allocator); 1514 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1515 } 1516 1517 // the conflicts split; its tab exists only when the repo has 1518 // conflicts, so skip the child too to keep the stack 1:1 with the 1519 // tabs. 1520 if (supports_conflicts and data.conflicts.count > 0) { 1521 var split = try initSplit(allocator, session, data, null, false); 1522 errdefer split.deinit(allocator); 1523 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1524 } 1525 1526 // the stack starts on the page's view. 1527 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 1528 1529 // focus entering the view lands on the tabs first. 1530 outer.getFocus().child_id = outer.children.keys()[header_index]; 1531 1532 return .{ 1533 .box = outer, 1534 .data = data, 1535 .session = session, 1536 }; 1537 } 1538 1539 // the master-detail split showing `status`'s window, or the conflicts 1540 // window when null. 1541 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 1542 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1543 errdefer box.deinit(allocator); 1544 1545 const win = if (status_maybe) |status| data.window(status) else if (drafts and supports_drafts) &data.drafts else if (supports_conflicts) &data.conflicts else unreachable; 1546 1547 // the thread list (one focusable row per title), plus a "next" link that 1548 // reloads the page rooted at the following thread. 1549 { 1550 var list_scroll = blk: { 1551 var list_box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 1552 errdefer list_box.deinit(allocator); 1553 if (win.prev_id) |prev| 1554 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 1555 for (win.items) |entry| { 1556 // conflicts take precedence over the comment count 1557 const label: []const u8 = if (entryConflicted(entry) and status_maybe != null) 1558 " conflict " 1559 else if (entry.comments.count > 0) 1560 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 1561 else 1562 ""; 1563 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 1564 } 1565 if (win.next_id) |next| 1566 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 1567 // select the window's first thread (past a leading "previous" 1568 // row) so its description shows on load. 1569 if (win.items.len > 0) 1570 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 1571 else if (list_box.children.count() > 0) 1572 list_box.getFocus().child_id = list_box.children.keys()[0]; 1573 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 1574 }; 1575 errdefer list_scroll.deinit(allocator); 1576 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 } }); 1577 } 1578 1579 // frame the detail pane inside the master-detail split 1580 { 1581 var frame = try wgt.Box(Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 1582 errdefer frame.deinit(allocator); 1583 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 1584 errdefer detail_view.deinit(allocator); 1585 frame.getFocus().child_id = detail_view.getFocus().id; 1586 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 1587 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 1588 } 1589 1590 box.getFocus().child_id = box.children.keys()[list_index]; 1591 return box; 1592 } 1593 1594 // a thread form, plus the target branch for patches, prefilled from 1595 // `record` when given. its form: subtree makes the web renderer POST 1596 // the inputs to `action`'s route. 1597 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 1598 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1599 errdefer box.deinit(allocator); 1600 box.getFocus().kind = .{ .custom = action }; 1601 1602 { 1603 var title = try wgt.TextInput.init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1604 errdefer title.deinit(allocator); 1605 title.getFocus().focusable = true; 1606 if (record) |r| try title.setContent(allocator, r.event.title); 1607 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1608 } 1609 1610 { 1611 var tags = try wgt.TextInput.init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1612 errdefer tags.deinit(allocator); 1613 tags.getFocus().focusable = true; 1614 if (record) |r| try tags.setContent(allocator, r.event.tags); 1615 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1616 } 1617 1618 { 1619 var description = try wgt.TextInput.init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 1620 errdefer description.deinit(allocator); 1621 description.getFocus().focusable = true; 1622 if (record) |r| try description.setContent(allocator, r.event.description); 1623 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 1624 } 1625 1626 if (supports_drafts) { 1627 var target = try wgt.TextInput.init(allocator, .{ .label = " target branch ", .name = "target_branch", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1628 errdefer target.deinit(allocator); 1629 target.getFocus().focusable = true; 1630 const content = if (record) |r| r.event.target_branch else data.default_target_branch; 1631 try target.setContent(allocator, content); 1632 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1633 } 1634 1635 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 1636 1637 box.getFocus().child_id = box.children.keys()[title_field_index]; 1638 return box; 1639 } 1640 1641 fn initCommentForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, author: ui.Author, parent_route: ui.RoutablePage, initial_body: ?[]const u8) !wgt.Box(Widget) { 1642 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1643 errdefer box.deinit(allocator); 1644 box.getFocus().kind = .{ .custom = action }; 1645 1646 { 1647 var parent = try ui.authorBox(allocator, session.page_arena, author); 1648 errdefer parent.deinit(allocator); 1649 parent.options.label = " replying to "; 1650 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 1651 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1652 } 1653 1654 { 1655 var body = try wgt.TextInput.init(allocator, .{ 1656 .label = " comment ", 1657 .name = "body", 1658 .visible_width = null, 1659 .rounded_corners = true, 1660 .render_content = session.is_terminal, 1661 .multiline = true, 1662 .visible_height = 5, 1663 .scroll = .{ .fill = true }, 1664 }); 1665 errdefer body.deinit(allocator); 1666 body.getFocus().focusable = true; 1667 if (initial_body) |text| try body.setContent(allocator, text); 1668 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 1669 } 1670 1671 try addSubmitButtonLabeled(allocator, &box, "submit"); 1672 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 1673 return box; 1674 } 1675 1676 const ConfirmationButton = struct { 1677 label: []const u8, 1678 action: []const u8 = "submit", 1679 }; 1680 1681 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 1682 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 1683 errdefer box.deinit(allocator); 1684 box.getFocus().kind = .{ .custom = action }; 1685 1686 var prompt = try wgt.Text.init(allocator, "are you sure?"); 1687 errdefer prompt.deinit(allocator); 1688 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 1689 1690 for (buttons, 0..) |options, index| { 1691 var button = try wgt.TextBox.init(allocator, options.label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1692 errdefer button.deinit(allocator); 1693 button.getFocus().focusable = true; 1694 button.getFocus().kind = .{ .custom = options.action }; 1695 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 1696 if (index == 0) box.getFocus().child_id = button.getFocus().id; 1697 } 1698 1699 return Center.init(allocator, .{ .box = box }); 1700 } 1701 1702 // a form's submit button, then a spacer absorbing the leftover 1703 // min-height the box hands its last child, so the button keeps its 1704 // natural height 1705 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 1706 { 1707 var submit = try SubmitButton.initLabeled(allocator, label); 1708 errdefer submit.deinit(allocator); 1709 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1710 } 1711 { 1712 var spacer = try Spacer.init(allocator); 1713 errdefer spacer.deinit(allocator); 1714 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1715 } 1716 } 1717 1718 // the conflict resolve form. the "use this" links are navigations that 1719 // flip the url's theirs: pick, reloading the prefills; one submit 1720 // settles every conflict. the form: subtree makes the web renderer POST 1721 // to the resolve route. 1722 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 1723 const aa = session.page_arena.allocator(); 1724 // init only picks the resolve view once it has read the conflict 1725 const conflict = if (data.conflict) |*c| c else unreachable; 1726 1727 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1728 errdefer box.deinit(allocator); 1729 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 1730 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 1731 1732 if (conflict.title) |*fc| { 1733 try addLabel(allocator, &box, "title conflict:"); 1734 try addFieldConflict(allocator, &box, session, data, "title", fc); 1735 } 1736 if (conflict.tags) |*fc| { 1737 try addLabel(allocator, &box, "tags conflict:"); 1738 try addFieldConflict(allocator, &box, session, data, "tags", fc); 1739 } 1740 if (comptime @hasField(Data.Conflict, "status")) { 1741 if (conflict.status) |*fc| { 1742 try addLabel(allocator, &box, "status conflict:"); 1743 try addAtomicConflict(allocator, &box, session, data, "status", fc); 1744 } 1745 } 1746 if (comptime @hasField(Data.Conflict, "revision")) { 1747 if (conflict.revision) |*fc| { 1748 try addLabel(allocator, &box, "revision conflict:"); 1749 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 1750 } 1751 if (conflict.target_branch) |*fc| { 1752 try addLabel(allocator, &box, "target branch conflict:"); 1753 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 1754 } 1755 } 1756 1757 if (conflict.description) |*desc| { 1758 try addLabel(allocator, &box, "description conflict:"); 1759 var hunk_index: usize = 0; 1760 for (desc.chunks, 0..) |*chunk, chunk_index| { 1761 switch (chunk.*) { 1762 .same => |text| { 1763 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 1764 errdefer tb.deinit(allocator); 1765 tb.getFocus().focusable = true; 1766 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1767 }, 1768 // an auto-resolved chunk: only one side changed it, so its 1769 // version stands, shown distinctly with its author 1770 .auto => |auto| { 1771 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 1772 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 1773 const label = switch (author) { 1774 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 1775 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 1776 }; 1777 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1778 errdefer tb.deinit(allocator); 1779 tb.getFocus().focusable = true; 1780 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1781 }, 1782 .conflict => |hunk| { 1783 // a blank row on each side sets the group apart from 1784 // the flowing shared and auto-resolved chunks 1785 if (chunk_index > 0) try addGap(allocator, &box); 1786 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 1787 hunk_index += 1; 1788 const picked_theirs = data.theirsPicked(name); 1789 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 1790 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 1791 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 1792 errdefer resolution_input.deinit(allocator); 1793 resolution_input.getFocus().focusable = true; 1794 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 1795 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1796 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 1797 }, 1798 } 1799 } 1800 } 1801 1802 try addSubmitButtonLabeled(allocator, &box, "submit"); 1803 1804 box.getFocus().child_id = box.children.keys()[0]; 1805 return box; 1806 } 1807 1808 // one conflicted scalar field: both sides' version rows, then the 1809 // resolution input prefilled from the picked side. 1810 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1811 const aa = session.page_arena.allocator(); 1812 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1813 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1814 1815 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1816 errdefer resolution_input.deinit(allocator); 1817 resolution_input.getFocus().focusable = true; 1818 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 1819 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1820 } 1821 1822 // atomic fields can only pick one complete side 1823 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1824 const aa = session.page_arena.allocator(); 1825 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1826 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1827 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 1828 .border_style = .single, 1829 .rounded_corners = true, 1830 .wrap_kind = .word, 1831 .label = " resolution ", 1832 }); 1833 errdefer selected.deinit(allocator); 1834 selected.getFocus().focusable = true; 1835 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 1836 } 1837 1838 // a blank row setting a conflict group apart from its neighbors 1839 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 1840 var text = try wgt.Text.init(allocator, " "); 1841 errdefer text.deinit(allocator); 1842 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1843 } 1844 1845 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 1846 var label = try SectionLabel.init(allocator, content); 1847 errdefer label.deinit(allocator); 1848 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1849 } 1850 1851 // one side of a conflict: the "use this" link on the left, the version 1852 // itself as a labeled read-only box. 1853 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1854 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1855 errdefer row.deinit(allocator); 1856 1857 { 1858 const use_label = "use this"; 1859 var use = try wgt.TextBox.init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1860 errdefer use.deinit(allocator); 1861 use.getFocus().focusable = true; 1862 use.getFocus().kind = .{ .custom = link }; 1863 try row.children.put(allocator, use.getFocus().id, .{ 1864 .widget = .{ .text_box = use }, 1865 .rect = null, 1866 .min_size = .{ .width = use_label.len + 2, .height = null }, 1867 .max_size = .{ .width = use_label.len + 2, .height = null }, 1868 }); 1869 } 1870 1871 { 1872 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1873 errdefer tb.deinit(allocator); 1874 tb.getFocus().focusable = true; 1875 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1876 } 1877 1878 row.getFocus().child_id = row.children.keys()[0]; 1879 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1880 } 1881 1882 // the label of one side's version box, naming its author 1883 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1884 return switch (author) { 1885 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1886 .unknown => if (ours) " current version " else " their version ", 1887 }; 1888 } 1889 1890 // the a: link that reloads the resolve page with `name` picked from ours 1891 // or theirs, reprefilling its resolution input 1892 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1893 const aa = session.page_arena.allocator(); 1894 var picks: std.ArrayList(u8) = .empty; 1895 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1896 while (it.next()) |pick| { 1897 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1898 if (picks.items.len > 0) try picks.append(aa, ','); 1899 try picks.appendSlice(aa, pick); 1900 } 1901 if (theirs) { 1902 if (picks.items.len > 0) try picks.append(aa, ','); 1903 try picks.appendSlice(aa, name); 1904 } 1905 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1906 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1907 } 1908 1909 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1910 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1911 errdefer row.deinit(allocator); 1912 row.getFocus().focusable = true; 1913 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1914 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1915 } 1916 1917 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 1918 self.box.deinit(allocator); 1919 } 1920 1921 fn header(self: *This) *HeaderType { 1922 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 1923 } 1924 1925 fn viewStack(self: *This) *wgt.Stack(Widget) { 1926 return &self.box.children.values()[stack_index].widget.stack; 1927 } 1928 1929 // `index`'s master-detail split inside the stack. 1930 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 1931 return &self.viewStack().children.values()[index].box; 1932 } 1933 1934 // the tags view's flow inside the stack. 1935 fn tagsView(self: *This) *TagFlow { 1936 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1937 } 1938 1939 // the new-thread, edit, or resolve form inside the stack, or null when the 1940 // unauthorized view stands in for it. 1941 fn threadForm(self: *This) ?*wgt.Box(Widget) { 1942 return switch (self.viewStack().children.values()[form_view_index]) { 1943 .box => |*box| box, 1944 // the resolve form sits inside a scroll on the terminal 1945 .scroll => |*scroll| &scroll.child.box, 1946 else => null, 1947 }; 1948 } 1949 1950 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 1951 return switch (self.viewStack().children.values()[form_view_index]) { 1952 .center => |*center| switch (center.child.*) { 1953 .box => |*box| box, 1954 else => null, 1955 }, 1956 else => null, 1957 }; 1958 } 1959 1960 // the resolve form's scroll on the terminal (the web page scrolls itself) 1961 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 1962 return switch (self.viewStack().children.values()[form_view_index]) { 1963 .scroll => |*scroll| scroll, 1964 else => null, 1965 }; 1966 } 1967 1968 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 1969 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1970 } 1971 1972 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 1973 return &self.listScroll(index).child.box; 1974 } 1975 1976 fn detail(self: *This, index: usize) *DetailType { 1977 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 1978 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 1979 } 1980 1981 fn window(self: *This, index: usize) *const Window { 1982 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 1983 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 1984 return self.data.window(splitStatus(index)); 1985 } 1986 1987 fn stackSelectedIndex(self: *This) ?usize { 1988 const stack = self.viewStack(); 1989 const cid = stack.getFocus().child_id orelse return null; 1990 return stack.children.getIndex(cid); 1991 } 1992 1993 // the stack's selected master-detail split (null when the tags view or the 1994 // new-thread form shows). 1995 fn selectedSplitIndex(self: *This) ?usize { 1996 const idx = self.stackSelectedIndex() orelse return null; 1997 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 1998 } 1999 2000 fn detailActive(self: *This, index: usize) bool { 2001 const rb = self.resultsBox(index); 2002 const cid = rb.getFocus().child_id orelse return false; 2003 return rb.children.getIndex(cid) == detail_index; 2004 } 2005 2006 fn headerActive(self: *This) bool { 2007 const cid = self.box.getFocus().child_id orelse return false; 2008 return self.box.children.getIndex(cid) == header_index; 2009 } 2010 2011 fn tagsViewActive(self: *This) bool { 2012 if (self.headerActive()) return false; 2013 return self.stackSelectedIndex() == tags_view_index; 2014 } 2015 2016 fn formViewActive(self: *This) bool { 2017 if (self.headerActive()) return false; 2018 return self.stackSelectedIndex() == form_view_index; 2019 } 2020 2021 // the selected thread's index, or null when a window-navigation row is 2022 // selected (a leading "previous" row shifts the thread rows down by one). 2023 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2024 const lb = self.listBox(index); 2025 const cid = lb.getFocus().child_id orelse return null; 2026 const idx = lb.children.getIndex(cid) orelse return null; 2027 const win = self.window(index); 2028 const lead: usize = if (win.prev_id != null) 1 else 0; 2029 if (idx < lead or idx - lead >= win.items.len) return null; 2030 return idx - lead; 2031 } 2032 2033 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2034 self.clearGrid(); 2035 2036 // each header tab maps 1:1 to a stack child by position 2037 if (self.header().getSelectedIndex()) |index| { 2038 const stack = self.viewStack(); 2039 stack.getFocus().child_id = stack.children.keys()[index]; 2040 } 2041 2042 if (self.selectedSplitIndex()) |i| { 2043 // swap the detail pane to the selected thread when it changes. 2044 if (self.selectedThreadIndex(i)) |selected| { 2045 const entry = self.window(i).items[selected]; 2046 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2047 if (changed) { 2048 try self.detail(i).setEntry(allocator, entry); 2049 } 2050 } 2051 2052 // the selected list row shows a border (the focused TextBox 2053 // upgrades it to a double border itself); the rest stay borderless. 2054 const lb = self.listBox(i); 2055 for (lb.children.keys(), lb.children.values()) |id, *child| { 2056 switch (child.widget) { 2057 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 2058 else => {}, 2059 } 2060 } 2061 2062 // cap the list at list_max_width only while the detail pane fits 2063 // beside it. the box drops the detail when the width can't hold 2064 // both minimums, so when it's that narrow we lift the cap and let 2065 // the list fill the whole width. 2066 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2067 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2068 2069 // stretch the detail pane across the rest of the width so it fills 2070 // the area rather than shrinking to its content; its scroll fills 2071 // the pane. 2072 if (constraint.max_size.width) |w| { 2073 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2074 } else { 2075 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2076 } 2077 } 2078 2079 // refresh the form inputs' entries in the session's focus-id -> input 2080 // map with this frame's addresses, so the web/wasm form handling can 2081 // find them by focus id 2082 if (self.threadForm()) |form| { 2083 const inputs_arena = self.session.arena.allocator(); 2084 for (form.children.values()) |*child| switch (child.widget) { 2085 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2086 else => {}, 2087 }; 2088 } 2089 2090 try self.box.build(allocator, constraint, root_focus); 2091 } 2092 2093 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2094 // wheel input moves focus like the arrows 2095 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2096 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2097 else 2098 raw_key; 2099 2100 if (self.headerActive()) { 2101 // down from the tabs re-enters the stack if the selected view has 2102 // something to focus; other keys move the tabs. 2103 if (inp.vertDirection(key) == .down) { 2104 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2105 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2106 // nothing focusable in the unauthorized view 2107 .unauthorized => false, 2108 else => true, 2109 } else false; 2110 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2111 } else { 2112 try self.header().input(allocator, key, root_focus); 2113 } 2114 return; 2115 } 2116 if (self.tagsViewActive()) { 2117 self.tagsViewInput(key, root_focus); 2118 return; 2119 } 2120 if (self.formViewActive()) { 2121 if (confirmation(self.data.view) != null) { 2122 try self.confirmationFormInput(allocator, key, root_focus); 2123 } else if (supports_conflicts and self.data.view == .resolve) { 2124 try self.resolveInput(allocator, key, root_focus); 2125 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 2126 try self.commentFormInput(allocator, key, root_focus); 2127 } else { 2128 try self.formInput(allocator, key, root_focus); 2129 } 2130 return; 2131 } 2132 const i = self.selectedSplitIndex() orelse return; 2133 if (self.detailActive(i)) { 2134 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2135 .none => {}, 2136 .list => self.focusList(i, root_focus), 2137 .header => self.focusHeader(root_focus), 2138 } 2139 } else { 2140 self.listInput(i, key, root_focus); 2141 } 2142 } 2143 2144 // arrow keys move the tag selection; up from the top row crosses to the 2145 // header tabs. 2146 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2147 const tf = self.tagsView(); 2148 const cid = tf.focus.child_id orelse return; 2149 const cur = tf.indexOfFocusId(cid) orelse return; 2150 const count = tf.text_boxes.items.len; 2151 switch (key) { 2152 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2153 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2154 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2155 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2156 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2157 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2158 else => {}, 2159 } 2160 } 2161 2162 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2163 // up/down (and the scroll wheel) move the selection a row; page up/down 2164 // jump a fixed amount. right/Enter cross into the detail pane. up from 2165 // the top row crosses into the header tabs. 2166 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2167 const lb = self.listBox(index); 2168 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2169 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2170 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2171 return; 2172 } 2173 switch (key) { 2174 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2175 else => {}, 2176 } 2177 } 2178 2179 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2180 const form = self.threadForm() orelse return; 2181 const cid = form.getFocus().child_id orelse return; 2182 const cur = form.children.getIndex(cid) orelse return; 2183 2184 if (cur == description_field_index) { 2185 const child = &form.children.values()[cur]; 2186 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2187 return child.widget.input(allocator, key, root_focus); 2188 } 2189 2190 switch (key) { 2191 .arrow_up, .back_tab => if (cur > 0) 2192 root_focus.setFocus(form.children.keys()[cur - 1]) 2193 else 2194 self.focusHeader(root_focus), 2195 .arrow_down, .tab => if (cur < submit_field_index) { 2196 root_focus.setFocus(form.children.keys()[cur + 1]); 2197 }, 2198 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2199 .mouse => |mouse| if (cur == submit_field_index) { 2200 const submit = &form.children.values()[cur].widget.submit_button; 2201 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2202 }, 2203 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2204 } 2205 } 2206 2207 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2208 const form = self.threadForm() orelse return; 2209 const cid = form.getFocus().child_id orelse return; 2210 const cur = form.children.getIndex(cid) orelse return; 2211 const child = &form.children.values()[cur]; 2212 2213 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2214 return child.widget.input(allocator, key, root_focus); 2215 2216 switch (key) { 2217 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2218 root_focus.setFocus(form.children.keys()[cur - 1]) 2219 else 2220 self.focusHeader(root_focus), 2221 .arrow_down, .tab => if (cur < comment_submit_field_index) 2222 root_focus.setFocus(form.children.keys()[cur + 1]), 2223 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2224 .mouse => |mouse| if (cur == comment_submit_field_index) { 2225 const submit = &child.widget.submit_button; 2226 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2227 }, 2228 else => try child.widget.input(allocator, key, root_focus), 2229 } 2230 } 2231 2232 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2233 const form = self.confirmationForm() orelse return; 2234 const child_id = form.getFocus().child_id orelse return; 2235 const index = form.children.getIndex(child_id) orelse return; 2236 switch (key) { 2237 .arrow_up, .back_tab => if (index > 1) 2238 root_focus.setFocus(form.children.keys()[index - 1]) 2239 else 2240 self.focusHeader(root_focus), 2241 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2242 .enter => try self.submitConfirmation(allocator, index), 2243 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2244 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2245 try self.submitConfirmation(allocator, button_index); 2246 break; 2247 } 2248 }, 2249 else => {}, 2250 } 2251 } 2252 2253 // the multiline hunk inputs keep enter and any up/down that has a row to 2254 // move to; enter on a "use this" link is a navigation the host follows 2255 // before this runs. 2256 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2257 const form = self.threadForm() orelse return; 2258 const cid = form.getFocus().child_id orelse return; 2259 const cur = form.children.getIndex(cid) orelse return; 2260 const child = &form.children.values()[cur]; 2261 2262 if (child.widget == .text_input) { 2263 const ti = &child.widget.text_input; 2264 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2265 return child.widget.input(allocator, key, root_focus); 2266 } 2267 2268 const on_submit = child.widget == .submit_button; 2269 2270 switch (key) { 2271 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2272 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2273 .arrow_left, .arrow_right => switch (child.widget) { 2274 .box => |*row| { 2275 const row_cid = row.getFocus().child_id orelse return; 2276 const row_index = row.children.getIndex(row_cid) orelse return; 2277 if (key == .arrow_left) { 2278 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2279 } else if (row_index + 1 < row.children.count()) { 2280 root_focus.setFocus(row.children.keys()[row_index + 1]); 2281 } 2282 }, 2283 else => try child.widget.input(allocator, key, root_focus), 2284 }, 2285 .enter => if (on_submit) try self.submitResolution(allocator), 2286 .mouse => |mouse| if (on_submit) { 2287 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2288 }, 2289 else => try child.widget.input(allocator, key, root_focus), 2290 } 2291 } 2292 2293 // a multiline input keeps enter and any up/down that has a row to move to 2294 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2295 return switch (key) { 2296 .enter => true, 2297 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2298 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2299 else => false, 2300 }; 2301 } 2302 2303 // the neighboring focusable resolve-form child, skipping the spacer and 2304 // the blank gap rows 2305 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2306 var i = cur; 2307 while (true) { 2308 if (down) { 2309 i += 1; 2310 if (i >= form.children.count()) return null; 2311 } else { 2312 if (i == 0) return null; 2313 i -= 1; 2314 } 2315 switch (form.children.values()[i].widget) { 2316 .spacer, .text => continue, 2317 else => return i, 2318 } 2319 } 2320 } 2321 2322 // focus the resolve-form child at `i` (a version row focuses its selected 2323 // child) and keep it visible on the terminal 2324 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2325 const child = &form.children.values()[i]; 2326 switch (child.widget) { 2327 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2328 else => root_focus.setFocus(form.children.keys()[i]), 2329 } 2330 if (self.session.is_terminal) { 2331 if (self.resolveScroll()) |sc| { 2332 if (child.rect) |rect| sc.scrollToRect(rect); 2333 } 2334 } 2335 } 2336 2337 // re-emit the conflicted thread's event with the resolved content; any 2338 // event on the thread settles its conflict entry. this is the terminal 2339 // path; the web posts the form to the resolve route. 2340 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2341 if (comptime wasm) return; 2342 const io = self.session.io orelse return; 2343 const src = self.data.repo_source orelse return; 2344 const entry = self.data.selectedThread() orelse return; 2345 const author = (try self.session.eventAuthor()) orelse return; 2346 const form = self.threadForm() orelse return; 2347 2348 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2349 var title: ?[]u8 = null; 2350 var tags: ?[]u8 = null; 2351 var hunks: std.ArrayList([]const u8) = .empty; 2352 defer { 2353 if (title) |t| allocator.free(t); 2354 if (tags) |t| allocator.free(t); 2355 for (hunks.items) |h| allocator.free(h); 2356 hunks.deinit(allocator); 2357 } 2358 for (form.children.values()) |*child| switch (child.widget) { 2359 .text_input => |*ti| { 2360 const text = try ti.text(allocator); 2361 errdefer allocator.free(text); 2362 if (std.mem.eql(u8, ti.options.name, "title")) { 2363 title = text; 2364 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2365 tags = text; 2366 } else { 2367 try hunks.append(allocator, text); 2368 } 2369 }, 2370 else => {}, 2371 }; 2372 2373 const id_bytes = try evt.parseEventId(entry.id); 2374 2375 switch (src.repo_kind) { 2376 inline else => |repo_kind| { 2377 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2378 defer any_repo.deinit(io, allocator); 2379 switch (any_repo) { 2380 inline else => |*repo| { 2381 var resolution: Event.Resolve = .{ 2382 .title = title, 2383 .tags = tags, 2384 .hunks = hunks.items, 2385 }; 2386 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2387 const change = Event.Update{ .resolve = resolution }; 2388 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2389 // leave the form up for correction 2390 error.InvalidFields => return, 2391 else => |e| return e, 2392 }; 2393 }, 2394 } 2395 }, 2396 } 2397 2398 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2399 try self.session.navigate(route); 2400 } 2401 2402 // commit a new or edited comment and navigate to its permalink. this is the 2403 // terminal path; the web posts the form to its comment route. 2404 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2405 if (comptime wasm) return; 2406 const io = self.session.io orelse return; 2407 const src = self.data.repo_source orelse return; 2408 const author = (try self.session.eventAuthor()) orelse return; 2409 const form = self.threadForm() orelse return; 2410 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2411 const body = try body_input.text(allocator); 2412 defer allocator.free(body); 2413 if (!evt.Comment.fieldsValid(body)) return; 2414 2415 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2416 @memcpy(&thread_id, self.data.selected_id); 2417 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2418 var parent: [evt.event_id_size * 2]u8 = undefined; 2419 @memcpy(&parent, self.data.comment_id); 2420 break :blk parent; 2421 }; 2422 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2423 const editing = self.data.view == .edit_comment; 2424 switch (src.repo_kind) { 2425 inline else => |repo_kind| { 2426 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2427 defer any_repo.deinit(io, allocator); 2428 switch (any_repo) { 2429 inline else => |*repo| if (editing) { 2430 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2431 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2432 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2433 } else { 2434 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2435 }, 2436 } 2437 }, 2438 } 2439 2440 body_input.clear(allocator); 2441 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2442 try self.session.navigate(route); 2443 } 2444 2445 // the terminal submit paths: the new form commits a new thread event, the 2446 // edit form re-emits the selected thread's event with the form's content. 2447 // the web posts the forms to their routes instead. 2448 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2449 if (self.data.view == .edit) { 2450 try self.submitEditedThread(allocator); 2451 } else { 2452 try self.submitNewThread(allocator); 2453 } 2454 } 2455 2456 // commit the new thread to the repo's events branch and navigate to it. 2457 // this is the terminal path; the web posts the form to the thread route, 2458 // so the wasm side never runs (or compiles) the repo access below. 2459 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2460 if (comptime wasm) return; 2461 const io = self.session.io orelse return; 2462 const src = self.data.repo_source orelse return; 2463 const author = (try self.session.eventAuthor()) orelse return; 2464 2465 const form = self.threadForm() orelse return; 2466 const title_input = &form.children.values()[title_field_index].widget.text_input; 2467 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2468 const description_input = &form.children.values()[description_field_index].widget.text_input; 2469 2470 const title = try title_input.text(allocator); 2471 defer allocator.free(title); 2472 const tags = try tags_input.text(allocator); 2473 defer allocator.free(tags); 2474 const description = try description_input.text(allocator); 2475 defer allocator.free(description); 2476 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2477 defer if (comptime supports_drafts) allocator.free(target_branch); 2478 2479 if (!Event.fieldsValid(title, tags)) return; 2480 2481 if (comptime supports_drafts) { 2482 const event_id_hex = Data.createDraft(self.data, self.session, allocator, title, tags, description, target_branch) catch return; 2483 title_input.clear(allocator); 2484 tags_input.clear(allocator); 2485 description_input.clear(allocator); 2486 form.children.values()[target_branch_field_index].widget.text_input.clear(allocator); 2487 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2488 try self.session.navigate(route); 2489 return; 2490 } 2491 2492 var id_bytes: [evt.event_id_size]u8 = undefined; 2493 io.random(&id_bytes); 2494 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2495 2496 const event = evt.EventWithId{ 2497 .id = event_id_hex, 2498 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2499 .author = author, 2500 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2501 .title = title, 2502 .description = description, 2503 .tags = tags, 2504 }), 2505 }; 2506 2507 switch (src.repo_kind) { 2508 inline else => |repo_kind| { 2509 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2510 defer any_repo.deinit(io, allocator); 2511 switch (any_repo) { 2512 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2513 } 2514 }, 2515 } 2516 2517 // wipe the form so a return visit starts fresh 2518 title_input.clear(allocator); 2519 tags_input.clear(allocator); 2520 description_input.clear(allocator); 2521 2522 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2523 try self.session.navigate(route); 2524 } 2525 2526 // re-emit the selected thread's event with the form's content (status 2527 // preserved), then reload the thread's page. this is the terminal path; 2528 // the web posts the form to the edit route. 2529 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2530 if (comptime wasm) return; 2531 const io = self.session.io orelse return; 2532 const entry = self.data.selectedThread() orelse return; 2533 2534 const form = self.threadForm() orelse return; 2535 const title_input = &form.children.values()[title_field_index].widget.text_input; 2536 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2537 const description_input = &form.children.values()[description_field_index].widget.text_input; 2538 2539 const title = try title_input.text(allocator); 2540 defer allocator.free(title); 2541 const tags = try tags_input.text(allocator); 2542 defer allocator.free(tags); 2543 const description = try description_input.text(allocator); 2544 defer allocator.free(description); 2545 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2546 defer if (comptime supports_drafts) allocator.free(target_branch); 2547 2548 if (!Event.fieldsValid(title, tags)) return; 2549 2550 if (comptime supports_drafts) { 2551 if (entryDraft(entry.*)) { 2552 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch return; 2553 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2554 try self.session.navigate(route); 2555 return; 2556 } 2557 } 2558 2559 const id_bytes = try evt.parseEventId(entry.id); 2560 const src = self.data.repo_source orelse return; 2561 const author = (try self.session.eventAuthor()) orelse return; 2562 2563 switch (src.repo_kind) { 2564 inline else => |repo_kind| { 2565 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2566 defer any_repo.deinit(io, allocator); 2567 switch (any_repo) { 2568 inline else => |*repo| if (supports_drafts) { 2569 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2570 .title = title, 2571 .tags = tags, 2572 .description = description, 2573 .target_branch = target_branch, 2574 } }, author); 2575 } else if (has_status) 2576 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2577 .title = title, 2578 .tags = tags, 2579 .description = description, 2580 } }, author) 2581 else 2582 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2583 } 2584 }, 2585 } 2586 2587 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2588 try self.session.navigate(route); 2589 } 2590 2591 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2592 switch (confirmation(self.data.view) orelse return) { 2593 .publish => if (comptime supports_drafts) { 2594 if (comptime wasm) return; 2595 const entry = self.data.selectedThread() orelse return; 2596 if (!entryDraft(entry.*)) return; 2597 try self.data.publishDraft(self.session, allocator, entry.id); 2598 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2599 try self.session.navigate(route); 2600 } else unreachable, 2601 .merge => if (comptime supports_merge) { 2602 if (comptime wasm) return; 2603 const entry = self.data.selectedThread() orelse return; 2604 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2605 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2606 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2607 try self.session.navigate(route); 2608 } else unreachable, 2609 .remove => try self.submitRemove(allocator), 2610 } 2611 } 2612 2613 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2614 const comment = self.data.comment_id.len != 0; 2615 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2616 const id = evt.parseEventId(event_id) catch return; 2617 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2618 } 2619 2620 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2621 if (comptime wasm) return; 2622 2623 if (comptime supports_drafts) { 2624 const selected = self.data.selectedThread() orelse return; 2625 if (event_kind == kind and entryDraft(selected.*)) { 2626 try Data.removeDraft(self.session, allocator, &id); 2627 const route = draftsRoute(self.data.identity) orelse return; 2628 try self.session.navigate(route); 2629 return; 2630 } 2631 } 2632 2633 const io = self.session.io orelse return; 2634 const src = self.data.repo_source orelse return; 2635 const author = (try self.session.eventAuthor()) orelse return; 2636 2637 switch (src.repo_kind) { 2638 inline else => |repo_kind| { 2639 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2640 defer any_repo.deinit(io, allocator); 2641 switch (any_repo) { 2642 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2643 } 2644 }, 2645 } 2646 2647 const id_hex = std.fmt.bytesToHex(id, .lower); 2648 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2649 try self.session.navigate(route); 2650 } 2651 2652 // return to the list. 2653 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2654 root_focus.setFocus(self.listScroll(index).getFocus().id); 2655 } 2656 2657 // cross to the header tabs above the stack. 2658 fn focusHeader(self: *This, root_focus: *Focus) void { 2659 root_focus.setFocus(self.box.children.keys()[header_index]); 2660 } 2661 2662 pub fn clearGrid(self: *This) void { 2663 self.box.clearGrid(); 2664 } 2665 2666 pub fn getGrid(self: This) ?Grid { 2667 return self.box.getGrid(); 2668 } 2669 2670 pub fn getFocus(self: *This) *Focus { 2671 return self.box.getFocus(); 2672 } 2673 2674 // for the parent's "scroll up at the top jumps to the header" check: at the 2675 // top only while the header tabs hold focus, so up from the split 2676 // crosses into the tabs first. 2677 pub fn getSelectedIndex(self: *This) ?usize { 2678 return if (self.headerActive()) 0 else 1; 2679 } 2680 }; 2681 } 2682