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