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