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