╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const pch = @import("../../patch.zig"); 4 const evt = @import("../../event.zig"); 5 const ui = @import("../../ui.zig"); 6 const widget = @import("../widget.zig"); 7 const xit = @import("xit"); 8 const rp = xit.repo; 9 const hash = xit.hash; 10 const xitui = xit.xitui; 11 const wgt = xitui.widget; 12 const layout = xitui.layout; 13 const Key = xitui.input.Key; 14 const Grid = xitui.grid.Grid; 15 const Focus = xitui.focus.Focus; 16 const inp = @import("../input.zig"); 17 const srch_thrd = @import("../../search_thread.zig"); 18 const diff3 = @import("../../diff3.zig"); 19 const Comment = @import("../Repo/Comment.zig"); 20 const Attachment = @import("../Repo/Attachment.zig"); 21 const Markdown = @import("../Repo/Markdown.zig"); 22 23 const Widget = widget.Widget; 24 const WordFlow = widget.WordFlow; 25 const Center = widget.Center; 26 const SubmitButton = widget.SubmitButton; 27 const Spacer = widget.Spacer; 28 const SectionLabel = widget.SectionLabel; 29 const moveRowFocus = widget.moveRowFocus; 30 const wasm = builtin.target.cpu.arch == .wasm32; 31 32 fn mergeBottomLabel(status: pch.Mergeability.Status) []const u8 { 33 return switch (status) { 34 .clean => "", 35 .conflict => " conflict ", 36 .unknown => " unavailable ", 37 }; 38 } 39 40 // read the selected thread's conflicted fields and attribute each side to its 41 // event commit author 42 pub fn readConflict( 43 comptime Data: type, 44 comptime repo_kind: rp.RepoKind, 45 comptime repo_opts: rp.RepoOpts(repo_kind), 46 arena: *std.heap.ArenaAllocator, 47 repo: *rp.Repo(repo_kind, repo_opts), 48 io: std.Io, 49 admin_moment: ?evt.AdminDB.HashMap(.read_only), 50 haxy_moment: evt.EventDB(repo_opts.hash).HashMap(.read_only), 51 id_bytes: *const [evt.event_id_size]u8, 52 ours: Data.Event.Record, 53 conflict_entry: evt.EventDB(repo_opts.hash).HashMap(.read_only), 54 ) !Data.Conflict { 55 const Event = Data.Event; 56 const DB = evt.EventDB(repo_opts.hash); 57 const aa = arena.allocator(); 58 59 const fields_cursor = (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key))) orelse return error.NotFound; 60 const conflicted_fields = try fields_cursor.readBytesAlloc(aa, null); 61 const their_cursor = (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key))) orelse return error.NotFound; 62 const theirs = try evt.read(Event.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(their_cursor)); 63 64 // absent when both sides created the thread independently 65 var base: ?Event.Record = null; 66 if (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.base_record_key))) |base_cursor| { 67 base = try evt.read(Event.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(base_cursor)); 68 } 69 70 var their_field_oids: ?DB.SortedMap(.read_only) = null; 71 if (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_field_to_oid_key))) |cursor| 72 their_field_oids = try DB.SortedMap(.read_only).init(cursor); 73 var our_field_oids: ?DB.SortedMap(.read_only) = null; 74 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, Event.id_to_field_to_oid_key))) |map_cursor| { 75 const id_to_field_to_oid = try DB.HashMap(.read_only).init(map_cursor); 76 if (try id_to_field_to_oid.getCursor(hash.hashInt(repo_opts.hash, id_bytes))) |cursor| 77 our_field_oids = try DB.SortedMap(.read_only).init(cursor); 78 } 79 80 var conflict = Data.Conflict{}; 81 var field_iter = std.mem.splitScalar(u8, conflicted_fields, ' '); 82 while (field_iter.next()) |field| { 83 const our_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, our_field_oids, field)); 84 const their_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, their_field_oids, field)); 85 if (std.mem.eql(u8, field, "title")) { 86 conflict.title = .{ 87 .ours = .{ .text = ours.event.title, .author = our_author }, 88 .theirs = .{ .text = theirs.event.title, .author = their_author }, 89 }; 90 } else if (std.mem.eql(u8, field, "labels")) { 91 conflict.labels = .{ 92 .ours = .{ .text = ours.event.labels, .author = our_author }, 93 .theirs = .{ .text = theirs.event.labels, .author = their_author }, 94 }; 95 } else if (std.mem.eql(u8, field, "description")) { 96 conflict.description = .{ 97 .chunks = try diff3.chunks(arena.child_allocator, arena, if (base) |b| b.event.description else "", ours.event.description, theirs.event.description), 98 .ours_author = our_author, 99 .theirs_author = their_author, 100 }; 101 } else if (comptime @hasField(Data.Conflict, "status")) { 102 if (std.mem.eql(u8, field, "status")) { 103 conflict.status = .{ 104 .ours = .{ .text = @tagName(Data.statusKind(ours.event)), .author = our_author }, 105 .theirs = .{ .text = @tagName(Data.statusKind(theirs.event)), .author = their_author }, 106 }; 107 } else if (std.mem.eql(u8, field, "revision")) { 108 conflict.revision = .{ 109 .ours = .{ .text = try revisionSummary(aa, ours.event.revision), .author = our_author }, 110 .theirs = .{ .text = try revisionSummary(aa, theirs.event.revision), .author = their_author }, 111 }; 112 } else if (std.mem.eql(u8, field, "target_branch")) { 113 conflict.target_branch = .{ 114 .ours = .{ .text = ours.event.target_branch, .author = our_author }, 115 .theirs = .{ .text = theirs.event.target_branch, .author = their_author }, 116 }; 117 } 118 } 119 } 120 return conflict; 121 } 122 123 fn revisionSummary(allocator: std.mem.Allocator, revision_maybe: ?evt.Patch.Revision) ![]const u8 { 124 const revision = revision_maybe orelse return "(none)"; 125 return allocator.print("squash {s}\nsource {s}", .{ revision.squash_oid, revision.source_oid }); 126 } 127 128 fn readFieldOid( 129 comptime hash_kind: hash.HashKind, 130 field_oids_maybe: ?evt.EventDB(hash_kind).SortedMap(.read_only), 131 field: []const u8, 132 ) !?[hash.byteLen(hash_kind)]u8 { 133 const field_oids = field_oids_maybe orelse return null; 134 const cursor = (try field_oids.getCursor(field)) orelse return null; 135 var oid: [hash.byteLen(hash_kind)]u8 = undefined; 136 _ = try cursor.readBytes(&oid); 137 return oid; 138 } 139 140 fn oidAuthor( 141 comptime repo_kind: rp.RepoKind, 142 comptime repo_opts: rp.RepoOpts(repo_kind), 143 arena: *std.heap.ArenaAllocator, 144 repo: *rp.Repo(repo_kind, repo_opts), 145 io: std.Io, 146 admin_moment: ?evt.AdminDB.HashMap(.read_only), 147 oid_maybe: ?[hash.byteLen(repo_opts.hash)]u8, 148 ) !ui.Author { 149 const oid = oid_maybe orelse return .unknown; 150 const gpa = arena.child_allocator; 151 var start_oids = [_][hash.hexLen(repo_opts.hash)]u8{std.fmt.bytesToHex(oid, .lower)}; 152 var commit_iter = repo.log(io, gpa, .{ .start_oids = start_oids[0..1] }) catch return .unknown; 153 defer commit_iter.deinit(); 154 const commit_object = (commit_iter.next(gpa) catch return .unknown) orelse return .unknown; 155 defer commit_object.deinit(); 156 return try ui.Author.init(admin_moment, arena, commit_object.content.commit.metadata.author orelse ""); 157 } 158 159 pub fn statusSet( 160 comptime Data: type, 161 comptime DB: type, 162 statuses: DB.SortedMap(.read_only), 163 status: Data.Status, 164 ) !?DB.SortedSet(.read_only) { 165 const cursor = (try statuses.getCursor(@tagName(status))) orelse return null; 166 return try DB.SortedSet(.read_only).init(cursor); 167 } 168 169 pub fn labelStatusSet( 170 comptime Data: type, 171 comptime DB: type, 172 label_statuses: DB.SortedMap(.read_only), 173 label: []const u8, 174 status: Data.Status, 175 ) !?DB.SortedSet(.read_only) { 176 var key_buffer: Data.Event.LabelStatusKey = undefined; 177 const key = Data.Event.labelStatusKey(&key_buffer, label, status) catch return null; 178 const cursor = (try label_statuses.getCursor(key)) orelse return null; 179 return try DB.SortedSet(.read_only).init(cursor); 180 } 181 182 pub fn loadLabels( 183 comptime Data: type, 184 comptime hash_kind: hash.HashKind, 185 arena: *std.heap.ArenaAllocator, 186 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 187 ) ![]const []const u8 { 188 const aa = arena.allocator(); 189 const DB = evt.EventDB(hash_kind); 190 const cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, Data.Event.label_status_to_id_set_key)) orelse return &.{}; 191 const label_statuses = try DB.SortedMap(.read_only).init(cursor); 192 var labels: std.ArrayList([]const u8) = .empty; 193 var iter = try label_statuses.iterator(); 194 while (try iter.next()) |pair_cursor| { 195 if (labels.items.len == Data.max_labels) break; 196 var entry_cursor = pair_cursor; 197 const pair = try entry_cursor.readKeyValuePair(); 198 const key = try pair.key_cursor.readBytesAlloc(aa, null); 199 const space = std.mem.indexOfScalar(u8, key, ' ') orelse continue; 200 const label = key[0..space]; 201 if (labels.last()) |last| { 202 if (std.mem.eql(u8, last, label)) continue; 203 } 204 try labels.append(aa, label); 205 } 206 return labels.items; 207 } 208 209 // the key a list set (and the search index) stores a thread under 210 const OrderKey = [@sizeOf(u64) + evt.event_id_size]u8; 211 212 // a search result's bytes as an order key 213 fn resultKey(key: []const u8) !OrderKey { 214 if (key.len != @sizeOf(u64) + evt.event_id_size) return error.InvalidSearchIndex; 215 var order_key: OrderKey = undefined; 216 @memcpy(&order_key, key); 217 return order_key; 218 } 219 220 // the hex event id an order key ends with 221 fn keyIdHex(order_key: *const OrderKey) [evt.event_id_size * 2]u8 { 222 return std.fmt.bytesToHex(order_key[@sizeOf(u64)..].*, .lower); 223 } 224 225 pub fn loadWindow( 226 comptime Data: type, 227 comptime kind: evt.EventKind, 228 comptime hash_kind: hash.HashKind, 229 arena: *std.heap.ArenaAllocator, 230 admin_moment: ?evt.AdminDB.HashMap(.read_only), 231 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 232 records: evt.EventDB(hash_kind).HashMap(.read_only), 233 set_maybe: ?evt.EventDB(hash_kind).SortedSet(.read_only), 234 root_key: ?[]const u8, 235 conflict_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 236 selected_id: []const u8, 237 comments_start: usize, 238 // the search the set is narrowed to, or null to list it whole 239 query: ?[]const u8, 240 // a second set the listing is narrowed to, keyed like `set_maybe` 241 extra_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 242 viewer: ?ui.Viewer, 243 ) !Data.Window { 244 if (query != null or extra_set != null) return loadSearchWindow(Data, kind, hash_kind, arena, admin_moment, haxy_moment, records, set_maybe, root_key, conflict_set, selected_id, comments_start, query orelse "", extra_set, viewer); 245 const set = set_maybe orelse return .empty; 246 const aa = arena.allocator(); 247 248 var prev_id: ?[]const u8 = null; 249 var iter = if (root_key) |key| blk: { 250 const rank = try set.rank(key); 251 if (rank > 0 and rank <= Data.page_size) { 252 prev_id = ""; 253 } else if (rank > Data.page_size) { 254 const pair = try set.getIndexKeyValuePair(@intCast(rank - Data.page_size)) orelse return error.NotFound; 255 var previous: OrderKey = undefined; 256 _ = try pair.key_cursor.readBytes(&previous); 257 prev_id = try aa.dupe(u8, &keyIdHex(&previous)); 258 } 259 break :blk try set.iteratorFrom(key); 260 } else try set.iteratorFromIndex(0); 261 262 var items: std.ArrayList(Data.Entry) = .empty; 263 var next_id: ?[]const u8 = null; 264 while (try iter.next()) |cursor_value| { 265 var cursor = cursor_value; 266 const pair = try cursor.readKeyValuePair(); 267 var order_key: OrderKey = undefined; 268 _ = try pair.key_cursor.readBytes(&order_key); 269 if (items.items.len == Data.page_size) { 270 next_id = try aa.dupe(u8, &keyIdHex(&order_key)); 271 break; 272 } 273 const entry = (try loadEntry(Data, hash_kind, arena, admin_moment, haxy_moment, records, &order_key, conflict_set, selected_id, comments_start, viewer)) orelse continue; 274 try items.append(aa, entry); 275 } 276 277 return .{ 278 .items = items.items, 279 .prev_id = prev_id, 280 .next_id = next_id, 281 .count = @intCast(try set.count()), 282 }; 283 } 284 285 // how many matches one search counts before the count is marked as capped 286 const max_search_results = 1000; 287 288 // `set`'s entries matching `query_text` (any when empty) and held by 289 // `extra_set` when set, windowed like `loadWindow`. one pass 290 // yields the window, its neighbours' roots and the match count, which stops at 291 // the cap. 292 fn loadSearchWindow( 293 comptime Data: type, 294 comptime kind: evt.EventKind, 295 comptime hash_kind: hash.HashKind, 296 arena: *std.heap.ArenaAllocator, 297 admin_moment: ?evt.AdminDB.HashMap(.read_only), 298 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 299 records: evt.EventDB(hash_kind).HashMap(.read_only), 300 set_maybe: ?evt.EventDB(hash_kind).SortedSet(.read_only), 301 root_key: ?[]const u8, 302 conflict_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 303 selected_id: []const u8, 304 comments_start: usize, 305 query_text: []const u8, 306 extra_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 307 viewer: ?ui.Viewer, 308 ) !Data.Window { 309 const set = set_maybe orelse return .empty; 310 const DB = evt.EventDB(hash_kind); 311 const aa = arena.allocator(); 312 const root = root_key orelse ""; 313 314 // the matching list keys, in list order. the cap bounds the count, not the 315 // paging: past it a pass goes on only until the window and its "next" link 316 // are known. 317 var keys: std.ArrayList(OrderKey) = .empty; 318 if (comptime kind == .discuss) { 319 // discussions list by activity but are indexed by creation, so the 320 // matching ids are gathered and the listed set is walked in its order 321 // until every one has been found 322 var matches: std.AutoHashMapUnmanaged([evt.event_id_size]u8, void) = .empty; 323 var results = try srch_thrd.query(DB, hash_kind, kind, haxy_moment, aa, query_text, &.{extra_set}); 324 while (try results.next()) |key| try matches.put(aa, (try resultKey(key))[@sizeOf(u64)..].*, {}); 325 326 var iter = try set.iteratorFromIndex(0); 327 while (matches.count() > 0) { 328 const pair = try ((try iter.next()) orelse break).readKeyValuePair(); 329 var order_key: OrderKey = undefined; 330 _ = try pair.key_cursor.readBytes(&order_key); 331 if (!matches.remove(order_key[@sizeOf(u64)..].*)) continue; 332 try keys.append(aa, order_key); 333 if (windowKnown(Data, keys.items, root)) break; 334 } 335 } else { 336 // issues and patches list in the index's own order, so the listed set 337 // narrows the query and the results stream out already sorted 338 var results = try srch_thrd.query(DB, hash_kind, kind, haxy_moment, aa, query_text, &.{ set, extra_set }); 339 while (try results.next()) |key| { 340 try keys.append(aa, try resultKey(key)); 341 if (windowKnown(Data, keys.items, root)) break; 342 } 343 } 344 345 // the window starts at the first key at or after the root 346 var first: usize = 0; 347 while (first < keys.items.len and std.mem.order(u8, &keys.items[first], root) == .lt) first += 1; 348 349 var items: std.ArrayList(Data.Entry) = .empty; 350 const last = @min(first + Data.page_size, keys.items.len); 351 for (keys.items[first..last]) |*order_key| { 352 const entry = (try loadEntry(Data, hash_kind, arena, admin_moment, haxy_moment, records, order_key, conflict_set, selected_id, comments_start, viewer)) orelse continue; 353 try items.append(aa, entry); 354 } 355 356 return .{ 357 .items = items.items, 358 // the page a window back, which is the first one when it holds the root 359 .prev_id = if (first == 0) null else if (first <= Data.page_size) "" else try aa.dupe(u8, &keyIdHex(&keys.items[first - Data.page_size])), 360 .next_id = if (last < keys.items.len) try aa.dupe(u8, &keyIdHex(&keys.items[last])) else null, 361 .count = @min(keys.items.len, max_search_results), 362 .more = keys.items.len > max_search_results, 363 }; 364 } 365 366 // whether a search pass can stop: the count is capped, and the window rooted at 367 // `root` and the key after it have been collected 368 fn windowKnown(comptime Data: type, keys: []const OrderKey, root: []const u8) bool { 369 if (keys.len <= max_search_results) return false; 370 return std.mem.order(u8, &keys[keys.len - 1 - Data.page_size], root) != .lt; 371 } 372 373 // one list entry: the record `order_key` names, with its author, comments, 374 // attachments and assignees. null when the record is gone. 375 fn loadEntry( 376 comptime Data: type, 377 comptime hash_kind: hash.HashKind, 378 arena: *std.heap.ArenaAllocator, 379 admin_moment: ?evt.AdminDB.HashMap(.read_only), 380 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 381 records: evt.EventDB(hash_kind).HashMap(.read_only), 382 order_key: *const OrderKey, 383 conflict_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 384 selected_id: []const u8, 385 comments_start: usize, 386 viewer: ?ui.Viewer, 387 ) !?Data.Entry { 388 const DB = evt.EventDB(hash_kind); 389 const aa = arena.allocator(); 390 const id_hex = keyIdHex(order_key); 391 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, order_key[@sizeOf(u64)..])) orelse return null; 392 const record = try evt.read(Data.Event.Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 393 var entry: Data.Entry = .{ 394 .id = try aa.dupe(u8, &id_hex), 395 .record = record, 396 .author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email), 397 .comments = try Comment.loadWindow( 398 hash_kind, 399 arena, 400 admin_moment, 401 haxy_moment, 402 evt.Comment.thread_id_to_comment_id_set_key, 403 &id_hex, 404 if (std.mem.eql(u8, &id_hex, selected_id)) comments_start else 0, 405 viewer, 406 ), 407 .attachments = try Attachment.load(hash_kind, arena, haxy_moment, &id_hex), 408 .assignees = try loadAssignees(hash_kind, arena, admin_moment, haxy_moment, &id_hex), 409 .can_modify = if (viewer) |v| v.role.canModify(v.email, record.author_email) else false, 410 }; 411 if (comptime @hasField(Data.Entry, "conflicted")) { 412 entry.conflicted = if (conflict_set) |conflicts| try conflicts.contains(order_key) else false; 413 } 414 return entry; 415 } 416 417 // a thread's assignees, each named after the user holding its email when one does 418 fn loadAssignees( 419 comptime hash_kind: hash.HashKind, 420 arena: *std.heap.ArenaAllocator, 421 admin_moment: ?evt.AdminDB.HashMap(.read_only), 422 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 423 thread_id: []const u8, 424 ) ![]const ui.Assignee { 425 const emails = try evt.Assignment.load(hash_kind, arena, haxy_moment, thread_id); 426 const assignees = try arena.allocator().alloc(ui.Assignee, emails.len); 427 for (assignees, emails) |*assignee, email| { 428 const user = if (admin_moment) |moment| try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email) else null; 429 assignee.* = .{ .email = email, .name = if (user) |u| u.name else null }; 430 } 431 return assignees; 432 } 433 434 // the url-encoded assignee filter a thread page carries, "" for kinds without one 435 fn assigneeFilter(data: anytype) []const u8 { 436 return if (@hasField(@TypeOf(data.*), "assignee")) data.assignee else ""; 437 } 438 439 // a tab link's route, carrying the page's query when it has one 440 pub fn searchRoute(route_maybe: ?ui.RoutablePage, search: ?[]const u8) !ui.RoutablePage { 441 const route = route_maybe orelse return error.RouteTooLong; 442 const query = search orelse return route; 443 return route.withSearch(query) orelse error.RouteTooLong; 444 } 445 446 // a status tab's label: its listing's count, marked when a search capped it 447 pub fn countLabel(buffer: []u8, name: []const u8, win: anytype) ![]const u8 { 448 return std.fmt.bufPrint(buffer, "{s} ({d}{s})", .{ name, win.count, if (win.more) "+" else "" }); 449 } 450 451 // the common sub-header used by each thread page: the search box, the 452 // assignee box on the issues page, then the tab strip 453 pub const Header = struct { 454 box: wgt.Box(Widget), 455 // focus id -> semantic stack index; some pages omit unavailable tabs 456 tab_ids: std.AutoArrayHashMapUnmanaged(usize, usize), 457 // the tab whose view the stack shows. focus lands on the boxes too, 458 // so the selection is remembered rather than read off the focus chain. 459 selected_tab: ?usize = null, 460 // how many text boxes come before the tabs 461 box_count: usize, 462 463 // which box enter submitted from 464 pub const Submission = struct { kind: enum { search, assignee }, text: []u8 }; 465 466 // the assignee box's label and prefilled text 467 pub const AssigneeBox = struct { label: []const u8, text: []const u8 }; 468 469 // a null `assignee` leaves out the assignee box 470 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, search: ?[]const u8, assignee: ?AssigneeBox) !Header { 471 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 472 errdefer box.deinit(allocator); 473 474 { 475 var search_box = try widget.SearchBox.init(allocator, session, " search ", "search", search); 476 errdefer search_box.deinit(allocator); 477 box.getFocus().child_id = search_box.getFocus().id; 478 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = widget.SearchBox.min_size }); 479 } 480 481 if (assignee) |value| { 482 var assignee_box = try widget.SearchBox.init(allocator, session, value.label, "assignee", value.text); 483 errdefer assignee_box.deinit(allocator); 484 try box.children.put(allocator, assignee_box.getFocus().id, .{ .widget = .{ .search_box = assignee_box }, .rect = null, .min_size = widget.SearchBox.min_size }); 485 } 486 487 return .{ .box = box, .tab_ids = .empty, .box_count = box.children.count() }; 488 } 489 490 pub fn addTab(self: *Header, allocator: std.mem.Allocator, label: []const u8, link: []const u8, view_index: usize) !void { 491 var text_box = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 492 errdefer text_box.deinit(allocator); 493 text_box.getFocus().mode = .all; 494 text_box.getFocus().kind = .{ .custom = link }; 495 try self.tab_ids.put(allocator, text_box.getFocus().id, view_index); 496 try self.box.children.put(allocator, text_box.getFocus().id, .{ 497 .widget = .{ .text_box = text_box }, 498 .rect = null, 499 .min_size = .{ .width = label.len + 2, .height = null }, 500 }); 501 } 502 503 pub fn select(self: *Header, view_index: usize) void { 504 for (self.tab_ids.values(), 0..) |index, tab| { 505 if (index == view_index) { 506 self.selected_tab = tab; 507 return; 508 } 509 } 510 } 511 512 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 513 self.box.deinit(allocator); 514 self.tab_ids.deinit(allocator); 515 } 516 517 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 518 self.clearGrid(); 519 // the search box takes focus without changing the view, so the tab it 520 // was last on stays selected 521 if (self.currentTabIndex()) |tab| self.selected_tab = tab; 522 // only the selected tab shows its border 523 const selected_id = if (self.selectedTab()) |tab| self.tab_ids.keys()[tab] else null; 524 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 525 switch (child.widget) { 526 .text_box => |*tb| widget.markSelected(tb, selected_id == id), 527 else => {}, 528 } 529 } 530 // a row taller than the tabs, leaving a blank line beneath them. the 531 // parent sets no min, which would make it lay the focused content out first. 532 var header_constraint = constraint; 533 header_constraint.min_size.height = @max(constraint.min_size.height orelse 0, 4); 534 try self.box.build(allocator, header_constraint, root_focus); 535 } 536 537 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 538 if (self.focusedBox()) |index| { 539 const search_box = self.searchBox(index); 540 // tab, and right-arrow past the last character, step to the next box or the tabs 541 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 542 root_focus.setFocus(if (index + 1 < self.box_count) self.searchBox(index + 1).getFocus().id else self.tab_ids.keys()[0]); 543 return; 544 } 545 // back-tab, and left-arrow before the first character, step to the previous box 546 if (index > 0 and (key == .back_tab or (key == .arrow_left and search_box.text_input.cursor == 0))) { 547 root_focus.setFocus(self.searchBox(index - 1).getFocus().id); 548 return; 549 } 550 return search_box.input(allocator, key, root_focus); 551 } 552 const current_tab = self.currentTabIndex() orelse return; 553 if (key == .back_tab or (key == .arrow_left and current_tab == 0)) { 554 // remembered here as well as in build, which may not run between keys 555 self.selected_tab = current_tab; 556 root_focus.setFocus(self.searchBox(self.box_count - 1).getFocus().id); 557 return; 558 } 559 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 560 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 561 } 562 } 563 564 // the typed text when enter should submit it: a box holds focus. the 565 // caller owns the returned text. 566 pub fn submittedText(self: *Header, allocator: std.mem.Allocator) !?Submission { 567 const index = self.focusedBox() orelse return null; 568 return .{ .kind = if (index == 0) .search else .assignee, .text = try self.searchBox(index).text(allocator) }; 569 } 570 571 // the index of the box holding focus, if one does 572 fn focusedBox(self: *Header) ?usize { 573 const child_id = self.box.getFocus().child_id orelse return null; 574 const index = self.box.children.getIndex(child_id) orelse return null; 575 return if (index < self.box_count) index else null; 576 } 577 578 fn searchBox(self: *Header, index: usize) *widget.SearchBox { 579 return &self.box.children.values()[index].widget.search_box; 580 } 581 582 pub fn clearGrid(self: *Header) void { 583 self.box.clearGrid(); 584 } 585 586 pub fn getGrid(self: Header) ?Grid { 587 return self.box.getGrid(); 588 } 589 590 pub fn getFocus(self: *Header) *Focus { 591 return self.box.getFocus(); 592 } 593 594 pub fn getSelectedIndex(self: Header) ?usize { 595 const tab = self.selectedTab() orelse return null; 596 return self.tab_ids.values()[tab]; 597 } 598 599 // the focused tab, or the one focus last left behind 600 fn selectedTab(self: Header) ?usize { 601 return self.currentTabIndex() orelse self.selected_tab; 602 } 603 604 fn currentTabIndex(self: Header) ?usize { 605 const child_id = self.box.focus.child_id orelse return null; 606 return self.tab_ids.getIndex(child_id); 607 } 608 }; 609 610 // the selected thread's scrollable contents, shared by repo and fork views 611 // the assignee row's form action, which also identifies the row 612 const assign_suffix = "/assign"; 613 614 // an input that assigns on enter, in a form of its own 615 fn appendAssigneeRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, thread_url: []const u8) !void { 616 const pa = session.page_arena.allocator(); 617 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 618 errdefer row.deinit(allocator); 619 row.getFocus().kind = .{ .custom = try pa.print("form:{s}" ++ assign_suffix, .{thread_url}) }; 620 621 var text_input = try wgt.TextInput.init(allocator, .{ .name = "assignee", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 622 errdefer text_input.deinit(allocator); 623 text_input.getFocus().mode = .all; 624 if (session.formFeedback(.assignment)) |saved| try text_input.setContent(allocator, saved.text); 625 row.getFocus().child_id = text_input.getFocus().id; 626 try row.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 627 628 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 629 } 630 631 const unassign_suffix = "/unassign"; 632 const assignee_seg = "/assignee:"; 633 634 // one row per assignee: its name or email, linking to the issues assigned to 635 // it, and for an editor a ✕ that unassigns 636 fn appendAssignedRows( 637 allocator: std.mem.Allocator, 638 box: *wgt.Box(Widget), 639 session: *ui.Session, 640 thread_url: []const u8, 641 assignees: []const ui.Assignee, 642 links: []const []const u8, 643 can_modify: bool, 644 ) !void { 645 const pa = session.page_arena.allocator(); 646 for (assignees, links) |assignee, link| { 647 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 648 errdefer row.deinit(allocator); 649 650 var tb = try wgt.TextBox.init(allocator, assignee.display(), .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " assigned " } }); 651 errdefer tb.deinit(allocator); 652 tb.getFocus().mode = .all; 653 tb.getFocus().kind = .{ .custom = link }; 654 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 655 656 if (can_modify) { 657 const action = try pa.print("{s}" ++ assignee_seg ++ "{s}" ++ unassign_suffix, .{ thread_url, try ui.urlEncodeRef(pa, assignee.email) }); 658 // the web renders a submit button only inside a form 659 row.getFocus().kind = .{ .custom = try pa.print("form:{s}", .{action}) }; 660 var remove = try wgt.TextBox.init(allocator, "✕", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 661 errdefer remove.deinit(allocator); 662 remove.getFocus().mode = .all; 663 remove.getFocus().kind = .{ .custom = try pa.print("{s}{s}", .{ ui.submit_action_prefix, action }) }; 664 try row.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 665 } 666 667 row.getFocus().child_id = row.children.keys()[0]; 668 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 669 } 670 } 671 672 // the url-encoded email an assignee row's ✕ unassigns 673 fn unassignEmail(row: *wgt.Box(Widget)) ?[]const u8 { 674 if (row.children.count() < 2) return null; 675 const action = switch (row.children.values()[1].widget.getFocus().kind) { 676 .custom => |custom| custom, 677 else => return null, 678 }; 679 if (!std.mem.startsWith(u8, action, ui.submit_action_prefix) or !std.mem.endsWith(u8, action, unassign_suffix)) return null; 680 const at = std.mem.lastIndexOf(u8, action, assignee_seg) orelse return null; 681 return action[at + assignee_seg.len .. action.len - unassign_suffix.len]; 682 } 683 684 // whether `row` is the assignee row 685 fn isAssigneeRow(row: *wgt.Box(Widget)) bool { 686 const action = switch (row.getFocus().kind) { 687 .custom => |custom| custom, 688 else => return false, 689 }; 690 return std.mem.startsWith(u8, action, "form:") and std.mem.endsWith(u8, action, assign_suffix); 691 } 692 693 // label the row's input for this frame and register it for the web form 694 // handling 695 fn buildAssigneeRow(row: *wgt.Box(Widget), session: *ui.Session, root_focus: *Focus) !void { 696 const text_input = &row.children.values()[0].widget.text_input; 697 const local = session.data.host_kind == .local; 698 const failure = if (session.formFeedback(.assignment)) |saved| saved.failure else null; 699 text_input.options.top_label.text = if (failure) |value| switch (value) { 700 .not_found => " assign by name or email (not found) ", 701 .not_an_email => if (local) " assign by email (not an email) " else " assign by name or email (not an email) ", 702 .already_assigned => if (local) " assign by email (already assigned) " else " assign by name or email (already assigned) ", 703 } else if (local) " assign by email " else " assign by name or email "; 704 text_input.options.bottom_label.text = if (root_focus.grandchild_id == text_input.getFocus().id) " press enter " else ""; 705 try session.text_inputs.put(session.arena.allocator(), text_input.getFocus().id, text_input); 706 } 707 708 pub fn Detail(comptime kind: evt.EventKind, comptime Data: type) type { 709 const Entry = Data.Entry; 710 const Event = Data.Event; 711 const Status = Data.Status; 712 const has_status = @hasField(Event, "status"); 713 const supports_conflicts = Event.merge_policy == .field_conflicts; 714 const supports_drafts = @hasField(Data, "drafts"); 715 const supports_forks = @hasField(Entry, "fork_exists"); 716 const supports_merge = @hasDecl(Data, "mergeRoute"); 717 return struct { 718 const This = @This(); 719 const thread_name = Data.thread_name; 720 721 pub const Exit = enum { none, list, header }; 722 pub const Options = struct { actions: bool = true }; 723 724 scroll: wgt.Scroll(Widget), 725 data: *const Data, 726 session: *ui.Session, 727 entry: ?Entry, 728 title_id: ?usize, 729 description_id: ?usize, 730 author_id: ?usize, 731 actions: bool, 732 exit: Exit, 733 734 const tool_row_index: usize = 0; 735 736 pub fn init(allocator: std.mem.Allocator, data: *const Data, session: *ui.Session, entry: ?Entry, options: Options) !This { 737 var content = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 738 errdefer content.deinit(allocator); 739 var detail_scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = content }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 740 errdefer detail_scroll.deinit(allocator); 741 742 var self: This = .{ 743 .scroll = detail_scroll, 744 .data = data, 745 .session = session, 746 .entry = null, 747 .title_id = null, 748 .description_id = null, 749 .author_id = null, 750 .actions = options.actions, 751 .exit = .none, 752 }; 753 if (entry) |value| try self.setEntry(allocator, value); 754 return self; 755 } 756 757 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 758 self.scroll.deinit(allocator); 759 } 760 761 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 762 self.clearGrid(); 763 if (kind == .issue) { 764 for (self.inner().children.values()) |*child| switch (child.widget) { 765 .box => |*row| if (isAssigneeRow(row)) try buildAssigneeRow(row, self.session, root_focus), 766 else => {}, 767 }; 768 } 769 try self.scroll.build(allocator, constraint, root_focus); 770 } 771 772 pub fn input(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 773 _ = try self.inputWithExit(allocator, key, root_focus); 774 } 775 776 pub fn clearGrid(self: *This) void { 777 self.scroll.clearGrid(); 778 } 779 780 pub fn getGrid(self: This) ?Grid { 781 return self.scroll.getGrid(); 782 } 783 784 pub fn getFocus(self: *This) *Focus { 785 return self.scroll.getFocus(); 786 } 787 788 fn inner(self: *This) *wgt.Box(Widget) { 789 return &self.scroll.child.box; 790 } 791 792 fn toolRow(self: *This) *wgt.Box(Widget) { 793 return &self.inner().children.values()[tool_row_index].widget.box; 794 } 795 796 fn entryConflicted(entry: Entry) bool { 797 return if (supports_conflicts) entry.conflicted else false; 798 } 799 800 fn entryStatus(entry: Entry) Status { 801 if (comptime has_status) return Data.statusKind(entry.record.event); 802 return @fromBackingInt(@intCast(0)); 803 } 804 805 fn entryDraft(entry: Entry) bool { 806 return if (supports_drafts) entry.draft else false; 807 } 808 809 const StatusChange = struct { 810 action: []const u8, 811 status: Status, 812 }; 813 814 fn statusChange(status: Status) ?StatusChange { 815 const name = @tagName(status); 816 if (std.mem.eql(u8, name, "open")) return .{ 817 .action = "close", 818 .status = std.meta.stringToEnum(Status, "closed") orelse return null, 819 }; 820 if (std.mem.eql(u8, name, "closed")) return .{ 821 .action = "open", 822 .status = std.meta.stringToEnum(Status, "open") orelse return null, 823 }; 824 return null; 825 } 826 827 fn listRoute(identity: []const u8, status: Status, label: []const u8, selected: []const u8) ?ui.RoutablePage { 828 return Data.listRoute(identity, status, label, selected); 829 } 830 831 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 832 return if (supports_drafts) Data.draftsRoute(identity) else null; 833 } 834 835 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 836 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 837 } 838 839 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 840 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 841 } 842 843 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 844 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 845 } 846 847 // a list link keeps the filters the page carries: the label, and the 848 // search query and url-encoded assignee when set 849 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, id: []const u8, search: []const u8, assignee: []const u8) ![]const u8 { 850 var route = listRoute(identity, status, label, id) orelse return error.RouteTooLong; 851 if (search.len != 0) route = route.withSearch(search) orelse return error.RouteTooLong; 852 if (assignee.len != 0) route = route.withEncodedAssignee(assignee) orelse return error.RouteTooLong; 853 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 854 } 855 856 fn labelLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, search: []const u8, assignee: []const u8) ![]const u8 { 857 const encoded = try ui.urlEncodeRef(page_arena.allocator(), label); 858 return listLink(page_arena, identity, status, encoded, "", search, assignee); 859 } 860 861 fn addToolButton(allocator: std.mem.Allocator, row: *wgt.Box(Widget), label: []const u8, bottom_label: []const u8, action: []const u8) !void { 862 var button = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = bottom_label } }); 863 errdefer button.deinit(allocator); 864 button.getFocus().mode = .all; 865 button.getFocus().kind = .{ .custom = action }; 866 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = @max(try xitui.width.displayWidth(label), try xitui.width.displayWidth(bottom_label)) + 2, .height = null } }); 867 } 868 869 pub fn setEntry(self: *This, allocator: std.mem.Allocator, entry: Entry) !void { 870 const inner_box = self.inner(); 871 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 872 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 873 874 for (inner_box.children.values()) |*child| child.widget.deinit(allocator); 875 inner_box.children.clearAndFree(allocator); 876 inner_box.getFocus().child_id = null; 877 self.entry = entry; 878 self.title_id = null; 879 self.author_id = null; 880 881 { 882 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 883 errdefer row.deinit(allocator); 884 try inner_box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 885 } 886 if (self.actions and !description_page and comment_page == null) { 887 const row = self.toolRow(); 888 const pa = self.session.page_arena.allocator(); 889 890 if (supports_forks and self.session.data.current_page.parent() == .fork) { 891 // the fork page's own tabs show the diff and commits 892 const route = ui.RoutablePage.repoPatchesRoute(self.data.identity, .open, "", entry.id) orelse return error.RouteTooLong; 893 try addToolButton(allocator, row, "view on target repo", "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 894 } else if (supports_forks) { 895 if (try Data.diffRoute(self.data.identity, entry)) |route| { 896 try addToolButton(allocator, row, "view diff", "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 897 } 898 if (try Data.commitsRoute(self.data.identity, entry)) |route| { 899 const label = if (entry.commit_count) |count| try pa.print("view commits ({d})", .{count}) else "view commits"; 900 try addToolButton(allocator, row, label, "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 901 } 902 } 903 904 { 905 var spacer = try Spacer.init(allocator); 906 errdefer spacer.deinit(allocator); 907 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 908 } 909 910 if (comptime supports_merge) { 911 if (Data.canMerge(entry, self.session)) { 912 const route = mergeRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 913 try addToolButton(allocator, row, "merge", mergeBottomLabel(entry.mergeability.status()), try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 914 } 915 } 916 917 if (supports_drafts and entryDraft(entry)) { 918 const route = publishRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 919 try addToolButton(allocator, row, "publish", "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 920 const edit_route = ui.RoutablePage.repoThreadEditRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 921 try addToolButton(allocator, row, "edit", "", try pa.print("a:{s}", .{try edit_route.toUrl(self.session.page_arena)})); 922 const remove_route = ui.RoutablePage.repoThreadRemoveRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong; 923 try addToolButton(allocator, row, "✕", "", try pa.print("a:{s}", .{try remove_route.toUrl(self.session.page_arena)})); 924 } else { 925 if (!self.session.is_terminal and !entryConflicted(entry)) { 926 const label = "add attachment"; 927 const action = if (self.data.identity.len == 0) 928 try pa.print("{s}/{s}:{s}/attach", .{ ui.file_input_prefix, @tagName(kind), entry.id }) 929 else 930 try pa.print("{s}/{s}/{s}:{s}/attach", .{ ui.file_input_prefix, self.data.identity, @tagName(kind), entry.id }); 931 try addToolButton(allocator, row, label, "", action); 932 } 933 934 if (supports_conflicts and entryConflicted(entry)) { 935 const route = resolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 936 try addToolButton(allocator, row, "resolve conflict", "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 937 } else { 938 if (has_status) { 939 if (statusChange(entryStatus(entry))) |change| { 940 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 941 row.getFocus().kind = .{ .custom = try pa.print("form:{s}/{s}", .{ try route.toUrl(self.session.page_arena), change.action }) }; 942 try addToolButton(allocator, row, change.action, "", "submit"); 943 } 944 } 945 946 const route = ui.RoutablePage.repoThreadEditRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 947 try addToolButton(allocator, row, "edit", "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 948 } 949 950 const route = ui.RoutablePage.repoThreadRemoveRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong; 951 try addToolButton(allocator, row, "✕", "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 952 } 953 } 954 955 if (supports_drafts and !description_page and comment_page == null) 956 try self.data.appendDetails(allocator, inner_box, self.session, entry); 957 958 if (comment_page) |comment_view| { 959 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 960 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 961 var back = try Comment.linkBox(allocator, self.session, back_label, ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 962 errdefer back.deinit(allocator); 963 try inner_box.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 964 965 try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, self.data.viewer != null, comment_view.selected); 966 try Comment.appendCount(allocator, inner_box, comment_view.replies.count, "reply", "replies"); 967 for (comment_view.replies.comments) |comment| try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, self.data.viewer != null, comment); 968 try Comment.appendWindowNav(allocator, inner_box, self.session, self.data.identity, kind, &comment_view.selected.comment.event.thread_id, &comment_view.selected.id, comment_view.replies); 969 970 var spacer = try Spacer.init(allocator); 971 errdefer spacer.deinit(allocator); 972 try inner_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 973 974 inner_box.getFocus().child_id = inner_box.children.keys()[tool_row_index + 1]; 975 self.resetScroll(); 976 return; 977 } 978 979 if (description_page) { 980 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 981 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 982 var text_box = try wgt.TextBox.init(allocator, back_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 983 errdefer text_box.deinit(allocator); 984 text_box.getFocus().mode = .all; 985 text_box.getFocus().kind = .{ .custom = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), "", entry.id, self.data.search orelse "", "") }; 986 try inner_box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 987 self.title_id = text_box.getFocus().id; 988 } else { 989 var title = try wgt.TextBox.init(allocator, entry.record.event.title, .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = " title " } }); 990 errdefer title.deinit(allocator); 991 title.getFocus().mode = .all; 992 try inner_box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_box = title }, .rect = null, .min_size = null }); 993 self.title_id = title.getFocus().id; 994 995 { 996 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 997 errdefer row.deinit(allocator); 998 var author = try ui.authorBox(allocator, self.session.page_arena, entry.author); 999 errdefer author.deinit(allocator); 1000 try row.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 1001 const timestamp = try ui.timestamp.format(self.session.page_arena.allocator(), std.math.cast(i64, entry.record.created_timestamp) orelse -1, try self.session.nowSeconds()); 1002 var time = try wgt.TextBox.init(allocator, timestamp.relative, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = timestamp.exact } }); 1003 errdefer time.deinit(allocator); 1004 time.getFocus().mode = .all; 1005 try row.children.put(allocator, time.getFocus().id, .{ .widget = .{ .text_box = time }, .rect = null, .min_size = null }); 1006 row.getFocus().child_id = author.getFocus().id; 1007 self.author_id = row.getFocus().id; 1008 try inner_box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1009 } 1010 1011 var items: std.ArrayList(WordFlow.Item) = .empty; 1012 defer items.deinit(allocator); 1013 var label_iter = Event.labelIterator(entry.record.event.labels); 1014 while (label_iter.next()) |label| { 1015 if (label.len == 0) continue; 1016 try items.append(allocator, .{ .text = label, .link = try labelLink(self.session.page_arena, self.data.identity, entryStatus(entry), label, self.data.search orelse "", assigneeFilter(self.data)) }); 1017 } 1018 if (items.items.len > 0) { 1019 var labels = try WordFlow.init(allocator); 1020 errdefer labels.deinit(allocator); 1021 try labels.setItems(allocator, items.items); 1022 try inner_box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .word_flow = labels }, .rect = null, .min_size = null }); 1023 } 1024 } 1025 1026 if (kind == .issue) { 1027 const pa = self.session.page_arena.allocator(); 1028 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1029 const thread_url = try parent_route.toUrl(self.session.page_arena); 1030 if (entry.can_modify) try appendAssigneeRow(allocator, inner_box, self.session, thread_url); 1031 // every viewer gets each assignee as a link to the issues assigned to it 1032 const links = try pa.alloc([]const u8, entry.assignees.len); 1033 for (links, entry.assignees) |*link, assignee| link.* = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), self.data.label, "", self.data.search orelse "", try ui.urlEncodeRef(pa, assignee.display())); 1034 try appendAssignedRows(allocator, inner_box, self.session, thread_url, entry.assignees, links, entry.can_modify); 1035 } 1036 1037 const whole = entry.record.event.description; 1038 const preview_end = if (description_page) null else ui.detailPreviewEnd(whole); 1039 const shown = if (preview_end) |end| 1040 std.mem.trimEnd(u8, whole[0..end], " \t\r\n") 1041 else if (whole.len == 0) 1042 "(no description)" 1043 else 1044 whole; 1045 var description = try Markdown.Frame.init(allocator, shown, " description ", if (preview_end != null) " click or press enter to see more " else "", self.session.page_arena); 1046 errdefer description.deinit(allocator); 1047 if (preview_end != null) { 1048 const route = ui.RoutablePage.repoThreadDescriptionRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 1049 description.body().kind = .{ .custom = try self.session.page_arena.allocator().print("a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1050 } 1051 try inner_box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .markdown_frame = description }, .rect = null, .min_size = null }); 1052 self.description_id = description.getFocus().id; 1053 1054 if (!supports_drafts or !entryDraft(entry)) { 1055 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1056 const parent_url = try parent_route.toUrl(self.session.page_arena); 1057 try Attachment.appendRows(allocator, inner_box, self.session, self.data.identity, parent_url, entry.attachments); 1058 1059 if (!description_page) { 1060 if (self.data.viewer != null) { 1061 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoThreadCommentNewRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1062 errdefer reply.deinit(allocator); 1063 try inner_box.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1064 } 1065 1066 try Comment.appendCount(allocator, inner_box, entry.comments.count, "comment", "comments"); 1067 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, self.data.viewer != null, comment); 1068 try Comment.appendWindowNav(allocator, inner_box, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1069 } 1070 } 1071 1072 if (!description_page) { 1073 var spacer = try Spacer.init(allocator); 1074 errdefer spacer.deinit(allocator); 1075 try inner_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1076 } 1077 1078 _ = self.focusFirst(null); 1079 self.resetScroll(); 1080 } 1081 1082 fn resetScroll(self: *This) void { 1083 const detail_scroll = &self.scroll; 1084 detail_scroll.x = 0; 1085 detail_scroll.y = 0; 1086 detail_scroll.getFocus().version +%= 1; 1087 } 1088 1089 pub fn inputWithExit(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !Exit { 1090 self.exit = .none; 1091 const key: Key = switch (raw_key) { 1092 .scroll_up => .arrow_up, 1093 .scroll_down => .arrow_down, 1094 else => raw_key, 1095 }; 1096 1097 // a focused assignee input keeps its editing keys 1098 if (key == .home or key == .end) if (self.focusedAssigneeInput(root_focus)) |text_input| { 1099 try text_input.input(allocator, key, root_focus); 1100 return self.exit; 1101 }; 1102 1103 switch (key) { 1104 .page_up => self.pageDetail(root_focus, -10), 1105 .page_down => self.pageDetail(root_focus, 10), 1106 .home => self.jump(root_focus, false), 1107 .end => self.jump(root_focus, true), 1108 // tab steps between the links of a focused description or 1109 // comment; the web leaves it to the browser 1110 .tab, .back_tab => if (self.session.is_terminal) if (self.focusedChild(root_focus)) |index| if (self.focusedFrame(index)) |frame| { 1111 try frame.frame.stepLink(allocator, root_focus, &self.scroll, frame.x, frame.y, key == .tab); 1112 }, 1113 else => { 1114 const entry = self.entry orelse return self.exit; 1115 if (self.data.comment_page != null and std.mem.eql(u8, entry.id, self.data.selected_id)) { 1116 self.commentInput(key, root_focus); 1117 } else if (self.toolRowFocused(root_focus)) { 1118 try self.toolRowInput(allocator, key, root_focus); 1119 } else { 1120 try self.childInput(allocator, key, root_focus); 1121 } 1122 }, 1123 } 1124 return self.exit; 1125 } 1126 1127 // the assignee row's input, when it holds focus 1128 fn focusedAssigneeInput(self: *This, root_focus: *Focus) ?*wgt.TextInput { 1129 const child_index = self.focusedChild(root_focus) orelse return null; 1130 const child = &self.inner().children.values()[child_index].widget; 1131 if (child.* != .box or !isAssigneeRow(&child.box)) return null; 1132 const text_input = &child.box.children.values()[0].widget.text_input; 1133 return if (root_focus.grandchild_id == text_input.getFocus().id) text_input else null; 1134 } 1135 1136 fn childInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1137 const child_index = self.focusedChild(root_focus) orelse return; 1138 const inner_box = self.inner(); 1139 const child_id = inner_box.children.keys()[child_index]; 1140 const child = &inner_box.children.values()[child_index].widget; 1141 1142 if (child_id == self.title_id) { 1143 switch (key) { 1144 .arrow_left => self.exit = .list, 1145 .arrow_up => if (!self.moveVertical(root_focus, false)) { 1146 self.exit = .header; 1147 }, 1148 .arrow_down => _ = self.moveVertical(root_focus, true), 1149 else => {}, 1150 } 1151 return; 1152 } 1153 if (child_id == self.author_id) { 1154 switch (key) { 1155 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 1156 self.exit = .list; 1157 }, 1158 .arrow_right => _ = self.moveHorizontal(root_focus, true), 1159 .arrow_up => _ = self.moveVertical(root_focus, false), 1160 .arrow_down => _ = self.moveVertical(root_focus, true), 1161 else => {}, 1162 } 1163 return; 1164 } 1165 if (child_id == self.description_id) { 1166 self.descriptionInput(key, root_focus); 1167 return; 1168 } 1169 1170 switch (child.*) { 1171 .word_flow => |*labels| self.labelsInput(child_index, labels, key, root_focus), 1172 .copyable_text => |*copyable| switch (key) { 1173 .arrow_up => if (!self.moveVertical(root_focus, false)) { 1174 self.exit = .header; 1175 }, 1176 .arrow_down => _ = self.moveVertical(root_focus, true), 1177 .arrow_left => if (copyable.selected == 0) { 1178 self.exit = .list; 1179 } else try copyable.input(allocator, key, root_focus), 1180 else => try copyable.input(allocator, key, root_focus), 1181 }, 1182 .box => |*row| if (isAssigneeRow(row)) { 1183 try self.assigneeInput(allocator, row, key, root_focus); 1184 } else if (Attachment.removeId(row)) |attachment_id| { 1185 if (self.removableRowInput(row, key, root_focus)) try self.removeEvent(allocator, .attach, attachment_id); 1186 } else if (unassignEmail(row)) |encoded| { 1187 if (self.removableRowInput(row, key, root_focus)) try self.unassign(allocator, std.Uri.percentDecodeInPlace(try self.session.page_arena.allocator().dupe(u8, encoded))); 1188 } else self.commentInput(key, root_focus), 1189 else => self.commentInput(key, root_focus), 1190 } 1191 } 1192 1193 fn toolRowInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1194 const row = self.toolRow(); 1195 const child_id = row.getFocus().child_id orelse return; 1196 const cur = row.children.getIndex(child_id) orelse return; 1197 const on_primary = switch (row.children.values()[cur].widget.getFocus().kind) { 1198 .custom => |action| std.mem.eql(u8, action, "submit"), 1199 else => false, 1200 }; 1201 switch (key) { 1202 .arrow_left => { 1203 var previous = cur; 1204 while (previous > 0) { 1205 previous -= 1; 1206 if (row.children.values()[previous].widget.getFocus().mode != .all) continue; 1207 root_focus.setFocus(row.children.keys()[previous]); 1208 return; 1209 } 1210 self.exit = .list; 1211 }, 1212 .arrow_right => { 1213 var next = cur + 1; 1214 while (next < row.children.count()) : (next += 1) { 1215 if (row.children.values()[next].widget.getFocus().mode != .all) continue; 1216 root_focus.setFocus(row.children.keys()[next]); 1217 return; 1218 } 1219 }, 1220 .arrow_up => self.exit = .header, 1221 .arrow_down => _ = self.moveVertical(root_focus, true), 1222 .enter => if (on_primary) try self.primaryAction(allocator), 1223 .mouse => |mouse| if (on_primary and inp.leftClickOn(root_focus, child_id, mouse)) try self.primaryAction(allocator), 1224 else => {}, 1225 } 1226 } 1227 1228 fn primaryAction(self: *This, allocator: std.mem.Allocator) !void { 1229 try self.toggleStatus(allocator); 1230 } 1231 1232 fn descriptionInput(self: *This, key: Key, root_focus: *Focus) void { 1233 switch (key) { 1234 .arrow_left => self.exit = .list, 1235 .arrow_up => _ = self.moveVertical(root_focus, false), 1236 .arrow_down => _ = self.moveVertical(root_focus, true), 1237 else => {}, 1238 } 1239 } 1240 1241 fn commentInput(self: *This, key: Key, root_focus: *Focus) void { 1242 switch (key) { 1243 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 1244 self.exit = .list; 1245 }, 1246 .arrow_right => _ = self.moveHorizontal(root_focus, true), 1247 .arrow_up => _ = self.moveVertical(root_focus, false), 1248 .arrow_down => _ = self.moveVertical(root_focus, true), 1249 else => {}, 1250 } 1251 } 1252 1253 fn pageDetail(self: *This, root_focus: *Focus, delta: isize) void { 1254 if (!self.session.is_terminal) return; 1255 const detail_scroll = &self.scroll; 1256 detail_scroll.y += delta; 1257 detail_scroll.clampToContent(); 1258 self.focusVisible(root_focus, delta > 0); 1259 } 1260 1261 fn jump(self: *This, root_focus: *Focus, to_end: bool) void { 1262 if (self.session.is_terminal) { 1263 const detail_scroll = &self.scroll; 1264 detail_scroll.y = if (to_end) std.math.maxInt(isize) else 0; 1265 detail_scroll.clampToContent(); 1266 self.focusVisible(root_focus, to_end); 1267 } else { 1268 const focus = self.inner().getFocus(); 1269 var chosen: ?usize = null; 1270 for (focus.children.keys(), focus.children.values()) |id, child| { 1271 if (child.focus.mode != .all) continue; 1272 chosen = id; 1273 if (!to_end) break; 1274 } 1275 if (chosen) |id| root_focus.setFocus(id); 1276 } 1277 } 1278 1279 fn rectVisible(self: *This, rect: layout.IRect) bool { 1280 const detail_scroll = &self.scroll; 1281 const viewport = detail_scroll.grid orelse return false; 1282 const top = detail_scroll.y; 1283 const bottom = top + @as(isize, @intCast(viewport.size.height - detail_scroll.bar_h)); 1284 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1285 } 1286 1287 fn focusVisible(self: *This, root_focus: *Focus, last: bool) void { 1288 const inner_box = self.inner(); 1289 var offset: usize = 0; 1290 while (offset < inner_box.children.count()) : (offset += 1) { 1291 const child_index = if (last) inner_box.children.count() - 1 - offset else offset; 1292 if (self.focusVisibleChild(child_index, root_focus, last)) return; 1293 } 1294 } 1295 1296 fn focusVisibleChild(self: *This, child_index: usize, root_focus: *Focus, last: bool) bool { 1297 const inner_box = self.inner(); 1298 const child = &inner_box.children.values()[child_index]; 1299 const child_rect = child.rect orelse return false; 1300 var chosen: ?usize = null; 1301 if (child.widget.getFocus().mode == .all and self.rectVisible(child_rect)) { 1302 const id = inner_box.children.keys()[child_index]; 1303 chosen = id; 1304 if (!last) { 1305 root_focus.setFocus(id); 1306 return true; 1307 } 1308 } 1309 1310 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1311 if (focus_child.focus.mode != .all) continue; 1312 const rect: layout.IRect = .{ 1313 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1314 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1315 .size = focus_child.rect.size, 1316 }; 1317 if (!self.rectVisible(rect)) continue; 1318 chosen = id; 1319 if (!last) break; 1320 } 1321 if (chosen) |id| { 1322 root_focus.setFocus(id); 1323 return true; 1324 } 1325 return false; 1326 } 1327 1328 // the markdown frame focused in child `child_index`, placed in the scroll's 1329 // content 1330 fn focusedFrame(self: *This, child_index: usize) ?struct { frame: *Markdown.Frame, x: isize, y: isize } { 1331 const child = &self.inner().children.values()[child_index]; 1332 const rect = child.rect orelse return null; 1333 switch (child.widget) { 1334 .markdown_frame => |*frame| return .{ .frame = frame, .x = rect.x, .y = rect.y }, 1335 .repo_comment => |*comment| { 1336 if (!comment.bodyFocused()) return null; 1337 const row = comment.rowRect(true) orelse return null; 1338 return .{ .frame = comment.body(), .x = rect.x + row.x, .y = rect.y + row.y }; 1339 }, 1340 else => return null, 1341 } 1342 } 1343 1344 fn focusedChild(self: *This, root_focus: *Focus) ?usize { 1345 const inner_box = self.inner(); 1346 var id = root_focus.grandchild_id orelse return null; 1347 while (inner_box.getFocus().children.get(id)) |child| { 1348 if (child.parent_id == inner_box.getFocus().id) return inner_box.children.getIndex(id); 1349 id = child.parent_id; 1350 } 1351 return null; 1352 } 1353 1354 fn childFocusable(self: *This, child_index: usize) bool { 1355 const focus = self.inner().children.values()[child_index].widget.getFocus(); 1356 if (focus.mode == .all) return true; 1357 for (focus.children.values()) |item| { 1358 if (item.focus.mode == .all) return true; 1359 } 1360 return false; 1361 } 1362 1363 // focus child `child_index`, its last part when `last`, entered from above 1364 // when `down` 1365 fn focusChild(self: *This, child_index: usize, last: bool, down: bool, root_focus: *Focus) void { 1366 const inner_box = self.inner(); 1367 const child = &inner_box.children.values()[child_index]; 1368 var target = inner_box.children.keys()[child_index]; 1369 var rect = child.rect orelse return; 1370 switch (child.widget) { 1371 .repo_comment => |*comment| { 1372 if (comment.rowRect(last)) |row_rect| { 1373 rect.x += row_rect.x; 1374 rect.y += row_rect.y; 1375 rect.size = row_rect.size; 1376 } 1377 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1378 }, 1379 // a row enters on its first cell from either direction; the tool 1380 // row keeps the cell it was on 1381 .box => |*box| if (box.children.count() > 0) { 1382 target = if (child_index == tool_row_index) 1383 box.getFocus().child_id orelse return 1384 else 1385 box.children.keys()[0]; 1386 root_focus.setFocus(target); 1387 }, 1388 else => root_focus.setFocus(target), 1389 } 1390 widget.scrollToEnteredRow(&self.scroll, rect, down); 1391 } 1392 1393 fn moveVertical(self: *This, root_focus: *Focus, down: bool) bool { 1394 const inner_box = self.inner(); 1395 const current = self.focusedChild(root_focus) orelse return false; 1396 // a tall description or comment body is scrolled through before focus 1397 // leaves it 1398 if (self.focusedRect(current)) |rect| if (widget.scrollThroughRow(&self.scroll, rect, if (down) 1 else -1)) return true; 1399 if (inner_box.children.values()[current].widget == .repo_comment) { 1400 const comment = &inner_box.children.values()[current].widget.repo_comment; 1401 if (down and !comment.bodyFocused()) { 1402 self.focusChild(current, true, true, root_focus); 1403 return true; 1404 } 1405 if (!down and comment.bodyFocused()) { 1406 self.focusChild(current, false, false, root_focus); 1407 return true; 1408 } 1409 } 1410 1411 var next = current; 1412 while (true) { 1413 if (down) { 1414 next += 1; 1415 if (next >= inner_box.children.count()) return false; 1416 } else { 1417 if (next == 0) return false; 1418 next -= 1; 1419 } 1420 if (!self.childFocusable(next)) continue; 1421 self.focusChild(next, !down, down, root_focus); 1422 return true; 1423 } 1424 } 1425 1426 // the focused part of child `child_index`, in the scroll's content: a 1427 // comment's metadata or body row, else the whole child 1428 fn focusedRect(self: *This, child_index: usize) ?layout.IRect { 1429 const child = &self.inner().children.values()[child_index]; 1430 var rect = child.rect orelse return null; 1431 if (child.widget == .repo_comment) { 1432 const comment = &child.widget.repo_comment; 1433 const row = comment.rowRect(comment.bodyFocused()) orelse return null; 1434 rect.x += row.x; 1435 rect.y += row.y; 1436 rect.size = row.size; 1437 } 1438 return rect; 1439 } 1440 1441 fn moveHorizontal(self: *This, root_focus: *Focus, right: bool) bool { 1442 const inner_box = self.inner(); 1443 const current = self.focusedChild(root_focus) orelse return false; 1444 const child = &inner_box.children.values()[current]; 1445 if (child.widget == .repo_comment) { 1446 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1447 if (moved) if (child.rect) |rect| self.scroll.scrollToRect(rect); 1448 return moved; 1449 } 1450 const row = switch (child.widget) { 1451 .box => |*box| box, 1452 else => return false, 1453 }; 1454 if (!widget.moveInRow(row, root_focus, right)) return false; 1455 if (child.rect) |rect| self.scroll.scrollToRect(rect); 1456 return true; 1457 } 1458 1459 // the input takes typing and assigns on enter 1460 fn assigneeInput(self: *This, allocator: std.mem.Allocator, row: *wgt.Box(Widget), key: Key, root_focus: *Focus) !void { 1461 const text_input = &row.children.values()[0].widget.text_input; 1462 switch (key) { 1463 .arrow_up => _ = self.moveVertical(root_focus, false), 1464 .arrow_down => _ = self.moveVertical(root_focus, true), 1465 .arrow_left => if (text_input.cursor == 0) { 1466 self.exit = .list; 1467 } else try text_input.input(allocator, key, root_focus), 1468 .enter => { 1469 const text = try text_input.text(allocator); 1470 defer allocator.free(text); 1471 try self.assign(allocator, text); 1472 }, 1473 else => try text_input.input(allocator, key, root_focus), 1474 } 1475 } 1476 1477 // arrows move along a row that ends in a ✕. returns whether enter or a 1478 // click hit the ✕. 1479 fn removableRowInput(self: *This, row: *wgt.Box(Widget), key: Key, root_focus: *Focus) bool { 1480 const remove_id = row.children.keys()[row.children.count() - 1]; 1481 switch (key) { 1482 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 1483 self.exit = .list; 1484 }, 1485 .arrow_right => _ = self.moveHorizontal(root_focus, true), 1486 .arrow_up => _ = self.moveVertical(root_focus, false), 1487 .arrow_down => _ = self.moveVertical(root_focus, true), 1488 .enter => return row.getFocus().child_id == remove_id, 1489 .mouse => |mouse| return inp.leftClickOn(root_focus, remove_id, mouse), 1490 else => {}, 1491 } 1492 return false; 1493 } 1494 1495 fn labelsInput(self: *This, child_index: usize, labels: *WordFlow, key: Key, root_focus: *Focus) void { 1496 const child_id = labels.focus.child_id orelse return; 1497 const cur = labels.indexOfFocusId(child_id) orelse return; 1498 const count = labels.text_boxes.items.len; 1499 switch (key) { 1500 .arrow_left => if (cur > 0) self.focusLabel(child_index, labels, root_focus, cur - 1) else { 1501 self.exit = .list; 1502 }, 1503 .arrow_right => if (cur + 1 < count) self.focusLabel(child_index, labels, root_focus, cur + 1), 1504 .arrow_up => if (labels.rowStep(cur, false)) |index| { 1505 self.focusLabel(child_index, labels, root_focus, index); 1506 } else { 1507 _ = self.moveVertical(root_focus, false); 1508 }, 1509 .arrow_down => if (labels.rowStep(cur, true)) |index| { 1510 self.focusLabel(child_index, labels, root_focus, index); 1511 } else { 1512 _ = self.moveVertical(root_focus, true); 1513 }, 1514 .home => self.focusLabel(child_index, labels, root_focus, 0), 1515 .end => self.focusLabel(child_index, labels, root_focus, count - 1), 1516 else => {}, 1517 } 1518 } 1519 1520 fn focusLabel(self: *This, child_index: usize, labels: *WordFlow, root_focus: *Focus, item: usize) void { 1521 root_focus.setFocus(labels.text_boxes.items[item].getFocus().id); 1522 if (self.session.is_terminal and item < labels.rects.items.len) { 1523 if (self.inner().children.values()[child_index].rect) |flow_rect| { 1524 var rect = labels.rects.items[item]; 1525 rect.x += flow_rect.x; 1526 rect.y += flow_rect.y; 1527 self.scroll.scrollToRect(rect); 1528 } 1529 } 1530 } 1531 1532 fn toolRowFocused(self: *This, root_focus: *Focus) bool { 1533 return self.focusedChild(root_focus) == tool_row_index; 1534 } 1535 1536 pub fn focusFirst(self: *This, root_focus: ?*Focus) bool { 1537 const inner_box = self.inner(); 1538 for (inner_box.children.values(), 0..) |*child, child_index| { 1539 switch (child.widget) { 1540 .box => |*box| { 1541 for (box.children.keys(), box.children.values()) |id, *item| { 1542 if (item.widget.getFocus().mode != .all) continue; 1543 box.getFocus().child_id = id; 1544 break; 1545 } else continue; 1546 }, 1547 else => if (!self.childFocusable(child_index)) continue, 1548 } 1549 if (root_focus) |focus| 1550 self.focusChild(child_index, false, true, focus) 1551 else 1552 inner_box.getFocus().child_id = inner_box.children.keys()[child_index]; 1553 return true; 1554 } 1555 return false; 1556 } 1557 1558 pub fn atTop(self: *This, root_focus: *Focus) bool { 1559 const focused = self.focusedChild(root_focus) orelse return false; 1560 for (0..self.inner().children.count()) |child_index| { 1561 if (!self.childFocusable(child_index)) continue; 1562 return focused == child_index; 1563 } 1564 return false; 1565 } 1566 1567 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 1568 if (comptime wasm) return; 1569 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1570 const author = actor.author; 1571 1572 if (comptime supports_drafts) { 1573 const entry = self.entry orelse return; 1574 if (event_kind == kind and entryDraft(entry)) { 1575 try Data.removeDraft(self.session, allocator, author, &id); 1576 const route = draftsRoute(self.data.identity) orelse return; 1577 try self.session.navigate(route); 1578 return; 1579 } 1580 } 1581 1582 const io = self.session.io orelse return; 1583 const source = self.data.repo_source orelse return; 1584 if (!try source.canModify(io, allocator, actor, event_kind, &id)) return; 1585 const thread_id = evt.parseEventId((self.entry orelse return).id) catch return; 1586 1587 switch (source.repo_kind) { 1588 inline else => |repo_kind| { 1589 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1590 defer any_repo.deinit(io, allocator); 1591 switch (any_repo) { 1592 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 1593 } 1594 }, 1595 } 1596 1597 const id_hex = std.fmt.bytesToHex(id, .lower); 1598 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 1599 try self.session.navigate(route); 1600 } 1601 1602 // assign the typed email, or the email of the user the text names 1603 fn assign(self: *This, allocator: std.mem.Allocator, text: []const u8) !void { 1604 if (comptime wasm) return; 1605 const entry = self.entry orelse return; 1606 const io = self.session.io orelse return; 1607 const source = self.data.repo_source orelse return; 1608 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1609 const thread_id = try evt.parseEventId(entry.id); 1610 if (!try source.canModify(io, allocator, actor, kind, &thread_id)) return; 1611 1612 var arena = std.heap.ArenaAllocator.init(allocator); 1613 defer arena.deinit(); 1614 const failure: ?ui.Session.FormFeedback.AssignmentFailure = blk: { 1615 const moment = if (self.session.data.host_kind == .local) null else self.session.haxy_moment; 1616 const email = switch (try ui.assigneeEmail(moment, &arena, text)) { 1617 .email => |email| email, 1618 .failure => |value| break :blk value, 1619 }; 1620 const thread_id_hex = std.fmt.bytesToHex(thread_id, .lower); 1621 switch (source.repo_kind) { 1622 inline else => |repo_kind| { 1623 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1624 defer any_repo.deinit(io, allocator); 1625 switch (any_repo) { 1626 inline else => |*repo| _ = evt.Assignment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id_hex, email, actor.author) catch |err| switch (err) { 1627 error.AlreadyAssigned => break :blk .already_assigned, 1628 error.InvalidFields => break :blk .not_an_email, 1629 else => |e| return e, 1630 }, 1631 } 1632 }, 1633 } 1634 break :blk null; 1635 }; 1636 if (failure) |value| { 1637 self.session.data.form_feedback = .{ .assignment = .{ .failure = value, .text = try self.session.arena.allocator().dupe(u8, text) } }; 1638 return; 1639 } 1640 try self.session.navigate(ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1641 } 1642 1643 fn unassign(self: *This, allocator: std.mem.Allocator, email: []const u8) !void { 1644 if (comptime wasm) return; 1645 const entry = self.entry orelse return; 1646 const io = self.session.io orelse return; 1647 const source = self.data.repo_source orelse return; 1648 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1649 const thread_id = try evt.parseEventId(entry.id); 1650 if (!try source.canModify(io, allocator, actor, kind, &thread_id)) return; 1651 1652 switch (source.repo_kind) { 1653 inline else => |repo_kind| { 1654 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1655 defer any_repo.deinit(io, allocator); 1656 switch (any_repo) { 1657 inline else => |*repo| evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, .assign, &evt.Assignment.idOf(&thread_id, email), actor.author) catch |err| switch (err) { 1658 // a stale button; the refresh below shows the row as it is 1659 error.EventNotFound => {}, 1660 else => |e| return e, 1661 }, 1662 } 1663 }, 1664 } 1665 try self.session.navigate(ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1666 } 1667 1668 fn toggleStatus(self: *This, allocator: std.mem.Allocator) !void { 1669 if (comptime (!has_status or wasm)) return; 1670 const entry = self.entry orelse return; 1671 const io = self.session.io orelse return; 1672 const source = self.data.repo_source orelse return; 1673 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1674 const author = actor.author; 1675 const status = (statusChange(entryStatus(entry)) orelse return).status; 1676 const id = try evt.parseEventId(entry.id); 1677 if (!try source.canModify(io, allocator, actor, kind, &id)) return; 1678 1679 switch (source.repo_kind) { 1680 inline else => |repo_kind| { 1681 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1682 defer any_repo.deinit(io, allocator); 1683 switch (any_repo) { 1684 inline else => |*repo| try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id, .{ .status = status }, author), 1685 } 1686 }, 1687 } 1688 1689 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 1690 try self.session.navigate(route); 1691 } 1692 }; 1693 } 1694 1695 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 1696 const Self = Data; 1697 const HeaderType = Data.Header; 1698 const Window = Data.Window; 1699 const Entry = Data.Entry; 1700 const Event = Data.Event; 1701 const feedback_tag: std.meta.Tag(ui.Session.FormFeedback) = switch (kind) { 1702 .issue => .issue, 1703 .patch => .patch, 1704 .discuss => .discussion, 1705 else => unreachable, 1706 }; 1707 const FeedbackFailure = if (kind == .patch) ui.Session.FormFeedback.PatchFailure else ui.Session.FormFeedback.ThreadFailure; 1708 const Status = Data.Status; 1709 const has_status = @hasField(Event, "status"); 1710 const supports_conflicts = Event.merge_policy == .field_conflicts; 1711 const supports_drafts = @hasField(Self, "drafts"); 1712 const supports_merge = @hasDecl(Data, "mergeRoute"); 1713 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 1714 const ViewKind = Data.ViewKind; 1715 const thread_name = Data.thread_name; 1716 const DetailType = Data.Detail; 1717 1718 return struct { 1719 const This = @This(); 1720 // a vertical box: the header tabs on top, then a stack holding a 1721 // master-detail split (thread list + description pane) per status list, 1722 // plus the labels view. 1723 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 1724 data: *const Self, 1725 session: *ui.Session, 1726 1727 const header_index: usize = 0; 1728 const stack_index: usize = 1; 1729 // indices within the stack, 1:1 with the header tabs. 1730 const status_count = @typeInfo(Status).@"enum".field_names.len; 1731 const labels_view_index: usize = status_count; 1732 // the new-thread form, or the edit, comment, or resolve form when the page was 1733 // loaded at one of their urls. 1734 const form_view_index: usize = labels_view_index + 1; 1735 // the conflicts split; the tab and stack child exist only when the repo 1736 // has conflicted threads. 1737 const drafts_view_index: usize = form_view_index + 1; 1738 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 1739 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 1740 // indices within a split (the horizontal box inside the stack). 1741 const list_index: usize = 0; 1742 const detail_index: usize = 1; 1743 const list_max_width: usize = 35; 1744 const detail_min_width: usize = 40; 1745 1746 pub fn viewIndex(view: ViewKind) usize { 1747 const name = @tagName(view); 1748 if (std.mem.eql(u8, name, "labels")) return labels_view_index; 1749 if (std.mem.eql(u8, name, "new") or std.mem.eql(u8, name, "edit") or std.mem.eql(u8, name, "publish") or std.mem.eql(u8, name, "merge") or std.mem.eql(u8, name, "new_comment") or std.mem.eql(u8, name, "edit_comment") or std.mem.eql(u8, name, "remove") or std.mem.eql(u8, name, "resolve")) return form_view_index; 1750 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 1751 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 1752 if (std.mem.eql(u8, name, "description")) unreachable; 1753 inline for (@typeInfo(Status).@"enum".field_names, 0..) |status_name, index| { 1754 if (std.mem.eql(u8, name, status_name)) return index; 1755 } 1756 unreachable; 1757 } 1758 1759 const Confirmation = enum { remove, publish, merge }; 1760 1761 fn confirmation(view: ViewKind) ?Confirmation { 1762 if (view == .remove) return .remove; 1763 if (supports_drafts and std.mem.eql(u8, @tagName(view), "publish")) return .publish; 1764 if (supports_merge and std.mem.eql(u8, @tagName(view), "merge")) return .merge; 1765 return null; 1766 } 1767 1768 // the status a status split lists; the conflicts split has none (its 1769 // threads carry their own). 1770 fn splitStatus(index: usize) Status { 1771 return @fromBackingInt(@intCast(index)); 1772 } 1773 1774 fn entryConflicted(entry: Entry) bool { 1775 return if (supports_conflicts) entry.conflicted else false; 1776 } 1777 1778 fn entryStatus(entry: Entry) Status { 1779 if (comptime has_status) return Data.statusKind(entry.record.event); 1780 return @fromBackingInt(@intCast(0)); 1781 } 1782 1783 fn entryDraft(entry: Entry) bool { 1784 return if (supports_drafts) entry.draft else false; 1785 } 1786 1787 fn listRoute(identity: []const u8, status: Status, label: []const u8, selected: []const u8) ?ui.RoutablePage { 1788 return Data.listRoute(identity, status, label, selected); 1789 } 1790 1791 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 1792 return if (supports_drafts) Data.draftsRoute(identity) else null; 1793 } 1794 1795 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1796 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 1797 } 1798 1799 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1800 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 1801 } 1802 1803 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1804 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 1805 } 1806 1807 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 1808 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 1809 } 1810 1811 // a list link keeps the filters the page carries: the label, and the 1812 // search query and url-encoded assignee when set 1813 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, id: []const u8, search: []const u8, assignee: []const u8) ![]const u8 { 1814 var route = listRoute(identity, status, label, id) orelse return error.RouteTooLong; 1815 if (search.len != 0) route = route.withSearch(search) orelse return error.RouteTooLong; 1816 if (assignee.len != 0) route = route.withEncodedAssignee(assignee) orelse return error.RouteTooLong; 1817 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1818 } 1819 1820 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 1821 const selected = std.mem.eql(u8, id, data.selected_id); 1822 const route = (if (selected and data.description_page) 1823 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 1824 else if (selected) 1825 if (data.comment_page) |page| 1826 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 1827 else 1828 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 1829 else 1830 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 1831 return page_arena.allocator().print("ai:{s}", .{try route.toUrl(page_arena)}); 1832 } 1833 1834 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 1835 if (supports_conflicts and status_maybe == null and !drafts) { 1836 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 1837 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1838 } 1839 if (drafts and id.len == 0) { 1840 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 1841 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1842 } 1843 return listLink(page_arena, data.identity, status_maybe orelse @fromBackingInt(@intCast(0)), data.label, id, data.search orelse "", assigneeFilter(data)); 1844 } 1845 1846 fn labelLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, search: []const u8, assignee: []const u8) ![]const u8 { 1847 const encoded = try ui.urlEncodeRef(page_arena.allocator(), label); 1848 return listLink(page_arena, identity, status, encoded, "", search, assignee); 1849 } 1850 1851 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 1852 var outer = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 1853 errdefer outer.deinit(allocator); 1854 1855 // the tabs at the top. 1856 { 1857 var hdr = try Data.initHeader(allocator, session, data); 1858 errdefer hdr.deinit(allocator); 1859 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 1860 } 1861 1862 // the stack enters `outer` before its children enter it, so an error 1863 // frees each child exactly once. 1864 { 1865 var stack = try wgt.Stack(Widget).init(allocator); 1866 errdefer stack.deinit(allocator); 1867 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1868 } 1869 const stack = &outer.children.values()[stack_index].widget.stack; 1870 1871 inline for (@typeInfo(Status).@"enum".field_values) |value| { 1872 var split = try initSplit(allocator, session, data, @fromBackingInt(@intCast(value)), false); 1873 errdefer split.deinit(allocator); 1874 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1875 } 1876 1877 // the labels view 1878 { 1879 var tf = try WordFlow.init(allocator); 1880 errdefer tf.deinit(allocator); 1881 var items: std.ArrayList(WordFlow.Item) = .empty; 1882 defer items.deinit(allocator); 1883 // when filtered, the first item clears the filter 1884 if (data.label.len != 0) 1885 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @fromBackingInt(@intCast(0)), "", "", data.search orelse "", assigneeFilter(data)) }); 1886 for (data.labels) |label| 1887 try items.append(allocator, .{ .text = label, .link = try labelLink(session.page_arena, data.identity, @fromBackingInt(@intCast(0)), label, data.search orelse "", assigneeFilter(data)) }); 1888 try tf.setItems(allocator, items.items); 1889 try stack.children.put(allocator, tf.getFocus().id, .{ .word_flow = tf }); 1890 } 1891 1892 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1893 // its place, prefilled with the selected thread's content. a viewer 1894 // who can't write in this tab gets the unauthorized view instead, 1895 // while local mode always writes. 1896 if (session.data.host_kind == .local or data.viewer != null) { 1897 if (confirmation(data.view)) |confirmation_kind| { 1898 const aa = session.page_arena.allocator(); 1899 const route = switch (confirmation_kind) { 1900 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1901 .publish => publishRoute(data.identity, data.selected_id), 1902 .merge => mergeRoute(data.identity, data.selected_id), 1903 } orelse return error.RouteTooLong; 1904 const page_url = try route.toUrl(session.page_arena); 1905 const action = try aa.print("form:{s}", .{page_url}); 1906 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1907 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1908 var buttons: [2]ConfirmationButton = undefined; 1909 buttons[0] = .{ .label = switch (confirmation_kind) { 1910 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1911 .publish => "publish patch", 1912 .merge => "merge patch", 1913 } }; 1914 var button_count: usize = 1; 1915 if (confirmation_kind == .merge) { 1916 if (comptime supports_merge) { 1917 const availability = if (data.selectedThread()) |entry| 1918 if (Data.canMerge(entry.*, session)) entry.mergeability else pch.Mergeability{} 1919 else 1920 pch.Mergeability{}; 1921 buttons[0].bottom_label = mergeBottomLabel(availability.source); 1922 if (availability.source != .clean) buttons[0].action = ""; 1923 buttons[1] = .{ 1924 .label = "squash and merge patch", 1925 .bottom_label = mergeBottomLabel(availability.squash), 1926 .action = if (availability.squash == .clean) try aa.print("{s}{s}/squash", .{ ui.submit_action_prefix, page_url }) else "", 1927 }; 1928 button_count += 1; 1929 } else unreachable; 1930 } 1931 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1932 errdefer center.deinit(allocator); 1933 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1934 } else if (data.view == .new_comment or data.view == .edit_comment) { 1935 const editing = data.view == .edit_comment; 1936 const route = if (editing) 1937 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1938 else 1939 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1940 const page_url = try route.toUrl(session.page_arena); 1941 const action = try session.page_arena.allocator().print("form:{s}", .{page_url}); 1942 const page = data.comment_page; 1943 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1944 const parent_route = if (editing) 1945 if (top_level) 1946 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1947 else if (page) |p| 1948 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1949 else 1950 null 1951 else if (page) |p| 1952 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1953 else 1954 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 1955 const parent_author: ui.Author = if (editing) 1956 if (top_level) 1957 if (data.selectedThread()) |entry| entry.author else .unknown 1958 else if (page) |p| 1959 p.selected.parent_author orelse .unknown 1960 else 1961 .unknown 1962 else if (page) |p| 1963 p.selected.author 1964 else if (data.selectedThread()) |entry| 1965 entry.author 1966 else 1967 .unknown; 1968 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 1969 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 1970 errdefer form.deinit(allocator); 1971 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1972 } else if (supports_conflicts and data.view == .resolve) { 1973 // on the web the page grows to the form's height and the 1974 // browser scrolls it; the terminal scrolls the form itself 1975 var form_widget: Widget = blk: { 1976 var form = try initResolveForm(allocator, session, data); 1977 errdefer form.deinit(allocator); 1978 break :blk if (session.is_terminal) 1979 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 1980 else 1981 .{ .box = form }; 1982 }; 1983 errdefer form_widget.deinit(allocator); 1984 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 1985 } else { 1986 const aa = session.page_arena.allocator(); 1987 const route = if (data.view == .edit) 1988 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 1989 else 1990 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 1991 const action = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}); 1992 const record = if (data.view == .edit) 1993 (if (data.selectedThread()) |entry| &entry.record else null) 1994 else 1995 null; 1996 var form = try initThreadForm(allocator, session, data, action, record); 1997 errdefer form.deinit(allocator); 1998 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1999 } 2000 } else { 2001 var message = try ui.Unauthorized.View.init(allocator); 2002 errdefer message.deinit(allocator); 2003 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 2004 } 2005 2006 if (supports_drafts) { 2007 var split = try initSplit(allocator, session, data, null, true); 2008 errdefer split.deinit(allocator); 2009 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 2010 } 2011 2012 // the conflicts split; its tab exists only when the repo has 2013 // conflicts, so skip the child too to keep the stack 1:1 with the 2014 // tabs. 2015 if (supports_conflicts and data.conflicts.count > 0) { 2016 var split = try initSplit(allocator, session, data, null, false); 2017 errdefer split.deinit(allocator); 2018 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 2019 } 2020 2021 // the stack starts on the page's view. 2022 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 2023 2024 // focus entering the view lands on the tabs first. 2025 outer.getFocus().child_id = outer.children.keys()[header_index]; 2026 2027 return .{ 2028 .box = outer, 2029 .data = data, 2030 .session = session, 2031 }; 2032 } 2033 2034 // the master-detail split showing `status`'s window, or the conflicts 2035 // window when null. 2036 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 2037 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2038 errdefer box.deinit(allocator); 2039 2040 const win = if (status_maybe) |status| data.window(status) else if (drafts and supports_drafts) &data.drafts else if (supports_conflicts) &data.conflicts else unreachable; 2041 2042 // the thread list (one focusable row per title), plus a "next" link that 2043 // reloads the page rooted at the following thread. 2044 { 2045 var list_scroll = blk: { 2046 var list_box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 2047 errdefer list_box.deinit(allocator); 2048 if (win.prev_id) |prev| 2049 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 2050 for (win.items) |entry| { 2051 const label: []const u8 = if (entry.comments.count > 0) 2052 try session.page_arena.allocator().print(" {d} comments ", .{entry.comments.count}) 2053 else 2054 ""; 2055 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 2056 } 2057 if (win.next_id) |next| 2058 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 2059 // select the window's first thread (past a leading "previous" 2060 // row) so its description shows on load. 2061 if (win.items.len > 0) 2062 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 2063 else if (list_box.children.count() > 0) 2064 list_box.getFocus().child_id = list_box.children.keys()[0]; 2065 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 2066 }; 2067 errdefer list_scroll.deinit(allocator); 2068 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 } }); 2069 } 2070 2071 // frame the detail pane inside the master-detail split, below the 2072 // way back to the list, shown only while the list is hidden 2073 { 2074 var frame = try wgt.Box(Widget).init(allocator, .{ .border = .hidden, .direction = .vert }); 2075 errdefer frame.deinit(allocator); 2076 frame.getFocus().mode = .mouse; 2077 try widget.addListBack(allocator, &frame, "← back to " ++ thread_name ++ " list"); 2078 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 2079 errdefer detail_view.deinit(allocator); 2080 frame.getFocus().child_id = detail_view.getFocus().id; 2081 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 2082 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 2083 } 2084 2085 // the page opens on the list, unless the url names a thread 2086 box.getFocus().child_id = box.children.keys()[if (data.selected_id.len > 0) detail_index else list_index]; 2087 return box; 2088 } 2089 2090 // a thread form, plus the target branch for patches, prefilled from 2091 // `record` when given. its form: subtree makes the web renderer POST 2092 // the inputs to `action`'s route. 2093 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 2094 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2095 errdefer box.deinit(allocator); 2096 box.getFocus().kind = .{ .custom = action }; 2097 2098 const saved_form = session.formFeedback(feedback_tag); 2099 const saved_fields = if (saved_form) |saved| saved.fields else null; 2100 2101 { 2102 var title = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " title " }, .name = "title", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2103 errdefer title.deinit(allocator); 2104 title.getFocus().mode = .all; 2105 if (saved_fields) |saved| 2106 try title.setContent(allocator, saved.title) 2107 else if (record) |r| 2108 try title.setContent(allocator, r.event.title); 2109 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2110 box.getFocus().child_id = title.getFocus().id; 2111 } 2112 2113 { 2114 var labels = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " labels (separate with spaces) " }, .name = "labels", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2115 errdefer labels.deinit(allocator); 2116 labels.getFocus().mode = .all; 2117 if (saved_fields) |saved| 2118 try labels.setContent(allocator, saved.labels) 2119 else if (record) |r| 2120 try labels.setContent(allocator, r.event.labels); 2121 try box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .text_input = labels }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2122 } 2123 2124 { 2125 var description = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " description (markdown supported) " }, .name = "description", .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 2126 errdefer description.deinit(allocator); 2127 description.getFocus().mode = .all; 2128 if (saved_fields) |saved| 2129 try description.setContent(allocator, saved.description) 2130 else if (record) |r| 2131 try description.setContent(allocator, r.event.description); 2132 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 2133 } 2134 2135 if (supports_drafts and record == null) { 2136 var source = try Data.Source.init(allocator, session, if (saved_fields) |saved| saved.source_branch else "", if (saved_fields) |saved| saved.source_kind else .fork); 2137 errdefer source.deinit(allocator); 2138 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .patch_source = source }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2139 } 2140 2141 if (supports_drafts) { 2142 var target = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " target branch " }, .name = "target_branch", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2143 errdefer target.deinit(allocator); 2144 target.getFocus().mode = .all; 2145 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 2146 try target.setContent(allocator, content); 2147 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2148 } 2149 2150 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 2151 2152 return box; 2153 } 2154 2155 fn initCommentForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, author: ui.Author, parent_route: ui.RoutablePage, initial_body: ?[]const u8) !wgt.Box(Widget) { 2156 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2157 errdefer box.deinit(allocator); 2158 box.getFocus().kind = .{ .custom = action }; 2159 2160 { 2161 var parent = try ui.authorBox(allocator, session.page_arena, author); 2162 errdefer parent.deinit(allocator); 2163 parent.options.top_label.text = " replying to "; 2164 parent.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 2165 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2166 box.getFocus().child_id = parent.getFocus().id; 2167 } 2168 2169 { 2170 var body = try wgt.TextInput.init(allocator, .{ 2171 .top_label = .{ .text = " comment (markdown supported) " }, 2172 .name = "body", 2173 .visible_width = null, 2174 .round_corners = true, 2175 .render_content = session.is_terminal, 2176 .multiline = true, 2177 .visible_height = 5, 2178 .scroll = .{ .fill = true }, 2179 }); 2180 errdefer body.deinit(allocator); 2181 body.getFocus().mode = .all; 2182 if (initial_body) |text| try body.setContent(allocator, text); 2183 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 2184 } 2185 2186 try addSubmitButtonLabeled(allocator, &box, "submit"); 2187 return box; 2188 } 2189 2190 const ConfirmationButton = struct { 2191 label: []const u8, 2192 bottom_label: []const u8 = "", 2193 action: []const u8 = "submit", 2194 }; 2195 2196 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 2197 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 2198 errdefer box.deinit(allocator); 2199 box.getFocus().kind = .{ .custom = action }; 2200 2201 var prompt = try wgt.Text.init(allocator, "are you sure?"); 2202 errdefer prompt.deinit(allocator); 2203 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 2204 2205 for (buttons, 0..) |options, index| { 2206 var button = try wgt.TextBox.init(allocator, options.label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = options.bottom_label } }); 2207 errdefer button.deinit(allocator); 2208 button.getFocus().mode = .all; 2209 button.getFocus().kind = .{ .custom = options.action }; 2210 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 2211 if (index == 0) box.getFocus().child_id = button.getFocus().id; 2212 } 2213 2214 return Center.init(allocator, .{ .box = box }); 2215 } 2216 2217 // a form's submit button, then a spacer absorbing the leftover 2218 // min-height the box hands its last child, so the button keeps its 2219 // natural height 2220 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 2221 { 2222 var submit = try SubmitButton.initLabeled(allocator, label); 2223 errdefer submit.deinit(allocator); 2224 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2225 } 2226 { 2227 var spacer = try Spacer.init(allocator); 2228 errdefer spacer.deinit(allocator); 2229 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 2230 } 2231 } 2232 2233 // the conflict resolve form. the "use this" links are navigations that 2234 // flip the url's theirs: pick, reloading the prefills; one submit 2235 // settles every conflict. the form: subtree makes the web renderer POST 2236 // to the resolve route. 2237 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 2238 const aa = session.page_arena.allocator(); 2239 // init only picks the resolve view once it has read the conflict 2240 const conflict = if (data.conflict) |*c| c else unreachable; 2241 2242 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2243 errdefer box.deinit(allocator); 2244 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 2245 box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 2246 2247 if (conflict.title) |*fc| { 2248 try addLabel(allocator, &box, "title conflict:"); 2249 try addFieldConflict(allocator, &box, session, data, "title", fc); 2250 } 2251 if (conflict.labels) |*fc| { 2252 try addLabel(allocator, &box, "labels conflict:"); 2253 try addFieldConflict(allocator, &box, session, data, "labels", fc); 2254 } 2255 if (comptime @hasField(Data.Conflict, "status")) { 2256 if (conflict.status) |*fc| { 2257 try addLabel(allocator, &box, "status conflict:"); 2258 try addAtomicConflict(allocator, &box, session, data, "status", fc); 2259 } 2260 } 2261 if (comptime @hasField(Data.Conflict, "revision")) { 2262 if (conflict.revision) |*fc| { 2263 try addLabel(allocator, &box, "revision conflict:"); 2264 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 2265 } 2266 if (conflict.target_branch) |*fc| { 2267 try addLabel(allocator, &box, "target branch conflict:"); 2268 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 2269 } 2270 } 2271 2272 if (conflict.description) |*desc| { 2273 try addLabel(allocator, &box, "description conflict:"); 2274 var hunk_index: usize = 0; 2275 for (desc.chunks, 0..) |*chunk, chunk_index| { 2276 switch (chunk.*) { 2277 .same => |text| { 2278 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .word }); 2279 errdefer tb.deinit(allocator); 2280 tb.getFocus().mode = .all; 2281 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2282 }, 2283 // an auto-resolved chunk: only one side changed it, so its 2284 // version stands, shown distinctly with its author 2285 .auto => |auto| { 2286 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 2287 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 2288 const label = switch (author) { 2289 .user_name, .email => |name| try aa.print(" {s} by {s} ", .{ verb, name }), 2290 .unknown => try aa.print(" {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 2291 }; 2292 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2293 errdefer tb.deinit(allocator); 2294 tb.getFocus().mode = .all; 2295 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2296 }, 2297 .conflict => |hunk| { 2298 // a blank row on each side sets the group apart from 2299 // the flowing shared and auto-resolved chunks 2300 if (chunk_index > 0) try addGap(allocator, &box); 2301 const name = try aa.print("d{d}", .{hunk_index}); 2302 hunk_index += 1; 2303 const picked_theirs = data.theirsPicked(name); 2304 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 2305 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 2306 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true }); 2307 errdefer resolution_input.deinit(allocator); 2308 resolution_input.getFocus().mode = .all; 2309 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 2310 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2311 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 2312 }, 2313 } 2314 } 2315 } 2316 2317 try addSubmitButtonLabeled(allocator, &box, "submit"); 2318 2319 box.getFocus().child_id = box.children.keys()[0]; 2320 return box; 2321 } 2322 2323 // one conflicted scalar field: both sides' version rows, then the 2324 // resolution input prefilled from the picked side. 2325 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2326 const aa = session.page_arena.allocator(); 2327 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2328 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2329 2330 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2331 errdefer resolution_input.deinit(allocator); 2332 resolution_input.getFocus().mode = .all; 2333 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2334 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2335 } 2336 2337 // atomic fields can only pick one complete side 2338 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2339 const aa = session.page_arena.allocator(); 2340 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2341 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2342 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2343 .border = .single, 2344 .round_corners = true, 2345 .wrap_kind = .word, 2346 .top_label = .{ .text = " resolution " }, 2347 }); 2348 errdefer selected.deinit(allocator); 2349 selected.getFocus().mode = .all; 2350 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2351 } 2352 2353 // a blank row setting a conflict group apart from its neighbors 2354 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2355 var text = try wgt.Text.init(allocator, " "); 2356 errdefer text.deinit(allocator); 2357 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2358 } 2359 2360 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2361 var label = try SectionLabel.init(allocator, content); 2362 errdefer label.deinit(allocator); 2363 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2364 } 2365 2366 // one side of a conflict: the "use this" link on the left, the version 2367 // itself as a labeled read-only box. 2368 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2369 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2370 errdefer row.deinit(allocator); 2371 2372 { 2373 const use_label = "use this"; 2374 var use = try wgt.TextBox.init(allocator, use_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 2375 errdefer use.deinit(allocator); 2376 use.getFocus().mode = .all; 2377 use.getFocus().kind = .{ .custom = link }; 2378 try row.children.put(allocator, use.getFocus().id, .{ 2379 .widget = .{ .text_box = use }, 2380 .rect = null, 2381 .min_size = .{ .width = use_label.len + 2, .height = null }, 2382 .max_size = .{ .width = use_label.len + 2, .height = null }, 2383 }); 2384 } 2385 2386 { 2387 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2388 errdefer tb.deinit(allocator); 2389 tb.getFocus().mode = .all; 2390 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2391 } 2392 2393 row.getFocus().child_id = row.children.keys()[0]; 2394 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2395 } 2396 2397 // the label of one side's version box, naming its author 2398 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2399 return switch (author) { 2400 .user_name, .email => |name| try aa.print(" edited by {s} ", .{name}), 2401 .unknown => if (ours) " current version " else " their version ", 2402 }; 2403 } 2404 2405 // the a: link that reloads the resolve page with `name` picked from ours 2406 // or theirs, reprefilling its resolution input 2407 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2408 const aa = session.page_arena.allocator(); 2409 var picks: std.ArrayList(u8) = .empty; 2410 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2411 while (it.next()) |pick| { 2412 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2413 if (picks.items.len > 0) try picks.append(aa, ','); 2414 try picks.appendSlice(aa, pick); 2415 } 2416 if (theirs) { 2417 if (picks.items.len > 0) try picks.append(aa, ','); 2418 try picks.appendSlice(aa, name); 2419 } 2420 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2421 return aa.print("a:{s}", .{try route.toUrl(session.page_arena)}); 2422 } 2423 2424 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2425 var row = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = bottom_label } }); 2426 errdefer row.deinit(allocator); 2427 row.getFocus().mode = .all; 2428 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2429 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2430 } 2431 2432 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2433 self.box.deinit(allocator); 2434 } 2435 2436 fn header(self: *This) *HeaderType { 2437 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2438 } 2439 2440 fn viewStack(self: *This) *wgt.Stack(Widget) { 2441 return &self.box.children.values()[stack_index].widget.stack; 2442 } 2443 2444 // `index`'s master-detail split inside the stack. 2445 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2446 return &self.viewStack().children.values()[index].box; 2447 } 2448 2449 // the labels view's flow inside the stack. 2450 fn labelsView(self: *This) *WordFlow { 2451 return &self.viewStack().children.values()[labels_view_index].word_flow; 2452 } 2453 2454 // the non-confirmation form inside the stack 2455 fn formBox(self: *This) ?*wgt.Box(Widget) { 2456 return switch (self.viewStack().children.values()[form_view_index]) { 2457 .box => |*box| box, 2458 // the resolve form sits inside a scroll on the terminal 2459 .scroll => |*scroll| &scroll.child.box, 2460 else => null, 2461 }; 2462 } 2463 2464 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2465 if (supports_conflicts and self.data.view == .resolve) return null; 2466 return switch (self.data.view) { 2467 .new_comment, .edit_comment => null, 2468 else => self.formBox(), 2469 }; 2470 } 2471 2472 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2473 for (form.children.values()) |*child| switch (child.widget) { 2474 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2475 else => {}, 2476 }; 2477 return error.MissingFormField; 2478 } 2479 2480 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2481 for (form.children.values()) |*child| { 2482 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2483 } 2484 return null; 2485 } 2486 2487 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2488 return switch (self.viewStack().children.values()[form_view_index]) { 2489 .center => |*center| switch (center.child.*) { 2490 .box => |*box| box, 2491 else => null, 2492 }, 2493 else => null, 2494 }; 2495 } 2496 2497 // the resolve form's scroll on the terminal (the web page scrolls itself) 2498 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2499 return switch (self.viewStack().children.values()[form_view_index]) { 2500 .scroll => |*scroll| scroll, 2501 else => null, 2502 }; 2503 } 2504 2505 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2506 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2507 } 2508 2509 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2510 return &self.listScroll(index).child.box; 2511 } 2512 2513 fn detailFrame(self: *This, index: usize) *wgt.Box(Widget) { 2514 return &self.resultsBox(index).children.values()[detail_index].widget.box; 2515 } 2516 2517 fn detail(self: *This, index: usize) *DetailType { 2518 return &@field(self.detailFrame(index).children.values()[widget.list_pane_index].widget, Data.detail_widget_name); 2519 } 2520 2521 fn window(self: *This, index: usize) *const Window { 2522 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2523 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2524 return self.data.window(splitStatus(index)); 2525 } 2526 2527 fn stackSelectedIndex(self: *This) ?usize { 2528 const stack = self.viewStack(); 2529 const cid = stack.getFocus().child_id orelse return null; 2530 return stack.children.getIndex(cid); 2531 } 2532 2533 // the stack's selected master-detail split (null when the labels view or the 2534 // new-thread form shows). 2535 fn selectedSplitIndex(self: *This) ?usize { 2536 const idx = self.stackSelectedIndex() orelse return null; 2537 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2538 } 2539 2540 fn detailActive(self: *This, index: usize) bool { 2541 const rb = self.resultsBox(index); 2542 const cid = rb.getFocus().child_id orelse return false; 2543 return rb.children.getIndex(cid) == detail_index; 2544 } 2545 2546 fn headerActive(self: *This) bool { 2547 const cid = self.box.getFocus().child_id orelse return false; 2548 return self.box.children.getIndex(cid) == header_index; 2549 } 2550 2551 fn labelsViewActive(self: *This) bool { 2552 if (self.headerActive()) return false; 2553 return self.stackSelectedIndex() == labels_view_index; 2554 } 2555 2556 fn formViewActive(self: *This) bool { 2557 if (self.headerActive()) return false; 2558 return self.stackSelectedIndex() == form_view_index; 2559 } 2560 2561 // the selected thread's index, or null when a window-navigation row is 2562 // selected (a leading "previous" row shifts the thread rows down by one). 2563 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2564 const lb = self.listBox(index); 2565 const cid = lb.getFocus().child_id orelse return null; 2566 const idx = lb.children.getIndex(cid) orelse return null; 2567 const win = self.window(index); 2568 const lead: usize = if (win.prev_id != null) 1 else 0; 2569 if (idx < lead or idx - lead >= win.items.len) return null; 2570 return idx - lead; 2571 } 2572 2573 // swap the detail pane to the selected thread when it changes 2574 fn refreshDetail(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2575 const selected = self.selectedThreadIndex(index) orelse return; 2576 const entry = self.window(index).items[selected]; 2577 const changed = if (self.detail(index).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2578 if (!changed) return; 2579 try self.detail(index).setEntry(allocator, entry); 2580 widget.setListBackLink(self.detailFrame(index), try rowLink(self.session.page_arena, self.data, entry.id)); 2581 } 2582 2583 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2584 self.clearGrid(); 2585 2586 // each header tab maps 1:1 to a stack child by position 2587 if (self.header().getSelectedIndex()) |index| { 2588 const stack = self.viewStack(); 2589 stack.getFocus().child_id = stack.children.keys()[index]; 2590 } 2591 2592 if (self.selectedSplitIndex()) |i| { 2593 // whether the detail pane fits beside the list, which decides 2594 // the list cap below and whether the detail carries a way back 2595 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2596 try self.refreshDetail(allocator, i); 2597 widget.showListBack(self.detailFrame(i), !both_panes_fit); 2598 2599 // the selected list row shows a border (the focused TextBox 2600 // upgrades it to a double border itself); the rest stay borderless. 2601 const lb = self.listBox(i); 2602 for (lb.children.keys(), lb.children.values()) |id, *child| { 2603 switch (child.widget) { 2604 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2605 else => {}, 2606 } 2607 } 2608 2609 // cap the list at list_max_width only while the detail pane fits 2610 // beside it. the box drops the detail when the width can't hold 2611 // both minimums, so when it's that narrow we lift the cap and let 2612 // the list fill the whole width. 2613 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2614 2615 // stretch the detail pane across the rest of the width so it fills 2616 // the area rather than shrinking to its content; its scroll fills 2617 // the pane. 2618 if (constraint.max_size.width) |w| { 2619 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2620 } else { 2621 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2622 } 2623 } 2624 2625 // refresh the form inputs' entries in the session's focus-id -> input 2626 // map with this frame's addresses, so the web/wasm form handling can 2627 // find them by focus id 2628 if (self.threadForm()) |form| { 2629 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2630 (try formField(form, "title")).options.top_label.text = if (failure == .required_title) " title (required) " else " title "; 2631 if (comptime supports_drafts) { 2632 if (formWidget(form, .patch_source)) |source| { 2633 source.field().options.top_label.text = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2634 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2635 try submit.setLabel(allocator, switch (source.selectedKind()) { 2636 .fork => "submit draft", 2637 .branch => "submit patch", 2638 }); 2639 } 2640 (try formField(form, "target_branch")).options.top_label.text = 2641 if (failure == .invalid_target_branch) " target branch (invalid) " else if (self.data.view == .edit and failure == .same_branch) " target branch (same as source) " else if (self.data.view == .edit and failure == .unrelated_branches) " target branch (unrelated to source) " else if (self.data.view == .edit and failure == .invalid_source_branch) " target branch (source branch not found) " else " target branch "; 2642 } 2643 } 2644 if (self.formBox()) |form| { 2645 const inputs_arena = self.session.arena.allocator(); 2646 for (form.children.values()) |*child| switch (child.widget) { 2647 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2648 else => {}, 2649 }; 2650 } 2651 2652 try self.box.build(allocator, constraint, root_focus); 2653 } 2654 2655 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2656 // wheel input moves focus like the arrows 2657 const key: Key = switch (raw_key) { 2658 .scroll_up => .arrow_up, 2659 .scroll_down => .arrow_down, 2660 else => raw_key, 2661 }; 2662 2663 if (self.headerActive()) { 2664 // down from the sub-header re-enters the stack if the selected 2665 // view has something to focus; enter in the search box submits 2666 // the query; other keys move within the sub-header. 2667 if (inp.vertDirection(key) == .down) { 2668 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2669 .word_flow => |*tf| tf.text_boxes.items.len > 0, 2670 // nothing focusable in the unauthorized view 2671 .unauthorized => false, 2672 else => true, 2673 } else false; 2674 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2675 } else if (key == .enter) { 2676 if (try self.header().submittedText(allocator)) |submission| { 2677 defer allocator.free(submission.text); 2678 switch (submission.kind) { 2679 .search => try self.submitSearch(submission.text), 2680 .assignee => try self.submitAssignee(submission.text), 2681 } 2682 } 2683 } else { 2684 try self.header().input(allocator, key, root_focus); 2685 } 2686 return; 2687 } 2688 if (self.labelsViewActive()) { 2689 self.labelsViewInput(key, root_focus); 2690 return; 2691 } 2692 if (self.formViewActive()) { 2693 if (confirmation(self.data.view) != null) { 2694 try self.confirmationFormInput(allocator, key, root_focus); 2695 } else if (supports_conflicts and self.data.view == .resolve) { 2696 try self.resolveInput(allocator, key, root_focus); 2697 } else { 2698 try self.formInput(allocator, key, root_focus); 2699 } 2700 return; 2701 } 2702 const i = self.selectedSplitIndex() orelse return; 2703 if (self.detailActive(i)) { 2704 // the way back to the list, by enter or a click 2705 if (widget.listBackId(self.detailFrame(i))) |id| { 2706 if (inp.activated(root_focus, id, key)) return self.focusList(i, root_focus); 2707 if (root_focus.grandchild_id == id) return switch (key) { 2708 .arrow_left => self.focusList(i, root_focus), 2709 .arrow_up => self.focusHeader(root_focus), 2710 .arrow_down => _ = self.detail(i).focusFirst(root_focus), 2711 else => {}, 2712 }; 2713 } 2714 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2715 .none => {}, 2716 .list => self.focusList(i, root_focus), 2717 .header => if (widget.listBackId(self.detailFrame(i))) |id| root_focus.setFocus(id) else self.focusHeader(root_focus), 2718 } 2719 } else { 2720 try self.listInput(allocator, i, key, root_focus); 2721 } 2722 } 2723 2724 // navigate to the typed query's results, keeping the label filter and the 2725 // status list the page shows. an empty box clears a filtered page and 2726 // does nothing on any other one. 2727 fn submitSearch(self: *This, text: []const u8) !void { 2728 if (text.len == 0 and self.data.search == null) return; 2729 const selected = self.header().getSelectedIndex() orelse 0; 2730 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2731 var route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2732 const assignee = assigneeFilter(self.data); 2733 if (assignee.len != 0) route = route.withEncodedAssignee(assignee) orelse return; 2734 try self.session.navigate(route.withSearch(text) orelse return); 2735 } 2736 2737 // navigate to the issues assigned to the typed name or email, keeping 2738 // the label, the search and the status list. an empty box clears the 2739 // filter and does nothing on an unfiltered page. 2740 fn submitAssignee(self: *This, typed: []const u8) !void { 2741 const text = std.mem.trim(u8, typed, " \t"); 2742 if (text.len == 0 and assigneeFilter(self.data).len == 0) return; 2743 const selected = self.header().getSelectedIndex() orelse 0; 2744 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2745 var route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2746 if (self.data.search) |search| route = route.withSearch(search) orelse return; 2747 try self.session.navigate(route.withEncodedAssignee(try ui.urlEncodeRef(self.session.page_arena.allocator(), text)) orelse return); 2748 } 2749 2750 // arrow keys move the label selection; up from the top row crosses to the 2751 // header tabs. 2752 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2753 const tf = self.labelsView(); 2754 const cid = tf.focus.child_id orelse return; 2755 const cur = tf.indexOfFocusId(cid) orelse return; 2756 const count = tf.text_boxes.items.len; 2757 switch (key) { 2758 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2759 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2760 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2761 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2762 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2763 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2764 else => {}, 2765 } 2766 } 2767 2768 fn listInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 2769 // up/down (and the scroll wheel) move the selection a row; page up/down 2770 // jump a fixed amount. right/Enter cross into the detail pane. up from 2771 // the top row crosses into the header tabs. 2772 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2773 const lb = self.listBox(index); 2774 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2775 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2776 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2777 return; 2778 } 2779 switch (key) { 2780 .enter, .arrow_right => self.focusDetail(index, root_focus), 2781 // when the window is too narrow to lay out the detail pane beside 2782 // the list, a click on a row opens it like enter. the row was just 2783 // selected, so the detail is swapped to it ahead of the build. 2784 .mouse => |mouse| if (self.resultsBox(index).children.values()[detail_index].rect == null and widget.clickOnSelectedRow(self.listBox(index), root_focus, mouse)) { 2785 try self.refreshDetail(allocator, index); 2786 self.focusDetail(index, root_focus); 2787 }, 2788 else => {}, 2789 } 2790 } 2791 2792 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2793 const form = self.formBox() orelse return; 2794 const cid = form.getFocus().child_id orelse return; 2795 const cur = form.children.getIndex(cid) orelse return; 2796 const child = &form.children.values()[cur]; 2797 2798 if (child.widget == .text_input) { 2799 const text_input = &child.widget.text_input; 2800 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2801 return child.widget.input(allocator, key, root_focus); 2802 } 2803 2804 const on_submit = child.widget == .submit_button; 2805 switch (key) { 2806 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2807 root_focus.setFocus(form.children.keys()[i]) 2808 else 2809 self.focusHeader(root_focus), 2810 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2811 .enter => if (on_submit) return self.submitForm(allocator), 2812 .mouse => |mouse| if (on_submit) { 2813 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2814 }, 2815 else => {}, 2816 } 2817 try child.widget.input(allocator, key, root_focus); 2818 } 2819 2820 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2821 const form = self.confirmationForm() orelse return; 2822 const child_id = form.getFocus().child_id orelse return; 2823 const index = form.children.getIndex(child_id) orelse return; 2824 switch (key) { 2825 .arrow_up, .back_tab => if (index > 1) 2826 root_focus.setFocus(form.children.keys()[index - 1]) 2827 else 2828 self.focusHeader(root_focus), 2829 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2830 .enter => try self.submitConfirmation(allocator, index), 2831 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2832 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2833 try self.submitConfirmation(allocator, button_index); 2834 break; 2835 } 2836 }, 2837 else => {}, 2838 } 2839 } 2840 2841 // the multiline hunk inputs keep enter and any up/down that has a row to 2842 // move to; enter on a "use this" link is a navigation the host follows 2843 // before this runs. 2844 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2845 const form = self.formBox() orelse return; 2846 const cid = form.getFocus().child_id orelse return; 2847 const cur = form.children.getIndex(cid) orelse return; 2848 const child = &form.children.values()[cur]; 2849 2850 if (child.widget == .text_input) { 2851 const ti = &child.widget.text_input; 2852 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2853 return child.widget.input(allocator, key, root_focus); 2854 } 2855 2856 const on_submit = child.widget == .submit_button; 2857 2858 switch (key) { 2859 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2860 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2861 .arrow_left, .arrow_right => switch (child.widget) { 2862 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2863 else => try child.widget.input(allocator, key, root_focus), 2864 }, 2865 .enter => if (on_submit) try self.submitResolution(allocator), 2866 .mouse => |mouse| if (on_submit) { 2867 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2868 }, 2869 else => try child.widget.input(allocator, key, root_focus), 2870 } 2871 } 2872 2873 // a multiline input keeps enter and any up/down that has a row to move to 2874 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2875 return switch (key) { 2876 .enter => true, 2877 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2878 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2879 else => false, 2880 }; 2881 } 2882 2883 // the neighboring form control, skipping spacers and blank rows 2884 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2885 var i = cur; 2886 while (true) { 2887 if (down) { 2888 i += 1; 2889 if (i >= form.children.count()) return null; 2890 } else { 2891 if (i == 0) return null; 2892 i -= 1; 2893 } 2894 const child = &form.children.values()[i]; 2895 if (child.hidden) continue; 2896 switch (child.widget) { 2897 .spacer, .text => continue, 2898 else => return i, 2899 } 2900 } 2901 } 2902 2903 // focus the resolve-form child at `i` (a version row focuses its selected 2904 // child) and keep it visible on the terminal 2905 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2906 const child = &form.children.values()[i]; 2907 switch (child.widget) { 2908 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2909 else => root_focus.setFocus(form.children.keys()[i]), 2910 } 2911 if (self.session.is_terminal) { 2912 if (self.resolveScroll()) |sc| { 2913 if (child.rect) |rect| sc.scrollToRect(rect); 2914 } 2915 } 2916 } 2917 2918 // re-emit the conflicted thread's event with the resolved content; any 2919 // event on the thread settles its conflict entry. this is the terminal 2920 // path; the web posts the form to the resolve route. 2921 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2922 if (comptime wasm) return; 2923 const io = self.session.io orelse return; 2924 const src = self.data.repo_source orelse return; 2925 const entry = self.data.selectedThread() orelse return; 2926 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2927 const form = self.formBox() orelse return; 2928 2929 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2930 var title: ?[]u8 = null; 2931 var labels: ?[]u8 = null; 2932 var hunks: std.ArrayList([]const u8) = .empty; 2933 defer { 2934 if (title) |t| allocator.free(t); 2935 if (labels) |t| allocator.free(t); 2936 for (hunks.items) |h| allocator.free(h); 2937 hunks.deinit(allocator); 2938 } 2939 for (form.children.values()) |*child| switch (child.widget) { 2940 .text_input => |*ti| { 2941 const text = try ti.text(allocator); 2942 errdefer allocator.free(text); 2943 if (std.mem.eql(u8, ti.options.name, "title")) { 2944 title = text; 2945 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2946 labels = text; 2947 } else { 2948 try hunks.append(allocator, text); 2949 } 2950 }, 2951 else => {}, 2952 }; 2953 2954 const id_bytes = try evt.parseEventId(entry.id); 2955 2956 switch (src.repo_kind) { 2957 inline else => |repo_kind| { 2958 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2959 defer any_repo.deinit(io, allocator); 2960 switch (any_repo) { 2961 inline else => |*repo| { 2962 var resolution: Event.Resolve = .{ 2963 .title = title, 2964 .labels = labels, 2965 .hunks = hunks.items, 2966 }; 2967 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2968 const change = Event.Update{ .resolve = resolution }; 2969 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2970 // leave the form up for correction 2971 error.InvalidFields => return, 2972 else => |e| return e, 2973 }; 2974 }, 2975 } 2976 }, 2977 } 2978 2979 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2980 try self.session.navigate(route); 2981 } 2982 2983 // commit a new or edited comment and navigate to its permalink. this is the 2984 // terminal path; the web posts the form to its comment route. 2985 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2986 if (comptime wasm) return; 2987 const io = self.session.io orelse return; 2988 const src = self.data.repo_source orelse return; 2989 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2990 const author = actor.author; 2991 const form = self.formBox() orelse return; 2992 const body_input = try formField(form, "body"); 2993 const body = try body_input.text(allocator); 2994 defer allocator.free(body); 2995 if (!evt.Comment.fieldsValid(body)) return; 2996 2997 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2998 @memcpy(&thread_id, self.data.selected_id); 2999 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 3000 var parent: [evt.event_id_size * 2]u8 = undefined; 3001 @memcpy(&parent, self.data.comment_id); 3002 break :blk parent; 3003 }; 3004 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3005 const editing = self.data.view == .edit_comment; 3006 if (editing) { 3007 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3008 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3009 } 3010 switch (src.repo_kind) { 3011 inline else => |repo_kind| { 3012 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3013 defer any_repo.deinit(io, allocator); 3014 switch (any_repo) { 3015 inline else => |*repo| if (editing) { 3016 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3017 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3018 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3019 } else { 3020 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &parent_id, body, author); 3021 }, 3022 } 3023 }, 3024 } 3025 3026 body_input.clear(allocator); 3027 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3028 try self.session.navigate(route); 3029 } 3030 3031 // terminal form submissions; the web posts to the forms' routes instead 3032 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3033 switch (self.data.view) { 3034 .new_comment, .edit_comment => try self.submitComment(allocator), 3035 .edit => try self.submitEditedThread(allocator), 3036 else => try self.submitNewThread(allocator), 3037 } 3038 } 3039 3040 // commit the new thread to the repo's events branch and navigate to it. 3041 // this is the terminal path; the web posts the form to the thread route, 3042 // so the wasm side never runs (or compiles) the repo access below. 3043 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3044 if (comptime wasm) return; 3045 const io = self.session.io orelse return; 3046 const src = self.data.repo_source orelse return; 3047 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3048 3049 const form = self.threadForm() orelse return; 3050 const title_input = try formField(form, "title"); 3051 const labels_input = try formField(form, "labels"); 3052 const description_input = try formField(form, "description"); 3053 3054 const title = try title_input.text(allocator); 3055 defer allocator.free(title); 3056 const labels = try labels_input.text(allocator); 3057 defer allocator.free(labels); 3058 const description = try description_input.text(allocator); 3059 defer allocator.free(description); 3060 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3061 defer if (comptime supports_drafts) allocator.free(target_branch); 3062 3063 if (!Event.fieldsValid(title, labels)) { 3064 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3065 return; 3066 } 3067 3068 if (comptime supports_drafts) { 3069 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3070 const source_branch = switch (source.selectedKind()) { 3071 .fork => null, 3072 .branch => try source.field().text(allocator), 3073 }; 3074 defer if (source_branch) |branch| allocator.free(branch); 3075 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3076 const failure = FeedbackFailure.fromError(err) orelse return err; 3077 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3078 return; 3079 }; 3080 title_input.clear(allocator); 3081 labels_input.clear(allocator); 3082 description_input.clear(allocator); 3083 (try formField(form, "target_branch")).clear(allocator); 3084 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3085 try self.session.navigate(route); 3086 return; 3087 } 3088 3089 var id_bytes: [evt.event_id_size]u8 = undefined; 3090 io.random(&id_bytes); 3091 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3092 3093 const event = evt.EventWithId{ 3094 .id = event_id_hex, 3095 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3096 .author = actor.author, 3097 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3098 .title = title, 3099 .description = description, 3100 .labels = labels, 3101 }), 3102 }; 3103 3104 switch (src.repo_kind) { 3105 inline else => |repo_kind| { 3106 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3107 defer any_repo.deinit(io, allocator); 3108 switch (any_repo) { 3109 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3110 } 3111 }, 3112 } 3113 3114 // wipe the form so a return visit starts fresh 3115 title_input.clear(allocator); 3116 labels_input.clear(allocator); 3117 description_input.clear(allocator); 3118 3119 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", &event_id_hex) orelse return; 3120 try self.session.navigate(route); 3121 } 3122 3123 // re-emit the selected thread's event with the form's content (status 3124 // preserved), then reload the thread's page. this is the terminal path; 3125 // the web posts the form to the edit route. 3126 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3127 if (comptime wasm) return; 3128 const io = self.session.io orelse return; 3129 const entry = self.data.selectedThread() orelse return; 3130 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3131 const author = actor.author; 3132 3133 const form = self.threadForm() orelse return; 3134 const title_input = try formField(form, "title"); 3135 const labels_input = try formField(form, "labels"); 3136 const description_input = try formField(form, "description"); 3137 3138 const title = try title_input.text(allocator); 3139 defer allocator.free(title); 3140 const labels = try labels_input.text(allocator); 3141 defer allocator.free(labels); 3142 const description = try description_input.text(allocator); 3143 defer allocator.free(description); 3144 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3145 defer if (comptime supports_drafts) allocator.free(target_branch); 3146 3147 if (!Event.fieldsValid(title, labels)) { 3148 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3149 return; 3150 } 3151 const src = self.data.repo_source orelse return; 3152 3153 if (comptime supports_drafts) { 3154 if (entryDraft(entry.*)) { 3155 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3156 const failure = FeedbackFailure.fromError(err) orelse return err; 3157 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3158 return; 3159 }; 3160 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3161 try self.session.navigate(route); 3162 return; 3163 } 3164 } 3165 3166 const id_bytes = try evt.parseEventId(entry.id); 3167 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3168 3169 switch (src.repo_kind) { 3170 inline else => |repo_kind| { 3171 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3172 defer any_repo.deinit(io, allocator); 3173 switch (any_repo) { 3174 inline else => |*repo| if (supports_drafts) { 3175 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3176 .title = title, 3177 .labels = labels, 3178 .description = description, 3179 .target_branch = target_branch, 3180 } }, author) catch |err| { 3181 const failure = FeedbackFailure.fromError(err) orelse return err; 3182 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3183 return; 3184 }; 3185 } else if (has_status) 3186 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3187 .title = title, 3188 .labels = labels, 3189 .description = description, 3190 } }, author) 3191 else 3192 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3193 } 3194 }, 3195 } 3196 3197 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3198 try self.session.navigate(route); 3199 } 3200 3201 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3202 const aa = self.session.arena.allocator(); 3203 const source = if (supports_drafts) blk: { 3204 const form = self.threadForm() orelse break :blk null; 3205 break :blk formWidget(form, .patch_source); 3206 } else null; 3207 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3208 .failure = failure, 3209 .fields = if (comptime supports_drafts) .{ 3210 .title = try aa.dupe(u8, title), 3211 .labels = try aa.dupe(u8, labels), 3212 .description = try aa.dupe(u8, description), 3213 .target_branch = try aa.dupe(u8, target_branch), 3214 .source_branch = if (source) |value| try value.field().text(aa) else "", 3215 .source_kind = if (source) |value| value.selectedKind() else .fork, 3216 } else .{ 3217 .title = try aa.dupe(u8, title), 3218 .labels = try aa.dupe(u8, labels), 3219 .description = try aa.dupe(u8, description), 3220 }, 3221 }); 3222 } 3223 3224 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3225 switch (confirmation(self.data.view) orelse return) { 3226 .publish => if (comptime supports_drafts) { 3227 if (comptime wasm) return; 3228 const entry = self.data.selectedThread() orelse return; 3229 if (!entryDraft(entry.*)) return; 3230 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3231 try self.data.publishDraft(self.session, allocator, author, entry.id); 3232 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", entry.id) orelse return; 3233 try self.session.navigate(route); 3234 } else unreachable, 3235 .merge => if (comptime supports_merge) { 3236 if (comptime wasm) return; 3237 const entry = self.data.selectedThread() orelse return; 3238 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3239 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3240 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3241 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3242 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3243 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3244 try self.session.navigate(route); 3245 return; 3246 }, 3247 else => return err, 3248 }; 3249 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3250 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 3251 try self.session.navigate(route); 3252 } else unreachable, 3253 .remove => try self.submitRemove(allocator), 3254 } 3255 } 3256 3257 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3258 const comment = self.data.comment_id.len != 0; 3259 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3260 const id = evt.parseEventId(event_id) catch return; 3261 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3262 } 3263 3264 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3265 if (comptime wasm) return; 3266 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3267 const author = actor.author; 3268 3269 if (comptime supports_drafts) { 3270 const selected = self.data.selectedThread() orelse return; 3271 if (event_kind == kind and entryDraft(selected.*)) { 3272 try Data.removeDraft(self.session, allocator, author, &id); 3273 const route = draftsRoute(self.data.identity) orelse return; 3274 try self.session.navigate(route); 3275 return; 3276 } 3277 } 3278 3279 const io = self.session.io orelse return; 3280 const src = self.data.repo_source orelse return; 3281 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3282 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3283 3284 switch (src.repo_kind) { 3285 inline else => |repo_kind| { 3286 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3287 defer any_repo.deinit(io, allocator); 3288 switch (any_repo) { 3289 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 3290 } 3291 }, 3292 } 3293 3294 const id_hex = std.fmt.bytesToHex(id, .lower); 3295 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3296 try self.session.navigate(route); 3297 } 3298 3299 // enter the detail pane, landing on the way back to the list when it shows 3300 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3301 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3302 _ = self.detail(index).focusFirst(root_focus); 3303 } 3304 3305 // return to the list. 3306 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3307 root_focus.setFocus(self.listScroll(index).getFocus().id); 3308 } 3309 3310 // cross to the header tabs above the stack. 3311 fn focusHeader(self: *This, root_focus: *Focus) void { 3312 root_focus.setFocus(self.box.children.keys()[header_index]); 3313 } 3314 3315 pub fn clearGrid(self: *This) void { 3316 self.box.clearGrid(); 3317 } 3318 3319 pub fn getGrid(self: This) ?Grid { 3320 return self.box.getGrid(); 3321 } 3322 3323 pub fn getFocus(self: *This) *Focus { 3324 return self.box.getFocus(); 3325 } 3326 3327 // for the parent's "scroll up at the top jumps to the header" check: at the 3328 // top only while the header tabs hold focus, so up from the split 3329 // crosses into the tabs first. 3330 pub fn atTop(self: *This) bool { 3331 return self.headerActive(); 3332 } 3333 }; 3334 } 3335