╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
132 133 // the issue `selected_id` names, in whichever window holds it. 134 pub fn selectedIssue(self: *const Self) ?*const IssueWithId { 135 if (self.selected_id.len == 0) return null; 136 for ([_]*const Window{ &self.open, &self.closed }) |win| { 137 for (win.issues) |*entry| { 138 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 139 } 140 } 141 return null; 142 } 143 144 // an empty listing, for the wasm / no-repo paths. 145 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, tag: []const u8, selected_id: []const u8, comment_id: []const u8, comments_start: usize, theirs_picks: []const u8, view: ui.RoutablePage.IssuesView) !Self { 146 return .{ 147 .identity = try aa.dupe(u8, identity), 148 .tag = try aa.dupe(u8, tag), 149 .selected_id = try aa.dupe(u8, selected_id), 150 .comment_id = try aa.dupe(u8, comment_id), 151 .comments_start = comments_start, 152 .open = .empty, 153 .closed = .empty, 154 .conflicts = .empty, 155 .theirs_picks = try aa.dupe(u8, theirs_picks), 156 // a description url shows a status list, the issue's own status 157 // picking it once the issue is read; conflicts and resolve urls also 158 // start there, upgraded by init when the conflict data exists. 159 .view = switch (view) { 160 .description, .conflicts, .resolve => .open, 161 else => view, 162 }, 163 .description_page = view == .description, 164 .tags = &.{}, 165 }; 166 } 167 168 // read one window per status of an opened repo's issues (filtered to `tag` 169 // when set), ordered by creation time (newest first). the window of the issue 170 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 171 // event db next to it (synced from the events branch on each page build); a 172 // xit repo reads its own db. 173 pub fn init( 174 comptime repo_kind: rp.RepoKind, 175 comptime repo_opts: rp.RepoOpts(repo_kind), 176 arena: *std.heap.ArenaAllocator, 177 repo: *rp.Repo(repo_kind, repo_opts), 178 io: std.Io, 179 // the admin db's moment, for resolving author emails to user names (null 180 // in local mode, which has no users) 181 admin_moment: ?evt.AdminDB.HashMap(.read_only), 182 identity: []const u8, 183 tag: []const u8, 184 selected_id: []const u8, 185 comment_id: []const u8, 186 comments_start: usize, 187 theirs_picks: []const u8, 188 view: ui.RoutablePage.IssuesView, 189 ) !Self { 190 const empty = try emptyResult(arena.allocator(), identity, tag, selected_id, comment_id, comments_start, theirs_picks, view); 191 192 const aa = arena.allocator(); 193 const DB = evt.EventDB(repo_opts.hash); 194 const rooted = empty.selected_id.len != 0; 195 const tagged = empty.tag.len != 0; 196 // an explicitly named issue or tag that doesn't exist is a bad url 197 // (NotFound -> 404); the bare route falls through to an empty listing. 198 const strict = rooted or tagged; 199 200 // a repo with no consumed events has no moment yet. 201 const gpa = arena.child_allocator; 202 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, gpa, repo.core.repo_dir) else null; 203 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 204 const haxy_moment = (if (event_db_maybe) |*event_db| 205 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 206 else if (repo_kind == .git) 207 return empty 208 else 209 evt.currentMoment(repo_opts, repo)) catch { 210 if (strict) return error.NotFound; 211 return empty; 212 }; 213 214 // the sorted sets to window, per status: the tag's sets when filtered, 215 // else the top-level per-status sets. a missing set is an empty window. 216 var open_set: ?DB.SortedSet(.read_only) = null; 217 var closed_set: ?DB.SortedSet(.read_only) = null; 218 if (tagged) { 219 const tag_to_issues_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag+status->issue-id-set")) orelse return error.NotFound; 220 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 221 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.tag)); 222 open_set = try tagStatusSet(DB, tag_to_issues, decoded, .open); 223 closed_set = try tagStatusSet(DB, tag_to_issues, decoded, .closed); 224 // a tag no issue carries is a bad url 225 if (open_set == null and closed_set == null) return error.NotFound; 226 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "status->issue-id-set"))) |status_to_issues_cursor| { 227 const status_to_issues = try DB.SortedMap(.read_only).init(status_to_issues_cursor); 228 open_set = try statusSet(DB, status_to_issues, .open); 229 closed_set = try statusSet(DB, status_to_issues, .closed); 230 } else if (rooted) return error.NotFound; 231 232 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse { 233 if (strict) return error.NotFound; 234 return empty; 235 }; 236 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 237 238 // the conflicted issues' container: a set view for membership and 239 // windowing, a map view for the resolve entry, over the same cursor. 240 var conflict_set: ?DB.SortedSet(.read_only) = null; 241 var conflicts_map: ?DB.SortedMap(.read_only) = null; 242 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.conflicts_key))) |cursor| { 243 conflict_set = try DB.SortedSet(.read_only).init(cursor); 244 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 245 } 246 247 // a named issue roots its own status's window at itself (the conflicts 248 // view's window instead); the other windows start at the beginning. 249 var resolved_view = empty.view; 250 var open_root: ?[]const u8 = null; 251 var closed_root: ?[]const u8 = null; 252 var conflicts_root: ?[]const u8 = null; 253 var conflict_data: ?Conflict = null; 254 if (rooted) { 255 if (empty.selected_id.len != evt.event_id_size * 2) return error.NotFound; 256 var id_bytes: [evt.event_id_size]u8 = undefined; 257 _ = std.fmt.hexToBytes(&id_bytes, empty.selected_id) catch return error.NotFound; 258 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 259 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 260 const issue_event = try evt.read(evt.Issue.Record, DB, repo_opts.hash, arena, issue_map); 261 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(issue_event.created_ts, &id_bytes)); 262 263 if (view == .conflicts) { 264 // the id roots the conflicts window, so it must be conflicted 265 const set = conflict_set orelse return error.NotFound; 266 if (!try set.contains(order_key)) return error.NotFound; 267 conflicts_root = order_key; 268 } else { 269 // the named issue must be in its windowed set (a tag url can name 270 // an issue that doesn't carry the tag). 271 const set = (switch (issue_event.event.status) { 272 .open => open_set, 273 .closed => closed_set, 274 }) orelse return error.NotFound; 275 if (!try set.contains(order_key)) return error.NotFound; 276 277 switch (issue_event.event.status) { 278 .open => open_root = order_key, 279 .closed => closed_root = order_key, 280 } 281 // form urls keep their view; otherwise the issue's status picks it. 282 if (view != .edit and view != .new_comment and view != .edit_comment) resolved_view = switch (issue_event.event.status) { 283 .open => .open, 284 .closed => .closed, 285 }; 286 287 // a resolve url shows the resolve view only while the issue still 288 // has a conflict entry; otherwise the status list stands. the 289 // wasm client renders from the snapshot, so the diff machinery 290 // this pulls in is gated out of its build. 291 if (comptime !wasm) { 292 if (view == .resolve) { 293 if (conflicts_map) |map| { 294 if (try map.getCursor(order_key)) |conflict_cursor| { 295 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 296 conflict_data = try readConflict(repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, issue_event, conflict_entry); 297 resolved_view = .resolve; 298 } 299 } 300 } 301 } 302 } 303 } 304 305 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 306 const open_window = try loadWindow(repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, open_set, open_root, conflict_set, empty.selected_id, thread_comments_start); 307 const closed_window = try loadWindow(repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, closed_set, closed_root, conflict_set, empty.selected_id, thread_comments_start); 308 const conflicts_window = try loadWindow(repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, conflict_set, conflicts_root, conflict_set, empty.selected_id, thread_comments_start); 309 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 310 311 const comment_page = if (empty.comment_id.len == 0) 312 null 313 else 314 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start); 315 316 // every tag in the repo, in the tag map's sorted order. the keys are 317 // "tag,status", so a tag's entries are adjacent and dedup by prefix. 318 var tags: std.ArrayList([]const u8) = .empty; 319 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag+status->issue-id-set"))) |tag_to_issues_cursor| { 320 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 321 var tag_iter = try tag_to_issues.iterator(); 322 while (try tag_iter.next()) |kv_pair_cursor| { 323 if (tags.items.len == max_tags) break; 324 var kv_cursor = kv_pair_cursor; 325 const kv_pair = try kv_cursor.readKeyValuePair(); 326 const key = try kv_pair.key_cursor.readBytesAlloc(aa, null); 327 const space = std.mem.indexOfScalar(u8, key, ' ') orelse continue; 328 const key_tag = key[0..space]; 329 if (tags.getLastOrNull()) |last| { 330 if (std.mem.eql(u8, last, key_tag)) continue; 331 } 332 try tags.append(aa, key_tag); 333 } 334 } 335 336 return .{ 337 .identity = empty.identity, 338 .tag = empty.tag, 339 .selected_id = empty.selected_id, 340 .comment_id = empty.comment_id, 341 .comments_start = comments_start, 342 .comment_page = comment_page, 343 .open = open_window, 344 .closed = closed_window, 345 .conflicts = conflicts_window, 346 .conflict = conflict_data, 347 .theirs_picks = empty.theirs_picks, 348 .view = resolved_view, 349 .description_page = empty.description_page, 350 .tags = tags.items, 351 }; 352 } 353 354 // `status`'s sorted set within `statuses` (null when the status has no issues) 355 fn statusSet( 356 comptime DB: type, 357 statuses: DB.SortedMap(.read_only), 358 status: evt.Issue.Status, 359 ) !?DB.SortedSet(.read_only) { 360 const cursor = (try statuses.getCursor(@tagName(status))) orelse return null; 361 return try DB.SortedSet(.read_only).init(cursor); 362 } 363 364 // the set at `tag_to_issues`'s "tag,status" key (null when no issue carries 365 // `tag` with `status`, or the tag is too long to exist) 366 fn tagStatusSet( 367 comptime DB: type, 368 tag_to_issues: DB.SortedMap(.read_only), 369 tag: []const u8, 370 status: evt.Issue.Status, 371 ) !?DB.SortedSet(.read_only) { 372 var key_buffer: evt.Issue.TagStatusKey = undefined; 373 const key = evt.Issue.tagStatusKey(&key_buffer, tag, status) catch return null; 374 const cursor = (try tag_to_issues.getCursor(key)) orelse return null; 375 return try DB.SortedSet(.read_only).init(cursor); 376 } 377 378 // read one window of `set_maybe` (null = an empty listing) starting at 379 // `root_key` (null = the beginning) 380 fn loadWindow( 381 comptime hash_kind: hash.HashKind, 382 arena: *std.heap.ArenaAllocator, 383 admin_moment: ?evt.AdminDB.HashMap(.read_only), 384 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 385 event_id_to_issue: evt.EventDB(hash_kind).HashMap(.read_only), 386 set_maybe: ?evt.EventDB(hash_kind).SortedSet(.read_only), 387 root_key: ?[]const u8, 388 conflict_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 389 selected_id: []const u8, 390 comments_start: usize, 391 ) !Window { 392 const set = set_maybe orelse return .empty; 393 const DB = evt.EventDB(hash_kind); 394 const aa = arena.allocator(); 395 396 // seek once to the window start: the root key, or the set's first entry. 397 var prev_id: ?[]const u8 = null; 398 var iter = if (root_key) |key| blk: { 399 // the previous window starts page_size ranks back ("" = the first 400 // window, linked as the bare list route). 401 const rank = try set.rank(key); 402 if (rank > 0 and rank <= page_size) { 403 prev_id = ""; 404 } else if (rank > page_size) { 405 const kv = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 406 var prev_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 407 _ = try kv.key_cursor.readBytes(&prev_key); 408 const prev_hex = std.fmt.bytesToHex(prev_key[@sizeOf(u64)..].*, .lower); 409 prev_id = try aa.dupe(u8, &prev_hex); 410 } 411 break :blk try set.iteratorFrom(key); 412 } else try set.iteratorFromIndex(0); 413 414 // collect this window's issues, plus a peek at the one after it (its id is 415 // the next window's start). the trailing bytes of each set key are the 416 // issue event id. 417 var issues: std.ArrayList(IssueWithId) = .empty; 418 var next_id: ?[]const u8 = null; 419 while (try iter.next()) |id_cursor_val| { 420 var id_cursor = id_cursor_val; 421 const id_kv = try id_cursor.readKeyValuePair(); 422 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 423 _ = try id_kv.key_cursor.readBytes(&order_key); 424 const id_hex = std.fmt.bytesToHex(order_key[@sizeOf(u64)..].*, .lower); 425 if (issues.items.len == page_size) { 426 next_id = try aa.dupe(u8, &id_hex); 427 break; 428 } 429 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(hash_kind, order_key[@sizeOf(u64)..])) orelse continue; 430 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 431 const issue_event = try evt.read(evt.Issue.Record, DB, hash_kind, arena, issue_map); 432 try issues.append(aa, .{ 433 .id = try aa.dupe(u8, &id_hex), 434 .issue = issue_event, 435 .author = try ui.Author.initFromEmail(admin_moment, arena, issue_event.author_email), 436 .conflicted = if (conflict_set) |cs| try cs.contains(&order_key) else false, 437 .comments = try Comment.loadWindow( 438 hash_kind, 439 arena, 440 admin_moment, 441 haxy_moment, 442 evt.Comment.thread_id_to_comment_id_set_key, 443 &id_hex, 444 if (std.mem.eql(u8, &id_hex, selected_id)) comments_start else 0, 445 ), 446 }); 447 } 448 449 return .{ 450 .issues = issues.items, 451 .prev_id = prev_id, 452 .next_id = next_id, 453 .count = @intCast(try set.count()), 454 }; 455 } 456 457 // the selected issue's conflict entry, shaped for the resolve view. `ours` is 458 // the live record. 459 fn readConflict( 460 comptime repo_kind: rp.RepoKind, 461 comptime repo_opts: rp.RepoOpts(repo_kind), 462 arena: *std.heap.ArenaAllocator, 463 repo: *rp.Repo(repo_kind, repo_opts), 464 io: std.Io, 465 admin_moment: ?evt.AdminDB.HashMap(.read_only), 466 haxy_moment: evt.EventDB(repo_opts.hash).HashMap(.read_only), 467 id_bytes: *const [evt.event_id_size]u8, 468 ours: evt.Issue.Record, 469 conflict_entry: evt.EventDB(repo_opts.hash).HashMap(.read_only), 470 ) !Conflict { 471 const DB = evt.EventDB(repo_opts.hash); 472 const aa = arena.allocator(); 473 const gpa = arena.child_allocator; 474 475 const fields_cursor = (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key))) orelse return error.NotFound; 476 const conflicted_fields = try fields_cursor.readBytesAlloc(aa, null); 477 478 const their_cursor = (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key))) orelse return error.NotFound; 479 const theirs = try evt.read(evt.Issue.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(their_cursor)); 480 481 // absent when both sides created the issue independently 482 var base: ?evt.Issue.Record = null; 483 if (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.base_record_key))) |base_cursor| { 484 base = try evt.read(evt.Issue.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(base_cursor)); 485 } 486 487 // each side's field->oid map, for attributing the versions; a field a 488 // merge left unattributed reads as an unknown author 489 var their_field_oids: ?DB.SortedMap(.read_only) = null; 490 if (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_field_to_oid_key))) |cursor| { 491 their_field_oids = try DB.SortedMap(.read_only).init(cursor); 492 } 493 var our_field_oids: ?DB.SortedMap(.read_only) = null; 494 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.id_to_field_to_oid_key))) |map_cursor| { 495 const id_to_field_to_oid = try DB.HashMap(.read_only).init(map_cursor); 496 if (try id_to_field_to_oid.getCursor(hash.hashInt(repo_opts.hash, id_bytes))) |cursor| { 497 our_field_oids = try DB.SortedMap(.read_only).init(cursor); 498 } 499 } 500 501 var conflict = Conflict{}; 502 var field_iter = std.mem.splitScalar(u8, conflicted_fields, ' '); 503 while (field_iter.next()) |field| { 504 // status can't conflict (two changes of a two-value field agree) 505 const known = std.meta.stringToEnum(enum { title, tags, description }, field) orelse continue; 506 const our_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, our_field_oids, field)); 507 const their_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, their_field_oids, field)); 508 switch (known) { 509 .title => conflict.title = .{ 510 .ours = .{ .text = ours.event.title, .author = our_author }, 511 .theirs = .{ .text = theirs.event.title, .author = their_author }, 512 }, 513 .tags => conflict.tags = .{ 514 .ours = .{ .text = ours.event.tags, .author = our_author }, 515 .theirs = .{ .text = theirs.event.tags, .author = their_author }, 516 }, 517 .description => conflict.description = .{ 518 .chunks = try diff3.chunks(io, gpa, arena, if (base) |b| b.event.description else "", ours.event.description, theirs.event.description), 519 .ours_author = our_author, 520 .theirs_author = their_author, 521 }, 522 } 523 } 524 return conflict; 525 } 526 527 // the oid `field` maps to in a field->oid map, or null 528 fn readFieldOid( 529 comptime hash_kind: hash.HashKind, 530 field_oids_maybe: ?evt.EventDB(hash_kind).SortedMap(.read_only), 531 field: []const u8, 532 ) !?[hash.byteLen(hash_kind)]u8 { 533 const field_oids = field_oids_maybe orelse return null; 534 const cursor = (try field_oids.getCursor(field)) orelse return null; 535 var oid: [hash.byteLen(hash_kind)]u8 = undefined; 536 _ = try cursor.readBytes(&oid); 537 return oid; 538 } 539 540 // the author of the event commit `oid_maybe` names 541 fn oidAuthor( 542 comptime repo_kind: rp.RepoKind, 543 comptime repo_opts: rp.RepoOpts(repo_kind), 544 arena: *std.heap.ArenaAllocator, 545 repo: *rp.Repo(repo_kind, repo_opts), 546 io: std.Io, 547 admin_moment: ?evt.AdminDB.HashMap(.read_only), 548 oid_maybe: ?[hash.byteLen(repo_opts.hash)]u8, 549 ) !ui.Author { 550 const oid = oid_maybe orelse return .unknown; 551 const gpa = arena.child_allocator; 552 var start_oids = [_][hash.hexLen(repo_opts.hash)]u8{std.fmt.bytesToHex(oid, .lower)}; 553 var commit_iter = repo.log(io, gpa, start_oids[0..1]) catch return .unknown; 554 defer commit_iter.deinit(); 555 const commit_object = (commit_iter.next(gpa) catch return .unknown) orelse return .unknown; 556 defer commit_object.deinit(); 557 return try ui.Author.init(admin_moment, arena, commit_object.content.commit.metadata.author orelse ""); 558 } 559 560 pub const View = struct { 561 // a vertical box: the header tabs on top, then a stack holding a 562 // master-detail split (issue list + description pane) per status list, 563 // plus the tags view. 564 box: wgt.Box(ui.Widget), // vert: [header_index] = tabs, [stack_index] = stack 565 data: *const Self, 566 session: *ui.Session, 567 // per-split state, indexed like the stack's split children: the issue the 568 // pane shows, and its description and author text boxes' focus ids. 569 detailed_index: [stack_child_max]?usize, 570 description_id: [stack_child_max]?usize, 571 author_id: [stack_child_max]?usize, 572 573 const header_index: usize = 0; 574 const stack_index: usize = 1; 575 // indices within the stack, 1:1 with the header tabs. 576 const open_view_index: usize = 0; 577 const closed_view_index: usize = 1; 578 const tags_view_index: usize = 2; 579 // the new-issue form, or the edit, comment, or resolve form when the page was 580 // loaded at one of their urls. 581 const form_view_index: usize = 3; 582 // the conflicts split; the tab and stack child exist only when the repo 583 // has conflicted issues. 584 const conflicts_view_index: usize = 4; 585 const stack_child_max: usize = conflicts_view_index + 1; 586 // the leading stack children that are status splits; the conflicts split 587 // sits apart at conflicts_view_index. 588 const split_count: usize = 2; 589 // indices within a split (the horizontal box inside the stack). 590 const list_index: usize = 0; 591 const detail_index: usize = 1; 592 const list_max_width: usize = 40; 593 const detail_min_width: usize = 40; 594 // indices within the issue form. 595 const title_field_index: usize = 0; 596 const tags_field_index: usize = 1; 597 const description_field_index: usize = 2; 598 const submit_field_index: usize = 3; 599 const comment_author_field_index: usize = 0; 600 const comment_body_field_index: usize = 1; 601 const comment_submit_field_index: usize = 2; 602 603 fn viewIndex(view: ui.RoutablePage.IssuesView) usize { 604 return switch (view) { 605 .open => open_view_index, 606 .closed => closed_view_index, 607 .tags => tags_view_index, 608 .new, .edit, .new_comment, .edit_comment, .resolve => form_view_index, 609 .conflicts => conflicts_view_index, 610 // init resolves a description url to its issue's status list 611 .description => unreachable, 612 }; 613 } 614 615 // the status a status split lists; the conflicts split has none (its 616 // issues carry their own). 617 fn splitStatus(index: usize) evt.Issue.Status { 618 return if (index == open_view_index) .open else .closed; 619 } 620 621 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 622 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 623 errdefer outer.deinit(allocator); 624 625 // the tabs at the top. 626 { 627 var hdr = try Header.init(allocator, session, data); 628 errdefer hdr.deinit(allocator); 629 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = .{ .repo_issues_header = hdr }, .rect = null, .min_size = null }); 630 } 631 632 // the stack enters `outer` before its children enter it, so an error 633 // frees each child exactly once. 634 { 635 var stack = try wgt.Stack(ui.Widget).init(allocator); 636 errdefer stack.deinit(allocator); 637 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 638 } 639 const stack = &outer.children.values()[stack_index].widget.stack; 640 641 // a master-detail split per status list. 642 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 643 var split = try initSplit(allocator, session, data, status); 644 errdefer split.deinit(allocator); 645 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 646 } 647 648 // the tags view 649 { 650 var tf = try ui.TagFlow.init(allocator); 651 errdefer tf.deinit(allocator); 652 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 653 defer items.deinit(allocator); 654 // when filtered, the first item clears the filter 655 if (data.tag.len != 0) 656 try items.append(allocator, .{ .text = "✕", .link = try issuesLink(session.page_arena, data.identity, .open, "", "") }); 657 for (data.tags) |tag| 658 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, .open, tag) }); 659 try tf.setItems(allocator, items.items); 660 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 661 } 662 663 // the new-issue form, or — on an edit, comment, or resolve url — that form in 664 // its place, prefilled with the selected issue's content. a 665 // logged-out session can't create events, so the unauthorized view 666 // stands in. 667 if (session.data.is_local or session.data.user_id != null) { 668 if (data.view == .new_comment or data.view == .edit_comment) { 669 const editing = data.view == .edit_comment; 670 const route = if (editing) 671 ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 672 else 673 ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 674 const page_url = try route.toUrl(session.page_arena); 675 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 676 const page = data.comment_page; 677 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 678 const parent_route = if (editing) 679 if (top_level) 680 ui.RoutablePage.repoIssueCommentsRoute(data.identity, data.selected_id, 0) 681 else if (page) |p| 682 ui.RoutablePage.repoCommentsRoute(data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 683 else 684 null 685 else if (page) |p| 686 ui.RoutablePage.repoCommentsRoute(data.identity, data.selected_id, &p.selected.id, 0) 687 else 688 ui.RoutablePage.repoIssueCommentsRoute(data.identity, data.selected_id, 0); 689 const parent_author: ui.Author = if (editing) 690 if (top_level) 691 if (data.selectedIssue()) |entry| entry.author else .unknown 692 else if (page) |p| 693 p.selected.parent_author orelse .unknown 694 else 695 .unknown 696 else if (page) |p| 697 p.selected.author 698 else if (data.selectedIssue()) |entry| 699 entry.author 700 else 701 .unknown; 702 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 703 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 704 errdefer form.deinit(allocator); 705 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 706 } else if (data.view == .resolve) { 707 // on the web the page grows to the form's height and the 708 // browser scrolls it; the terminal scrolls the form itself 709 var form_widget: ui.Widget = blk: { 710 var form = try initResolveForm(allocator, session, data); 711 errdefer form.deinit(allocator); 712 break :blk if (session.is_terminal) 713 .{ .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 714 else 715 .{ .box = form }; 716 }; 717 errdefer form_widget.deinit(allocator); 718 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 719 } else { 720 const aa = session.page_arena.allocator(); 721 const route = if (data.view == .edit) 722 ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong 723 else 724 ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 725 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 726 const issue = if (data.view == .edit) 727 (if (data.selectedIssue()) |entry| &entry.issue else null) 728 else 729 null; 730 var form = try initIssueForm(allocator, session, action, issue); 731 errdefer form.deinit(allocator); 732 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 733 } 734 } else { 735 var message = try ui.Unauthorized.View.init(allocator); 736 errdefer message.deinit(allocator); 737 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 738 } 739 740 // the conflicts split; its tab exists only when the repo has 741 // conflicts, so skip the child too to keep the stack 1:1 with the 742 // tabs. 743 if (data.conflicts.count > 0) { 744 var split = try initSplit(allocator, session, data, null); 745 errdefer split.deinit(allocator); 746 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 747 } 748 749 // the stack starts on the page's view. 750 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 751 752 // focus entering the view lands on the tabs first. 753 outer.getFocus().child_id = outer.children.keys()[header_index]; 754 755 return .{ 756 .box = outer, 757 .data = data, 758 .session = session, 759 .detailed_index = @splat(null), 760 .description_id = @splat(null), 761 .author_id = @splat(null), 762 }; 763 } 764 765 // the master-detail split showing `status`'s window, or the conflicts 766 // window when null. 767 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?evt.Issue.Status) !wgt.Box(ui.Widget) { 768 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 769 errdefer box.deinit(allocator); 770 771 const win = if (status_maybe) |status| data.window(status) else &data.conflicts; 772 773 // the issue list (one focusable row per title), plus a "next" link that 774 // reloads the page rooted at the following issue. 775 { 776 var list_scroll = blk: { 777 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 778 errdefer list_box.deinit(allocator); 779 if (win.prev_id) |prev| 780 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, prev)); 781 for (win.issues) |entry| { 782 // conflicts take precedence over the comment count 783 const label: []const u8 = if (entry.conflicted and status_maybe != null) 784 " conflict " 785 else if (entry.comments.count > 0) 786 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 787 else 788 ""; 789 try addRow(allocator, &list_box, entry.issue.event.title, label, try issueRowLink(session.page_arena, data.identity, entry.id)); 790 } 791 if (win.next_id) |next| 792 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, next)); 793 // select the window's first issue (past a leading "previous" 794 // row) so its description shows on load. 795 if (win.issues.len > 0) 796 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 797 else if (list_box.children.count() > 0) 798 list_box.getFocus().child_id = list_box.children.keys()[0]; 799 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 800 }; 801 errdefer list_scroll.deinit(allocator); 802 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 } }); 803 } 804 805 // the detail pane — a frame around a scroll of the description 806 { 807 var detail_outer = blk: { 808 var detail_scroll = blk2: { 809 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 810 errdefer detail_inner.deinit(allocator); 811 // fill the pane (content top-left, scroll bar pinned to the 812 // edge) rather than shrinking to the description. 813 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 814 }; 815 errdefer detail_scroll.deinit(allocator); 816 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 817 errdefer frame.deinit(allocator); 818 // the tool row sits above the scroll (populateDetail fills 819 // it per issue) so it can't scroll out from under the web 820 // overlay <form>, whose position doesn't track pane scrolling. 821 { 822 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 823 errdefer row.deinit(allocator); 824 try frame.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 825 } 826 // the frame's selected child is its scroll, so the focus chain 827 // reaches the description (populateDetail points the scroll's 828 // inner box at it), letting focus recovery descend into the pane. 829 frame.getFocus().child_id = detail_scroll.getFocus().id; 830 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 831 break :blk frame; 832 }; 833 errdefer detail_outer.deinit(allocator); 834 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 835 } 836 837 box.getFocus().child_id = box.children.keys()[list_index]; 838 return box; 839 } 840 841 // an issue form: title/tags/description inputs and a submit button, 842 // prefilled from `issue` when given. its form: subtree makes the web 843 // overlay wrap them in a <form> POSTing to `action`'s route. 844 fn initIssueForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, issue: ?*const evt.Issue.Record) !wgt.Box(ui.Widget) { 845 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 846 errdefer box.deinit(allocator); 847 box.getFocus().kind = .{ .custom = action }; 848 849 { 850 var title = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 851 errdefer title.deinit(allocator); 852 title.getFocus().focusable = true; 853 if (issue) |i| try title.setContent(allocator, i.event.title); 854 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 855 } 856 857 { 858 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 }); 859 errdefer tags.deinit(allocator); 860 tags.getFocus().focusable = true; 861 if (issue) |i| try tags.setContent(allocator, i.event.tags); 862 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 863 } 864 865 { 866 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 } }); 867 errdefer description.deinit(allocator); 868 description.getFocus().focusable = true; 869 if (issue) |i| try description.setContent(allocator, i.event.description); 870 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 871 } 872 873 try addSubmitButton(allocator, &box); 874 875 box.getFocus().child_id = box.children.keys()[title_field_index]; 876 return box; 877 } 878 879 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) { 880 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 881 errdefer box.deinit(allocator); 882 box.getFocus().kind = .{ .custom = action }; 883 884 { 885 var parent = try ui.authorBox(allocator, session.page_arena, author); 886 errdefer parent.deinit(allocator); 887 parent.options.label = " replying to "; 888 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 889 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 890 } 891 892 { 893 var body = try wgt.TextInput(ui.Widget).init(allocator, .{ 894 .label = " comment ", 895 .name = "body", 896 .visible_width = null, 897 .rounded_corners = true, 898 .render_content = session.is_terminal, 899 .multiline = true, 900 .visible_height = 5, 901 .scroll = .{ .fill = true }, 902 }); 903 errdefer body.deinit(allocator); 904 body.getFocus().focusable = true; 905 if (initial_body) |text| try body.setContent(allocator, text); 906 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 907 } 908 909 try addSubmitButtonLabeled(allocator, &box, "submit"); 910 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 911 return box; 912 } 913 914 // a form's submit button, then a spacer absorbing the leftover 915 // min-height the box hands its last child, so the button keeps its 916 // natural height 917 fn addSubmitButton(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 918 return addSubmitButtonLabeled(allocator, box, "submit"); 919 } 920 921 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8) !void { 922 { 923 var submit = try ui.SubmitButton.initLabeled(allocator, label); 924 errdefer submit.deinit(allocator); 925 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 926 } 927 { 928 var spacer = try ui.Spacer.init(allocator); 929 errdefer spacer.deinit(allocator); 930 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 931 } 932 } 933 934 // the conflict resolve form. the "use this" links are navigations that 935 // flip the url's theirs: pick, reloading the prefills; one submit 936 // settles every conflict. the form: subtree makes the web overlay POST 937 // to the resolve route. 938 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(ui.Widget) { 939 const aa = session.page_arena.allocator(); 940 // init only picks the resolve view once it has read the conflict 941 const conflict = if (data.conflict) |*c| c else unreachable; 942 943 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 944 errdefer box.deinit(allocator); 945 box.getFocus().kind = .{ .custom = if (data.identity.len == 0) 946 try std.fmt.allocPrint(aa, "form:/issue:{s}/resolve", .{data.selected_id}) 947 else 948 try std.fmt.allocPrint(aa, "form:/repo/{s}/issue:{s}/resolve", .{ data.identity, data.selected_id }) }; 949 950 if (conflict.title) |*fc| { 951 try addLabel(allocator, &box, "title conflict:"); 952 try addFieldConflict(allocator, &box, session, data, "title", fc); 953 } 954 if (conflict.tags) |*fc| { 955 try addLabel(allocator, &box, "tags conflict:"); 956 try addFieldConflict(allocator, &box, session, data, "tags", fc); 957 } 958 959 if (conflict.description) |*desc| { 960 try addLabel(allocator, &box, "description conflict:"); 961 var hunk_index: usize = 0; 962 for (desc.chunks, 0..) |*chunk, chunk_index| { 963 switch (chunk.*) { 964 .same => |text| { 965 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 966 errdefer tb.deinit(allocator); 967 tb.getFocus().focusable = true; 968 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 969 }, 970 // an auto-resolved chunk: only one side changed it, so its 971 // version stands, shown distinctly with its author 972 .auto => |auto| { 973 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 974 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 975 const label = switch (author) { 976 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 977 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 978 }; 979 var tb = try wgt.TextBox(ui.Widget).init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 980 errdefer tb.deinit(allocator); 981 tb.getFocus().focusable = true; 982 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 983 }, 984 .conflict => |hunk| { 985 // a blank row on each side sets the group apart from 986 // the flowing shared and auto-resolved chunks 987 if (chunk_index > 0) try addGap(allocator, &box); 988 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 989 hunk_index += 1; 990 const picked_theirs = data.theirsPicked(name); 991 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 992 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 993 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 }); 994 errdefer resolution_input.deinit(allocator); 995 resolution_input.getFocus().focusable = true; 996 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 997 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 998 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 999 }, 1000 } 1001 } 1002 } 1003 1004 try addSubmitButton(allocator, &box); 1005 1006 box.getFocus().child_id = box.children.keys()[0]; 1007 return box; 1008 } 1009 1010 // one conflicted scalar field: both sides' version rows, then the 1011 // resolution input prefilled from the picked side. 1012 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 { 1013 const aa = session.page_arena.allocator(); 1014 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1015 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1016 1017 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 }); 1018 errdefer resolution_input.deinit(allocator); 1019 resolution_input.getFocus().focusable = true; 1020 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 1021 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1022 } 1023 1024 // a blank row setting a conflict group apart from its neighbors 1025 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 1026 var text = try wgt.Text(ui.Widget).init(allocator, " "); 1027 errdefer text.deinit(allocator); 1028 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1029 } 1030 1031 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), content: []const u8) !void { 1032 var label = try ui.SectionLabel.init(allocator, content); 1033 errdefer label.deinit(allocator); 1034 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1035 } 1036 1037 // one side of a conflict: the "use this" link on the left, the version 1038 // itself as a labeled read-only box. 1039 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1040 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1041 errdefer row.deinit(allocator); 1042 1043 { 1044 const use_label = "use this"; 1045 var use = try wgt.TextBox(ui.Widget).init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1046 errdefer use.deinit(allocator); 1047 use.getFocus().focusable = true; 1048 use.getFocus().kind = .{ .custom = link }; 1049 try row.children.put(allocator, use.getFocus().id, .{ 1050 .widget = .{ .text_box = use }, 1051 .rect = null, 1052 .min_size = .{ .width = use_label.len + 2, .height = null }, 1053 .max_size = .{ .width = use_label.len + 2, .height = null }, 1054 }); 1055 } 1056 1057 { 1058 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 }); 1059 errdefer tb.deinit(allocator); 1060 tb.getFocus().focusable = true; 1061 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1062 } 1063 1064 row.getFocus().child_id = row.children.keys()[0]; 1065 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1066 } 1067 1068 // the label of one side's version box, naming its author 1069 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1070 return switch (author) { 1071 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1072 .unknown => if (ours) " current version " else " their version ", 1073 }; 1074 } 1075 1076 // the a: link that reloads the resolve page with `name` picked from ours 1077 // or theirs, reprefilling its resolution input 1078 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1079 const aa = session.page_arena.allocator(); 1080 var picks: std.ArrayList(u8) = .empty; 1081 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1082 while (it.next()) |pick| { 1083 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1084 if (picks.items.len > 0) try picks.append(aa, ','); 1085 try picks.appendSlice(aa, pick); 1086 } 1087 if (theirs) { 1088 if (picks.items.len > 0) try picks.append(aa, ','); 1089 try picks.appendSlice(aa, name); 1090 } 1091 const route = ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1092 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1093 } 1094 1095 // the resolve form's page-constant initial value for the input `name`: 1096 // the picked side of its conflicted field or hunk 1097 fn resolvePrefill(self: *View, name: []const u8) ?[]const u8 { 1098 const conflict = if (self.data.conflict) |*c| c else return null; 1099 const theirs = self.data.theirsPicked(name); 1100 if (std.mem.eql(u8, name, "title")) { 1101 const fc = if (conflict.title) |*f| f else return null; 1102 return if (theirs) fc.theirs.text else fc.ours.text; 1103 } 1104 if (std.mem.eql(u8, name, "tags")) { 1105 const fc = if (conflict.tags) |*f| f else return null; 1106 return if (theirs) fc.theirs.text else fc.ours.text; 1107 } 1108 if (std.mem.startsWith(u8, name, "d")) { 1109 const desc = if (conflict.description) |*d| d else return null; 1110 const hunk_index = std.fmt.parseInt(usize, name[1..], 10) catch return null; 1111 var seen: usize = 0; 1112 for (desc.chunks) |chunk| switch (chunk) { 1113 .conflict => |hunk| { 1114 if (seen == hunk_index) return (if (theirs) hunk.theirs else hunk.ours) orelse ""; 1115 seen += 1; 1116 }, 1117 else => {}, 1118 }; 1119 } 1120 return null; 1121 } 1122 1123 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1124 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1125 errdefer row.deinit(allocator); 1126 row.getFocus().focusable = true; 1127 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1128 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1129 } 1130 1131 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 1132 self.box.deinit(allocator); 1133 } 1134 1135 fn header(self: *View) *Header { 1136 return &self.box.children.values()[header_index].widget.repo_issues_header; 1137 } 1138 1139 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 1140 return &self.box.children.values()[stack_index].widget.stack; 1141 } 1142 1143 // `index`'s master-detail split inside the stack. 1144 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1145 return &self.viewStack().children.values()[index].box; 1146 } 1147 1148 // the tags view's flow inside the stack. 1149 fn tagsView(self: *View) *ui.TagFlow { 1150 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1151 } 1152 1153 // the new-issue, edit, or resolve form inside the stack, or null when the 1154 // unauthorized view stands in for it. 1155 fn issueForm(self: *View) ?*wgt.Box(ui.Widget) { 1156 return switch (self.viewStack().children.values()[form_view_index]) { 1157 .box => |*box| box, 1158 // the resolve form sits inside a scroll on the terminal 1159 .scroll => |*scroll| &scroll.child.box, 1160 else => null, 1161 }; 1162 } 1163 1164 // the resolve form's scroll on the terminal (the web page scrolls itself) 1165 fn resolveScroll(self: *View) ?*wgt.Scroll(ui.Widget) { 1166 return switch (self.viewStack().children.values()[form_view_index]) { 1167 .scroll => |*scroll| scroll, 1168 else => null, 1169 }; 1170 } 1171 1172 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1173 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1174 } 1175 1176 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1177 return &self.listScroll(index).child.box; 1178 } 1179 1180 // the detail frame's children: the tool row above the scroll pane. 1181 const tool_row_index: usize = 0; 1182 const detail_scroll_index: usize = 1; 1183 1184 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 1185 return &self.resultsBox(index).children.values()[detail_index].widget.box; 1186 } 1187 1188 fn toolRow(self: *View, index: usize) *wgt.Box(ui.Widget) { 1189 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 1190 } 1191 1192 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1193 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 1194 } 1195 1196 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 1197 return &self.detailScroll(index).child.box; 1198 } 1199 1200 fn window(self: *View, index: usize) *const Window { 1201 if (index == conflicts_view_index) return &self.data.conflicts; 1202 return self.data.window(splitStatus(index)); 1203 } 1204 1205 fn stackSelectedIndex(self: *View) ?usize { 1206 const stack = self.viewStack(); 1207 const cid = stack.getFocus().child_id orelse return null; 1208 return stack.children.getIndex(cid); 1209 } 1210 1211 // the stack's selected master-detail split (null when the tags view or the 1212 // new-issue form shows). 1213 fn selectedSplitIndex(self: *View) ?usize { 1214 const idx = self.stackSelectedIndex() orelse return null; 1215 return if (idx < split_count or idx == conflicts_view_index) idx else null; 1216 } 1217 1218 fn detailActive(self: *View, index: usize) bool { 1219 const rb = self.resultsBox(index); 1220 const cid = rb.getFocus().child_id orelse return false; 1221 return rb.children.getIndex(cid) == detail_index; 1222 } 1223 1224 fn headerActive(self: *View) bool { 1225 const cid = self.box.getFocus().child_id orelse return false; 1226 return self.box.children.getIndex(cid) == header_index; 1227 } 1228 1229 fn tagsViewActive(self: *View) bool { 1230 if (self.headerActive()) return false; 1231 return self.stackSelectedIndex() == tags_view_index; 1232 } 1233 1234 fn formViewActive(self: *View) bool { 1235 if (self.headerActive()) return false; 1236 return self.stackSelectedIndex() == form_view_index; 1237 } 1238 1239 // the selected issue's index, or null when a window-navigation row is 1240 // selected (a leading "previous" row shifts the issue rows down by one). 1241 fn selectedIssueIndex(self: *View, index: usize) ?usize { 1242 const lb = self.listBox(index); 1243 const cid = lb.getFocus().child_id orelse return null; 1244 const idx = lb.children.getIndex(cid) orelse return null; 1245 const win = self.window(index); 1246 const lead: usize = if (win.prev_id != null) 1 else 0; 1247 if (idx < lead or idx - lead >= win.issues.len) return null; 1248 return idx - lead; 1249 } 1250 1251 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1252 self.clearGrid(); 1253 1254 // each header tab maps 1:1 to a stack child by position. mirror the 1255 // selection into the url: the view the page's window is rooted at 1256 // keeps its rooted url, the others get their list route (keeping the 1257 // tag filter). 1258 if (self.header().getSelectedIndex()) |index| { 1259 const stack = self.viewStack(); 1260 stack.getFocus().child_id = stack.children.keys()[index]; 1261 self.session.data.current_page = (if (index == form_view_index) 1262 (switch (self.data.view) { 1263 .edit => ui.RoutablePage.repoIssuesEditRoute(self.data.identity, self.data.selected_id), 1264 .new_comment => ui.RoutablePage.repoCommentNewRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 1265 .edit_comment => ui.RoutablePage.repoCommentEditRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 1266 .resolve => ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, self.data.selected_id, self.data.theirs_picks), 1267 else => ui.RoutablePage.repoIssuesNewRoute(self.data.identity), 1268 }) 1269 else if (index == conflicts_view_index) 1270 ui.RoutablePage.repoIssuesConflictsRoute(self.data.identity, if (self.data.view == .conflicts) self.data.selected_id else "") 1271 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 1272 (if (self.data.description_page) 1273 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, self.data.selected_id) 1274 else if (self.data.comment_page) |page| 1275 ui.RoutablePage.repoCommentsRoute(self.data.identity, self.data.selected_id, &page.selected.id, page.replies.start) 1276 else 1277 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, self.data.selected_id, self.data.comments_start)) 1278 else if (index == tags_view_index) 1279 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 1280 else 1281 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 1282 } 1283 1284 if (self.selectedSplitIndex()) |i| { 1285 // swap the detail pane to the selected issue when it changes. 1286 try self.refreshDetail(allocator, i); 1287 1288 // mirror the focused issue into the url, but only while focus is 1289 // inside the split. an issue's url is the same whether or not the 1290 // list is filtered, so the mirror drops the tag. 1291 if (root_focus.grandchild_id) |g| { 1292 if (self.resultsBox(i).getFocus().children.contains(g)) { 1293 if (self.selectedIssueIndex(i)) |sel| { 1294 // selecting another issue leaves the description page 1295 // behind, like it leaves the tag filter behind. 1296 const entry = &self.window(i).issues[sel]; 1297 const mirrored = if (self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id)) 1298 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, entry.id) 1299 else if (self.data.comment_page) |page| 1300 if (std.mem.eql(u8, entry.id, self.data.selected_id)) 1301 ui.RoutablePage.repoCommentsRoute(self.data.identity, entry.id, &page.selected.id, page.replies.start) 1302 else 1303 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) 1304 else if (std.mem.eql(u8, entry.id, self.data.selected_id)) 1305 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, entry.comments.start) 1306 else 1307 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0); 1308 if (mirrored) |route| self.session.data.current_page = route; 1309 } 1310 } 1311 } 1312 1313 // the selected list row shows a border (the focused TextBox 1314 // upgrades it to a double border itself); the rest stay borderless. 1315 const lb = self.listBox(i); 1316 for (lb.children.keys(), lb.children.values()) |id, *child| { 1317 switch (child.widget) { 1318 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 1319 else => {}, 1320 } 1321 } 1322 1323 // the pane's selected child shows the selection border: the 1324 // description directly, the tags via the flow's selected item. 1325 // cap the list at list_max_width only while the detail pane fits 1326 // beside it. the box drops the detail when the width can't hold 1327 // both minimums, so when it's that narrow we lift the cap and let 1328 // the list fill the whole width. 1329 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 1330 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 1331 1332 // stretch the detail pane across the rest of the width so it fills 1333 // the area rather than shrinking to its content; its scroll fills 1334 // the pane. 1335 if (constraint.max_size.width) |w| { 1336 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 1337 } else { 1338 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 1339 } 1340 } 1341 1342 // refresh the form inputs' entries in the session's focus-id -> input 1343 // map with this frame's addresses, so the web/wasm form handling can 1344 // find them by focus id 1345 if (self.issueForm()) |form| { 1346 const inputs_arena = self.session.arena.allocator(); 1347 for (form.children.values()) |*child| switch (child.widget) { 1348 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 1349 else => {}, 1350 }; 1351 1352 // on an edit page, register the issue's content as the inputs' 1353 // page-constant initial values, which the web overlay renders 1354 // into them. 1355 if (self.data.view == .edit) { 1356 if (self.data.selectedIssue()) |entry| { 1357 const fields = form.children.values(); 1358 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.issue.event.title); 1359 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.issue.event.tags); 1360 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.issue.event.description); 1361 } 1362 } 1363 1364 if (self.data.view == .edit_comment) { 1365 if (self.data.comment_page) |page| { 1366 const body = &form.children.values()[comment_body_field_index].widget.text_input; 1367 try self.session.input_values.put(inputs_arena, body.getFocus().id, page.selected.comment.event.body); 1368 } 1369 } 1370 1371 // the resolve form's inputs prefill from the picked side, which 1372 // is url state and so page-constant like the edit values above 1373 if (self.data.view == .resolve) { 1374 for (form.children.values()) |*child| switch (child.widget) { 1375 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 1376 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 1377 }, 1378 else => {}, 1379 }; 1380 } 1381 } 1382 1383 try self.box.build(allocator, constraint, root_focus); 1384 } 1385 1386 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1387 const sel = self.selectedIssueIndex(index) orelse return; 1388 if (self.detailed_index[index]) |d| if (d == sel) return; 1389 try self.populateDetail(allocator, index, sel); 1390 self.detailed_index[index] = sel; 1391 } 1392 1393 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1394 const entry = self.window(index).issues[sel]; 1395 const inner = self.detailInner(index); 1396 // the /description page replaces its issue's detail with a back link 1397 // and the whole description; other issues keep their normal detail. 1398 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1399 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 1400 1401 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1402 inner.children.clearAndFree(allocator); 1403 inner.getFocus().child_id = null; 1404 self.author_id[index] = null; 1405 1406 // the tool row: the open/close and edit buttons; the description page 1407 // shows none. 1408 { 1409 const row = self.toolRow(index); 1410 for (row.children.values()) |*child| child.widget.deinit(allocator); 1411 row.children.clearAndFree(allocator); 1412 row.getFocus().child_id = null; 1413 row.getFocus().kind = .container; 1414 } 1415 if (!description_page and comment_page == null) { 1416 const row = self.toolRow(index); 1417 const pa = self.session.page_arena.allocator(); 1418 1419 { 1420 var spacer = try ui.Spacer.init(allocator); 1421 errdefer spacer.deinit(allocator); 1422 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1423 } 1424 1425 // a conflicted issue's only action is resolving it. the button 1426 // stays visible logged out; the resolve page shows the 1427 // unauthorized view then. 1428 if (entry.conflicted) { 1429 const label = "resolve conflict"; 1430 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1431 errdefer button.deinit(allocator); 1432 button.getFocus().focusable = true; 1433 const route = ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1434 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1435 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1436 } else { 1437 // a logged-out session can't flip the status, so it gets no 1438 // open/close button (and no form action on the row) 1439 if (self.session.data.is_local or self.session.data.user_id != null) { 1440 const action: []const u8 = switch (entry.issue.event.status) { 1441 .open => "close", 1442 .closed => "open", 1443 }; 1444 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1445 try std.fmt.allocPrint(pa, "form:/issue:{s}/{s}", .{ entry.id, action }) 1446 else 1447 try std.fmt.allocPrint(pa, "form:/repo/{s}/issue:{s}/{s}", .{ self.data.identity, entry.id, action }) }; 1448 1449 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1450 errdefer button.deinit(allocator); 1451 button.getFocus().focusable = true; 1452 // the renderer distinguishes plain clickables from buttons that 1453 // should POST to a server route by this kind. 1454 button.getFocus().kind = .{ .custom = "submit" }; 1455 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1456 } 1457 1458 // the edit button links to the issue's edit page. 1459 { 1460 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1461 errdefer button.deinit(allocator); 1462 button.getFocus().focusable = true; 1463 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1464 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1465 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1466 } 1467 } 1468 1469 // the resolve button on a conflicted issue, else the open/close 1470 // button when present, the edit button otherwise 1471 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 1472 } 1473 1474 if (comment_page) |page| { 1475 var back = try Comment.linkBox(allocator, self.session, "← back to issue", ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1476 errdefer back.deinit(allocator); 1477 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1478 1479 try Comment.appendComment(allocator, inner, self.session, self.data.identity, page.selected); 1480 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies", self.session.page_arena.allocator()); 1481 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1482 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1483 1484 var spacer = try ui.Spacer.init(allocator); 1485 errdefer spacer.deinit(allocator); 1486 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1487 1488 inner.getFocus().child_id = inner.children.keys()[0]; 1489 const sc = self.detailScroll(index); 1490 sc.x = 0; 1491 sc.y = 0; 1492 sc.getFocus().version +%= 1; 1493 return; 1494 } 1495 1496 if (description_page) { 1497 // the back link, in the title slot so the pane's input handling 1498 // applies to it unchanged. 1499 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1500 errdefer tb.deinit(allocator); 1501 tb.getFocus().focusable = true; 1502 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.event.status, "", entry.id) }; 1503 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1504 } else { 1505 // the issue's title as a focusable word-wrapped text box. 1506 { 1507 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1508 errdefer tb.deinit(allocator); 1509 tb.getFocus().focusable = true; 1510 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1511 } 1512 1513 self.author_id[index] = blk: { 1514 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1515 errdefer tb.deinit(allocator); 1516 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1517 break :blk tb.getFocus().id; 1518 }; 1519 1520 // the issue's tags, each linking to this status's list filtered to 1521 // that tag. 1522 { 1523 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1524 defer items.deinit(allocator); 1525 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 1526 while (tag_iter.next()) |tag| { 1527 if (tag.len == 0) continue; 1528 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entry.issue.event.status, tag) }); 1529 } 1530 if (items.items.len > 0) { 1531 var tf = try ui.TagFlow.init(allocator); 1532 errdefer tf.deinit(allocator); 1533 try tf.setItems(allocator, items.items); 1534 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1535 } 1536 } 1537 } 1538 1539 // the description as a focusable word-wrapped text box. past the limit 1540 // it's cut at the last whole line and links to the /description page, 1541 // which shows the whole thing. 1542 self.description_id[index] = blk: { 1543 const whole = entry.issue.event.description; 1544 const cut_short = !description_page and whole.len > max_description_size; 1545 const shown = if (cut_short) 1546 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 1547 else if (whole.len == 0) 1548 "(no description)" 1549 else 1550 whole; 1551 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1552 .border_style = .single, 1553 .rounded_corners = true, 1554 .wrap_kind = .word, 1555 .label = " description ", 1556 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1557 }); 1558 errdefer tb.deinit(allocator); 1559 tb.getFocus().focusable = true; 1560 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1561 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1562 break :blk tb.getFocus().id; 1563 }; 1564 1565 if (!description_page) { 1566 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoCommentNewRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1567 errdefer reply.deinit(allocator); 1568 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1569 1570 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments", self.session.page_arena.allocator()); 1571 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1572 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, entry.id, null, entry.comments); 1573 1574 var spacer = try ui.Spacer.init(allocator); 1575 errdefer spacer.deinit(allocator); 1576 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1577 } 1578 1579 // select the title by default 1580 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1581 1582 // reset the scroll to the top for the newly-shown issue: directly on the 1583 // terminal (the wasm offset), and via a version bump on the web (so the 1584 // renderer's scroll id changes and JS drops the preserved position). 1585 const sc = self.detailScroll(index); 1586 sc.x = 0; 1587 sc.y = 0; 1588 sc.getFocus().version +%= 1; 1589 } 1590 1591 pub fn input(self: *View, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1592 // wheel input moves focus like the arrows 1593 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1594 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1595 else 1596 raw_key; 1597 1598 if (self.headerActive()) { 1599 // down from the tabs re-enters the stack if the selected view has 1600 // something to focus; other keys move the tabs. 1601 if (inp.vertDirection(key) == .down) { 1602 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1603 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1604 // nothing focusable in the unauthorized view 1605 .unauthorized => false, 1606 else => true, 1607 } else false; 1608 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1609 } else { 1610 try self.header().input(allocator, key, root_focus); 1611 } 1612 return; 1613 } 1614 if (self.tagsViewActive()) { 1615 self.tagsViewInput(key, root_focus); 1616 return; 1617 } 1618 if (self.formViewActive()) { 1619 if (self.data.view == .resolve) { 1620 try self.resolveInput(allocator, key, root_focus); 1621 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1622 try self.commentFormInput(allocator, key, root_focus); 1623 } else { 1624 try self.formInput(allocator, key, root_focus); 1625 } 1626 return; 1627 } 1628 const i = self.selectedSplitIndex() orelse return; 1629 if (self.detailActive(i)) { 1630 try self.detailInput(allocator, i, key, root_focus); 1631 } else { 1632 try self.listInput(i, key, root_focus); 1633 } 1634 } 1635 1636 // arrow keys move the tag selection; up from the top row crosses to the 1637 // header tabs. 1638 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1639 const tf = self.tagsView(); 1640 const cid = tf.focus.child_id orelse return; 1641 const cur = tf.indexOfFocusId(cid) orelse return; 1642 const count = tf.text_boxes.items.len; 1643 switch (key) { 1644 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1645 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1646 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1647 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1648 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1649 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1650 else => {}, 1651 } 1652 } 1653 1654 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1655 // up/down (and the scroll wheel) move the selection a row; page up/down 1656 // jump a fixed amount. right/Enter cross into the detail pane. up from 1657 // the top row crosses into the header tabs. 1658 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1659 const lb = self.listBox(index); 1660 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1661 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1662 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1663 return; 1664 } 1665 switch (key) { 1666 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1667 else => {}, 1668 } 1669 } 1670 1671 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1672 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)) { 1673 try self.commentInput(index, key, root_focus); 1674 } else if (self.toolRowFocused(index)) { 1675 try self.toolRowInput(allocator, index, key, root_focus); 1676 } else if (self.titleFocused(index)) { 1677 try self.titleInput(index, key, root_focus); 1678 } else if (self.authorFocused(index)) { 1679 try self.authorInput(index, key, root_focus); 1680 } else if (self.tagsFocused(index)) { 1681 try self.tagsInput(index, key, root_focus); 1682 } else if (self.descriptionFocused(index)) { 1683 try self.descriptionInput(index, key, root_focus); 1684 } else { 1685 try self.commentInput(index, key, root_focus); 1686 } 1687 } 1688 1689 // the tool row: enter or a click on the open/close button flips the 1690 // issue's status; the edit and resolve buttons are a: links the host 1691 // follows. arrows cross between the buttons and to the neighboring 1692 // widgets. 1693 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1694 const row = self.toolRow(index); 1695 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1696 const on_status = cur == button_in_row_index and self.statusButton(index) != null; 1697 switch (key) { 1698 .arrow_left => if (cur > button_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1699 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1700 .arrow_up => self.focusHeader(root_focus), 1701 .arrow_down => self.focusTitle(index, root_focus), 1702 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1703 .mouse => |mouse| if (self.statusButton(index)) |button| { 1704 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1705 }, 1706 else => {}, 1707 } 1708 } 1709 1710 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1711 switch (key) { 1712 .arrow_left => try self.focusList(index, root_focus), 1713 .arrow_up => self.focusToolRow(index, root_focus), 1714 .arrow_down => if (self.authorPresent(index)) self.focusAuthor(index, root_focus) else try self.focusDescription(index, root_focus), 1715 else => {}, 1716 } 1717 } 1718 1719 // the author box's a: link (when it has one) is followed by the host on 1720 // enter / a click; arrows cross to the neighboring widgets. 1721 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1722 switch (key) { 1723 .arrow_left => try self.focusList(index, root_focus), 1724 .arrow_up => self.focusTitle(index, root_focus), 1725 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1726 else => {}, 1727 } 1728 } 1729 1730 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1731 const sc = self.detailScroll(index); 1732 switch (key) { 1733 .arrow_left => return self.focusList(index, root_focus), 1734 // once the scroll can't move further, cross into the tags (or the 1735 // open/close button when the issue has none). 1736 .arrow_up => { 1737 const before = sc.y; 1738 sc.y -= 1; 1739 sc.clampToContent(); 1740 if (sc.y == before) { 1741 if (self.tagFlow(index) != null) 1742 try self.focusTags(index, root_focus) 1743 else if (self.authorPresent(index)) 1744 self.focusAuthor(index, root_focus) 1745 else 1746 self.focusTitle(index, root_focus); 1747 } 1748 return; 1749 }, 1750 .arrow_down => { 1751 if (self.moveCommentVertical(index, root_focus, true)) return; 1752 const before = sc.y; 1753 sc.y += 1; 1754 sc.clampToContent(); 1755 if (sc.y == before) _ = self.moveCommentVertical(index, root_focus, true); 1756 return; 1757 }, 1758 .page_up => sc.y -= 10, 1759 .page_down => sc.y += 10, 1760 .home => sc.y = 0, 1761 .end => sc.y = std.math.maxInt(isize), 1762 else => return, 1763 } 1764 sc.clampToContent(); 1765 } 1766 1767 fn commentInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1768 switch (key) { 1769 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1770 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1771 .arrow_up => _ = self.moveCommentVertical(index, root_focus, false), 1772 .arrow_down => _ = self.moveCommentVertical(index, root_focus, true), 1773 .page_up => self.detailScroll(index).y -= 10, 1774 .page_down => self.detailScroll(index).y += 10, 1775 .home => self.detailScroll(index).y = 0, 1776 .end => self.detailScroll(index).y = std.math.maxInt(isize), 1777 else => return, 1778 } 1779 self.detailScroll(index).clampToContent(); 1780 } 1781 1782 fn focusedDetailChild(self: *View, index: usize, root_focus: *Focus) ?usize { 1783 const inner = self.detailInner(index); 1784 var id = root_focus.grandchild_id orelse return null; 1785 while (inner.getFocus().children.get(id)) |child| { 1786 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1787 id = child.parent_id; 1788 } 1789 return null; 1790 } 1791 1792 fn detailChildFocusable(self: *View, index: usize, child_index: usize) bool { 1793 const child = &self.detailInner(index).children.values()[child_index].widget; 1794 return switch (child.*) { 1795 .repo_comment => true, 1796 .box => |*box| for (box.children.values()) |*item| { 1797 if (item.widget.getFocus().focusable) break true; 1798 } else false, 1799 else => child.getFocus().focusable, 1800 }; 1801 } 1802 1803 fn focusDetailChild(self: *View, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1804 const inner = self.detailInner(index); 1805 const child = &inner.children.values()[child_index]; 1806 var target = inner.children.keys()[child_index]; 1807 var rect = child.rect orelse return; 1808 switch (child.widget) { 1809 .repo_comment => |*comment| { 1810 if (comment.rowRect(last)) |row_rect| { 1811 rect.x += row_rect.x; 1812 rect.y += row_rect.y; 1813 rect.size = row_rect.size; 1814 } 1815 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1816 }, 1817 .box => |*box| if (box.children.count() > 0) { 1818 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1819 root_focus.setFocus(target); 1820 }, 1821 else => root_focus.setFocus(target), 1822 } 1823 self.detailScroll(index).scrollToRect(rect); 1824 } 1825 1826 fn moveCommentVertical(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1827 const inner = self.detailInner(index); 1828 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1829 if (inner.children.values()[current].widget == .repo_comment) { 1830 const comment = &inner.children.values()[current].widget.repo_comment; 1831 if (down and !comment.bodyFocused()) { 1832 self.focusDetailChild(index, current, true, root_focus); 1833 return true; 1834 } 1835 if (!down and comment.bodyFocused()) { 1836 self.focusDetailChild(index, current, false, root_focus); 1837 return true; 1838 } 1839 } 1840 1841 var next = current; 1842 while (true) { 1843 if (down) { 1844 next += 1; 1845 if (next >= inner.children.count()) return false; 1846 } else { 1847 if (next == 0) return false; 1848 next -= 1; 1849 } 1850 if (!self.detailChildFocusable(index, next)) continue; 1851 self.focusDetailChild(index, next, !down, root_focus); 1852 return true; 1853 } 1854 } 1855 1856 fn moveCommentHorizontal(self: *View, index: usize, root_focus: *Focus, right: bool) bool { 1857 const inner = self.detailInner(index); 1858 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1859 const child = &inner.children.values()[current]; 1860 if (child.widget == .repo_comment) { 1861 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1862 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1863 return moved; 1864 } 1865 const row = switch (child.widget) { 1866 .box => |*box| box, 1867 else => return false, 1868 }; 1869 const selected = row.getFocus().child_id orelse return false; 1870 const selected_index = row.children.getIndex(selected) orelse return false; 1871 const target = if (right) selected_index + 1 else selected_index -| 1; 1872 if (target == selected_index or target >= row.children.count()) return false; 1873 root_focus.setFocus(row.children.keys()[target]); 1874 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1875 return true; 1876 } 1877 1878 // up/down (and tab/shift+tab) move between a form's fields; up from the 1879 // title crosses into the header tabs. the multiline description keeps 1880 // enter and any up/down that has a row to move to. 1881 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1882 const form = self.issueForm() orelse return; 1883 const cid = form.getFocus().child_id orelse return; 1884 const cur = form.children.getIndex(cid) orelse return; 1885 1886 if (cur == description_field_index) { 1887 const child = &form.children.values()[cur]; 1888 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1889 return child.widget.input(allocator, key, root_focus); 1890 } 1891 1892 switch (key) { 1893 .arrow_up, .back_tab => if (cur > 0) 1894 root_focus.setFocus(form.children.keys()[cur - 1]) 1895 else 1896 self.focusHeader(root_focus), 1897 .arrow_down, .tab => if (cur < submit_field_index) { 1898 root_focus.setFocus(form.children.keys()[cur + 1]); 1899 }, 1900 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1901 .mouse => |mouse| if (cur == submit_field_index) { 1902 const submit = &form.children.values()[cur].widget.submit_button; 1903 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1904 }, 1905 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1906 } 1907 } 1908 1909 fn commentFormInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1910 const form = self.issueForm() orelse return; 1911 const cid = form.getFocus().child_id orelse return; 1912 const cur = form.children.getIndex(cid) orelse return; 1913 const child = &form.children.values()[cur]; 1914 1915 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1916 return child.widget.input(allocator, key, root_focus); 1917 1918 switch (key) { 1919 .arrow_up, .back_tab => if (cur > comment_author_field_index) 1920 root_focus.setFocus(form.children.keys()[cur - 1]) 1921 else 1922 self.focusHeader(root_focus), 1923 .arrow_down, .tab => if (cur < comment_submit_field_index) 1924 root_focus.setFocus(form.children.keys()[cur + 1]), 1925 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 1926 .mouse => |mouse| if (cur == comment_submit_field_index) { 1927 const submit = &child.widget.submit_button; 1928 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 1929 }, 1930 else => try child.widget.input(allocator, key, root_focus), 1931 } 1932 } 1933 1934 // the multiline hunk inputs keep enter and any up/down that has a row to 1935 // move to; enter on a "use this" link is a navigation the host follows 1936 // before this runs. 1937 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1938 const form = self.issueForm() orelse return; 1939 const cid = form.getFocus().child_id orelse return; 1940 const cur = form.children.getIndex(cid) orelse return; 1941 const child = &form.children.values()[cur]; 1942 1943 if (child.widget == .text_input) { 1944 const ti = &child.widget.text_input; 1945 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1946 return child.widget.input(allocator, key, root_focus); 1947 } 1948 1949 const on_submit = child.widget == .submit_button; 1950 1951 switch (key) { 1952 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1953 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1954 .arrow_left, .arrow_right => switch (child.widget) { 1955 .box => |*row| { 1956 const row_cid = row.getFocus().child_id orelse return; 1957 const row_index = row.children.getIndex(row_cid) orelse return; 1958 if (key == .arrow_left) { 1959 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1960 } else if (row_index + 1 < row.children.count()) { 1961 root_focus.setFocus(row.children.keys()[row_index + 1]); 1962 } 1963 }, 1964 else => try child.widget.input(allocator, key, root_focus), 1965 }, 1966 .enter => if (on_submit) try self.submitResolution(allocator), 1967 .mouse => |mouse| if (on_submit) { 1968 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1969 }, 1970 else => try child.widget.input(allocator, key, root_focus), 1971 } 1972 } 1973 1974 // a multiline input keeps enter and any up/down that has a row to move to 1975 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1976 return switch (key) { 1977 .enter => true, 1978 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1979 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1980 else => false, 1981 }; 1982 } 1983 1984 // the neighboring focusable resolve-form child, skipping the spacer and 1985 // the blank gap rows 1986 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1987 var i = cur; 1988 while (true) { 1989 if (down) { 1990 i += 1; 1991 if (i >= form.children.count()) return null; 1992 } else { 1993 if (i == 0) return null; 1994 i -= 1; 1995 } 1996 switch (form.children.values()[i].widget) { 1997 .spacer, .text => continue, 1998 else => return i, 1999 } 2000 } 2001 } 2002 2003 // focus the resolve-form child at `i` (a version row focuses its selected 2004 // child) and keep it visible on the terminal 2005 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 2006 const child = &form.children.values()[i]; 2007 switch (child.widget) { 2008 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2009 else => root_focus.setFocus(form.children.keys()[i]), 2010 } 2011 if (self.session.is_terminal) { 2012 if (self.resolveScroll()) |sc| { 2013 if (child.rect) |rect| sc.scrollToRect(rect); 2014 } 2015 } 2016 } 2017 2018 // re-emit the conflicted issue's event with the resolved content; any 2019 // event on the issue settles its conflict entry. this is the terminal 2020 // path; the web posts the form to the resolve route. 2021 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 2022 if (comptime wasm) return; 2023 const io = self.session.io orelse return; 2024 const src = self.data.repo_source orelse return; 2025 const entry = self.data.selectedIssue() orelse return; 2026 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2027 const form = self.issueForm() orelse return; 2028 2029 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2030 var title: ?[]u8 = null; 2031 var tags: ?[]u8 = null; 2032 var hunks: std.ArrayList([]const u8) = .empty; 2033 defer { 2034 if (title) |t| allocator.free(t); 2035 if (tags) |t| allocator.free(t); 2036 for (hunks.items) |h| allocator.free(h); 2037 hunks.deinit(allocator); 2038 } 2039 for (form.children.values()) |*child| switch (child.widget) { 2040 .text_input => |*ti| { 2041 const text = try ti.text(allocator); 2042 errdefer allocator.free(text); 2043 if (std.mem.eql(u8, ti.options.name, "title")) { 2044 title = text; 2045 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2046 tags = text; 2047 } else { 2048 try hunks.append(allocator, text); 2049 } 2050 }, 2051 else => {}, 2052 }; 2053 2054 var id_bytes: [evt.event_id_size]u8 = undefined; 2055 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2056 2057 switch (src.repo_kind) { 2058 inline else => |repo_kind| { 2059 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2060 defer any_repo.deinit(io, allocator); 2061 switch (any_repo) { 2062 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 2063 .title = title, 2064 .tags = tags, 2065 .hunks = hunks.items, 2066 } }, author_email) catch |err| switch (err) { 2067 // leave the form up for correction 2068 error.InvalidFields => return, 2069 else => |e| return e, 2070 }, 2071 } 2072 }, 2073 } 2074 2075 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2076 try self.session.navigate(route); 2077 } 2078 2079 // commit a new or edited comment and navigate to its permalink. this is the 2080 // terminal path; the web posts the form to its comment route. 2081 fn submitComment(self: *View, allocator: std.mem.Allocator) !void { 2082 if (comptime wasm) return; 2083 const io = self.session.io orelse return; 2084 const src = self.data.repo_source orelse return; 2085 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2086 const form = self.issueForm() orelse return; 2087 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2088 const body = try body_input.text(allocator); 2089 defer allocator.free(body); 2090 if (!evt.Comment.fieldsValid(body)) return; 2091 2092 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2093 @memcpy(&thread_id, self.data.selected_id); 2094 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2095 var parent: [evt.event_id_size * 2]u8 = undefined; 2096 @memcpy(&parent, self.data.comment_id); 2097 break :blk parent; 2098 }; 2099 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2100 const editing = self.data.view == .edit_comment; 2101 switch (src.repo_kind) { 2102 inline else => |repo_kind| { 2103 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2104 defer any_repo.deinit(io, allocator); 2105 switch (any_repo) { 2106 inline else => |*repo| if (editing) { 2107 var comment_id: [evt.event_id_size]u8 = undefined; 2108 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 2109 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author_email); 2110 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2111 } else { 2112 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author_email); 2113 }, 2114 } 2115 }, 2116 } 2117 2118 body_input.clear(allocator); 2119 const route = ui.RoutablePage.repoCommentsRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2120 try self.session.navigate(route); 2121 } 2122 2123 // the terminal submit paths: the new form commits a new-issue event, the 2124 // edit form re-emits the selected issue's event with the form's content. 2125 // the web posts the forms to their routes instead. 2126 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 2127 if (self.data.view == .edit) { 2128 try self.submitEditedIssue(allocator); 2129 } else { 2130 try self.submitNewIssue(allocator); 2131 } 2132 } 2133 2134 // commit the new issue to the repo's events branch and navigate to it. 2135 // this is the terminal path; the web posts the form to the issue route, 2136 // so the wasm side never runs (or compiles) the repo access below. 2137 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 2138 if (comptime wasm) return; 2139 const io = self.session.io orelse return; 2140 const src = self.data.repo_source orelse return; 2141 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2142 2143 const form = self.issueForm() orelse return; 2144 const title_input = &form.children.values()[title_field_index].widget.text_input; 2145 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2146 const description_input = &form.children.values()[description_field_index].widget.text_input; 2147 2148 const title = try title_input.text(allocator); 2149 defer allocator.free(title); 2150 const tags = try tags_input.text(allocator); 2151 defer allocator.free(tags); 2152 const description = try description_input.text(allocator); 2153 defer allocator.free(description); 2154 2155 if (!evt.Issue.fieldsValid(title, tags)) return; 2156 2157 var id_bytes: [evt.event_id_size]u8 = undefined; 2158 io.random(&id_bytes); 2159 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2160 2161 const event = evt.EventWithId{ 2162 .id = event_id_hex, 2163 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2164 .author_email = author_email, 2165 .event = .{ .issue = .{ 2166 .title = title, 2167 .description = description, 2168 .tags = tags, 2169 } }, 2170 }; 2171 2172 switch (src.repo_kind) { 2173 inline else => |repo_kind| { 2174 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2175 defer any_repo.deinit(io, allocator); 2176 switch (any_repo) { 2177 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2178 } 2179 }, 2180 } 2181 2182 // wipe the form so a return visit starts fresh 2183 title_input.clear(allocator); 2184 tags_input.clear(allocator); 2185 description_input.clear(allocator); 2186 2187 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2188 try self.session.navigate(route); 2189 } 2190 2191 // re-emit the selected issue's event with the form's content (status 2192 // preserved), then reload the issue's page. this is the terminal path; 2193 // the web posts the form to the edit route. 2194 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2195 if (comptime wasm) return; 2196 const io = self.session.io orelse return; 2197 const src = self.data.repo_source orelse return; 2198 const entry = self.data.selectedIssue() orelse return; 2199 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2200 2201 const form = self.issueForm() orelse return; 2202 const title_input = &form.children.values()[title_field_index].widget.text_input; 2203 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2204 const description_input = &form.children.values()[description_field_index].widget.text_input; 2205 2206 const title = try title_input.text(allocator); 2207 defer allocator.free(title); 2208 const tags = try tags_input.text(allocator); 2209 defer allocator.free(tags); 2210 const description = try description_input.text(allocator); 2211 defer allocator.free(description); 2212 2213 if (!evt.Issue.fieldsValid(title, tags)) return; 2214 2215 var id_bytes: [evt.event_id_size]u8 = undefined; 2216 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2217 2218 switch (src.repo_kind) { 2219 inline else => |repo_kind| { 2220 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2221 defer any_repo.deinit(io, allocator); 2222 switch (any_repo) { 2223 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2224 .title = title, 2225 .tags = tags, 2226 .description = description, 2227 } }, author_email), 2228 } 2229 }, 2230 } 2231 2232 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2233 try self.session.navigate(route); 2234 } 2235 2236 // flip the shown issue's status by re-emitting its event, then reload the 2237 // page rooted at the issue so the view reflects the change. this is the 2238 // terminal path; the web posts the button's form to the status route. 2239 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2240 if (comptime wasm) return; 2241 const io = self.session.io orelse return; 2242 const src = self.data.repo_source orelse return; 2243 const sel = self.detailed_index[index] orelse return; 2244 const entry = self.window(index).issues[sel]; 2245 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2246 2247 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2248 .open => .closed, 2249 .closed => .open, 2250 }; 2251 2252 var id_bytes: [evt.event_id_size]u8 = undefined; 2253 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2254 2255 switch (src.repo_kind) { 2256 inline else => |repo_kind| { 2257 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2258 defer any_repo.deinit(io, allocator); 2259 switch (any_repo) { 2260 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 2261 } 2262 }, 2263 } 2264 2265 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2266 try self.session.navigate(route); 2267 } 2268 2269 // arrow keys move the tag selection; at the flow's edges focus crosses to 2270 // the neighboring widgets. 2271 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2272 const tf = self.tagFlow(index) orelse return; 2273 const cid = tf.focus.child_id orelse return; 2274 const cur = tf.indexOfFocusId(cid) orelse return; 2275 const count = tf.text_boxes.items.len; 2276 switch (key) { 2277 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2278 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2279 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 2280 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 2281 .home => self.focusTag(index, tf, root_focus, 0), 2282 .end => self.focusTag(index, tf, root_focus, count - 1), 2283 else => {}, 2284 } 2285 } 2286 2287 const button_in_row_index: usize = 1; 2288 const title_child_index: usize = 0; 2289 const tags_child_index: usize = 2; 2290 2291 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2292 const inner = self.detailInner(index); 2293 if (inner.children.count() <= tags_child_index) return null; 2294 return switch (inner.children.values()[tags_child_index].widget) { 2295 .tag_flow => |*tf| tf, 2296 else => null, 2297 }; 2298 } 2299 2300 fn tagsFocused(self: *View, index: usize) bool { 2301 const inner = self.detailInner(index); 2302 const cid = inner.getFocus().child_id orelse return false; 2303 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2304 } 2305 2306 // the open/close button inside the detail frame's tool row, identified 2307 // by its posting kind; a logged-out session's or conflicted issue's row 2308 // has none. 2309 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 2310 const row = self.toolRow(index); 2311 if (row.children.count() <= button_in_row_index) return null; 2312 switch (row.children.values()[button_in_row_index].widget) { 2313 .text_box => |*tb| switch (tb.box.focus.kind) { 2314 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 2315 else => {}, 2316 }, 2317 else => {}, 2318 } 2319 return null; 2320 } 2321 2322 fn toolRowFocused(self: *View, index: usize) bool { 2323 const outer = self.detailOuter(index); 2324 const cid = outer.getFocus().child_id orelse return false; 2325 return outer.children.getIndex(cid) == tool_row_index; 2326 } 2327 2328 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2329 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2330 // keep the tag visible on the terminal: its rect offset by the flow's 2331 // position in the pane. 2332 if (self.session.is_terminal and item < tf.rects.items.len) { 2333 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2334 var rect = tf.rects.items[item]; 2335 rect.x += flow_rect.x; 2336 rect.y += flow_rect.y; 2337 self.detailScroll(index).scrollToRect(rect); 2338 } 2339 } 2340 } 2341 2342 fn titleFocused(self: *View, index: usize) bool { 2343 const inner = self.detailInner(index); 2344 const cid = inner.getFocus().child_id orelse return false; 2345 return inner.children.getIndex(cid) == title_child_index; 2346 } 2347 2348 fn authorPresent(self: *View, index: usize) bool { 2349 return self.author_id[index] != null; 2350 } 2351 2352 fn authorFocused(self: *View, index: usize) bool { 2353 const inner = self.detailInner(index); 2354 const cid = inner.getFocus().child_id orelse return false; 2355 return cid == self.author_id[index]; 2356 } 2357 2358 fn descriptionFocused(self: *View, index: usize) bool { 2359 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2360 return cid == self.description_id[index]; 2361 } 2362 2363 // the author sits right under the title at the top of the pane, so 2364 // focusing it scrolls there. 2365 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 2366 const id = self.author_id[index] orelse return; 2367 root_focus.setFocus(id); 2368 self.detailScroll(index).y = 0; 2369 } 2370 2371 // the title sits at the top of the pane, so focusing it scrolls there. 2372 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2373 const inner = self.detailInner(index); 2374 if (inner.children.count() == 0) return; 2375 root_focus.setFocus(inner.children.keys()[title_child_index]); 2376 self.detailScroll(index).y = 0; 2377 } 2378 2379 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2380 const tf = self.tagFlow(index) orelse return; 2381 if (tf.text_boxes.items.len == 0) return; 2382 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2383 const item = tf.indexOfFocusId(cid) orelse 0; 2384 self.focusTag(index, tf, root_focus, item); 2385 } 2386 2387 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2388 if (self.description_id[index]) |id| root_focus.setFocus(id); 2389 } 2390 2391 // return to the tool row's last-focused button (the header when the 2392 // description page shows no row). 2393 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2394 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2395 root_focus.setFocus(cid); 2396 } 2397 2398 // enter the detail pane. an empty pane (no issues) can't be entered. 2399 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2400 if (self.detailInner(index).children.count() == 0) return; 2401 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2402 } 2403 2404 // return to the list. 2405 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2406 root_focus.setFocus(self.listScroll(index).getFocus().id); 2407 } 2408 2409 // cross to the header tabs above the stack. 2410 fn focusHeader(self: *View, root_focus: *Focus) void { 2411 root_focus.setFocus(self.box.children.keys()[header_index]); 2412 } 2413 2414 pub fn clearGrid(self: *View) void { 2415 self.box.clearGrid(); 2416 } 2417 2418 pub fn getGrid(self: View) ?Grid { 2419 return self.box.getGrid(); 2420 } 2421 2422 pub fn getFocus(self: *View) *Focus { 2423 return self.box.getFocus(); 2424 } 2425 2426 // for the parent's "scroll up at the top jumps to the header" check: at the 2427 // top only while the header tabs hold focus, so up from the split 2428 // crosses into the tabs first. 2429 pub fn getSelectedIndex(self: *View) ?usize { 2430 return if (self.headerActive()) 0 else 1; 2431 } 2432 }; 2433 2434 // the "a:" navigation link for `status`'s issues page filtered to the 2435 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2436 // a rooted url ignores `status` (it derives its view from the issue's own); 2437 // with an empty `id` this is the bare list link. 2438 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2439 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2440 const url = try route.toUrl(page_arena); 2441 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2442 } 2443 2444 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2445 // href is only followed with js off. 2446 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2447 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2448 const url = try route.toUrl(page_arena); 2449 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2450 } 2451 2452 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2453 // or the conflicts window when null, rooted at issue `id`. 2454 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2455 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2456 return conflictsLink(page_arena, data.identity, id); 2457 } 2458 2459 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2460 // first window). 2461 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2462 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2463 const url = try route.toUrl(page_arena); 2464 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2465 } 2466 2467 // the "a:" link to the whole-description page of issue `id` within `identity`. 2468 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2469 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2470 const url = try route.toUrl(page_arena); 2471 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2472 } 2473 2474 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2475 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2476 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2477 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2478 const url = try route.toUrl(page_arena); 2479 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2480 } 2481 2482 // tabs switching between the issues page's views. 2483 pub const Header = struct { 2484 box: wgt.Box(ui.Widget), 2485 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2486 2487 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2488 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2489 errdefer box.deinit(allocator); 2490 2491 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2492 errdefer tab_ids.deinit(allocator); 2493 2494 const aa = session.page_arena.allocator(); 2495 2496 // a list tab per status, labeled with its listing's issue count 2497 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2498 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2499 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2500 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2501 try addTab(allocator, &box, &tab_ids, label, link); 2502 } 2503 2504 // tags tab, labeled with the active tag filter 2505 { 2506 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2507 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2508 const label = if (data.tag.len == 0) "tags" else blk: { 2509 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2510 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2511 }; 2512 try addTab(allocator, &box, &tab_ids, label, tags_link); 2513 } 2514 2515 // new-issue tab; an edit or resolve url shows its tab in this place 2516 { 2517 const route = switch (data.view) { 2518 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2519 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2520 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2521 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2522 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2523 }; 2524 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2525 const label: []const u8 = switch (data.view) { 2526 .edit => "edit", 2527 .new_comment => "reply", 2528 .edit_comment => "edit", 2529 .resolve => "resolve", 2530 else => "new", 2531 }; 2532 try addTab(allocator, &box, &tab_ids, label, link); 2533 } 2534 2535 // conflicts tab, labeled with the conflict count 2536 if (data.conflicts.count > 0) { 2537 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2538 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2539 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2540 try addTab(allocator, &box, &tab_ids, label, link); 2541 } 2542 2543 var self = Header{ .box = box, .tab_ids = tab_ids }; 2544 // the tab matching the page's view is selected initially. 2545 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2546 return self; 2547 } 2548 2549 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2550 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2551 errdefer text_box.deinit(allocator); 2552 text_box.getFocus().focusable = true; 2553 text_box.getFocus().kind = .{ .custom = link }; 2554 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2555 try box.children.put(allocator, text_box.getFocus().id, .{ 2556 .widget = .{ .text_box = text_box }, 2557 .rect = null, 2558 .min_size = .{ .width = label.len + 2, .height = null }, 2559 }); 2560 } 2561 2562 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2563 self.box.deinit(allocator); 2564 self.tab_ids.deinit(allocator); 2565 } 2566 2567 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2568 self.clearGrid(); 2569 // only the selected tab shows its border 2570 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2571 switch (child.widget) { 2572 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2573 else => {}, 2574 } 2575 } 2576 try self.box.build(allocator, constraint, root_focus); 2577 } 2578 2579 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2580 _ = allocator; 2581 const current_tab = self.currentTabIndex() orelse return; 2582 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2583 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2584 } 2585 } 2586 2587 pub fn clearGrid(self: *Header) void { 2588 self.box.clearGrid(); 2589 } 2590 2591 pub fn getGrid(self: Header) ?Grid { 2592 return self.box.getGrid(); 2593 } 2594 2595 pub fn getFocus(self: *Header) *Focus { 2596 return self.box.getFocus(); 2597 } 2598 2599 pub fn getSelectedIndex(self: Header) ?usize { 2600 return self.currentTabIndex(); 2601 } 2602 2603 fn currentTabIndex(self: Header) ?usize { 2604 const child_id = self.box.focus.child_id orelse return null; 2605 return self.tab_ids.getIndex(child_id); 2606 } 2607 }; 2608