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