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