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