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