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