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