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