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