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