╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
293 arena: *std.heap.ArenaAllocator, 294 admin_moment: ?evt.AdminDB.HashMap(.read_only), 295 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 296 records: evt.EventDB(hash_kind).HashMap(.read_only), 297 set_maybe: ?evt.EventDB(hash_kind).SortedSet(.read_only), 298 root_key: ?[]const u8, 299 conflict_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 300 selected_id: []const u8, 301 comments_start: usize, 302 query_text: []const u8, 303 viewer: ?ui.Viewer, 304 ) !Data.Window { 305 const set = set_maybe orelse return .empty; 306 const DB = evt.EventDB(hash_kind); 307 const aa = arena.allocator(); 308 const root = root_key orelse ""; 309 310 // the matching list keys, in list order. the cap bounds the count, not the 311 // paging: past it a pass goes on only until the window and its "next" link 312 // are known. 313 var keys: std.ArrayList(OrderKey) = .empty; 314 if (comptime kind == .discuss) { 315 // discussions list by activity but are indexed by creation, so the 316 // matching ids are gathered and the listed set is walked in its order 317 // until every one has been found 318 var matches: std.AutoHashMapUnmanaged([evt.event_id_size]u8, void) = .empty; 319 var results = try srch_thrd.query(DB, hash_kind, kind, haxy_moment, aa, query_text, null); 320 while (try results.next()) |key| try matches.put(aa, (try resultKey(key))[@sizeOf(u64)..].*, {}); 321 322 var iter = try set.iteratorFromIndex(0); 323 while (matches.count() > 0) { 324 const pair = try ((try iter.next()) orelse break).readKeyValuePair(); 325 var order_key: OrderKey = undefined; 326 _ = try pair.key_cursor.readBytes(&order_key); 327 if (!matches.remove(order_key[@sizeOf(u64)..].*)) continue; 328 try keys.append(aa, order_key); 329 if (windowKnown(Data, keys.items, root)) break; 330 } 331 } else { 332 // issues and patches list in the index's own order, so the listed set 333 // narrows the query and the results stream out already sorted 334 var results = try srch_thrd.query(DB, hash_kind, kind, haxy_moment, aa, query_text, set); 335 while (try results.next()) |key| { 336 try keys.append(aa, try resultKey(key)); 337 if (windowKnown(Data, keys.items, root)) break; 338 } 339 } 340 341 // the window starts at the first key at or after the root 342 var first: usize = 0; 343 while (first < keys.items.len and std.mem.order(u8, &keys.items[first], root) == .lt) first += 1; 344 345 var items: std.ArrayList(Data.Entry) = .empty; 346 const last = @min(first + Data.page_size, keys.items.len); 347 for (keys.items[first..last]) |*order_key| { 348 const entry = (try loadEntry(Data, hash_kind, arena, admin_moment, haxy_moment, records, order_key, conflict_set, selected_id, comments_start, viewer)) orelse continue; 349 try items.append(aa, entry); 350 } 351 352 return .{ 353 .items = items.items, 354 // the page a window back, which is the first one when it holds the root 355 .prev_id = if (first == 0) null else if (first <= Data.page_size) "" else try aa.dupe(u8, &keyIdHex(&keys.items[first - Data.page_size])), 356 .next_id = if (last < keys.items.len) try aa.dupe(u8, &keyIdHex(&keys.items[last])) else null, 357 .count = @min(keys.items.len, max_search_results), 358 .more = keys.items.len > max_search_results, 359 }; 360 } 361 362 // whether a search pass can stop: the count is capped, and the window rooted at 363 // `root` and the key after it have been collected 364 fn windowKnown(comptime Data: type, keys: []const OrderKey, root: []const u8) bool { 365 if (keys.len <= max_search_results) return false; 366 return std.mem.order(u8, &keys[keys.len - 1 - Data.page_size], root) != .lt; 367 } 368 369 // one list entry: the record `order_key` names, with its author, comments and 370 // attachments. null when the record is gone. 371 fn loadEntry( 372 comptime Data: type, 373 comptime hash_kind: hash.HashKind, 374 arena: *std.heap.ArenaAllocator, 375 admin_moment: ?evt.AdminDB.HashMap(.read_only), 376 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 377 records: evt.EventDB(hash_kind).HashMap(.read_only), 378 order_key: *const OrderKey, 379 conflict_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 380 selected_id: []const u8, 381 comments_start: usize, 382 viewer: ?ui.Viewer, 383 ) !?Data.Entry { 384 const DB = evt.EventDB(hash_kind); 385 const aa = arena.allocator(); 386 const id_hex = keyIdHex(order_key); 387 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, order_key[@sizeOf(u64)..])) orelse return null; 388 const record = try evt.read(Data.Event.Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 389 var entry: Data.Entry = .{ 390 .id = try aa.dupe(u8, &id_hex), 391 .record = record, 392 .author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email), 393 .comments = try Comment.loadWindow( 394 hash_kind, 395 arena, 396 admin_moment, 397 haxy_moment, 398 evt.Comment.thread_id_to_comment_id_set_key, 399 &id_hex, 400 if (std.mem.eql(u8, &id_hex, selected_id)) comments_start else 0, 401 viewer, 402 ), 403 .attachments = try Attachment.load(hash_kind, arena, haxy_moment, &id_hex), 404 }; 405 if (comptime @hasField(Data.Entry, "conflicted")) { 406 entry.conflicted = if (conflict_set) |conflicts| try conflicts.contains(order_key) else false; 407 } 408 return entry; 409 } 410 411 // a tab link's route, carrying the page's query when it has one 412 pub fn searchRoute(route_maybe: ?ui.RoutablePage, search: ?[]const u8) !ui.RoutablePage { 413 const route = route_maybe orelse return error.RouteTooLong; 414 const query = search orelse return route; 415 return route.withSearch(query) orelse error.RouteTooLong; 416 } 417 418 // a status tab's label: its listing's count, marked when a search capped it 419 pub fn countLabel(buffer: []u8, name: []const u8, win: anytype) ![]const u8 { 420 return std.fmt.bufPrint(buffer, "{s} ({d}{s})", .{ name, win.count, if (win.more) "+" else "" }); 421 } 422 423 // the common sub-header used by each thread page: the search box, then the 424 // tab strip 425 pub const Header = struct { 426 box: wgt.Box(Widget), 427 // focus id -> semantic stack index; some pages omit unavailable tabs 428 tab_ids: std.AutoArrayHashMapUnmanaged(usize, usize), 429 // the tab whose view the stack shows. focus lands on the search box too, 430 // so the selection is remembered rather than read off the focus chain. 431 selected_tab: ?usize = null, 432 433 // the search box's place in the box, ahead of the tabs 434 const search_index: usize = 0; 435 436 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, search: ?[]const u8) !Header { 437 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 438 errdefer box.deinit(allocator); 439 440 { 441 var search_box = try widget.SearchBox.init(allocator, session, " search ", "search", search); 442 errdefer search_box.deinit(allocator); 443 box.getFocus().child_id = search_box.getFocus().id; 444 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = widget.SearchBox.min_size }); 445 } 446 447 return .{ .box = box, .tab_ids = .empty }; 448 } 449 450 pub fn addTab(self: *Header, allocator: std.mem.Allocator, label: []const u8, link: []const u8, view_index: usize) !void { 451 var text_box = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 452 errdefer text_box.deinit(allocator); 453 text_box.getFocus().mode = .all; 454 text_box.getFocus().kind = .{ .custom = link }; 455 try self.tab_ids.put(allocator, text_box.getFocus().id, view_index); 456 try self.box.children.put(allocator, text_box.getFocus().id, .{ 457 .widget = .{ .text_box = text_box }, 458 .rect = null, 459 .min_size = .{ .width = label.len + 2, .height = null }, 460 }); 461 } 462 463 pub fn select(self: *Header, view_index: usize) void { 464 for (self.tab_ids.values(), 0..) |index, tab| { 465 if (index == view_index) { 466 self.selected_tab = tab; 467 return; 468 } 469 } 470 } 471 472 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 473 self.box.deinit(allocator); 474 self.tab_ids.deinit(allocator); 475 } 476 477 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 478 self.clearGrid(); 479 // the search box takes focus without changing the view, so the tab it 480 // was last on stays selected 481 if (self.currentTabIndex()) |tab| self.selected_tab = tab; 482 // only the selected tab shows its border 483 const selected_id = if (self.selectedTab()) |tab| self.tab_ids.keys()[tab] else null; 484 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 485 switch (child.widget) { 486 .text_box => |*tb| widget.markSelected(tb, selected_id == id), 487 else => {}, 488 } 489 } 490 // a row taller than the tabs, leaving a blank line beneath them. the 491 // parent sets no min, which would make it lay the focused content out first. 492 var header_constraint = constraint; 493 header_constraint.min_size.height = @max(constraint.min_size.height orelse 0, 4); 494 try self.box.build(allocator, header_constraint, root_focus); 495 } 496 497 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 498 const search_box = self.searchBox(); 499 if (self.box.getFocus().child_id == search_box.getFocus().id) { 500 // tab, and right-arrow past the last character, step to the tabs 501 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 502 root_focus.setFocus(self.tab_ids.keys()[0]); 503 return; 504 } 505 return search_box.input(allocator, key, root_focus); 506 } 507 const current_tab = self.currentTabIndex() orelse return; 508 if (key == .back_tab or (key == .arrow_left and current_tab == 0)) { 509 // remembered here as well as in build, which may not run between keys 510 self.selected_tab = current_tab; 511 root_focus.setFocus(search_box.getFocus().id); 512 return; 513 } 514 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 515 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 516 } 517 } 518 519 // the typed query when enter should submit it: the box holds focus. the 520 // caller owns the returned text. 521 pub fn submittedText(self: *Header, allocator: std.mem.Allocator) !?[]u8 { 522 const search_box = self.searchBox(); 523 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 524 return try search_box.text(allocator); 525 } 526 527 fn searchBox(self: *Header) *widget.SearchBox { 528 return &self.box.children.values()[search_index].widget.search_box; 529 } 530 531 pub fn clearGrid(self: *Header) void { 532 self.box.clearGrid(); 533 } 534 535 pub fn getGrid(self: Header) ?Grid { 536 return self.box.getGrid(); 537 } 538 539 pub fn getFocus(self: *Header) *Focus { 540 return self.box.getFocus(); 541 } 542 543 pub fn getSelectedIndex(self: Header) ?usize { 544 const tab = self.selectedTab() orelse return null; 545 return self.tab_ids.values()[tab]; 546 } 547 548 // the focused tab, or the one focus last left behind 549 fn selectedTab(self: Header) ?usize { 550 return self.currentTabIndex() orelse self.selected_tab; 551 } 552 553 fn currentTabIndex(self: Header) ?usize { 554 const child_id = self.box.focus.child_id orelse return null; 555 return self.tab_ids.getIndex(child_id); 556 } 557 }; 558 559 // the selected thread's scrollable contents, shared by repo and fork views 560 pub fn Detail(comptime kind: evt.EventKind, comptime Data: type) type { 561 const Entry = Data.Entry; 562 const Event = Data.Event; 563 const Status = Data.Status; 564 const has_status = @hasField(Event, "status"); 565 const supports_conflicts = Event.merge_policy == .field_conflicts; 566 const supports_drafts = @hasField(Data, "drafts"); 567 const supports_forks = @hasField(Entry, "fork_exists"); 568 const supports_merge = @hasDecl(Data, "mergeRoute"); 569 return struct { 570 const This = @This(); 571 const thread_name = Data.thread_name; 572 573 pub const Exit = enum { none, list, header }; 574 pub const Options = struct { actions: bool = true }; 575 576 scroll: wgt.Scroll(Widget), 577 data: *const Data, 578 session: *ui.Session, 579 entry: ?Entry, 580 title_id: ?usize, 581 description_id: ?usize, 582 author_id: ?usize, 583 actions: bool, 584 exit: Exit, 585 586 const tool_row_index: usize = 0; 587 588 pub fn init(allocator: std.mem.Allocator, data: *const Data, session: *ui.Session, entry: ?Entry, options: Options) !This { 589 var content = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 590 errdefer content.deinit(allocator); 591 var detail_scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = content }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 592 errdefer detail_scroll.deinit(allocator); 593 594 var self: This = .{ 595 .scroll = detail_scroll, 596 .data = data, 597 .session = session, 598 .entry = null, 599 .title_id = null, 600 .description_id = null, 601 .author_id = null, 602 .actions = options.actions, 603 .exit = .none, 604 }; 605 if (entry) |value| try self.setEntry(allocator, value); 606 return self; 607 } 608 609 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 610 self.scroll.deinit(allocator); 611 } 612 613 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 614 self.clearGrid(); 615 try self.scroll.build(allocator, constraint, root_focus); 616 } 617 618 pub fn input(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 619 _ = try self.inputWithExit(allocator, key, root_focus); 620 } 621 622 pub fn clearGrid(self: *This) void { 623 self.scroll.clearGrid(); 624 } 625 626 pub fn getGrid(self: This) ?Grid { 627 return self.scroll.getGrid(); 628 } 629 630 pub fn getFocus(self: *This) *Focus { 631 return self.scroll.getFocus(); 632 } 633 634 fn inner(self: *This) *wgt.Box(Widget) { 635 return &self.scroll.child.box; 636 } 637 638 fn toolRow(self: *This) *wgt.Box(Widget) { 639 return &self.inner().children.values()[tool_row_index].widget.box; 640 } 641 642 fn entryConflicted(entry: Entry) bool { 643 return if (supports_conflicts) entry.conflicted else false; 644 } 645 646 fn entryStatus(entry: Entry) Status { 647 if (comptime has_status) return Data.statusKind(entry.record.event); 648 return @enumFromInt(0); 649 } 650 651 fn entryDraft(entry: Entry) bool { 652 return if (supports_drafts) entry.draft else false; 653 } 654 655 const StatusChange = struct { 656 action: []const u8, 657 status: Status, 658 }; 659 660 fn statusChange(status: Status) ?StatusChange { 661 const name = @tagName(status); 662 if (std.mem.eql(u8, name, "open")) return .{ 663 .action = "close", 664 .status = std.meta.stringToEnum(Status, "closed") orelse return null, 665 }; 666 if (std.mem.eql(u8, name, "closed")) return .{ 667 .action = "open", 668 .status = std.meta.stringToEnum(Status, "open") orelse return null, 669 }; 670 return null; 671 } 672 673 fn listRoute(identity: []const u8, status: Status, label: []const u8, selected: []const u8) ?ui.RoutablePage { 674 return Data.listRoute(identity, status, label, selected); 675 } 676 677 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 678 return if (supports_drafts) Data.draftsRoute(identity) else null; 679 } 680 681 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 682 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 683 } 684 685 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 686 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 687 } 688 689 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 690 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 691 } 692 693 // a list link keeps the filters the page carries: the label, and the 694 // search query when one is set 695 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, id: []const u8, search: []const u8) ![]const u8 { 696 var route = listRoute(identity, status, label, id) orelse return error.RouteTooLong; 697 if (search.len != 0) route = route.withSearch(search) orelse return error.RouteTooLong; 698 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 699 } 700 701 fn labelLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, search: []const u8) ![]const u8 { 702 const encoded = try ui.urlEncodeRef(page_arena.allocator(), label); 703 return listLink(page_arena, identity, status, encoded, "", search); 704 } 705 706 fn addToolButton(allocator: std.mem.Allocator, row: *wgt.Box(Widget), label: []const u8, bottom_label: []const u8, action: []const u8) !void { 707 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = bottom_label }); 708 errdefer button.deinit(allocator); 709 button.getFocus().mode = .all; 710 button.getFocus().kind = .{ .custom = action }; 711 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = @max(try xitui.width.displayWidth(label), try xitui.width.displayWidth(bottom_label)) + 2, .height = null } }); 712 } 713 714 pub fn setEntry(self: *This, allocator: std.mem.Allocator, entry: Entry) !void { 715 const inner_box = self.inner(); 716 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 717 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 718 719 for (inner_box.children.values()) |*child| child.widget.deinit(allocator); 720 inner_box.children.clearAndFree(allocator); 721 inner_box.getFocus().child_id = null; 722 self.entry = entry; 723 self.title_id = null; 724 self.author_id = null; 725 726 { 727 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 728 errdefer row.deinit(allocator); 729 try inner_box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 730 } 731 if (self.actions and !description_page and comment_page == null) { 732 const row = self.toolRow(); 733 const pa = self.session.page_arena.allocator(); 734 735 if (supports_forks and self.session.data.current_page.parent() == .fork) { 736 // the fork page's own tabs show the diff and commits 737 const route = ui.RoutablePage.repoPatchesRoute(self.data.identity, .open, "", entry.id) orelse return error.RouteTooLong; 738 try addToolButton(allocator, row, "view on target repo", "", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 739 } else if (supports_forks) { 740 if (try Data.diffRoute(self.data.identity, entry)) |route| { 741 try addToolButton(allocator, row, "view diff", "", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 742 } 743 if (try Data.commitsRoute(self.data.identity, entry)) |route| { 744 const label = if (entry.commit_count) |count| try std.fmt.allocPrint(pa, "view commits ({d})", .{count}) else "view commits"; 745 try addToolButton(allocator, row, label, "", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 746 } 747 } 748 749 { 750 var spacer = try Spacer.init(allocator); 751 errdefer spacer.deinit(allocator); 752 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 753 } 754 755 if (comptime supports_merge) { 756 if (Data.canMerge(entry, self.session)) { 757 const route = mergeRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 758 try addToolButton(allocator, row, "merge", mergeBottomLabel(entry.mergeability.status()), try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 759 } 760 } 761 762 if (supports_drafts and entryDraft(entry)) { 763 const route = publishRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 764 try addToolButton(allocator, row, "publish", "", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 765 const edit_route = ui.RoutablePage.repoThreadEditRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 766 try addToolButton(allocator, row, "edit", "", try std.fmt.allocPrint(pa, "a:{s}", .{try edit_route.toUrl(self.session.page_arena)})); 767 const remove_route = ui.RoutablePage.repoThreadRemoveRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong; 768 try addToolButton(allocator, row, "✕", "", try std.fmt.allocPrint(pa, "a:{s}", .{try remove_route.toUrl(self.session.page_arena)})); 769 } else { 770 if (!self.session.is_terminal and !entryConflicted(entry)) { 771 const label = "add attachment"; 772 const action = if (self.data.identity.len == 0) 773 try std.fmt.allocPrint(pa, "{s}/{s}:{s}/attach", .{ ui.file_input_prefix, @tagName(kind), entry.id }) 774 else 775 try std.fmt.allocPrint(pa, "{s}/{s}/{s}:{s}/attach", .{ ui.file_input_prefix, self.data.identity, @tagName(kind), entry.id }); 776 try addToolButton(allocator, row, label, "", action); 777 } 778 779 if (supports_conflicts and entryConflicted(entry)) { 780 const route = resolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 781 try addToolButton(allocator, row, "resolve conflict", "", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 782 } else { 783 if (has_status) { 784 if (statusChange(entryStatus(entry))) |change| { 785 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 786 row.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "form:{s}/{s}", .{ try route.toUrl(self.session.page_arena), change.action }) }; 787 try addToolButton(allocator, row, change.action, "", "submit"); 788 } 789 } 790 791 const route = ui.RoutablePage.repoThreadEditRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 792 try addToolButton(allocator, row, "edit", "", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 793 } 794 795 const route = ui.RoutablePage.repoThreadRemoveRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong; 796 try addToolButton(allocator, row, "✕", "", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 797 } 798 } 799 800 if (supports_drafts and !description_page and comment_page == null) 801 try self.data.appendDetails(allocator, inner_box, self.session, entry); 802 803 if (comment_page) |comment_view| { 804 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 805 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 806 var back = try Comment.linkBox(allocator, self.session, back_label, ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 807 errdefer back.deinit(allocator); 808 try inner_box.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 809 810 try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, self.data.viewer != null, comment_view.selected); 811 try Comment.appendCount(allocator, inner_box, comment_view.replies.count, "reply", "replies"); 812 for (comment_view.replies.comments) |comment| try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, self.data.viewer != null, comment); 813 try Comment.appendWindowNav(allocator, inner_box, self.session, self.data.identity, kind, &comment_view.selected.comment.event.thread_id, &comment_view.selected.id, comment_view.replies); 814 815 var spacer = try Spacer.init(allocator); 816 errdefer spacer.deinit(allocator); 817 try inner_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 818 819 inner_box.getFocus().child_id = inner_box.children.keys()[tool_row_index + 1]; 820 self.resetScroll(); 821 return; 822 } 823 824 if (description_page) { 825 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 826 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 827 var text_box = try wgt.TextBox.init(allocator, back_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 828 errdefer text_box.deinit(allocator); 829 text_box.getFocus().mode = .all; 830 text_box.getFocus().kind = .{ .custom = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), "", entry.id, self.data.search orelse "") }; 831 try inner_box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 832 self.title_id = text_box.getFocus().id; 833 } else { 834 var title = try wgt.TextBox.init(allocator, entry.record.event.title, .{ .border_style = .single, .round_corners = true, .wrap_kind = .word, .label = " title " }); 835 errdefer title.deinit(allocator); 836 title.getFocus().mode = .all; 837 try inner_box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_box = title }, .rect = null, .min_size = null }); 838 self.title_id = title.getFocus().id; 839 840 var author = try ui.authorBox(allocator, self.session.page_arena, entry.author); 841 errdefer author.deinit(allocator); 842 try inner_box.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 843 self.author_id = author.getFocus().id; 844 845 var items: std.ArrayList(WordFlow.Item) = .empty; 846 defer items.deinit(allocator); 847 var label_iter = Event.labelIterator(entry.record.event.labels); 848 while (label_iter.next()) |label| { 849 if (label.len == 0) continue; 850 try items.append(allocator, .{ .text = label, .link = try labelLink(self.session.page_arena, self.data.identity, entryStatus(entry), label, self.data.search orelse "") }); 851 } 852 if (items.items.len > 0) { 853 var labels = try WordFlow.init(allocator); 854 errdefer labels.deinit(allocator); 855 try labels.setItems(allocator, items.items); 856 try inner_box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .word_flow = labels }, .rect = null, .min_size = null }); 857 } 858 } 859 860 const whole = entry.record.event.description; 861 const preview_end = if (description_page) null else ui.detailPreviewEnd(whole); 862 const shown = if (preview_end) |end| 863 std.mem.trimEnd(u8, whole[0..end], " \t\r\n") 864 else if (whole.len == 0) 865 "(no description)" 866 else 867 whole; 868 var description = try Markdown.Frame.init(allocator, shown, " description ", if (preview_end != null) " click or press enter to see more " else "", self.session.page_arena); 869 errdefer description.deinit(allocator); 870 if (preview_end != null) { 871 const route = ui.RoutablePage.repoThreadDescriptionRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 872 description.body().kind = .{ .custom = try std.fmt.allocPrint(self.session.page_arena.allocator(), "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 873 } 874 try inner_box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .markdown_frame = description }, .rect = null, .min_size = null }); 875 self.description_id = description.getFocus().id; 876 877 if (!supports_drafts or !entryDraft(entry)) { 878 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 879 try Attachment.appendRows(allocator, inner_box, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 880 881 if (!description_page) { 882 if (self.data.viewer != null) { 883 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoThreadCommentNewRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong); 884 errdefer reply.deinit(allocator); 885 try inner_box.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 886 } 887 888 try Comment.appendCount(allocator, inner_box, entry.comments.count, "comment", "comments"); 889 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, self.data.viewer != null, comment); 890 try Comment.appendWindowNav(allocator, inner_box, self.session, self.data.identity, kind, entry.id, null, entry.comments); 891 } 892 } 893 894 if (!description_page) { 895 var spacer = try Spacer.init(allocator); 896 errdefer spacer.deinit(allocator); 897 try inner_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 898 } 899 900 _ = self.focusFirst(null); 901 self.resetScroll(); 902 } 903 904 fn resetScroll(self: *This) void { 905 const detail_scroll = &self.scroll; 906 detail_scroll.x = 0; 907 detail_scroll.y = 0; 908 detail_scroll.getFocus().version +%= 1; 909 } 910 911 pub fn inputWithExit(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !Exit { 912 self.exit = .none; 913 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 914 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 915 else 916 raw_key; 917 918 switch (key) { 919 .page_up => self.pageDetail(root_focus, -10), 920 .page_down => self.pageDetail(root_focus, 10), 921 .home => self.jump(root_focus, false), 922 .end => self.jump(root_focus, true), 923 // tab steps between the links of a focused description or 924 // comment; the web leaves it to the browser 925 .tab, .back_tab => if (self.session.is_terminal) if (self.focusedChild(root_focus)) |index| if (self.focusedFrame(index)) |frame| { 926 try frame.frame.stepLink(allocator, root_focus, &self.scroll, frame.x, frame.y, key == .tab); 927 }, 928 else => { 929 const entry = self.entry orelse return self.exit; 930 if (self.data.comment_page != null and std.mem.eql(u8, entry.id, self.data.selected_id)) { 931 self.commentInput(key, root_focus); 932 } else if (self.toolRowFocused(root_focus)) { 933 try self.toolRowInput(allocator, key, root_focus); 934 } else { 935 try self.childInput(allocator, key, root_focus); 936 } 937 }, 938 } 939 return self.exit; 940 } 941 942 fn childInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 943 const child_index = self.focusedChild(root_focus) orelse return; 944 const inner_box = self.inner(); 945 const child_id = inner_box.children.keys()[child_index]; 946 const child = &inner_box.children.values()[child_index].widget; 947 948 if (child_id == self.title_id) { 949 switch (key) { 950 .arrow_left => self.exit = .list, 951 .arrow_up => if (!self.moveVertical(root_focus, false)) { 952 self.exit = .header; 953 }, 954 .arrow_down => _ = self.moveVertical(root_focus, true), 955 else => {}, 956 } 957 return; 958 } 959 if (child_id == self.author_id) { 960 switch (key) { 961 .arrow_left => self.exit = .list, 962 .arrow_up => _ = self.moveVertical(root_focus, false), 963 .arrow_down => _ = self.moveVertical(root_focus, true), 964 else => {}, 965 } 966 return; 967 } 968 if (child_id == self.description_id) { 969 self.descriptionInput(key, root_focus); 970 return; 971 } 972 973 switch (child.*) { 974 .word_flow => |*labels| self.labelsInput(child_index, labels, key, root_focus), 975 .copyable_text => |*copyable| switch (key) { 976 .arrow_up => if (!self.moveVertical(root_focus, false)) { 977 self.exit = .header; 978 }, 979 .arrow_down => _ = self.moveVertical(root_focus, true), 980 .arrow_left => if (copyable.selected == 0) { 981 self.exit = .list; 982 } else try copyable.input(allocator, key, root_focus), 983 else => try copyable.input(allocator, key, root_focus), 984 }, 985 .box => |*row| if (Attachment.removeId(row)) |attachment_id| { 986 const remove_id = row.children.keys()[1]; 987 switch (key) { 988 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 989 self.exit = .list; 990 }, 991 .arrow_right => _ = self.moveHorizontal(root_focus, true), 992 .arrow_up => _ = self.moveVertical(root_focus, false), 993 .arrow_down => _ = self.moveVertical(root_focus, true), 994 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 995 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 996 else => {}, 997 } 998 } else self.commentInput(key, root_focus), 999 else => self.commentInput(key, root_focus), 1000 } 1001 } 1002 1003 fn toolRowInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1004 const row = self.toolRow(); 1005 const child_id = row.getFocus().child_id orelse return; 1006 const cur = row.children.getIndex(child_id) orelse return; 1007 const on_primary = switch (row.children.values()[cur].widget.getFocus().kind) { 1008 .custom => |action| std.mem.eql(u8, action, "submit"), 1009 else => false, 1010 }; 1011 switch (key) { 1012 .arrow_left => { 1013 var previous = cur; 1014 while (previous > 0) { 1015 previous -= 1; 1016 if (row.children.values()[previous].widget.getFocus().mode != .all) continue; 1017 root_focus.setFocus(row.children.keys()[previous]); 1018 return; 1019 } 1020 self.exit = .list; 1021 }, 1022 .arrow_right => { 1023 var next = cur + 1; 1024 while (next < row.children.count()) : (next += 1) { 1025 if (row.children.values()[next].widget.getFocus().mode != .all) continue; 1026 root_focus.setFocus(row.children.keys()[next]); 1027 return; 1028 } 1029 }, 1030 .arrow_up => self.exit = .header, 1031 .arrow_down => _ = self.moveVertical(root_focus, true), 1032 .enter => if (on_primary) try self.primaryAction(allocator), 1033 .mouse => |mouse| if (on_primary and inp.leftClickOn(root_focus, child_id, mouse)) try self.primaryAction(allocator), 1034 else => {}, 1035 } 1036 } 1037 1038 fn primaryAction(self: *This, allocator: std.mem.Allocator) !void { 1039 try self.toggleStatus(allocator); 1040 } 1041 1042 fn descriptionInput(self: *This, key: Key, root_focus: *Focus) void { 1043 switch (key) { 1044 .arrow_left => self.exit = .list, 1045 .arrow_up => _ = self.moveVertical(root_focus, false), 1046 .arrow_down => _ = self.moveVertical(root_focus, true), 1047 else => {}, 1048 } 1049 } 1050 1051 fn commentInput(self: *This, key: Key, root_focus: *Focus) void { 1052 switch (key) { 1053 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 1054 self.exit = .list; 1055 }, 1056 .arrow_right => _ = self.moveHorizontal(root_focus, true), 1057 .arrow_up => _ = self.moveVertical(root_focus, false), 1058 .arrow_down => _ = self.moveVertical(root_focus, true), 1059 else => {}, 1060 } 1061 } 1062 1063 fn pageDetail(self: *This, root_focus: *Focus, delta: isize) void { 1064 if (!self.session.is_terminal) return; 1065 const detail_scroll = &self.scroll; 1066 detail_scroll.y += delta; 1067 detail_scroll.clampToContent(); 1068 self.focusVisible(root_focus, delta > 0); 1069 } 1070 1071 fn jump(self: *This, root_focus: *Focus, to_end: bool) void { 1072 if (self.session.is_terminal) { 1073 const detail_scroll = &self.scroll; 1074 detail_scroll.y = if (to_end) std.math.maxInt(isize) else 0; 1075 detail_scroll.clampToContent(); 1076 self.focusVisible(root_focus, to_end); 1077 } else { 1078 const focus = self.inner().getFocus(); 1079 var chosen: ?usize = null; 1080 for (focus.children.keys(), focus.children.values()) |id, child| { 1081 if (child.focus.mode != .all) continue; 1082 chosen = id; 1083 if (!to_end) break; 1084 } 1085 if (chosen) |id| root_focus.setFocus(id); 1086 } 1087 } 1088 1089 fn rectVisible(self: *This, rect: layout.IRect) bool { 1090 const detail_scroll = &self.scroll; 1091 const viewport = detail_scroll.grid orelse return false; 1092 const top = detail_scroll.y; 1093 const bottom = top + @as(isize, @intCast(viewport.size.height - detail_scroll.bar_h)); 1094 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1095 } 1096 1097 fn focusVisible(self: *This, root_focus: *Focus, last: bool) void { 1098 const inner_box = self.inner(); 1099 var offset: usize = 0; 1100 while (offset < inner_box.children.count()) : (offset += 1) { 1101 const child_index = if (last) inner_box.children.count() - 1 - offset else offset; 1102 if (self.focusVisibleChild(child_index, root_focus, last)) return; 1103 } 1104 } 1105 1106 fn focusVisibleChild(self: *This, child_index: usize, root_focus: *Focus, last: bool) bool { 1107 const inner_box = self.inner(); 1108 const child = &inner_box.children.values()[child_index]; 1109 const child_rect = child.rect orelse return false; 1110 var chosen: ?usize = null; 1111 if (child.widget.getFocus().mode == .all and self.rectVisible(child_rect)) { 1112 const id = inner_box.children.keys()[child_index]; 1113 chosen = id; 1114 if (!last) { 1115 root_focus.setFocus(id); 1116 return true; 1117 } 1118 } 1119 1120 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1121 if (focus_child.focus.mode != .all) continue; 1122 const rect: layout.IRect = .{ 1123 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1124 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1125 .size = focus_child.rect.size, 1126 }; 1127 if (!self.rectVisible(rect)) continue; 1128 chosen = id; 1129 if (!last) break; 1130 } 1131 if (chosen) |id| { 1132 root_focus.setFocus(id); 1133 return true; 1134 } 1135 return false; 1136 } 1137 1138 // the markdown frame focused in child `child_index`, placed in the scroll's 1139 // content 1140 fn focusedFrame(self: *This, child_index: usize) ?struct { frame: *Markdown.Frame, x: isize, y: isize } { 1141 const child = &self.inner().children.values()[child_index]; 1142 const rect = child.rect orelse return null; 1143 switch (child.widget) { 1144 .markdown_frame => |*frame| return .{ .frame = frame, .x = rect.x, .y = rect.y }, 1145 .repo_comment => |*comment| { 1146 if (!comment.bodyFocused()) return null; 1147 const row = comment.rowRect(true) orelse return null; 1148 return .{ .frame = comment.body(), .x = rect.x + row.x, .y = rect.y + row.y }; 1149 }, 1150 else => return null, 1151 } 1152 } 1153 1154 fn focusedChild(self: *This, root_focus: *Focus) ?usize { 1155 const inner_box = self.inner(); 1156 var id = root_focus.grandchild_id orelse return null; 1157 while (inner_box.getFocus().children.get(id)) |child| { 1158 if (child.parent_id == inner_box.getFocus().id) return inner_box.children.getIndex(id); 1159 id = child.parent_id; 1160 } 1161 return null; 1162 } 1163 1164 fn childFocusable(self: *This, child_index: usize) bool { 1165 const focus = self.inner().children.values()[child_index].widget.getFocus(); 1166 if (focus.mode == .all) return true; 1167 for (focus.children.values()) |item| { 1168 if (item.focus.mode == .all) return true; 1169 } 1170 return false; 1171 } 1172 1173 // focus child `child_index`, its last part when `last`, entered from above 1174 // when `down` 1175 fn focusChild(self: *This, child_index: usize, last: bool, down: bool, root_focus: *Focus) void { 1176 const inner_box = self.inner(); 1177 const child = &inner_box.children.values()[child_index]; 1178 var target = inner_box.children.keys()[child_index]; 1179 var rect = child.rect orelse return; 1180 switch (child.widget) { 1181 .repo_comment => |*comment| { 1182 if (comment.rowRect(last)) |row_rect| { 1183 rect.x += row_rect.x; 1184 rect.y += row_rect.y; 1185 rect.size = row_rect.size; 1186 } 1187 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1188 }, 1189 .box => |*box| if (box.children.count() > 0) { 1190 target = if (child_index == tool_row_index) 1191 box.getFocus().child_id orelse return 1192 else 1193 box.children.keys()[if (last) box.children.count() - 1 else 0]; 1194 root_focus.setFocus(target); 1195 }, 1196 else => root_focus.setFocus(target), 1197 } 1198 widget.scrollToEnteredRow(&self.scroll, rect, down); 1199 } 1200 1201 fn moveVertical(self: *This, root_focus: *Focus, down: bool) bool { 1202 const inner_box = self.inner(); 1203 const current = self.focusedChild(root_focus) orelse return false; 1204 // a tall description or comment body is scrolled through before focus 1205 // leaves it 1206 if (self.focusedRect(current)) |rect| if (widget.scrollThroughRow(&self.scroll, rect, if (down) 1 else -1)) return true; 1207 if (inner_box.children.values()[current].widget == .repo_comment) { 1208 const comment = &inner_box.children.values()[current].widget.repo_comment; 1209 if (down and !comment.bodyFocused()) { 1210 self.focusChild(current, true, true, root_focus); 1211 return true; 1212 } 1213 if (!down and comment.bodyFocused()) { 1214 self.focusChild(current, false, false, root_focus); 1215 return true; 1216 } 1217 } 1218 1219 var next = current; 1220 while (true) { 1221 if (down) { 1222 next += 1; 1223 if (next >= inner_box.children.count()) return false; 1224 } else { 1225 if (next == 0) return false; 1226 next -= 1; 1227 } 1228 if (!self.childFocusable(next)) continue; 1229 self.focusChild(next, !down, down, root_focus); 1230 return true; 1231 } 1232 } 1233 1234 // the focused part of child `child_index`, in the scroll's content: a 1235 // comment's metadata or body row, else the whole child 1236 fn focusedRect(self: *This, child_index: usize) ?layout.IRect { 1237 const child = &self.inner().children.values()[child_index]; 1238 var rect = child.rect orelse return null; 1239 if (child.widget == .repo_comment) { 1240 const comment = &child.widget.repo_comment; 1241 const row = comment.rowRect(comment.bodyFocused()) orelse return null; 1242 rect.x += row.x; 1243 rect.y += row.y; 1244 rect.size = row.size; 1245 } 1246 return rect; 1247 } 1248 1249 fn moveHorizontal(self: *This, root_focus: *Focus, right: bool) bool { 1250 const inner_box = self.inner(); 1251 const current = self.focusedChild(root_focus) orelse return false; 1252 const child = &inner_box.children.values()[current]; 1253 if (child.widget == .repo_comment) { 1254 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1255 if (moved) if (child.rect) |rect| self.scroll.scrollToRect(rect); 1256 return moved; 1257 } 1258 const row = switch (child.widget) { 1259 .box => |*box| box, 1260 else => return false, 1261 }; 1262 if (!widget.moveInRow(row, root_focus, right)) return false; 1263 if (child.rect) |rect| self.scroll.scrollToRect(rect); 1264 return true; 1265 } 1266 1267 fn labelsInput(self: *This, child_index: usize, labels: *WordFlow, key: Key, root_focus: *Focus) void { 1268 const child_id = labels.focus.child_id orelse return; 1269 const cur = labels.indexOfFocusId(child_id) orelse return; 1270 const count = labels.text_boxes.items.len; 1271 switch (key) { 1272 .arrow_left => if (cur > 0) self.focusLabel(child_index, labels, root_focus, cur - 1) else { 1273 self.exit = .list; 1274 }, 1275 .arrow_right => if (cur + 1 < count) self.focusLabel(child_index, labels, root_focus, cur + 1), 1276 .arrow_up => if (labels.rowStep(cur, false)) |index| { 1277 self.focusLabel(child_index, labels, root_focus, index); 1278 } else { 1279 _ = self.moveVertical(root_focus, false); 1280 }, 1281 .arrow_down => if (labels.rowStep(cur, true)) |index| { 1282 self.focusLabel(child_index, labels, root_focus, index); 1283 } else { 1284 _ = self.moveVertical(root_focus, true); 1285 }, 1286 .home => self.focusLabel(child_index, labels, root_focus, 0), 1287 .end => self.focusLabel(child_index, labels, root_focus, count - 1), 1288 else => {}, 1289 } 1290 } 1291 1292 fn focusLabel(self: *This, child_index: usize, labels: *WordFlow, root_focus: *Focus, item: usize) void { 1293 root_focus.setFocus(labels.text_boxes.items[item].getFocus().id); 1294 if (self.session.is_terminal and item < labels.rects.items.len) { 1295 if (self.inner().children.values()[child_index].rect) |flow_rect| { 1296 var rect = labels.rects.items[item]; 1297 rect.x += flow_rect.x; 1298 rect.y += flow_rect.y; 1299 self.scroll.scrollToRect(rect); 1300 } 1301 } 1302 } 1303 1304 fn toolRowFocused(self: *This, root_focus: *Focus) bool { 1305 return self.focusedChild(root_focus) == tool_row_index; 1306 } 1307 1308 pub fn focusFirst(self: *This, root_focus: ?*Focus) bool { 1309 const inner_box = self.inner(); 1310 for (inner_box.children.values(), 0..) |*child, child_index| { 1311 switch (child.widget) { 1312 .box => |*box| { 1313 for (box.children.keys(), box.children.values()) |id, *item| { 1314 if (item.widget.getFocus().mode != .all) continue; 1315 box.getFocus().child_id = id; 1316 break; 1317 } else continue; 1318 }, 1319 else => if (!self.childFocusable(child_index)) continue, 1320 } 1321 if (root_focus) |focus| 1322 self.focusChild(child_index, false, true, focus) 1323 else 1324 inner_box.getFocus().child_id = inner_box.children.keys()[child_index]; 1325 return true; 1326 } 1327 return false; 1328 } 1329 1330 pub fn atTop(self: *This, root_focus: *Focus) bool { 1331 const focused = self.focusedChild(root_focus) orelse return false; 1332 for (0..self.inner().children.count()) |child_index| { 1333 if (!self.childFocusable(child_index)) continue; 1334 return focused == child_index; 1335 } 1336 return false; 1337 } 1338 1339 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 1340 if (comptime wasm) return; 1341 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 1342 const author = actor.author; 1343 1344 if (comptime supports_drafts) { 1345 const entry = self.entry orelse return; 1346 if (event_kind == kind and entryDraft(entry)) { 1347 try Data.removeDraft(self.session, allocator, author, &id); 1348 const route = draftsRoute(self.data.identity) orelse return; 1349 try self.session.navigate(route); 1350 return; 1351 } 1352 } 1353 1354 const io = self.session.io orelse return; 1355 const source = self.data.repo_source orelse return; 1356 if (!try source.canModify(io, allocator, actor, event_kind, &id)) return; 1357 1358 switch (source.repo_kind) { 1359 inline else => |repo_kind| { 1360 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1361 defer any_repo.deinit(io, allocator); 1362 switch (any_repo) { 1363 inline else => |*repo| try evt.remove(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 1364 } 1365 }, 1366 } 1367 1368 const id_hex = std.fmt.bytesToHex(id, .lower); 1369 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 1370 try self.session.navigate(route); 1371 } 1372 1373 fn toggleStatus(self: *This, allocator: std.mem.Allocator) !void { 1374 if (comptime (!has_status or wasm)) return; 1375 const entry = self.entry orelse return; 1376 const io = self.session.io orelse return; 1377 const source = self.data.repo_source orelse return; 1378 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 1379 const author = actor.author; 1380 const status = (statusChange(entryStatus(entry)) orelse return).status; 1381 const id = try evt.parseEventId(entry.id); 1382 if (!try source.canModify(io, allocator, actor, kind, &id)) return; 1383 1384 switch (source.repo_kind) { 1385 inline else => |repo_kind| { 1386 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1387 defer any_repo.deinit(io, allocator); 1388 switch (any_repo) { 1389 inline else => |*repo| try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id, .{ .status = status }, author), 1390 } 1391 }, 1392 } 1393 1394 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 1395 try self.session.navigate(route); 1396 } 1397 }; 1398 } 1399 1400 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 1401 const Self = Data; 1402 const HeaderType = Data.Header; 1403 const Window = Data.Window; 1404 const Entry = Data.Entry; 1405 const Event = Data.Event; 1406 const feedback_tag: std.meta.Tag(ui.Session.FormFeedback) = switch (kind) { 1407 .issue => .issue, 1408 .patch => .patch, 1409 .discuss => .discussion, 1410 else => unreachable, 1411 }; 1412 const FeedbackFailure = if (kind == .patch) ui.Session.FormFeedback.PatchFailure else ui.Session.FormFeedback.ThreadFailure; 1413 const Status = Data.Status; 1414 const has_status = @hasField(Event, "status"); 1415 const supports_conflicts = Event.merge_policy == .field_conflicts; 1416 const supports_drafts = @hasField(Self, "drafts"); 1417 const supports_merge = @hasDecl(Data, "mergeRoute"); 1418 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 1419 const ViewKind = Data.ViewKind; 1420 const thread_name = Data.thread_name; 1421 const DetailType = Data.Detail; 1422 1423 return struct { 1424 const This = @This(); 1425 // a vertical box: the header tabs on top, then a stack holding a 1426 // master-detail split (thread list + description pane) per status list, 1427 // plus the labels view. 1428 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 1429 data: *const Self, 1430 session: *ui.Session, 1431 1432 const header_index: usize = 0; 1433 const stack_index: usize = 1; 1434 // indices within the stack, 1:1 with the header tabs. 1435 const status_count = @typeInfo(Status).@"enum".fields.len; 1436 const labels_view_index: usize = status_count; 1437 // the new-thread form, or the edit, comment, or resolve form when the page was 1438 // loaded at one of their urls. 1439 const form_view_index: usize = labels_view_index + 1; 1440 // the conflicts split; the tab and stack child exist only when the repo 1441 // has conflicted threads. 1442 const drafts_view_index: usize = form_view_index + 1; 1443 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 1444 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 1445 // indices within a split (the horizontal box inside the stack). 1446 const list_index: usize = 0; 1447 const detail_index: usize = 1; 1448 const list_max_width: usize = 35; 1449 const detail_min_width: usize = 40; 1450 1451 pub fn viewIndex(view: ViewKind) usize { 1452 const name = @tagName(view); 1453 if (std.mem.eql(u8, name, "labels")) return labels_view_index; 1454 if (std.mem.eql(u8, name, "new") or std.mem.eql(u8, name, "edit") or std.mem.eql(u8, name, "publish") or std.mem.eql(u8, name, "merge") 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; 1455 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 1456 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 1457 if (std.mem.eql(u8, name, "description")) unreachable; 1458 inline for (@typeInfo(Status).@"enum".fields, 0..) |field, index| { 1459 if (std.mem.eql(u8, name, field.name)) return index; 1460 } 1461 unreachable; 1462 } 1463 1464 const Confirmation = enum { remove, publish, merge }; 1465 1466 fn confirmation(view: ViewKind) ?Confirmation { 1467 if (view == .remove) return .remove; 1468 if (supports_drafts and std.mem.eql(u8, @tagName(view), "publish")) return .publish; 1469 if (supports_merge and std.mem.eql(u8, @tagName(view), "merge")) return .merge; 1470 return null; 1471 } 1472 1473 // the status a status split lists; the conflicts split has none (its 1474 // threads carry their own). 1475 fn splitStatus(index: usize) Status { 1476 return @enumFromInt(index); 1477 } 1478 1479 fn entryConflicted(entry: Entry) bool { 1480 return if (supports_conflicts) entry.conflicted else false; 1481 } 1482 1483 fn entryStatus(entry: Entry) Status { 1484 if (comptime has_status) return Data.statusKind(entry.record.event); 1485 return @enumFromInt(0); 1486 } 1487 1488 fn entryDraft(entry: Entry) bool { 1489 return if (supports_drafts) entry.draft else false; 1490 } 1491 1492 fn listRoute(identity: []const u8, status: Status, label: []const u8, selected: []const u8) ?ui.RoutablePage { 1493 return Data.listRoute(identity, status, label, selected); 1494 } 1495 1496 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 1497 return if (supports_drafts) Data.draftsRoute(identity) else null; 1498 } 1499 1500 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1501 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 1502 } 1503 1504 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1505 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 1506 } 1507 1508 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1509 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 1510 } 1511 1512 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 1513 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 1514 } 1515 1516 // a list link keeps the filters the page carries: the label, and the 1517 // search query when one is set 1518 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, id: []const u8, search: []const u8) ![]const u8 { 1519 var route = listRoute(identity, status, label, id) orelse return error.RouteTooLong; 1520 if (search.len != 0) route = route.withSearch(search) orelse return error.RouteTooLong; 1521 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1522 } 1523 1524 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 1525 const selected = std.mem.eql(u8, id, data.selected_id); 1526 const route = (if (selected and data.description_page) 1527 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 1528 else if (selected) 1529 if (data.comment_page) |page| 1530 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 1531 else 1532 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 1533 else 1534 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 1535 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 1536 } 1537 1538 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 1539 if (supports_conflicts and status_maybe == null and !drafts) { 1540 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 1541 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1542 } 1543 if (drafts and id.len == 0) { 1544 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 1545 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1546 } 1547 return listLink(page_arena, data.identity, status_maybe orelse @enumFromInt(0), data.label, id, data.search orelse ""); 1548 } 1549 1550 fn labelLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, search: []const u8) ![]const u8 { 1551 const encoded = try ui.urlEncodeRef(page_arena.allocator(), label); 1552 return listLink(page_arena, identity, status, encoded, "", search); 1553 } 1554 1555 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 1556 var outer = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1557 errdefer outer.deinit(allocator); 1558 1559 // the tabs at the top. 1560 { 1561 var hdr = try Data.initHeader(allocator, session, data); 1562 errdefer hdr.deinit(allocator); 1563 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 1564 } 1565 1566 // the stack enters `outer` before its children enter it, so an error 1567 // frees each child exactly once. 1568 { 1569 var stack = try wgt.Stack(Widget).init(allocator); 1570 errdefer stack.deinit(allocator); 1571 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1572 } 1573 const stack = &outer.children.values()[stack_index].widget.stack; 1574 1575 inline for (@typeInfo(Status).@"enum".fields) |field| { 1576 var split = try initSplit(allocator, session, data, @enumFromInt(field.value), false); 1577 errdefer split.deinit(allocator); 1578 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1579 } 1580 1581 // the labels view 1582 { 1583 var tf = try WordFlow.init(allocator); 1584 errdefer tf.deinit(allocator); 1585 var items: std.ArrayList(WordFlow.Item) = .empty; 1586 defer items.deinit(allocator); 1587 // when filtered, the first item clears the filter 1588 if (data.label.len != 0) 1589 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @enumFromInt(0), "", "", data.search orelse "") }); 1590 for (data.labels) |label| 1591 try items.append(allocator, .{ .text = label, .link = try labelLink(session.page_arena, data.identity, @enumFromInt(0), label, data.search orelse "") }); 1592 try tf.setItems(allocator, items.items); 1593 try stack.children.put(allocator, tf.getFocus().id, .{ .word_flow = tf }); 1594 } 1595 1596 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1597 // its place, prefilled with the selected thread's content. a 1598 // logged-out session can't create events, so the unauthorized view 1599 // stands in. 1600 if (session.data.host_kind == .local or session.data.user_id != null) { 1601 if (confirmation(data.view)) |confirmation_kind| { 1602 const aa = session.page_arena.allocator(); 1603 const route = switch (confirmation_kind) { 1604 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1605 .publish => publishRoute(data.identity, data.selected_id), 1606 .merge => mergeRoute(data.identity, data.selected_id), 1607 } orelse return error.RouteTooLong; 1608 const page_url = try route.toUrl(session.page_arena); 1609 const action = try std.fmt.allocPrint(aa, "form:{s}", .{page_url}); 1610 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1611 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1612 var buttons: [2]ConfirmationButton = undefined; 1613 buttons[0] = .{ .label = switch (confirmation_kind) { 1614 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1615 .publish => "publish patch", 1616 .merge => "merge patch", 1617 } }; 1618 var button_count: usize = 1; 1619 if (confirmation_kind == .merge) { 1620 if (comptime supports_merge) { 1621 const availability = if (data.selectedThread()) |entry| 1622 if (Data.canMerge(entry.*, session)) entry.mergeability else pch.Mergeability{} 1623 else 1624 pch.Mergeability{}; 1625 buttons[0].bottom_label = mergeBottomLabel(availability.source); 1626 if (availability.source != .clean) buttons[0].action = ""; 1627 buttons[1] = .{ 1628 .label = "squash and merge patch", 1629 .bottom_label = mergeBottomLabel(availability.squash), 1630 .action = if (availability.squash == .clean) try std.fmt.allocPrint(aa, "{s}{s}/squash", .{ ui.submit_action_prefix, page_url }) else "", 1631 }; 1632 button_count += 1; 1633 } else unreachable; 1634 } 1635 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1636 errdefer center.deinit(allocator); 1637 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1638 } else if (data.view == .new_comment or data.view == .edit_comment) { 1639 const editing = data.view == .edit_comment; 1640 const route = if (editing) 1641 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1642 else 1643 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1644 const page_url = try route.toUrl(session.page_arena); 1645 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 1646 const page = data.comment_page; 1647 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1648 const parent_route = if (editing) 1649 if (top_level) 1650 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1651 else if (page) |p| 1652 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1653 else 1654 null 1655 else if (page) |p| 1656 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1657 else 1658 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 1659 const parent_author: ui.Author = if (editing) 1660 if (top_level) 1661 if (data.selectedThread()) |entry| entry.author else .unknown 1662 else if (page) |p| 1663 p.selected.parent_author orelse .unknown 1664 else 1665 .unknown 1666 else if (page) |p| 1667 p.selected.author 1668 else if (data.selectedThread()) |entry| 1669 entry.author 1670 else 1671 .unknown; 1672 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 1673 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 1674 errdefer form.deinit(allocator); 1675 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1676 } else if (supports_conflicts and data.view == .resolve) { 1677 // on the web the page grows to the form's height and the 1678 // browser scrolls it; the terminal scrolls the form itself 1679 var form_widget: Widget = blk: { 1680 var form = try initResolveForm(allocator, session, data); 1681 errdefer form.deinit(allocator); 1682 break :blk if (session.is_terminal) 1683 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 1684 else 1685 .{ .box = form }; 1686 }; 1687 errdefer form_widget.deinit(allocator); 1688 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 1689 } else { 1690 const aa = session.page_arena.allocator(); 1691 const route = if (data.view == .edit) 1692 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 1693 else 1694 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 1695 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 1696 const record = if (data.view == .edit) 1697 (if (data.selectedThread()) |entry| &entry.record else null) 1698 else 1699 null; 1700 var form = try initThreadForm(allocator, session, data, action, record); 1701 errdefer form.deinit(allocator); 1702 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1703 } 1704 } else { 1705 var message = try ui.Unauthorized.View.init(allocator); 1706 errdefer message.deinit(allocator); 1707 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 1708 } 1709 1710 if (supports_drafts) { 1711 var split = try initSplit(allocator, session, data, null, true); 1712 errdefer split.deinit(allocator); 1713 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1714 } 1715 1716 // the conflicts split; its tab exists only when the repo has 1717 // conflicts, so skip the child too to keep the stack 1:1 with the 1718 // tabs. 1719 if (supports_conflicts and data.conflicts.count > 0) { 1720 var split = try initSplit(allocator, session, data, null, false); 1721 errdefer split.deinit(allocator); 1722 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1723 } 1724 1725 // the stack starts on the page's view. 1726 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 1727 1728 // focus entering the view lands on the tabs first. 1729 outer.getFocus().child_id = outer.children.keys()[header_index]; 1730 1731 return .{ 1732 .box = outer, 1733 .data = data, 1734 .session = session, 1735 }; 1736 } 1737 1738 // the master-detail split showing `status`'s window, or the conflicts 1739 // window when null. 1740 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 1741 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1742 errdefer box.deinit(allocator); 1743 1744 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; 1745 1746 // the thread list (one focusable row per title), plus a "next" link that 1747 // reloads the page rooted at the following thread. 1748 { 1749 var list_scroll = blk: { 1750 var list_box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 1751 errdefer list_box.deinit(allocator); 1752 if (win.prev_id) |prev| 1753 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 1754 for (win.items) |entry| { 1755 const label: []const u8 = if (entry.comments.count > 0) 1756 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 1757 else 1758 ""; 1759 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 1760 } 1761 if (win.next_id) |next| 1762 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 1763 // select the window's first thread (past a leading "previous" 1764 // row) so its description shows on load. 1765 if (win.items.len > 0) 1766 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 1767 else if (list_box.children.count() > 0) 1768 list_box.getFocus().child_id = list_box.children.keys()[0]; 1769 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 1770 }; 1771 errdefer list_scroll.deinit(allocator); 1772 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 } }); 1773 } 1774 1775 // frame the detail pane inside the master-detail split 1776 { 1777 var frame = try wgt.Box(Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 1778 errdefer frame.deinit(allocator); 1779 frame.getFocus().mode = .mouse; 1780 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 1781 errdefer detail_view.deinit(allocator); 1782 frame.getFocus().child_id = detail_view.getFocus().id; 1783 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 1784 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 1785 } 1786 1787 box.getFocus().child_id = box.children.keys()[if (win.items.len > 0) detail_index else list_index]; 1788 return box; 1789 } 1790 1791 // a thread form, plus the target branch for patches, prefilled from 1792 // `record` when given. its form: subtree makes the web renderer POST 1793 // the inputs to `action`'s route. 1794 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 1795 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1796 errdefer box.deinit(allocator); 1797 box.getFocus().kind = .{ .custom = action }; 1798 1799 const saved_form = session.formFeedback(feedback_tag); 1800 const saved_fields = if (saved_form) |saved| saved.fields else null; 1801 1802 { 1803 var title = try wgt.TextInput.init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 1804 errdefer title.deinit(allocator); 1805 title.getFocus().mode = .all; 1806 if (saved_fields) |saved| 1807 try title.setContent(allocator, saved.title) 1808 else if (record) |r| 1809 try title.setContent(allocator, r.event.title); 1810 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1811 box.getFocus().child_id = title.getFocus().id; 1812 } 1813 1814 { 1815 var labels = try wgt.TextInput.init(allocator, .{ .label = " labels (separate with spaces) ", .name = "labels", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 1816 errdefer labels.deinit(allocator); 1817 labels.getFocus().mode = .all; 1818 if (saved_fields) |saved| 1819 try labels.setContent(allocator, saved.labels) 1820 else if (record) |r| 1821 try labels.setContent(allocator, r.event.labels); 1822 try box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .text_input = labels }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1823 } 1824 1825 { 1826 var description = try wgt.TextInput.init(allocator, .{ .label = " description (markdown supported) ", .name = "description", .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 1827 errdefer description.deinit(allocator); 1828 description.getFocus().mode = .all; 1829 if (saved_fields) |saved| 1830 try description.setContent(allocator, saved.description) 1831 else if (record) |r| 1832 try description.setContent(allocator, r.event.description); 1833 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 1834 } 1835 1836 if (supports_drafts and record == null) { 1837 var source = try Data.Source.init(allocator, session, if (saved_fields) |saved| saved.source_branch else "", if (saved_fields) |saved| saved.source_kind else .fork); 1838 errdefer source.deinit(allocator); 1839 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .patch_source = source }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1840 } 1841 1842 if (supports_drafts) { 1843 var target = try wgt.TextInput.init(allocator, .{ .label = " target branch ", .name = "target_branch", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 1844 errdefer target.deinit(allocator); 1845 target.getFocus().mode = .all; 1846 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 1847 try target.setContent(allocator, content); 1848 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1849 } 1850 1851 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 1852 1853 return box; 1854 } 1855 1856 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(Widget) { 1857 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1858 errdefer box.deinit(allocator); 1859 box.getFocus().kind = .{ .custom = action }; 1860 1861 { 1862 var parent = try ui.authorBox(allocator, session.page_arena, author); 1863 errdefer parent.deinit(allocator); 1864 parent.options.label = " replying to "; 1865 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 1866 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1867 box.getFocus().child_id = parent.getFocus().id; 1868 } 1869 1870 { 1871 var body = try wgt.TextInput.init(allocator, .{ 1872 .label = " comment (markdown supported) ", 1873 .name = "body", 1874 .visible_width = null, 1875 .round_corners = true, 1876 .render_content = session.is_terminal, 1877 .multiline = true, 1878 .visible_height = 5, 1879 .scroll = .{ .fill = true }, 1880 }); 1881 errdefer body.deinit(allocator); 1882 body.getFocus().mode = .all; 1883 if (initial_body) |text| try body.setContent(allocator, text); 1884 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 1885 } 1886 1887 try addSubmitButtonLabeled(allocator, &box, "submit"); 1888 return box; 1889 } 1890 1891 const ConfirmationButton = struct { 1892 label: []const u8, 1893 bottom_label: []const u8 = "", 1894 action: []const u8 = "submit", 1895 }; 1896 1897 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 1898 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .round_corners = true, .direction = .vert }); 1899 errdefer box.deinit(allocator); 1900 box.getFocus().kind = .{ .custom = action }; 1901 1902 var prompt = try wgt.Text.init(allocator, "are you sure?"); 1903 errdefer prompt.deinit(allocator); 1904 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 1905 1906 for (buttons, 0..) |options, index| { 1907 var button = try wgt.TextBox.init(allocator, options.label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = options.bottom_label }); 1908 errdefer button.deinit(allocator); 1909 button.getFocus().mode = .all; 1910 button.getFocus().kind = .{ .custom = options.action }; 1911 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 1912 if (index == 0) box.getFocus().child_id = button.getFocus().id; 1913 } 1914 1915 return Center.init(allocator, .{ .box = box }); 1916 } 1917 1918 // a form's submit button, then a spacer absorbing the leftover 1919 // min-height the box hands its last child, so the button keeps its 1920 // natural height 1921 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 1922 { 1923 var submit = try SubmitButton.initLabeled(allocator, label); 1924 errdefer submit.deinit(allocator); 1925 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1926 } 1927 { 1928 var spacer = try Spacer.init(allocator); 1929 errdefer spacer.deinit(allocator); 1930 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1931 } 1932 } 1933 1934 // the conflict resolve form. the "use this" links are navigations that 1935 // flip the url's theirs: pick, reloading the prefills; one submit 1936 // settles every conflict. the form: subtree makes the web renderer POST 1937 // to the resolve route. 1938 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 1939 const aa = session.page_arena.allocator(); 1940 // init only picks the resolve view once it has read the conflict 1941 const conflict = if (data.conflict) |*c| c else unreachable; 1942 1943 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1944 errdefer box.deinit(allocator); 1945 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 1946 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 1947 1948 if (conflict.title) |*fc| { 1949 try addLabel(allocator, &box, "title conflict:"); 1950 try addFieldConflict(allocator, &box, session, data, "title", fc); 1951 } 1952 if (conflict.labels) |*fc| { 1953 try addLabel(allocator, &box, "labels conflict:"); 1954 try addFieldConflict(allocator, &box, session, data, "labels", fc); 1955 } 1956 if (comptime @hasField(Data.Conflict, "status")) { 1957 if (conflict.status) |*fc| { 1958 try addLabel(allocator, &box, "status conflict:"); 1959 try addAtomicConflict(allocator, &box, session, data, "status", fc); 1960 } 1961 } 1962 if (comptime @hasField(Data.Conflict, "revision")) { 1963 if (conflict.revision) |*fc| { 1964 try addLabel(allocator, &box, "revision conflict:"); 1965 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 1966 } 1967 if (conflict.target_branch) |*fc| { 1968 try addLabel(allocator, &box, "target branch conflict:"); 1969 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 1970 } 1971 } 1972 1973 if (conflict.description) |*desc| { 1974 try addLabel(allocator, &box, "description conflict:"); 1975 var hunk_index: usize = 0; 1976 for (desc.chunks, 0..) |*chunk, chunk_index| { 1977 switch (chunk.*) { 1978 .same => |text| { 1979 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .word }); 1980 errdefer tb.deinit(allocator); 1981 tb.getFocus().mode = .all; 1982 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1983 }, 1984 // an auto-resolved chunk: only one side changed it, so its 1985 // version stands, shown distinctly with its author 1986 .auto => |auto| { 1987 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 1988 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 1989 const label = switch (author) { 1990 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 1991 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 1992 }; 1993 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .round_corners = true, .wrap_kind = .word, .label = label }); 1994 errdefer tb.deinit(allocator); 1995 tb.getFocus().mode = .all; 1996 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1997 }, 1998 .conflict => |hunk| { 1999 // a blank row on each side sets the group apart from 2000 // the flowing shared and auto-resolved chunks 2001 if (chunk_index > 0) try addGap(allocator, &box); 2002 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 2003 hunk_index += 1; 2004 const picked_theirs = data.theirsPicked(name); 2005 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 2006 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 2007 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true }); 2008 errdefer resolution_input.deinit(allocator); 2009 resolution_input.getFocus().mode = .all; 2010 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 2011 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2012 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 2013 }, 2014 } 2015 } 2016 } 2017 2018 try addSubmitButtonLabeled(allocator, &box, "submit"); 2019 2020 box.getFocus().child_id = box.children.keys()[0]; 2021 return box; 2022 } 2023 2024 // one conflicted scalar field: both sides' version rows, then the 2025 // resolution input prefilled from the picked side. 2026 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2027 const aa = session.page_arena.allocator(); 2028 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2029 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2030 2031 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2032 errdefer resolution_input.deinit(allocator); 2033 resolution_input.getFocus().mode = .all; 2034 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2035 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2036 } 2037 2038 // atomic fields can only pick one complete side 2039 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2040 const aa = session.page_arena.allocator(); 2041 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2042 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2043 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2044 .border_style = .single, 2045 .round_corners = true, 2046 .wrap_kind = .word, 2047 .label = " resolution ", 2048 }); 2049 errdefer selected.deinit(allocator); 2050 selected.getFocus().mode = .all; 2051 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2052 } 2053 2054 // a blank row setting a conflict group apart from its neighbors 2055 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2056 var text = try wgt.Text.init(allocator, " "); 2057 errdefer text.deinit(allocator); 2058 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2059 } 2060 2061 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2062 var label = try SectionLabel.init(allocator, content); 2063 errdefer label.deinit(allocator); 2064 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2065 } 2066 2067 // one side of a conflict: the "use this" link on the left, the version 2068 // itself as a labeled read-only box. 2069 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2070 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2071 errdefer row.deinit(allocator); 2072 2073 { 2074 const use_label = "use this"; 2075 var use = try wgt.TextBox.init(allocator, use_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 2076 errdefer use.deinit(allocator); 2077 use.getFocus().mode = .all; 2078 use.getFocus().kind = .{ .custom = link }; 2079 try row.children.put(allocator, use.getFocus().id, .{ 2080 .widget = .{ .text_box = use }, 2081 .rect = null, 2082 .min_size = .{ .width = use_label.len + 2, .height = null }, 2083 .max_size = .{ .width = use_label.len + 2, .height = null }, 2084 }); 2085 } 2086 2087 { 2088 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .round_corners = true, .wrap_kind = .word, .label = label }); 2089 errdefer tb.deinit(allocator); 2090 tb.getFocus().mode = .all; 2091 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2092 } 2093 2094 row.getFocus().child_id = row.children.keys()[0]; 2095 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2096 } 2097 2098 // the label of one side's version box, naming its author 2099 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2100 return switch (author) { 2101 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 2102 .unknown => if (ours) " current version " else " their version ", 2103 }; 2104 } 2105 2106 // the a: link that reloads the resolve page with `name` picked from ours 2107 // or theirs, reprefilling its resolution input 2108 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2109 const aa = session.page_arena.allocator(); 2110 var picks: std.ArrayList(u8) = .empty; 2111 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2112 while (it.next()) |pick| { 2113 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2114 if (picks.items.len > 0) try picks.append(aa, ','); 2115 try picks.appendSlice(aa, pick); 2116 } 2117 if (theirs) { 2118 if (picks.items.len > 0) try picks.append(aa, ','); 2119 try picks.appendSlice(aa, name); 2120 } 2121 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2122 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 2123 } 2124 2125 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2126 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 2127 errdefer row.deinit(allocator); 2128 row.getFocus().mode = .all; 2129 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2130 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2131 } 2132 2133 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2134 self.box.deinit(allocator); 2135 } 2136 2137 fn header(self: *This) *HeaderType { 2138 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2139 } 2140 2141 fn viewStack(self: *This) *wgt.Stack(Widget) { 2142 return &self.box.children.values()[stack_index].widget.stack; 2143 } 2144 2145 // `index`'s master-detail split inside the stack. 2146 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2147 return &self.viewStack().children.values()[index].box; 2148 } 2149 2150 // the labels view's flow inside the stack. 2151 fn labelsView(self: *This) *WordFlow { 2152 return &self.viewStack().children.values()[labels_view_index].word_flow; 2153 } 2154 2155 // the non-confirmation form inside the stack 2156 fn formBox(self: *This) ?*wgt.Box(Widget) { 2157 return switch (self.viewStack().children.values()[form_view_index]) { 2158 .box => |*box| box, 2159 // the resolve form sits inside a scroll on the terminal 2160 .scroll => |*scroll| &scroll.child.box, 2161 else => null, 2162 }; 2163 } 2164 2165 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2166 if (supports_conflicts and self.data.view == .resolve) return null; 2167 return switch (self.data.view) { 2168 .new_comment, .edit_comment => null, 2169 else => self.formBox(), 2170 }; 2171 } 2172 2173 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2174 for (form.children.values()) |*child| switch (child.widget) { 2175 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2176 else => {}, 2177 }; 2178 return error.MissingFormField; 2179 } 2180 2181 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2182 for (form.children.values()) |*child| { 2183 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2184 } 2185 return null; 2186 } 2187 2188 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2189 return switch (self.viewStack().children.values()[form_view_index]) { 2190 .center => |*center| switch (center.child.*) { 2191 .box => |*box| box, 2192 else => null, 2193 }, 2194 else => null, 2195 }; 2196 } 2197 2198 // the resolve form's scroll on the terminal (the web page scrolls itself) 2199 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2200 return switch (self.viewStack().children.values()[form_view_index]) { 2201 .scroll => |*scroll| scroll, 2202 else => null, 2203 }; 2204 } 2205 2206 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2207 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2208 } 2209 2210 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2211 return &self.listScroll(index).child.box; 2212 } 2213 2214 fn detail(self: *This, index: usize) *DetailType { 2215 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 2216 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 2217 } 2218 2219 fn window(self: *This, index: usize) *const Window { 2220 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2221 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2222 return self.data.window(splitStatus(index)); 2223 } 2224 2225 fn stackSelectedIndex(self: *This) ?usize { 2226 const stack = self.viewStack(); 2227 const cid = stack.getFocus().child_id orelse return null; 2228 return stack.children.getIndex(cid); 2229 } 2230 2231 // the stack's selected master-detail split (null when the labels view or the 2232 // new-thread form shows). 2233 fn selectedSplitIndex(self: *This) ?usize { 2234 const idx = self.stackSelectedIndex() orelse return null; 2235 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2236 } 2237 2238 fn detailActive(self: *This, index: usize) bool { 2239 const rb = self.resultsBox(index); 2240 const cid = rb.getFocus().child_id orelse return false; 2241 return rb.children.getIndex(cid) == detail_index; 2242 } 2243 2244 fn headerActive(self: *This) bool { 2245 const cid = self.box.getFocus().child_id orelse return false; 2246 return self.box.children.getIndex(cid) == header_index; 2247 } 2248 2249 fn labelsViewActive(self: *This) bool { 2250 if (self.headerActive()) return false; 2251 return self.stackSelectedIndex() == labels_view_index; 2252 } 2253 2254 fn formViewActive(self: *This) bool { 2255 if (self.headerActive()) return false; 2256 return self.stackSelectedIndex() == form_view_index; 2257 } 2258 2259 // the selected thread's index, or null when a window-navigation row is 2260 // selected (a leading "previous" row shifts the thread rows down by one). 2261 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2262 const lb = self.listBox(index); 2263 const cid = lb.getFocus().child_id orelse return null; 2264 const idx = lb.children.getIndex(cid) orelse return null; 2265 const win = self.window(index); 2266 const lead: usize = if (win.prev_id != null) 1 else 0; 2267 if (idx < lead or idx - lead >= win.items.len) return null; 2268 return idx - lead; 2269 } 2270 2271 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2272 self.clearGrid(); 2273 2274 // each header tab maps 1:1 to a stack child by position 2275 if (self.header().getSelectedIndex()) |index| { 2276 const stack = self.viewStack(); 2277 stack.getFocus().child_id = stack.children.keys()[index]; 2278 } 2279 2280 if (self.selectedSplitIndex()) |i| { 2281 // swap the detail pane to the selected thread when it changes. 2282 if (self.selectedThreadIndex(i)) |selected| { 2283 const entry = self.window(i).items[selected]; 2284 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2285 if (changed) { 2286 try self.detail(i).setEntry(allocator, entry); 2287 } 2288 } 2289 2290 // the selected list row shows a border (the focused TextBox 2291 // upgrades it to a double border itself); the rest stay borderless. 2292 const lb = self.listBox(i); 2293 for (lb.children.keys(), lb.children.values()) |id, *child| { 2294 switch (child.widget) { 2295 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2296 else => {}, 2297 } 2298 } 2299 2300 // cap the list at list_max_width only while the detail pane fits 2301 // beside it. the box drops the detail when the width can't hold 2302 // both minimums, so when it's that narrow we lift the cap and let 2303 // the list fill the whole width. 2304 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2305 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2306 2307 // stretch the detail pane across the rest of the width so it fills 2308 // the area rather than shrinking to its content; its scroll fills 2309 // the pane. 2310 if (constraint.max_size.width) |w| { 2311 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2312 } else { 2313 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2314 } 2315 } 2316 2317 // refresh the form inputs' entries in the session's focus-id -> input 2318 // map with this frame's addresses, so the web/wasm form handling can 2319 // find them by focus id 2320 if (self.threadForm()) |form| { 2321 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2322 (try formField(form, "title")).options.label = if (failure == .required_title) " title (required) " else " title "; 2323 if (comptime supports_drafts) { 2324 if (formWidget(form, .patch_source)) |source| { 2325 source.field().options.label = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2326 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2327 try submit.setLabel(allocator, switch (source.selectedKind()) { 2328 .fork => "submit draft", 2329 .branch => "submit patch", 2330 }); 2331 } 2332 (try formField(form, "target_branch")).options.label = 2333 if (failure == .invalid_target_branch) " target branch (invalid) " else if (self.data.view == .edit and failure == .same_branch) " target branch (same as source) " else if (self.data.view == .edit and failure == .unrelated_branches) " target branch (unrelated to source) " else if (self.data.view == .edit and failure == .invalid_source_branch) " target branch (source branch not found) " else " target branch "; 2334 } 2335 } 2336 if (self.formBox()) |form| { 2337 const inputs_arena = self.session.arena.allocator(); 2338 for (form.children.values()) |*child| switch (child.widget) { 2339 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2340 else => {}, 2341 }; 2342 } 2343 2344 try self.box.build(allocator, constraint, root_focus); 2345 } 2346 2347 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2348 // wheel input moves focus like the arrows 2349 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2350 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2351 else 2352 raw_key; 2353 2354 if (self.headerActive()) { 2355 // down from the sub-header re-enters the stack if the selected 2356 // view has something to focus; enter in the search box submits 2357 // the query; other keys move within the sub-header. 2358 if (inp.vertDirection(key) == .down) { 2359 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2360 .word_flow => |*tf| tf.text_boxes.items.len > 0, 2361 // nothing focusable in the unauthorized view 2362 .unauthorized => false, 2363 else => true, 2364 } else false; 2365 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2366 } else if (key == .enter) { 2367 if (try self.header().submittedText(allocator)) |text| { 2368 defer allocator.free(text); 2369 try self.submitSearch(text); 2370 } 2371 } else { 2372 try self.header().input(allocator, key, root_focus); 2373 } 2374 return; 2375 } 2376 if (self.labelsViewActive()) { 2377 self.labelsViewInput(key, root_focus); 2378 return; 2379 } 2380 if (self.formViewActive()) { 2381 if (confirmation(self.data.view) != null) { 2382 try self.confirmationFormInput(allocator, key, root_focus); 2383 } else if (supports_conflicts and self.data.view == .resolve) { 2384 try self.resolveInput(allocator, key, root_focus); 2385 } else { 2386 try self.formInput(allocator, key, root_focus); 2387 } 2388 return; 2389 } 2390 const i = self.selectedSplitIndex() orelse return; 2391 if (self.detailActive(i)) { 2392 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2393 .none => {}, 2394 .list => self.focusList(i, root_focus), 2395 .header => self.focusHeader(root_focus), 2396 } 2397 } else { 2398 self.listInput(i, key, root_focus); 2399 } 2400 } 2401 2402 // navigate to the typed query's results, keeping the label filter and the 2403 // status list the page shows. an empty box clears a filtered page and 2404 // does nothing on any other one. 2405 fn submitSearch(self: *This, text: []const u8) !void { 2406 if (text.len == 0 and self.data.search == null) return; 2407 const selected = self.header().getSelectedIndex() orelse 0; 2408 const status: Status = if (selected < status_count) splitStatus(selected) else @enumFromInt(0); 2409 const route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2410 try self.session.navigate(route.withSearch(text) orelse return); 2411 } 2412 2413 // arrow keys move the label selection; up from the top row crosses to the 2414 // header tabs. 2415 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2416 const tf = self.labelsView(); 2417 const cid = tf.focus.child_id orelse return; 2418 const cur = tf.indexOfFocusId(cid) orelse return; 2419 const count = tf.text_boxes.items.len; 2420 switch (key) { 2421 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2422 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2423 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2424 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2425 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2426 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2427 else => {}, 2428 } 2429 } 2430 2431 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2432 // up/down (and the scroll wheel) move the selection a row; page up/down 2433 // jump a fixed amount. right/Enter cross into the detail pane. up from 2434 // the top row crosses into the header tabs. 2435 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2436 const lb = self.listBox(index); 2437 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2438 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2439 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2440 return; 2441 } 2442 switch (key) { 2443 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2444 else => {}, 2445 } 2446 } 2447 2448 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2449 const form = self.formBox() orelse return; 2450 const cid = form.getFocus().child_id orelse return; 2451 const cur = form.children.getIndex(cid) orelse return; 2452 const child = &form.children.values()[cur]; 2453 2454 if (child.widget == .text_input) { 2455 const text_input = &child.widget.text_input; 2456 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2457 return child.widget.input(allocator, key, root_focus); 2458 } 2459 2460 const on_submit = child.widget == .submit_button; 2461 switch (key) { 2462 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2463 root_focus.setFocus(form.children.keys()[i]) 2464 else 2465 self.focusHeader(root_focus), 2466 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2467 .enter => if (on_submit) return self.submitForm(allocator), 2468 .mouse => |mouse| if (on_submit) { 2469 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2470 }, 2471 else => {}, 2472 } 2473 try child.widget.input(allocator, key, root_focus); 2474 } 2475 2476 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2477 const form = self.confirmationForm() orelse return; 2478 const child_id = form.getFocus().child_id orelse return; 2479 const index = form.children.getIndex(child_id) orelse return; 2480 switch (key) { 2481 .arrow_up, .back_tab => if (index > 1) 2482 root_focus.setFocus(form.children.keys()[index - 1]) 2483 else 2484 self.focusHeader(root_focus), 2485 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2486 .enter => try self.submitConfirmation(allocator, index), 2487 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2488 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2489 try self.submitConfirmation(allocator, button_index); 2490 break; 2491 } 2492 }, 2493 else => {}, 2494 } 2495 } 2496 2497 // the multiline hunk inputs keep enter and any up/down that has a row to 2498 // move to; enter on a "use this" link is a navigation the host follows 2499 // before this runs. 2500 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2501 const form = self.formBox() orelse return; 2502 const cid = form.getFocus().child_id orelse return; 2503 const cur = form.children.getIndex(cid) orelse return; 2504 const child = &form.children.values()[cur]; 2505 2506 if (child.widget == .text_input) { 2507 const ti = &child.widget.text_input; 2508 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2509 return child.widget.input(allocator, key, root_focus); 2510 } 2511 2512 const on_submit = child.widget == .submit_button; 2513 2514 switch (key) { 2515 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2516 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2517 .arrow_left, .arrow_right => switch (child.widget) { 2518 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2519 else => try child.widget.input(allocator, key, root_focus), 2520 }, 2521 .enter => if (on_submit) try self.submitResolution(allocator), 2522 .mouse => |mouse| if (on_submit) { 2523 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2524 }, 2525 else => try child.widget.input(allocator, key, root_focus), 2526 } 2527 } 2528 2529 // a multiline input keeps enter and any up/down that has a row to move to 2530 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2531 return switch (key) { 2532 .enter => true, 2533 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2534 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2535 else => false, 2536 }; 2537 } 2538 2539 // the neighboring form control, skipping spacers and blank rows 2540 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2541 var i = cur; 2542 while (true) { 2543 if (down) { 2544 i += 1; 2545 if (i >= form.children.count()) return null; 2546 } else { 2547 if (i == 0) return null; 2548 i -= 1; 2549 } 2550 const child = &form.children.values()[i]; 2551 if (child.hidden) continue; 2552 switch (child.widget) { 2553 .spacer, .text => continue, 2554 else => return i, 2555 } 2556 } 2557 } 2558 2559 // focus the resolve-form child at `i` (a version row focuses its selected 2560 // child) and keep it visible on the terminal 2561 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2562 const child = &form.children.values()[i]; 2563 switch (child.widget) { 2564 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2565 else => root_focus.setFocus(form.children.keys()[i]), 2566 } 2567 if (self.session.is_terminal) { 2568 if (self.resolveScroll()) |sc| { 2569 if (child.rect) |rect| sc.scrollToRect(rect); 2570 } 2571 } 2572 } 2573 2574 // re-emit the conflicted thread's event with the resolved content; any 2575 // event on the thread settles its conflict entry. this is the terminal 2576 // path; the web posts the form to the resolve route. 2577 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2578 if (comptime wasm) return; 2579 const io = self.session.io orelse return; 2580 const src = self.data.repo_source orelse return; 2581 const entry = self.data.selectedThread() orelse return; 2582 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2583 const form = self.formBox() orelse return; 2584 2585 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2586 var title: ?[]u8 = null; 2587 var labels: ?[]u8 = null; 2588 var hunks: std.ArrayList([]const u8) = .empty; 2589 defer { 2590 if (title) |t| allocator.free(t); 2591 if (labels) |t| allocator.free(t); 2592 for (hunks.items) |h| allocator.free(h); 2593 hunks.deinit(allocator); 2594 } 2595 for (form.children.values()) |*child| switch (child.widget) { 2596 .text_input => |*ti| { 2597 const text = try ti.text(allocator); 2598 errdefer allocator.free(text); 2599 if (std.mem.eql(u8, ti.options.name, "title")) { 2600 title = text; 2601 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2602 labels = text; 2603 } else { 2604 try hunks.append(allocator, text); 2605 } 2606 }, 2607 else => {}, 2608 }; 2609 2610 const id_bytes = try evt.parseEventId(entry.id); 2611 2612 switch (src.repo_kind) { 2613 inline else => |repo_kind| { 2614 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2615 defer any_repo.deinit(io, allocator); 2616 switch (any_repo) { 2617 inline else => |*repo| { 2618 var resolution: Event.Resolve = .{ 2619 .title = title, 2620 .labels = labels, 2621 .hunks = hunks.items, 2622 }; 2623 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2624 const change = Event.Update{ .resolve = resolution }; 2625 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2626 // leave the form up for correction 2627 error.InvalidFields => return, 2628 else => |e| return e, 2629 }; 2630 }, 2631 } 2632 }, 2633 } 2634 2635 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2636 try self.session.navigate(route); 2637 } 2638 2639 // commit a new or edited comment and navigate to its permalink. this is the 2640 // terminal path; the web posts the form to its comment route. 2641 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2642 if (comptime wasm) return; 2643 const io = self.session.io orelse return; 2644 const src = self.data.repo_source orelse return; 2645 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2646 const author = actor.author; 2647 const form = self.formBox() orelse return; 2648 const body_input = try formField(form, "body"); 2649 const body = try body_input.text(allocator); 2650 defer allocator.free(body); 2651 if (!evt.Comment.fieldsValid(body)) return; 2652 2653 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2654 @memcpy(&thread_id, self.data.selected_id); 2655 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2656 var parent: [evt.event_id_size * 2]u8 = undefined; 2657 @memcpy(&parent, self.data.comment_id); 2658 break :blk parent; 2659 }; 2660 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2661 const editing = self.data.view == .edit_comment; 2662 if (editing) { 2663 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2664 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 2665 } 2666 switch (src.repo_kind) { 2667 inline else => |repo_kind| { 2668 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2669 defer any_repo.deinit(io, allocator); 2670 switch (any_repo) { 2671 inline else => |*repo| if (editing) { 2672 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2673 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2674 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2675 } else { 2676 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2677 }, 2678 } 2679 }, 2680 } 2681 2682 body_input.clear(allocator); 2683 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2684 try self.session.navigate(route); 2685 } 2686 2687 // terminal form submissions; the web posts to the forms' routes instead 2688 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2689 switch (self.data.view) { 2690 .new_comment, .edit_comment => try self.submitComment(allocator), 2691 .edit => try self.submitEditedThread(allocator), 2692 else => try self.submitNewThread(allocator), 2693 } 2694 } 2695 2696 // commit the new thread to the repo's events branch and navigate to it. 2697 // this is the terminal path; the web posts the form to the thread route, 2698 // so the wasm side never runs (or compiles) the repo access below. 2699 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2700 if (comptime wasm) return; 2701 const io = self.session.io orelse return; 2702 const src = self.data.repo_source orelse return; 2703 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2704 2705 const form = self.threadForm() orelse return; 2706 const title_input = try formField(form, "title"); 2707 const labels_input = try formField(form, "labels"); 2708 const description_input = try formField(form, "description"); 2709 2710 const title = try title_input.text(allocator); 2711 defer allocator.free(title); 2712 const labels = try labels_input.text(allocator); 2713 defer allocator.free(labels); 2714 const description = try description_input.text(allocator); 2715 defer allocator.free(description); 2716 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2717 defer if (comptime supports_drafts) allocator.free(target_branch); 2718 2719 if (!Event.fieldsValid(title, labels)) { 2720 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2721 return; 2722 } 2723 2724 if (comptime supports_drafts) { 2725 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2726 const source_branch = switch (source.selectedKind()) { 2727 .fork => null, 2728 .branch => try source.field().text(allocator), 2729 }; 2730 defer if (source_branch) |branch| allocator.free(branch); 2731 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 2732 const failure = FeedbackFailure.fromError(err) orelse return err; 2733 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2734 return; 2735 }; 2736 title_input.clear(allocator); 2737 labels_input.clear(allocator); 2738 description_input.clear(allocator); 2739 (try formField(form, "target_branch")).clear(allocator); 2740 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2741 try self.session.navigate(route); 2742 return; 2743 } 2744 2745 var id_bytes: [evt.event_id_size]u8 = undefined; 2746 io.random(&id_bytes); 2747 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2748 2749 const event = evt.EventWithId{ 2750 .id = event_id_hex, 2751 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2752 .author = actor.author, 2753 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2754 .title = title, 2755 .description = description, 2756 .labels = labels, 2757 }), 2758 }; 2759 2760 switch (src.repo_kind) { 2761 inline else => |repo_kind| { 2762 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2763 defer any_repo.deinit(io, allocator); 2764 switch (any_repo) { 2765 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2766 } 2767 }, 2768 } 2769 2770 // wipe the form so a return visit starts fresh 2771 title_input.clear(allocator); 2772 labels_input.clear(allocator); 2773 description_input.clear(allocator); 2774 2775 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2776 try self.session.navigate(route); 2777 } 2778 2779 // re-emit the selected thread's event with the form's content (status 2780 // preserved), then reload the thread's page. this is the terminal path; 2781 // the web posts the form to the edit route. 2782 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2783 if (comptime wasm) return; 2784 const io = self.session.io orelse return; 2785 const entry = self.data.selectedThread() orelse return; 2786 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2787 const author = actor.author; 2788 2789 const form = self.threadForm() orelse return; 2790 const title_input = try formField(form, "title"); 2791 const labels_input = try formField(form, "labels"); 2792 const description_input = try formField(form, "description"); 2793 2794 const title = try title_input.text(allocator); 2795 defer allocator.free(title); 2796 const labels = try labels_input.text(allocator); 2797 defer allocator.free(labels); 2798 const description = try description_input.text(allocator); 2799 defer allocator.free(description); 2800 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2801 defer if (comptime supports_drafts) allocator.free(target_branch); 2802 2803 if (!Event.fieldsValid(title, labels)) { 2804 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2805 return; 2806 } 2807 const src = self.data.repo_source orelse return; 2808 2809 if (comptime supports_drafts) { 2810 if (entryDraft(entry.*)) { 2811 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 2812 const failure = FeedbackFailure.fromError(err) orelse return err; 2813 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2814 return; 2815 }; 2816 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2817 try self.session.navigate(route); 2818 return; 2819 } 2820 } 2821 2822 const id_bytes = try evt.parseEventId(entry.id); 2823 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2824 2825 switch (src.repo_kind) { 2826 inline else => |repo_kind| { 2827 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2828 defer any_repo.deinit(io, allocator); 2829 switch (any_repo) { 2830 inline else => |*repo| if (supports_drafts) { 2831 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2832 .title = title, 2833 .labels = labels, 2834 .description = description, 2835 .target_branch = target_branch, 2836 } }, author) catch |err| { 2837 const failure = FeedbackFailure.fromError(err) orelse return err; 2838 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2839 return; 2840 }; 2841 } else if (has_status) 2842 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2843 .title = title, 2844 .labels = labels, 2845 .description = description, 2846 } }, author) 2847 else 2848 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 2849 } 2850 }, 2851 } 2852 2853 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2854 try self.session.navigate(route); 2855 } 2856 2857 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 2858 const aa = self.session.arena.allocator(); 2859 const source = if (supports_drafts) blk: { 2860 const form = self.threadForm() orelse break :blk null; 2861 break :blk formWidget(form, .patch_source); 2862 } else null; 2863 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2864 .failure = failure, 2865 .fields = if (comptime supports_drafts) .{ 2866 .title = try aa.dupe(u8, title), 2867 .labels = try aa.dupe(u8, labels), 2868 .description = try aa.dupe(u8, description), 2869 .target_branch = try aa.dupe(u8, target_branch), 2870 .source_branch = if (source) |value| try value.field().text(aa) else "", 2871 .source_kind = if (source) |value| value.selectedKind() else .fork, 2872 } else .{ 2873 .title = try aa.dupe(u8, title), 2874 .labels = try aa.dupe(u8, labels), 2875 .description = try aa.dupe(u8, description), 2876 }, 2877 }); 2878 } 2879 2880 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2881 switch (confirmation(self.data.view) orelse return) { 2882 .publish => if (comptime supports_drafts) { 2883 if (comptime wasm) return; 2884 const entry = self.data.selectedThread() orelse return; 2885 if (!entryDraft(entry.*)) return; 2886 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2887 try self.data.publishDraft(self.session, allocator, author, entry.id); 2888 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2889 try self.session.navigate(route); 2890 } else unreachable, 2891 .merge => if (comptime supports_merge) { 2892 if (comptime wasm) return; 2893 const entry = self.data.selectedThread() orelse return; 2894 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2895 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2896 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2897 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2898 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2899 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2900 try self.session.navigate(route); 2901 return; 2902 }, 2903 else => return err, 2904 }; 2905 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2906 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2907 try self.session.navigate(route); 2908 } else unreachable, 2909 .remove => try self.submitRemove(allocator), 2910 } 2911 } 2912 2913 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2914 const comment = self.data.comment_id.len != 0; 2915 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2916 const id = evt.parseEventId(event_id) catch return; 2917 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2918 } 2919 2920 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2921 if (comptime wasm) return; 2922 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2923 const author = actor.author; 2924 2925 if (comptime supports_drafts) { 2926 const selected = self.data.selectedThread() orelse return; 2927 if (event_kind == kind and entryDraft(selected.*)) { 2928 try Data.removeDraft(self.session, allocator, author, &id); 2929 const route = draftsRoute(self.data.identity) orelse return; 2930 try self.session.navigate(route); 2931 return; 2932 } 2933 } 2934 2935 const io = self.session.io orelse return; 2936 const src = self.data.repo_source orelse return; 2937 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2938 2939 switch (src.repo_kind) { 2940 inline else => |repo_kind| { 2941 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2942 defer any_repo.deinit(io, allocator); 2943 switch (any_repo) { 2944 inline else => |*repo| try evt.remove(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2945 } 2946 }, 2947 } 2948 2949 const id_hex = std.fmt.bytesToHex(id, .lower); 2950 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2951 try self.session.navigate(route); 2952 } 2953 2954 // return to the list. 2955 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2956 root_focus.setFocus(self.listScroll(index).getFocus().id); 2957 } 2958 2959 // cross to the header tabs above the stack. 2960 fn focusHeader(self: *This, root_focus: *Focus) void { 2961 root_focus.setFocus(self.box.children.keys()[header_index]); 2962 } 2963 2964 pub fn clearGrid(self: *This) void { 2965 self.box.clearGrid(); 2966 } 2967 2968 pub fn getGrid(self: This) ?Grid { 2969 return self.box.getGrid(); 2970 } 2971 2972 pub fn getFocus(self: *This) *Focus { 2973 return self.box.getFocus(); 2974 } 2975 2976 // for the parent's "scroll up at the top jumps to the header" check: at the 2977 // top only while the header tabs hold focus, so up from the split 2978 // crosses into the tabs first. 2979 pub fn atTop(self: *This) bool { 2980 return self.headerActive(); 2981 } 2982 }; 2983 } 2984