╭ 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 !description_page and comment_page == null) { 1367 try self.data.appendDetails(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 or !entryDraft(entry)) { 1471 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1472 try Attachment.appendRows(allocator, inner, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 1473 1474 if (!description_page) { 1475 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoThreadCommentNewRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1476 errdefer reply.deinit(allocator); 1477 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1478 1479 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments"); 1480 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1481 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1482 1483 var spacer = try Spacer.init(allocator); 1484 errdefer spacer.deinit(allocator); 1485 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1486 } 1487 } 1488 1489 // select the first focusable widget below the tool row 1490 for (tool_row_index + 1..inner.children.count()) |child_index| { 1491 if (!self.detailChildFocusable(index, child_index)) continue; 1492 inner.getFocus().child_id = inner.children.keys()[child_index]; 1493 break; 1494 } 1495 1496 // reset the scroll to the top for the newly-shown thread: directly on the 1497 // terminal (the wasm offset), and via a version bump on the web (so the 1498 // renderer's scroll id changes and JS drops the preserved position). 1499 const sc = self.detailScroll(index); 1500 sc.x = 0; 1501 sc.y = 0; 1502 sc.getFocus().version +%= 1; 1503 } 1504 1505 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1506 // wheel input moves focus like the arrows 1507 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1508 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1509 else 1510 raw_key; 1511 1512 if (self.headerActive()) { 1513 // down from the tabs re-enters the stack if the selected view has 1514 // something to focus; other keys move the tabs. 1515 if (inp.vertDirection(key) == .down) { 1516 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1517 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1518 // nothing focusable in the unauthorized view 1519 .unauthorized => false, 1520 else => true, 1521 } else false; 1522 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1523 } else { 1524 try self.header().input(allocator, key, root_focus); 1525 } 1526 return; 1527 } 1528 if (self.tagsViewActive()) { 1529 self.tagsViewInput(key, root_focus); 1530 return; 1531 } 1532 if (self.formViewActive()) { 1533 if (self.data.view == .remove) { 1534 try self.removeFormInput(allocator, key, root_focus); 1535 } else if (supports_conflicts and self.data.view == .resolve) { 1536 try self.resolveInput(allocator, key, root_focus); 1537 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1538 try self.commentFormInput(allocator, key, root_focus); 1539 } else { 1540 try self.formInput(allocator, key, root_focus); 1541 } 1542 return; 1543 } 1544 const i = self.selectedSplitIndex() orelse return; 1545 if (self.detailActive(i)) { 1546 try self.detailInput(allocator, i, key, root_focus); 1547 } else { 1548 self.listInput(i, key, root_focus); 1549 } 1550 } 1551 1552 // arrow keys move the tag selection; up from the top row crosses to the 1553 // header tabs. 1554 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 1555 const tf = self.tagsView(); 1556 const cid = tf.focus.child_id orelse return; 1557 const cur = tf.indexOfFocusId(cid) orelse return; 1558 const count = tf.text_boxes.items.len; 1559 switch (key) { 1560 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1561 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1562 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1563 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1564 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1565 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1566 else => {}, 1567 } 1568 } 1569 1570 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1571 // up/down (and the scroll wheel) move the selection a row; page up/down 1572 // jump a fixed amount. right/Enter cross into the detail pane. up from 1573 // the top row crosses into the header tabs. 1574 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1575 const lb = self.listBox(index); 1576 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1577 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1578 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1579 return; 1580 } 1581 switch (key) { 1582 .enter, .arrow_right => _ = self.focusDetail(root_focus), 1583 else => {}, 1584 } 1585 } 1586 1587 fn detailInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1588 switch (key) { 1589 .page_up => { 1590 self.pageDetail(index, root_focus, -10); 1591 return; 1592 }, 1593 .page_down => { 1594 self.pageDetail(index, root_focus, 10); 1595 return; 1596 }, 1597 .home => { 1598 self.jumpDetail(index, root_focus, false); 1599 return; 1600 }, 1601 .end => { 1602 self.jumpDetail(index, root_focus, true); 1603 return; 1604 }, 1605 else => {}, 1606 } 1607 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)) { 1608 self.commentInput(index, key, root_focus); 1609 return; 1610 } 1611 if (self.toolRowFocused(index, root_focus)) { 1612 try self.toolRowInput(allocator, index, key, root_focus); 1613 return; 1614 } 1615 1616 const child_index = self.focusedDetailChild(index, root_focus) orelse return; 1617 const inner = self.detailInner(index); 1618 const child_id = inner.children.keys()[child_index]; 1619 const child = &inner.children.values()[child_index].widget; 1620 1621 if (child_id == self.title_id[index]) { 1622 switch (key) { 1623 .arrow_left => self.focusList(index, root_focus), 1624 .arrow_up => if (!self.moveDetailVertical(index, root_focus, false)) self.focusToolRow(index, root_focus), 1625 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1626 else => {}, 1627 } 1628 return; 1629 } 1630 if (child_id == self.author_id[index]) { 1631 // the author's a: link is followed by the host; arrows cross to 1632 // neighboring widgets. 1633 switch (key) { 1634 .arrow_left => self.focusList(index, root_focus), 1635 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1636 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1637 else => {}, 1638 } 1639 return; 1640 } 1641 if (child_id == self.description_id[index]) { 1642 self.descriptionInput(index, key, root_focus); 1643 return; 1644 } 1645 1646 switch (child.*) { 1647 .tag_flow => |*tf| { 1648 self.tagsInput(index, child_index, tf, key, root_focus); 1649 return; 1650 }, 1651 .copyable_text => |*copyable| { 1652 switch (key) { 1653 .arrow_up => if (!self.moveDetailVertical(index, root_focus, false)) self.focusToolRow(index, root_focus), 1654 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1655 .arrow_left => if (copyable.selected == 0) 1656 self.focusList(index, root_focus) 1657 else 1658 try copyable.input(allocator, key, root_focus), 1659 else => try copyable.input(allocator, key, root_focus), 1660 } 1661 return; 1662 }, 1663 .box => |*row| if (Attachment.removeId(row)) |attachment_id| { 1664 const remove_id = row.children.keys()[1]; 1665 switch (key) { 1666 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) self.focusList(index, root_focus), 1667 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1668 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1669 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1670 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 1671 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 1672 else => {}, 1673 } 1674 return; 1675 }, 1676 else => {}, 1677 } 1678 self.commentInput(index, key, root_focus); 1679 } 1680 1681 // enter or a click runs the primary action; links are handled by the host 1682 fn toolRowInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1683 const row = self.toolRow(index); 1684 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1685 const child_id = row.children.keys()[cur]; 1686 const on_primary = switch (row.children.values()[cur].widget.getFocus().kind) { 1687 .custom => |action| std.mem.eql(u8, action, "submit"), 1688 else => false, 1689 }; 1690 switch (key) { 1691 .arrow_left => { 1692 var previous = cur; 1693 while (previous > 0) { 1694 previous -= 1; 1695 if (!row.children.values()[previous].widget.getFocus().focusable) continue; 1696 root_focus.setFocus(row.children.keys()[previous]); 1697 return; 1698 } 1699 self.focusList(index, root_focus); 1700 }, 1701 .arrow_right => { 1702 var next = cur + 1; 1703 while (next < row.children.count()) : (next += 1) { 1704 if (!row.children.values()[next].widget.getFocus().focusable) continue; 1705 root_focus.setFocus(row.children.keys()[next]); 1706 return; 1707 } 1708 }, 1709 .arrow_up => self.focusHeader(root_focus), 1710 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1711 .enter => if (on_primary) try self.primaryAction(allocator, index), 1712 .mouse => |mouse| if (on_primary and inp.leftClickOn(root_focus, child_id, mouse)) try self.primaryAction(allocator, index), 1713 else => {}, 1714 } 1715 } 1716 1717 fn primaryAction(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1718 const selected = self.detailed_index[index] orelse return; 1719 const entry = self.window(index).items[selected]; 1720 if (supports_drafts and entryDraft(entry)) 1721 try self.publishDraft(allocator, index) 1722 else 1723 try self.toggleStatus(allocator, index); 1724 } 1725 1726 fn publishDraft(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1727 if (!supports_drafts or comptime wasm) return; 1728 const selected = self.detailed_index[index] orelse return; 1729 const entry = self.window(index).items[selected]; 1730 if (!entryDraft(entry) or !entry.revision_ready) return; 1731 try self.data.publishDraft(self.session, allocator, entry.id); 1732 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 1733 try self.session.navigate(route); 1734 } 1735 1736 fn descriptionInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1737 const sc = self.detailScroll(index); 1738 switch (key) { 1739 .arrow_left => return self.focusList(index, root_focus), 1740 // the terminal moves through the description one line at a time; 1741 // the web lets focus changes drive its native scroll. 1742 .arrow_up => { 1743 if (!self.session.is_terminal) { 1744 _ = self.moveDetailVertical(index, root_focus, false); 1745 return; 1746 } 1747 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1748 const before = sc.y; 1749 sc.y -= 1; 1750 sc.clampToContent(); 1751 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1752 return; 1753 }, 1754 .arrow_down => { 1755 if (!self.session.is_terminal) { 1756 _ = self.moveDetailVertical(index, root_focus, true); 1757 return; 1758 } 1759 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1760 const before = sc.y; 1761 sc.y += 1; 1762 sc.clampToContent(); 1763 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1764 return; 1765 }, 1766 else => return, 1767 } 1768 } 1769 1770 fn commentInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1771 switch (key) { 1772 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1773 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1774 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1775 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1776 else => return, 1777 } 1778 } 1779 1780 // page the detail scroll, then focus the first or last visible widget. 1781 fn pageDetail(self: *This, index: usize, root_focus: *Focus, delta: isize) void { 1782 if (!self.session.is_terminal) return; 1783 const sc = self.detailScroll(index); 1784 sc.y += delta; 1785 sc.clampToContent(); 1786 self.focusVisible(index, root_focus, delta > 0); 1787 } 1788 1789 // jump to the detail's first or last focusable widget. the terminal pins 1790 // its synthetic scroll too; the browser scrolls to the new focus itself. 1791 fn jumpDetail(self: *This, index: usize, root_focus: *Focus, to_end: bool) void { 1792 if (self.session.is_terminal) { 1793 const sc = self.detailScroll(index); 1794 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1795 sc.clampToContent(); 1796 self.focusVisible(index, root_focus, to_end); 1797 } else { 1798 self.focusDetailEdge(index, root_focus, to_end); 1799 } 1800 } 1801 1802 fn focusDetailEdge(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1803 const focus = self.detailInner(index).getFocus(); 1804 var chosen: ?usize = null; 1805 for (focus.children.keys(), focus.children.values()) |id, child| { 1806 if (!child.focus.focusable) continue; 1807 chosen = id; 1808 if (!last) break; 1809 } 1810 if (chosen) |id| root_focus.setFocus(id); 1811 } 1812 1813 fn detailRectVisible(self: *This, index: usize, rect: layout.IRect) bool { 1814 const sc = self.detailScroll(index); 1815 const viewport = sc.grid orelse return false; 1816 const top = sc.y; 1817 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1818 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1819 } 1820 1821 // focus the top- or bottom-most focusable widget intersecting the viewport. 1822 fn focusVisible(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1823 const inner = self.detailInner(index); 1824 var offset: usize = 0; 1825 while (offset < inner.children.count()) : (offset += 1) { 1826 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1827 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1828 } 1829 } 1830 1831 fn focusVisibleChild(self: *This, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1832 const child = &self.detailInner(index).children.values()[child_index]; 1833 const child_rect = child.rect orelse return false; 1834 var chosen: ?usize = null; 1835 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1836 const id = self.detailInner(index).children.keys()[child_index]; 1837 chosen = id; 1838 if (!last) { 1839 root_focus.setFocus(id); 1840 return true; 1841 } 1842 } 1843 1844 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1845 if (!focus_child.focus.focusable) continue; 1846 const rect: layout.IRect = .{ 1847 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1848 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1849 .size = focus_child.rect.size, 1850 }; 1851 if (!self.detailRectVisible(index, rect)) continue; 1852 chosen = id; 1853 if (!last) break; 1854 } 1855 if (chosen) |id| { 1856 root_focus.setFocus(id); 1857 return true; 1858 } 1859 return false; 1860 } 1861 1862 fn focusAdjacentVisible(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1863 const inner = self.detailInner(index); 1864 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1865 while (true) { 1866 if (down) { 1867 next += 1; 1868 if (next >= inner.children.count()) return false; 1869 } else { 1870 if (next == 0) return false; 1871 next -= 1; 1872 } 1873 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1874 self.focusDetailChild(index, next, !down, root_focus); 1875 return true; 1876 } 1877 } 1878 } 1879 1880 fn focusedDetailChild(self: *This, index: usize, root_focus: *Focus) ?usize { 1881 const inner = self.detailInner(index); 1882 var id = root_focus.grandchild_id orelse return null; 1883 while (inner.getFocus().children.get(id)) |child| { 1884 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1885 id = child.parent_id; 1886 } 1887 return null; 1888 } 1889 1890 fn detailChildFocusable(self: *This, index: usize, child_index: usize) bool { 1891 const child = &self.detailInner(index).children.values()[child_index].widget; 1892 const focus = child.getFocus(); 1893 if (focus.focusable) return true; 1894 for (focus.children.values()) |item| { 1895 if (item.focus.focusable) return true; 1896 } 1897 return false; 1898 } 1899 1900 fn focusDetailChild(self: *This, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1901 const inner = self.detailInner(index); 1902 const child = &inner.children.values()[child_index]; 1903 var target = inner.children.keys()[child_index]; 1904 var rect = child.rect orelse return; 1905 switch (child.widget) { 1906 .repo_comment => |*comment| { 1907 if (comment.rowRect(last)) |row_rect| { 1908 rect.x += row_rect.x; 1909 rect.y += row_rect.y; 1910 rect.size = row_rect.size; 1911 } 1912 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1913 }, 1914 .box => |*box| if (box.children.count() > 0) { 1915 target = if (child_index == tool_row_index) 1916 box.getFocus().child_id orelse return 1917 else 1918 box.children.keys()[if (last) box.children.count() - 1 else 0]; 1919 root_focus.setFocus(target); 1920 }, 1921 else => root_focus.setFocus(target), 1922 } 1923 const sc = self.detailScroll(index); 1924 if (sc.grid) |viewport| { 1925 const viewport_height = viewport.size.height - sc.bar_h; 1926 if (rect.size.height > viewport_height) { 1927 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1928 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1929 sc.clampToContent(); 1930 return; 1931 } 1932 } 1933 sc.scrollToRect(rect); 1934 } 1935 1936 fn moveDetailVertical(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1937 const inner = self.detailInner(index); 1938 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1939 if (inner.children.values()[current].widget == .repo_comment) { 1940 const comment = &inner.children.values()[current].widget.repo_comment; 1941 if (down and !comment.bodyFocused()) { 1942 self.focusDetailChild(index, current, true, root_focus); 1943 return true; 1944 } 1945 if (!down and comment.bodyFocused()) { 1946 self.focusDetailChild(index, current, false, root_focus); 1947 return true; 1948 } 1949 } 1950 1951 var next = current; 1952 while (true) { 1953 if (down) { 1954 next += 1; 1955 if (next >= inner.children.count()) return false; 1956 } else { 1957 if (next == 0) return false; 1958 next -= 1; 1959 } 1960 if (!self.detailChildFocusable(index, next)) continue; 1961 self.focusDetailChild(index, next, !down, root_focus); 1962 return true; 1963 } 1964 } 1965 1966 fn moveCommentHorizontal(self: *This, index: usize, root_focus: *Focus, right: bool) bool { 1967 const inner = self.detailInner(index); 1968 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1969 const child = &inner.children.values()[current]; 1970 if (child.widget == .repo_comment) { 1971 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1972 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1973 return moved; 1974 } 1975 const row = switch (child.widget) { 1976 .box => |*box| box, 1977 else => return false, 1978 }; 1979 const selected = row.getFocus().child_id orelse return false; 1980 const selected_index = row.children.getIndex(selected) orelse return false; 1981 const target = if (right) selected_index + 1 else selected_index -| 1; 1982 if (target == selected_index or target >= row.children.count()) return false; 1983 root_focus.setFocus(row.children.keys()[target]); 1984 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1985 return true; 1986 } 1987 1988 // up/down (and tab/shift+tab) move between a form's fields; up from the 1989 // title crosses into the header tabs. the multiline description keeps 1990 // enter and any up/down that has a row to move to. 1991 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1992 const form = self.threadForm() orelse return; 1993 const cid = form.getFocus().child_id orelse return; 1994 const cur = form.children.getIndex(cid) orelse return; 1995 1996 if (cur == description_field_index) { 1997 const child = &form.children.values()[cur]; 1998 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1999 return child.widget.input(allocator, key, root_focus); 2000 } 2001 2002 switch (key) { 2003 .arrow_up, .back_tab => if (cur > 0) 2004 root_focus.setFocus(form.children.keys()[cur - 1]) 2005 else 2006 self.focusHeader(root_focus), 2007 .arrow_down, .tab => if (cur < submit_field_index) { 2008 root_focus.setFocus(form.children.keys()[cur + 1]); 2009 }, 2010 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2011 .mouse => |mouse| if (cur == submit_field_index) { 2012 const submit = &form.children.values()[cur].widget.submit_button; 2013 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2014 }, 2015 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2016 } 2017 } 2018 2019 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2020 const form = self.threadForm() orelse return; 2021 const cid = form.getFocus().child_id orelse return; 2022 const cur = form.children.getIndex(cid) orelse return; 2023 const child = &form.children.values()[cur]; 2024 2025 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2026 return child.widget.input(allocator, key, root_focus); 2027 2028 switch (key) { 2029 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2030 root_focus.setFocus(form.children.keys()[cur - 1]) 2031 else 2032 self.focusHeader(root_focus), 2033 .arrow_down, .tab => if (cur < comment_submit_field_index) 2034 root_focus.setFocus(form.children.keys()[cur + 1]), 2035 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2036 .mouse => |mouse| if (cur == comment_submit_field_index) { 2037 const submit = &child.widget.submit_button; 2038 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2039 }, 2040 else => try child.widget.input(allocator, key, root_focus), 2041 } 2042 } 2043 2044 fn removeFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2045 const form = self.removeForm() orelse return; 2046 const button_id = form.children.keys()[1]; 2047 switch (key) { 2048 .arrow_up, .back_tab => self.focusHeader(root_focus), 2049 .enter => try self.submitRemove(allocator), 2050 .mouse => |mouse| if (inp.leftClickOn(root_focus, button_id, mouse)) try self.submitRemove(allocator), 2051 else => {}, 2052 } 2053 } 2054 2055 // the multiline hunk inputs keep enter and any up/down that has a row to 2056 // move to; enter on a "use this" link is a navigation the host follows 2057 // before this runs. 2058 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2059 const form = self.threadForm() orelse return; 2060 const cid = form.getFocus().child_id orelse return; 2061 const cur = form.children.getIndex(cid) orelse return; 2062 const child = &form.children.values()[cur]; 2063 2064 if (child.widget == .text_input) { 2065 const ti = &child.widget.text_input; 2066 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2067 return child.widget.input(allocator, key, root_focus); 2068 } 2069 2070 const on_submit = child.widget == .submit_button; 2071 2072 switch (key) { 2073 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2074 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2075 .arrow_left, .arrow_right => switch (child.widget) { 2076 .box => |*row| { 2077 const row_cid = row.getFocus().child_id orelse return; 2078 const row_index = row.children.getIndex(row_cid) orelse return; 2079 if (key == .arrow_left) { 2080 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2081 } else if (row_index + 1 < row.children.count()) { 2082 root_focus.setFocus(row.children.keys()[row_index + 1]); 2083 } 2084 }, 2085 else => try child.widget.input(allocator, key, root_focus), 2086 }, 2087 .enter => if (on_submit) try self.submitResolution(allocator), 2088 .mouse => |mouse| if (on_submit) { 2089 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2090 }, 2091 else => try child.widget.input(allocator, key, root_focus), 2092 } 2093 } 2094 2095 // a multiline input keeps enter and any up/down that has a row to move to 2096 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2097 return switch (key) { 2098 .enter => true, 2099 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2100 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2101 else => false, 2102 }; 2103 } 2104 2105 // the neighboring focusable resolve-form child, skipping the spacer and 2106 // the blank gap rows 2107 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2108 var i = cur; 2109 while (true) { 2110 if (down) { 2111 i += 1; 2112 if (i >= form.children.count()) return null; 2113 } else { 2114 if (i == 0) return null; 2115 i -= 1; 2116 } 2117 switch (form.children.values()[i].widget) { 2118 .spacer, .text => continue, 2119 else => return i, 2120 } 2121 } 2122 } 2123 2124 // focus the resolve-form child at `i` (a version row focuses its selected 2125 // child) and keep it visible on the terminal 2126 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2127 const child = &form.children.values()[i]; 2128 switch (child.widget) { 2129 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2130 else => root_focus.setFocus(form.children.keys()[i]), 2131 } 2132 if (self.session.is_terminal) { 2133 if (self.resolveScroll()) |sc| { 2134 if (child.rect) |rect| sc.scrollToRect(rect); 2135 } 2136 } 2137 } 2138 2139 // re-emit the conflicted thread's event with the resolved content; any 2140 // event on the thread settles its conflict entry. this is the terminal 2141 // path; the web posts the form to the resolve route. 2142 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2143 if (comptime wasm) return; 2144 const io = self.session.io orelse return; 2145 const src = self.data.repo_source orelse return; 2146 const entry = self.data.selectedThread() orelse return; 2147 const author = (try self.session.eventAuthor()) orelse return; 2148 const form = self.threadForm() orelse return; 2149 2150 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2151 var title: ?[]u8 = null; 2152 var tags: ?[]u8 = null; 2153 var hunks: std.ArrayList([]const u8) = .empty; 2154 defer { 2155 if (title) |t| allocator.free(t); 2156 if (tags) |t| allocator.free(t); 2157 for (hunks.items) |h| allocator.free(h); 2158 hunks.deinit(allocator); 2159 } 2160 for (form.children.values()) |*child| switch (child.widget) { 2161 .text_input => |*ti| { 2162 const text = try ti.text(allocator); 2163 errdefer allocator.free(text); 2164 if (std.mem.eql(u8, ti.options.name, "title")) { 2165 title = text; 2166 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2167 tags = text; 2168 } else { 2169 try hunks.append(allocator, text); 2170 } 2171 }, 2172 else => {}, 2173 }; 2174 2175 const id_bytes = try evt.parseEventId(entry.id); 2176 2177 switch (src.repo_kind) { 2178 inline else => |repo_kind| { 2179 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2180 defer any_repo.deinit(io, allocator); 2181 switch (any_repo) { 2182 inline else => |*repo| { 2183 var resolution: Event.Resolve = .{ 2184 .title = title, 2185 .tags = tags, 2186 .hunks = hunks.items, 2187 }; 2188 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2189 const change = Event.Update{ .resolve = resolution }; 2190 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2191 // leave the form up for correction 2192 error.InvalidFields => return, 2193 else => |e| return e, 2194 }; 2195 }, 2196 } 2197 }, 2198 } 2199 2200 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2201 try self.session.navigate(route); 2202 } 2203 2204 // commit a new or edited comment and navigate to its permalink. this is the 2205 // terminal path; the web posts the form to its comment route. 2206 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2207 if (comptime wasm) return; 2208 const io = self.session.io orelse return; 2209 const src = self.data.repo_source orelse return; 2210 const author = (try self.session.eventAuthor()) orelse return; 2211 const form = self.threadForm() orelse return; 2212 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2213 const body = try body_input.text(allocator); 2214 defer allocator.free(body); 2215 if (!evt.Comment.fieldsValid(body)) return; 2216 2217 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2218 @memcpy(&thread_id, self.data.selected_id); 2219 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2220 var parent: [evt.event_id_size * 2]u8 = undefined; 2221 @memcpy(&parent, self.data.comment_id); 2222 break :blk parent; 2223 }; 2224 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2225 const editing = self.data.view == .edit_comment; 2226 switch (src.repo_kind) { 2227 inline else => |repo_kind| { 2228 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2229 defer any_repo.deinit(io, allocator); 2230 switch (any_repo) { 2231 inline else => |*repo| if (editing) { 2232 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2233 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2234 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2235 } else { 2236 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2237 }, 2238 } 2239 }, 2240 } 2241 2242 body_input.clear(allocator); 2243 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2244 try self.session.navigate(route); 2245 } 2246 2247 // the terminal submit paths: the new form commits a new thread event, the 2248 // edit form re-emits the selected thread's event with the form's content. 2249 // the web posts the forms to their routes instead. 2250 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2251 if (self.data.view == .edit) { 2252 try self.submitEditedThread(allocator); 2253 } else { 2254 try self.submitNewThread(allocator); 2255 } 2256 } 2257 2258 // commit the new thread to the repo's events branch and navigate to it. 2259 // this is the terminal path; the web posts the form to the thread route, 2260 // so the wasm side never runs (or compiles) the repo access below. 2261 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2262 if (comptime wasm) return; 2263 const io = self.session.io orelse return; 2264 const src = self.data.repo_source orelse return; 2265 const author = (try self.session.eventAuthor()) orelse return; 2266 2267 const form = self.threadForm() orelse return; 2268 const title_input = &form.children.values()[title_field_index].widget.text_input; 2269 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2270 const description_input = &form.children.values()[description_field_index].widget.text_input; 2271 2272 const title = try title_input.text(allocator); 2273 defer allocator.free(title); 2274 const tags = try tags_input.text(allocator); 2275 defer allocator.free(tags); 2276 const description = try description_input.text(allocator); 2277 defer allocator.free(description); 2278 2279 if (!Event.fieldsValid(title, tags)) return; 2280 2281 if (comptime supports_drafts) { 2282 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2283 title_input.clear(allocator); 2284 tags_input.clear(allocator); 2285 description_input.clear(allocator); 2286 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2287 try self.session.navigate(route); 2288 return; 2289 } 2290 2291 var id_bytes: [evt.event_id_size]u8 = undefined; 2292 io.random(&id_bytes); 2293 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2294 2295 const event = evt.EventWithId{ 2296 .id = event_id_hex, 2297 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2298 .author = author, 2299 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2300 .title = title, 2301 .description = description, 2302 .tags = tags, 2303 }), 2304 }; 2305 2306 switch (src.repo_kind) { 2307 inline else => |repo_kind| { 2308 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2309 defer any_repo.deinit(io, allocator); 2310 switch (any_repo) { 2311 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2312 } 2313 }, 2314 } 2315 2316 // wipe the form so a return visit starts fresh 2317 title_input.clear(allocator); 2318 tags_input.clear(allocator); 2319 description_input.clear(allocator); 2320 2321 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2322 try self.session.navigate(route); 2323 } 2324 2325 // re-emit the selected thread's event with the form's content (status 2326 // preserved), then reload the thread's page. this is the terminal path; 2327 // the web posts the form to the edit route. 2328 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2329 if (comptime wasm) return; 2330 const io = self.session.io orelse return; 2331 const entry = self.data.selectedThread() orelse return; 2332 2333 const form = self.threadForm() orelse return; 2334 const title_input = &form.children.values()[title_field_index].widget.text_input; 2335 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2336 const description_input = &form.children.values()[description_field_index].widget.text_input; 2337 2338 const title = try title_input.text(allocator); 2339 defer allocator.free(title); 2340 const tags = try tags_input.text(allocator); 2341 defer allocator.free(tags); 2342 const description = try description_input.text(allocator); 2343 defer allocator.free(description); 2344 2345 if (!Event.fieldsValid(title, tags)) return; 2346 2347 if (comptime supports_drafts) { 2348 if (entryDraft(entry.*)) { 2349 try self.data.editDraft(self.session, allocator, entry.id, title, tags, description); 2350 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2351 try self.session.navigate(route); 2352 return; 2353 } 2354 } 2355 2356 const id_bytes = try evt.parseEventId(entry.id); 2357 const src = self.data.repo_source orelse return; 2358 const author = (try self.session.eventAuthor()) orelse return; 2359 2360 switch (src.repo_kind) { 2361 inline else => |repo_kind| { 2362 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2363 defer any_repo.deinit(io, allocator); 2364 switch (any_repo) { 2365 inline else => |*repo| if (has_status) 2366 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2367 .title = title, 2368 .tags = tags, 2369 .description = description, 2370 } }, author) 2371 else 2372 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2373 } 2374 }, 2375 } 2376 2377 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2378 try self.session.navigate(route); 2379 } 2380 2381 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2382 const comment = self.data.comment_id.len != 0; 2383 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2384 const id = evt.parseEventId(event_id) catch return; 2385 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2386 } 2387 2388 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2389 if (comptime wasm) return; 2390 2391 if (comptime supports_drafts) { 2392 const selected = self.data.selectedThread() orelse return; 2393 if (event_kind == kind and entryDraft(selected.*)) { 2394 try Data.removeDraft(self.session, allocator, &id); 2395 const route = draftsRoute(self.data.identity) orelse return; 2396 try self.session.navigate(route); 2397 return; 2398 } 2399 } 2400 2401 const io = self.session.io orelse return; 2402 const src = self.data.repo_source orelse return; 2403 const author = (try self.session.eventAuthor()) orelse return; 2404 2405 switch (src.repo_kind) { 2406 inline else => |repo_kind| { 2407 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2408 defer any_repo.deinit(io, allocator); 2409 switch (any_repo) { 2410 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2411 } 2412 }, 2413 } 2414 2415 const id_hex = std.fmt.bytesToHex(id, .lower); 2416 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2417 try self.session.navigate(route); 2418 } 2419 2420 // flip the shown thread's status by re-emitting its event, then reload the 2421 // page rooted at the thread so the view reflects the change. this is the 2422 // terminal path; the web posts the button's form to the status route. 2423 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2424 if (comptime !has_status) return; 2425 if (comptime wasm) return; 2426 const io = self.session.io orelse return; 2427 const src = self.data.repo_source orelse return; 2428 const sel = self.detailed_index[index] orelse return; 2429 const entry = self.window(index).items[sel]; 2430 const author = (try self.session.eventAuthor()) orelse return; 2431 2432 const status = (statusChange(entryStatus(entry)) orelse return).status; 2433 2434 const id_bytes = try evt.parseEventId(entry.id); 2435 2436 switch (src.repo_kind) { 2437 inline else => |repo_kind| { 2438 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2439 defer any_repo.deinit(io, allocator); 2440 switch (any_repo) { 2441 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2442 } 2443 }, 2444 } 2445 2446 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2447 try self.session.navigate(route); 2448 } 2449 2450 // arrow keys move the tag selection; at the flow's edges focus crosses to 2451 // the neighboring widgets. 2452 fn tagsInput(self: *This, index: usize, child_index: usize, tf: *TagFlow, key: Key, root_focus: *Focus) void { 2453 const cid = tf.focus.child_id orelse return; 2454 const cur = tf.indexOfFocusId(cid) orelse return; 2455 const count = tf.text_boxes.items.len; 2456 switch (key) { 2457 .arrow_left => if (cur > 0) self.focusTag(index, child_index, tf, root_focus, cur - 1) else self.focusList(index, root_focus), 2458 .arrow_right => if (cur + 1 < count) self.focusTag(index, child_index, tf, root_focus, cur + 1), 2459 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2460 self.focusTag(index, child_index, tf, root_focus, i); 2461 } else { 2462 _ = self.moveDetailVertical(index, root_focus, false); 2463 }, 2464 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2465 self.focusTag(index, child_index, tf, root_focus, i); 2466 } else { 2467 _ = self.moveDetailVertical(index, root_focus, true); 2468 }, 2469 .home => self.focusTag(index, child_index, tf, root_focus, 0), 2470 .end => self.focusTag(index, child_index, tf, root_focus, count - 1), 2471 else => {}, 2472 } 2473 } 2474 2475 fn toolRowFocused(self: *This, index: usize, root_focus: *Focus) bool { 2476 return self.focusedDetailChild(index, root_focus) == tool_row_index; 2477 } 2478 2479 fn focusTag(self: *This, index: usize, child_index: usize, tf: *TagFlow, root_focus: *Focus, item: usize) void { 2480 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2481 // keep the tag visible on the terminal: its rect offset by the flow's 2482 // position in the pane. 2483 if (self.session.is_terminal and item < tf.rects.items.len) { 2484 if (self.detailInner(index).children.values()[child_index].rect) |flow_rect| { 2485 var rect = tf.rects.items[item]; 2486 rect.x += flow_rect.x; 2487 rect.y += flow_rect.y; 2488 self.detailScroll(index).scrollToRect(rect); 2489 } 2490 } 2491 } 2492 2493 // return to the tool row's last-focused button (the header when the 2494 // description page shows no row). 2495 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2496 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2497 root_focus.setFocus(cid); 2498 if (self.detailInner(index).children.values()[tool_row_index].rect) |rect| 2499 self.detailScroll(index).scrollToRect(rect); 2500 } 2501 2502 // enter the detail at its first focusable widget below the tool row. 2503 pub fn focusDetail(self: *This, root_focus: *Focus) bool { 2504 const index = self.selectedSplitIndex() orelse return false; 2505 for (tool_row_index + 1..self.detailInner(index).children.count()) |child_index| { 2506 if (!self.detailChildFocusable(index, child_index)) continue; 2507 self.focusDetailChild(index, child_index, false, root_focus); 2508 return true; 2509 } 2510 return false; 2511 } 2512 2513 // return to the list. 2514 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2515 if (self.detail_only) return; 2516 root_focus.setFocus(self.listScroll(index).getFocus().id); 2517 } 2518 2519 // cross to the header tabs above the stack. 2520 fn focusHeader(self: *This, root_focus: *Focus) void { 2521 if (self.detail_only) return; 2522 root_focus.setFocus(self.box.children.keys()[header_index]); 2523 } 2524 2525 pub fn clearGrid(self: *This) void { 2526 self.box.clearGrid(); 2527 } 2528 2529 pub fn getGrid(self: This) ?Grid { 2530 return self.box.getGrid(); 2531 } 2532 2533 pub fn getFocus(self: *This) *Focus { 2534 return self.box.getFocus(); 2535 } 2536 2537 pub fn atTop(self: *This, root_focus: *Focus) bool { 2538 if (!self.detail_only) return self.getSelectedIndex() == 0; 2539 const index = self.selectedSplitIndex() orelse return false; 2540 const focused = self.focusedDetailChild(index, root_focus) orelse return false; 2541 for (0..self.detailInner(index).children.count()) |child_index| { 2542 if (!self.detailChildFocusable(index, child_index)) continue; 2543 return focused == child_index; 2544 } 2545 return false; 2546 } 2547 2548 // for the parent's "scroll up at the top jumps to the header" check: at the 2549 // top only while the header tabs hold focus, so up from the split 2550 // crosses into the tabs first. 2551 pub fn getSelectedIndex(self: *This) ?usize { 2552 return if (self.headerActive()) 0 else 1; 2553 } 2554 }; 2555 } 2556