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