╭ Threads.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const xit = @import("xit"); 6 const rp = xit.repo; 7 const xitui = xit.xitui; 8 const wgt = xitui.widget; 9 const layout = xitui.layout; 10 const Key = xitui.input.Key; 11 const Grid = xitui.grid.Grid; 12 const Focus = xitui.focus.Focus; 13 const inp = @import("../input.zig"); 14 const diff3 = @import("../../diff3.zig"); 15 const Comment = @import("Comment.zig"); 16 const Attachment = @import("Attachment.zig"); 17 18 const wasm = builtin.target.cpu.arch == .wasm32; 19 20 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 21 const Self = Data; 22 const Header = Data.Header; 23 const Window = Data.Window; 24 const Entry = Data.Entry; 25 const Event = Data.Event; 26 const Status = Data.Status; 27 const has_status = @hasField(Event, "status"); 28 const has_conflicts = Event.merge_policy == .field_conflicts; 29 const FieldConflict = if (has_conflicts) Data.FieldConflict else void; 30 const ViewKind = Data.ViewKind; 31 32 return struct { 33 const This = @This(); 34 // a vertical box: the header tabs on top, then a stack holding a 35 // master-detail split (issue list + description pane) per status list, 36 // plus the tags view. 37 box: wgt.Box(ui.Widget), // vert: [header_index] = tabs, [stack_index] = stack 38 data: *const Self, 39 session: *ui.Session, 40 // per-split state, indexed like the stack's split children: the issue the 41 // pane shows, and its description, author and open/close button focus ids. 42 detailed_index: [view_count]?usize, 43 description_id: [view_count]?usize, 44 author_id: [view_count]?usize, 45 status_button_id: [view_count]?usize, 46 remove_button_id: [view_count]?usize, 47 48 const header_index: usize = 0; 49 const stack_index: usize = 1; 50 // indices within the stack, 1:1 with the header tabs. 51 const status_count = @typeInfo(Status).@"enum".fields.len; 52 const tags_view_index: usize = status_count; 53 // the new-issue form, or the edit, comment, or resolve form when the page was 54 // loaded at one of their urls. 55 const form_view_index: usize = tags_view_index + 1; 56 // the conflicts split; the tab and stack child exist only when the repo 57 // has conflicted issues. 58 const conflict_view_index: usize = form_view_index + 1; 59 const view_count: usize = conflict_view_index + @intFromBool(has_conflicts); 60 // indices within a split (the horizontal box inside the stack). 61 const list_index: usize = 0; 62 const detail_index: usize = 1; 63 const list_max_width: usize = 40; 64 const detail_min_width: usize = 40; 65 // indices within the issue form. 66 const title_field_index: usize = 0; 67 const tags_field_index: usize = 1; 68 const description_field_index: usize = 2; 69 const submit_field_index: usize = 3; 70 const comment_author_field_index: usize = 0; 71 const comment_body_field_index: usize = 1; 72 const comment_submit_field_index: usize = 2; 73 74 pub fn viewIndex(view: ViewKind) usize { 75 const name = @tagName(view); 76 if (std.mem.eql(u8, name, "tags")) return tags_view_index; 77 if (std.mem.eql(u8, name, "new") or std.mem.eql(u8, name, "edit") or std.mem.eql(u8, name, "new_comment") or std.mem.eql(u8, name, "edit_comment") or std.mem.eql(u8, name, "resolve")) return form_view_index; 78 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 79 if (std.mem.eql(u8, name, "description")) unreachable; 80 inline for (@typeInfo(Status).@"enum".fields, 0..) |field, index| { 81 if (std.mem.eql(u8, name, field.name)) return index; 82 } 83 unreachable; 84 } 85 86 // the status a status split lists; the conflicts split has none (its 87 // issues carry their own). 88 fn splitStatus(index: usize) Status { 89 return @enumFromInt(index); 90 } 91 92 fn entryConflicted(entry: Entry) bool { 93 return if (has_conflicts) entry.conflicted else false; 94 } 95 96 fn entryStatus(entry: Entry) Status { 97 return if (has_status) entry.record.event.status else @enumFromInt(0); 98 } 99 100 fn listRoute(identity: []const u8, status: Status, tag: []const u8, selected: []const u8) ?ui.RoutablePage { 101 return switch (kind) { 102 .issue => ui.RoutablePage.repoIssuesRoute(identity, status, tag, selected), 103 .discussion => ui.RoutablePage.repoDiscussionsRoute(identity, tag, selected), 104 else => @compileError("unsupported thread event kind"), 105 }; 106 } 107 108 fn commentsRoute(identity: []const u8, selected: []const u8, start: usize) ?ui.RoutablePage { 109 return switch (kind) { 110 .issue => ui.RoutablePage.repoIssueCommentsRoute(identity, selected, start), 111 .discussion => ui.RoutablePage.repoDiscussionCommentsRoute(identity, selected, start), 112 else => @compileError("unsupported thread event kind"), 113 }; 114 } 115 116 fn commentRoute(identity: []const u8, thread_id: []const u8, comment_id: []const u8, start: usize) ?ui.RoutablePage { 117 return switch (kind) { 118 .issue => ui.RoutablePage.repoCommentsRoute(identity, thread_id, comment_id, start), 119 .discussion => ui.RoutablePage.repoDiscussionCommentRoute(identity, thread_id, comment_id, start), 120 else => @compileError("unsupported thread event kind"), 121 }; 122 } 123 124 fn newCommentRoute(identity: []const u8, thread_id: []const u8, parent_id: []const u8) ?ui.RoutablePage { 125 return switch (kind) { 126 .issue => ui.RoutablePage.repoCommentNewRoute(identity, thread_id, parent_id), 127 .discussion => ui.RoutablePage.repoDiscussionCommentNewRoute(identity, thread_id, parent_id), 128 else => @compileError("unsupported thread event kind"), 129 }; 130 } 131 132 fn editCommentRoute(identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?ui.RoutablePage { 133 return switch (kind) { 134 .issue => ui.RoutablePage.repoCommentEditRoute(identity, thread_id, comment_id), 135 .discussion => ui.RoutablePage.repoDiscussionCommentEditRoute(identity, thread_id, comment_id), 136 else => @compileError("unsupported thread event kind"), 137 }; 138 } 139 140 fn editRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 141 return switch (kind) { 142 .issue => ui.RoutablePage.repoIssuesEditRoute(identity, selected), 143 .discussion => ui.RoutablePage.repoDiscussionsEditRoute(identity, selected), 144 else => @compileError("unsupported thread event kind"), 145 }; 146 } 147 148 fn newRoute(identity: []const u8) ?ui.RoutablePage { 149 return switch (kind) { 150 .issue => ui.RoutablePage.repoIssuesNewRoute(identity), 151 .discussion => ui.RoutablePage.repoDiscussionsNewRoute(identity), 152 else => @compileError("unsupported thread event kind"), 153 }; 154 } 155 156 fn descriptionRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 157 return switch (kind) { 158 .issue => ui.RoutablePage.repoIssuesDescriptionRoute(identity, selected), 159 .discussion => ui.RoutablePage.repoDiscussionsDescriptionRoute(identity, selected), 160 else => @compileError("unsupported thread event kind"), 161 }; 162 } 163 164 fn tagsRoute(identity: []const u8, tag: []const u8) ?ui.RoutablePage { 165 return switch (kind) { 166 .issue => ui.RoutablePage.repoIssuesTagsRoute(identity, tag), 167 .discussion => ui.RoutablePage.repoDiscussionsTagsRoute(identity, tag), 168 else => @compileError("unsupported thread event kind"), 169 }; 170 } 171 172 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 173 return switch (kind) { 174 .issue => ui.RoutablePage.repoIssuesConflictsRoute(identity, selected), 175 else => @compileError("thread event kind has no conflict route"), 176 }; 177 } 178 179 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 180 return switch (kind) { 181 .issue => ui.RoutablePage.repoIssuesResolveRoute(identity, selected, picks), 182 else => @compileError("thread event kind has no resolve route"), 183 }; 184 } 185 186 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8, id: []const u8) ![]const u8 { 187 const route = listRoute(identity, status, tag, id) orelse return error.RouteTooLong; 188 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 189 } 190 191 fn rowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 192 const route = listRoute(identity, @enumFromInt(0), "", id) orelse return error.RouteTooLong; 193 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 194 } 195 196 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, id: []const u8) ![]const u8 { 197 if (has_conflicts and status_maybe == null) { 198 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 199 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 200 } 201 return listLink(page_arena, data.identity, status_maybe orelse @enumFromInt(0), data.tag, id); 202 } 203 204 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 205 const route = descriptionRoute(identity, id) orelse return error.RouteTooLong; 206 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 207 } 208 209 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8) ![]const u8 { 210 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 211 return listLink(page_arena, identity, status, encoded, ""); 212 } 213 214 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 215 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 216 errdefer outer.deinit(allocator); 217 218 // the tabs at the top. 219 { 220 var hdr = try Header.init(allocator, session, data); 221 errdefer hdr.deinit(allocator); 222 switch (kind) { 223 .issue => try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = .{ .repo_issues_header = hdr }, .rect = null, .min_size = null }), 224 .discussion => try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = .{ .repo_discussions_header = hdr }, .rect = null, .min_size = null }), 225 else => @compileError("unsupported thread event kind"), 226 } 227 } 228 229 // the stack enters `outer` before its children enter it, so an error 230 // frees each child exactly once. 231 { 232 var stack = try wgt.Stack(ui.Widget).init(allocator); 233 errdefer stack.deinit(allocator); 234 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 235 } 236 const stack = &outer.children.values()[stack_index].widget.stack; 237 238 inline for (@typeInfo(Status).@"enum".fields) |field| { 239 var split = try initSplit(allocator, session, data, @enumFromInt(field.value)); 240 errdefer split.deinit(allocator); 241 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 242 } 243 244 // the tags view 245 { 246 var tf = try ui.TagFlow.init(allocator); 247 errdefer tf.deinit(allocator); 248 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 249 defer items.deinit(allocator); 250 // when filtered, the first item clears the filter 251 if (data.tag.len != 0) 252 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @enumFromInt(0), "", "") }); 253 for (data.tags) |tag| 254 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, @enumFromInt(0), tag) }); 255 try tf.setItems(allocator, items.items); 256 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 257 } 258 259 // the new-issue form, or — on an edit, comment, or resolve url — that form in 260 // its place, prefilled with the selected issue's content. a 261 // logged-out session can't create events, so the unauthorized view 262 // stands in. 263 if (session.data.is_local or session.data.user_id != null) { 264 if (data.view == .new_comment or data.view == .edit_comment) { 265 const editing = data.view == .edit_comment; 266 const route = if (editing) 267 editCommentRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 268 else 269 newCommentRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 270 const page_url = try route.toUrl(session.page_arena); 271 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 272 const page = data.comment_page; 273 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 274 const parent_route = if (editing) 275 if (top_level) 276 commentsRoute(data.identity, data.selected_id, 0) 277 else if (page) |p| 278 commentRoute(data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 279 else 280 null 281 else if (page) |p| 282 commentRoute(data.identity, data.selected_id, &p.selected.id, 0) 283 else 284 commentsRoute(data.identity, data.selected_id, 0); 285 const parent_author: ui.Author = if (editing) 286 if (top_level) 287 if (data.selectedThread()) |entry| entry.author else .unknown 288 else if (page) |p| 289 p.selected.parent_author orelse .unknown 290 else 291 .unknown 292 else if (page) |p| 293 p.selected.author 294 else if (data.selectedThread()) |entry| 295 entry.author 296 else 297 .unknown; 298 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 299 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 300 errdefer form.deinit(allocator); 301 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 302 } else if (has_conflicts and data.view == .resolve) { 303 // on the web the page grows to the form's height and the 304 // browser scrolls it; the terminal scrolls the form itself 305 var form_widget: ui.Widget = blk: { 306 var form = try initResolveForm(allocator, session, data); 307 errdefer form.deinit(allocator); 308 break :blk if (session.is_terminal) 309 .{ .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 310 else 311 .{ .box = form }; 312 }; 313 errdefer form_widget.deinit(allocator); 314 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 315 } else { 316 const aa = session.page_arena.allocator(); 317 const route = if (data.view == .edit) 318 editRoute(data.identity, data.selected_id) orelse return error.RouteTooLong 319 else 320 newRoute(data.identity) orelse return error.RouteTooLong; 321 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 322 const record = if (data.view == .edit) 323 (if (data.selectedThread()) |entry| &entry.record else null) 324 else 325 null; 326 var form = try initThreadForm(allocator, session, action, record); 327 errdefer form.deinit(allocator); 328 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 329 } 330 } else { 331 var message = try ui.Unauthorized.View.init(allocator); 332 errdefer message.deinit(allocator); 333 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 334 } 335 336 // the conflicts split; its tab exists only when the repo has 337 // conflicts, so skip the child too to keep the stack 1:1 with the 338 // tabs. 339 if (has_conflicts and data.conflicts.count > 0) { 340 var split = try initSplit(allocator, session, data, null); 341 errdefer split.deinit(allocator); 342 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 343 } 344 345 // the stack starts on the page's view. 346 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 347 348 // focus entering the view lands on the tabs first. 349 outer.getFocus().child_id = outer.children.keys()[header_index]; 350 351 return .{ 352 .box = outer, 353 .data = data, 354 .session = session, 355 .detailed_index = @splat(null), 356 .description_id = @splat(null), 357 .author_id = @splat(null), 358 .status_button_id = @splat(null), 359 .remove_button_id = @splat(null), 360 }; 361 } 362 363 // the master-detail split showing `status`'s window, or the conflicts 364 // window when null. 365 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status) !wgt.Box(ui.Widget) { 366 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 367 errdefer box.deinit(allocator); 368 369 const win = if (status_maybe) |status| data.window(status) else if (has_conflicts) &data.conflicts else unreachable; 370 371 // the issue list (one focusable row per title), plus a "next" link that 372 // reloads the page rooted at the following issue. 373 { 374 var list_scroll = blk: { 375 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 376 errdefer list_box.deinit(allocator); 377 if (win.prev_id) |prev| 378 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, prev)); 379 for (win.items) |entry| { 380 // conflicts take precedence over the comment count 381 const label: []const u8 = if (entryConflicted(entry) and status_maybe != null) 382 " conflict " 383 else if (entry.comments.count > 0) 384 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 385 else 386 ""; 387 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data.identity, entry.id)); 388 } 389 if (win.next_id) |next| 390 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, next)); 391 // select the window's first issue (past a leading "previous" 392 // row) so its description shows on load. 393 if (win.items.len > 0) 394 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 395 else if (list_box.children.count() > 0) 396 list_box.getFocus().child_id = list_box.children.keys()[0]; 397 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 398 }; 399 errdefer list_scroll.deinit(allocator); 400 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 } }); 401 } 402 403 // the detail pane — a frame around a scroll of the description 404 { 405 var detail_outer = blk: { 406 var detail_scroll = blk2: { 407 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 408 errdefer detail_inner.deinit(allocator); 409 // fill the pane (content top-left, scroll bar pinned to the 410 // edge) rather than shrinking to the description. 411 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 412 }; 413 errdefer detail_scroll.deinit(allocator); 414 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 415 errdefer frame.deinit(allocator); 416 // the tool row sits above the scroll (populateDetail fills 417 // it per issue) so it can't scroll out from under the web 418 // overlay <form>, whose position doesn't track pane scrolling. 419 { 420 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 421 errdefer row.deinit(allocator); 422 try frame.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 423 } 424 // the frame's selected child is its scroll, so the focus chain 425 // reaches the description (populateDetail points the scroll's 426 // inner box at it), letting focus recovery descend into the pane. 427 frame.getFocus().child_id = detail_scroll.getFocus().id; 428 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 429 break :blk frame; 430 }; 431 errdefer detail_outer.deinit(allocator); 432 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 433 } 434 435 box.getFocus().child_id = box.children.keys()[list_index]; 436 return box; 437 } 438 439 // a thread form: title/tags/description inputs and a submit button, 440 // prefilled from `record` when given. its form: subtree makes the web 441 // overlay wrap them in a <form> POSTing to `action`'s route. 442 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, record: ?*const Event.Record) !wgt.Box(ui.Widget) { 443 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 444 errdefer box.deinit(allocator); 445 box.getFocus().kind = .{ .custom = action }; 446 447 { 448 var title = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 449 errdefer title.deinit(allocator); 450 title.getFocus().focusable = true; 451 if (record) |r| try title.setContent(allocator, r.event.title); 452 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 453 } 454 455 { 456 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 }); 457 errdefer tags.deinit(allocator); 458 tags.getFocus().focusable = true; 459 if (record) |r| try tags.setContent(allocator, r.event.tags); 460 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 461 } 462 463 { 464 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 } }); 465 errdefer description.deinit(allocator); 466 description.getFocus().focusable = true; 467 if (record) |r| try description.setContent(allocator, r.event.description); 468 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 469 } 470 471 try addSubmitButton(allocator, &box); 472 473 box.getFocus().child_id = box.children.keys()[title_field_index]; 474 return box; 475 } 476 477 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) { 478 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 479 errdefer box.deinit(allocator); 480 box.getFocus().kind = .{ .custom = action }; 481 482 { 483 var parent = try ui.authorBox(allocator, session.page_arena, author); 484 errdefer parent.deinit(allocator); 485 parent.options.label = " replying to "; 486 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 487 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 488 } 489 490 { 491 var body = try wgt.TextInput(ui.Widget).init(allocator, .{ 492 .label = " comment ", 493 .name = "body", 494 .visible_width = null, 495 .rounded_corners = true, 496 .render_content = session.is_terminal, 497 .multiline = true, 498 .visible_height = 5, 499 .scroll = .{ .fill = true }, 500 }); 501 errdefer body.deinit(allocator); 502 body.getFocus().focusable = true; 503 if (initial_body) |text| try body.setContent(allocator, text); 504 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 505 } 506 507 try addSubmitButtonLabeled(allocator, &box, "submit"); 508 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 509 return box; 510 } 511 512 // a form's submit button, then a spacer absorbing the leftover 513 // min-height the box hands its last child, so the button keeps its 514 // natural height 515 fn addSubmitButton(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 516 return addSubmitButtonLabeled(allocator, box, "submit"); 517 } 518 519 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8) !void { 520 { 521 var submit = try ui.SubmitButton.initLabeled(allocator, label); 522 errdefer submit.deinit(allocator); 523 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 524 } 525 { 526 var spacer = try ui.Spacer.init(allocator); 527 errdefer spacer.deinit(allocator); 528 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 529 } 530 } 531 532 // the conflict resolve form. the "use this" links are navigations that 533 // flip the url's theirs: pick, reloading the prefills; one submit 534 // settles every conflict. the form: subtree makes the web overlay POST 535 // to the resolve route. 536 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(ui.Widget) { 537 const aa = session.page_arena.allocator(); 538 // init only picks the resolve view once it has read the conflict 539 const conflict = if (data.conflict) |*c| c else unreachable; 540 541 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 542 errdefer box.deinit(allocator); 543 const route = resolveRoute(data.identity, data.selected_id, "") orelse return error.RouteTooLong; 544 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 545 546 if (conflict.title) |*fc| { 547 try addLabel(allocator, &box, "title conflict:"); 548 try addFieldConflict(allocator, &box, session, data, "title", fc); 549 } 550 if (conflict.tags) |*fc| { 551 try addLabel(allocator, &box, "tags conflict:"); 552 try addFieldConflict(allocator, &box, session, data, "tags", fc); 553 } 554 555 if (conflict.description) |*desc| { 556 try addLabel(allocator, &box, "description conflict:"); 557 var hunk_index: usize = 0; 558 for (desc.chunks, 0..) |*chunk, chunk_index| { 559 switch (chunk.*) { 560 .same => |text| { 561 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 562 errdefer tb.deinit(allocator); 563 tb.getFocus().focusable = true; 564 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 565 }, 566 // an auto-resolved chunk: only one side changed it, so its 567 // version stands, shown distinctly with its author 568 .auto => |auto| { 569 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 570 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 571 const label = switch (author) { 572 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 573 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 574 }; 575 var tb = try wgt.TextBox(ui.Widget).init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 576 errdefer tb.deinit(allocator); 577 tb.getFocus().focusable = true; 578 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 579 }, 580 .conflict => |hunk| { 581 // a blank row on each side sets the group apart from 582 // the flowing shared and auto-resolved chunks 583 if (chunk_index > 0) try addGap(allocator, &box); 584 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 585 hunk_index += 1; 586 const picked_theirs = data.theirsPicked(name); 587 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 588 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 589 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 }); 590 errdefer resolution_input.deinit(allocator); 591 resolution_input.getFocus().focusable = true; 592 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 593 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 594 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 595 }, 596 } 597 } 598 } 599 600 try addSubmitButton(allocator, &box); 601 602 box.getFocus().child_id = box.children.keys()[0]; 603 return box; 604 } 605 606 // one conflicted scalar field: both sides' version rows, then the 607 // resolution input prefilled from the picked side. 608 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 { 609 const aa = session.page_arena.allocator(); 610 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 611 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 612 613 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 }); 614 errdefer resolution_input.deinit(allocator); 615 resolution_input.getFocus().focusable = true; 616 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 617 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 618 } 619 620 // a blank row setting a conflict group apart from its neighbors 621 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 622 var text = try wgt.Text(ui.Widget).init(allocator, " "); 623 errdefer text.deinit(allocator); 624 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 625 } 626 627 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), content: []const u8) !void { 628 var label = try ui.SectionLabel.init(allocator, content); 629 errdefer label.deinit(allocator); 630 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 631 } 632 633 // one side of a conflict: the "use this" link on the left, the version 634 // itself as a labeled read-only box. 635 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, text: []const u8, link: []const u8) !void { 636 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 637 errdefer row.deinit(allocator); 638 639 { 640 const use_label = "use this"; 641 var use = try wgt.TextBox(ui.Widget).init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 642 errdefer use.deinit(allocator); 643 use.getFocus().focusable = true; 644 use.getFocus().kind = .{ .custom = link }; 645 try row.children.put(allocator, use.getFocus().id, .{ 646 .widget = .{ .text_box = use }, 647 .rect = null, 648 .min_size = .{ .width = use_label.len + 2, .height = null }, 649 .max_size = .{ .width = use_label.len + 2, .height = null }, 650 }); 651 } 652 653 { 654 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 }); 655 errdefer tb.deinit(allocator); 656 tb.getFocus().focusable = true; 657 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 658 } 659 660 row.getFocus().child_id = row.children.keys()[0]; 661 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 662 } 663 664 // the label of one side's version box, naming its author 665 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 666 return switch (author) { 667 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 668 .unknown => if (ours) " current version " else " their version ", 669 }; 670 } 671 672 // the a: link that reloads the resolve page with `name` picked from ours 673 // or theirs, reprefilling its resolution input 674 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 675 const aa = session.page_arena.allocator(); 676 var picks: std.ArrayList(u8) = .empty; 677 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 678 while (it.next()) |pick| { 679 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 680 if (picks.items.len > 0) try picks.append(aa, ','); 681 try picks.appendSlice(aa, pick); 682 } 683 if (theirs) { 684 if (picks.items.len > 0) try picks.append(aa, ','); 685 try picks.appendSlice(aa, name); 686 } 687 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 688 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 689 } 690 691 // the resolve form's page-constant initial value for the input `name`: 692 // the picked side of its conflicted field or hunk 693 fn resolvePrefill(self: *This, name: []const u8) ?[]const u8 { 694 const conflict = if (self.data.conflict) |*c| c else return null; 695 const theirs = self.data.theirsPicked(name); 696 if (std.mem.eql(u8, name, "title")) { 697 const fc = if (conflict.title) |*f| f else return null; 698 return if (theirs) fc.theirs.text else fc.ours.text; 699 } 700 if (std.mem.eql(u8, name, "tags")) { 701 const fc = if (conflict.tags) |*f| f else return null; 702 return if (theirs) fc.theirs.text else fc.ours.text; 703 } 704 if (std.mem.startsWith(u8, name, "d")) { 705 const desc = if (conflict.description) |*d| d else return null; 706 const hunk_index = std.fmt.parseInt(usize, name[1..], 10) catch return null; 707 var seen: usize = 0; 708 for (desc.chunks) |chunk| switch (chunk) { 709 .conflict => |hunk| { 710 if (seen == hunk_index) return (if (theirs) hunk.theirs else hunk.ours) orelse ""; 711 seen += 1; 712 }, 713 else => {}, 714 }; 715 } 716 return null; 717 } 718 719 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 720 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 721 errdefer row.deinit(allocator); 722 row.getFocus().focusable = true; 723 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 724 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 725 } 726 727 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 728 self.box.deinit(allocator); 729 } 730 731 fn header(self: *This) *Header { 732 return switch (kind) { 733 .issue => &self.box.children.values()[header_index].widget.repo_issues_header, 734 .discussion => &self.box.children.values()[header_index].widget.repo_discussions_header, 735 else => @compileError("unsupported thread event kind"), 736 }; 737 } 738 739 fn viewStack(self: *This) *wgt.Stack(ui.Widget) { 740 return &self.box.children.values()[stack_index].widget.stack; 741 } 742 743 // `index`'s master-detail split inside the stack. 744 fn resultsBox(self: *This, index: usize) *wgt.Box(ui.Widget) { 745 return &self.viewStack().children.values()[index].box; 746 } 747 748 // the tags view's flow inside the stack. 749 fn tagsView(self: *This) *ui.TagFlow { 750 return &self.viewStack().children.values()[tags_view_index].tag_flow; 751 } 752 753 // the new-issue, edit, or resolve form inside the stack, or null when the 754 // unauthorized view stands in for it. 755 fn threadForm(self: *This) ?*wgt.Box(ui.Widget) { 756 return switch (self.viewStack().children.values()[form_view_index]) { 757 .box => |*box| box, 758 // the resolve form sits inside a scroll on the terminal 759 .scroll => |*scroll| &scroll.child.box, 760 else => null, 761 }; 762 } 763 764 // the resolve form's scroll on the terminal (the web page scrolls itself) 765 fn resolveScroll(self: *This) ?*wgt.Scroll(ui.Widget) { 766 return switch (self.viewStack().children.values()[form_view_index]) { 767 .scroll => |*scroll| scroll, 768 else => null, 769 }; 770 } 771 772 fn listScroll(self: *This, index: usize) *wgt.Scroll(ui.Widget) { 773 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 774 } 775 776 fn listBox(self: *This, index: usize) *wgt.Box(ui.Widget) { 777 return &self.listScroll(index).child.box; 778 } 779 780 // the detail frame's children: the tool row above the scroll pane. 781 const tool_row_index: usize = 0; 782 const detail_scroll_index: usize = 1; 783 784 fn detailOuter(self: *This, index: usize) *wgt.Box(ui.Widget) { 785 return &self.resultsBox(index).children.values()[detail_index].widget.box; 786 } 787 788 fn toolRow(self: *This, index: usize) *wgt.Box(ui.Widget) { 789 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 790 } 791 792 fn detailScroll(self: *This, index: usize) *wgt.Scroll(ui.Widget) { 793 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 794 } 795 796 fn detailInner(self: *This, index: usize) *wgt.Box(ui.Widget) { 797 return &self.detailScroll(index).child.box; 798 } 799 800 fn window(self: *This, index: usize) *const Window { 801 if (has_conflicts and index == conflict_view_index) return &self.data.conflicts; 802 return self.data.window(splitStatus(index)); 803 } 804 805 fn stackSelectedIndex(self: *This) ?usize { 806 const stack = self.viewStack(); 807 const cid = stack.getFocus().child_id orelse return null; 808 return stack.children.getIndex(cid); 809 } 810 811 // the stack's selected master-detail split (null when the tags view or the 812 // new-issue form shows). 813 fn selectedSplitIndex(self: *This) ?usize { 814 const idx = self.stackSelectedIndex() orelse return null; 815 return if (idx < status_count or idx == conflict_view_index) idx else null; 816 } 817 818 fn detailActive(self: *This, index: usize) bool { 819 const rb = self.resultsBox(index); 820 const cid = rb.getFocus().child_id orelse return false; 821 return rb.children.getIndex(cid) == detail_index; 822 } 823 824 fn headerActive(self: *This) bool { 825 const cid = self.box.getFocus().child_id orelse return false; 826 return self.box.children.getIndex(cid) == header_index; 827 } 828 829 fn tagsViewActive(self: *This) bool { 830 if (self.headerActive()) return false; 831 return self.stackSelectedIndex() == tags_view_index; 832 } 833 834 fn formViewActive(self: *This) bool { 835 if (self.headerActive()) return false; 836 return self.stackSelectedIndex() == form_view_index; 837 } 838 839 // the selected issue's index, or null when a window-navigation row is 840 // selected (a leading "previous" row shifts the issue rows down by one). 841 fn selectedThreadIndex(self: *This, index: usize) ?usize { 842 const lb = self.listBox(index); 843 const cid = lb.getFocus().child_id orelse return null; 844 const idx = lb.children.getIndex(cid) orelse return null; 845 const win = self.window(index); 846 const lead: usize = if (win.prev_id != null) 1 else 0; 847 if (idx < lead or idx - lead >= win.items.len) return null; 848 return idx - lead; 849 } 850 851 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 852 self.clearGrid(); 853 854 // each header tab maps 1:1 to a stack child by position. mirror the 855 // selection into the url: the view the page's window is rooted at 856 // keeps its rooted url, the others get their list route (keeping the 857 // tag filter). 858 if (self.header().getSelectedIndex()) |index| { 859 const stack = self.viewStack(); 860 stack.getFocus().child_id = stack.children.keys()[index]; 861 self.session.data.current_page = (if (index == form_view_index) 862 (if (has_conflicts) switch (self.data.view) { 863 .edit => editRoute(self.data.identity, self.data.selected_id), 864 .new_comment => newCommentRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 865 .edit_comment => editCommentRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 866 .resolve => resolveRoute(self.data.identity, self.data.selected_id, self.data.theirs_picks), 867 else => newRoute(self.data.identity), 868 } else switch (self.data.view) { 869 .edit => editRoute(self.data.identity, self.data.selected_id), 870 .new_comment => newCommentRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 871 .edit_comment => editCommentRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 872 else => newRoute(self.data.identity), 873 }) 874 else if (has_conflicts and index == conflict_view_index) 875 conflictsRoute(self.data.identity, if (self.data.view == .conflicts) self.data.selected_id else "") 876 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 877 (if (self.data.description_page) 878 descriptionRoute(self.data.identity, self.data.selected_id) 879 else if (self.data.comment_page) |page| 880 commentRoute(self.data.identity, self.data.selected_id, &page.selected.id, page.replies.start) 881 else 882 commentsRoute(self.data.identity, self.data.selected_id, self.data.comments_start)) 883 else if (index == tags_view_index) 884 tagsRoute(self.data.identity, self.data.tag) 885 else 886 listRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 887 } 888 889 if (self.selectedSplitIndex()) |i| { 890 // swap the detail pane to the selected issue when it changes. 891 try self.refreshDetail(allocator, i); 892 893 // mirror the focused issue into the url, but only while focus is 894 // inside the split. an issue's url is the same whether or not the 895 // list is filtered, so the mirror drops the tag. 896 if (root_focus.grandchild_id) |g| { 897 if (self.resultsBox(i).getFocus().children.contains(g)) { 898 if (self.selectedThreadIndex(i)) |sel| { 899 // selecting another issue leaves the description page 900 // behind, like it leaves the tag filter behind. 901 const entry = &self.window(i).items[sel]; 902 const mirrored = if (self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id)) 903 descriptionRoute(self.data.identity, entry.id) 904 else if (self.data.comment_page) |page| 905 if (std.mem.eql(u8, entry.id, self.data.selected_id)) 906 commentRoute(self.data.identity, entry.id, &page.selected.id, page.replies.start) 907 else 908 commentsRoute(self.data.identity, entry.id, 0) 909 else if (std.mem.eql(u8, entry.id, self.data.selected_id)) 910 commentsRoute(self.data.identity, entry.id, entry.comments.start) 911 else 912 commentsRoute(self.data.identity, entry.id, 0); 913 if (mirrored) |route| self.session.data.current_page = route; 914 } 915 } 916 } 917 918 // the selected list row shows a border (the focused TextBox 919 // upgrades it to a double border itself); the rest stay borderless. 920 const lb = self.listBox(i); 921 for (lb.children.keys(), lb.children.values()) |id, *child| { 922 switch (child.widget) { 923 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 924 else => {}, 925 } 926 } 927 928 // the pane's selected child shows the selection border: the 929 // description directly, the tags via the flow's selected item. 930 // cap the list at list_max_width only while the detail pane fits 931 // beside it. the box drops the detail when the width can't hold 932 // both minimums, so when it's that narrow we lift the cap and let 933 // the list fill the whole width. 934 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 935 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 936 937 // stretch the detail pane across the rest of the width so it fills 938 // the area rather than shrinking to its content; its scroll fills 939 // the pane. 940 if (constraint.max_size.width) |w| { 941 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 942 } else { 943 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 944 } 945 } 946 947 // refresh the form inputs' entries in the session's focus-id -> input 948 // map with this frame's addresses, so the web/wasm form handling can 949 // find them by focus id 950 if (self.threadForm()) |form| { 951 const inputs_arena = self.session.arena.allocator(); 952 for (form.children.values()) |*child| switch (child.widget) { 953 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 954 else => {}, 955 }; 956 957 // on an edit page, register the issue's content as the inputs' 958 // page-constant initial values, which the web overlay renders 959 // into them. 960 if (self.data.view == .edit) { 961 if (self.data.selectedThread()) |entry| { 962 const fields = form.children.values(); 963 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.record.event.title); 964 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.record.event.tags); 965 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.record.event.description); 966 } 967 } 968 969 if (self.data.view == .edit_comment) { 970 if (self.data.comment_page) |page| { 971 const body = &form.children.values()[comment_body_field_index].widget.text_input; 972 try self.session.input_values.put(inputs_arena, body.getFocus().id, page.selected.comment.event.body); 973 } 974 } 975 976 // the resolve form's inputs prefill from the picked side, which 977 // is url state and so page-constant like the edit values above 978 if (has_conflicts and self.data.view == .resolve) { 979 for (form.children.values()) |*child| switch (child.widget) { 980 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 981 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 982 }, 983 else => {}, 984 }; 985 } 986 } 987 988 try self.box.build(allocator, constraint, root_focus); 989 } 990 991 fn refreshDetail(self: *This, allocator: std.mem.Allocator, index: usize) !void { 992 const sel = self.selectedThreadIndex(index) orelse return; 993 if (self.detailed_index[index]) |d| if (d == sel) return; 994 try self.populateDetail(allocator, index, sel); 995 self.detailed_index[index] = sel; 996 } 997 998 fn populateDetail(self: *This, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 999 const entry = self.window(index).items[sel]; 1000 const inner = self.detailInner(index); 1001 // the /description page replaces its issue's detail with a back link 1002 // and the whole description; other issues keep their normal detail. 1003 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1004 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 1005 1006 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1007 inner.children.clearAndFree(allocator); 1008 inner.getFocus().child_id = null; 1009 self.author_id[index] = null; 1010 self.status_button_id[index] = null; 1011 self.remove_button_id[index] = null; 1012 1013 // the tool row: the open/close and edit buttons; the description page 1014 // shows none. 1015 { 1016 const row = self.toolRow(index); 1017 for (row.children.values()) |*child| child.widget.deinit(allocator); 1018 row.children.clearAndFree(allocator); 1019 row.getFocus().child_id = null; 1020 row.getFocus().kind = .container; 1021 } 1022 if (!description_page and comment_page == null) { 1023 const row = self.toolRow(index); 1024 const pa = self.session.page_arena.allocator(); 1025 1026 { 1027 var spacer = try ui.Spacer.init(allocator); 1028 errdefer spacer.deinit(allocator); 1029 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1030 } 1031 1032 // a terminal has no file picker, so attaching is web only. the 1033 // renderer covers this button with a file input, so clicking it 1034 // opens the browser's own picker and posts what it chose. 1035 if (!self.session.is_terminal and !entryConflicted(entry) and (self.session.data.is_local or self.session.data.user_id != null)) { 1036 const label = "add attachment"; 1037 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1038 errdefer button.deinit(allocator); 1039 button.getFocus().focusable = true; 1040 button.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1041 try std.fmt.allocPrint(pa, "{s}/{s}:{s}/attach", .{ ui.file_input_prefix, @tagName(kind), entry.id }) 1042 else 1043 try std.fmt.allocPrint(pa, "{s}/repo/{s}/{s}:{s}/attach", .{ ui.file_input_prefix, self.data.identity, @tagName(kind), entry.id }) }; 1044 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1045 } 1046 1047 // a conflicted issue's only action is resolving it. the button 1048 // stays visible logged out; the resolve page shows the 1049 // unauthorized view then. 1050 if (has_conflicts and entryConflicted(entry)) { 1051 const label = "resolve conflict"; 1052 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1053 errdefer button.deinit(allocator); 1054 button.getFocus().focusable = true; 1055 const route = resolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1056 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1057 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1058 } else { 1059 // a logged-out session can't flip the status, so it gets no 1060 // open/close button 1061 if (has_status and (self.session.data.is_local or self.session.data.user_id != null)) { 1062 const action: []const u8 = switch (entryStatus(entry)) { 1063 .open => "close", 1064 .closed => "open", 1065 }; 1066 const route = commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1067 row.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "form:{s}/{s}", .{ try route.toUrl(self.session.page_arena), action }) }; 1068 1069 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1070 errdefer button.deinit(allocator); 1071 button.getFocus().focusable = true; 1072 // the renderer distinguishes plain clickables from buttons 1073 // that should POST to a server route by this kind. 1074 button.getFocus().kind = .{ .custom = "submit" }; 1075 self.status_button_id[index] = button.getFocus().id; 1076 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1077 } 1078 1079 // the edit button links to the issue's edit page. 1080 { 1081 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1082 errdefer button.deinit(allocator); 1083 button.getFocus().focusable = true; 1084 const route = editRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1085 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1086 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1087 } 1088 } 1089 1090 if (self.session.data.is_local or self.session.data.user_id != null) { 1091 const thread_route = commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1092 const remove_url = try std.fmt.allocPrint(pa, "{s}/remove", .{try thread_route.toUrl(self.session.page_arena)}); 1093 const action_kind: []const u8 = if (entryConflicted(entry)) blk: { 1094 row.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "form:{s}", .{remove_url}) }; 1095 break :blk "submit"; 1096 } else try std.fmt.allocPrint(pa, "{s}{s}", .{ ui.submit_action_prefix, remove_url }); 1097 1098 var remove = try wgt.TextBox(ui.Widget).init(allocator, "✕", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1099 errdefer remove.deinit(allocator); 1100 remove.getFocus().focusable = true; 1101 remove.getFocus().kind = .{ .custom = action_kind }; 1102 self.remove_button_id[index] = remove.getFocus().id; 1103 try row.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 1104 } 1105 1106 // the leftmost button: the attachment button when the session has 1107 // one, else the resolve button on a conflicted issue, the 1108 // open/close button when present, the edit button otherwise 1109 row.getFocus().child_id = row.children.keys()[first_in_row_index]; 1110 } 1111 1112 if (comment_page) |page| { 1113 const back_label = try std.fmt.allocPrint(self.session.page_arena.allocator(), "← back to {s}", .{@tagName(kind)}); 1114 var back = try Comment.linkBox(allocator, self.session, back_label, commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1115 errdefer back.deinit(allocator); 1116 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1117 1118 try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, page.selected); 1119 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies", self.session.page_arena.allocator()); 1120 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1121 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1122 1123 var spacer = try ui.Spacer.init(allocator); 1124 errdefer spacer.deinit(allocator); 1125 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1126 1127 inner.getFocus().child_id = inner.children.keys()[0]; 1128 const sc = self.detailScroll(index); 1129 sc.x = 0; 1130 sc.y = 0; 1131 sc.getFocus().version +%= 1; 1132 return; 1133 } 1134 1135 if (description_page) { 1136 // the back link, in the title slot so the pane's input handling 1137 // applies to it unchanged. 1138 const back_label = try std.fmt.allocPrint(self.session.page_arena.allocator(), "← back to {s}", .{@tagName(kind)}); 1139 var tb = try wgt.TextBox(ui.Widget).init(allocator, back_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1140 errdefer tb.deinit(allocator); 1141 tb.getFocus().focusable = true; 1142 tb.getFocus().kind = .{ .custom = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), "", entry.id) }; 1143 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1144 } else { 1145 // the issue's title as a focusable word-wrapped text box. 1146 { 1147 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.record.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1148 errdefer tb.deinit(allocator); 1149 tb.getFocus().focusable = true; 1150 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1151 } 1152 1153 self.author_id[index] = blk: { 1154 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1155 errdefer tb.deinit(allocator); 1156 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1157 break :blk tb.getFocus().id; 1158 }; 1159 1160 // the issue's tags, each linking to this status's list filtered to 1161 // that tag. 1162 { 1163 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1164 defer items.deinit(allocator); 1165 var tag_iter = Event.tagIterator(entry.record.event.tags); 1166 while (tag_iter.next()) |tag| { 1167 if (tag.len == 0) continue; 1168 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entryStatus(entry), tag) }); 1169 } 1170 if (items.items.len > 0) { 1171 var tf = try ui.TagFlow.init(allocator); 1172 errdefer tf.deinit(allocator); 1173 try tf.setItems(allocator, items.items); 1174 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1175 } 1176 } 1177 } 1178 1179 // the description as a focusable word-wrapped text box. past the limit 1180 // it's cut at the last whole line and links to the /description page, 1181 // which shows the whole thing. 1182 self.description_id[index] = blk: { 1183 const whole = entry.record.event.description; 1184 const cut_short = !description_page and whole.len > Data.max_description_size; 1185 const shown = if (cut_short) 1186 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..Data.max_description_size], '\n') orelse 0], " \t\r\n") 1187 else if (whole.len == 0) 1188 "(no description)" 1189 else 1190 whole; 1191 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1192 .border_style = .single, 1193 .rounded_corners = true, 1194 .wrap_kind = .word, 1195 .label = " description ", 1196 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1197 }); 1198 errdefer tb.deinit(allocator); 1199 tb.getFocus().focusable = true; 1200 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1201 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1202 break :blk tb.getFocus().id; 1203 }; 1204 1205 const parent_route = commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1206 try Attachment.appendRows(allocator, inner, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 1207 1208 if (!description_page) { 1209 var reply = try Comment.linkBox(allocator, self.session, "new comment", newCommentRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1210 errdefer reply.deinit(allocator); 1211 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1212 1213 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments", self.session.page_arena.allocator()); 1214 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1215 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1216 1217 var spacer = try ui.Spacer.init(allocator); 1218 errdefer spacer.deinit(allocator); 1219 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1220 } 1221 1222 // select the title by default 1223 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1224 1225 // reset the scroll to the top for the newly-shown issue: directly on the 1226 // terminal (the wasm offset), and via a version bump on the web (so the 1227 // renderer's scroll id changes and JS drops the preserved position). 1228 const sc = self.detailScroll(index); 1229 sc.x = 0; 1230 sc.y = 0; 1231 sc.getFocus().version +%= 1; 1232 } 1233 1234 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1235 // wheel input moves focus like the arrows 1236 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1237 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1238 else 1239 raw_key; 1240 1241 if (self.headerActive()) { 1242 // down from the tabs re-enters the stack if the selected view has 1243 // something to focus; other keys move the tabs. 1244 if (inp.vertDirection(key) == .down) { 1245 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1246 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1247 // nothing focusable in the unauthorized view 1248 .unauthorized => false, 1249 else => true, 1250 } else false; 1251 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1252 } else { 1253 try self.header().input(allocator, key, root_focus); 1254 } 1255 return; 1256 } 1257 if (self.tagsViewActive()) { 1258 self.tagsViewInput(key, root_focus); 1259 return; 1260 } 1261 if (self.formViewActive()) { 1262 if (has_conflicts and self.data.view == .resolve) { 1263 try self.resolveInput(allocator, key, root_focus); 1264 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1265 try self.commentFormInput(allocator, key, root_focus); 1266 } else { 1267 try self.formInput(allocator, key, root_focus); 1268 } 1269 return; 1270 } 1271 const i = self.selectedSplitIndex() orelse return; 1272 if (self.detailActive(i)) { 1273 try self.detailInput(allocator, i, key, root_focus); 1274 } else { 1275 try self.listInput(i, key, root_focus); 1276 } 1277 } 1278 1279 // arrow keys move the tag selection; up from the top row crosses to the 1280 // header tabs. 1281 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 1282 const tf = self.tagsView(); 1283 const cid = tf.focus.child_id orelse return; 1284 const cur = tf.indexOfFocusId(cid) orelse return; 1285 const count = tf.text_boxes.items.len; 1286 switch (key) { 1287 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1288 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1289 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1290 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1291 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1292 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1293 else => {}, 1294 } 1295 } 1296 1297 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1298 // up/down (and the scroll wheel) move the selection a row; page up/down 1299 // jump a fixed amount. right/Enter cross into the detail pane. up from 1300 // the top row crosses into the header tabs. 1301 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1302 const lb = self.listBox(index); 1303 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1304 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1305 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1306 return; 1307 } 1308 switch (key) { 1309 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1310 else => {}, 1311 } 1312 } 1313 1314 fn detailInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1315 switch (key) { 1316 .page_up => { 1317 self.pageDetail(index, root_focus, -10); 1318 return; 1319 }, 1320 .page_down => { 1321 self.pageDetail(index, root_focus, 10); 1322 return; 1323 }, 1324 .home => { 1325 self.jumpDetail(index, root_focus, false); 1326 return; 1327 }, 1328 .end => { 1329 self.jumpDetail(index, root_focus, true); 1330 return; 1331 }, 1332 else => {}, 1333 } 1334 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)) { 1335 try self.commentInput(allocator, index, key, root_focus); 1336 } else if (self.toolRowFocused(index)) { 1337 try self.toolRowInput(allocator, index, key, root_focus); 1338 } else if (self.titleFocused(index)) { 1339 try self.titleInput(index, key, root_focus); 1340 } else if (self.authorFocused(index)) { 1341 try self.authorInput(index, key, root_focus); 1342 } else if (self.tagsFocused(index)) { 1343 try self.tagsInput(index, key, root_focus); 1344 } else if (self.descriptionFocused(index)) { 1345 try self.descriptionInput(index, key, root_focus); 1346 } else if (try self.attachmentInput(allocator, index, key, root_focus)) {} else { 1347 try self.commentInput(allocator, index, key, root_focus); 1348 } 1349 } 1350 1351 fn attachmentInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !bool { 1352 const child_index = self.focusedDetailChild(index, root_focus) orelse return false; 1353 const child = &self.detailInner(index).children.values()[child_index].widget; 1354 const row = switch (child.*) { 1355 .box => |*box| box, 1356 else => return false, 1357 }; 1358 const attachment_id = Attachment.removeId(row) orelse return false; 1359 if (row.children.count() < 2) return false; 1360 const remove_id = row.children.keys()[1]; 1361 switch (key) { 1362 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) try self.focusList(index, root_focus), 1363 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1364 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1365 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1366 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 1367 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 1368 else => {}, 1369 } 1370 return true; 1371 } 1372 1373 // the tool row: enter or a click on the open/close button flips the 1374 // issue's status; the edit and resolve buttons are a: links the host 1375 // follows. arrows cross between the buttons and to the neighboring 1376 // widgets. 1377 fn toolRowInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1378 const row = self.toolRow(index); 1379 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1380 const on_status = if (self.status_button_id[index]) |id| row.getFocus().child_id == id else false; 1381 const on_remove = if (self.remove_button_id[index]) |id| row.getFocus().child_id == id else false; 1382 switch (key) { 1383 .arrow_left => if (cur > first_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1384 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1385 .arrow_up => self.focusHeader(root_focus), 1386 .arrow_down => { 1387 self.focusDetailEdge(index, root_focus, false); 1388 self.detailScroll(index).y = 0; 1389 }, 1390 .enter => if (on_remove) try self.removeThread(allocator, index) else if (on_status) try self.toggleStatus(allocator, index), 1391 .mouse => |mouse| if (self.remove_button_id[index]) |id| { 1392 if (inp.leftClickOn(root_focus, id, mouse)) return self.removeThread(allocator, index); 1393 if (self.status_button_id[index]) |status_id| if (inp.leftClickOn(root_focus, status_id, mouse)) try self.toggleStatus(allocator, index); 1394 } else if (self.status_button_id[index]) |id| { 1395 if (inp.leftClickOn(root_focus, id, mouse)) try self.toggleStatus(allocator, index); 1396 }, 1397 else => {}, 1398 } 1399 } 1400 1401 fn titleInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1402 switch (key) { 1403 .arrow_left => try self.focusList(index, root_focus), 1404 .arrow_up => self.focusToolRow(index, root_focus), 1405 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1406 else => {}, 1407 } 1408 } 1409 1410 // the author box's a: link (when it has one) is followed by the host on 1411 // enter / a click; arrows cross to the neighboring widgets. 1412 fn authorInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1413 switch (key) { 1414 .arrow_left => try self.focusList(index, root_focus), 1415 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1416 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1417 else => {}, 1418 } 1419 } 1420 1421 fn descriptionInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1422 const sc = self.detailScroll(index); 1423 switch (key) { 1424 .arrow_left => return self.focusList(index, root_focus), 1425 // the terminal moves through the description one line at a time; 1426 // the web lets focus changes drive its native scroll. 1427 .arrow_up => { 1428 if (!self.session.is_terminal) { 1429 _ = self.moveDetailVertical(index, root_focus, false); 1430 return; 1431 } 1432 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1433 const before = sc.y; 1434 sc.y -= 1; 1435 sc.clampToContent(); 1436 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1437 return; 1438 }, 1439 .arrow_down => { 1440 if (!self.session.is_terminal) { 1441 _ = self.moveDetailVertical(index, root_focus, true); 1442 return; 1443 } 1444 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1445 const before = sc.y; 1446 sc.y += 1; 1447 sc.clampToContent(); 1448 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1449 return; 1450 }, 1451 else => return, 1452 } 1453 sc.clampToContent(); 1454 } 1455 1456 fn commentInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1457 const item = self.focusedComment(index, root_focus); 1458 switch (key) { 1459 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1460 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1461 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1462 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1463 .enter => if (item) |comment| if (comment.removeFocused()) try self.removeComment(allocator, comment), 1464 .mouse => |mouse| if (item) |comment| if (comment.remove_button_id) |id| { 1465 if (inp.leftClickOn(root_focus, id, mouse)) try self.removeComment(allocator, comment); 1466 }, 1467 else => return, 1468 } 1469 } 1470 1471 fn focusedComment(self: *This, index: usize, root_focus: *Focus) ?*Comment.Item { 1472 const child_index = self.focusedDetailChild(index, root_focus) orelse return null; 1473 return switch (self.detailInner(index).children.values()[child_index].widget) { 1474 .repo_comment => |*comment| comment, 1475 else => null, 1476 }; 1477 } 1478 1479 // page the detail scroll, then focus the first or last visible widget. 1480 fn pageDetail(self: *This, index: usize, root_focus: *Focus, delta: isize) void { 1481 if (!self.session.is_terminal) return; 1482 const sc = self.detailScroll(index); 1483 sc.y += delta; 1484 sc.clampToContent(); 1485 self.focusVisible(index, root_focus, delta > 0); 1486 } 1487 1488 // jump to the detail's first or last focusable widget. the terminal pins 1489 // its synthetic scroll too; the browser scrolls to the new focus itself. 1490 fn jumpDetail(self: *This, index: usize, root_focus: *Focus, to_end: bool) void { 1491 if (self.session.is_terminal) { 1492 const sc = self.detailScroll(index); 1493 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1494 sc.clampToContent(); 1495 self.focusVisible(index, root_focus, to_end); 1496 } else { 1497 self.focusDetailEdge(index, root_focus, to_end); 1498 } 1499 } 1500 1501 fn focusDetailEdge(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1502 const focus = self.detailInner(index).getFocus(); 1503 var chosen: ?usize = null; 1504 for (focus.children.keys(), focus.children.values()) |id, child| { 1505 if (!child.focus.focusable) continue; 1506 chosen = id; 1507 if (!last) break; 1508 } 1509 if (chosen) |id| root_focus.setFocus(id); 1510 } 1511 1512 fn detailRectVisible(self: *This, index: usize, rect: layout.IRect) bool { 1513 const sc = self.detailScroll(index); 1514 const viewport = sc.grid orelse return false; 1515 const top = sc.y; 1516 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1517 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1518 } 1519 1520 // focus the top- or bottom-most focusable widget intersecting the viewport. 1521 fn focusVisible(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1522 const inner = self.detailInner(index); 1523 var offset: usize = 0; 1524 while (offset < inner.children.count()) : (offset += 1) { 1525 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1526 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1527 } 1528 } 1529 1530 fn focusVisibleChild(self: *This, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1531 const child = &self.detailInner(index).children.values()[child_index]; 1532 const child_rect = child.rect orelse return false; 1533 var chosen: ?usize = null; 1534 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1535 const id = self.detailInner(index).children.keys()[child_index]; 1536 chosen = id; 1537 if (!last) { 1538 root_focus.setFocus(id); 1539 return true; 1540 } 1541 } 1542 1543 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1544 if (!focus_child.focus.focusable) continue; 1545 const rect: layout.IRect = .{ 1546 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1547 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1548 .size = focus_child.rect.size, 1549 }; 1550 if (!self.detailRectVisible(index, rect)) continue; 1551 chosen = id; 1552 if (!last) break; 1553 } 1554 if (chosen) |id| { 1555 root_focus.setFocus(id); 1556 return true; 1557 } 1558 return false; 1559 } 1560 1561 fn focusAdjacentVisible(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1562 const inner = self.detailInner(index); 1563 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1564 while (true) { 1565 if (down) { 1566 next += 1; 1567 if (next >= inner.children.count()) return false; 1568 } else { 1569 if (next == 0) return false; 1570 next -= 1; 1571 } 1572 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1573 self.focusDetailChild(index, next, !down, root_focus); 1574 return true; 1575 } 1576 } 1577 } 1578 1579 fn focusedDetailChild(self: *This, index: usize, root_focus: *Focus) ?usize { 1580 const inner = self.detailInner(index); 1581 var id = root_focus.grandchild_id orelse return null; 1582 while (inner.getFocus().children.get(id)) |child| { 1583 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1584 id = child.parent_id; 1585 } 1586 return null; 1587 } 1588 1589 fn detailChildFocusable(self: *This, index: usize, child_index: usize) bool { 1590 const child = &self.detailInner(index).children.values()[child_index].widget; 1591 const focus = child.getFocus(); 1592 if (focus.focusable) return true; 1593 for (focus.children.values()) |item| { 1594 if (item.focus.focusable) return true; 1595 } 1596 return false; 1597 } 1598 1599 fn focusDetailChild(self: *This, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1600 const inner = self.detailInner(index); 1601 const child = &inner.children.values()[child_index]; 1602 var target = inner.children.keys()[child_index]; 1603 var rect = child.rect orelse return; 1604 switch (child.widget) { 1605 .repo_comment => |*comment| { 1606 if (comment.rowRect(last)) |row_rect| { 1607 rect.x += row_rect.x; 1608 rect.y += row_rect.y; 1609 rect.size = row_rect.size; 1610 } 1611 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1612 }, 1613 .box => |*box| if (box.children.count() > 0) { 1614 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1615 root_focus.setFocus(target); 1616 }, 1617 else => root_focus.setFocus(target), 1618 } 1619 const sc = self.detailScroll(index); 1620 if (sc.grid) |viewport| { 1621 const viewport_height = viewport.size.height - sc.bar_h; 1622 if (rect.size.height > viewport_height) { 1623 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1624 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1625 sc.clampToContent(); 1626 return; 1627 } 1628 } 1629 sc.scrollToRect(rect); 1630 } 1631 1632 fn moveDetailVertical(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1633 const inner = self.detailInner(index); 1634 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1635 if (inner.children.values()[current].widget == .repo_comment) { 1636 const comment = &inner.children.values()[current].widget.repo_comment; 1637 if (down and !comment.bodyFocused()) { 1638 self.focusDetailChild(index, current, true, root_focus); 1639 return true; 1640 } 1641 if (!down and comment.bodyFocused()) { 1642 self.focusDetailChild(index, current, false, root_focus); 1643 return true; 1644 } 1645 } 1646 1647 var next = current; 1648 while (true) { 1649 if (down) { 1650 next += 1; 1651 if (next >= inner.children.count()) return false; 1652 } else { 1653 if (next == 0) return false; 1654 next -= 1; 1655 } 1656 if (!self.detailChildFocusable(index, next)) continue; 1657 self.focusDetailChild(index, next, !down, root_focus); 1658 return true; 1659 } 1660 } 1661 1662 fn moveCommentHorizontal(self: *This, index: usize, root_focus: *Focus, right: bool) bool { 1663 const inner = self.detailInner(index); 1664 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1665 const child = &inner.children.values()[current]; 1666 if (child.widget == .repo_comment) { 1667 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1668 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1669 return moved; 1670 } 1671 const row = switch (child.widget) { 1672 .box => |*box| box, 1673 else => return false, 1674 }; 1675 const selected = row.getFocus().child_id orelse return false; 1676 const selected_index = row.children.getIndex(selected) orelse return false; 1677 const target = if (right) selected_index + 1 else selected_index -| 1; 1678 if (target == selected_index or target >= row.children.count()) return false; 1679 root_focus.setFocus(row.children.keys()[target]); 1680 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1681 return true; 1682 } 1683 1684 // up/down (and tab/shift+tab) move between a form's fields; up from the 1685 // title crosses into the header tabs. the multiline description keeps 1686 // enter and any up/down that has a row to move to. 1687 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1688 const form = self.threadForm() orelse return; 1689 const cid = form.getFocus().child_id orelse return; 1690 const cur = form.children.getIndex(cid) orelse return; 1691 1692 if (cur == description_field_index) { 1693 const child = &form.children.values()[cur]; 1694 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1695 return child.widget.input(allocator, key, root_focus); 1696 } 1697 1698 switch (key) { 1699 .arrow_up, .back_tab => if (cur > 0) 1700 root_focus.setFocus(form.children.keys()[cur - 1]) 1701 else 1702 self.focusHeader(root_focus), 1703 .arrow_down, .tab => if (cur < submit_field_index) { 1704 root_focus.setFocus(form.children.keys()[cur + 1]); 1705 }, 1706 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1707 .mouse => |mouse| if (cur == submit_field_index) { 1708 const submit = &form.children.values()[cur].widget.submit_button; 1709 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1710 }, 1711 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1712 } 1713 } 1714 1715 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1716 const form = self.threadForm() orelse return; 1717 const cid = form.getFocus().child_id orelse return; 1718 const cur = form.children.getIndex(cid) orelse return; 1719 const child = &form.children.values()[cur]; 1720 1721 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1722 return child.widget.input(allocator, key, root_focus); 1723 1724 switch (key) { 1725 .arrow_up, .back_tab => if (cur > comment_author_field_index) 1726 root_focus.setFocus(form.children.keys()[cur - 1]) 1727 else 1728 self.focusHeader(root_focus), 1729 .arrow_down, .tab => if (cur < comment_submit_field_index) 1730 root_focus.setFocus(form.children.keys()[cur + 1]), 1731 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 1732 .mouse => |mouse| if (cur == comment_submit_field_index) { 1733 const submit = &child.widget.submit_button; 1734 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 1735 }, 1736 else => try child.widget.input(allocator, key, root_focus), 1737 } 1738 } 1739 1740 // the multiline hunk inputs keep enter and any up/down that has a row to 1741 // move to; enter on a "use this" link is a navigation the host follows 1742 // before this runs. 1743 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1744 const form = self.threadForm() orelse return; 1745 const cid = form.getFocus().child_id orelse return; 1746 const cur = form.children.getIndex(cid) orelse return; 1747 const child = &form.children.values()[cur]; 1748 1749 if (child.widget == .text_input) { 1750 const ti = &child.widget.text_input; 1751 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1752 return child.widget.input(allocator, key, root_focus); 1753 } 1754 1755 const on_submit = child.widget == .submit_button; 1756 1757 switch (key) { 1758 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1759 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1760 .arrow_left, .arrow_right => switch (child.widget) { 1761 .box => |*row| { 1762 const row_cid = row.getFocus().child_id orelse return; 1763 const row_index = row.children.getIndex(row_cid) orelse return; 1764 if (key == .arrow_left) { 1765 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1766 } else if (row_index + 1 < row.children.count()) { 1767 root_focus.setFocus(row.children.keys()[row_index + 1]); 1768 } 1769 }, 1770 else => try child.widget.input(allocator, key, root_focus), 1771 }, 1772 .enter => if (on_submit) try self.submitResolution(allocator), 1773 .mouse => |mouse| if (on_submit) { 1774 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1775 }, 1776 else => try child.widget.input(allocator, key, root_focus), 1777 } 1778 } 1779 1780 // a multiline input keeps enter and any up/down that has a row to move to 1781 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1782 return switch (key) { 1783 .enter => true, 1784 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1785 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1786 else => false, 1787 }; 1788 } 1789 1790 // the neighboring focusable resolve-form child, skipping the spacer and 1791 // the blank gap rows 1792 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1793 var i = cur; 1794 while (true) { 1795 if (down) { 1796 i += 1; 1797 if (i >= form.children.count()) return null; 1798 } else { 1799 if (i == 0) return null; 1800 i -= 1; 1801 } 1802 switch (form.children.values()[i].widget) { 1803 .spacer, .text => continue, 1804 else => return i, 1805 } 1806 } 1807 } 1808 1809 // focus the resolve-form child at `i` (a version row focuses its selected 1810 // child) and keep it visible on the terminal 1811 fn focusResolveChild(self: *This, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 1812 const child = &form.children.values()[i]; 1813 switch (child.widget) { 1814 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 1815 else => root_focus.setFocus(form.children.keys()[i]), 1816 } 1817 if (self.session.is_terminal) { 1818 if (self.resolveScroll()) |sc| { 1819 if (child.rect) |rect| sc.scrollToRect(rect); 1820 } 1821 } 1822 } 1823 1824 // re-emit the conflicted issue's event with the resolved content; any 1825 // event on the issue settles its conflict entry. this is the terminal 1826 // path; the web posts the form to the resolve route. 1827 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 1828 if (comptime wasm) return; 1829 const io = self.session.io orelse return; 1830 const src = self.data.repo_source orelse return; 1831 const entry = self.data.selectedThread() orelse return; 1832 const author = (try self.session.eventAuthor()) orelse return; 1833 const form = self.threadForm() orelse return; 1834 1835 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 1836 var title: ?[]u8 = null; 1837 var tags: ?[]u8 = null; 1838 var hunks: std.ArrayList([]const u8) = .empty; 1839 defer { 1840 if (title) |t| allocator.free(t); 1841 if (tags) |t| allocator.free(t); 1842 for (hunks.items) |h| allocator.free(h); 1843 hunks.deinit(allocator); 1844 } 1845 for (form.children.values()) |*child| switch (child.widget) { 1846 .text_input => |*ti| { 1847 const text = try ti.text(allocator); 1848 errdefer allocator.free(text); 1849 if (std.mem.eql(u8, ti.options.name, "title")) { 1850 title = text; 1851 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 1852 tags = text; 1853 } else { 1854 try hunks.append(allocator, text); 1855 } 1856 }, 1857 else => {}, 1858 }; 1859 1860 var id_bytes: [evt.event_id_size]u8 = undefined; 1861 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1862 1863 switch (src.repo_kind) { 1864 inline else => |repo_kind| { 1865 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 1866 defer any_repo.deinit(io, allocator); 1867 switch (any_repo) { 1868 inline else => |*repo| Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 1869 .title = title, 1870 .tags = tags, 1871 .hunks = hunks.items, 1872 } }, author) catch |err| switch (err) { 1873 // leave the form up for correction 1874 error.InvalidFields => return, 1875 else => |e| return e, 1876 }, 1877 } 1878 }, 1879 } 1880 1881 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 1882 try self.session.navigate(route); 1883 } 1884 1885 // commit a new or edited comment and navigate to its permalink. this is the 1886 // terminal path; the web posts the form to its comment route. 1887 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 1888 if (comptime wasm) return; 1889 const io = self.session.io orelse return; 1890 const src = self.data.repo_source orelse return; 1891 const author = (try self.session.eventAuthor()) orelse return; 1892 const form = self.threadForm() orelse return; 1893 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 1894 const body = try body_input.text(allocator); 1895 defer allocator.free(body); 1896 if (!evt.Comment.fieldsValid(body)) return; 1897 1898 var thread_id: [evt.event_id_size * 2]u8 = undefined; 1899 @memcpy(&thread_id, self.data.selected_id); 1900 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 1901 var parent: [evt.event_id_size * 2]u8 = undefined; 1902 @memcpy(&parent, self.data.comment_id); 1903 break :blk parent; 1904 }; 1905 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 1906 const editing = self.data.view == .edit_comment; 1907 switch (src.repo_kind) { 1908 inline else => |repo_kind| { 1909 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 1910 defer any_repo.deinit(io, allocator); 1911 switch (any_repo) { 1912 inline else => |*repo| if (editing) { 1913 var comment_id: [evt.event_id_size]u8 = undefined; 1914 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 1915 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 1916 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 1917 } else { 1918 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 1919 }, 1920 } 1921 }, 1922 } 1923 1924 body_input.clear(allocator); 1925 const route = commentRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 1926 try self.session.navigate(route); 1927 } 1928 1929 // the terminal submit paths: the new form commits a new-issue event, the 1930 // edit form re-emits the selected issue's event with the form's content. 1931 // the web posts the forms to their routes instead. 1932 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 1933 if (self.data.view == .edit) { 1934 try self.submitEditedThread(allocator); 1935 } else { 1936 try self.submitNewThread(allocator); 1937 } 1938 } 1939 1940 // commit the new issue to the repo's events branch and navigate to it. 1941 // this is the terminal path; the web posts the form to the issue route, 1942 // so the wasm side never runs (or compiles) the repo access below. 1943 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 1944 if (comptime wasm) return; 1945 const io = self.session.io orelse return; 1946 const src = self.data.repo_source orelse return; 1947 const author = (try self.session.eventAuthor()) orelse return; 1948 1949 const form = self.threadForm() orelse return; 1950 const title_input = &form.children.values()[title_field_index].widget.text_input; 1951 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1952 const description_input = &form.children.values()[description_field_index].widget.text_input; 1953 1954 const title = try title_input.text(allocator); 1955 defer allocator.free(title); 1956 const tags = try tags_input.text(allocator); 1957 defer allocator.free(tags); 1958 const description = try description_input.text(allocator); 1959 defer allocator.free(description); 1960 1961 if (!Event.fieldsValid(title, tags)) return; 1962 1963 var id_bytes: [evt.event_id_size]u8 = undefined; 1964 io.random(&id_bytes); 1965 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1966 1967 const event = evt.EventWithId{ 1968 .id = event_id_hex, 1969 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1970 .author = author, 1971 .event = @unionInit(evt.Event, @tagName(kind), Event{ 1972 .title = title, 1973 .description = description, 1974 .tags = tags, 1975 }), 1976 }; 1977 1978 switch (src.repo_kind) { 1979 inline else => |repo_kind| { 1980 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 1981 defer any_repo.deinit(io, allocator); 1982 switch (any_repo) { 1983 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1984 } 1985 }, 1986 } 1987 1988 // wipe the form so a return visit starts fresh 1989 title_input.clear(allocator); 1990 tags_input.clear(allocator); 1991 description_input.clear(allocator); 1992 1993 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 1994 try self.session.navigate(route); 1995 } 1996 1997 // re-emit the selected issue's event with the form's content (status 1998 // preserved), then reload the issue's page. this is the terminal path; 1999 // the web posts the form to the edit route. 2000 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2001 if (comptime wasm) return; 2002 const io = self.session.io orelse return; 2003 const src = self.data.repo_source orelse return; 2004 const entry = self.data.selectedThread() orelse return; 2005 const author = (try self.session.eventAuthor()) orelse return; 2006 2007 const form = self.threadForm() orelse return; 2008 const title_input = &form.children.values()[title_field_index].widget.text_input; 2009 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2010 const description_input = &form.children.values()[description_field_index].widget.text_input; 2011 2012 const title = try title_input.text(allocator); 2013 defer allocator.free(title); 2014 const tags = try tags_input.text(allocator); 2015 defer allocator.free(tags); 2016 const description = try description_input.text(allocator); 2017 defer allocator.free(description); 2018 2019 if (!Event.fieldsValid(title, tags)) return; 2020 2021 var id_bytes: [evt.event_id_size]u8 = undefined; 2022 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2023 2024 switch (src.repo_kind) { 2025 inline else => |repo_kind| { 2026 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2027 defer any_repo.deinit(io, allocator); 2028 switch (any_repo) { 2029 inline else => |*repo| if (has_status) 2030 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2031 .title = title, 2032 .tags = tags, 2033 .description = description, 2034 } }, author) 2035 else 2036 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2037 } 2038 }, 2039 } 2040 2041 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2042 try self.session.navigate(route); 2043 } 2044 2045 fn removeThread(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2046 const sel = self.detailed_index[index] orelse return; 2047 const entry = self.window(index).items[sel]; 2048 2049 var id_bytes: [evt.event_id_size]u8 = undefined; 2050 _ = std.fmt.hexToBytes(&id_bytes, entry.id) catch return; 2051 try self.removeEvent(allocator, kind, id_bytes); 2052 } 2053 2054 fn removeComment(self: *This, allocator: std.mem.Allocator, comment: *Comment.Item) !void { 2055 try self.removeEvent(allocator, .comment, comment.id); 2056 } 2057 2058 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2059 if (comptime wasm) return; 2060 const io = self.session.io orelse return; 2061 const src = self.data.repo_source orelse return; 2062 const author = (try self.session.eventAuthor()) orelse return; 2063 2064 switch (src.repo_kind) { 2065 inline else => |repo_kind| { 2066 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2067 defer any_repo.deinit(io, allocator); 2068 switch (any_repo) { 2069 inline else => |*repo| try evt.remove(repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2070 } 2071 }, 2072 } 2073 2074 const id_hex = std.fmt.bytesToHex(id, .lower); 2075 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2076 try self.session.navigate(route); 2077 } 2078 2079 // flip the shown issue's status by re-emitting its event, then reload the 2080 // page rooted at the issue so the view reflects the change. this is the 2081 // terminal path; the web posts the button's form to the status route. 2082 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2083 if (comptime !has_status) return; 2084 if (comptime wasm) return; 2085 const io = self.session.io orelse return; 2086 const src = self.data.repo_source orelse return; 2087 const sel = self.detailed_index[index] orelse return; 2088 const entry = self.window(index).items[sel]; 2089 const author = (try self.session.eventAuthor()) orelse return; 2090 2091 const status: Status = switch (entryStatus(entry)) { 2092 .open => .closed, 2093 .closed => .open, 2094 }; 2095 2096 var id_bytes: [evt.event_id_size]u8 = undefined; 2097 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2098 2099 switch (src.repo_kind) { 2100 inline else => |repo_kind| { 2101 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2102 defer any_repo.deinit(io, allocator); 2103 switch (any_repo) { 2104 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2105 } 2106 }, 2107 } 2108 2109 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2110 try self.session.navigate(route); 2111 } 2112 2113 // arrow keys move the tag selection; at the flow's edges focus crosses to 2114 // the neighboring widgets. 2115 fn tagsInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 2116 const tf = self.tagFlow(index) orelse return; 2117 const cid = tf.focus.child_id orelse return; 2118 const cur = tf.indexOfFocusId(cid) orelse return; 2119 const count = tf.text_boxes.items.len; 2120 switch (key) { 2121 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2122 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2123 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2124 self.focusTag(index, tf, root_focus, i); 2125 } else { 2126 _ = self.moveDetailVertical(index, root_focus, false); 2127 }, 2128 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2129 self.focusTag(index, tf, root_focus, i); 2130 } else { 2131 _ = self.moveDetailVertical(index, root_focus, true); 2132 }, 2133 .home => self.focusTag(index, tf, root_focus, 0), 2134 .end => self.focusTag(index, tf, root_focus, count - 1), 2135 else => {}, 2136 } 2137 } 2138 2139 const title_child_index: usize = 0; 2140 const tags_child_index: usize = 2; 2141 2142 // the tool row's first child after the spacer that pushes it right 2143 const first_in_row_index: usize = 1; 2144 2145 fn tagFlow(self: *This, index: usize) ?*ui.TagFlow { 2146 const inner = self.detailInner(index); 2147 if (inner.children.count() <= tags_child_index) return null; 2148 return switch (inner.children.values()[tags_child_index].widget) { 2149 .tag_flow => |*tf| tf, 2150 else => null, 2151 }; 2152 } 2153 2154 fn tagsFocused(self: *This, index: usize) bool { 2155 const inner = self.detailInner(index); 2156 const cid = inner.getFocus().child_id orelse return false; 2157 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2158 } 2159 2160 fn toolRowFocused(self: *This, index: usize) bool { 2161 const outer = self.detailOuter(index); 2162 const cid = outer.getFocus().child_id orelse return false; 2163 return outer.children.getIndex(cid) == tool_row_index; 2164 } 2165 2166 fn focusTag(self: *This, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2167 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2168 // keep the tag visible on the terminal: its rect offset by the flow's 2169 // position in the pane. 2170 if (self.session.is_terminal and item < tf.rects.items.len) { 2171 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2172 var rect = tf.rects.items[item]; 2173 rect.x += flow_rect.x; 2174 rect.y += flow_rect.y; 2175 self.detailScroll(index).scrollToRect(rect); 2176 } 2177 } 2178 } 2179 2180 fn titleFocused(self: *This, index: usize) bool { 2181 const inner = self.detailInner(index); 2182 const cid = inner.getFocus().child_id orelse return false; 2183 return inner.children.getIndex(cid) == title_child_index; 2184 } 2185 2186 fn authorFocused(self: *This, index: usize) bool { 2187 const inner = self.detailInner(index); 2188 const cid = inner.getFocus().child_id orelse return false; 2189 return cid == self.author_id[index]; 2190 } 2191 2192 fn descriptionFocused(self: *This, index: usize) bool { 2193 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2194 return cid == self.description_id[index]; 2195 } 2196 2197 // return to the tool row's last-focused button (the header when the 2198 // description page shows no row). 2199 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2200 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2201 root_focus.setFocus(cid); 2202 } 2203 2204 // enter the detail pane. an empty pane (no issues) can't be entered. 2205 fn focusDetail(self: *This, index: usize, root_focus: *Focus) !void { 2206 if (self.detailInner(index).children.count() == 0) return; 2207 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2208 } 2209 2210 // return to the list. 2211 fn focusList(self: *This, index: usize, root_focus: *Focus) !void { 2212 root_focus.setFocus(self.listScroll(index).getFocus().id); 2213 } 2214 2215 // cross to the header tabs above the stack. 2216 fn focusHeader(self: *This, root_focus: *Focus) void { 2217 root_focus.setFocus(self.box.children.keys()[header_index]); 2218 } 2219 2220 pub fn clearGrid(self: *This) void { 2221 self.box.clearGrid(); 2222 } 2223 2224 pub fn getGrid(self: This) ?Grid { 2225 return self.box.getGrid(); 2226 } 2227 2228 pub fn getFocus(self: *This) *Focus { 2229 return self.box.getFocus(); 2230 } 2231 2232 // for the parent's "scroll up at the top jumps to the header" check: at the 2233 // top only while the header tabs hold focus, so up from the split 2234 // crosses into the tabs first. 2235 pub fn getSelectedIndex(self: *This) ?usize { 2236 return if (self.headerActive()) 0 else 1; 2237 } 2238 }; 2239 } 2240