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