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