╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
493 if (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_field_to_oid_key))) |cursor| { 494 their_field_oids = try DB.SortedMap(.read_only).init(cursor); 495 } 496 var our_field_oids: ?DB.SortedMap(.read_only) = null; 497 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.id_to_field_to_oid_key))) |map_cursor| { 498 const id_to_field_to_oid = try DB.HashMap(.read_only).init(map_cursor); 499 if (try id_to_field_to_oid.getCursor(hash.hashInt(repo_opts.hash, id_bytes))) |cursor| { 500 our_field_oids = try DB.SortedMap(.read_only).init(cursor); 501 } 502 } 503 504 var conflict = Conflict{}; 505 var field_iter = std.mem.splitScalar(u8, conflicted_fields, ' '); 506 while (field_iter.next()) |field| { 507 // status can't conflict (two changes of a two-value field agree) 508 const known = std.meta.stringToEnum(enum { title, tags, description }, field) orelse continue; 509 const our_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, our_field_oids, field)); 510 const their_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, their_field_oids, field)); 511 switch (known) { 512 .title => conflict.title = .{ 513 .ours = .{ .text = ours.event.title, .author = our_author }, 514 .theirs = .{ .text = theirs.event.title, .author = their_author }, 515 }, 516 .tags => conflict.tags = .{ 517 .ours = .{ .text = ours.event.tags, .author = our_author }, 518 .theirs = .{ .text = theirs.event.tags, .author = their_author }, 519 }, 520 .description => conflict.description = .{ 521 .chunks = try diff3.chunks(io, gpa, arena, if (base) |b| b.event.description else "", ours.event.description, theirs.event.description), 522 .ours_author = our_author, 523 .theirs_author = their_author, 524 }, 525 } 526 } 527 return conflict; 528 } 529 530 // the oid `field` maps to in a field->oid map, or null 531 fn readFieldOid( 532 comptime hash_kind: hash.HashKind, 533 field_oids_maybe: ?evt.EventDB(hash_kind).SortedMap(.read_only), 534 field: []const u8, 535 ) !?[hash.byteLen(hash_kind)]u8 { 536 const field_oids = field_oids_maybe orelse return null; 537 const cursor = (try field_oids.getCursor(field)) orelse return null; 538 var oid: [hash.byteLen(hash_kind)]u8 = undefined; 539 _ = try cursor.readBytes(&oid); 540 return oid; 541 } 542 543 // the author of the event commit `oid_maybe` names 544 fn oidAuthor( 545 comptime repo_kind: rp.RepoKind, 546 comptime repo_opts: rp.RepoOpts(repo_kind), 547 arena: *std.heap.ArenaAllocator, 548 repo: *rp.Repo(repo_kind, repo_opts), 549 io: std.Io, 550 admin_moment: ?evt.AdminDB.HashMap(.read_only), 551 oid_maybe: ?[hash.byteLen(repo_opts.hash)]u8, 552 ) !ui.Author { 553 const oid = oid_maybe orelse return .unknown; 554 const gpa = arena.child_allocator; 555 var start_oids = [_][hash.hexLen(repo_opts.hash)]u8{std.fmt.bytesToHex(oid, .lower)}; 556 var commit_iter = repo.log(io, gpa, start_oids[0..1]) catch return .unknown; 557 defer commit_iter.deinit(); 558 const commit_object = (commit_iter.next(gpa) catch return .unknown) orelse return .unknown; 559 defer commit_object.deinit(); 560 return try ui.Author.init(admin_moment, arena, commit_object.content.commit.metadata.author orelse ""); 561 } 562 563 pub const View = struct { 564 // a vertical box: the header tabs on top, then a stack holding a 565 // master-detail split (issue list + description pane) per status list, 566 // plus the tags view. 567 box: wgt.Box(ui.Widget), // vert: [header_index] = tabs, [stack_index] = stack 568 data: *const Self, 569 session: *ui.Session, 570 // per-split state, indexed like the stack's split children: the issue the 571 // pane shows, and its description, author and open/close button focus ids. 572 detailed_index: [stack_child_max]?usize, 573 description_id: [stack_child_max]?usize, 574 author_id: [stack_child_max]?usize, 575 status_button_id: [stack_child_max]?usize, 576 remove_button_id: [stack_child_max]?usize, 577 578 const header_index: usize = 0; 579 const stack_index: usize = 1; 580 // indices within the stack, 1:1 with the header tabs. 581 const open_view_index: usize = 0; 582 const closed_view_index: usize = 1; 583 const tags_view_index: usize = 2; 584 // the new-issue form, or the edit, comment, or resolve form when the page was 585 // loaded at one of their urls. 586 const form_view_index: usize = 3; 587 // the conflicts split; the tab and stack child exist only when the repo 588 // has conflicted issues. 589 const conflicts_view_index: usize = 4; 590 const stack_child_max: usize = conflicts_view_index + 1; 591 // the leading stack children that are status splits; the conflicts split 592 // sits apart at conflicts_view_index. 593 const split_count: usize = 2; 594 // indices within a split (the horizontal box inside the stack). 595 const list_index: usize = 0; 596 const detail_index: usize = 1; 597 const list_max_width: usize = 40; 598 const detail_min_width: usize = 40; 599 // indices within the issue form. 600 const title_field_index: usize = 0; 601 const tags_field_index: usize = 1; 602 const description_field_index: usize = 2; 603 const submit_field_index: usize = 3; 604 const comment_author_field_index: usize = 0; 605 const comment_body_field_index: usize = 1; 606 const comment_submit_field_index: usize = 2; 607 608 fn viewIndex(view: ui.RoutablePage.IssuesView) usize { 609 return switch (view) { 610 .open => open_view_index, 611 .closed => closed_view_index, 612 .tags => tags_view_index, 613 .new, .edit, .new_comment, .edit_comment, .resolve => form_view_index, 614 .conflicts => conflicts_view_index, 615 // init resolves a description url to its issue's status list 616 .description => unreachable, 617 }; 618 } 619 620 // the status a status split lists; the conflicts split has none (its 621 // issues carry their own). 622 fn splitStatus(index: usize) evt.Issue.Status { 623 return if (index == open_view_index) .open else .closed; 624 } 625 626 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 627 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 628 errdefer outer.deinit(allocator); 629 630 // the tabs at the top. 631 { 632 var hdr = try Header.init(allocator, session, data); 633 errdefer hdr.deinit(allocator); 634 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = .{ .repo_issues_header = hdr }, .rect = null, .min_size = null }); 635 } 636 637 // the stack enters `outer` before its children enter it, so an error 638 // frees each child exactly once. 639 { 640 var stack = try wgt.Stack(ui.Widget).init(allocator); 641 errdefer stack.deinit(allocator); 642 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 643 } 644 const stack = &outer.children.values()[stack_index].widget.stack; 645 646 // a master-detail split per status list. 647 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 648 var split = try initSplit(allocator, session, data, status); 649 errdefer split.deinit(allocator); 650 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 651 } 652 653 // the tags view 654 { 655 var tf = try ui.TagFlow.init(allocator); 656 errdefer tf.deinit(allocator); 657 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 658 defer items.deinit(allocator); 659 // when filtered, the first item clears the filter 660 if (data.tag.len != 0) 661 try items.append(allocator, .{ .text = "✕", .link = try issuesLink(session.page_arena, data.identity, .open, "", "") }); 662 for (data.tags) |tag| 663 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, .open, tag) }); 664 try tf.setItems(allocator, items.items); 665 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 666 } 667 668 // the new-issue form, or — on an edit, comment, or resolve url — that form in 669 // its place, prefilled with the selected issue's content. a 670 // logged-out session can't create events, so the unauthorized view 671 // stands in. 672 if (session.data.is_local or session.data.user_id != null) { 673 if (data.view == .new_comment or data.view == .edit_comment) { 674 const editing = data.view == .edit_comment; 675 const route = if (editing) 676 ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 677 else 678 ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 679 const page_url = try route.toUrl(session.page_arena); 680 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 681 const page = data.comment_page; 682 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 683 const parent_route = if (editing) 684 if (top_level) 685 ui.RoutablePage.repoIssueCommentsRoute(data.identity, data.selected_id, 0) 686 else if (page) |p| 687 ui.RoutablePage.repoCommentsRoute(data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 688 else 689 null 690 else if (page) |p| 691 ui.RoutablePage.repoCommentsRoute(data.identity, data.selected_id, &p.selected.id, 0) 692 else 693 ui.RoutablePage.repoIssueCommentsRoute(data.identity, data.selected_id, 0); 694 const parent_author: ui.Author = if (editing) 695 if (top_level) 696 if (data.selectedIssue()) |entry| entry.author else .unknown 697 else if (page) |p| 698 p.selected.parent_author orelse .unknown 699 else 700 .unknown 701 else if (page) |p| 702 p.selected.author 703 else if (data.selectedIssue()) |entry| 704 entry.author 705 else 706 .unknown; 707 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 708 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 709 errdefer form.deinit(allocator); 710 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 711 } else if (data.view == .resolve) { 712 // on the web the page grows to the form's height and the 713 // browser scrolls it; the terminal scrolls the form itself 714 var form_widget: ui.Widget = blk: { 715 var form = try initResolveForm(allocator, session, data); 716 errdefer form.deinit(allocator); 717 break :blk if (session.is_terminal) 718 .{ .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 719 else 720 .{ .box = form }; 721 }; 722 errdefer form_widget.deinit(allocator); 723 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 724 } else { 725 const aa = session.page_arena.allocator(); 726 const route = if (data.view == .edit) 727 ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong 728 else 729 ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 730 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 731 const issue = if (data.view == .edit) 732 (if (data.selectedIssue()) |entry| &entry.issue else null) 733 else 734 null; 735 var form = try initIssueForm(allocator, session, action, issue); 736 errdefer form.deinit(allocator); 737 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 738 } 739 } else { 740 var message = try ui.Unauthorized.View.init(allocator); 741 errdefer message.deinit(allocator); 742 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 743 } 744 745 // the conflicts split; its tab exists only when the repo has 746 // conflicts, so skip the child too to keep the stack 1:1 with the 747 // tabs. 748 if (data.conflicts.count > 0) { 749 var split = try initSplit(allocator, session, data, null); 750 errdefer split.deinit(allocator); 751 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 752 } 753 754 // the stack starts on the page's view. 755 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 756 757 // focus entering the view lands on the tabs first. 758 outer.getFocus().child_id = outer.children.keys()[header_index]; 759 760 return .{ 761 .box = outer, 762 .data = data, 763 .session = session, 764 .detailed_index = @splat(null), 765 .description_id = @splat(null), 766 .author_id = @splat(null), 767 .status_button_id = @splat(null), 768 .remove_button_id = @splat(null), 769 }; 770 } 771 772 // the master-detail split showing `status`'s window, or the conflicts 773 // window when null. 774 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?evt.Issue.Status) !wgt.Box(ui.Widget) { 775 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 776 errdefer box.deinit(allocator); 777 778 const win = if (status_maybe) |status| data.window(status) else &data.conflicts; 779 780 // the issue list (one focusable row per title), plus a "next" link that 781 // reloads the page rooted at the following issue. 782 { 783 var list_scroll = blk: { 784 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 785 errdefer list_box.deinit(allocator); 786 if (win.prev_id) |prev| 787 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, prev)); 788 for (win.issues) |entry| { 789 // conflicts take precedence over the comment count 790 const label: []const u8 = if (entry.conflicted and status_maybe != null) 791 " conflict " 792 else if (entry.comments.count > 0) 793 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 794 else 795 ""; 796 try addRow(allocator, &list_box, entry.issue.event.title, label, try issueRowLink(session.page_arena, data.identity, entry.id)); 797 } 798 if (win.next_id) |next| 799 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, next)); 800 // select the window's first issue (past a leading "previous" 801 // row) so its description shows on load. 802 if (win.issues.len > 0) 803 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 804 else if (list_box.children.count() > 0) 805 list_box.getFocus().child_id = list_box.children.keys()[0]; 806 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 807 }; 808 errdefer list_scroll.deinit(allocator); 809 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 } }); 810 } 811 812 // the detail pane — a frame around a scroll of the description 813 { 814 var detail_outer = blk: { 815 var detail_scroll = blk2: { 816 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 817 errdefer detail_inner.deinit(allocator); 818 // fill the pane (content top-left, scroll bar pinned to the 819 // edge) rather than shrinking to the description. 820 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 821 }; 822 errdefer detail_scroll.deinit(allocator); 823 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 824 errdefer frame.deinit(allocator); 825 // the tool row sits above the scroll (populateDetail fills 826 // it per issue) so it can't scroll out from under the web 827 // overlay <form>, whose position doesn't track pane scrolling. 828 { 829 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 830 errdefer row.deinit(allocator); 831 try frame.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 832 } 833 // the frame's selected child is its scroll, so the focus chain 834 // reaches the description (populateDetail points the scroll's 835 // inner box at it), letting focus recovery descend into the pane. 836 frame.getFocus().child_id = detail_scroll.getFocus().id; 837 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 838 break :blk frame; 839 }; 840 errdefer detail_outer.deinit(allocator); 841 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 842 } 843 844 box.getFocus().child_id = box.children.keys()[list_index]; 845 return box; 846 } 847 848 // an issue form: title/tags/description inputs and a submit button, 849 // prefilled from `issue` when given. its form: subtree makes the web 850 // overlay wrap them in a <form> POSTing to `action`'s route. 851 fn initIssueForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, issue: ?*const evt.Issue.Record) !wgt.Box(ui.Widget) { 852 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 853 errdefer box.deinit(allocator); 854 box.getFocus().kind = .{ .custom = action }; 855 856 { 857 var title = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 858 errdefer title.deinit(allocator); 859 title.getFocus().focusable = true; 860 if (issue) |i| try title.setContent(allocator, i.event.title); 861 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 862 } 863 864 { 865 var tags = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 866 errdefer tags.deinit(allocator); 867 tags.getFocus().focusable = true; 868 if (issue) |i| try tags.setContent(allocator, i.event.tags); 869 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 870 } 871 872 { 873 var description = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 874 errdefer description.deinit(allocator); 875 description.getFocus().focusable = true; 876 if (issue) |i| try description.setContent(allocator, i.event.description); 877 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 878 } 879 880 try addSubmitButton(allocator, &box); 881 882 box.getFocus().child_id = box.children.keys()[title_field_index]; 883 return box; 884 } 885 886 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(ui.Widget) { 887 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 888 errdefer box.deinit(allocator); 889 box.getFocus().kind = .{ .custom = action }; 890 891 { 892 var parent = try ui.authorBox(allocator, session.page_arena, author); 893 errdefer parent.deinit(allocator); 894 parent.options.label = " replying to "; 895 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 896 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 897 } 898 899 { 900 var body = try wgt.TextInput(ui.Widget).init(allocator, .{ 901 .label = " comment ", 902 .name = "body", 903 .visible_width = null, 904 .rounded_corners = true, 905 .render_content = session.is_terminal, 906 .multiline = true, 907 .visible_height = 5, 908 .scroll = .{ .fill = true }, 909 }); 910 errdefer body.deinit(allocator); 911 body.getFocus().focusable = true; 912 if (initial_body) |text| try body.setContent(allocator, text); 913 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 914 } 915 916 try addSubmitButtonLabeled(allocator, &box, "submit"); 917 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 918 return box; 919 } 920 921 // a form's submit button, then a spacer absorbing the leftover 922 // min-height the box hands its last child, so the button keeps its 923 // natural height 924 fn addSubmitButton(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 925 return addSubmitButtonLabeled(allocator, box, "submit"); 926 } 927 928 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8) !void { 929 { 930 var submit = try ui.SubmitButton.initLabeled(allocator, label); 931 errdefer submit.deinit(allocator); 932 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 933 } 934 { 935 var spacer = try ui.Spacer.init(allocator); 936 errdefer spacer.deinit(allocator); 937 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 938 } 939 } 940 941 // the conflict resolve form. the "use this" links are navigations that 942 // flip the url's theirs: pick, reloading the prefills; one submit 943 // settles every conflict. the form: subtree makes the web overlay POST 944 // to the resolve route. 945 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(ui.Widget) { 946 const aa = session.page_arena.allocator(); 947 // init only picks the resolve view once it has read the conflict 948 const conflict = if (data.conflict) |*c| c else unreachable; 949 950 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 951 errdefer box.deinit(allocator); 952 box.getFocus().kind = .{ .custom = if (data.identity.len == 0) 953 try std.fmt.allocPrint(aa, "form:/issue:{s}/resolve", .{data.selected_id}) 954 else 955 try std.fmt.allocPrint(aa, "form:/repo/{s}/issue:{s}/resolve", .{ data.identity, data.selected_id }) }; 956 957 if (conflict.title) |*fc| { 958 try addLabel(allocator, &box, "title conflict:"); 959 try addFieldConflict(allocator, &box, session, data, "title", fc); 960 } 961 if (conflict.tags) |*fc| { 962 try addLabel(allocator, &box, "tags conflict:"); 963 try addFieldConflict(allocator, &box, session, data, "tags", fc); 964 } 965 966 if (conflict.description) |*desc| { 967 try addLabel(allocator, &box, "description conflict:"); 968 var hunk_index: usize = 0; 969 for (desc.chunks, 0..) |*chunk, chunk_index| { 970 switch (chunk.*) { 971 .same => |text| { 972 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 973 errdefer tb.deinit(allocator); 974 tb.getFocus().focusable = true; 975 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 976 }, 977 // an auto-resolved chunk: only one side changed it, so its 978 // version stands, shown distinctly with its author 979 .auto => |auto| { 980 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 981 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 982 const label = switch (author) { 983 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 984 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 985 }; 986 var tb = try wgt.TextBox(ui.Widget).init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 987 errdefer tb.deinit(allocator); 988 tb.getFocus().focusable = true; 989 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 990 }, 991 .conflict => |hunk| { 992 // a blank row on each side sets the group apart from 993 // the flowing shared and auto-resolved chunks 994 if (chunk_index > 0) try addGap(allocator, &box); 995 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 996 hunk_index += 1; 997 const picked_theirs = data.theirsPicked(name); 998 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 999 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 1000 var resolution_input = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 1001 errdefer resolution_input.deinit(allocator); 1002 resolution_input.getFocus().focusable = true; 1003 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 1004 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1005 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 1006 }, 1007 } 1008 } 1009 } 1010 1011 try addSubmitButton(allocator, &box); 1012 1013 box.getFocus().child_id = box.children.keys()[0]; 1014 return box; 1015 } 1016 1017 // one conflicted scalar field: both sides' version rows, then the 1018 // resolution input prefilled from the picked side. 1019 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1020 const aa = session.page_arena.allocator(); 1021 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1022 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1023 1024 var resolution_input = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1025 errdefer resolution_input.deinit(allocator); 1026 resolution_input.getFocus().focusable = true; 1027 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 1028 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1029 } 1030 1031 // a blank row setting a conflict group apart from its neighbors 1032 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 1033 var text = try wgt.Text(ui.Widget).init(allocator, " "); 1034 errdefer text.deinit(allocator); 1035 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1036 } 1037 1038 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), content: []const u8) !void { 1039 var label = try ui.SectionLabel.init(allocator, content); 1040 errdefer label.deinit(allocator); 1041 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1042 } 1043 1044 // one side of a conflict: the "use this" link on the left, the version 1045 // itself as a labeled read-only box. 1046 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1047 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1048 errdefer row.deinit(allocator); 1049 1050 { 1051 const use_label = "use this"; 1052 var use = try wgt.TextBox(ui.Widget).init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1053 errdefer use.deinit(allocator); 1054 use.getFocus().focusable = true; 1055 use.getFocus().kind = .{ .custom = link }; 1056 try row.children.put(allocator, use.getFocus().id, .{ 1057 .widget = .{ .text_box = use }, 1058 .rect = null, 1059 .min_size = .{ .width = use_label.len + 2, .height = null }, 1060 .max_size = .{ .width = use_label.len + 2, .height = null }, 1061 }); 1062 } 1063 1064 { 1065 var tb = try wgt.TextBox(ui.Widget).init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1066 errdefer tb.deinit(allocator); 1067 tb.getFocus().focusable = true; 1068 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1069 } 1070 1071 row.getFocus().child_id = row.children.keys()[0]; 1072 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1073 } 1074 1075 // the label of one side's version box, naming its author 1076 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1077 return switch (author) { 1078 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1079 .unknown => if (ours) " current version " else " their version ", 1080 }; 1081 } 1082 1083 // the a: link that reloads the resolve page with `name` picked from ours 1084 // or theirs, reprefilling its resolution input 1085 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1086 const aa = session.page_arena.allocator(); 1087 var picks: std.ArrayList(u8) = .empty; 1088 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1089 while (it.next()) |pick| { 1090 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1091 if (picks.items.len > 0) try picks.append(aa, ','); 1092 try picks.appendSlice(aa, pick); 1093 } 1094 if (theirs) { 1095 if (picks.items.len > 0) try picks.append(aa, ','); 1096 try picks.appendSlice(aa, name); 1097 } 1098 const route = ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1099 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1100 } 1101 1102 // the resolve form's page-constant initial value for the input `name`: 1103 // the picked side of its conflicted field or hunk 1104 fn resolvePrefill(self: *View, name: []const u8) ?[]const u8 { 1105 const conflict = if (self.data.conflict) |*c| c else return null; 1106 const theirs = self.data.theirsPicked(name); 1107 if (std.mem.eql(u8, name, "title")) { 1108 const fc = if (conflict.title) |*f| f else return null; 1109 return if (theirs) fc.theirs.text else fc.ours.text; 1110 } 1111 if (std.mem.eql(u8, name, "tags")) { 1112 const fc = if (conflict.tags) |*f| f else return null; 1113 return if (theirs) fc.theirs.text else fc.ours.text; 1114 } 1115 if (std.mem.startsWith(u8, name, "d")) { 1116 const desc = if (conflict.description) |*d| d else return null; 1117 const hunk_index = std.fmt.parseInt(usize, name[1..], 10) catch return null; 1118 var seen: usize = 0; 1119 for (desc.chunks) |chunk| switch (chunk) { 1120 .conflict => |hunk| { 1121 if (seen == hunk_index) return (if (theirs) hunk.theirs else hunk.ours) orelse ""; 1122 seen += 1; 1123 }, 1124 else => {}, 1125 }; 1126 } 1127 return null; 1128 } 1129 1130 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1131 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1132 errdefer row.deinit(allocator); 1133 row.getFocus().focusable = true; 1134 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1135 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1136 } 1137 1138 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 1139 self.box.deinit(allocator); 1140 } 1141 1142 fn header(self: *View) *Header { 1143 return &self.box.children.values()[header_index].widget.repo_issues_header; 1144 } 1145 1146 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 1147 return &self.box.children.values()[stack_index].widget.stack; 1148 } 1149 1150 // `index`'s master-detail split inside the stack. 1151 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1152 return &self.viewStack().children.values()[index].box; 1153 } 1154 1155 // the tags view's flow inside the stack. 1156 fn tagsView(self: *View) *ui.TagFlow { 1157 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1158 } 1159 1160 // the new-issue, edit, or resolve form inside the stack, or null when the 1161 // unauthorized view stands in for it. 1162 fn issueForm(self: *View) ?*wgt.Box(ui.Widget) { 1163 return switch (self.viewStack().children.values()[form_view_index]) { 1164 .box => |*box| box, 1165 // the resolve form sits inside a scroll on the terminal 1166 .scroll => |*scroll| &scroll.child.box, 1167 else => null, 1168 }; 1169 } 1170 1171 // the resolve form's scroll on the terminal (the web page scrolls itself) 1172 fn resolveScroll(self: *View) ?*wgt.Scroll(ui.Widget) { 1173 return switch (self.viewStack().children.values()[form_view_index]) { 1174 .scroll => |*scroll| scroll, 1175 else => null, 1176 }; 1177 } 1178 1179 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1180 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1181 } 1182 1183 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1184 return &self.listScroll(index).child.box; 1185 } 1186 1187 // the detail frame's children: the tool row above the scroll pane. 1188 const tool_row_index: usize = 0; 1189 const detail_scroll_index: usize = 1; 1190 1191 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 1192 return &self.resultsBox(index).children.values()[detail_index].widget.box; 1193 } 1194 1195 fn toolRow(self: *View, index: usize) *wgt.Box(ui.Widget) { 1196 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 1197 } 1198 1199 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1200 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 1201 } 1202 1203 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 1204 return &self.detailScroll(index).child.box; 1205 } 1206 1207 fn window(self: *View, index: usize) *const Window { 1208 if (index == conflicts_view_index) return &self.data.conflicts; 1209 return self.data.window(splitStatus(index)); 1210 } 1211 1212 fn stackSelectedIndex(self: *View) ?usize { 1213 const stack = self.viewStack(); 1214 const cid = stack.getFocus().child_id orelse return null; 1215 return stack.children.getIndex(cid); 1216 } 1217 1218 // the stack's selected master-detail split (null when the tags view or the 1219 // new-issue form shows). 1220 fn selectedSplitIndex(self: *View) ?usize { 1221 const idx = self.stackSelectedIndex() orelse return null; 1222 return if (idx < split_count or idx == conflicts_view_index) idx else null; 1223 } 1224 1225 fn detailActive(self: *View, index: usize) bool { 1226 const rb = self.resultsBox(index); 1227 const cid = rb.getFocus().child_id orelse return false; 1228 return rb.children.getIndex(cid) == detail_index; 1229 } 1230 1231 fn headerActive(self: *View) bool { 1232 const cid = self.box.getFocus().child_id orelse return false; 1233 return self.box.children.getIndex(cid) == header_index; 1234 } 1235 1236 fn tagsViewActive(self: *View) bool { 1237 if (self.headerActive()) return false; 1238 return self.stackSelectedIndex() == tags_view_index; 1239 } 1240 1241 fn formViewActive(self: *View) bool { 1242 if (self.headerActive()) return false; 1243 return self.stackSelectedIndex() == form_view_index; 1244 } 1245 1246 // the selected issue's index, or null when a window-navigation row is 1247 // selected (a leading "previous" row shifts the issue rows down by one). 1248 fn selectedIssueIndex(self: *View, index: usize) ?usize { 1249 const lb = self.listBox(index); 1250 const cid = lb.getFocus().child_id orelse return null; 1251 const idx = lb.children.getIndex(cid) orelse return null; 1252 const win = self.window(index); 1253 const lead: usize = if (win.prev_id != null) 1 else 0; 1254 if (idx < lead or idx - lead >= win.issues.len) return null; 1255 return idx - lead; 1256 } 1257 1258 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1259 self.clearGrid(); 1260 1261 // each header tab maps 1:1 to a stack child by position. mirror the 1262 // selection into the url: the view the page's window is rooted at 1263 // keeps its rooted url, the others get their list route (keeping the 1264 // tag filter). 1265 if (self.header().getSelectedIndex()) |index| { 1266 const stack = self.viewStack(); 1267 stack.getFocus().child_id = stack.children.keys()[index]; 1268 self.session.data.current_page = (if (index == form_view_index) 1269 (switch (self.data.view) { 1270 .edit => ui.RoutablePage.repoIssuesEditRoute(self.data.identity, self.data.selected_id), 1271 .new_comment => ui.RoutablePage.repoCommentNewRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 1272 .edit_comment => ui.RoutablePage.repoCommentEditRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 1273 .resolve => ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, self.data.selected_id, self.data.theirs_picks), 1274 else => ui.RoutablePage.repoIssuesNewRoute(self.data.identity), 1275 }) 1276 else if (index == conflicts_view_index) 1277 ui.RoutablePage.repoIssuesConflictsRoute(self.data.identity, if (self.data.view == .conflicts) self.data.selected_id else "") 1278 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 1279 (if (self.data.description_page) 1280 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, self.data.selected_id) 1281 else if (self.data.comment_page) |page| 1282 ui.RoutablePage.repoCommentsRoute(self.data.identity, self.data.selected_id, &page.selected.id, page.replies.start) 1283 else 1284 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, self.data.selected_id, self.data.comments_start)) 1285 else if (index == tags_view_index) 1286 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 1287 else 1288 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 1289 } 1290 1291 if (self.selectedSplitIndex()) |i| { 1292 // swap the detail pane to the selected issue when it changes. 1293 try self.refreshDetail(allocator, i); 1294 1295 // mirror the focused issue into the url, but only while focus is 1296 // inside the split. an issue's url is the same whether or not the 1297 // list is filtered, so the mirror drops the tag. 1298 if (root_focus.grandchild_id) |g| { 1299 if (self.resultsBox(i).getFocus().children.contains(g)) { 1300 if (self.selectedIssueIndex(i)) |sel| { 1301 // selecting another issue leaves the description page 1302 // behind, like it leaves the tag filter behind. 1303 const entry = &self.window(i).issues[sel]; 1304 const mirrored = if (self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id)) 1305 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, entry.id) 1306 else if (self.data.comment_page) |page| 1307 if (std.mem.eql(u8, entry.id, self.data.selected_id)) 1308 ui.RoutablePage.repoCommentsRoute(self.data.identity, entry.id, &page.selected.id, page.replies.start) 1309 else 1310 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) 1311 else if (std.mem.eql(u8, entry.id, self.data.selected_id)) 1312 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, entry.comments.start) 1313 else 1314 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0); 1315 if (mirrored) |route| self.session.data.current_page = route; 1316 } 1317 } 1318 } 1319 1320 // the selected list row shows a border (the focused TextBox 1321 // upgrades it to a double border itself); the rest stay borderless. 1322 const lb = self.listBox(i); 1323 for (lb.children.keys(), lb.children.values()) |id, *child| { 1324 switch (child.widget) { 1325 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 1326 else => {}, 1327 } 1328 } 1329 1330 // the pane's selected child shows the selection border: the 1331 // description directly, the tags via the flow's selected item. 1332 // cap the list at list_max_width only while the detail pane fits 1333 // beside it. the box drops the detail when the width can't hold 1334 // both minimums, so when it's that narrow we lift the cap and let 1335 // the list fill the whole width. 1336 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 1337 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 1338 1339 // stretch the detail pane across the rest of the width so it fills 1340 // the area rather than shrinking to its content; its scroll fills 1341 // the pane. 1342 if (constraint.max_size.width) |w| { 1343 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 1344 } else { 1345 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 1346 } 1347 } 1348 1349 // refresh the form inputs' entries in the session's focus-id -> input 1350 // map with this frame's addresses, so the web/wasm form handling can 1351 // find them by focus id 1352 if (self.issueForm()) |form| { 1353 const inputs_arena = self.session.arena.allocator(); 1354 for (form.children.values()) |*child| switch (child.widget) { 1355 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 1356 else => {}, 1357 }; 1358 1359 // on an edit page, register the issue's content as the inputs' 1360 // page-constant initial values, which the web overlay renders 1361 // into them. 1362 if (self.data.view == .edit) { 1363 if (self.data.selectedIssue()) |entry| { 1364 const fields = form.children.values(); 1365 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.issue.event.title); 1366 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.issue.event.tags); 1367 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.issue.event.description); 1368 } 1369 } 1370 1371 if (self.data.view == .edit_comment) { 1372 if (self.data.comment_page) |page| { 1373 const body = &form.children.values()[comment_body_field_index].widget.text_input; 1374 try self.session.input_values.put(inputs_arena, body.getFocus().id, page.selected.comment.event.body); 1375 } 1376 } 1377 1378 // the resolve form's inputs prefill from the picked side, which 1379 // is url state and so page-constant like the edit values above 1380 if (self.data.view == .resolve) { 1381 for (form.children.values()) |*child| switch (child.widget) { 1382 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 1383 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 1384 }, 1385 else => {}, 1386 }; 1387 } 1388 } 1389 1390 try self.box.build(allocator, constraint, root_focus); 1391 } 1392 1393 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1394 const sel = self.selectedIssueIndex(index) orelse return; 1395 if (self.detailed_index[index]) |d| if (d == sel) return; 1396 try self.populateDetail(allocator, index, sel); 1397 self.detailed_index[index] = sel; 1398 } 1399 1400 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1401 const entry = self.window(index).issues[sel]; 1402 const inner = self.detailInner(index); 1403 // the /description page replaces its issue's detail with a back link 1404 // and the whole description; other issues keep their normal detail. 1405 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1406 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 1407 1408 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1409 inner.children.clearAndFree(allocator); 1410 inner.getFocus().child_id = null; 1411 self.author_id[index] = null; 1412 self.status_button_id[index] = null; 1413 self.remove_button_id[index] = null; 1414 1415 // the tool row: the open/close and edit buttons; the description page 1416 // shows none. 1417 { 1418 const row = self.toolRow(index); 1419 for (row.children.values()) |*child| child.widget.deinit(allocator); 1420 row.children.clearAndFree(allocator); 1421 row.getFocus().child_id = null; 1422 row.getFocus().kind = .container; 1423 } 1424 if (!description_page and comment_page == null) { 1425 const row = self.toolRow(index); 1426 const pa = self.session.page_arena.allocator(); 1427 1428 { 1429 var spacer = try ui.Spacer.init(allocator); 1430 errdefer spacer.deinit(allocator); 1431 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1432 } 1433 1434 // a terminal has no file picker, so attaching is web only. the 1435 // renderer covers this button with a file input, so clicking it 1436 // opens the browser's own picker and posts what it chose. 1437 if (!self.session.is_terminal and !entry.conflicted and (self.session.data.is_local or self.session.data.user_id != null)) { 1438 const label = "add attachment"; 1439 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1440 errdefer button.deinit(allocator); 1441 button.getFocus().focusable = true; 1442 button.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1443 try std.fmt.allocPrint(pa, "{s}/issue:{s}/attach", .{ ui.file_input_prefix, entry.id }) 1444 else 1445 try std.fmt.allocPrint(pa, "{s}/repo/{s}/issue:{s}/attach", .{ ui.file_input_prefix, self.data.identity, entry.id }) }; 1446 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1447 } 1448 1449 // a conflicted issue's only action is resolving it. the button 1450 // stays visible logged out; the resolve page shows the 1451 // unauthorized view then. 1452 if (entry.conflicted) { 1453 const label = "resolve conflict"; 1454 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1455 errdefer button.deinit(allocator); 1456 button.getFocus().focusable = true; 1457 const route = ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1458 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1459 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1460 } else { 1461 // a logged-out session can't flip the status, so it gets no 1462 // open/close button 1463 if (self.session.data.is_local or self.session.data.user_id != null) { 1464 const action: []const u8 = switch (entry.issue.event.status) { 1465 .open => "close", 1466 .closed => "open", 1467 }; 1468 // the renderer distinguishes plain clickables from buttons 1469 // that should POST to a server route by the submit kind. 1470 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1471 try std.fmt.allocPrint(pa, "form:/issue:{s}/{s}", .{ entry.id, action }) 1472 else 1473 try std.fmt.allocPrint(pa, "form:/repo/{s}/issue:{s}/{s}", .{ self.data.identity, entry.id, action }) }; 1474 1475 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1476 errdefer button.deinit(allocator); 1477 button.getFocus().focusable = true; 1478 // the renderer distinguishes plain clickables from buttons 1479 // that should POST to a server route by this kind. 1480 button.getFocus().kind = .{ .custom = "submit" }; 1481 self.status_button_id[index] = button.getFocus().id; 1482 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1483 } 1484 1485 // the edit button links to the issue's edit page. 1486 { 1487 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1488 errdefer button.deinit(allocator); 1489 button.getFocus().focusable = true; 1490 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1491 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1492 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1493 } 1494 } 1495 1496 if (self.session.data.is_local or self.session.data.user_id != null) { 1497 const issue_route = ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1498 const remove_url = try std.fmt.allocPrint(pa, "{s}/remove", .{try issue_route.toUrl(self.session.page_arena)}); 1499 const kind: []const u8 = if (entry.conflicted) blk: { 1500 row.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "form:{s}", .{remove_url}) }; 1501 break :blk "submit"; 1502 } else try std.fmt.allocPrint(pa, "{s}{s}", .{ ui.submit_action_prefix, remove_url }); 1503 1504 var remove = try wgt.TextBox(ui.Widget).init(allocator, "✕", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1505 errdefer remove.deinit(allocator); 1506 remove.getFocus().focusable = true; 1507 remove.getFocus().kind = .{ .custom = kind }; 1508 self.remove_button_id[index] = remove.getFocus().id; 1509 try row.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 1510 } 1511 1512 // the leftmost button: the attachment button when the session has 1513 // one, else the resolve button on a conflicted issue, the 1514 // open/close button when present, the edit button otherwise 1515 row.getFocus().child_id = row.children.keys()[first_in_row_index]; 1516 } 1517 1518 if (comment_page) |page| { 1519 var back = try Comment.linkBox(allocator, self.session, "← back to issue", ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1520 errdefer back.deinit(allocator); 1521 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1522 1523 try Comment.appendComment(allocator, inner, self.session, self.data.identity, page.selected); 1524 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies", self.session.page_arena.allocator()); 1525 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1526 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1527 1528 var spacer = try ui.Spacer.init(allocator); 1529 errdefer spacer.deinit(allocator); 1530 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1531 1532 inner.getFocus().child_id = inner.children.keys()[0]; 1533 const sc = self.detailScroll(index); 1534 sc.x = 0; 1535 sc.y = 0; 1536 sc.getFocus().version +%= 1; 1537 return; 1538 } 1539 1540 if (description_page) { 1541 // the back link, in the title slot so the pane's input handling 1542 // applies to it unchanged. 1543 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1544 errdefer tb.deinit(allocator); 1545 tb.getFocus().focusable = true; 1546 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.event.status, "", entry.id) }; 1547 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1548 } else { 1549 // the issue's title as a focusable word-wrapped text box. 1550 { 1551 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1552 errdefer tb.deinit(allocator); 1553 tb.getFocus().focusable = true; 1554 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1555 } 1556 1557 self.author_id[index] = blk: { 1558 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1559 errdefer tb.deinit(allocator); 1560 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1561 break :blk tb.getFocus().id; 1562 }; 1563 1564 // the issue's tags, each linking to this status's list filtered to 1565 // that tag. 1566 { 1567 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1568 defer items.deinit(allocator); 1569 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 1570 while (tag_iter.next()) |tag| { 1571 if (tag.len == 0) continue; 1572 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entry.issue.event.status, tag) }); 1573 } 1574 if (items.items.len > 0) { 1575 var tf = try ui.TagFlow.init(allocator); 1576 errdefer tf.deinit(allocator); 1577 try tf.setItems(allocator, items.items); 1578 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1579 } 1580 } 1581 } 1582 1583 // the description as a focusable word-wrapped text box. past the limit 1584 // it's cut at the last whole line and links to the /description page, 1585 // which shows the whole thing. 1586 self.description_id[index] = blk: { 1587 const whole = entry.issue.event.description; 1588 const cut_short = !description_page and whole.len > max_description_size; 1589 const shown = if (cut_short) 1590 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 1591 else if (whole.len == 0) 1592 "(no description)" 1593 else 1594 whole; 1595 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1596 .border_style = .single, 1597 .rounded_corners = true, 1598 .wrap_kind = .word, 1599 .label = " description ", 1600 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1601 }); 1602 errdefer tb.deinit(allocator); 1603 tb.getFocus().focusable = true; 1604 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1605 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1606 break :blk tb.getFocus().id; 1607 }; 1608 1609 const parent_route = ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1610 try Attachment.appendRows(allocator, inner, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 1611 1612 if (!description_page) { 1613 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoCommentNewRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1614 errdefer reply.deinit(allocator); 1615 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1616 1617 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments", self.session.page_arena.allocator()); 1618 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1619 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, entry.id, null, entry.comments); 1620 1621 var spacer = try ui.Spacer.init(allocator); 1622 errdefer spacer.deinit(allocator); 1623 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1624 } 1625 1626 // select the title by default 1627 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1628 1629 // reset the scroll to the top for the newly-shown issue: directly on the 1630 // terminal (the wasm offset), and via a version bump on the web (so the 1631 // renderer's scroll id changes and JS drops the preserved position). 1632 const sc = self.detailScroll(index); 1633 sc.x = 0; 1634 sc.y = 0; 1635 sc.getFocus().version +%= 1; 1636 } 1637 1638 pub fn input(self: *View, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1639 // wheel input moves focus like the arrows 1640 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1641 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1642 else 1643 raw_key; 1644 1645 if (self.headerActive()) { 1646 // down from the tabs re-enters the stack if the selected view has 1647 // something to focus; other keys move the tabs. 1648 if (inp.vertDirection(key) == .down) { 1649 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1650 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1651 // nothing focusable in the unauthorized view 1652 .unauthorized => false, 1653 else => true, 1654 } else false; 1655 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1656 } else { 1657 try self.header().input(allocator, key, root_focus); 1658 } 1659 return; 1660 } 1661 if (self.tagsViewActive()) { 1662 self.tagsViewInput(key, root_focus); 1663 return; 1664 } 1665 if (self.formViewActive()) { 1666 if (self.data.view == .resolve) { 1667 try self.resolveInput(allocator, key, root_focus); 1668 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1669 try self.commentFormInput(allocator, key, root_focus); 1670 } else { 1671 try self.formInput(allocator, key, root_focus); 1672 } 1673 return; 1674 } 1675 const i = self.selectedSplitIndex() orelse return; 1676 if (self.detailActive(i)) { 1677 try self.detailInput(allocator, i, key, root_focus); 1678 } else { 1679 try self.listInput(i, key, root_focus); 1680 } 1681 } 1682 1683 // arrow keys move the tag selection; up from the top row crosses to the 1684 // header tabs. 1685 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1686 const tf = self.tagsView(); 1687 const cid = tf.focus.child_id orelse return; 1688 const cur = tf.indexOfFocusId(cid) orelse return; 1689 const count = tf.text_boxes.items.len; 1690 switch (key) { 1691 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1692 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1693 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1694 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1695 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1696 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1697 else => {}, 1698 } 1699 } 1700 1701 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1702 // up/down (and the scroll wheel) move the selection a row; page up/down 1703 // jump a fixed amount. right/Enter cross into the detail pane. up from 1704 // the top row crosses into the header tabs. 1705 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1706 const lb = self.listBox(index); 1707 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1708 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1709 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1710 return; 1711 } 1712 switch (key) { 1713 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1714 else => {}, 1715 } 1716 } 1717 1718 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1719 switch (key) { 1720 .page_up => { 1721 self.pageDetail(index, root_focus, -10); 1722 return; 1723 }, 1724 .page_down => { 1725 self.pageDetail(index, root_focus, 10); 1726 return; 1727 }, 1728 .home => { 1729 self.jumpDetail(index, root_focus, false); 1730 return; 1731 }, 1732 .end => { 1733 self.jumpDetail(index, root_focus, true); 1734 return; 1735 }, 1736 else => {}, 1737 } 1738 if (self.data.comment_page != null and std.mem.eql(u8, self.window(index).issues[self.detailed_index[index] orelse return].id, self.data.selected_id)) { 1739 try self.commentInput(allocator, index, key, root_focus); 1740 } else if (self.toolRowFocused(index)) { 1741 try self.toolRowInput(allocator, index, key, root_focus); 1742 } else if (self.titleFocused(index)) { 1743 try self.titleInput(index, key, root_focus); 1744 } else if (self.authorFocused(index)) { 1745 try self.authorInput(index, key, root_focus); 1746 } else if (self.tagsFocused(index)) { 1747 try self.tagsInput(index, key, root_focus); 1748 } else if (self.descriptionFocused(index)) { 1749 try self.descriptionInput(index, key, root_focus); 1750 } else if (try self.attachmentInput(allocator, index, key, root_focus)) {} else { 1751 try self.commentInput(allocator, index, key, root_focus); 1752 } 1753 } 1754 1755 fn attachmentInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !bool { 1756 const child_index = self.focusedDetailChild(index, root_focus) orelse return false; 1757 const child = &self.detailInner(index).children.values()[child_index].widget; 1758 const row = switch (child.*) { 1759 .box => |*box| box, 1760 else => return false, 1761 }; 1762 const attachment_id = Attachment.removeId(row) orelse return false; 1763 if (row.children.count() < 2) return false; 1764 const remove_id = row.children.keys()[1]; 1765 switch (key) { 1766 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) try self.focusList(index, root_focus), 1767 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1768 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1769 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1770 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 1771 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 1772 else => {}, 1773 } 1774 return true; 1775 } 1776 1777 // the tool row: enter or a click on the open/close button flips the 1778 // issue's status; the edit and resolve buttons are a: links the host 1779 // follows. arrows cross between the buttons and to the neighboring 1780 // widgets. 1781 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1782 const row = self.toolRow(index); 1783 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1784 const on_status = if (self.status_button_id[index]) |id| row.getFocus().child_id == id else false; 1785 const on_remove = if (self.remove_button_id[index]) |id| row.getFocus().child_id == id else false; 1786 switch (key) { 1787 .arrow_left => if (cur > first_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1788 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1789 .arrow_up => self.focusHeader(root_focus), 1790 .arrow_down => { 1791 self.focusDetailEdge(index, root_focus, false); 1792 self.detailScroll(index).y = 0; 1793 }, 1794 .enter => if (on_remove) try self.removeIssue(allocator, index) else if (on_status) try self.toggleIssueStatus(allocator, index), 1795 .mouse => |mouse| if (self.remove_button_id[index]) |id| { 1796 if (inp.leftClickOn(root_focus, id, mouse)) return self.removeIssue(allocator, index); 1797 if (self.status_button_id[index]) |status_id| if (inp.leftClickOn(root_focus, status_id, mouse)) try self.toggleIssueStatus(allocator, index); 1798 } else if (self.status_button_id[index]) |id| { 1799 if (inp.leftClickOn(root_focus, id, mouse)) try self.toggleIssueStatus(allocator, index); 1800 }, 1801 else => {}, 1802 } 1803 } 1804 1805 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1806 switch (key) { 1807 .arrow_left => try self.focusList(index, root_focus), 1808 .arrow_up => self.focusToolRow(index, root_focus), 1809 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1810 else => {}, 1811 } 1812 } 1813 1814 // the author box's a: link (when it has one) is followed by the host on 1815 // enter / a click; arrows cross to the neighboring widgets. 1816 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1817 switch (key) { 1818 .arrow_left => try self.focusList(index, root_focus), 1819 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1820 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1821 else => {}, 1822 } 1823 } 1824 1825 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1826 const sc = self.detailScroll(index); 1827 switch (key) { 1828 .arrow_left => return self.focusList(index, root_focus), 1829 // the terminal moves through the description one line at a time; 1830 // the web lets focus changes drive its native scroll. 1831 .arrow_up => { 1832 if (!self.session.is_terminal) { 1833 _ = self.moveDetailVertical(index, root_focus, false); 1834 return; 1835 } 1836 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1837 const before = sc.y; 1838 sc.y -= 1; 1839 sc.clampToContent(); 1840 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1841 return; 1842 }, 1843 .arrow_down => { 1844 if (!self.session.is_terminal) { 1845 _ = self.moveDetailVertical(index, root_focus, true); 1846 return; 1847 } 1848 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1849 const before = sc.y; 1850 sc.y += 1; 1851 sc.clampToContent(); 1852 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1853 return; 1854 }, 1855 else => return, 1856 } 1857 sc.clampToContent(); 1858 } 1859 1860 fn commentInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1861 const item = self.focusedComment(index, root_focus); 1862 switch (key) { 1863 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1864 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1865 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1866 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1867 .enter => if (item) |comment| if (comment.removeFocused()) try self.removeComment(allocator, comment), 1868 .mouse => |mouse| if (item) |comment| if (comment.remove_button_id) |id| { 1869 if (inp.leftClickOn(root_focus, id, mouse)) try self.removeComment(allocator, comment); 1870 }, 1871 else => return, 1872 } 1873 } 1874 1875 fn focusedComment(self: *View, index: usize, root_focus: *Focus) ?*Comment.Item { 1876 const child_index = self.focusedDetailChild(index, root_focus) orelse return null; 1877 return switch (self.detailInner(index).children.values()[child_index].widget) { 1878 .repo_comment => |*comment| comment, 1879 else => null, 1880 }; 1881 } 1882 1883 // page the detail scroll, then focus the first or last visible widget. 1884 fn pageDetail(self: *View, index: usize, root_focus: *Focus, delta: isize) void { 1885 if (!self.session.is_terminal) return; 1886 const sc = self.detailScroll(index); 1887 sc.y += delta; 1888 sc.clampToContent(); 1889 self.focusVisible(index, root_focus, delta > 0); 1890 } 1891 1892 // jump to the detail's first or last focusable widget. the terminal pins 1893 // its synthetic scroll too; the browser scrolls to the new focus itself. 1894 fn jumpDetail(self: *View, index: usize, root_focus: *Focus, to_end: bool) void { 1895 if (self.session.is_terminal) { 1896 const sc = self.detailScroll(index); 1897 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1898 sc.clampToContent(); 1899 self.focusVisible(index, root_focus, to_end); 1900 } else { 1901 self.focusDetailEdge(index, root_focus, to_end); 1902 } 1903 } 1904 1905 fn focusDetailEdge(self: *View, index: usize, root_focus: *Focus, last: bool) void { 1906 const focus = self.detailInner(index).getFocus(); 1907 var chosen: ?usize = null; 1908 for (focus.children.keys(), focus.children.values()) |id, child| { 1909 if (!child.focus.focusable) continue; 1910 chosen = id; 1911 if (!last) break; 1912 } 1913 if (chosen) |id| root_focus.setFocus(id); 1914 } 1915 1916 fn detailRectVisible(self: *View, index: usize, rect: layout.IRect) bool { 1917 const sc = self.detailScroll(index); 1918 const viewport = sc.grid orelse return false; 1919 const top = sc.y; 1920 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1921 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1922 } 1923 1924 // focus the top- or bottom-most focusable widget intersecting the viewport. 1925 fn focusVisible(self: *View, index: usize, root_focus: *Focus, last: bool) void { 1926 const inner = self.detailInner(index); 1927 var offset: usize = 0; 1928 while (offset < inner.children.count()) : (offset += 1) { 1929 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1930 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1931 } 1932 } 1933 1934 fn focusVisibleChild(self: *View, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1935 const child = &self.detailInner(index).children.values()[child_index]; 1936 const child_rect = child.rect orelse return false; 1937 var chosen: ?usize = null; 1938 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1939 const id = self.detailInner(index).children.keys()[child_index]; 1940 chosen = id; 1941 if (!last) { 1942 root_focus.setFocus(id); 1943 return true; 1944 } 1945 } 1946 1947 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1948 if (!focus_child.focus.focusable) continue; 1949 const rect: layout.IRect = .{ 1950 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1951 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1952 .size = focus_child.rect.size, 1953 }; 1954 if (!self.detailRectVisible(index, rect)) continue; 1955 chosen = id; 1956 if (!last) break; 1957 } 1958 if (chosen) |id| { 1959 root_focus.setFocus(id); 1960 return true; 1961 } 1962 return false; 1963 } 1964 1965 fn focusAdjacentVisible(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1966 const inner = self.detailInner(index); 1967 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1968 while (true) { 1969 if (down) { 1970 next += 1; 1971 if (next >= inner.children.count()) return false; 1972 } else { 1973 if (next == 0) return false; 1974 next -= 1; 1975 } 1976 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1977 self.focusDetailChild(index, next, !down, root_focus); 1978 return true; 1979 } 1980 } 1981 } 1982 1983 fn focusedDetailChild(self: *View, index: usize, root_focus: *Focus) ?usize { 1984 const inner = self.detailInner(index); 1985 var id = root_focus.grandchild_id orelse return null; 1986 while (inner.getFocus().children.get(id)) |child| { 1987 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1988 id = child.parent_id; 1989 } 1990 return null; 1991 } 1992 1993 fn detailChildFocusable(self: *View, index: usize, child_index: usize) bool { 1994 const child = &self.detailInner(index).children.values()[child_index].widget; 1995 const focus = child.getFocus(); 1996 if (focus.focusable) return true; 1997 for (focus.children.values()) |item| { 1998 if (item.focus.focusable) return true; 1999 } 2000 return false; 2001 } 2002 2003 fn focusDetailChild(self: *View, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 2004 const inner = self.detailInner(index); 2005 const child = &inner.children.values()[child_index]; 2006 var target = inner.children.keys()[child_index]; 2007 var rect = child.rect orelse return; 2008 switch (child.widget) { 2009 .repo_comment => |*comment| { 2010 if (comment.rowRect(last)) |row_rect| { 2011 rect.x += row_rect.x; 2012 rect.y += row_rect.y; 2013 rect.size = row_rect.size; 2014 } 2015 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 2016 }, 2017 .box => |*box| if (box.children.count() > 0) { 2018 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 2019 root_focus.setFocus(target); 2020 }, 2021 else => root_focus.setFocus(target), 2022 } 2023 const sc = self.detailScroll(index); 2024 if (sc.grid) |viewport| { 2025 const viewport_height = viewport.size.height - sc.bar_h; 2026 if (rect.size.height > viewport_height) { 2027 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 2028 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 2029 sc.clampToContent(); 2030 return; 2031 } 2032 } 2033 sc.scrollToRect(rect); 2034 } 2035 2036 fn moveDetailVertical(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 2037 const inner = self.detailInner(index); 2038 const current = self.focusedDetailChild(index, root_focus) orelse return false; 2039 if (inner.children.values()[current].widget == .repo_comment) { 2040 const comment = &inner.children.values()[current].widget.repo_comment; 2041 if (down and !comment.bodyFocused()) { 2042 self.focusDetailChild(index, current, true, root_focus); 2043 return true; 2044 } 2045 if (!down and comment.bodyFocused()) { 2046 self.focusDetailChild(index, current, false, root_focus); 2047 return true; 2048 } 2049 } 2050 2051 var next = current; 2052 while (true) { 2053 if (down) { 2054 next += 1; 2055 if (next >= inner.children.count()) return false; 2056 } else { 2057 if (next == 0) return false; 2058 next -= 1; 2059 } 2060 if (!self.detailChildFocusable(index, next)) continue; 2061 self.focusDetailChild(index, next, !down, root_focus); 2062 return true; 2063 } 2064 } 2065 2066 fn moveCommentHorizontal(self: *View, index: usize, root_focus: *Focus, right: bool) bool { 2067 const inner = self.detailInner(index); 2068 const current = self.focusedDetailChild(index, root_focus) orelse return false; 2069 const child = &inner.children.values()[current]; 2070 if (child.widget == .repo_comment) { 2071 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 2072 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 2073 return moved; 2074 } 2075 const row = switch (child.widget) { 2076 .box => |*box| box, 2077 else => return false, 2078 }; 2079 const selected = row.getFocus().child_id orelse return false; 2080 const selected_index = row.children.getIndex(selected) orelse return false; 2081 const target = if (right) selected_index + 1 else selected_index -| 1; 2082 if (target == selected_index or target >= row.children.count()) return false; 2083 root_focus.setFocus(row.children.keys()[target]); 2084 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 2085 return true; 2086 } 2087 2088 // up/down (and tab/shift+tab) move between a form's fields; up from the 2089 // title crosses into the header tabs. the multiline description keeps 2090 // enter and any up/down that has a row to move to. 2091 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2092 const form = self.issueForm() orelse return; 2093 const cid = form.getFocus().child_id orelse return; 2094 const cur = form.children.getIndex(cid) orelse return; 2095 2096 if (cur == description_field_index) { 2097 const child = &form.children.values()[cur]; 2098 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2099 return child.widget.input(allocator, key, root_focus); 2100 } 2101 2102 switch (key) { 2103 .arrow_up, .back_tab => if (cur > 0) 2104 root_focus.setFocus(form.children.keys()[cur - 1]) 2105 else 2106 self.focusHeader(root_focus), 2107 .arrow_down, .tab => if (cur < submit_field_index) { 2108 root_focus.setFocus(form.children.keys()[cur + 1]); 2109 }, 2110 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2111 .mouse => |mouse| if (cur == submit_field_index) { 2112 const submit = &form.children.values()[cur].widget.submit_button; 2113 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2114 }, 2115 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2116 } 2117 } 2118 2119 fn commentFormInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2120 const form = self.issueForm() orelse return; 2121 const cid = form.getFocus().child_id orelse return; 2122 const cur = form.children.getIndex(cid) orelse return; 2123 const child = &form.children.values()[cur]; 2124 2125 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2126 return child.widget.input(allocator, key, root_focus); 2127 2128 switch (key) { 2129 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2130 root_focus.setFocus(form.children.keys()[cur - 1]) 2131 else 2132 self.focusHeader(root_focus), 2133 .arrow_down, .tab => if (cur < comment_submit_field_index) 2134 root_focus.setFocus(form.children.keys()[cur + 1]), 2135 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2136 .mouse => |mouse| if (cur == comment_submit_field_index) { 2137 const submit = &child.widget.submit_button; 2138 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2139 }, 2140 else => try child.widget.input(allocator, key, root_focus), 2141 } 2142 } 2143 2144 // the multiline hunk inputs keep enter and any up/down that has a row to 2145 // move to; enter on a "use this" link is a navigation the host follows 2146 // before this runs. 2147 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2148 const form = self.issueForm() orelse return; 2149 const cid = form.getFocus().child_id orelse return; 2150 const cur = form.children.getIndex(cid) orelse return; 2151 const child = &form.children.values()[cur]; 2152 2153 if (child.widget == .text_input) { 2154 const ti = &child.widget.text_input; 2155 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2156 return child.widget.input(allocator, key, root_focus); 2157 } 2158 2159 const on_submit = child.widget == .submit_button; 2160 2161 switch (key) { 2162 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2163 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2164 .arrow_left, .arrow_right => switch (child.widget) { 2165 .box => |*row| { 2166 const row_cid = row.getFocus().child_id orelse return; 2167 const row_index = row.children.getIndex(row_cid) orelse return; 2168 if (key == .arrow_left) { 2169 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2170 } else if (row_index + 1 < row.children.count()) { 2171 root_focus.setFocus(row.children.keys()[row_index + 1]); 2172 } 2173 }, 2174 else => try child.widget.input(allocator, key, root_focus), 2175 }, 2176 .enter => if (on_submit) try self.submitResolution(allocator), 2177 .mouse => |mouse| if (on_submit) { 2178 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2179 }, 2180 else => try child.widget.input(allocator, key, root_focus), 2181 } 2182 } 2183 2184 // a multiline input keeps enter and any up/down that has a row to move to 2185 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 2186 return switch (key) { 2187 .enter => true, 2188 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2189 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2190 else => false, 2191 }; 2192 } 2193 2194 // the neighboring focusable resolve-form child, skipping the spacer and 2195 // the blank gap rows 2196 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 2197 var i = cur; 2198 while (true) { 2199 if (down) { 2200 i += 1; 2201 if (i >= form.children.count()) return null; 2202 } else { 2203 if (i == 0) return null; 2204 i -= 1; 2205 } 2206 switch (form.children.values()[i].widget) { 2207 .spacer, .text => continue, 2208 else => return i, 2209 } 2210 } 2211 } 2212 2213 // focus the resolve-form child at `i` (a version row focuses its selected 2214 // child) and keep it visible on the terminal 2215 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 2216 const child = &form.children.values()[i]; 2217 switch (child.widget) { 2218 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2219 else => root_focus.setFocus(form.children.keys()[i]), 2220 } 2221 if (self.session.is_terminal) { 2222 if (self.resolveScroll()) |sc| { 2223 if (child.rect) |rect| sc.scrollToRect(rect); 2224 } 2225 } 2226 } 2227 2228 // re-emit the conflicted issue's event with the resolved content; any 2229 // event on the issue settles its conflict entry. this is the terminal 2230 // path; the web posts the form to the resolve route. 2231 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 2232 if (comptime wasm) return; 2233 const io = self.session.io orelse return; 2234 const src = self.data.repo_source orelse return; 2235 const entry = self.data.selectedIssue() orelse return; 2236 const author = (try self.session.eventAuthor()) orelse return; 2237 const form = self.issueForm() orelse return; 2238 2239 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2240 var title: ?[]u8 = null; 2241 var tags: ?[]u8 = null; 2242 var hunks: std.ArrayList([]const u8) = .empty; 2243 defer { 2244 if (title) |t| allocator.free(t); 2245 if (tags) |t| allocator.free(t); 2246 for (hunks.items) |h| allocator.free(h); 2247 hunks.deinit(allocator); 2248 } 2249 for (form.children.values()) |*child| switch (child.widget) { 2250 .text_input => |*ti| { 2251 const text = try ti.text(allocator); 2252 errdefer allocator.free(text); 2253 if (std.mem.eql(u8, ti.options.name, "title")) { 2254 title = text; 2255 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2256 tags = text; 2257 } else { 2258 try hunks.append(allocator, text); 2259 } 2260 }, 2261 else => {}, 2262 }; 2263 2264 var id_bytes: [evt.event_id_size]u8 = undefined; 2265 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2266 2267 switch (src.repo_kind) { 2268 inline else => |repo_kind| { 2269 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2270 defer any_repo.deinit(io, allocator); 2271 switch (any_repo) { 2272 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 2273 .title = title, 2274 .tags = tags, 2275 .hunks = hunks.items, 2276 } }, author) catch |err| switch (err) { 2277 // leave the form up for correction 2278 error.InvalidFields => return, 2279 else => |e| return e, 2280 }, 2281 } 2282 }, 2283 } 2284 2285 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2286 try self.session.navigate(route); 2287 } 2288 2289 // commit a new or edited comment and navigate to its permalink. this is the 2290 // terminal path; the web posts the form to its comment route. 2291 fn submitComment(self: *View, allocator: std.mem.Allocator) !void { 2292 if (comptime wasm) return; 2293 const io = self.session.io orelse return; 2294 const src = self.data.repo_source orelse return; 2295 const author = (try self.session.eventAuthor()) orelse return; 2296 const form = self.issueForm() orelse return; 2297 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2298 const body = try body_input.text(allocator); 2299 defer allocator.free(body); 2300 if (!evt.Comment.fieldsValid(body)) return; 2301 2302 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2303 @memcpy(&thread_id, self.data.selected_id); 2304 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2305 var parent: [evt.event_id_size * 2]u8 = undefined; 2306 @memcpy(&parent, self.data.comment_id); 2307 break :blk parent; 2308 }; 2309 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2310 const editing = self.data.view == .edit_comment; 2311 switch (src.repo_kind) { 2312 inline else => |repo_kind| { 2313 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2314 defer any_repo.deinit(io, allocator); 2315 switch (any_repo) { 2316 inline else => |*repo| if (editing) { 2317 var comment_id: [evt.event_id_size]u8 = undefined; 2318 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 2319 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2320 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2321 } else { 2322 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2323 }, 2324 } 2325 }, 2326 } 2327 2328 body_input.clear(allocator); 2329 const route = ui.RoutablePage.repoCommentsRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2330 try self.session.navigate(route); 2331 } 2332 2333 // the terminal submit paths: the new form commits a new-issue event, the 2334 // edit form re-emits the selected issue's event with the form's content. 2335 // the web posts the forms to their routes instead. 2336 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 2337 if (self.data.view == .edit) { 2338 try self.submitEditedIssue(allocator); 2339 } else { 2340 try self.submitNewIssue(allocator); 2341 } 2342 } 2343 2344 // commit the new issue to the repo's events branch and navigate to it. 2345 // this is the terminal path; the web posts the form to the issue route, 2346 // so the wasm side never runs (or compiles) the repo access below. 2347 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 2348 if (comptime wasm) return; 2349 const io = self.session.io orelse return; 2350 const src = self.data.repo_source orelse return; 2351 const author = (try self.session.eventAuthor()) orelse return; 2352 2353 const form = self.issueForm() orelse return; 2354 const title_input = &form.children.values()[title_field_index].widget.text_input; 2355 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2356 const description_input = &form.children.values()[description_field_index].widget.text_input; 2357 2358 const title = try title_input.text(allocator); 2359 defer allocator.free(title); 2360 const tags = try tags_input.text(allocator); 2361 defer allocator.free(tags); 2362 const description = try description_input.text(allocator); 2363 defer allocator.free(description); 2364 2365 if (!evt.Issue.fieldsValid(title, tags)) return; 2366 2367 var id_bytes: [evt.event_id_size]u8 = undefined; 2368 io.random(&id_bytes); 2369 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2370 2371 const event = evt.EventWithId{ 2372 .id = event_id_hex, 2373 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2374 .author = author, 2375 .event = .{ .issue = .{ 2376 .title = title, 2377 .description = description, 2378 .tags = tags, 2379 } }, 2380 }; 2381 2382 switch (src.repo_kind) { 2383 inline else => |repo_kind| { 2384 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2385 defer any_repo.deinit(io, allocator); 2386 switch (any_repo) { 2387 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2388 } 2389 }, 2390 } 2391 2392 // wipe the form so a return visit starts fresh 2393 title_input.clear(allocator); 2394 tags_input.clear(allocator); 2395 description_input.clear(allocator); 2396 2397 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2398 try self.session.navigate(route); 2399 } 2400 2401 // re-emit the selected issue's event with the form's content (status 2402 // preserved), then reload the issue's page. this is the terminal path; 2403 // the web posts the form to the edit route. 2404 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2405 if (comptime wasm) return; 2406 const io = self.session.io orelse return; 2407 const src = self.data.repo_source orelse return; 2408 const entry = self.data.selectedIssue() orelse return; 2409 const author = (try self.session.eventAuthor()) orelse return; 2410 2411 const form = self.issueForm() orelse return; 2412 const title_input = &form.children.values()[title_field_index].widget.text_input; 2413 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2414 const description_input = &form.children.values()[description_field_index].widget.text_input; 2415 2416 const title = try title_input.text(allocator); 2417 defer allocator.free(title); 2418 const tags = try tags_input.text(allocator); 2419 defer allocator.free(tags); 2420 const description = try description_input.text(allocator); 2421 defer allocator.free(description); 2422 2423 if (!evt.Issue.fieldsValid(title, tags)) return; 2424 2425 var id_bytes: [evt.event_id_size]u8 = undefined; 2426 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2427 2428 switch (src.repo_kind) { 2429 inline else => |repo_kind| { 2430 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2431 defer any_repo.deinit(io, allocator); 2432 switch (any_repo) { 2433 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2434 .title = title, 2435 .tags = tags, 2436 .description = description, 2437 } }, author), 2438 } 2439 }, 2440 } 2441 2442 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2443 try self.session.navigate(route); 2444 } 2445 2446 fn removeIssue(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2447 const sel = self.detailed_index[index] orelse return; 2448 const entry = self.window(index).issues[sel]; 2449 2450 var id_bytes: [evt.event_id_size]u8 = undefined; 2451 _ = std.fmt.hexToBytes(&id_bytes, entry.id) catch return; 2452 try self.removeEvent(allocator, .issue, id_bytes); 2453 } 2454 2455 fn removeComment(self: *View, allocator: std.mem.Allocator, comment: *Comment.Item) !void { 2456 try self.removeEvent(allocator, .comment, comment.id); 2457 } 2458 2459 fn removeEvent(self: *View, allocator: std.mem.Allocator, kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2460 if (comptime wasm) return; 2461 const io = self.session.io orelse return; 2462 const src = self.data.repo_source orelse return; 2463 const author = (try self.session.eventAuthor()) orelse return; 2464 2465 switch (src.repo_kind) { 2466 inline else => |repo_kind| { 2467 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2468 defer any_repo.deinit(io, allocator); 2469 switch (any_repo) { 2470 inline else => |*repo| try evt.remove(repo_kind, repo.self_repo_opts, io, allocator, repo, &id, kind, author), 2471 } 2472 }, 2473 } 2474 2475 const id_hex = std.fmt.bytesToHex(id, .lower); 2476 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, kind, &id_hex) orelse return; 2477 try self.session.navigate(route); 2478 } 2479 2480 // flip the shown issue's status by re-emitting its event, then reload the 2481 // page rooted at the issue so the view reflects the change. this is the 2482 // terminal path; the web posts the button's form to the status route. 2483 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2484 if (comptime wasm) return; 2485 const io = self.session.io orelse return; 2486 const src = self.data.repo_source orelse return; 2487 const sel = self.detailed_index[index] orelse return; 2488 const entry = self.window(index).issues[sel]; 2489 const author = (try self.session.eventAuthor()) orelse return; 2490 2491 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2492 .open => .closed, 2493 .closed => .open, 2494 }; 2495 2496 var id_bytes: [evt.event_id_size]u8 = undefined; 2497 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2498 2499 switch (src.repo_kind) { 2500 inline else => |repo_kind| { 2501 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2502 defer any_repo.deinit(io, allocator); 2503 switch (any_repo) { 2504 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2505 } 2506 }, 2507 } 2508 2509 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2510 try self.session.navigate(route); 2511 } 2512 2513 // arrow keys move the tag selection; at the flow's edges focus crosses to 2514 // the neighboring widgets. 2515 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2516 const tf = self.tagFlow(index) orelse return; 2517 const cid = tf.focus.child_id orelse return; 2518 const cur = tf.indexOfFocusId(cid) orelse return; 2519 const count = tf.text_boxes.items.len; 2520 switch (key) { 2521 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2522 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2523 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2524 self.focusTag(index, tf, root_focus, i); 2525 } else { 2526 _ = self.moveDetailVertical(index, root_focus, false); 2527 }, 2528 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2529 self.focusTag(index, tf, root_focus, i); 2530 } else { 2531 _ = self.moveDetailVertical(index, root_focus, true); 2532 }, 2533 .home => self.focusTag(index, tf, root_focus, 0), 2534 .end => self.focusTag(index, tf, root_focus, count - 1), 2535 else => {}, 2536 } 2537 } 2538 2539 const title_child_index: usize = 0; 2540 const tags_child_index: usize = 2; 2541 2542 // the tool row's first child after the spacer that pushes it right 2543 const first_in_row_index: usize = 1; 2544 2545 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2546 const inner = self.detailInner(index); 2547 if (inner.children.count() <= tags_child_index) return null; 2548 return switch (inner.children.values()[tags_child_index].widget) { 2549 .tag_flow => |*tf| tf, 2550 else => null, 2551 }; 2552 } 2553 2554 fn tagsFocused(self: *View, index: usize) bool { 2555 const inner = self.detailInner(index); 2556 const cid = inner.getFocus().child_id orelse return false; 2557 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2558 } 2559 2560 fn toolRowFocused(self: *View, index: usize) bool { 2561 const outer = self.detailOuter(index); 2562 const cid = outer.getFocus().child_id orelse return false; 2563 return outer.children.getIndex(cid) == tool_row_index; 2564 } 2565 2566 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2567 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2568 // keep the tag visible on the terminal: its rect offset by the flow's 2569 // position in the pane. 2570 if (self.session.is_terminal and item < tf.rects.items.len) { 2571 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2572 var rect = tf.rects.items[item]; 2573 rect.x += flow_rect.x; 2574 rect.y += flow_rect.y; 2575 self.detailScroll(index).scrollToRect(rect); 2576 } 2577 } 2578 } 2579 2580 fn titleFocused(self: *View, index: usize) bool { 2581 const inner = self.detailInner(index); 2582 const cid = inner.getFocus().child_id orelse return false; 2583 return inner.children.getIndex(cid) == title_child_index; 2584 } 2585 2586 fn authorFocused(self: *View, index: usize) bool { 2587 const inner = self.detailInner(index); 2588 const cid = inner.getFocus().child_id orelse return false; 2589 return cid == self.author_id[index]; 2590 } 2591 2592 fn descriptionFocused(self: *View, index: usize) bool { 2593 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2594 return cid == self.description_id[index]; 2595 } 2596 2597 // return to the tool row's last-focused button (the header when the 2598 // description page shows no row). 2599 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2600 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2601 root_focus.setFocus(cid); 2602 } 2603 2604 // enter the detail pane. an empty pane (no issues) can't be entered. 2605 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2606 if (self.detailInner(index).children.count() == 0) return; 2607 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2608 } 2609 2610 // return to the list. 2611 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2612 root_focus.setFocus(self.listScroll(index).getFocus().id); 2613 } 2614 2615 // cross to the header tabs above the stack. 2616 fn focusHeader(self: *View, root_focus: *Focus) void { 2617 root_focus.setFocus(self.box.children.keys()[header_index]); 2618 } 2619 2620 pub fn clearGrid(self: *View) void { 2621 self.box.clearGrid(); 2622 } 2623 2624 pub fn getGrid(self: View) ?Grid { 2625 return self.box.getGrid(); 2626 } 2627 2628 pub fn getFocus(self: *View) *Focus { 2629 return self.box.getFocus(); 2630 } 2631 2632 // for the parent's "scroll up at the top jumps to the header" check: at the 2633 // top only while the header tabs hold focus, so up from the split 2634 // crosses into the tabs first. 2635 pub fn getSelectedIndex(self: *View) ?usize { 2636 return if (self.headerActive()) 0 else 1; 2637 } 2638 }; 2639 2640 // the "a:" navigation link for `status`'s issues page filtered to the 2641 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2642 // a rooted url ignores `status` (it derives its view from the issue's own); 2643 // with an empty `id` this is the bare list link. 2644 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2645 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2646 const url = try route.toUrl(page_arena); 2647 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2648 } 2649 2650 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2651 // href is only followed with js off. 2652 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2653 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2654 const url = try route.toUrl(page_arena); 2655 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2656 } 2657 2658 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2659 // or the conflicts window when null, rooted at issue `id`. 2660 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2661 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2662 return conflictsLink(page_arena, data.identity, id); 2663 } 2664 2665 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2666 // first window). 2667 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2668 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2669 const url = try route.toUrl(page_arena); 2670 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2671 } 2672 2673 // the "a:" link to the whole-description page of issue `id` within `identity`. 2674 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2675 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2676 const url = try route.toUrl(page_arena); 2677 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2678 } 2679 2680 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2681 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2682 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2683 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2684 const url = try route.toUrl(page_arena); 2685 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2686 } 2687 2688 // tabs switching between the issues page's views. 2689 pub const Header = struct { 2690 box: wgt.Box(ui.Widget), 2691 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2692 2693 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2694 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2695 errdefer box.deinit(allocator); 2696 2697 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2698 errdefer tab_ids.deinit(allocator); 2699 2700 const aa = session.page_arena.allocator(); 2701 2702 // a list tab per status, labeled with its listing's issue count 2703 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2704 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2705 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2706 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2707 try addTab(allocator, &box, &tab_ids, label, link); 2708 } 2709 2710 // tags tab, labeled with the active tag filter 2711 { 2712 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2713 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2714 const label = if (data.tag.len == 0) "tags" else blk: { 2715 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2716 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2717 }; 2718 try addTab(allocator, &box, &tab_ids, label, tags_link); 2719 } 2720 2721 // new-issue tab; an edit or resolve url shows its tab in this place 2722 { 2723 const route = switch (data.view) { 2724 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2725 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2726 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2727 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2728 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2729 }; 2730 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2731 const label: []const u8 = switch (data.view) { 2732 .edit => "edit", 2733 .new_comment => "reply", 2734 .edit_comment => "edit", 2735 .resolve => "resolve", 2736 else => "new", 2737 }; 2738 try addTab(allocator, &box, &tab_ids, label, link); 2739 } 2740 2741 // conflicts tab, labeled with the conflict count 2742 if (data.conflicts.count > 0) { 2743 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2744 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2745 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2746 try addTab(allocator, &box, &tab_ids, label, link); 2747 } 2748 2749 var self = Header{ .box = box, .tab_ids = tab_ids }; 2750 // the tab matching the page's view is selected initially. 2751 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2752 return self; 2753 } 2754 2755 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2756 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2757 errdefer text_box.deinit(allocator); 2758 text_box.getFocus().focusable = true; 2759 text_box.getFocus().kind = .{ .custom = link }; 2760 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2761 try box.children.put(allocator, text_box.getFocus().id, .{ 2762 .widget = .{ .text_box = text_box }, 2763 .rect = null, 2764 .min_size = .{ .width = label.len + 2, .height = null }, 2765 }); 2766 } 2767 2768 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2769 self.box.deinit(allocator); 2770 self.tab_ids.deinit(allocator); 2771 } 2772 2773 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2774 self.clearGrid(); 2775 // only the selected tab shows its border 2776 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2777 switch (child.widget) { 2778 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2779 else => {}, 2780 } 2781 } 2782 try self.box.build(allocator, constraint, root_focus); 2783 } 2784 2785 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2786 _ = allocator; 2787 const current_tab = self.currentTabIndex() orelse return; 2788 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2789 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2790 } 2791 } 2792 2793 pub fn clearGrid(self: *Header) void { 2794 self.box.clearGrid(); 2795 } 2796 2797 pub fn getGrid(self: Header) ?Grid { 2798 return self.box.getGrid(); 2799 } 2800 2801 pub fn getFocus(self: *Header) *Focus { 2802 return self.box.getFocus(); 2803 } 2804 2805 pub fn getSelectedIndex(self: Header) ?usize { 2806 return self.currentTabIndex(); 2807 } 2808 2809 fn currentTabIndex(self: Header) ?usize { 2810 const child_id = self.box.focus.child_id orelse return null; 2811 return self.tab_ids.getIndex(child_id); 2812 } 2813 }; 2814