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