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