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