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