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