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