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