╭ 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, key: Key, root_focus: *Focus) !void { 1424 if (self.headerActive()) { 1425 // down from the tabs re-enters the stack if the selected view has 1426 // something to focus; other keys move the tabs. 1427 if (inp.vertDirection(key) == .down) { 1428 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1429 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1430 // nothing focusable in the unauthorized view 1431 .unauthorized => false, 1432 else => true, 1433 } else false; 1434 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1435 } else { 1436 try self.header().input(allocator, key, root_focus); 1437 } 1438 return; 1439 } 1440 if (self.tagsViewActive()) { 1441 self.tagsViewInput(key, root_focus); 1442 return; 1443 } 1444 if (self.formViewActive()) { 1445 if (self.data.view == .resolve) { 1446 try self.resolveInput(allocator, key, root_focus); 1447 } else { 1448 try self.formInput(allocator, key, root_focus); 1449 } 1450 return; 1451 } 1452 const i = self.selectedSplitIndex() orelse return; 1453 if (self.detailActive(i)) { 1454 try self.detailInput(allocator, i, key, root_focus); 1455 } else { 1456 try self.listInput(i, key, root_focus); 1457 } 1458 } 1459 1460 // arrow keys move the tag selection; up from the top row crosses to the 1461 // header tabs. 1462 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1463 const tf = self.tagsView(); 1464 const cid = tf.focus.child_id orelse return; 1465 const cur = tf.indexOfFocusId(cid) orelse return; 1466 const count = tf.text_boxes.items.len; 1467 switch (key) { 1468 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1469 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1470 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1471 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1472 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1473 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1474 else => {}, 1475 } 1476 } 1477 1478 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1479 // up/down (and the scroll wheel) move the selection a row; page up/down 1480 // jump a fixed amount. right/Enter cross into the detail pane. up from 1481 // the top row crosses into the header tabs. 1482 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1483 const lb = self.listBox(index); 1484 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1485 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1486 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1487 return; 1488 } 1489 switch (key) { 1490 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1491 else => {}, 1492 } 1493 } 1494 1495 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1496 if (self.toolRowFocused(index)) { 1497 try self.toolRowInput(allocator, index, key, root_focus); 1498 } else if (self.titleFocused(index)) { 1499 try self.titleInput(index, key, root_focus); 1500 } else if (self.authorFocused(index)) { 1501 try self.authorInput(index, key, root_focus); 1502 } else if (self.tagsFocused(index)) { 1503 try self.tagsInput(index, key, root_focus); 1504 } else { 1505 try self.descriptionInput(index, key, root_focus); 1506 } 1507 } 1508 1509 // the tool row: enter or a click on the open/close button flips the 1510 // issue's status; the edit and resolve buttons are a: links the host 1511 // follows. arrows cross between the buttons and to the neighboring 1512 // widgets. 1513 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1514 const row = self.toolRow(index); 1515 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1516 const on_status = cur == button_in_row_index and self.statusButton(index) != null; 1517 switch (key) { 1518 .arrow_left => if (cur > button_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1519 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1520 .arrow_up => self.focusHeader(root_focus), 1521 .arrow_down => self.focusTitle(index, root_focus), 1522 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1523 .mouse => |mouse| switch (mouse.action) { 1524 .scroll => |dir| { 1525 const sc = self.detailScroll(index); 1526 sc.y += if (dir == .up) @as(isize, -1) else 1; 1527 sc.clampToContent(); 1528 }, 1529 else => if (self.statusButton(index)) |button| { 1530 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1531 }, 1532 }, 1533 else => {}, 1534 } 1535 } 1536 1537 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1538 const sc = self.detailScroll(index); 1539 switch (key) { 1540 .arrow_left => try self.focusList(index, root_focus), 1541 .arrow_up => self.focusToolRow(index, root_focus), 1542 .arrow_down => if (self.authorPresent(index)) self.focusAuthor(index, root_focus) else try self.focusDescription(index, root_focus), 1543 .mouse => |mouse| switch (mouse.action) { 1544 .scroll => |dir| { 1545 sc.y += if (dir == .up) @as(isize, -1) else 1; 1546 sc.clampToContent(); 1547 }, 1548 else => {}, 1549 }, 1550 else => {}, 1551 } 1552 } 1553 1554 // the author box's a: link (when it has one) is followed by the host on 1555 // enter / a click; arrows cross to the neighboring widgets. 1556 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1557 const sc = self.detailScroll(index); 1558 switch (key) { 1559 .arrow_left => try self.focusList(index, root_focus), 1560 .arrow_up => self.focusTitle(index, root_focus), 1561 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1562 .mouse => |mouse| switch (mouse.action) { 1563 .scroll => |dir| { 1564 sc.y += if (dir == .up) @as(isize, -1) else 1; 1565 sc.clampToContent(); 1566 }, 1567 else => {}, 1568 }, 1569 else => {}, 1570 } 1571 } 1572 1573 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1574 const sc = self.detailScroll(index); 1575 switch (key) { 1576 .arrow_left => return self.focusList(index, root_focus), 1577 // once the scroll can't move further, cross into the tags (or the 1578 // open/close button when the issue has none). 1579 .arrow_up => { 1580 const before = sc.y; 1581 sc.y -= 1; 1582 sc.clampToContent(); 1583 if (sc.y == before) { 1584 if (self.tagFlow(index) != null) 1585 try self.focusTags(index, root_focus) 1586 else if (self.authorPresent(index)) 1587 self.focusAuthor(index, root_focus) 1588 else 1589 self.focusTitle(index, root_focus); 1590 } 1591 return; 1592 }, 1593 .arrow_down => sc.y += 1, 1594 .page_up => sc.y -= 10, 1595 .page_down => sc.y += 10, 1596 .home => sc.y = 0, 1597 .end => sc.y = std.math.maxInt(isize), 1598 .mouse => |mouse| switch (mouse.action) { 1599 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 1600 else => {}, 1601 }, 1602 else => return, 1603 } 1604 sc.clampToContent(); 1605 } 1606 1607 // up/down (and tab/shift+tab) move between a form's fields; up from the 1608 // title crosses into the header tabs. the multiline description keeps 1609 // enter and any up/down that has a row to move to. 1610 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1611 const form = self.issueForm() orelse return; 1612 const cid = form.getFocus().child_id orelse return; 1613 const cur = form.children.getIndex(cid) orelse return; 1614 1615 if (cur == description_field_index) { 1616 const child = &form.children.values()[cur]; 1617 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1618 return child.widget.input(allocator, key, root_focus); 1619 } 1620 1621 switch (key) { 1622 .arrow_up, .back_tab => if (cur > 0) 1623 root_focus.setFocus(form.children.keys()[cur - 1]) 1624 else 1625 self.focusHeader(root_focus), 1626 .arrow_down, .tab => if (cur < submit_field_index) { 1627 root_focus.setFocus(form.children.keys()[cur + 1]); 1628 }, 1629 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1630 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 1631 try self.submitForm(allocator); 1632 }, 1633 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1634 } 1635 } 1636 1637 // the multiline hunk inputs keep enter and any up/down that has a row to 1638 // move to; enter on a "use this" link is a navigation the host follows 1639 // before this runs. 1640 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1641 const form = self.issueForm() orelse return; 1642 const cid = form.getFocus().child_id orelse return; 1643 const cur = form.children.getIndex(cid) orelse return; 1644 const child = &form.children.values()[cur]; 1645 1646 if (child.widget == .text_input) { 1647 const ti = &child.widget.text_input; 1648 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1649 return child.widget.input(allocator, key, root_focus); 1650 } 1651 1652 const on_submit = isSubmitButton(&child.widget); 1653 1654 switch (key) { 1655 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1656 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1657 .arrow_left, .arrow_right => switch (child.widget) { 1658 .box => |*row| { 1659 const row_cid = row.getFocus().child_id orelse return; 1660 const row_index = row.children.getIndex(row_cid) orelse return; 1661 if (key == .arrow_left) { 1662 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1663 } else if (row_index + 1 < row.children.count()) { 1664 root_focus.setFocus(row.children.keys()[row_index + 1]); 1665 } 1666 }, 1667 else => try child.widget.input(allocator, key, root_focus), 1668 }, 1669 .enter => if (on_submit) try self.submitResolution(allocator), 1670 .mouse => |mouse| if (on_submit and inp.leftClickOn(root_focus, cid, mouse)) { 1671 try self.submitResolution(allocator); 1672 } else if (mouse.action == .scroll) { 1673 if (self.resolveScroll()) |sc| { 1674 sc.y += if (mouse.action.scroll == .up) @as(isize, -1) else 1; 1675 sc.clampToContent(); 1676 } 1677 }, 1678 else => try child.widget.input(allocator, key, root_focus), 1679 } 1680 } 1681 1682 // a multiline input keeps enter and any up/down that has a row to move to 1683 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1684 return switch (key) { 1685 .enter => true, 1686 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1687 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1688 else => false, 1689 }; 1690 } 1691 1692 // whether the widget is a button that posts its form 1693 fn isSubmitButton(widget: *const ui.Widget) bool { 1694 return switch (widget.*) { 1695 .text_box => |*tb| switch (tb.box.focus.kind) { 1696 .custom => |custom| std.mem.eql(u8, custom, "submit"), 1697 else => false, 1698 }, 1699 else => false, 1700 }; 1701 } 1702 1703 // the neighboring focusable resolve-form child, skipping the spacer and 1704 // the blank gap rows 1705 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1706 var i = cur; 1707 while (true) { 1708 if (down) { 1709 i += 1; 1710 if (i >= form.children.count()) return null; 1711 } else { 1712 if (i == 0) return null; 1713 i -= 1; 1714 } 1715 switch (form.children.values()[i].widget) { 1716 .spacer, .text => continue, 1717 else => return i, 1718 } 1719 } 1720 } 1721 1722 // focus the resolve-form child at `i` (a version row focuses its selected 1723 // child) and keep it visible on the terminal 1724 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 1725 const child = &form.children.values()[i]; 1726 switch (child.widget) { 1727 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 1728 else => root_focus.setFocus(form.children.keys()[i]), 1729 } 1730 if (self.session.is_terminal) { 1731 if (self.resolveScroll()) |sc| { 1732 if (child.rect) |rect| sc.scrollToRect(rect); 1733 } 1734 } 1735 } 1736 1737 // re-emit the conflicted issue's event with the resolved content; any 1738 // event on the issue settles its conflict entry. this is the terminal 1739 // path; the web posts the form to the resolve route. 1740 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 1741 if (comptime wasm) return; 1742 const io = self.session.io orelse return; 1743 const src = self.data.repo_source orelse return; 1744 const entry = self.data.selectedIssue() orelse return; 1745 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1746 const form = self.issueForm() orelse return; 1747 1748 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 1749 var title: ?[]u8 = null; 1750 var tags: ?[]u8 = null; 1751 var hunks: std.ArrayList([]const u8) = .empty; 1752 defer { 1753 if (title) |t| allocator.free(t); 1754 if (tags) |t| allocator.free(t); 1755 for (hunks.items) |h| allocator.free(h); 1756 hunks.deinit(allocator); 1757 } 1758 for (form.children.values()) |*child| switch (child.widget) { 1759 .text_input => |*ti| { 1760 const text = try ti.text(allocator); 1761 errdefer allocator.free(text); 1762 if (std.mem.eql(u8, ti.options.name, "title")) { 1763 title = text; 1764 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 1765 tags = text; 1766 } else { 1767 try hunks.append(allocator, text); 1768 } 1769 }, 1770 else => {}, 1771 }; 1772 1773 var id_bytes: [evt.event_id_size]u8 = undefined; 1774 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1775 1776 switch (src.repo_kind) { 1777 inline else => |repo_kind| { 1778 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1779 defer any_repo.deinit(io, allocator); 1780 switch (any_repo) { 1781 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 1782 .title = title, 1783 .tags = tags, 1784 .hunks = hunks.items, 1785 } }, author_email) catch |err| switch (err) { 1786 // leave the form up for correction 1787 error.InvalidFields => return, 1788 else => |e| return e, 1789 }, 1790 } 1791 }, 1792 } 1793 1794 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1795 try self.session.navigate(route); 1796 } 1797 1798 // the terminal submit paths: the new form commits a new-issue event, the 1799 // edit form re-emits the selected issue's event with the form's content. 1800 // the web posts the forms to their routes instead. 1801 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 1802 if (self.data.view == .edit) { 1803 try self.submitEditedIssue(allocator); 1804 } else { 1805 try self.submitNewIssue(allocator); 1806 } 1807 } 1808 1809 // commit the new issue to the repo's events branch and navigate to it. 1810 // this is the terminal path; the web posts the form to the issue route, 1811 // so the wasm side never runs (or compiles) the repo access below. 1812 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 1813 if (comptime wasm) return; 1814 const io = self.session.io orelse return; 1815 const src = self.data.repo_source orelse return; 1816 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1817 1818 const form = self.issueForm() orelse return; 1819 const title_input = &form.children.values()[title_field_index].widget.text_input; 1820 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1821 const description_input = &form.children.values()[description_field_index].widget.text_input; 1822 1823 const title = try title_input.text(allocator); 1824 defer allocator.free(title); 1825 const tags = try tags_input.text(allocator); 1826 defer allocator.free(tags); 1827 const description = try description_input.text(allocator); 1828 defer allocator.free(description); 1829 1830 if (!evt.Issue.fieldsValid(title, tags)) return; 1831 1832 var id_bytes: [evt.event_id_size]u8 = undefined; 1833 io.random(&id_bytes); 1834 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1835 1836 const event = evt.EventWithId{ 1837 .id = event_id_hex, 1838 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1839 .author_email = author_email, 1840 .event = .{ .issue = .{ 1841 .title = title, 1842 .description = description, 1843 .tags = tags, 1844 } }, 1845 }; 1846 1847 switch (src.repo_kind) { 1848 inline else => |repo_kind| { 1849 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1850 defer any_repo.deinit(io, allocator); 1851 switch (any_repo) { 1852 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1853 } 1854 }, 1855 } 1856 1857 // wipe the form so a return visit starts fresh 1858 title_input.clear(allocator); 1859 tags_input.clear(allocator); 1860 description_input.clear(allocator); 1861 1862 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1863 try self.session.navigate(route); 1864 } 1865 1866 // re-emit the selected issue's event with the form's content (status 1867 // preserved), then reload the issue's page. this is the terminal path; 1868 // the web posts the form to the edit route. 1869 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 1870 if (comptime wasm) return; 1871 const io = self.session.io orelse return; 1872 const src = self.data.repo_source orelse return; 1873 const entry = self.data.selectedIssue() orelse return; 1874 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1875 1876 const form = self.issueForm() orelse return; 1877 const title_input = &form.children.values()[title_field_index].widget.text_input; 1878 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1879 const description_input = &form.children.values()[description_field_index].widget.text_input; 1880 1881 const title = try title_input.text(allocator); 1882 defer allocator.free(title); 1883 const tags = try tags_input.text(allocator); 1884 defer allocator.free(tags); 1885 const description = try description_input.text(allocator); 1886 defer allocator.free(description); 1887 1888 if (!evt.Issue.fieldsValid(title, tags)) return; 1889 1890 var id_bytes: [evt.event_id_size]u8 = undefined; 1891 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1892 1893 switch (src.repo_kind) { 1894 inline else => |repo_kind| { 1895 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1896 defer any_repo.deinit(io, allocator); 1897 switch (any_repo) { 1898 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 1899 .title = title, 1900 .tags = tags, 1901 .description = description, 1902 } }, author_email), 1903 } 1904 }, 1905 } 1906 1907 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1908 try self.session.navigate(route); 1909 } 1910 1911 // flip the shown issue's status by re-emitting its event, then reload the 1912 // page rooted at the issue so the view reflects the change. this is the 1913 // terminal path; the web posts the button's form to the status route. 1914 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1915 if (comptime wasm) return; 1916 const io = self.session.io orelse return; 1917 const src = self.data.repo_source orelse return; 1918 const sel = self.detailed_index[index] orelse return; 1919 const entry = self.window(index).issues[sel]; 1920 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1921 1922 const status: evt.Issue.Status = switch (entry.issue.event.status) { 1923 .open => .closed, 1924 .closed => .open, 1925 }; 1926 1927 var id_bytes: [evt.event_id_size]u8 = undefined; 1928 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1929 1930 switch (src.repo_kind) { 1931 inline else => |repo_kind| { 1932 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1933 defer any_repo.deinit(io, allocator); 1934 switch (any_repo) { 1935 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 1936 } 1937 }, 1938 } 1939 1940 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 1941 try self.session.navigate(route); 1942 } 1943 1944 // arrow keys move the tag selection; at the flow's edges focus crosses to 1945 // the neighboring widgets. 1946 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1947 const tf = self.tagFlow(index) orelse return; 1948 const cid = tf.focus.child_id orelse return; 1949 const cur = tf.indexOfFocusId(cid) orelse return; 1950 const count = tf.text_boxes.items.len; 1951 const sc = self.detailScroll(index); 1952 switch (key) { 1953 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1954 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1955 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 1956 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1957 .home => self.focusTag(index, tf, root_focus, 0), 1958 .end => self.focusTag(index, tf, root_focus, count - 1), 1959 .mouse => |mouse| switch (mouse.action) { 1960 .scroll => |dir| { 1961 sc.y += if (dir == .up) @as(isize, -1) else 1; 1962 sc.clampToContent(); 1963 }, 1964 else => {}, 1965 }, 1966 else => {}, 1967 } 1968 } 1969 1970 const button_in_row_index: usize = 1; 1971 const title_child_index: usize = 0; 1972 const tags_child_index: usize = 2; 1973 1974 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1975 const inner = self.detailInner(index); 1976 if (inner.children.count() <= tags_child_index) return null; 1977 return switch (inner.children.values()[tags_child_index].widget) { 1978 .tag_flow => |*tf| tf, 1979 else => null, 1980 }; 1981 } 1982 1983 fn tagsFocused(self: *View, index: usize) bool { 1984 const inner = self.detailInner(index); 1985 const cid = inner.getFocus().child_id orelse return false; 1986 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1987 } 1988 1989 // the open/close button inside the detail frame's tool row; a logged-out 1990 // session's or conflicted issue's row has none. 1991 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1992 const row = self.toolRow(index); 1993 if (row.children.count() <= button_in_row_index) return null; 1994 const child = &row.children.values()[button_in_row_index].widget; 1995 if (!isSubmitButton(child)) return null; 1996 return &child.text_box; 1997 } 1998 1999 fn toolRowFocused(self: *View, index: usize) bool { 2000 const outer = self.detailOuter(index); 2001 const cid = outer.getFocus().child_id orelse return false; 2002 return outer.children.getIndex(cid) == tool_row_index; 2003 } 2004 2005 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2006 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2007 // keep the tag visible on the terminal: its rect offset by the flow's 2008 // position in the pane. 2009 if (self.session.is_terminal and item < tf.rects.items.len) { 2010 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2011 var rect = tf.rects.items[item]; 2012 rect.x += flow_rect.x; 2013 rect.y += flow_rect.y; 2014 self.detailScroll(index).scrollToRect(rect); 2015 } 2016 } 2017 } 2018 2019 fn titleFocused(self: *View, index: usize) bool { 2020 const inner = self.detailInner(index); 2021 const cid = inner.getFocus().child_id orelse return false; 2022 return inner.children.getIndex(cid) == title_child_index; 2023 } 2024 2025 fn authorPresent(self: *View, index: usize) bool { 2026 return self.author_id[index] != null; 2027 } 2028 2029 fn authorFocused(self: *View, index: usize) bool { 2030 const inner = self.detailInner(index); 2031 const cid = inner.getFocus().child_id orelse return false; 2032 return cid == self.author_id[index]; 2033 } 2034 2035 // the author sits right under the title at the top of the pane, so 2036 // focusing it scrolls there. 2037 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 2038 const id = self.author_id[index] orelse return; 2039 root_focus.setFocus(id); 2040 self.detailScroll(index).y = 0; 2041 } 2042 2043 // the title sits at the top of the pane, so focusing it scrolls there. 2044 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2045 const inner = self.detailInner(index); 2046 if (inner.children.count() == 0) return; 2047 root_focus.setFocus(inner.children.keys()[title_child_index]); 2048 self.detailScroll(index).y = 0; 2049 } 2050 2051 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2052 const tf = self.tagFlow(index) orelse return; 2053 if (tf.text_boxes.items.len == 0) return; 2054 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2055 const item = tf.indexOfFocusId(cid) orelse 0; 2056 self.focusTag(index, tf, root_focus, item); 2057 } 2058 2059 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2060 if (self.description_id[index]) |id| root_focus.setFocus(id); 2061 } 2062 2063 // return to the tool row's last-focused button (the header when the 2064 // description page shows no row). 2065 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2066 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2067 root_focus.setFocus(cid); 2068 } 2069 2070 // enter the detail pane. an empty pane (no issues) can't be entered. 2071 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2072 if (self.detailInner(index).children.count() == 0) return; 2073 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2074 } 2075 2076 // return to the list. 2077 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2078 root_focus.setFocus(self.listScroll(index).getFocus().id); 2079 } 2080 2081 // cross to the header tabs above the stack. 2082 fn focusHeader(self: *View, root_focus: *Focus) void { 2083 root_focus.setFocus(self.box.children.keys()[header_index]); 2084 } 2085 2086 pub fn clearGrid(self: *View) void { 2087 self.box.clearGrid(); 2088 } 2089 2090 pub fn getGrid(self: View) ?Grid { 2091 return self.box.getGrid(); 2092 } 2093 2094 pub fn getFocus(self: *View) *Focus { 2095 return self.box.getFocus(); 2096 } 2097 2098 // for the parent's "scroll up at the top jumps to the header" check: at the 2099 // top only while the header tabs hold focus, so up from the split 2100 // crosses into the tabs first. 2101 pub fn getSelectedIndex(self: *View) ?usize { 2102 return if (self.headerActive()) 0 else 1; 2103 } 2104 }; 2105 2106 // the "a:" navigation link for `status`'s issues page filtered to the 2107 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2108 // a rooted url ignores `status` (it derives its view from the issue's own); 2109 // with an empty `id` this is the bare list link. 2110 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2111 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2112 const url = try route.toUrl(page_arena); 2113 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2114 } 2115 2116 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2117 // href is only followed with js off. 2118 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2119 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2120 const url = try route.toUrl(page_arena); 2121 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2122 } 2123 2124 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2125 // or the conflicts window when null, rooted at issue `id`. 2126 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2127 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2128 return conflictsLink(page_arena, data.identity, id); 2129 } 2130 2131 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2132 // first window). 2133 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2134 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2135 const url = try route.toUrl(page_arena); 2136 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2137 } 2138 2139 // the "a:" link to the whole-description page of issue `id` within `identity`. 2140 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2141 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2142 const url = try route.toUrl(page_arena); 2143 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2144 } 2145 2146 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2147 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2148 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2149 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2150 const url = try route.toUrl(page_arena); 2151 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2152 } 2153 2154 // tabs switching between the issues page's views. 2155 pub const Header = struct { 2156 box: wgt.Box(ui.Widget), 2157 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2158 2159 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2160 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2161 errdefer box.deinit(allocator); 2162 2163 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2164 errdefer tab_ids.deinit(allocator); 2165 2166 const aa = session.page_arena.allocator(); 2167 2168 // a list tab per status, labeled with its listing's issue count 2169 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2170 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2171 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2172 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2173 try addTab(allocator, &box, &tab_ids, label, link); 2174 } 2175 2176 // tags tab, labeled with the active tag filter 2177 { 2178 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2179 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2180 const label = if (data.tag.len == 0) "tags" else blk: { 2181 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2182 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2183 }; 2184 try addTab(allocator, &box, &tab_ids, label, tags_link); 2185 } 2186 2187 // new-issue tab; an edit or resolve url shows its tab in this place 2188 { 2189 const route = switch (data.view) { 2190 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2191 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2192 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2193 }; 2194 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2195 const label: []const u8 = switch (data.view) { 2196 .edit => "edit", 2197 .resolve => "resolve", 2198 else => "new", 2199 }; 2200 try addTab(allocator, &box, &tab_ids, label, link); 2201 } 2202 2203 // conflicts tab, labeled with the conflict count 2204 if (data.conflicts.count > 0) { 2205 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2206 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2207 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2208 try addTab(allocator, &box, &tab_ids, label, link); 2209 } 2210 2211 var self = Header{ .box = box, .tab_ids = tab_ids }; 2212 // the tab matching the page's view is selected initially. 2213 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2214 return self; 2215 } 2216 2217 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2218 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2219 errdefer text_box.deinit(allocator); 2220 text_box.getFocus().focusable = true; 2221 text_box.getFocus().kind = .{ .custom = link }; 2222 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2223 try box.children.put(allocator, text_box.getFocus().id, .{ 2224 .widget = .{ .text_box = text_box }, 2225 .rect = null, 2226 .min_size = .{ .width = label.len + 2, .height = null }, 2227 }); 2228 } 2229 2230 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2231 self.box.deinit(allocator); 2232 self.tab_ids.deinit(allocator); 2233 } 2234 2235 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2236 self.clearGrid(); 2237 // only the selected tab shows its border 2238 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2239 switch (child.widget) { 2240 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2241 else => {}, 2242 } 2243 } 2244 try self.box.build(allocator, constraint, root_focus); 2245 } 2246 2247 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2248 _ = allocator; 2249 const current_tab = self.currentTabIndex() orelse return; 2250 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2251 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2252 } 2253 } 2254 2255 pub fn clearGrid(self: *Header) void { 2256 self.box.clearGrid(); 2257 } 2258 2259 pub fn getGrid(self: Header) ?Grid { 2260 return self.box.getGrid(); 2261 } 2262 2263 pub fn getFocus(self: *Header) *Focus { 2264 return self.box.getFocus(); 2265 } 2266 2267 pub fn getSelectedIndex(self: Header) ?usize { 2268 return self.currentTabIndex(); 2269 } 2270 2271 fn currentTabIndex(self: Header) ?usize { 2272 const child_id = self.box.focus.child_id orelse return null; 2273 return self.tab_ids.getIndex(child_id); 2274 } 2275 }; 2276