╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const xit = @import("xit"); 6 const rp = xit.repo; 7 const hash = xit.hash; 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const Key = xitui.input.Key; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 const inp = @import("../input.zig"); 15 const diff3 = @import("../../diff3.zig"); 16 const Comment = @import("Comment.zig"); 17 18 const wasm = builtin.target.cpu.arch == .wasm32; 19 20 // how much of a description the detail pane shows before linking to the 21 // /description page, the same limit commit messages use. 22 pub const max_description_size = 2 * 1024; 23 24 // how many issues one window shows before a "next" link appears. 25 pub const page_size = 20; 26 27 // how many tags the tags view shows at most. 28 pub const max_tags = 1000; 29 30 // one issue from the repo's consumed event database, with its hex event id 31 // (the id lives in the event envelope, not the payload). 32 pub const IssueWithId = struct { 33 id: []const u8, 34 issue: evt.Issue.Record, 35 // the creation commit's author 36 author: ui.Author = .unknown, 37 // whether the issue has an unresolved merge conflict 38 conflicted: bool = false, 39 comments: Comment.Window = .empty, 40 }; 41 42 // one side of a conflicted field: its value and who set it 43 pub const Side = struct { 44 text: []const u8, 45 author: ui.Author = .unknown, 46 }; 47 48 pub const FieldConflict = struct { 49 ours: Side, 50 theirs: Side, 51 }; 52 53 // the selected issue's conflict, read for the resolve view. ours is the live 54 // record; the chunks split the description against the merge base. 55 pub const Conflict = struct { 56 title: ?FieldConflict = null, 57 tags: ?FieldConflict = null, 58 description: ?struct { 59 chunks: []const diff3.Chunk, 60 ours_author: ui.Author = .unknown, 61 theirs_author: ui.Author = .unknown, 62 } = null, 63 }; 64 65 // one status's windowed listing. 66 pub const Window = struct { 67 issues: []const IssueWithId, 68 // the id of the previous window's first issue, or null when this window 69 // is already the first. 70 prev_id: ?[]const u8, 71 // the id of the next window's first issue, or null when this is the last 72 // window. 73 next_id: ?[]const u8, 74 // how many issues the listing holds across all windows. 75 count: usize, 76 77 pub const empty: Window = .{ .issues = &.{}, .prev_id = null, .next_id = null, .count = 0 }; 78 }; 79 80 // "owner/name", so the view can build /repo/owner/name/issues/... links. 81 identity: []const u8, 82 // the url-encoded tag the lists are filtered to ("" = unfiltered). 83 tag: []const u8, 84 // the hex event id of the issue its status's window is rooted at ("" = the 85 // first window), mirrored into the url. 86 selected_id: []const u8, 87 // the comment shown in the selected issue's detail pane. 88 comment_id: []const u8, 89 // the selected issue's comment window (0 = the first window). 90 comments_start: usize, 91 // the selected comment and its immediate replies. 92 comment_page: ?Comment.Permalink = null, 93 open: Window, 94 closed: Window, 95 // the conflicted issues' listing; its count also gates the conflicts tab. 96 conflicts: Window, 97 // the selected issue's conflict, set when the page shows the resolve view. 98 conflict: ?Conflict = null, 99 // the resolve view's comma-separated fields prefilled from their side, 100 // mirrored from the url. 101 theirs_picks: []const u8 = "", 102 // the view the page shows initially (a selected issue's status overrides the route's) 103 view: ui.RoutablePage.IssuesView, 104 // the /description page: the detail pane shows the selected issue's whole 105 // description behind a back link. 106 description_page: bool = false, 107 // every tag in the repo, in sorted order, for the tags view. 108 tags: []const []const u8, 109 // the on-disk repo this page was read from, for the terminal submit path 110 // (the web posts the new-issue form to the issue route instead). 111 repo_source: ?ui.RepoSource = null, 112 113 const Self = @This(); 114 115 // `status`'s windowed listing. 116 pub fn window(self: *const Self, status: evt.Issue.Status) *const Window { 117 return switch (status) { 118 .open => &self.open, 119 .closed => &self.closed, 120 }; 121 } 122 123 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 124 // theirs: picks. 125 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 126 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 127 while (it.next()) |pick| { 128 if (std.mem.eql(u8, pick, field)) return true; 129 } 130 return false; 131 } 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) 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, .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) { 669 const route = ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 670 const page_url = try route.toUrl(session.page_arena); 671 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}/comment", .{page_url[0 .. page_url.len - "/new".len]}); 672 const parent_route = if (data.comment_page) |page| 673 ui.RoutablePage.repoCommentsRoute(data.identity, data.selected_id, &page.selected.id, 0) 674 else 675 ui.RoutablePage.repoIssueCommentsRoute(data.identity, data.selected_id, 0); 676 var form = try initCommentForm(allocator, session, action, if (data.comment_page) |page| page.selected.author else if (data.selectedIssue()) |entry| entry.author else .unknown, parent_route orelse return error.RouteTooLong); 677 errdefer form.deinit(allocator); 678 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 679 } else if (data.view == .resolve) { 680 // on the web the page grows to the form's height and the 681 // browser scrolls it; the terminal scrolls the form itself 682 var form_widget: ui.Widget = blk: { 683 var form = try initResolveForm(allocator, session, data); 684 errdefer form.deinit(allocator); 685 break :blk if (session.is_terminal) 686 .{ .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 687 else 688 .{ .box = form }; 689 }; 690 errdefer form_widget.deinit(allocator); 691 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 692 } else { 693 const aa = session.page_arena.allocator(); 694 const action = if (data.view == .edit) 695 (if (data.identity.len == 0) 696 try std.fmt.allocPrint(aa, "form:/issue:{s}/edit", .{data.selected_id}) 697 else 698 try std.fmt.allocPrint(aa, "form:/repo/{s}/issue:{s}/edit", .{ data.identity, data.selected_id })) 699 else if (data.identity.len == 0) 700 "form:/issue" 701 else 702 try std.fmt.allocPrint(aa, "form:/repo/{s}/issue", .{data.identity}); 703 const issue = if (data.view == .edit) 704 (if (data.selectedIssue()) |entry| &entry.issue else null) 705 else 706 null; 707 var form = try initIssueForm(allocator, session, action, issue); 708 errdefer form.deinit(allocator); 709 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 710 } 711 } else { 712 var message = try ui.Unauthorized.View.init(allocator); 713 errdefer message.deinit(allocator); 714 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 715 } 716 717 // the conflicts split; its tab exists only when the repo has 718 // conflicts, so skip the child too to keep the stack 1:1 with the 719 // tabs. 720 if (data.conflicts.count > 0) { 721 var split = try initSplit(allocator, session, data, null); 722 errdefer split.deinit(allocator); 723 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 724 } 725 726 // the stack starts on the page's view. 727 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 728 729 // focus entering the view lands on the tabs first. 730 outer.getFocus().child_id = outer.children.keys()[header_index]; 731 732 return .{ 733 .box = outer, 734 .data = data, 735 .session = session, 736 .detailed_index = @splat(null), 737 .description_id = @splat(null), 738 .author_id = @splat(null), 739 }; 740 } 741 742 // the master-detail split showing `status`'s window, or the conflicts 743 // window when null. 744 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?evt.Issue.Status) !wgt.Box(ui.Widget) { 745 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 746 errdefer box.deinit(allocator); 747 748 const win = if (status_maybe) |status| data.window(status) else &data.conflicts; 749 750 // the issue list (one focusable row per title), plus a "next" link that 751 // reloads the page rooted at the following issue. 752 { 753 var list_scroll = blk: { 754 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 755 errdefer list_box.deinit(allocator); 756 if (win.prev_id) |prev| 757 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, prev)); 758 for (win.issues) |entry| { 759 // conflicts take precedence over the comment count 760 const label: []const u8 = if (entry.conflicted and status_maybe != null) 761 " conflict " 762 else if (entry.comments.count > 0) 763 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 764 else 765 ""; 766 try addRow(allocator, &list_box, entry.issue.event.title, label, try issueRowLink(session.page_arena, data.identity, entry.id)); 767 } 768 if (win.next_id) |next| 769 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, next)); 770 // select the window's first issue (past a leading "previous" 771 // row) so its description shows on load. 772 if (win.issues.len > 0) 773 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 774 else if (list_box.children.count() > 0) 775 list_box.getFocus().child_id = list_box.children.keys()[0]; 776 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 777 }; 778 errdefer list_scroll.deinit(allocator); 779 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 } }); 780 } 781 782 // the detail pane — a frame around a scroll of the description 783 { 784 var detail_outer = blk: { 785 var detail_scroll = blk2: { 786 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 787 errdefer detail_inner.deinit(allocator); 788 // fill the pane (content top-left, scroll bar pinned to the 789 // edge) rather than shrinking to the description. 790 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 791 }; 792 errdefer detail_scroll.deinit(allocator); 793 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 794 errdefer frame.deinit(allocator); 795 // the tool row sits above the scroll (populateDetail fills 796 // it per issue) so it can't scroll out from under the web 797 // overlay <form>, whose position doesn't track pane scrolling. 798 { 799 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 800 errdefer row.deinit(allocator); 801 try frame.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 802 } 803 // the frame's selected child is its scroll, so the focus chain 804 // reaches the description (populateDetail points the scroll's 805 // inner box at it), letting focus recovery descend into the pane. 806 frame.getFocus().child_id = detail_scroll.getFocus().id; 807 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 808 break :blk frame; 809 }; 810 errdefer detail_outer.deinit(allocator); 811 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 812 } 813 814 box.getFocus().child_id = box.children.keys()[list_index]; 815 return box; 816 } 817 818 // an issue form: title/tags/description inputs and a submit button, 819 // prefilled from `issue` when given. its form: subtree makes the web 820 // overlay wrap them in a <form> POSTing to `action`'s route. 821 fn initIssueForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, issue: ?*const evt.Issue.Record) !wgt.Box(ui.Widget) { 822 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 823 errdefer box.deinit(allocator); 824 box.getFocus().kind = .{ .custom = action }; 825 826 { 827 var title = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 828 errdefer title.deinit(allocator); 829 title.getFocus().focusable = true; 830 if (issue) |i| try title.setContent(allocator, i.event.title); 831 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 832 } 833 834 { 835 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 }); 836 errdefer tags.deinit(allocator); 837 tags.getFocus().focusable = true; 838 if (issue) |i| try tags.setContent(allocator, i.event.tags); 839 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 840 } 841 842 { 843 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 } }); 844 errdefer description.deinit(allocator); 845 description.getFocus().focusable = true; 846 if (issue) |i| try description.setContent(allocator, i.event.description); 847 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 848 } 849 850 try addSubmitButton(allocator, &box); 851 852 box.getFocus().child_id = box.children.keys()[title_field_index]; 853 return box; 854 } 855 856 fn initCommentForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, author: ui.Author, parent_route: ui.RoutablePage) !wgt.Box(ui.Widget) { 857 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 858 errdefer box.deinit(allocator); 859 box.getFocus().kind = .{ .custom = action }; 860 861 { 862 var parent = try ui.authorBox(allocator, session.page_arena, author); 863 errdefer parent.deinit(allocator); 864 parent.options.label = " replying to "; 865 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 866 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 867 } 868 869 { 870 var body = try wgt.TextInput(ui.Widget).init(allocator, .{ 871 .label = " comment ", 872 .name = "body", 873 .visible_width = null, 874 .rounded_corners = true, 875 .render_content = session.is_terminal, 876 .multiline = true, 877 .visible_height = 5, 878 .scroll = .{ .fill = true }, 879 }); 880 errdefer body.deinit(allocator); 881 body.getFocus().focusable = true; 882 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 883 } 884 885 try addSubmitButtonLabeled(allocator, &box, "submit reply"); 886 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 887 return box; 888 } 889 890 // a form's submit button, then a spacer absorbing the leftover 891 // min-height the box hands its last child, so the button keeps its 892 // natural height 893 fn addSubmitButton(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 894 return addSubmitButtonLabeled(allocator, box, "submit"); 895 } 896 897 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8) !void { 898 { 899 var submit = try ui.SubmitButton.initLabeled(allocator, label); 900 errdefer submit.deinit(allocator); 901 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 902 } 903 { 904 var spacer = try ui.Spacer.init(allocator); 905 errdefer spacer.deinit(allocator); 906 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 907 } 908 } 909 910 // the conflict resolve form. the "use this" links are navigations that 911 // flip the url's theirs: pick, reloading the prefills; one submit 912 // settles every conflict. the form: subtree makes the web overlay POST 913 // to the resolve route. 914 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(ui.Widget) { 915 const aa = session.page_arena.allocator(); 916 // init only picks the resolve view once it has read the conflict 917 const conflict = if (data.conflict) |*c| c else unreachable; 918 919 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 920 errdefer box.deinit(allocator); 921 box.getFocus().kind = .{ .custom = if (data.identity.len == 0) 922 try std.fmt.allocPrint(aa, "form:/issue:{s}/resolve", .{data.selected_id}) 923 else 924 try std.fmt.allocPrint(aa, "form:/repo/{s}/issue:{s}/resolve", .{ data.identity, data.selected_id }) }; 925 926 if (conflict.title) |*fc| { 927 try addLabel(allocator, &box, "title conflict:"); 928 try addFieldConflict(allocator, &box, session, data, "title", fc); 929 } 930 if (conflict.tags) |*fc| { 931 try addLabel(allocator, &box, "tags conflict:"); 932 try addFieldConflict(allocator, &box, session, data, "tags", fc); 933 } 934 935 if (conflict.description) |*desc| { 936 try addLabel(allocator, &box, "description conflict:"); 937 var hunk_index: usize = 0; 938 for (desc.chunks, 0..) |*chunk, chunk_index| { 939 switch (chunk.*) { 940 .same => |text| { 941 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 942 errdefer tb.deinit(allocator); 943 tb.getFocus().focusable = true; 944 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 945 }, 946 // an auto-resolved chunk: only one side changed it, so its 947 // version stands, shown distinctly with its author 948 .auto => |auto| { 949 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 950 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 951 const label = switch (author) { 952 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 953 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 954 }; 955 var tb = try wgt.TextBox(ui.Widget).init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 956 errdefer tb.deinit(allocator); 957 tb.getFocus().focusable = true; 958 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 959 }, 960 .conflict => |hunk| { 961 // a blank row on each side sets the group apart from 962 // the flowing shared and auto-resolved chunks 963 if (chunk_index > 0) try addGap(allocator, &box); 964 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 965 hunk_index += 1; 966 const picked_theirs = data.theirsPicked(name); 967 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 968 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 969 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 }); 970 errdefer resolution_input.deinit(allocator); 971 resolution_input.getFocus().focusable = true; 972 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 973 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 974 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 975 }, 976 } 977 } 978 } 979 980 try addSubmitButton(allocator, &box); 981 982 box.getFocus().child_id = box.children.keys()[0]; 983 return box; 984 } 985 986 // one conflicted scalar field: both sides' version rows, then the 987 // resolution input prefilled from the picked side. 988 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 { 989 const aa = session.page_arena.allocator(); 990 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 991 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 992 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 }); 994 errdefer resolution_input.deinit(allocator); 995 resolution_input.getFocus().focusable = true; 996 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 997 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 998 } 999 1000 // a blank row setting a conflict group apart from its neighbors 1001 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 1002 var text = try wgt.Text(ui.Widget).init(allocator, " "); 1003 errdefer text.deinit(allocator); 1004 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1005 } 1006 1007 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), content: []const u8) !void { 1008 var label = try ui.SectionLabel.init(allocator, content); 1009 errdefer label.deinit(allocator); 1010 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1011 } 1012 1013 // one side of a conflict: the "use this" link on the left, the version 1014 // itself as a labeled read-only box. 1015 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1016 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1017 errdefer row.deinit(allocator); 1018 1019 { 1020 const use_label = "use this"; 1021 var use = try wgt.TextBox(ui.Widget).init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1022 errdefer use.deinit(allocator); 1023 use.getFocus().focusable = true; 1024 use.getFocus().kind = .{ .custom = link }; 1025 try row.children.put(allocator, use.getFocus().id, .{ 1026 .widget = .{ .text_box = use }, 1027 .rect = null, 1028 .min_size = .{ .width = use_label.len + 2, .height = null }, 1029 .max_size = .{ .width = use_label.len + 2, .height = null }, 1030 }); 1031 } 1032 1033 { 1034 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 }); 1035 errdefer tb.deinit(allocator); 1036 tb.getFocus().focusable = true; 1037 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1038 } 1039 1040 row.getFocus().child_id = row.children.keys()[0]; 1041 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1042 } 1043 1044 // the label of one side's version box, naming its author 1045 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1046 return switch (author) { 1047 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1048 .unknown => if (ours) " current version " else " their version ", 1049 }; 1050 } 1051 1052 // the a: link that reloads the resolve page with `name` picked from ours 1053 // or theirs, reprefilling its resolution input 1054 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1055 const aa = session.page_arena.allocator(); 1056 var picks: std.ArrayList(u8) = .empty; 1057 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1058 while (it.next()) |pick| { 1059 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1060 if (picks.items.len > 0) try picks.append(aa, ','); 1061 try picks.appendSlice(aa, pick); 1062 } 1063 if (theirs) { 1064 if (picks.items.len > 0) try picks.append(aa, ','); 1065 try picks.appendSlice(aa, name); 1066 } 1067 const route = ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1068 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1069 } 1070 1071 // the resolve form's page-constant initial value for the input `name`: 1072 // the picked side of its conflicted field or hunk 1073 fn resolvePrefill(self: *View, name: []const u8) ?[]const u8 { 1074 const conflict = if (self.data.conflict) |*c| c else return null; 1075 const theirs = self.data.theirsPicked(name); 1076 if (std.mem.eql(u8, name, "title")) { 1077 const fc = if (conflict.title) |*f| f else return null; 1078 return if (theirs) fc.theirs.text else fc.ours.text; 1079 } 1080 if (std.mem.eql(u8, name, "tags")) { 1081 const fc = if (conflict.tags) |*f| f else return null; 1082 return if (theirs) fc.theirs.text else fc.ours.text; 1083 } 1084 if (std.mem.startsWith(u8, name, "d")) { 1085 const desc = if (conflict.description) |*d| d else return null; 1086 const hunk_index = std.fmt.parseInt(usize, name[1..], 10) catch return null; 1087 var seen: usize = 0; 1088 for (desc.chunks) |chunk| switch (chunk) { 1089 .conflict => |hunk| { 1090 if (seen == hunk_index) return (if (theirs) hunk.theirs else hunk.ours) orelse ""; 1091 seen += 1; 1092 }, 1093 else => {}, 1094 }; 1095 } 1096 return null; 1097 } 1098 1099 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1100 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1101 errdefer row.deinit(allocator); 1102 row.getFocus().focusable = true; 1103 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1104 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1105 } 1106 1107 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 1108 self.box.deinit(allocator); 1109 } 1110 1111 fn header(self: *View) *Header { 1112 return &self.box.children.values()[header_index].widget.repo_issues_header; 1113 } 1114 1115 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 1116 return &self.box.children.values()[stack_index].widget.stack; 1117 } 1118 1119 // `index`'s master-detail split inside the stack. 1120 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1121 return &self.viewStack().children.values()[index].box; 1122 } 1123 1124 // the tags view's flow inside the stack. 1125 fn tagsView(self: *View) *ui.TagFlow { 1126 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1127 } 1128 1129 // the new-issue, edit, or resolve form inside the stack, or null when the 1130 // unauthorized view stands in for it. 1131 fn issueForm(self: *View) ?*wgt.Box(ui.Widget) { 1132 return switch (self.viewStack().children.values()[form_view_index]) { 1133 .box => |*box| box, 1134 // the resolve form sits inside a scroll on the terminal 1135 .scroll => |*scroll| &scroll.child.box, 1136 else => null, 1137 }; 1138 } 1139 1140 // the resolve form's scroll on the terminal (the web page scrolls itself) 1141 fn resolveScroll(self: *View) ?*wgt.Scroll(ui.Widget) { 1142 return switch (self.viewStack().children.values()[form_view_index]) { 1143 .scroll => |*scroll| scroll, 1144 else => null, 1145 }; 1146 } 1147 1148 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1149 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1150 } 1151 1152 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1153 return &self.listScroll(index).child.box; 1154 } 1155 1156 // the detail frame's children: the tool row above the scroll pane. 1157 const tool_row_index: usize = 0; 1158 const detail_scroll_index: usize = 1; 1159 1160 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 1161 return &self.resultsBox(index).children.values()[detail_index].widget.box; 1162 } 1163 1164 fn toolRow(self: *View, index: usize) *wgt.Box(ui.Widget) { 1165 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 1166 } 1167 1168 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1169 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 1170 } 1171 1172 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 1173 return &self.detailScroll(index).child.box; 1174 } 1175 1176 fn window(self: *View, index: usize) *const Window { 1177 if (index == conflicts_view_index) return &self.data.conflicts; 1178 return self.data.window(splitStatus(index)); 1179 } 1180 1181 fn stackSelectedIndex(self: *View) ?usize { 1182 const stack = self.viewStack(); 1183 const cid = stack.getFocus().child_id orelse return null; 1184 return stack.children.getIndex(cid); 1185 } 1186 1187 // the stack's selected master-detail split (null when the tags view or the 1188 // new-issue form shows). 1189 fn selectedSplitIndex(self: *View) ?usize { 1190 const idx = self.stackSelectedIndex() orelse return null; 1191 return if (idx < split_count or idx == conflicts_view_index) idx else null; 1192 } 1193 1194 fn detailActive(self: *View, index: usize) bool { 1195 const rb = self.resultsBox(index); 1196 const cid = rb.getFocus().child_id orelse return false; 1197 return rb.children.getIndex(cid) == detail_index; 1198 } 1199 1200 fn headerActive(self: *View) bool { 1201 const cid = self.box.getFocus().child_id orelse return false; 1202 return self.box.children.getIndex(cid) == header_index; 1203 } 1204 1205 fn tagsViewActive(self: *View) bool { 1206 if (self.headerActive()) return false; 1207 return self.stackSelectedIndex() == tags_view_index; 1208 } 1209 1210 fn formViewActive(self: *View) bool { 1211 if (self.headerActive()) return false; 1212 return self.stackSelectedIndex() == form_view_index; 1213 } 1214 1215 // the selected issue's index, or null when a window-navigation row is 1216 // selected (a leading "previous" row shifts the issue rows down by one). 1217 fn selectedIssueIndex(self: *View, index: usize) ?usize { 1218 const lb = self.listBox(index); 1219 const cid = lb.getFocus().child_id orelse return null; 1220 const idx = lb.children.getIndex(cid) orelse return null; 1221 const win = self.window(index); 1222 const lead: usize = if (win.prev_id != null) 1 else 0; 1223 if (idx < lead or idx - lead >= win.issues.len) return null; 1224 return idx - lead; 1225 } 1226 1227 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1228 self.clearGrid(); 1229 1230 // each header tab maps 1:1 to a stack child by position. mirror the 1231 // selection into the url: the view the page's window is rooted at 1232 // keeps its rooted url, the others get their list route (keeping the 1233 // tag filter). 1234 if (self.header().getSelectedIndex()) |index| { 1235 const stack = self.viewStack(); 1236 stack.getFocus().child_id = stack.children.keys()[index]; 1237 self.session.data.current_page = (if (index == form_view_index) 1238 (switch (self.data.view) { 1239 .edit => ui.RoutablePage.repoIssuesEditRoute(self.data.identity, self.data.selected_id), 1240 .new_comment => ui.RoutablePage.repoCommentNewRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 1241 .resolve => ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, self.data.selected_id, self.data.theirs_picks), 1242 else => ui.RoutablePage.repoIssuesNewRoute(self.data.identity), 1243 }) 1244 else if (index == conflicts_view_index) 1245 ui.RoutablePage.repoIssuesConflictsRoute(self.data.identity, if (self.data.view == .conflicts) self.data.selected_id else "") 1246 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 1247 (if (self.data.description_page) 1248 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, self.data.selected_id) 1249 else if (self.data.comment_page) |page| 1250 ui.RoutablePage.repoCommentsRoute(self.data.identity, self.data.selected_id, &page.selected.id, page.replies.start) 1251 else 1252 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, self.data.selected_id, self.data.comments_start)) 1253 else if (index == tags_view_index) 1254 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 1255 else 1256 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 1257 } 1258 1259 if (self.selectedSplitIndex()) |i| { 1260 // swap the detail pane to the selected issue when it changes. 1261 try self.refreshDetail(allocator, i); 1262 1263 // mirror the focused issue into the url, but only while focus is 1264 // inside the split. an issue's url is the same whether or not the 1265 // list is filtered, so the mirror drops the tag. 1266 if (root_focus.grandchild_id) |g| { 1267 if (self.resultsBox(i).getFocus().children.contains(g)) { 1268 if (self.selectedIssueIndex(i)) |sel| { 1269 // selecting another issue leaves the description page 1270 // behind, like it leaves the tag filter behind. 1271 const entry = &self.window(i).issues[sel]; 1272 const mirrored = if (self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id)) 1273 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, entry.id) 1274 else if (self.data.comment_page) |page| 1275 if (std.mem.eql(u8, entry.id, self.data.selected_id)) 1276 ui.RoutablePage.repoCommentsRoute(self.data.identity, entry.id, &page.selected.id, page.replies.start) 1277 else 1278 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) 1279 else if (std.mem.eql(u8, entry.id, self.data.selected_id)) 1280 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, entry.comments.start) 1281 else 1282 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0); 1283 if (mirrored) |route| self.session.data.current_page = route; 1284 } 1285 } 1286 } 1287 1288 // the selected list row shows a border (the focused TextBox 1289 // upgrades it to a double border itself); the rest stay borderless. 1290 const lb = self.listBox(i); 1291 for (lb.children.keys(), lb.children.values()) |id, *child| { 1292 switch (child.widget) { 1293 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 1294 else => {}, 1295 } 1296 } 1297 1298 // the pane's selected child shows the selection border: the 1299 // description directly, the tags via the flow's selected item. 1300 // cap the list at list_max_width only while the detail pane fits 1301 // beside it. the box drops the detail when the width can't hold 1302 // both minimums, so when it's that narrow we lift the cap and let 1303 // the list fill the whole width. 1304 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 1305 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 1306 1307 // stretch the detail pane across the rest of the width so it fills 1308 // the area rather than shrinking to its content; its scroll fills 1309 // the pane. 1310 if (constraint.max_size.width) |w| { 1311 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 1312 } else { 1313 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 1314 } 1315 } 1316 1317 // refresh the form inputs' entries in the session's focus-id -> input 1318 // map with this frame's addresses, so the web/wasm form handling can 1319 // find them by focus id 1320 if (self.issueForm()) |form| { 1321 const inputs_arena = self.session.arena.allocator(); 1322 for (form.children.values()) |*child| switch (child.widget) { 1323 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 1324 else => {}, 1325 }; 1326 1327 // on an edit page, register the issue's content as the inputs' 1328 // page-constant initial values, which the web overlay renders 1329 // into them. 1330 if (self.data.view == .edit) { 1331 if (self.data.selectedIssue()) |entry| { 1332 const fields = form.children.values(); 1333 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.issue.event.title); 1334 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.issue.event.tags); 1335 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.issue.event.description); 1336 } 1337 } 1338 1339 // the resolve form's inputs prefill from the picked side, which 1340 // is url state and so page-constant like the edit values above 1341 if (self.data.view == .resolve) { 1342 for (form.children.values()) |*child| switch (child.widget) { 1343 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 1344 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 1345 }, 1346 else => {}, 1347 }; 1348 } 1349 } 1350 1351 try self.box.build(allocator, constraint, root_focus); 1352 } 1353 1354 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1355 const sel = self.selectedIssueIndex(index) orelse return; 1356 if (self.detailed_index[index]) |d| if (d == sel) return; 1357 try self.populateDetail(allocator, index, sel); 1358 self.detailed_index[index] = sel; 1359 } 1360 1361 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1362 const entry = self.window(index).issues[sel]; 1363 const inner = self.detailInner(index); 1364 // the /description page replaces its issue's detail with a back link 1365 // and the whole description; other issues keep their normal detail. 1366 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1367 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 1368 1369 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1370 inner.children.clearAndFree(allocator); 1371 inner.getFocus().child_id = null; 1372 self.author_id[index] = null; 1373 1374 // the tool row: the open/close and edit buttons; the description page 1375 // shows none. 1376 { 1377 const row = self.toolRow(index); 1378 for (row.children.values()) |*child| child.widget.deinit(allocator); 1379 row.children.clearAndFree(allocator); 1380 row.getFocus().child_id = null; 1381 row.getFocus().kind = .container; 1382 } 1383 if (!description_page and comment_page == null) { 1384 const row = self.toolRow(index); 1385 const pa = self.session.page_arena.allocator(); 1386 1387 { 1388 var spacer = try ui.Spacer.init(allocator); 1389 errdefer spacer.deinit(allocator); 1390 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1391 } 1392 1393 // a conflicted issue's only action is resolving it. the button 1394 // stays visible logged out; the resolve page shows the 1395 // unauthorized view then. 1396 if (entry.conflicted) { 1397 const label = "resolve conflict"; 1398 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1399 errdefer button.deinit(allocator); 1400 button.getFocus().focusable = true; 1401 const route = ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1402 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1403 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1404 } else { 1405 // a logged-out session can't flip the status, so it gets no 1406 // open/close button (and no form action on the row) 1407 if (self.session.data.is_local or self.session.data.user_id != null) { 1408 const action: []const u8 = switch (entry.issue.event.status) { 1409 .open => "close", 1410 .closed => "open", 1411 }; 1412 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1413 try std.fmt.allocPrint(pa, "form:/issue:{s}/{s}", .{ entry.id, action }) 1414 else 1415 try std.fmt.allocPrint(pa, "form:/repo/{s}/issue:{s}/{s}", .{ self.data.identity, entry.id, action }) }; 1416 1417 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1418 errdefer button.deinit(allocator); 1419 button.getFocus().focusable = true; 1420 // the renderer distinguishes plain clickables from buttons that 1421 // should POST to a server route by this kind. 1422 button.getFocus().kind = .{ .custom = "submit" }; 1423 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1424 } 1425 1426 // the edit button links to the issue's edit page. 1427 { 1428 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1429 errdefer button.deinit(allocator); 1430 button.getFocus().focusable = true; 1431 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1432 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1433 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1434 } 1435 } 1436 1437 // the resolve button on a conflicted issue, else the open/close 1438 // button when present, the edit button otherwise 1439 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 1440 } 1441 1442 if (comment_page) |page| { 1443 var back = try Comment.linkBox(allocator, self.session, "← back to issue", ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1444 errdefer back.deinit(allocator); 1445 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1446 1447 try Comment.appendComment(allocator, inner, self.session, self.data.identity, page.selected); 1448 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies", self.session.page_arena.allocator()); 1449 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1450 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1451 1452 var spacer = try ui.Spacer.init(allocator); 1453 errdefer spacer.deinit(allocator); 1454 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1455 1456 inner.getFocus().child_id = inner.children.keys()[0]; 1457 const sc = self.detailScroll(index); 1458 sc.x = 0; 1459 sc.y = 0; 1460 sc.getFocus().version +%= 1; 1461 return; 1462 } 1463 1464 if (description_page) { 1465 // the back link, in the title slot so the pane's input handling 1466 // applies to it unchanged. 1467 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1468 errdefer tb.deinit(allocator); 1469 tb.getFocus().focusable = true; 1470 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.event.status, "", entry.id) }; 1471 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1472 } else { 1473 // the issue's title as a focusable word-wrapped text box. 1474 { 1475 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1476 errdefer tb.deinit(allocator); 1477 tb.getFocus().focusable = true; 1478 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1479 } 1480 1481 self.author_id[index] = blk: { 1482 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1483 errdefer tb.deinit(allocator); 1484 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1485 break :blk tb.getFocus().id; 1486 }; 1487 1488 // the issue's tags, each linking to this status's list filtered to 1489 // that tag. 1490 { 1491 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1492 defer items.deinit(allocator); 1493 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 1494 while (tag_iter.next()) |tag| { 1495 if (tag.len == 0) continue; 1496 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entry.issue.event.status, tag) }); 1497 } 1498 if (items.items.len > 0) { 1499 var tf = try ui.TagFlow.init(allocator); 1500 errdefer tf.deinit(allocator); 1501 try tf.setItems(allocator, items.items); 1502 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1503 } 1504 } 1505 } 1506 1507 // the description as a focusable word-wrapped text box. past the limit 1508 // it's cut at the last whole line and links to the /description page, 1509 // which shows the whole thing. 1510 self.description_id[index] = blk: { 1511 const whole = entry.issue.event.description; 1512 const cut_short = !description_page and whole.len > max_description_size; 1513 const shown = if (cut_short) 1514 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 1515 else if (whole.len == 0) 1516 "(no description)" 1517 else 1518 whole; 1519 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1520 .border_style = .single, 1521 .rounded_corners = true, 1522 .wrap_kind = .word, 1523 .label = " description ", 1524 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1525 }); 1526 errdefer tb.deinit(allocator); 1527 tb.getFocus().focusable = true; 1528 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1529 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1530 break :blk tb.getFocus().id; 1531 }; 1532 1533 if (!description_page) { 1534 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoCommentNewRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1535 errdefer reply.deinit(allocator); 1536 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1537 1538 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments", self.session.page_arena.allocator()); 1539 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1540 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, entry.id, null, entry.comments); 1541 1542 var spacer = try ui.Spacer.init(allocator); 1543 errdefer spacer.deinit(allocator); 1544 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1545 } 1546 1547 // select the title by default 1548 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1549 1550 // reset the scroll to the top for the newly-shown issue: directly on the 1551 // terminal (the wasm offset), and via a version bump on the web (so the 1552 // renderer's scroll id changes and JS drops the preserved position). 1553 const sc = self.detailScroll(index); 1554 sc.x = 0; 1555 sc.y = 0; 1556 sc.getFocus().version +%= 1; 1557 } 1558 1559 pub fn input(self: *View, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1560 // wheel input moves focus like the arrows 1561 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1562 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1563 else 1564 raw_key; 1565 1566 if (self.headerActive()) { 1567 // down from the tabs re-enters the stack if the selected view has 1568 // something to focus; other keys move the tabs. 1569 if (inp.vertDirection(key) == .down) { 1570 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1571 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1572 // nothing focusable in the unauthorized view 1573 .unauthorized => false, 1574 else => true, 1575 } else false; 1576 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1577 } else { 1578 try self.header().input(allocator, key, root_focus); 1579 } 1580 return; 1581 } 1582 if (self.tagsViewActive()) { 1583 self.tagsViewInput(key, root_focus); 1584 return; 1585 } 1586 if (self.formViewActive()) { 1587 if (self.data.view == .resolve) { 1588 try self.resolveInput(allocator, key, root_focus); 1589 } else if (self.data.view == .new_comment) { 1590 try self.commentFormInput(allocator, key, root_focus); 1591 } else { 1592 try self.formInput(allocator, key, root_focus); 1593 } 1594 return; 1595 } 1596 const i = self.selectedSplitIndex() orelse return; 1597 if (self.detailActive(i)) { 1598 try self.detailInput(allocator, i, key, root_focus); 1599 } else { 1600 try self.listInput(i, key, root_focus); 1601 } 1602 } 1603 1604 // arrow keys move the tag selection; up from the top row crosses to the 1605 // header tabs. 1606 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1607 const tf = self.tagsView(); 1608 const cid = tf.focus.child_id orelse return; 1609 const cur = tf.indexOfFocusId(cid) orelse return; 1610 const count = tf.text_boxes.items.len; 1611 switch (key) { 1612 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1613 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1614 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1615 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1616 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1617 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1618 else => {}, 1619 } 1620 } 1621 1622 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1623 // up/down (and the scroll wheel) move the selection a row; page up/down 1624 // jump a fixed amount. right/Enter cross into the detail pane. up from 1625 // the top row crosses into the header tabs. 1626 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1627 const lb = self.listBox(index); 1628 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1629 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1630 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1631 return; 1632 } 1633 switch (key) { 1634 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1635 else => {}, 1636 } 1637 } 1638 1639 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1640 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)) { 1641 try self.commentInput(index, key, root_focus); 1642 } else if (self.toolRowFocused(index)) { 1643 try self.toolRowInput(allocator, index, key, root_focus); 1644 } else if (self.titleFocused(index)) { 1645 try self.titleInput(index, key, root_focus); 1646 } else if (self.authorFocused(index)) { 1647 try self.authorInput(index, key, root_focus); 1648 } else if (self.tagsFocused(index)) { 1649 try self.tagsInput(index, key, root_focus); 1650 } else if (self.descriptionFocused(index)) { 1651 try self.descriptionInput(index, key, root_focus); 1652 } else { 1653 try self.commentInput(index, key, root_focus); 1654 } 1655 } 1656 1657 // the tool row: enter or a click on the open/close button flips the 1658 // issue's status; the edit and resolve buttons are a: links the host 1659 // follows. arrows cross between the buttons and to the neighboring 1660 // widgets. 1661 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1662 const row = self.toolRow(index); 1663 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1664 const on_status = cur == button_in_row_index and self.statusButton(index) != null; 1665 switch (key) { 1666 .arrow_left => if (cur > button_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1667 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1668 .arrow_up => self.focusHeader(root_focus), 1669 .arrow_down => self.focusTitle(index, root_focus), 1670 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1671 .mouse => |mouse| if (self.statusButton(index)) |button| { 1672 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1673 }, 1674 else => {}, 1675 } 1676 } 1677 1678 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1679 switch (key) { 1680 .arrow_left => try self.focusList(index, root_focus), 1681 .arrow_up => self.focusToolRow(index, root_focus), 1682 .arrow_down => if (self.authorPresent(index)) self.focusAuthor(index, root_focus) else try self.focusDescription(index, root_focus), 1683 else => {}, 1684 } 1685 } 1686 1687 // the author box's a: link (when it has one) is followed by the host on 1688 // enter / a click; arrows cross to the neighboring widgets. 1689 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1690 switch (key) { 1691 .arrow_left => try self.focusList(index, root_focus), 1692 .arrow_up => self.focusTitle(index, root_focus), 1693 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1694 else => {}, 1695 } 1696 } 1697 1698 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1699 const sc = self.detailScroll(index); 1700 switch (key) { 1701 .arrow_left => return self.focusList(index, root_focus), 1702 // once the scroll can't move further, cross into the tags (or the 1703 // open/close button when the issue has none). 1704 .arrow_up => { 1705 const before = sc.y; 1706 sc.y -= 1; 1707 sc.clampToContent(); 1708 if (sc.y == before) { 1709 if (self.tagFlow(index) != null) 1710 try self.focusTags(index, root_focus) 1711 else if (self.authorPresent(index)) 1712 self.focusAuthor(index, root_focus) 1713 else 1714 self.focusTitle(index, root_focus); 1715 } 1716 return; 1717 }, 1718 .arrow_down => { 1719 if (self.moveCommentVertical(index, root_focus, true)) return; 1720 const before = sc.y; 1721 sc.y += 1; 1722 sc.clampToContent(); 1723 if (sc.y == before) _ = self.moveCommentVertical(index, root_focus, true); 1724 return; 1725 }, 1726 .page_up => sc.y -= 10, 1727 .page_down => sc.y += 10, 1728 .home => sc.y = 0, 1729 .end => sc.y = std.math.maxInt(isize), 1730 else => return, 1731 } 1732 sc.clampToContent(); 1733 } 1734 1735 fn commentInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1736 switch (key) { 1737 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1738 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1739 .arrow_up => _ = self.moveCommentVertical(index, root_focus, false), 1740 .arrow_down => _ = self.moveCommentVertical(index, root_focus, true), 1741 .page_up => self.detailScroll(index).y -= 10, 1742 .page_down => self.detailScroll(index).y += 10, 1743 .home => self.detailScroll(index).y = 0, 1744 .end => self.detailScroll(index).y = std.math.maxInt(isize), 1745 else => return, 1746 } 1747 self.detailScroll(index).clampToContent(); 1748 } 1749 1750 fn focusedDetailChild(self: *View, index: usize, root_focus: *Focus) ?usize { 1751 const inner = self.detailInner(index); 1752 var id = root_focus.grandchild_id orelse return null; 1753 while (inner.getFocus().children.get(id)) |child| { 1754 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1755 id = child.parent_id; 1756 } 1757 return null; 1758 } 1759 1760 fn detailChildFocusable(self: *View, index: usize, child_index: usize) bool { 1761 const child = &self.detailInner(index).children.values()[child_index].widget; 1762 return switch (child.*) { 1763 .repo_comment => true, 1764 .box => |*box| for (box.children.values()) |*item| { 1765 if (item.widget.getFocus().focusable) break true; 1766 } else false, 1767 else => child.getFocus().focusable, 1768 }; 1769 } 1770 1771 fn focusDetailChild(self: *View, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1772 const inner = self.detailInner(index); 1773 const child = &inner.children.values()[child_index]; 1774 var target = inner.children.keys()[child_index]; 1775 var rect = child.rect orelse return; 1776 switch (child.widget) { 1777 .repo_comment => |*comment| { 1778 if (comment.rowRect(last)) |row_rect| { 1779 rect.x += row_rect.x; 1780 rect.y += row_rect.y; 1781 rect.size = row_rect.size; 1782 } 1783 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1784 }, 1785 .box => |*box| if (box.children.count() > 0) { 1786 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1787 root_focus.setFocus(target); 1788 }, 1789 else => root_focus.setFocus(target), 1790 } 1791 self.detailScroll(index).scrollToRect(rect); 1792 } 1793 1794 fn moveCommentVertical(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1795 const inner = self.detailInner(index); 1796 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1797 if (inner.children.values()[current].widget == .repo_comment) { 1798 const comment = &inner.children.values()[current].widget.repo_comment; 1799 if (down and !comment.bodyFocused()) { 1800 self.focusDetailChild(index, current, true, root_focus); 1801 return true; 1802 } 1803 if (!down and comment.bodyFocused()) { 1804 self.focusDetailChild(index, current, false, root_focus); 1805 return true; 1806 } 1807 } 1808 1809 var next = current; 1810 while (true) { 1811 if (down) { 1812 next += 1; 1813 if (next >= inner.children.count()) return false; 1814 } else { 1815 if (next == 0) return false; 1816 next -= 1; 1817 } 1818 if (!self.detailChildFocusable(index, next)) continue; 1819 self.focusDetailChild(index, next, !down, root_focus); 1820 return true; 1821 } 1822 } 1823 1824 fn moveCommentHorizontal(self: *View, index: usize, root_focus: *Focus, right: bool) bool { 1825 const inner = self.detailInner(index); 1826 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1827 const child = &inner.children.values()[current]; 1828 if (child.widget == .repo_comment) { 1829 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1830 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1831 return moved; 1832 } 1833 const row = switch (child.widget) { 1834 .box => |*box| box, 1835 else => return false, 1836 }; 1837 const selected = row.getFocus().child_id orelse return false; 1838 const selected_index = row.children.getIndex(selected) orelse return false; 1839 const target = if (right) selected_index + 1 else selected_index -| 1; 1840 if (target == selected_index or target >= row.children.count()) return false; 1841 root_focus.setFocus(row.children.keys()[target]); 1842 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1843 return true; 1844 } 1845 1846 // up/down (and tab/shift+tab) move between a form's fields; up from the 1847 // title crosses into the header tabs. the multiline description keeps 1848 // enter and any up/down that has a row to move to. 1849 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1850 const form = self.issueForm() orelse return; 1851 const cid = form.getFocus().child_id orelse return; 1852 const cur = form.children.getIndex(cid) orelse return; 1853 1854 if (cur == description_field_index) { 1855 const child = &form.children.values()[cur]; 1856 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1857 return child.widget.input(allocator, key, root_focus); 1858 } 1859 1860 switch (key) { 1861 .arrow_up, .back_tab => if (cur > 0) 1862 root_focus.setFocus(form.children.keys()[cur - 1]) 1863 else 1864 self.focusHeader(root_focus), 1865 .arrow_down, .tab => if (cur < submit_field_index) { 1866 root_focus.setFocus(form.children.keys()[cur + 1]); 1867 }, 1868 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1869 .mouse => |mouse| if (cur == submit_field_index) { 1870 const submit = &form.children.values()[cur].widget.submit_button; 1871 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1872 }, 1873 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1874 } 1875 } 1876 1877 fn commentFormInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1878 const form = self.issueForm() orelse return; 1879 const cid = form.getFocus().child_id orelse return; 1880 const cur = form.children.getIndex(cid) orelse return; 1881 const child = &form.children.values()[cur]; 1882 1883 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1884 return child.widget.input(allocator, key, root_focus); 1885 1886 switch (key) { 1887 .arrow_up, .back_tab => if (cur > comment_author_field_index) 1888 root_focus.setFocus(form.children.keys()[cur - 1]) 1889 else 1890 self.focusHeader(root_focus), 1891 .arrow_down, .tab => if (cur < comment_submit_field_index) 1892 root_focus.setFocus(form.children.keys()[cur + 1]), 1893 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 1894 .mouse => |mouse| if (cur == comment_submit_field_index) { 1895 const submit = &child.widget.submit_button; 1896 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 1897 }, 1898 else => try child.widget.input(allocator, key, root_focus), 1899 } 1900 } 1901 1902 // the multiline hunk inputs keep enter and any up/down that has a row to 1903 // move to; enter on a "use this" link is a navigation the host follows 1904 // before this runs. 1905 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1906 const form = self.issueForm() orelse return; 1907 const cid = form.getFocus().child_id orelse return; 1908 const cur = form.children.getIndex(cid) orelse return; 1909 const child = &form.children.values()[cur]; 1910 1911 if (child.widget == .text_input) { 1912 const ti = &child.widget.text_input; 1913 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1914 return child.widget.input(allocator, key, root_focus); 1915 } 1916 1917 const on_submit = child.widget == .submit_button; 1918 1919 switch (key) { 1920 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1921 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1922 .arrow_left, .arrow_right => switch (child.widget) { 1923 .box => |*row| { 1924 const row_cid = row.getFocus().child_id orelse return; 1925 const row_index = row.children.getIndex(row_cid) orelse return; 1926 if (key == .arrow_left) { 1927 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1928 } else if (row_index + 1 < row.children.count()) { 1929 root_focus.setFocus(row.children.keys()[row_index + 1]); 1930 } 1931 }, 1932 else => try child.widget.input(allocator, key, root_focus), 1933 }, 1934 .enter => if (on_submit) try self.submitResolution(allocator), 1935 .mouse => |mouse| if (on_submit) { 1936 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1937 }, 1938 else => try child.widget.input(allocator, key, root_focus), 1939 } 1940 } 1941 1942 // a multiline input keeps enter and any up/down that has a row to move to 1943 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1944 return switch (key) { 1945 .enter => true, 1946 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1947 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1948 else => false, 1949 }; 1950 } 1951 1952 // the neighboring focusable resolve-form child, skipping the spacer and 1953 // the blank gap rows 1954 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1955 var i = cur; 1956 while (true) { 1957 if (down) { 1958 i += 1; 1959 if (i >= form.children.count()) return null; 1960 } else { 1961 if (i == 0) return null; 1962 i -= 1; 1963 } 1964 switch (form.children.values()[i].widget) { 1965 .spacer, .text => continue, 1966 else => return i, 1967 } 1968 } 1969 } 1970 1971 // focus the resolve-form child at `i` (a version row focuses its selected 1972 // child) and keep it visible on the terminal 1973 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 1974 const child = &form.children.values()[i]; 1975 switch (child.widget) { 1976 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 1977 else => root_focus.setFocus(form.children.keys()[i]), 1978 } 1979 if (self.session.is_terminal) { 1980 if (self.resolveScroll()) |sc| { 1981 if (child.rect) |rect| sc.scrollToRect(rect); 1982 } 1983 } 1984 } 1985 1986 // re-emit the conflicted issue's event with the resolved content; any 1987 // event on the issue settles its conflict entry. this is the terminal 1988 // path; the web posts the form to the resolve route. 1989 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 1990 if (comptime wasm) return; 1991 const io = self.session.io orelse return; 1992 const src = self.data.repo_source orelse return; 1993 const entry = self.data.selectedIssue() orelse return; 1994 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1995 const form = self.issueForm() orelse return; 1996 1997 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 1998 var title: ?[]u8 = null; 1999 var tags: ?[]u8 = null; 2000 var hunks: std.ArrayList([]const u8) = .empty; 2001 defer { 2002 if (title) |t| allocator.free(t); 2003 if (tags) |t| allocator.free(t); 2004 for (hunks.items) |h| allocator.free(h); 2005 hunks.deinit(allocator); 2006 } 2007 for (form.children.values()) |*child| switch (child.widget) { 2008 .text_input => |*ti| { 2009 const text = try ti.text(allocator); 2010 errdefer allocator.free(text); 2011 if (std.mem.eql(u8, ti.options.name, "title")) { 2012 title = text; 2013 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2014 tags = text; 2015 } else { 2016 try hunks.append(allocator, text); 2017 } 2018 }, 2019 else => {}, 2020 }; 2021 2022 var id_bytes: [evt.event_id_size]u8 = undefined; 2023 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2024 2025 switch (src.repo_kind) { 2026 inline else => |repo_kind| { 2027 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2028 defer any_repo.deinit(io, allocator); 2029 switch (any_repo) { 2030 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 2031 .title = title, 2032 .tags = tags, 2033 .hunks = hunks.items, 2034 } }, author_email) catch |err| switch (err) { 2035 // leave the form up for correction 2036 error.InvalidFields => return, 2037 else => |e| return e, 2038 }, 2039 } 2040 }, 2041 } 2042 2043 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2044 try self.session.navigate(route); 2045 } 2046 2047 // commit a reply and navigate to its permalink. this is the terminal path; 2048 // the web posts the form to its /comment route. 2049 fn submitComment(self: *View, allocator: std.mem.Allocator) !void { 2050 if (comptime wasm) return; 2051 const io = self.session.io orelse return; 2052 const src = self.data.repo_source orelse return; 2053 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2054 const form = self.issueForm() orelse return; 2055 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2056 const body = try body_input.text(allocator); 2057 defer allocator.free(body); 2058 if (!evt.Comment.fieldsValid(body)) return; 2059 2060 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2061 @memcpy(&thread_id, self.data.selected_id); 2062 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2063 var parent: [evt.event_id_size * 2]u8 = undefined; 2064 @memcpy(&parent, self.data.comment_id); 2065 break :blk parent; 2066 }; 2067 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2068 switch (src.repo_kind) { 2069 inline else => |repo_kind| { 2070 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2071 defer any_repo.deinit(io, allocator); 2072 switch (any_repo) { 2073 inline else => |*repo| event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author_email), 2074 } 2075 }, 2076 } 2077 2078 body_input.clear(allocator); 2079 const route = ui.RoutablePage.repoCommentsRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2080 try self.session.navigate(route); 2081 } 2082 2083 // the terminal submit paths: the new form commits a new-issue event, the 2084 // edit form re-emits the selected issue's event with the form's content. 2085 // the web posts the forms to their routes instead. 2086 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 2087 if (self.data.view == .edit) { 2088 try self.submitEditedIssue(allocator); 2089 } else { 2090 try self.submitNewIssue(allocator); 2091 } 2092 } 2093 2094 // commit the new issue to the repo's events branch and navigate to it. 2095 // this is the terminal path; the web posts the form to the issue route, 2096 // so the wasm side never runs (or compiles) the repo access below. 2097 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 2098 if (comptime wasm) return; 2099 const io = self.session.io orelse return; 2100 const src = self.data.repo_source orelse return; 2101 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2102 2103 const form = self.issueForm() orelse return; 2104 const title_input = &form.children.values()[title_field_index].widget.text_input; 2105 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2106 const description_input = &form.children.values()[description_field_index].widget.text_input; 2107 2108 const title = try title_input.text(allocator); 2109 defer allocator.free(title); 2110 const tags = try tags_input.text(allocator); 2111 defer allocator.free(tags); 2112 const description = try description_input.text(allocator); 2113 defer allocator.free(description); 2114 2115 if (!evt.Issue.fieldsValid(title, tags)) return; 2116 2117 var id_bytes: [evt.event_id_size]u8 = undefined; 2118 io.random(&id_bytes); 2119 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2120 2121 const event = evt.EventWithId{ 2122 .id = event_id_hex, 2123 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2124 .author_email = author_email, 2125 .event = .{ .issue = .{ 2126 .title = title, 2127 .description = description, 2128 .tags = tags, 2129 } }, 2130 }; 2131 2132 switch (src.repo_kind) { 2133 inline else => |repo_kind| { 2134 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2135 defer any_repo.deinit(io, allocator); 2136 switch (any_repo) { 2137 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2138 } 2139 }, 2140 } 2141 2142 // wipe the form so a return visit starts fresh 2143 title_input.clear(allocator); 2144 tags_input.clear(allocator); 2145 description_input.clear(allocator); 2146 2147 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2148 try self.session.navigate(route); 2149 } 2150 2151 // re-emit the selected issue's event with the form's content (status 2152 // preserved), then reload the issue's page. this is the terminal path; 2153 // the web posts the form to the edit route. 2154 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2155 if (comptime wasm) return; 2156 const io = self.session.io orelse return; 2157 const src = self.data.repo_source orelse return; 2158 const entry = self.data.selectedIssue() orelse return; 2159 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2160 2161 const form = self.issueForm() orelse return; 2162 const title_input = &form.children.values()[title_field_index].widget.text_input; 2163 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2164 const description_input = &form.children.values()[description_field_index].widget.text_input; 2165 2166 const title = try title_input.text(allocator); 2167 defer allocator.free(title); 2168 const tags = try tags_input.text(allocator); 2169 defer allocator.free(tags); 2170 const description = try description_input.text(allocator); 2171 defer allocator.free(description); 2172 2173 if (!evt.Issue.fieldsValid(title, tags)) return; 2174 2175 var id_bytes: [evt.event_id_size]u8 = undefined; 2176 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2177 2178 switch (src.repo_kind) { 2179 inline else => |repo_kind| { 2180 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2181 defer any_repo.deinit(io, allocator); 2182 switch (any_repo) { 2183 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2184 .title = title, 2185 .tags = tags, 2186 .description = description, 2187 } }, author_email), 2188 } 2189 }, 2190 } 2191 2192 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2193 try self.session.navigate(route); 2194 } 2195 2196 // flip the shown issue's status by re-emitting its event, then reload the 2197 // page rooted at the issue so the view reflects the change. this is the 2198 // terminal path; the web posts the button's form to the status route. 2199 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2200 if (comptime wasm) return; 2201 const io = self.session.io orelse return; 2202 const src = self.data.repo_source orelse return; 2203 const sel = self.detailed_index[index] orelse return; 2204 const entry = self.window(index).issues[sel]; 2205 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2206 2207 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2208 .open => .closed, 2209 .closed => .open, 2210 }; 2211 2212 var id_bytes: [evt.event_id_size]u8 = undefined; 2213 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2214 2215 switch (src.repo_kind) { 2216 inline else => |repo_kind| { 2217 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2218 defer any_repo.deinit(io, allocator); 2219 switch (any_repo) { 2220 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 2221 } 2222 }, 2223 } 2224 2225 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2226 try self.session.navigate(route); 2227 } 2228 2229 // arrow keys move the tag selection; at the flow's edges focus crosses to 2230 // the neighboring widgets. 2231 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2232 const tf = self.tagFlow(index) orelse return; 2233 const cid = tf.focus.child_id orelse return; 2234 const cur = tf.indexOfFocusId(cid) orelse return; 2235 const count = tf.text_boxes.items.len; 2236 switch (key) { 2237 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2238 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2239 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 2240 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 2241 .home => self.focusTag(index, tf, root_focus, 0), 2242 .end => self.focusTag(index, tf, root_focus, count - 1), 2243 else => {}, 2244 } 2245 } 2246 2247 const button_in_row_index: usize = 1; 2248 const title_child_index: usize = 0; 2249 const tags_child_index: usize = 2; 2250 2251 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2252 const inner = self.detailInner(index); 2253 if (inner.children.count() <= tags_child_index) return null; 2254 return switch (inner.children.values()[tags_child_index].widget) { 2255 .tag_flow => |*tf| tf, 2256 else => null, 2257 }; 2258 } 2259 2260 fn tagsFocused(self: *View, index: usize) bool { 2261 const inner = self.detailInner(index); 2262 const cid = inner.getFocus().child_id orelse return false; 2263 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2264 } 2265 2266 // the open/close button inside the detail frame's tool row, identified 2267 // by its posting kind; a logged-out session's or conflicted issue's row 2268 // has none. 2269 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 2270 const row = self.toolRow(index); 2271 if (row.children.count() <= button_in_row_index) return null; 2272 switch (row.children.values()[button_in_row_index].widget) { 2273 .text_box => |*tb| switch (tb.box.focus.kind) { 2274 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 2275 else => {}, 2276 }, 2277 else => {}, 2278 } 2279 return null; 2280 } 2281 2282 fn toolRowFocused(self: *View, index: usize) bool { 2283 const outer = self.detailOuter(index); 2284 const cid = outer.getFocus().child_id orelse return false; 2285 return outer.children.getIndex(cid) == tool_row_index; 2286 } 2287 2288 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2289 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2290 // keep the tag visible on the terminal: its rect offset by the flow's 2291 // position in the pane. 2292 if (self.session.is_terminal and item < tf.rects.items.len) { 2293 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2294 var rect = tf.rects.items[item]; 2295 rect.x += flow_rect.x; 2296 rect.y += flow_rect.y; 2297 self.detailScroll(index).scrollToRect(rect); 2298 } 2299 } 2300 } 2301 2302 fn titleFocused(self: *View, index: usize) bool { 2303 const inner = self.detailInner(index); 2304 const cid = inner.getFocus().child_id orelse return false; 2305 return inner.children.getIndex(cid) == title_child_index; 2306 } 2307 2308 fn authorPresent(self: *View, index: usize) bool { 2309 return self.author_id[index] != null; 2310 } 2311 2312 fn authorFocused(self: *View, index: usize) bool { 2313 const inner = self.detailInner(index); 2314 const cid = inner.getFocus().child_id orelse return false; 2315 return cid == self.author_id[index]; 2316 } 2317 2318 fn descriptionFocused(self: *View, index: usize) bool { 2319 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2320 return cid == self.description_id[index]; 2321 } 2322 2323 // the author sits right under the title at the top of the pane, so 2324 // focusing it scrolls there. 2325 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 2326 const id = self.author_id[index] orelse return; 2327 root_focus.setFocus(id); 2328 self.detailScroll(index).y = 0; 2329 } 2330 2331 // the title sits at the top of the pane, so focusing it scrolls there. 2332 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2333 const inner = self.detailInner(index); 2334 if (inner.children.count() == 0) return; 2335 root_focus.setFocus(inner.children.keys()[title_child_index]); 2336 self.detailScroll(index).y = 0; 2337 } 2338 2339 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2340 const tf = self.tagFlow(index) orelse return; 2341 if (tf.text_boxes.items.len == 0) return; 2342 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2343 const item = tf.indexOfFocusId(cid) orelse 0; 2344 self.focusTag(index, tf, root_focus, item); 2345 } 2346 2347 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2348 if (self.description_id[index]) |id| root_focus.setFocus(id); 2349 } 2350 2351 // return to the tool row's last-focused button (the header when the 2352 // description page shows no row). 2353 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2354 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2355 root_focus.setFocus(cid); 2356 } 2357 2358 // enter the detail pane. an empty pane (no issues) can't be entered. 2359 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2360 if (self.detailInner(index).children.count() == 0) return; 2361 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2362 } 2363 2364 // return to the list. 2365 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2366 root_focus.setFocus(self.listScroll(index).getFocus().id); 2367 } 2368 2369 // cross to the header tabs above the stack. 2370 fn focusHeader(self: *View, root_focus: *Focus) void { 2371 root_focus.setFocus(self.box.children.keys()[header_index]); 2372 } 2373 2374 pub fn clearGrid(self: *View) void { 2375 self.box.clearGrid(); 2376 } 2377 2378 pub fn getGrid(self: View) ?Grid { 2379 return self.box.getGrid(); 2380 } 2381 2382 pub fn getFocus(self: *View) *Focus { 2383 return self.box.getFocus(); 2384 } 2385 2386 // for the parent's "scroll up at the top jumps to the header" check: at the 2387 // top only while the header tabs hold focus, so up from the split 2388 // crosses into the tabs first. 2389 pub fn getSelectedIndex(self: *View) ?usize { 2390 return if (self.headerActive()) 0 else 1; 2391 } 2392 }; 2393 2394 // the "a:" navigation link for `status`'s issues page filtered to the 2395 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2396 // a rooted url ignores `status` (it derives its view from the issue's own); 2397 // with an empty `id` this is the bare list link. 2398 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2399 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2400 const url = try route.toUrl(page_arena); 2401 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2402 } 2403 2404 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2405 // href is only followed with js off. 2406 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2407 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2408 const url = try route.toUrl(page_arena); 2409 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2410 } 2411 2412 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2413 // or the conflicts window when null, rooted at issue `id`. 2414 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2415 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2416 return conflictsLink(page_arena, data.identity, id); 2417 } 2418 2419 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2420 // first window). 2421 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2422 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2423 const url = try route.toUrl(page_arena); 2424 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2425 } 2426 2427 // the "a:" link to the whole-description page of issue `id` within `identity`. 2428 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2429 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2430 const url = try route.toUrl(page_arena); 2431 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2432 } 2433 2434 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2435 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2436 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2437 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2438 const url = try route.toUrl(page_arena); 2439 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2440 } 2441 2442 // tabs switching between the issues page's views. 2443 pub const Header = struct { 2444 box: wgt.Box(ui.Widget), 2445 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2446 2447 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2448 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2449 errdefer box.deinit(allocator); 2450 2451 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2452 errdefer tab_ids.deinit(allocator); 2453 2454 const aa = session.page_arena.allocator(); 2455 2456 // a list tab per status, labeled with its listing's issue count 2457 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2458 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2459 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2460 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2461 try addTab(allocator, &box, &tab_ids, label, link); 2462 } 2463 2464 // tags tab, labeled with the active tag filter 2465 { 2466 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2467 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2468 const label = if (data.tag.len == 0) "tags" else blk: { 2469 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2470 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2471 }; 2472 try addTab(allocator, &box, &tab_ids, label, tags_link); 2473 } 2474 2475 // new-issue tab; an edit or resolve url shows its tab in this place 2476 { 2477 const route = switch (data.view) { 2478 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2479 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2480 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2481 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2482 }; 2483 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2484 const label: []const u8 = switch (data.view) { 2485 .edit => "edit", 2486 .new_comment => "reply", 2487 .resolve => "resolve", 2488 else => "new", 2489 }; 2490 try addTab(allocator, &box, &tab_ids, label, link); 2491 } 2492 2493 // conflicts tab, labeled with the conflict count 2494 if (data.conflicts.count > 0) { 2495 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2496 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2497 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2498 try addTab(allocator, &box, &tab_ids, label, link); 2499 } 2500 2501 var self = Header{ .box = box, .tab_ids = tab_ids }; 2502 // the tab matching the page's view is selected initially. 2503 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2504 return self; 2505 } 2506 2507 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2508 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2509 errdefer text_box.deinit(allocator); 2510 text_box.getFocus().focusable = true; 2511 text_box.getFocus().kind = .{ .custom = link }; 2512 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2513 try box.children.put(allocator, text_box.getFocus().id, .{ 2514 .widget = .{ .text_box = text_box }, 2515 .rect = null, 2516 .min_size = .{ .width = label.len + 2, .height = null }, 2517 }); 2518 } 2519 2520 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2521 self.box.deinit(allocator); 2522 self.tab_ids.deinit(allocator); 2523 } 2524 2525 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2526 self.clearGrid(); 2527 // only the selected tab shows its border 2528 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2529 switch (child.widget) { 2530 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2531 else => {}, 2532 } 2533 } 2534 try self.box.build(allocator, constraint, root_focus); 2535 } 2536 2537 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2538 _ = allocator; 2539 const current_tab = self.currentTabIndex() orelse return; 2540 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2541 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2542 } 2543 } 2544 2545 pub fn clearGrid(self: *Header) void { 2546 self.box.clearGrid(); 2547 } 2548 2549 pub fn getGrid(self: Header) ?Grid { 2550 return self.box.getGrid(); 2551 } 2552 2553 pub fn getFocus(self: *Header) *Focus { 2554 return self.box.getFocus(); 2555 } 2556 2557 pub fn getSelectedIndex(self: Header) ?usize { 2558 return self.currentTabIndex(); 2559 } 2560 2561 fn currentTabIndex(self: Header) ?usize { 2562 const child_id = self.box.focus.child_id orelse return null; 2563 return self.tab_ids.getIndex(child_id); 2564 } 2565 }; 2566