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