╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Threads.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
557 for (data.tags) |tag| 558 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, @enumFromInt(0), tag) }); 559 try tf.setItems(allocator, items.items); 560 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 561 } 562 563 // the new-thread form, or — on an edit, comment, or resolve url — that form in 564 // its place, prefilled with the selected thread's content. a 565 // logged-out session can't create events, so the unauthorized view 566 // stands in. 567 if (session.data.is_local or session.data.user_id != null) { 568 if (data.view == .remove) { 569 const aa = session.page_arena.allocator(); 570 const route = ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 571 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 572 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 573 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 574 const label = try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}); 575 var center = try initRemoveForm(allocator, action, label); 576 errdefer center.deinit(allocator); 577 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 578 } else if (data.view == .new_comment or data.view == .edit_comment) { 579 const editing = data.view == .edit_comment; 580 const route = if (editing) 581 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 582 else 583 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 584 const page_url = try route.toUrl(session.page_arena); 585 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 586 const page = data.comment_page; 587 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 588 const parent_route = if (editing) 589 if (top_level) 590 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 591 else if (page) |p| 592 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 593 else 594 null 595 else if (page) |p| 596 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 597 else 598 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 599 const parent_author: ui.Author = if (editing) 600 if (top_level) 601 if (data.selectedThread()) |entry| entry.author else .unknown 602 else if (page) |p| 603 p.selected.parent_author orelse .unknown 604 else 605 .unknown 606 else if (page) |p| 607 p.selected.author 608 else if (data.selectedThread()) |entry| 609 entry.author 610 else 611 .unknown; 612 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 613 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 614 errdefer form.deinit(allocator); 615 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 616 } else if (has_conflicts and data.view == .resolve) { 617 // on the web the page grows to the form's height and the 618 // browser scrolls it; the terminal scrolls the form itself 619 var form_widget: ui.Widget = blk: { 620 var form = try initResolveForm(allocator, session, data); 621 errdefer form.deinit(allocator); 622 break :blk if (session.is_terminal) 623 .{ .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 624 else 625 .{ .box = form }; 626 }; 627 errdefer form_widget.deinit(allocator); 628 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 629 } else { 630 const aa = session.page_arena.allocator(); 631 const route = if (data.view == .edit) 632 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 633 else 634 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 635 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 636 const record = if (data.view == .edit) 637 (if (data.selectedThread()) |entry| &entry.record else null) 638 else 639 null; 640 var form = try initThreadForm(allocator, session, action, record); 641 errdefer form.deinit(allocator); 642 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 643 } 644 } else { 645 var message = try ui.Unauthorized.View.init(allocator); 646 errdefer message.deinit(allocator); 647 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 648 } 649 650 if (has_drafts) { 651 var split = try initSplit(allocator, session, data, null, true); 652 errdefer split.deinit(allocator); 653 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 654 } 655 656 // the conflicts split; its tab exists only when the repo has 657 // conflicts, so skip the child too to keep the stack 1:1 with the 658 // tabs. 659 if (has_conflicts and data.conflicts.count > 0) { 660 var split = try initSplit(allocator, session, data, null, false); 661 errdefer split.deinit(allocator); 662 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 663 } 664 665 // the stack starts on the page's view. 666 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 667 668 // focus entering the view lands on the tabs first. 669 outer.getFocus().child_id = outer.children.keys()[header_index]; 670 671 return .{ 672 .box = outer, 673 .data = data, 674 .session = session, 675 .detailed_index = @splat(null), 676 .description_id = @splat(null), 677 .author_id = @splat(null), 678 }; 679 } 680 681 // the master-detail split showing `status`'s window, or the conflicts 682 // window when null. 683 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(ui.Widget) { 684 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 685 errdefer box.deinit(allocator); 686 687 const win = if (status_maybe) |status| data.window(status) else if (drafts and has_drafts) &data.drafts else if (has_conflicts) &data.conflicts else unreachable; 688 689 // the thread list (one focusable row per title), plus a "next" link that 690 // reloads the page rooted at the following thread. 691 { 692 var list_scroll = blk: { 693 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 694 errdefer list_box.deinit(allocator); 695 if (win.prev_id) |prev| 696 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 697 for (win.items) |entry| { 698 // conflicts take precedence over the comment count 699 const label: []const u8 = if (entryConflicted(entry) and status_maybe != null) 700 " conflict " 701 else if (entry.comments.count > 0) 702 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 703 else 704 ""; 705 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 706 } 707 if (win.next_id) |next| 708 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 709 // select the window's first thread (past a leading "previous" 710 // row) so its description shows on load. 711 if (win.items.len > 0) 712 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 713 else if (list_box.children.count() > 0) 714 list_box.getFocus().child_id = list_box.children.keys()[0]; 715 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 716 }; 717 errdefer list_scroll.deinit(allocator); 718 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 } }); 719 } 720 721 // the detail pane — a frame around a scroll of the description 722 { 723 var detail_outer = blk: { 724 var detail_scroll = blk2: { 725 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 726 errdefer detail_inner.deinit(allocator); 727 // fill the pane (content top-left, scroll bar pinned to the 728 // edge) rather than shrinking to the description. 729 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 730 }; 731 errdefer detail_scroll.deinit(allocator); 732 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 733 errdefer frame.deinit(allocator); 734 // the tool row sits above the scroll (populateDetail fills 735 // it per thread) so it can't scroll out from under the web 736 // overlay <form>, whose position doesn't track pane scrolling. 737 { 738 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 739 errdefer row.deinit(allocator); 740 try frame.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 741 } 742 // the frame's selected child is its scroll, so the focus chain 743 // reaches the description (populateDetail points the scroll's 744 // inner box at it), letting focus recovery descend into the pane. 745 frame.getFocus().child_id = detail_scroll.getFocus().id; 746 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 747 break :blk frame; 748 }; 749 errdefer detail_outer.deinit(allocator); 750 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 751 } 752 753 box.getFocus().child_id = box.children.keys()[list_index]; 754 return box; 755 } 756 757 // a thread form: title/tags/description inputs and a submit button, 758 // prefilled from `record` when given. its form: subtree makes the web 759 // overlay wrap them in a <form> POSTing to `action`'s route. 760 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, record: ?*const Event.Record) !wgt.Box(ui.Widget) { 761 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 762 errdefer box.deinit(allocator); 763 box.getFocus().kind = .{ .custom = action }; 764 765 { 766 var title = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 767 errdefer title.deinit(allocator); 768 title.getFocus().focusable = true; 769 if (record) |r| try title.setContent(allocator, r.event.title); 770 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 771 } 772 773 { 774 var tags = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 775 errdefer tags.deinit(allocator); 776 tags.getFocus().focusable = true; 777 if (record) |r| try tags.setContent(allocator, r.event.tags); 778 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 779 } 780 781 { 782 var description = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 783 errdefer description.deinit(allocator); 784 description.getFocus().focusable = true; 785 if (record) |r| try description.setContent(allocator, r.event.description); 786 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 787 } 788 789 try addSubmitButtonLabeled(allocator, &box, if (kind == .patch and record == null) "submit draft" else "submit"); 790 791 box.getFocus().child_id = box.children.keys()[title_field_index]; 792 return box; 793 } 794 795 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(ui.Widget) { 796 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 797 errdefer box.deinit(allocator); 798 box.getFocus().kind = .{ .custom = action }; 799 800 { 801 var parent = try ui.authorBox(allocator, session.page_arena, author); 802 errdefer parent.deinit(allocator); 803 parent.options.label = " replying to "; 804 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 805 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 806 } 807 808 { 809 var body = try wgt.TextInput(ui.Widget).init(allocator, .{ 810 .label = " comment ", 811 .name = "body", 812 .visible_width = null, 813 .rounded_corners = true, 814 .render_content = session.is_terminal, 815 .multiline = true, 816 .visible_height = 5, 817 .scroll = .{ .fill = true }, 818 }); 819 errdefer body.deinit(allocator); 820 body.getFocus().focusable = true; 821 if (initial_body) |text| try body.setContent(allocator, text); 822 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 823 } 824 825 try addSubmitButtonLabeled(allocator, &box, "submit"); 826 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 827 return box; 828 } 829 830 fn initRemoveForm(allocator: std.mem.Allocator, action: []const u8, label: []const u8) !ui.Center { 831 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 832 errdefer box.deinit(allocator); 833 box.getFocus().kind = .{ .custom = action }; 834 835 var prompt = try wgt.Text(ui.Widget).init(allocator, "are you sure?"); 836 errdefer prompt.deinit(allocator); 837 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 838 839 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 840 errdefer button.deinit(allocator); 841 button.getFocus().focusable = true; 842 button.getFocus().kind = .{ .custom = "submit" }; 843 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 844 box.getFocus().child_id = button.getFocus().id; 845 846 return ui.Center.init(allocator, .{ .box = box }); 847 } 848 849 // a form's submit button, then a spacer absorbing the leftover 850 // min-height the box hands its last child, so the button keeps its 851 // natural height 852 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8) !void { 853 { 854 var submit = try ui.SubmitButton.initLabeled(allocator, label); 855 errdefer submit.deinit(allocator); 856 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 857 } 858 { 859 var spacer = try ui.Spacer.init(allocator); 860 errdefer spacer.deinit(allocator); 861 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 862 } 863 } 864 865 // the conflict resolve form. the "use this" links are navigations that 866 // flip the url's theirs: pick, reloading the prefills; one submit 867 // settles every conflict. the form: subtree makes the web overlay POST 868 // to the resolve route. 869 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(ui.Widget) { 870 const aa = session.page_arena.allocator(); 871 // init only picks the resolve view once it has read the conflict 872 const conflict = if (data.conflict) |*c| c else unreachable; 873 874 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 875 errdefer box.deinit(allocator); 876 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 877 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 878 879 if (conflict.title) |*fc| { 880 try addLabel(allocator, &box, "title conflict:"); 881 try addFieldConflict(allocator, &box, session, data, "title", fc); 882 } 883 if (conflict.tags) |*fc| { 884 try addLabel(allocator, &box, "tags conflict:"); 885 try addFieldConflict(allocator, &box, session, data, "tags", fc); 886 } 887 if (comptime kind == .patch) { 888 if (conflict.status) |*fc| { 889 try addLabel(allocator, &box, "status conflict:"); 890 try addAtomicConflict(allocator, &box, session, data, "status", fc); 891 } 892 if (conflict.revision) |*fc| { 893 try addLabel(allocator, &box, "revision conflict:"); 894 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 895 } 896 } 897 898 if (conflict.description) |*desc| { 899 try addLabel(allocator, &box, "description conflict:"); 900 var hunk_index: usize = 0; 901 for (desc.chunks, 0..) |*chunk, chunk_index| { 902 switch (chunk.*) { 903 .same => |text| { 904 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 905 errdefer tb.deinit(allocator); 906 tb.getFocus().focusable = true; 907 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 908 }, 909 // an auto-resolved chunk: only one side changed it, so its 910 // version stands, shown distinctly with its author 911 .auto => |auto| { 912 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 913 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 914 const label = switch (author) { 915 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 916 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 917 }; 918 var tb = try wgt.TextBox(ui.Widget).init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 919 errdefer tb.deinit(allocator); 920 tb.getFocus().focusable = true; 921 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 922 }, 923 .conflict => |hunk| { 924 // a blank row on each side sets the group apart from 925 // the flowing shared and auto-resolved chunks 926 if (chunk_index > 0) try addGap(allocator, &box); 927 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 928 hunk_index += 1; 929 const picked_theirs = data.theirsPicked(name); 930 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 931 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 932 var resolution_input = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 933 errdefer resolution_input.deinit(allocator); 934 resolution_input.getFocus().focusable = true; 935 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 936 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 937 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 938 }, 939 } 940 } 941 } 942 943 try addSubmitButtonLabeled(allocator, &box, "submit"); 944 945 box.getFocus().child_id = box.children.keys()[0]; 946 return box; 947 } 948 949 // one conflicted scalar field: both sides' version rows, then the 950 // resolution input prefilled from the picked side. 951 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 952 const aa = session.page_arena.allocator(); 953 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 954 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 955 956 var resolution_input = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 957 errdefer resolution_input.deinit(allocator); 958 resolution_input.getFocus().focusable = true; 959 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 960 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 961 } 962 963 // atomic fields can only pick one complete side 964 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 965 const aa = session.page_arena.allocator(); 966 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 967 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 968 var selected = try wgt.TextBox(ui.Widget).init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 969 .border_style = .single, 970 .rounded_corners = true, 971 .wrap_kind = .word, 972 .label = " resolution ", 973 }); 974 errdefer selected.deinit(allocator); 975 selected.getFocus().focusable = true; 976 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 977 } 978 979 // a blank row setting a conflict group apart from its neighbors 980 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 981 var text = try wgt.Text(ui.Widget).init(allocator, " "); 982 errdefer text.deinit(allocator); 983 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 984 } 985 986 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), content: []const u8) !void { 987 var label = try ui.SectionLabel.init(allocator, content); 988 errdefer label.deinit(allocator); 989 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 990 } 991 992 // one side of a conflict: the "use this" link on the left, the version 993 // itself as a labeled read-only box. 994 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, text: []const u8, link: []const u8) !void { 995 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 996 errdefer row.deinit(allocator); 997 998 { 999 const use_label = "use this"; 1000 var use = try wgt.TextBox(ui.Widget).init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1001 errdefer use.deinit(allocator); 1002 use.getFocus().focusable = true; 1003 use.getFocus().kind = .{ .custom = link }; 1004 try row.children.put(allocator, use.getFocus().id, .{ 1005 .widget = .{ .text_box = use }, 1006 .rect = null, 1007 .min_size = .{ .width = use_label.len + 2, .height = null }, 1008 .max_size = .{ .width = use_label.len + 2, .height = null }, 1009 }); 1010 } 1011 1012 { 1013 var tb = try wgt.TextBox(ui.Widget).init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1014 errdefer tb.deinit(allocator); 1015 tb.getFocus().focusable = true; 1016 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1017 } 1018 1019 row.getFocus().child_id = row.children.keys()[0]; 1020 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1021 } 1022 1023 // the label of one side's version box, naming its author 1024 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1025 return switch (author) { 1026 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1027 .unknown => if (ours) " current version " else " their version ", 1028 }; 1029 } 1030 1031 // the a: link that reloads the resolve page with `name` picked from ours 1032 // or theirs, reprefilling its resolution input 1033 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1034 const aa = session.page_arena.allocator(); 1035 var picks: std.ArrayList(u8) = .empty; 1036 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1037 while (it.next()) |pick| { 1038 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1039 if (picks.items.len > 0) try picks.append(aa, ','); 1040 try picks.appendSlice(aa, pick); 1041 } 1042 if (theirs) { 1043 if (picks.items.len > 0) try picks.append(aa, ','); 1044 try picks.appendSlice(aa, name); 1045 } 1046 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1047 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1048 } 1049 1050 // the resolve form's page-constant initial value for the input `name`: 1051 // the picked side of its conflicted field or hunk 1052 fn resolvePrefill(self: *This, name: []const u8) ?[]const u8 { 1053 const conflict = if (self.data.conflict) |*c| c else return null; 1054 const theirs = self.data.theirsPicked(name); 1055 if (std.mem.eql(u8, name, "title")) { 1056 const fc = if (conflict.title) |*f| f else return null; 1057 return if (theirs) fc.theirs.text else fc.ours.text; 1058 } 1059 if (std.mem.eql(u8, name, "tags")) { 1060 const fc = if (conflict.tags) |*f| f else return null; 1061 return if (theirs) fc.theirs.text else fc.ours.text; 1062 } 1063 if (std.mem.startsWith(u8, name, "d")) { 1064 const desc = if (conflict.description) |*d| d else return null; 1065 const hunk_index = std.fmt.parseInt(usize, name[1..], 10) catch return null; 1066 var seen: usize = 0; 1067 for (desc.chunks) |chunk| switch (chunk) { 1068 .conflict => |hunk| { 1069 if (seen == hunk_index) return (if (theirs) hunk.theirs else hunk.ours) orelse ""; 1070 seen += 1; 1071 }, 1072 else => {}, 1073 }; 1074 } 1075 return null; 1076 } 1077 1078 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1079 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1080 errdefer row.deinit(allocator); 1081 row.getFocus().focusable = true; 1082 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1083 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1084 } 1085 1086 fn addToolButton(allocator: std.mem.Allocator, row: *wgt.Box(ui.Widget), label: []const u8, action: []const u8) !void { 1087 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1088 errdefer button.deinit(allocator); 1089 button.getFocus().focusable = true; 1090 button.getFocus().kind = .{ .custom = action }; 1091 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = try xitui.width.displayWidth(label) + 2, .height = null } }); 1092 } 1093 1094 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 1095 self.box.deinit(allocator); 1096 } 1097 1098 fn header(self: *This) *HeaderType { 1099 return switch (kind) { 1100 .issue => &self.box.children.values()[header_index].widget.repo_issues_header, 1101 .patch => &self.box.children.values()[header_index].widget.repo_patches_header, 1102 .discuss => &self.box.children.values()[header_index].widget.repo_discussions_header, 1103 else => @compileError("unsupported thread event kind"), 1104 }; 1105 } 1106 1107 fn viewStack(self: *This) *wgt.Stack(ui.Widget) { 1108 return &self.box.children.values()[stack_index].widget.stack; 1109 } 1110 1111 // `index`'s master-detail split inside the stack. 1112 fn resultsBox(self: *This, index: usize) *wgt.Box(ui.Widget) { 1113 return &self.viewStack().children.values()[index].box; 1114 } 1115 1116 // the tags view's flow inside the stack. 1117 fn tagsView(self: *This) *ui.TagFlow { 1118 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1119 } 1120 1121 // the new-thread, edit, or resolve form inside the stack, or null when the 1122 // unauthorized view stands in for it. 1123 fn threadForm(self: *This) ?*wgt.Box(ui.Widget) { 1124 return switch (self.viewStack().children.values()[form_view_index]) { 1125 .box => |*box| box, 1126 // the resolve form sits inside a scroll on the terminal 1127 .scroll => |*scroll| &scroll.child.box, 1128 else => null, 1129 }; 1130 } 1131 1132 fn removeForm(self: *This) ?*wgt.Box(ui.Widget) { 1133 return switch (self.viewStack().children.values()[form_view_index]) { 1134 .center => |*center| switch (center.child.*) { 1135 .box => |*box| box, 1136 else => null, 1137 }, 1138 else => null, 1139 }; 1140 } 1141 1142 // the resolve form's scroll on the terminal (the web page scrolls itself) 1143 fn resolveScroll(self: *This) ?*wgt.Scroll(ui.Widget) { 1144 return switch (self.viewStack().children.values()[form_view_index]) { 1145 .scroll => |*scroll| scroll, 1146 else => null, 1147 }; 1148 } 1149 1150 fn listScroll(self: *This, index: usize) *wgt.Scroll(ui.Widget) { 1151 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1152 } 1153 1154 fn listBox(self: *This, index: usize) *wgt.Box(ui.Widget) { 1155 return &self.listScroll(index).child.box; 1156 } 1157 1158 // the detail frame's children: the tool row above the scroll pane. 1159 const tool_row_index: usize = 0; 1160 const detail_scroll_index: usize = 1; 1161 1162 fn detailOuter(self: *This, index: usize) *wgt.Box(ui.Widget) { 1163 return &self.resultsBox(index).children.values()[detail_index].widget.box; 1164 } 1165 1166 fn toolRow(self: *This, index: usize) *wgt.Box(ui.Widget) { 1167 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 1168 } 1169 1170 fn detailScroll(self: *This, index: usize) *wgt.Scroll(ui.Widget) { 1171 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 1172 } 1173 1174 fn detailInner(self: *This, index: usize) *wgt.Box(ui.Widget) { 1175 return &self.detailScroll(index).child.box; 1176 } 1177 1178 fn window(self: *This, index: usize) *const Window { 1179 if (has_conflicts and index == conflict_view_index) return &self.data.conflicts; 1180 if (has_drafts and index == drafts_view_index) return &self.data.drafts; 1181 return self.data.window(splitStatus(index)); 1182 } 1183 1184 fn stackSelectedIndex(self: *This) ?usize { 1185 const stack = self.viewStack(); 1186 const cid = stack.getFocus().child_id orelse return null; 1187 return stack.children.getIndex(cid); 1188 } 1189 1190 // the stack's selected master-detail split (null when the tags view or the 1191 // new-thread form shows). 1192 fn selectedSplitIndex(self: *This) ?usize { 1193 const idx = self.stackSelectedIndex() orelse return null; 1194 return if (idx < status_count or (has_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 1195 } 1196 1197 fn detailActive(self: *This, index: usize) bool { 1198 const rb = self.resultsBox(index); 1199 const cid = rb.getFocus().child_id orelse return false; 1200 return rb.children.getIndex(cid) == detail_index; 1201 } 1202 1203 fn headerActive(self: *This) bool { 1204 const cid = self.box.getFocus().child_id orelse return false; 1205 return self.box.children.getIndex(cid) == header_index; 1206 } 1207 1208 fn tagsViewActive(self: *This) bool { 1209 if (self.headerActive()) return false; 1210 return self.stackSelectedIndex() == tags_view_index; 1211 } 1212 1213 fn formViewActive(self: *This) bool { 1214 if (self.headerActive()) return false; 1215 return self.stackSelectedIndex() == form_view_index; 1216 } 1217 1218 // the selected thread's index, or null when a window-navigation row is 1219 // selected (a leading "previous" row shifts the thread rows down by one). 1220 fn selectedThreadIndex(self: *This, index: usize) ?usize { 1221 const lb = self.listBox(index); 1222 const cid = lb.getFocus().child_id orelse return null; 1223 const idx = lb.children.getIndex(cid) orelse return null; 1224 const win = self.window(index); 1225 const lead: usize = if (win.prev_id != null) 1 else 0; 1226 if (idx < lead or idx - lead >= win.items.len) return null; 1227 return idx - lead; 1228 } 1229 1230 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1231 self.clearGrid(); 1232 1233 // each header tab maps 1:1 to a stack child by position 1234 if (self.header().getSelectedIndex()) |index| { 1235 const stack = self.viewStack(); 1236 stack.getFocus().child_id = stack.children.keys()[index]; 1237 } 1238 1239 if (self.selectedSplitIndex()) |i| { 1240 // swap the detail pane to the selected thread when it changes. 1241 if (self.selectedThreadIndex(i)) |selected| { 1242 const changed = if (self.detailed_index[i]) |current| current != selected else true; 1243 if (changed) { 1244 try self.populateDetail(allocator, i, selected); 1245 self.detailed_index[i] = selected; 1246 } 1247 } 1248 1249 // the selected list row shows a border (the focused TextBox 1250 // upgrades it to a double border itself); the rest stay borderless. 1251 const lb = self.listBox(i); 1252 for (lb.children.keys(), lb.children.values()) |id, *child| { 1253 switch (child.widget) { 1254 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 1255 else => {}, 1256 } 1257 } 1258 1259 // the pane's selected child shows the selection border: the 1260 // description directly, the tags via the flow's selected item. 1261 // cap the list at list_max_width only while the detail pane fits 1262 // beside it. the box drops the detail when the width can't hold 1263 // both minimums, so when it's that narrow we lift the cap and let 1264 // the list fill the whole width. 1265 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 1266 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 1267 1268 // stretch the detail pane across the rest of the width so it fills 1269 // the area rather than shrinking to its content; its scroll fills 1270 // the pane. 1271 if (constraint.max_size.width) |w| { 1272 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 1273 } else { 1274 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 1275 } 1276 } 1277 1278 // refresh the form inputs' entries in the session's focus-id -> input 1279 // map with this frame's addresses, so the web/wasm form handling can 1280 // find them by focus id 1281 if (self.threadForm()) |form| { 1282 const inputs_arena = self.session.arena.allocator(); 1283 for (form.children.values()) |*child| switch (child.widget) { 1284 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 1285 else => {}, 1286 }; 1287 1288 // on an edit page, register the thread's content as the inputs' 1289 // page-constant initial values, which the web overlay renders 1290 // into them. 1291 if (self.data.view == .edit) { 1292 if (self.data.selectedThread()) |entry| { 1293 const fields = form.children.values(); 1294 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.record.event.title); 1295 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.record.event.tags); 1296 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.record.event.description); 1297 } 1298 } 1299 1300 if (self.data.view == .edit_comment) { 1301 if (self.data.comment_page) |page| { 1302 const body = &form.children.values()[comment_body_field_index].widget.text_input; 1303 try self.session.input_values.put(inputs_arena, body.getFocus().id, page.selected.comment.event.body); 1304 } 1305 } 1306 1307 // the resolve form's inputs prefill from the picked side, which 1308 // is url state and so page-constant like the edit values above 1309 if (has_conflicts and self.data.view == .resolve) { 1310 for (form.children.values()) |*child| switch (child.widget) { 1311 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 1312 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 1313 }, 1314 else => {}, 1315 }; 1316 } 1317 } 1318 1319 try self.box.build(allocator, constraint, root_focus); 1320 } 1321 1322 fn populateDetail(self: *This, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1323 const entry = self.window(index).items[sel]; 1324 const inner = self.detailInner(index); 1325 // the /description page replaces its thread's detail with a back link 1326 // and the whole description; other threads keep their normal detail. 1327 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1328 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 1329 1330 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1331 inner.children.clearAndFree(allocator); 1332 inner.getFocus().child_id = null; 1333 self.author_id[index] = null; 1334 1335 // the tool row; the description page shows none. 1336 { 1337 const row = self.toolRow(index); 1338 for (row.children.values()) |*child| child.widget.deinit(allocator); 1339 row.children.clearAndFree(allocator); 1340 row.getFocus().child_id = null; 1341 row.getFocus().kind = .container; 1342 } 1343 if (!description_page and comment_page == null) { 1344 const row = self.toolRow(index); 1345 const pa = self.session.page_arena.allocator(); 1346 1347 { 1348 var spacer = try ui.Spacer.init(allocator); 1349 errdefer spacer.deinit(allocator); 1350 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1351 } 1352 1353 if (has_drafts and entryDraft(entry)) { 1354 if (entry.revision_ready) { 1355 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return error.RouteTooLong; 1356 row.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "form:{s}/post", .{try route.toUrl(self.session.page_arena)}) }; 1357 try addToolButton(allocator, row, "post", "submit"); 1358 } 1359 if (row.children.count() > first_in_row_index) 1360 row.getFocus().child_id = row.children.keys()[first_in_row_index]; 1361 } else { 1362 // a terminal has no file picker, so attaching is web only. the 1363 // renderer covers this button with a file input, so clicking it 1364 // opens the browser's own picker and posts what it chose. 1365 if (!self.session.is_terminal and !entryConflicted(entry) and (self.session.data.is_local or self.session.data.user_id != null)) { 1366 const label = "add attachment"; 1367 const action = if (self.data.identity.len == 0) 1368 try std.fmt.allocPrint(pa, "{s}/{s}:{s}/attach", .{ ui.file_input_prefix, @tagName(kind), entry.id }) 1369 else 1370 try std.fmt.allocPrint(pa, "{s}/repo/{s}/{s}:{s}/attach", .{ ui.file_input_prefix, self.data.identity, @tagName(kind), entry.id }); 1371 try addToolButton(allocator, row, label, action); 1372 } 1373 1374 // a conflicted thread's only action is resolving it. the button 1375 // stays visible logged out; the resolve page shows the 1376 // unauthorized view then. 1377 if (has_conflicts and entryConflicted(entry)) { 1378 const label = "resolve conflict"; 1379 const route = resolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1380 try addToolButton(allocator, row, label, try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 1381 } else { 1382 // a logged-out session can't flip the status, so it gets no 1383 // open/close button 1384 if (has_status and (self.session.data.is_local or self.session.data.user_id != null)) { 1385 if (statusChange(entryStatus(entry))) |change| { 1386 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1387 row.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "form:{s}/{s}", .{ try route.toUrl(self.session.page_arena), change.action }) }; 1388 try addToolButton(allocator, row, change.action, "submit"); 1389 } 1390 } 1391 1392 // the edit button links to the thread's edit page. 1393 { 1394 const route = ui.RoutablePage.repoThreadEditRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 1395 try addToolButton(allocator, row, "edit", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 1396 } 1397 } 1398 1399 if (self.session.data.is_local or self.session.data.user_id != null) { 1400 const route = ui.RoutablePage.repoThreadRemoveRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1401 try addToolButton(allocator, row, "✕", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 1402 } 1403 1404 // the leftmost button: the attachment button when the session has 1405 // one, else the resolve button on a conflicted thread, the 1406 // open/close button when present, the edit button otherwise 1407 row.getFocus().child_id = row.children.keys()[first_in_row_index]; 1408 } 1409 } 1410 1411 if (comment_page) |page| { 1412 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 1413 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 1414 var back = try Comment.linkBox(allocator, self.session, back_label, ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1415 errdefer back.deinit(allocator); 1416 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1417 1418 try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, page.selected); 1419 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies"); 1420 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1421 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1422 1423 var spacer = try ui.Spacer.init(allocator); 1424 errdefer spacer.deinit(allocator); 1425 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1426 1427 inner.getFocus().child_id = inner.children.keys()[0]; 1428 const sc = self.detailScroll(index); 1429 sc.x = 0; 1430 sc.y = 0; 1431 sc.getFocus().version +%= 1; 1432 return; 1433 } 1434 1435 if (description_page) { 1436 // the back link, in the title slot so the pane's input handling 1437 // applies to it unchanged. 1438 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 1439 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 1440 var tb = try wgt.TextBox(ui.Widget).init(allocator, back_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1441 errdefer tb.deinit(allocator); 1442 tb.getFocus().focusable = true; 1443 tb.getFocus().kind = .{ .custom = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), "", entry.id) }; 1444 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1445 } else { 1446 // the thread's title as a focusable word-wrapped text box. 1447 { 1448 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.record.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1449 errdefer tb.deinit(allocator); 1450 tb.getFocus().focusable = true; 1451 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1452 } 1453 1454 self.author_id[index] = blk: { 1455 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1456 errdefer tb.deinit(allocator); 1457 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1458 break :blk tb.getFocus().id; 1459 }; 1460 1461 // the thread's tags, each linking to this status's list filtered to 1462 // that tag. 1463 { 1464 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1465 defer items.deinit(allocator); 1466 var tag_iter = Event.tagIterator(entry.record.event.tags); 1467 while (tag_iter.next()) |tag| { 1468 if (tag.len == 0) continue; 1469 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entryStatus(entry), tag) }); 1470 } 1471 if (items.items.len > 0) { 1472 var tf = try ui.TagFlow.init(allocator); 1473 errdefer tf.deinit(allocator); 1474 try tf.setItems(allocator, items.items); 1475 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1476 } 1477 } 1478 } 1479 1480 // the description as a focusable word-wrapped text box. past the limit 1481 // it's cut at the last whole line and links to the /description page, 1482 // which shows the whole thing. 1483 self.description_id[index] = blk: { 1484 const whole = entry.record.event.description; 1485 const cut_short = !description_page and whole.len > Data.max_description_size; 1486 const shown = if (cut_short) 1487 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..Data.max_description_size], '\n') orelse 0], " \t\r\n") 1488 else if (whole.len == 0) 1489 "(no description)" 1490 else 1491 whole; 1492 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1493 .border_style = .single, 1494 .rounded_corners = true, 1495 .wrap_kind = .word, 1496 .label = " description ", 1497 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1498 }); 1499 errdefer tb.deinit(allocator); 1500 tb.getFocus().focusable = true; 1501 if (cut_short) { 1502 const route = ui.RoutablePage.repoThreadDescriptionRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 1503 tb.getFocus().kind = .{ .custom = try std.fmt.allocPrint(self.session.page_arena.allocator(), "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1504 } 1505 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1506 break :blk tb.getFocus().id; 1507 }; 1508 1509 if (has_drafts and entryDraft(entry)) { 1510 if (self.data.target_branch.len != 0 and self.session.data.git_ssh_port != null) { 1511 var push = try ui.GitRemote.View.initPush(allocator, self.session, self.data.identity, entry.id, self.data.target_branch); 1512 errdefer push.deinit(allocator); 1513 try inner.children.put(allocator, push.getFocus().id, .{ .widget = .{ .git_remote = push }, .rect = null, .min_size = null }); 1514 } 1515 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1516 const sc = self.detailScroll(index); 1517 sc.x = 0; 1518 sc.y = 0; 1519 sc.getFocus().version +%= 1; 1520 return; 1521 } 1522 1523 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1524 try Attachment.appendRows(allocator, inner, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 1525 1526 if (!description_page) { 1527 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoThreadCommentNewRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1528 errdefer reply.deinit(allocator); 1529 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1530 1531 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments"); 1532 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1533 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1534 1535 var spacer = try ui.Spacer.init(allocator); 1536 errdefer spacer.deinit(allocator); 1537 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1538 } 1539 1540 // select the title by default 1541 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1542 1543 // reset the scroll to the top for the newly-shown thread: directly on the 1544 // terminal (the wasm offset), and via a version bump on the web (so the 1545 // renderer's scroll id changes and JS drops the preserved position). 1546 const sc = self.detailScroll(index); 1547 sc.x = 0; 1548 sc.y = 0; 1549 sc.getFocus().version +%= 1; 1550 } 1551 1552 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1553 // wheel input moves focus like the arrows 1554 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1555 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1556 else 1557 raw_key; 1558 1559 if (self.headerActive()) { 1560 // down from the tabs re-enters the stack if the selected view has 1561 // something to focus; other keys move the tabs. 1562 if (inp.vertDirection(key) == .down) { 1563 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1564 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1565 // nothing focusable in the unauthorized view 1566 .unauthorized => false, 1567 else => true, 1568 } else false; 1569 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1570 } else { 1571 try self.header().input(allocator, key, root_focus); 1572 } 1573 return; 1574 } 1575 if (self.tagsViewActive()) { 1576 self.tagsViewInput(key, root_focus); 1577 return; 1578 } 1579 if (self.formViewActive()) { 1580 if (self.data.view == .remove) { 1581 try self.removeFormInput(allocator, key, root_focus); 1582 } else if (has_conflicts and self.data.view == .resolve) { 1583 try self.resolveInput(allocator, key, root_focus); 1584 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1585 try self.commentFormInput(allocator, key, root_focus); 1586 } else { 1587 try self.formInput(allocator, key, root_focus); 1588 } 1589 return; 1590 } 1591 const i = self.selectedSplitIndex() orelse return; 1592 if (self.detailActive(i)) { 1593 try self.detailInput(allocator, i, key, root_focus); 1594 } else { 1595 self.listInput(i, key, root_focus); 1596 } 1597 } 1598 1599 // arrow keys move the tag selection; up from the top row crosses to the 1600 // header tabs. 1601 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 1602 const tf = self.tagsView(); 1603 const cid = tf.focus.child_id orelse return; 1604 const cur = tf.indexOfFocusId(cid) orelse return; 1605 const count = tf.text_boxes.items.len; 1606 switch (key) { 1607 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1608 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1609 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1610 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1611 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1612 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1613 else => {}, 1614 } 1615 } 1616 1617 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1618 // up/down (and the scroll wheel) move the selection a row; page up/down 1619 // jump a fixed amount. right/Enter cross into the detail pane. up from 1620 // the top row crosses into the header tabs. 1621 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1622 const lb = self.listBox(index); 1623 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1624 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1625 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1626 return; 1627 } 1628 switch (key) { 1629 .enter, .arrow_right => self.focusDetail(index, root_focus), 1630 else => {}, 1631 } 1632 } 1633 1634 fn detailInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1635 switch (key) { 1636 .page_up => { 1637 self.pageDetail(index, root_focus, -10); 1638 return; 1639 }, 1640 .page_down => { 1641 self.pageDetail(index, root_focus, 10); 1642 return; 1643 }, 1644 .home => { 1645 self.jumpDetail(index, root_focus, false); 1646 return; 1647 }, 1648 .end => { 1649 self.jumpDetail(index, root_focus, true); 1650 return; 1651 }, 1652 else => {}, 1653 } 1654 if (self.data.comment_page != null and std.mem.eql(u8, self.window(index).items[self.detailed_index[index] orelse return].id, self.data.selected_id)) { 1655 self.commentInput(index, key, root_focus); 1656 return; 1657 } 1658 if (self.toolRowFocused(index)) { 1659 try self.toolRowInput(allocator, index, key, root_focus); 1660 return; 1661 } 1662 1663 const child_index = self.focusedDetailChild(index, root_focus) orelse return; 1664 const inner = self.detailInner(index); 1665 const child_id = inner.children.keys()[child_index]; 1666 const child = &inner.children.values()[child_index].widget; 1667 1668 if (child_index == title_child_index) { 1669 switch (key) { 1670 .arrow_left => self.focusList(index, root_focus), 1671 .arrow_up => self.focusToolRow(index, root_focus), 1672 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1673 else => {}, 1674 } 1675 return; 1676 } 1677 if (child_id == self.author_id[index]) { 1678 // the author's a: link is followed by the host; arrows cross to 1679 // neighboring widgets. 1680 switch (key) { 1681 .arrow_left => self.focusList(index, root_focus), 1682 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1683 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1684 else => {}, 1685 } 1686 return; 1687 } 1688 if (child_id == self.description_id[index]) { 1689 self.descriptionInput(index, key, root_focus); 1690 return; 1691 } 1692 1693 switch (child.*) { 1694 .tag_flow => |*tf| { 1695 self.tagsInput(index, child_index, tf, key, root_focus); 1696 return; 1697 }, 1698 .git_remote => |*git_remote| { 1699 switch (key) { 1700 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1701 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1702 .arrow_left => if (git_remote.selected == 0) 1703 self.focusList(index, root_focus) 1704 else 1705 try git_remote.input(allocator, key, root_focus), 1706 else => try git_remote.input(allocator, key, root_focus), 1707 } 1708 return; 1709 }, 1710 .box => |*row| if (Attachment.removeId(row)) |attachment_id| { 1711 const remove_id = row.children.keys()[1]; 1712 switch (key) { 1713 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) self.focusList(index, root_focus), 1714 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1715 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1716 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1717 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 1718 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 1719 else => {}, 1720 } 1721 return; 1722 }, 1723 else => {}, 1724 } 1725 self.commentInput(index, key, root_focus); 1726 } 1727 1728 // enter or a click runs the primary action; links are handled by the host 1729 fn toolRowInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1730 const row = self.toolRow(index); 1731 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1732 const child_id = row.children.keys()[cur]; 1733 const on_primary = switch (row.children.values()[cur].widget.getFocus().kind) { 1734 .custom => |action| std.mem.eql(u8, action, "submit"), 1735 else => false, 1736 }; 1737 switch (key) { 1738 .arrow_left => if (cur > first_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else self.focusList(index, root_focus), 1739 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1740 .arrow_up => self.focusHeader(root_focus), 1741 .arrow_down => { 1742 self.focusDetailEdge(index, root_focus, false); 1743 self.detailScroll(index).y = 0; 1744 }, 1745 .enter => if (on_primary) try self.primaryAction(allocator, index), 1746 .mouse => |mouse| if (on_primary and inp.leftClickOn(root_focus, child_id, mouse)) try self.primaryAction(allocator, index), 1747 else => {}, 1748 } 1749 } 1750 1751 fn primaryAction(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1752 const selected = self.detailed_index[index] orelse return; 1753 const entry = self.window(index).items[selected]; 1754 if (has_drafts and entryDraft(entry)) 1755 try self.postDraft(allocator, index) 1756 else 1757 try self.toggleStatus(allocator, index); 1758 } 1759 1760 fn postDraft(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1761 if (!has_drafts or comptime wasm) return; 1762 const selected = self.detailed_index[index] orelse return; 1763 const entry = self.window(index).items[selected]; 1764 if (!entryDraft(entry) or !entry.revision_ready) return; 1765 try self.data.postDraft(self.session, allocator, entry.id); 1766 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 1767 try self.session.navigate(route); 1768 } 1769 1770 fn descriptionInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1771 const sc = self.detailScroll(index); 1772 switch (key) { 1773 .arrow_left => return self.focusList(index, root_focus), 1774 // the terminal moves through the description one line at a time; 1775 // the web lets focus changes drive its native scroll. 1776 .arrow_up => { 1777 if (!self.session.is_terminal) { 1778 _ = self.moveDetailVertical(index, root_focus, false); 1779 return; 1780 } 1781 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1782 const before = sc.y; 1783 sc.y -= 1; 1784 sc.clampToContent(); 1785 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1786 return; 1787 }, 1788 .arrow_down => { 1789 if (!self.session.is_terminal) { 1790 _ = self.moveDetailVertical(index, root_focus, true); 1791 return; 1792 } 1793 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1794 const before = sc.y; 1795 sc.y += 1; 1796 sc.clampToContent(); 1797 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1798 return; 1799 }, 1800 else => return, 1801 } 1802 } 1803 1804 fn commentInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1805 switch (key) { 1806 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1807 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1808 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1809 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1810 else => return, 1811 } 1812 } 1813 1814 // page the detail scroll, then focus the first or last visible widget. 1815 fn pageDetail(self: *This, index: usize, root_focus: *Focus, delta: isize) void { 1816 if (!self.session.is_terminal) return; 1817 const sc = self.detailScroll(index); 1818 sc.y += delta; 1819 sc.clampToContent(); 1820 self.focusVisible(index, root_focus, delta > 0); 1821 } 1822 1823 // jump to the detail's first or last focusable widget. the terminal pins 1824 // its synthetic scroll too; the browser scrolls to the new focus itself. 1825 fn jumpDetail(self: *This, index: usize, root_focus: *Focus, to_end: bool) void { 1826 if (self.session.is_terminal) { 1827 const sc = self.detailScroll(index); 1828 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1829 sc.clampToContent(); 1830 self.focusVisible(index, root_focus, to_end); 1831 } else { 1832 self.focusDetailEdge(index, root_focus, to_end); 1833 } 1834 } 1835 1836 fn focusDetailEdge(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1837 const focus = self.detailInner(index).getFocus(); 1838 var chosen: ?usize = null; 1839 for (focus.children.keys(), focus.children.values()) |id, child| { 1840 if (!child.focus.focusable) continue; 1841 chosen = id; 1842 if (!last) break; 1843 } 1844 if (chosen) |id| root_focus.setFocus(id); 1845 } 1846 1847 fn detailRectVisible(self: *This, index: usize, rect: layout.IRect) bool { 1848 const sc = self.detailScroll(index); 1849 const viewport = sc.grid orelse return false; 1850 const top = sc.y; 1851 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1852 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1853 } 1854 1855 // focus the top- or bottom-most focusable widget intersecting the viewport. 1856 fn focusVisible(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1857 const inner = self.detailInner(index); 1858 var offset: usize = 0; 1859 while (offset < inner.children.count()) : (offset += 1) { 1860 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1861 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1862 } 1863 } 1864 1865 fn focusVisibleChild(self: *This, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1866 const child = &self.detailInner(index).children.values()[child_index]; 1867 const child_rect = child.rect orelse return false; 1868 var chosen: ?usize = null; 1869 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1870 const id = self.detailInner(index).children.keys()[child_index]; 1871 chosen = id; 1872 if (!last) { 1873 root_focus.setFocus(id); 1874 return true; 1875 } 1876 } 1877 1878 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1879 if (!focus_child.focus.focusable) continue; 1880 const rect: layout.IRect = .{ 1881 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1882 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1883 .size = focus_child.rect.size, 1884 }; 1885 if (!self.detailRectVisible(index, rect)) continue; 1886 chosen = id; 1887 if (!last) break; 1888 } 1889 if (chosen) |id| { 1890 root_focus.setFocus(id); 1891 return true; 1892 } 1893 return false; 1894 } 1895 1896 fn focusAdjacentVisible(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1897 const inner = self.detailInner(index); 1898 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1899 while (true) { 1900 if (down) { 1901 next += 1; 1902 if (next >= inner.children.count()) return false; 1903 } else { 1904 if (next == 0) return false; 1905 next -= 1; 1906 } 1907 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1908 self.focusDetailChild(index, next, !down, root_focus); 1909 return true; 1910 } 1911 } 1912 } 1913 1914 fn focusedDetailChild(self: *This, index: usize, root_focus: *Focus) ?usize { 1915 const inner = self.detailInner(index); 1916 var id = root_focus.grandchild_id orelse return null; 1917 while (inner.getFocus().children.get(id)) |child| { 1918 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1919 id = child.parent_id; 1920 } 1921 return null; 1922 } 1923 1924 fn detailChildFocusable(self: *This, index: usize, child_index: usize) bool { 1925 const child = &self.detailInner(index).children.values()[child_index].widget; 1926 const focus = child.getFocus(); 1927 if (focus.focusable) return true; 1928 for (focus.children.values()) |item| { 1929 if (item.focus.focusable) return true; 1930 } 1931 return false; 1932 } 1933 1934 fn focusDetailChild(self: *This, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1935 const inner = self.detailInner(index); 1936 const child = &inner.children.values()[child_index]; 1937 var target = inner.children.keys()[child_index]; 1938 var rect = child.rect orelse return; 1939 switch (child.widget) { 1940 .repo_comment => |*comment| { 1941 if (comment.rowRect(last)) |row_rect| { 1942 rect.x += row_rect.x; 1943 rect.y += row_rect.y; 1944 rect.size = row_rect.size; 1945 } 1946 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1947 }, 1948 .box => |*box| if (box.children.count() > 0) { 1949 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1950 root_focus.setFocus(target); 1951 }, 1952 else => root_focus.setFocus(target), 1953 } 1954 const sc = self.detailScroll(index); 1955 if (sc.grid) |viewport| { 1956 const viewport_height = viewport.size.height - sc.bar_h; 1957 if (rect.size.height > viewport_height) { 1958 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1959 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1960 sc.clampToContent(); 1961 return; 1962 } 1963 } 1964 sc.scrollToRect(rect); 1965 } 1966 1967 fn moveDetailVertical(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1968 const inner = self.detailInner(index); 1969 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1970 if (inner.children.values()[current].widget == .repo_comment) { 1971 const comment = &inner.children.values()[current].widget.repo_comment; 1972 if (down and !comment.bodyFocused()) { 1973 self.focusDetailChild(index, current, true, root_focus); 1974 return true; 1975 } 1976 if (!down and comment.bodyFocused()) { 1977 self.focusDetailChild(index, current, false, root_focus); 1978 return true; 1979 } 1980 } 1981 1982 var next = current; 1983 while (true) { 1984 if (down) { 1985 next += 1; 1986 if (next >= inner.children.count()) return false; 1987 } else { 1988 if (next == 0) return false; 1989 next -= 1; 1990 } 1991 if (!self.detailChildFocusable(index, next)) continue; 1992 self.focusDetailChild(index, next, !down, root_focus); 1993 return true; 1994 } 1995 } 1996 1997 fn moveCommentHorizontal(self: *This, index: usize, root_focus: *Focus, right: bool) bool { 1998 const inner = self.detailInner(index); 1999 const current = self.focusedDetailChild(index, root_focus) orelse return false; 2000 const child = &inner.children.values()[current]; 2001 if (child.widget == .repo_comment) { 2002 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 2003 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 2004 return moved; 2005 } 2006 const row = switch (child.widget) { 2007 .box => |*box| box, 2008 else => return false, 2009 }; 2010 const selected = row.getFocus().child_id orelse return false; 2011 const selected_index = row.children.getIndex(selected) orelse return false; 2012 const target = if (right) selected_index + 1 else selected_index -| 1; 2013 if (target == selected_index or target >= row.children.count()) return false; 2014 root_focus.setFocus(row.children.keys()[target]); 2015 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 2016 return true; 2017 } 2018 2019 // up/down (and tab/shift+tab) move between a form's fields; up from the 2020 // title crosses into the header tabs. the multiline description keeps 2021 // enter and any up/down that has a row to move to. 2022 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2023 const form = self.threadForm() orelse return; 2024 const cid = form.getFocus().child_id orelse return; 2025 const cur = form.children.getIndex(cid) orelse return; 2026 2027 if (cur == description_field_index) { 2028 const child = &form.children.values()[cur]; 2029 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2030 return child.widget.input(allocator, key, root_focus); 2031 } 2032 2033 switch (key) { 2034 .arrow_up, .back_tab => if (cur > 0) 2035 root_focus.setFocus(form.children.keys()[cur - 1]) 2036 else 2037 self.focusHeader(root_focus), 2038 .arrow_down, .tab => if (cur < submit_field_index) { 2039 root_focus.setFocus(form.children.keys()[cur + 1]); 2040 }, 2041 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2042 .mouse => |mouse| if (cur == submit_field_index) { 2043 const submit = &form.children.values()[cur].widget.submit_button; 2044 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2045 }, 2046 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2047 } 2048 } 2049 2050 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2051 const form = self.threadForm() orelse return; 2052 const cid = form.getFocus().child_id orelse return; 2053 const cur = form.children.getIndex(cid) orelse return; 2054 const child = &form.children.values()[cur]; 2055 2056 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2057 return child.widget.input(allocator, key, root_focus); 2058 2059 switch (key) { 2060 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2061 root_focus.setFocus(form.children.keys()[cur - 1]) 2062 else 2063 self.focusHeader(root_focus), 2064 .arrow_down, .tab => if (cur < comment_submit_field_index) 2065 root_focus.setFocus(form.children.keys()[cur + 1]), 2066 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2067 .mouse => |mouse| if (cur == comment_submit_field_index) { 2068 const submit = &child.widget.submit_button; 2069 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2070 }, 2071 else => try child.widget.input(allocator, key, root_focus), 2072 } 2073 } 2074 2075 fn removeFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2076 const form = self.removeForm() orelse return; 2077 const button_id = form.children.keys()[1]; 2078 switch (key) { 2079 .arrow_up, .back_tab => self.focusHeader(root_focus), 2080 .enter => try self.submitRemove(allocator), 2081 .mouse => |mouse| if (inp.leftClickOn(root_focus, button_id, mouse)) try self.submitRemove(allocator), 2082 else => {}, 2083 } 2084 } 2085 2086 // the multiline hunk inputs keep enter and any up/down that has a row to 2087 // move to; enter on a "use this" link is a navigation the host follows 2088 // before this runs. 2089 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2090 const form = self.threadForm() orelse return; 2091 const cid = form.getFocus().child_id orelse return; 2092 const cur = form.children.getIndex(cid) orelse return; 2093 const child = &form.children.values()[cur]; 2094 2095 if (child.widget == .text_input) { 2096 const ti = &child.widget.text_input; 2097 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2098 return child.widget.input(allocator, key, root_focus); 2099 } 2100 2101 const on_submit = child.widget == .submit_button; 2102 2103 switch (key) { 2104 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2105 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2106 .arrow_left, .arrow_right => switch (child.widget) { 2107 .box => |*row| { 2108 const row_cid = row.getFocus().child_id orelse return; 2109 const row_index = row.children.getIndex(row_cid) orelse return; 2110 if (key == .arrow_left) { 2111 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2112 } else if (row_index + 1 < row.children.count()) { 2113 root_focus.setFocus(row.children.keys()[row_index + 1]); 2114 } 2115 }, 2116 else => try child.widget.input(allocator, key, root_focus), 2117 }, 2118 .enter => if (on_submit) try self.submitResolution(allocator), 2119 .mouse => |mouse| if (on_submit) { 2120 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2121 }, 2122 else => try child.widget.input(allocator, key, root_focus), 2123 } 2124 } 2125 2126 // a multiline input keeps enter and any up/down that has a row to move to 2127 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 2128 return switch (key) { 2129 .enter => true, 2130 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2131 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2132 else => false, 2133 }; 2134 } 2135 2136 // the neighboring focusable resolve-form child, skipping the spacer and 2137 // the blank gap rows 2138 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 2139 var i = cur; 2140 while (true) { 2141 if (down) { 2142 i += 1; 2143 if (i >= form.children.count()) return null; 2144 } else { 2145 if (i == 0) return null; 2146 i -= 1; 2147 } 2148 switch (form.children.values()[i].widget) { 2149 .spacer, .text => continue, 2150 else => return i, 2151 } 2152 } 2153 } 2154 2155 // focus the resolve-form child at `i` (a version row focuses its selected 2156 // child) and keep it visible on the terminal 2157 fn focusResolveChild(self: *This, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 2158 const child = &form.children.values()[i]; 2159 switch (child.widget) { 2160 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2161 else => root_focus.setFocus(form.children.keys()[i]), 2162 } 2163 if (self.session.is_terminal) { 2164 if (self.resolveScroll()) |sc| { 2165 if (child.rect) |rect| sc.scrollToRect(rect); 2166 } 2167 } 2168 } 2169 2170 // re-emit the conflicted thread's event with the resolved content; any 2171 // event on the thread settles its conflict entry. this is the terminal 2172 // path; the web posts the form to the resolve route. 2173 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2174 if (comptime wasm) return; 2175 const io = self.session.io orelse return; 2176 const src = self.data.repo_source orelse return; 2177 const entry = self.data.selectedThread() orelse return; 2178 const author = (try self.session.eventAuthor()) orelse return; 2179 const form = self.threadForm() orelse return; 2180 2181 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2182 var title: ?[]u8 = null; 2183 var tags: ?[]u8 = null; 2184 var hunks: std.ArrayList([]const u8) = .empty; 2185 defer { 2186 if (title) |t| allocator.free(t); 2187 if (tags) |t| allocator.free(t); 2188 for (hunks.items) |h| allocator.free(h); 2189 hunks.deinit(allocator); 2190 } 2191 for (form.children.values()) |*child| switch (child.widget) { 2192 .text_input => |*ti| { 2193 const text = try ti.text(allocator); 2194 errdefer allocator.free(text); 2195 if (std.mem.eql(u8, ti.options.name, "title")) { 2196 title = text; 2197 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2198 tags = text; 2199 } else { 2200 try hunks.append(allocator, text); 2201 } 2202 }, 2203 else => {}, 2204 }; 2205 2206 const id_bytes = try evt.parseEventId(entry.id); 2207 2208 switch (src.repo_kind) { 2209 inline else => |repo_kind| { 2210 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2211 defer any_repo.deinit(io, allocator); 2212 switch (any_repo) { 2213 inline else => |*repo| { 2214 const change: Event.Update = if (comptime kind == .patch) 2215 .{ .resolve = .{ 2216 .title = title, 2217 .tags = tags, 2218 .hunks = hunks.items, 2219 .theirs = self.data.theirs_picks, 2220 } } 2221 else 2222 .{ .resolve = .{ 2223 .title = title, 2224 .tags = tags, 2225 .hunks = hunks.items, 2226 } }; 2227 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2228 // leave the form up for correction 2229 error.InvalidFields => return, 2230 else => |e| return e, 2231 }; 2232 }, 2233 } 2234 }, 2235 } 2236 2237 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2238 try self.session.navigate(route); 2239 } 2240 2241 // commit a new or edited comment and navigate to its permalink. this is the 2242 // terminal path; the web posts the form to its comment route. 2243 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2244 if (comptime wasm) return; 2245 const io = self.session.io orelse return; 2246 const src = self.data.repo_source orelse return; 2247 const author = (try self.session.eventAuthor()) orelse return; 2248 const form = self.threadForm() orelse return; 2249 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2250 const body = try body_input.text(allocator); 2251 defer allocator.free(body); 2252 if (!evt.Comment.fieldsValid(body)) return; 2253 2254 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2255 @memcpy(&thread_id, self.data.selected_id); 2256 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2257 var parent: [evt.event_id_size * 2]u8 = undefined; 2258 @memcpy(&parent, self.data.comment_id); 2259 break :blk parent; 2260 }; 2261 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2262 const editing = self.data.view == .edit_comment; 2263 switch (src.repo_kind) { 2264 inline else => |repo_kind| { 2265 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2266 defer any_repo.deinit(io, allocator); 2267 switch (any_repo) { 2268 inline else => |*repo| if (editing) { 2269 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2270 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2271 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2272 } else { 2273 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2274 }, 2275 } 2276 }, 2277 } 2278 2279 body_input.clear(allocator); 2280 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2281 try self.session.navigate(route); 2282 } 2283 2284 // the terminal submit paths: the new form commits a new thread event, the 2285 // edit form re-emits the selected thread's event with the form's content. 2286 // the web posts the forms to their routes instead. 2287 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2288 if (self.data.view == .edit) { 2289 try self.submitEditedThread(allocator); 2290 } else { 2291 try self.submitNewThread(allocator); 2292 } 2293 } 2294 2295 // commit the new thread to the repo's events branch and navigate to it. 2296 // this is the terminal path; the web posts the form to the thread route, 2297 // so the wasm side never runs (or compiles) the repo access below. 2298 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2299 if (comptime wasm) return; 2300 const io = self.session.io orelse return; 2301 const src = self.data.repo_source orelse return; 2302 const author = (try self.session.eventAuthor()) orelse return; 2303 2304 const form = self.threadForm() orelse return; 2305 const title_input = &form.children.values()[title_field_index].widget.text_input; 2306 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2307 const description_input = &form.children.values()[description_field_index].widget.text_input; 2308 2309 const title = try title_input.text(allocator); 2310 defer allocator.free(title); 2311 const tags = try tags_input.text(allocator); 2312 defer allocator.free(tags); 2313 const description = try description_input.text(allocator); 2314 defer allocator.free(description); 2315 2316 if (!Event.fieldsValid(title, tags)) return; 2317 2318 if (comptime kind == .patch) { 2319 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2320 title_input.clear(allocator); 2321 tags_input.clear(allocator); 2322 description_input.clear(allocator); 2323 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2324 try self.session.navigate(route); 2325 return; 2326 } 2327 2328 var id_bytes: [evt.event_id_size]u8 = undefined; 2329 io.random(&id_bytes); 2330 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2331 2332 const event = evt.EventWithId{ 2333 .id = event_id_hex, 2334 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2335 .author = author, 2336 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2337 .title = title, 2338 .description = description, 2339 .tags = tags, 2340 }), 2341 }; 2342 2343 switch (src.repo_kind) { 2344 inline else => |repo_kind| { 2345 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2346 defer any_repo.deinit(io, allocator); 2347 switch (any_repo) { 2348 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2349 } 2350 }, 2351 } 2352 2353 // wipe the form so a return visit starts fresh 2354 title_input.clear(allocator); 2355 tags_input.clear(allocator); 2356 description_input.clear(allocator); 2357 2358 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2359 try self.session.navigate(route); 2360 } 2361 2362 // re-emit the selected thread's event with the form's content (status 2363 // preserved), then reload the thread's page. this is the terminal path; 2364 // the web posts the form to the edit route. 2365 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2366 if (comptime wasm) return; 2367 const io = self.session.io orelse return; 2368 const src = self.data.repo_source orelse return; 2369 const entry = self.data.selectedThread() orelse return; 2370 const author = (try self.session.eventAuthor()) orelse return; 2371 2372 const form = self.threadForm() orelse return; 2373 const title_input = &form.children.values()[title_field_index].widget.text_input; 2374 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2375 const description_input = &form.children.values()[description_field_index].widget.text_input; 2376 2377 const title = try title_input.text(allocator); 2378 defer allocator.free(title); 2379 const tags = try tags_input.text(allocator); 2380 defer allocator.free(tags); 2381 const description = try description_input.text(allocator); 2382 defer allocator.free(description); 2383 2384 if (!Event.fieldsValid(title, tags)) return; 2385 2386 const id_bytes = try evt.parseEventId(entry.id); 2387 2388 switch (src.repo_kind) { 2389 inline else => |repo_kind| { 2390 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2391 defer any_repo.deinit(io, allocator); 2392 switch (any_repo) { 2393 inline else => |*repo| if (has_status) 2394 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2395 .title = title, 2396 .tags = tags, 2397 .description = description, 2398 } }, author) 2399 else 2400 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2401 } 2402 }, 2403 } 2404 2405 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2406 try self.session.navigate(route); 2407 } 2408 2409 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2410 const comment = self.data.comment_id.len != 0; 2411 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2412 const id = evt.parseEventId(event_id) catch return; 2413 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2414 } 2415 2416 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2417 if (comptime wasm) return; 2418 const io = self.session.io orelse return; 2419 const src = self.data.repo_source orelse return; 2420 const author = (try self.session.eventAuthor()) orelse return; 2421 2422 switch (src.repo_kind) { 2423 inline else => |repo_kind| { 2424 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2425 defer any_repo.deinit(io, allocator); 2426 switch (any_repo) { 2427 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2428 } 2429 }, 2430 } 2431 2432 const id_hex = std.fmt.bytesToHex(id, .lower); 2433 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2434 try self.session.navigate(route); 2435 } 2436 2437 // flip the shown thread's status by re-emitting its event, then reload the 2438 // page rooted at the thread so the view reflects the change. this is the 2439 // terminal path; the web posts the button's form to the status route. 2440 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2441 if (comptime !has_status) return; 2442 if (comptime wasm) return; 2443 const io = self.session.io orelse return; 2444 const src = self.data.repo_source orelse return; 2445 const sel = self.detailed_index[index] orelse return; 2446 const entry = self.window(index).items[sel]; 2447 const author = (try self.session.eventAuthor()) orelse return; 2448 2449 const status = (statusChange(entryStatus(entry)) orelse return).status; 2450 2451 const id_bytes = try evt.parseEventId(entry.id); 2452 2453 switch (src.repo_kind) { 2454 inline else => |repo_kind| { 2455 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2456 defer any_repo.deinit(io, allocator); 2457 switch (any_repo) { 2458 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2459 } 2460 }, 2461 } 2462 2463 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2464 try self.session.navigate(route); 2465 } 2466 2467 // arrow keys move the tag selection; at the flow's edges focus crosses to 2468 // the neighboring widgets. 2469 fn tagsInput(self: *This, index: usize, child_index: usize, tf: *ui.TagFlow, key: Key, root_focus: *Focus) void { 2470 const cid = tf.focus.child_id orelse return; 2471 const cur = tf.indexOfFocusId(cid) orelse return; 2472 const count = tf.text_boxes.items.len; 2473 switch (key) { 2474 .arrow_left => if (cur > 0) self.focusTag(index, child_index, tf, root_focus, cur - 1) else self.focusList(index, root_focus), 2475 .arrow_right => if (cur + 1 < count) self.focusTag(index, child_index, tf, root_focus, cur + 1), 2476 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2477 self.focusTag(index, child_index, tf, root_focus, i); 2478 } else { 2479 _ = self.moveDetailVertical(index, root_focus, false); 2480 }, 2481 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2482 self.focusTag(index, child_index, tf, root_focus, i); 2483 } else { 2484 _ = self.moveDetailVertical(index, root_focus, true); 2485 }, 2486 .home => self.focusTag(index, child_index, tf, root_focus, 0), 2487 .end => self.focusTag(index, child_index, tf, root_focus, count - 1), 2488 else => {}, 2489 } 2490 } 2491 2492 const title_child_index: usize = 0; 2493 2494 // the tool row's first child after the spacer that pushes it right 2495 const first_in_row_index: usize = 1; 2496 2497 fn toolRowFocused(self: *This, index: usize) bool { 2498 const outer = self.detailOuter(index); 2499 const cid = outer.getFocus().child_id orelse return false; 2500 return outer.children.getIndex(cid) == tool_row_index; 2501 } 2502 2503 fn focusTag(self: *This, index: usize, child_index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2504 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2505 // keep the tag visible on the terminal: its rect offset by the flow's 2506 // position in the pane. 2507 if (self.session.is_terminal and item < tf.rects.items.len) { 2508 if (self.detailInner(index).children.values()[child_index].rect) |flow_rect| { 2509 var rect = tf.rects.items[item]; 2510 rect.x += flow_rect.x; 2511 rect.y += flow_rect.y; 2512 self.detailScroll(index).scrollToRect(rect); 2513 } 2514 } 2515 } 2516 2517 // return to the tool row's last-focused button (the header when the 2518 // description page shows no row). 2519 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2520 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2521 root_focus.setFocus(cid); 2522 } 2523 2524 // enter the detail pane. an empty pane can't be entered. 2525 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 2526 if (self.detailInner(index).children.count() == 0) return; 2527 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2528 } 2529 2530 // return to the list. 2531 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2532 root_focus.setFocus(self.listScroll(index).getFocus().id); 2533 } 2534 2535 // cross to the header tabs above the stack. 2536 fn focusHeader(self: *This, root_focus: *Focus) void { 2537 root_focus.setFocus(self.box.children.keys()[header_index]); 2538 } 2539 2540 pub fn clearGrid(self: *This) void { 2541 self.box.clearGrid(); 2542 } 2543 2544 pub fn getGrid(self: This) ?Grid { 2545 return self.box.getGrid(); 2546 } 2547 2548 pub fn getFocus(self: *This) *Focus { 2549 return self.box.getFocus(); 2550 } 2551 2552 // for the parent's "scroll up at the top jumps to the header" check: at the 2553 // top only while the header tabs hold focus, so up from the split 2554 // crosses into the tabs first. 2555 pub fn getSelectedIndex(self: *This) ?usize { 2556 return if (self.headerActive()) 0 else 1; 2557 } 2558 }; 2559 } 2560