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