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