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