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