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