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