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