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