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