╭ 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 notify = @import("../../notify.zig"); 6 const ui = @import("../../ui.zig"); 7 const widget = @import("../widget.zig"); 8 const xit = @import("xit"); 9 const rp = xit.repo; 10 const hash = xit.hash; 11 const xitui = xit.xitui; 12 const wgt = xitui.widget; 13 const layout = xitui.layout; 14 const Key = xitui.input.Key; 15 const Grid = xitui.grid.Grid; 16 const Focus = xitui.focus.Focus; 17 const inp = @import("../input.zig"); 18 const srch_thrd = @import("../../search_thread.zig"); 19 const srch = @import("../../search.zig"); 20 const diff3 = @import("../../diff3.zig"); 21 const Comment = @import("../Repo/Comment.zig"); 22 const Attachment = @import("../Repo/Attachment.zig"); 23 const Markdown = @import("../Repo/Markdown.zig"); 24 25 const Widget = widget.Widget; 26 const WordFlow = widget.WordFlow; 27 const Center = widget.Center; 28 const SubmitButton = widget.SubmitButton; 29 const Spacer = widget.Spacer; 30 const SectionLabel = widget.SectionLabel; 31 const moveRowFocus = widget.moveRowFocus; 32 const wasm = builtin.target.cpu.arch == .wasm32; 33 34 fn mergeBottomLabel(status: pch.Mergeability.Status) []const u8 { 35 return switch (status) { 36 .clean => "", 37 .conflict => " conflict ", 38 .unknown => " unavailable ", 39 }; 40 } 41 42 // read the selected thread's conflicted fields and attribute each side to its 43 // event commit author 44 pub fn readConflict( 45 comptime Data: type, 46 comptime repo_kind: rp.RepoKind, 47 comptime repo_opts: rp.RepoOpts(repo_kind), 48 arena: *std.heap.ArenaAllocator, 49 repo: *rp.Repo(repo_kind, repo_opts), 50 io: std.Io, 51 admin_moment: ?evt.AdminDB.HashMap(.read_only), 52 haxy_moment: evt.EventDB(repo_opts.hash).HashMap(.read_only), 53 id_bytes: *const [evt.event_id_size]u8, 54 ours: Data.Event.Record, 55 conflict_entry: evt.EventDB(repo_opts.hash).HashMap(.read_only), 56 ) !Data.Conflict { 57 const Event = Data.Event; 58 const DB = evt.EventDB(repo_opts.hash); 59 const aa = arena.allocator(); 60 61 const fields_cursor = (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key))) orelse return error.NotFound; 62 const conflicted_fields = try fields_cursor.readBytesAlloc(aa, null); 63 const their_cursor = (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key))) orelse return error.NotFound; 64 const theirs = try evt.read(Event.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(their_cursor)); 65 66 // absent when both sides created the thread independently 67 var base: ?Event.Record = null; 68 if (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.base_record_key))) |base_cursor| { 69 base = try evt.read(Event.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(base_cursor)); 70 } 71 72 var their_field_oids: ?DB.SortedMap(.read_only) = null; 73 if (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_field_to_oid_key))) |cursor| 74 their_field_oids = try DB.SortedMap(.read_only).init(cursor); 75 var our_field_oids: ?DB.SortedMap(.read_only) = null; 76 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, Event.id_to_field_to_oid_key))) |map_cursor| { 77 const id_to_field_to_oid = try DB.HashMap(.read_only).init(map_cursor); 78 if (try id_to_field_to_oid.getCursor(hash.hashInt(repo_opts.hash, id_bytes))) |cursor| 79 our_field_oids = try DB.SortedMap(.read_only).init(cursor); 80 } 81 82 var conflict = Data.Conflict{}; 83 var field_iter = std.mem.splitScalar(u8, conflicted_fields, ' '); 84 while (field_iter.next()) |field| { 85 const our_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, our_field_oids, field)); 86 const their_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, their_field_oids, field)); 87 if (std.mem.eql(u8, field, "title")) { 88 conflict.title = .{ 89 .ours = .{ .text = ours.event.title, .author = our_author }, 90 .theirs = .{ .text = theirs.event.title, .author = their_author }, 91 }; 92 } else if (std.mem.eql(u8, field, "labels")) { 93 conflict.labels = .{ 94 .ours = .{ .text = ours.event.labels, .author = our_author }, 95 .theirs = .{ .text = theirs.event.labels, .author = their_author }, 96 }; 97 } else if (std.mem.eql(u8, field, "description")) { 98 conflict.description = .{ 99 .chunks = try diff3.chunks(arena.child_allocator, arena, if (base) |b| b.event.description else "", ours.event.description, theirs.event.description), 100 .ours_author = our_author, 101 .theirs_author = their_author, 102 }; 103 } else if (comptime @hasField(Data.Conflict, "status")) { 104 if (std.mem.eql(u8, field, "status")) { 105 conflict.status = .{ 106 .ours = .{ .text = @tagName(Data.statusKind(ours.event)), .author = our_author }, 107 .theirs = .{ .text = @tagName(Data.statusKind(theirs.event)), .author = their_author }, 108 }; 109 } else if (std.mem.eql(u8, field, "revision")) { 110 conflict.revision = .{ 111 .ours = .{ .text = try revisionSummary(aa, ours.event.revision), .author = our_author }, 112 .theirs = .{ .text = try revisionSummary(aa, theirs.event.revision), .author = their_author }, 113 }; 114 } else if (std.mem.eql(u8, field, "target_branch")) { 115 conflict.target_branch = .{ 116 .ours = .{ .text = ours.event.target_branch, .author = our_author }, 117 .theirs = .{ .text = theirs.event.target_branch, .author = their_author }, 118 }; 119 } 120 } 121 } 122 return conflict; 123 } 124 125 fn revisionSummary(allocator: std.mem.Allocator, revision_maybe: ?evt.Patch.Revision) ![]const u8 { 126 const revision = revision_maybe orelse return "(none)"; 127 return allocator.print("squash {s}\nsource {s}", .{ revision.squash_oid, revision.source_oid }); 128 } 129 130 fn readFieldOid( 131 comptime hash_kind: hash.HashKind, 132 field_oids_maybe: ?evt.EventDB(hash_kind).SortedMap(.read_only), 133 field: []const u8, 134 ) !?[hash.byteLen(hash_kind)]u8 { 135 const field_oids = field_oids_maybe orelse return null; 136 const cursor = (try field_oids.getCursor(field)) orelse return null; 137 var oid: [hash.byteLen(hash_kind)]u8 = undefined; 138 _ = try cursor.readBytes(&oid); 139 return oid; 140 } 141 142 fn oidAuthor( 143 comptime repo_kind: rp.RepoKind, 144 comptime repo_opts: rp.RepoOpts(repo_kind), 145 arena: *std.heap.ArenaAllocator, 146 repo: *rp.Repo(repo_kind, repo_opts), 147 io: std.Io, 148 admin_moment: ?evt.AdminDB.HashMap(.read_only), 149 oid_maybe: ?[hash.byteLen(repo_opts.hash)]u8, 150 ) !ui.Author { 151 const oid = oid_maybe orelse return .unknown; 152 const gpa = arena.child_allocator; 153 var start_oids = [_][hash.hexLen(repo_opts.hash)]u8{std.fmt.bytesToHex(oid, .lower)}; 154 var commit_iter = repo.log(io, gpa, .{ .start_oids = start_oids[0..1] }) catch return .unknown; 155 defer commit_iter.deinit(); 156 const commit_object = (commit_iter.next(gpa) catch return .unknown) orelse return .unknown; 157 defer commit_object.deinit(); 158 return try ui.Author.init(admin_moment, arena, commit_object.content.commit.metadata.author orelse ""); 159 } 160 161 fn statusSet( 162 comptime Data: type, 163 comptime DB: type, 164 statuses: DB.SortedMap(.read_only), 165 status: Data.Status, 166 ) !?DB.SortedSet(.read_only) { 167 const cursor = (try statuses.getCursor(@tagName(status))) orelse return null; 168 return try DB.SortedSet(.read_only).init(cursor); 169 } 170 171 fn labelStatusSet( 172 comptime Data: type, 173 comptime DB: type, 174 label_statuses: DB.SortedMap(.read_only), 175 label: []const u8, 176 status: Data.Status, 177 ) !?DB.SortedSet(.read_only) { 178 var key_buffer: Data.Event.LabelStatusKey = undefined; 179 const key = Data.Event.labelStatusKey(&key_buffer, label, status) catch return null; 180 const cursor = (try label_statuses.getCursor(key)) orelse return null; 181 return try DB.SortedSet(.read_only).init(cursor); 182 } 183 184 // the sets `status`'s listing intersects: each label's set for the status (each 185 // within the status set, which joins only without labels), then `extra`. none 186 // when one is missing, which lists nothing. 187 pub fn statusSets( 188 comptime Data: type, 189 comptime hash_kind: hash.HashKind, 190 aa: std.mem.Allocator, 191 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 192 labels: []const []const u8, 193 // null when one of them is missing 194 extra: ?[]const evt.EventDB(hash_kind).SortedSet(.read_only), 195 status: Data.Status, 196 ) ![]const evt.EventDB(hash_kind).SortedSet(.read_only) { 197 const DB = evt.EventDB(hash_kind); 198 var sets: std.ArrayList(DB.SortedSet(.read_only)) = .empty; 199 if (labels.len == 0) { 200 const cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, Data.Event.status_to_id_set_key)) orelse return &.{}; 201 try sets.append(aa, (try statusSet(Data, DB, try DB.SortedMap(.read_only).init(cursor), status)) orelse return &.{}); 202 } else { 203 const cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, Data.Event.label_status_to_id_set_key)) orelse return &.{}; 204 const label_statuses = try DB.SortedMap(.read_only).init(cursor); 205 for (labels) |label| try sets.append(aa, (try labelStatusSet(Data, DB, label_statuses, label, status)) orelse return &.{}); 206 } 207 try sets.appendSlice(aa, extra orelse return &.{}); 208 return sets.items; 209 } 210 211 // whether `order_key` is in every one of `sets`, and there is at least one 212 pub fn inEverySet(comptime DB: type, sets: []const DB.SortedSet(.read_only), order_key: []const u8) !bool { 213 if (sets.len == 0) return false; 214 for (sets) |set| { 215 if (!try set.contains(order_key)) return false; 216 } 217 return true; 218 } 219 220 pub fn loadLabels( 221 comptime Data: type, 222 comptime hash_kind: hash.HashKind, 223 arena: *std.heap.ArenaAllocator, 224 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 225 ) ![]const []const u8 { 226 const aa = arena.allocator(); 227 const DB = evt.EventDB(hash_kind); 228 const cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, Data.Event.label_status_to_id_set_key)) orelse return &.{}; 229 const label_statuses = try DB.SortedMap(.read_only).init(cursor); 230 var labels: std.ArrayList([]const u8) = .empty; 231 var iter = try label_statuses.iterator(); 232 while (try iter.next()) |pair_cursor| { 233 if (labels.items.len == Data.max_labels) break; 234 var entry_cursor = pair_cursor; 235 const pair = try entry_cursor.readKeyValuePair(); 236 const key = try pair.key_cursor.readBytesAlloc(aa, null); 237 const space = std.mem.indexOfScalar(u8, key, ' ') orelse continue; 238 const label = key[0..space]; 239 if (labels.last()) |last| { 240 if (std.mem.eql(u8, last, label)) continue; 241 } 242 try labels.append(aa, label); 243 } 244 return labels.items; 245 } 246 247 // the key a list set (and the search index) stores a thread under 248 const OrderKey = [@sizeOf(u64) + evt.event_id_size]u8; 249 250 // a search result's bytes as an order key 251 fn resultKey(key: []const u8) !OrderKey { 252 if (key.len != @sizeOf(u64) + evt.event_id_size) return error.InvalidSearchIndex; 253 var order_key: OrderKey = undefined; 254 @memcpy(&order_key, key); 255 return order_key; 256 } 257 258 // the hex event id an order key ends with 259 fn keyIdHex(order_key: *const OrderKey) [evt.event_id_size * 2]u8 { 260 return std.fmt.bytesToHex(order_key[@sizeOf(u64)..].*, .lower); 261 } 262 263 pub fn loadWindow( 264 comptime Data: type, 265 comptime kind: evt.EventKind, 266 comptime hash_kind: hash.HashKind, 267 arena: *std.heap.ArenaAllocator, 268 admin_moment: ?evt.AdminDB.HashMap(.read_only), 269 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 270 records: evt.EventDB(hash_kind).HashMap(.read_only), 271 // the sets the listing intersects, all keyed alike; none lists nothing 272 sets: []const evt.EventDB(hash_kind).SortedSet(.read_only), 273 root_key: ?[]const u8, 274 conflict_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 275 selected_id: []const u8, 276 comments_start: usize, 277 // the words the listing is narrowed to, "" for none 278 words: []const u8, 279 viewer: ?ui.Viewer, 280 ) !Data.Window { 281 if (sets.len == 0) return .empty; 282 if (sets.len > 1 or words.len != 0) return loadSearchWindow(Data, kind, hash_kind, arena, admin_moment, haxy_moment, records, sets, root_key, conflict_set, selected_id, comments_start, words, viewer); 283 const set = sets[0]; 284 const aa = arena.allocator(); 285 286 var prev_id: ?[]const u8 = null; 287 var iter = if (root_key) |key| blk: { 288 const rank = try set.rank(key); 289 if (rank > 0 and rank <= Data.page_size) { 290 prev_id = ""; 291 } else if (rank > Data.page_size) { 292 const pair = try set.getIndexKeyValuePair(@intCast(rank - Data.page_size)) orelse return error.NotFound; 293 var previous: OrderKey = undefined; 294 _ = try pair.key_cursor.readBytes(&previous); 295 prev_id = try aa.dupe(u8, &keyIdHex(&previous)); 296 } 297 break :blk try set.iteratorFrom(key); 298 } else try set.iteratorFromIndex(0); 299 300 var items: std.ArrayList(Data.Entry) = .empty; 301 var next_id: ?[]const u8 = null; 302 while (try iter.next()) |cursor_value| { 303 var cursor = cursor_value; 304 const pair = try cursor.readKeyValuePair(); 305 var order_key: OrderKey = undefined; 306 _ = try pair.key_cursor.readBytes(&order_key); 307 if (items.items.len == Data.page_size) { 308 next_id = try aa.dupe(u8, &keyIdHex(&order_key)); 309 break; 310 } 311 const entry = (try loadEntry(Data, hash_kind, arena, admin_moment, haxy_moment, records, &order_key, conflict_set, selected_id, comments_start, viewer)) orelse continue; 312 try items.append(aa, entry); 313 } 314 315 return .{ 316 .items = items.items, 317 .prev_id = prev_id, 318 .next_id = next_id, 319 .count = @intCast(try set.count()), 320 }; 321 } 322 323 // how many matches one search counts before the count is marked as capped 324 const max_search_results = 1000; 325 326 // the intersection of `sets` narrowed to `words`, windowed like `loadWindow`. 327 // one pass yields the window, its neighbours' roots and the match count, which 328 // stops at the cap. 329 fn loadSearchWindow( 330 comptime Data: type, 331 comptime kind: evt.EventKind, 332 comptime hash_kind: hash.HashKind, 333 arena: *std.heap.ArenaAllocator, 334 admin_moment: ?evt.AdminDB.HashMap(.read_only), 335 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 336 records: evt.EventDB(hash_kind).HashMap(.read_only), 337 sets: []const evt.EventDB(hash_kind).SortedSet(.read_only), 338 root_key: ?[]const u8, 339 conflict_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 340 selected_id: []const u8, 341 comments_start: usize, 342 words: []const u8, 343 viewer: ?ui.Viewer, 344 ) !Data.Window { 345 const DB = evt.EventDB(hash_kind); 346 const aa = arena.allocator(); 347 const root = root_key orelse ""; 348 349 // the matching list keys, in list order. the cap bounds the count, not the 350 // paging: past it a pass goes on only until the window and its "next" link 351 // are known. 352 var keys: std.ArrayList(OrderKey) = .empty; 353 if (comptime kind == .discuss) { 354 // discussions list by activity but are indexed by creation, so the 355 // word matches are gathered by id and the listed sets' intersection is 356 // walked in its order until every one has been found 357 // words too short to index narrow nothing, as on the other tabs 358 var matches: ?std.AutoHashMapUnmanaged([evt.event_id_size]u8, void) = null; 359 if (words.len != 0) { 360 var results = try srch_thrd.query(DB, hash_kind, kind, haxy_moment, aa, words); 361 if (results.terms.items.len != 0) { 362 var found: std.AutoHashMapUnmanaged([evt.event_id_size]u8, void) = .empty; 363 while (try results.next()) |key| try found.put(aa, (try resultKey(key))[@sizeOf(u64)..].*, {}); 364 matches = found; 365 } 366 } 367 368 var listed: srch.Query(DB) = .{ .terms = .empty }; 369 for (sets) |set| try listed.require(aa, set); 370 while (true) { 371 if (matches) |found| if (found.count() == 0) break; 372 const order_key = try resultKey((try listed.next()) orelse break); 373 if (matches) |*found| if (!found.remove(order_key[@sizeOf(u64)..].*)) continue; 374 try keys.append(aa, order_key); 375 if (windowKnown(Data, keys.items, root)) break; 376 } 377 } else { 378 // issues and patches list in the index's own order, so the listed sets 379 // narrow the query and the results stream out already sorted 380 var results: srch.Query(DB) = if (words.len == 0) .{ .terms = .empty } else try srch_thrd.query(DB, hash_kind, kind, haxy_moment, aa, words); 381 for (sets) |set| try results.require(aa, set); 382 while (try results.next()) |key| { 383 try keys.append(aa, try resultKey(key)); 384 if (windowKnown(Data, keys.items, root)) break; 385 } 386 } 387 388 // the window starts at the first key at or after the root 389 var first: usize = 0; 390 while (first < keys.items.len and std.mem.order(u8, &keys.items[first], root) == .lt) first += 1; 391 392 var items: std.ArrayList(Data.Entry) = .empty; 393 const last = @min(first + Data.page_size, keys.items.len); 394 for (keys.items[first..last]) |*order_key| { 395 const entry = (try loadEntry(Data, hash_kind, arena, admin_moment, haxy_moment, records, order_key, conflict_set, selected_id, comments_start, viewer)) orelse continue; 396 try items.append(aa, entry); 397 } 398 399 return .{ 400 .items = items.items, 401 // the page a window back, which is the first one when it holds the root 402 .prev_id = if (first == 0) null else if (first <= Data.page_size) "" else try aa.dupe(u8, &keyIdHex(&keys.items[first - Data.page_size])), 403 .next_id = if (last < keys.items.len) try aa.dupe(u8, &keyIdHex(&keys.items[last])) else null, 404 .count = @min(keys.items.len, max_search_results), 405 .more = keys.items.len > max_search_results, 406 }; 407 } 408 409 // whether a search pass can stop: the count is capped, and the window rooted at 410 // `root` and the key after it have been collected 411 fn windowKnown(comptime Data: type, keys: []const OrderKey, root: []const u8) bool { 412 if (keys.len <= max_search_results) return false; 413 return std.mem.order(u8, &keys[keys.len - 1 - Data.page_size], root) != .lt; 414 } 415 416 // one list entry: the record `order_key` names, with its author, comments, 417 // attachments and assignees. null when the record is gone. 418 fn loadEntry( 419 comptime Data: type, 420 comptime hash_kind: hash.HashKind, 421 arena: *std.heap.ArenaAllocator, 422 admin_moment: ?evt.AdminDB.HashMap(.read_only), 423 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 424 records: evt.EventDB(hash_kind).HashMap(.read_only), 425 order_key: *const OrderKey, 426 conflict_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 427 selected_id: []const u8, 428 comments_start: usize, 429 viewer: ?ui.Viewer, 430 ) !?Data.Entry { 431 const DB = evt.EventDB(hash_kind); 432 const aa = arena.allocator(); 433 const id_hex = keyIdHex(order_key); 434 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, order_key[@sizeOf(u64)..])) orelse return null; 435 const record = try evt.read(Data.Event.Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 436 var entry: Data.Entry = .{ 437 .id = try aa.dupe(u8, &id_hex), 438 .record = record, 439 .author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email), 440 .comments = try Comment.loadWindow( 441 hash_kind, 442 arena, 443 admin_moment, 444 haxy_moment, 445 evt.Comment.thread_id_to_comment_id_set_key, 446 &id_hex, 447 if (std.mem.eql(u8, &id_hex, selected_id)) comments_start else 0, 448 viewer, 449 ), 450 .attachments = try Attachment.load(hash_kind, arena, haxy_moment, &id_hex), 451 .assignees = try loadAssignees(hash_kind, arena, admin_moment, haxy_moment, &id_hex), 452 .can_modify = if (viewer) |v| v.role.canModify(v.email, record.author_email) else false, 453 }; 454 if (comptime @hasField(Data.Entry, "conflicted")) { 455 entry.conflicted = if (conflict_set) |conflicts| try conflicts.contains(order_key) else false; 456 } 457 return entry; 458 } 459 460 // a thread's assignees, each named after the user holding its email when one does 461 fn loadAssignees( 462 comptime hash_kind: hash.HashKind, 463 arena: *std.heap.ArenaAllocator, 464 admin_moment: ?evt.AdminDB.HashMap(.read_only), 465 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 466 thread_id: []const u8, 467 ) ![]const ui.Assignee { 468 const emails = try evt.Assignment.load(hash_kind, arena, haxy_moment, thread_id); 469 const assignees = try arena.allocator().alloc(ui.Assignee, emails.len); 470 for (assignees, emails) |*assignee, email| { 471 const user = if (admin_moment) |moment| try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email) else null; 472 assignee.* = .{ .email = email, .name = if (user) |u| u.name else null }; 473 } 474 return assignees; 475 } 476 477 // a tab link's route, carrying the page's query when it has one 478 pub fn searchRoute(route_maybe: ?ui.RoutablePage, search: ?[]const u8) !ui.RoutablePage { 479 const route = route_maybe orelse return error.RouteTooLong; 480 const query = search orelse return route; 481 return route.withSearch(query) orelse error.RouteTooLong; 482 } 483 484 // a status tab's label: its listing's count, marked when a search capped it 485 pub fn countLabel(buffer: []u8, name: []const u8, win: anytype) ![]const u8 { 486 return std.fmt.bufPrint(buffer, "{s} ({d}{s})", .{ name, win.count, if (win.more) "+" else "" }); 487 } 488 489 // the common sub-header used by each thread page: the search box, then the 490 // tab strip 491 pub const Header = struct { 492 box: wgt.Box(Widget), 493 // focus id -> semantic stack index; some pages omit unavailable tabs 494 tab_ids: std.AutoArrayHashMapUnmanaged(usize, usize), 495 // the tab whose view the stack shows. focus lands on the search box too, 496 // so the selection is remembered rather than read off the focus chain. 497 selected_tab: ?usize = null, 498 499 // the search box's place in the box, ahead of the tabs 500 const search_index: usize = 0; 501 502 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, search: ?[]const u8, search_label: []const u8) !Header { 503 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 504 errdefer box.deinit(allocator); 505 506 { 507 var search_box = try widget.SearchBox.init(allocator, session, search_label, "search", search); 508 errdefer search_box.deinit(allocator); 509 box.getFocus().child_id = search_box.getFocus().id; 510 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = widget.SearchBox.min_size }); 511 } 512 513 return .{ .box = box, .tab_ids = .empty }; 514 } 515 516 pub fn addTab(self: *Header, allocator: std.mem.Allocator, label: []const u8, link: []const u8, view_index: usize) !void { 517 var text_box = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 518 errdefer text_box.deinit(allocator); 519 text_box.getFocus().mode = .all; 520 text_box.getFocus().kind = .{ .custom = link }; 521 try self.tab_ids.put(allocator, text_box.getFocus().id, view_index); 522 try self.box.children.put(allocator, text_box.getFocus().id, .{ 523 .widget = .{ .text_box = text_box }, 524 .rect = null, 525 .min_size = .{ .width = label.len + 2, .height = null }, 526 }); 527 } 528 529 pub fn select(self: *Header, view_index: usize) void { 530 for (self.tab_ids.values(), 0..) |index, tab| { 531 if (index == view_index) { 532 self.selected_tab = tab; 533 return; 534 } 535 } 536 } 537 538 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 539 self.box.deinit(allocator); 540 self.tab_ids.deinit(allocator); 541 } 542 543 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 544 self.clearGrid(); 545 // the search box takes focus without changing the view, so the tab it 546 // was last on stays selected 547 if (self.currentTabIndex()) |tab| self.selected_tab = tab; 548 // only the selected tab shows its border 549 const selected_id = if (self.selectedTab()) |tab| self.tab_ids.keys()[tab] else null; 550 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 551 switch (child.widget) { 552 .text_box => |*tb| widget.markSelected(tb, selected_id == id), 553 else => {}, 554 } 555 } 556 // a row taller than the tabs, leaving a blank line beneath them. the 557 // parent sets no min, which would make it lay the focused content out first. 558 var header_constraint = constraint; 559 header_constraint.min_size.height = @max(constraint.min_size.height orelse 0, 4); 560 try self.box.build(allocator, header_constraint, root_focus); 561 } 562 563 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 564 const search_box = self.searchBox(); 565 if (self.box.getFocus().child_id == search_box.getFocus().id) { 566 // tab, and right-arrow past the last character, step to the tabs 567 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 568 root_focus.setFocus(self.tab_ids.keys()[0]); 569 return; 570 } 571 return search_box.input(allocator, key, root_focus); 572 } 573 const current_tab = self.currentTabIndex() orelse return; 574 if (key == .back_tab or (key == .arrow_left and current_tab == 0)) { 575 // remembered here as well as in build, which may not run between keys 576 self.selected_tab = current_tab; 577 root_focus.setFocus(search_box.getFocus().id); 578 return; 579 } 580 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 581 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 582 } 583 } 584 585 // the typed query when enter should submit it: the box holds focus. the 586 // caller owns the returned text. 587 pub fn submittedText(self: *Header, allocator: std.mem.Allocator) !?[]u8 { 588 const search_box = self.searchBox(); 589 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 590 return try search_box.text(allocator); 591 } 592 593 fn searchBox(self: *Header) *widget.SearchBox { 594 return &self.box.children.values()[search_index].widget.search_box; 595 } 596 597 pub fn clearGrid(self: *Header) void { 598 self.box.clearGrid(); 599 } 600 601 pub fn getGrid(self: Header) ?Grid { 602 return self.box.getGrid(); 603 } 604 605 pub fn getFocus(self: *Header) *Focus { 606 return self.box.getFocus(); 607 } 608 609 pub fn getSelectedIndex(self: Header) ?usize { 610 const tab = self.selectedTab() orelse return null; 611 return self.tab_ids.values()[tab]; 612 } 613 614 // the focused tab, or the one focus last left behind 615 fn selectedTab(self: Header) ?usize { 616 return self.currentTabIndex() orelse self.selected_tab; 617 } 618 619 fn currentTabIndex(self: Header) ?usize { 620 const child_id = self.box.focus.child_id orelse return null; 621 return self.tab_ids.getIndex(child_id); 622 } 623 }; 624 625 // the selected thread's scrollable contents, shared by repo and fork views 626 // the assignee row's form action, which also identifies the row 627 const assign_suffix = "/assign"; 628 629 // an input that assigns on enter, in a form of its own 630 fn appendAssigneeRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, thread_url: []const u8) !void { 631 const pa = session.page_arena.allocator(); 632 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 633 errdefer row.deinit(allocator); 634 row.getFocus().kind = .{ .custom = try pa.print("form:{s}" ++ assign_suffix, .{thread_url}) }; 635 636 var text_input = try wgt.TextInput.init(allocator, .{ .name = "assignee", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 637 errdefer text_input.deinit(allocator); 638 text_input.getFocus().mode = .all; 639 if (session.formFeedback(.assignment)) |saved| try text_input.setContent(allocator, saved.text); 640 row.getFocus().child_id = text_input.getFocus().id; 641 try row.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 642 643 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 644 } 645 646 const unassign_suffix = "/unassign"; 647 const assignee_seg = "/assignee:"; 648 649 // one row per assignee: its name or email, linking to the issues assigned to 650 // it, and for an editor a ✕ that unassigns 651 fn appendAssignedRows( 652 allocator: std.mem.Allocator, 653 box: *wgt.Box(Widget), 654 session: *ui.Session, 655 thread_url: []const u8, 656 assignees: []const ui.Assignee, 657 links: []const []const u8, 658 can_modify: bool, 659 ) !void { 660 const pa = session.page_arena.allocator(); 661 for (assignees, links) |assignee, link| { 662 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 663 errdefer row.deinit(allocator); 664 665 var tb = try wgt.TextBox.init(allocator, assignee.display(), .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " assigned " } }); 666 errdefer tb.deinit(allocator); 667 tb.getFocus().mode = .all; 668 tb.getFocus().kind = .{ .custom = link }; 669 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 670 671 if (can_modify) { 672 const action = try pa.print("{s}" ++ assignee_seg ++ "{s}" ++ unassign_suffix, .{ thread_url, try ui.urlEncodeRef(pa, assignee.email) }); 673 // the web renders a submit button only inside a form 674 row.getFocus().kind = .{ .custom = try pa.print("form:{s}", .{action}) }; 675 var remove = try wgt.TextBox.init(allocator, "✕", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 676 errdefer remove.deinit(allocator); 677 remove.getFocus().mode = .all; 678 remove.getFocus().kind = .{ .custom = try pa.print("{s}{s}", .{ ui.submit_action_prefix, action }) }; 679 try row.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 680 } 681 682 row.getFocus().child_id = row.children.keys()[0]; 683 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 684 } 685 } 686 687 // the url-encoded email an assignee row's ✕ unassigns 688 fn unassignEmail(row: *wgt.Box(Widget)) ?[]const u8 { 689 if (row.children.count() < 2) return null; 690 const action = switch (row.children.values()[1].widget.getFocus().kind) { 691 .custom => |custom| custom, 692 else => return null, 693 }; 694 if (!std.mem.startsWith(u8, action, ui.submit_action_prefix) or !std.mem.endsWith(u8, action, unassign_suffix)) return null; 695 const at = std.mem.lastIndexOf(u8, action, assignee_seg) orelse return null; 696 return action[at + assignee_seg.len .. action.len - unassign_suffix.len]; 697 } 698 699 // whether `row` is the assignee row 700 fn isAssigneeRow(row: *wgt.Box(Widget)) bool { 701 const action = switch (row.getFocus().kind) { 702 .custom => |custom| custom, 703 else => return false, 704 }; 705 return std.mem.startsWith(u8, action, "form:") and std.mem.endsWith(u8, action, assign_suffix); 706 } 707 708 // label the row's input for this frame and register it for the web form 709 // handling 710 fn buildAssigneeRow(row: *wgt.Box(Widget), session: *ui.Session, root_focus: *Focus) !void { 711 const text_input = &row.children.values()[0].widget.text_input; 712 const local = session.data.host_kind == .local; 713 const failure = if (session.formFeedback(.assignment)) |saved| saved.failure else null; 714 text_input.options.top_label.text = if (failure) |value| switch (value) { 715 .not_found => " assign by name or email (not found) ", 716 .not_an_email => if (local) " assign by email (not an email) " else " assign by name or email (not an email) ", 717 .already_assigned => if (local) " assign by email (already assigned) " else " assign by name or email (already assigned) ", 718 } else if (local) " assign by email " else " assign by name or email "; 719 text_input.options.bottom_label.text = if (root_focus.grandchild_id == text_input.getFocus().id) " press enter " else ""; 720 try session.text_inputs.put(session.arena.allocator(), text_input.getFocus().id, text_input); 721 } 722 723 pub fn Detail(comptime kind: evt.EventKind, comptime Data: type) type { 724 const Entry = Data.Entry; 725 const Event = Data.Event; 726 const Status = Data.Status; 727 const has_status = @hasField(Event, "status"); 728 const supports_conflicts = Event.merge_policy == .field_conflicts; 729 const supports_drafts = @hasField(Data, "drafts"); 730 const supports_forks = @hasField(Entry, "fork_exists"); 731 const supports_merge = @hasDecl(Data, "mergeRoute"); 732 return struct { 733 const This = @This(); 734 const thread_name = Data.thread_name; 735 736 pub const Exit = enum { none, list, header }; 737 pub const Options = struct { actions: bool = true }; 738 739 scroll: wgt.Scroll(Widget), 740 data: *const Data, 741 session: *ui.Session, 742 entry: ?Entry, 743 title_id: ?usize, 744 description_id: ?usize, 745 author_id: ?usize, 746 actions: bool, 747 exit: Exit, 748 749 const tool_row_index: usize = 0; 750 751 pub fn init(allocator: std.mem.Allocator, data: *const Data, session: *ui.Session, entry: ?Entry, options: Options) !This { 752 var content = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 753 errdefer content.deinit(allocator); 754 var detail_scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = content }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 755 errdefer detail_scroll.deinit(allocator); 756 757 var self: This = .{ 758 .scroll = detail_scroll, 759 .data = data, 760 .session = session, 761 .entry = null, 762 .title_id = null, 763 .description_id = null, 764 .author_id = null, 765 .actions = options.actions, 766 .exit = .none, 767 }; 768 if (entry) |value| try self.setEntry(allocator, value); 769 return self; 770 } 771 772 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 773 self.scroll.deinit(allocator); 774 } 775 776 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 777 self.clearGrid(); 778 if (kind == .issue) { 779 for (self.inner().children.values()) |*child| switch (child.widget) { 780 .box => |*row| if (isAssigneeRow(row)) try buildAssigneeRow(row, self.session, root_focus), 781 else => {}, 782 }; 783 } 784 try self.scroll.build(allocator, constraint, root_focus); 785 } 786 787 pub fn input(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 788 _ = try self.inputWithExit(allocator, key, root_focus); 789 } 790 791 pub fn clearGrid(self: *This) void { 792 self.scroll.clearGrid(); 793 } 794 795 pub fn getGrid(self: This) ?Grid { 796 return self.scroll.getGrid(); 797 } 798 799 pub fn getFocus(self: *This) *Focus { 800 return self.scroll.getFocus(); 801 } 802 803 fn inner(self: *This) *wgt.Box(Widget) { 804 return &self.scroll.child.box; 805 } 806 807 fn toolRow(self: *This) *wgt.Box(Widget) { 808 return &self.inner().children.values()[tool_row_index].widget.box; 809 } 810 811 fn entryConflicted(entry: Entry) bool { 812 return if (supports_conflicts) entry.conflicted else false; 813 } 814 815 fn entryStatus(entry: Entry) Status { 816 if (comptime has_status) return Data.statusKind(entry.record.event); 817 return @fromBackingInt(@intCast(0)); 818 } 819 820 fn entryDraft(entry: Entry) bool { 821 return if (supports_drafts) entry.draft else false; 822 } 823 824 const StatusChange = struct { 825 action: []const u8, 826 status: Status, 827 }; 828 829 fn statusChange(status: Status) ?StatusChange { 830 const name = @tagName(status); 831 if (std.mem.eql(u8, name, "open")) return .{ 832 .action = "close", 833 .status = std.meta.stringToEnum(Status, "closed") orelse return null, 834 }; 835 if (std.mem.eql(u8, name, "closed")) return .{ 836 .action = "open", 837 .status = std.meta.stringToEnum(Status, "open") orelse return null, 838 }; 839 return null; 840 } 841 842 fn listRoute(identity: []const u8, status: Status, selected: []const u8) ?ui.RoutablePage { 843 return Data.listRoute(identity, status, selected); 844 } 845 846 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 847 return if (supports_drafts) Data.draftsRoute(identity) else null; 848 } 849 850 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 851 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 852 } 853 854 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 855 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 856 } 857 858 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 859 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 860 } 861 862 // a list link keeps the search the page carries 863 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, id: []const u8, search: []const u8) ![]const u8 { 864 var route = listRoute(identity, status, id) orelse return error.RouteTooLong; 865 if (search.len != 0) route = route.withSearch(search) orelse return error.RouteTooLong; 866 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 867 } 868 869 fn addToolButton(allocator: std.mem.Allocator, row: *wgt.Box(Widget), label: []const u8, bottom_label: []const u8, action: []const u8) !void { 870 var button = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = bottom_label } }); 871 errdefer button.deinit(allocator); 872 button.getFocus().mode = .all; 873 button.getFocus().kind = .{ .custom = action }; 874 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 } }); 875 } 876 877 pub fn setEntry(self: *This, allocator: std.mem.Allocator, entry: Entry) !void { 878 const inner_box = self.inner(); 879 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 880 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 881 882 for (inner_box.children.values()) |*child| child.widget.deinit(allocator); 883 inner_box.children.clearAndFree(allocator); 884 inner_box.getFocus().child_id = null; 885 self.entry = entry; 886 self.title_id = null; 887 self.author_id = null; 888 889 { 890 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 891 errdefer row.deinit(allocator); 892 try inner_box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 893 } 894 if (self.actions and !description_page and comment_page == null) { 895 const row = self.toolRow(); 896 const pa = self.session.page_arena.allocator(); 897 898 if (supports_forks and self.session.data.current_page.parent() == .fork) { 899 // the fork page's own tabs show the diff and commits 900 const route = ui.RoutablePage.repoPatchesRoute(self.data.identity, .open, entry.id) orelse return error.RouteTooLong; 901 try addToolButton(allocator, row, "view on target repo", "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 902 } else if (supports_forks) { 903 if (try Data.diffRoute(self.data.identity, entry)) |route| { 904 try addToolButton(allocator, row, "view diff", "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 905 } 906 if (try Data.commitsRoute(self.data.identity, entry)) |route| { 907 const label = if (entry.commit_count) |count| try pa.print("view commits ({d})", .{count}) else "view commits"; 908 try addToolButton(allocator, row, label, "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 909 } 910 } 911 912 { 913 var spacer = try Spacer.init(allocator); 914 errdefer spacer.deinit(allocator); 915 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 916 } 917 918 if (comptime supports_merge) { 919 if (Data.canMerge(entry, self.session)) { 920 const route = mergeRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 921 try addToolButton(allocator, row, "merge", mergeBottomLabel(entry.mergeability.status()), try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 922 } 923 } 924 925 if (supports_drafts and entryDraft(entry)) { 926 const route = publishRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 927 try addToolButton(allocator, row, "publish", "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 928 const edit_route = ui.RoutablePage.repoThreadEditRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 929 try addToolButton(allocator, row, "edit", "", try pa.print("a:{s}", .{try edit_route.toUrl(self.session.page_arena)})); 930 const remove_route = ui.RoutablePage.repoThreadRemoveRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong; 931 try addToolButton(allocator, row, "✕", "", try pa.print("a:{s}", .{try remove_route.toUrl(self.session.page_arena)})); 932 } else { 933 if (!self.session.is_terminal and !entryConflicted(entry)) { 934 const label = "add attachment"; 935 const action = if (self.data.identity.len == 0) 936 try pa.print("{s}/{s}:{s}/attach", .{ ui.file_input_prefix, @tagName(kind), entry.id }) 937 else 938 try pa.print("{s}/{s}/{s}:{s}/attach", .{ ui.file_input_prefix, self.data.identity, @tagName(kind), entry.id }); 939 try addToolButton(allocator, row, label, "", action); 940 } 941 942 const conflicted = supports_conflicts and entryConflicted(entry); 943 if (conflicted) { 944 const route = resolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 945 try addToolButton(allocator, row, "resolve conflict", "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 946 } else if (has_status) { 947 if (statusChange(entryStatus(entry))) |change| { 948 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 949 row.getFocus().kind = .{ .custom = try pa.print("form:{s}/{s}", .{ try route.toUrl(self.session.page_arena), change.action }) }; 950 try addToolButton(allocator, row, change.action, "", "submit"); 951 } 952 } 953 954 if (entry.notify_action) |mode| { 955 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 956 const action = try pa.print("{s}/{s}", .{ try route.toUrl(self.session.page_arena), @tagName(mode) }); 957 // a status change already made the row a form, which the button's own action overrides 958 if (row.getFocus().kind != .custom) row.getFocus().kind = .{ .custom = try pa.print("form:{s}", .{action}) }; 959 const label = switch (mode) { 960 .subscribe => "subscribe to", 961 .mute => "mute", 962 }; 963 try addToolButton(allocator, row, label, " notifications ", try pa.print("{s}{s}", .{ ui.submit_action_prefix, action })); 964 } 965 966 if (!conflicted) { 967 const route = ui.RoutablePage.repoThreadEditRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 968 try addToolButton(allocator, row, "edit", "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 969 } 970 971 const route = ui.RoutablePage.repoThreadRemoveRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong; 972 try addToolButton(allocator, row, "✕", "", try pa.print("a:{s}", .{try route.toUrl(self.session.page_arena)})); 973 } 974 } 975 976 if (supports_drafts and !description_page and comment_page == null) 977 try self.data.appendDetails(allocator, inner_box, self.session, entry); 978 979 if (comment_page) |comment_view| { 980 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 981 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 982 var back = try Comment.linkBox(allocator, self.session, back_label, ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 983 errdefer back.deinit(allocator); 984 try inner_box.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 985 986 try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, self.data.viewer != null, comment_view.selected); 987 try Comment.appendCount(allocator, inner_box, comment_view.replies.count, "reply", "replies"); 988 for (comment_view.replies.comments) |comment| try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, self.data.viewer != null, comment); 989 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); 990 991 var spacer = try Spacer.init(allocator); 992 errdefer spacer.deinit(allocator); 993 try inner_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 994 995 inner_box.getFocus().child_id = inner_box.children.keys()[tool_row_index + 1]; 996 self.resetScroll(); 997 return; 998 } 999 1000 if (description_page) { 1001 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 1002 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 1003 var text_box = try wgt.TextBox.init(allocator, back_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1004 errdefer text_box.deinit(allocator); 1005 text_box.getFocus().mode = .all; 1006 text_box.getFocus().kind = .{ .custom = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), entry.id, self.data.search orelse "") }; 1007 try inner_box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 1008 self.title_id = text_box.getFocus().id; 1009 } else { 1010 var title = try wgt.TextBox.init(allocator, entry.record.event.title, .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = " title " } }); 1011 errdefer title.deinit(allocator); 1012 title.getFocus().mode = .all; 1013 try inner_box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_box = title }, .rect = null, .min_size = null }); 1014 self.title_id = title.getFocus().id; 1015 1016 { 1017 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1018 errdefer row.deinit(allocator); 1019 var author = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1020 errdefer author.deinit(allocator); 1021 try row.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 1022 const timestamp = try ui.timestamp.format(self.session.page_arena.allocator(), std.math.cast(i64, entry.record.created_timestamp) orelse -1, try self.session.nowSeconds()); 1023 var time = try wgt.TextBox.init(allocator, timestamp.relative, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = timestamp.exact } }); 1024 errdefer time.deinit(allocator); 1025 time.getFocus().mode = .all; 1026 try row.children.put(allocator, time.getFocus().id, .{ .widget = .{ .text_box = time }, .rect = null, .min_size = null }); 1027 row.getFocus().child_id = author.getFocus().id; 1028 self.author_id = row.getFocus().id; 1029 try inner_box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1030 } 1031 1032 var items: std.ArrayList(WordFlow.Item) = .empty; 1033 defer items.deinit(allocator); 1034 var label_iter = Event.labelIterator(entry.record.event.labels); 1035 while (label_iter.next()) |label| { 1036 if (label.len == 0) continue; 1037 const search = try srch_thrd.withFilter(self.session.page_arena.allocator(), self.data.search orelse "", srch_thrd.label_prefix, label); 1038 try items.append(allocator, .{ .text = label, .link = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), "", search), .top_label = " label " }); 1039 } 1040 if (items.items.len > 0) { 1041 var labels = try WordFlow.init(allocator); 1042 errdefer labels.deinit(allocator); 1043 try labels.setItems(allocator, items.items); 1044 try inner_box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .word_flow = labels }, .rect = null, .min_size = null }); 1045 } 1046 } 1047 1048 if (kind == .issue) { 1049 const pa = self.session.page_arena.allocator(); 1050 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1051 const thread_url = try parent_route.toUrl(self.session.page_arena); 1052 if (entry.can_modify) try appendAssigneeRow(allocator, inner_box, self.session, thread_url); 1053 // every viewer gets each assignee as a link to the issues assigned to it 1054 const links = try pa.alloc([]const u8, entry.assignees.len); 1055 for (links, entry.assignees) |*link, assignee| { 1056 const search = try srch_thrd.withFilter(pa, self.data.search orelse "", srch_thrd.assignee_prefix, assignee.display()); 1057 link.* = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), "", search); 1058 } 1059 try appendAssignedRows(allocator, inner_box, self.session, thread_url, entry.assignees, links, entry.can_modify); 1060 } 1061 1062 const whole = entry.record.event.description; 1063 const preview_end = if (description_page) null else ui.detailPreviewEnd(whole); 1064 const shown = if (preview_end) |end| 1065 std.mem.trimEnd(u8, whole[0..end], " \t\r\n") 1066 else if (whole.len == 0) 1067 "(no description)" 1068 else 1069 whole; 1070 var description = try Markdown.Frame.init(allocator, shown, " description ", if (preview_end != null) " click or press enter to see more " else "", self.session.page_arena); 1071 errdefer description.deinit(allocator); 1072 if (preview_end != null) { 1073 const route = ui.RoutablePage.repoThreadDescriptionRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 1074 description.body().kind = .{ .custom = try self.session.page_arena.allocator().print("a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1075 } 1076 try inner_box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .markdown_frame = description }, .rect = null, .min_size = null }); 1077 self.description_id = description.getFocus().id; 1078 1079 if (!supports_drafts or !entryDraft(entry)) { 1080 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1081 const parent_url = try parent_route.toUrl(self.session.page_arena); 1082 try Attachment.appendRows(allocator, inner_box, self.session, self.data.identity, parent_url, entry.attachments); 1083 1084 if (!description_page) { 1085 if (self.data.viewer != null) { 1086 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoThreadCommentNewRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1087 errdefer reply.deinit(allocator); 1088 try inner_box.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1089 } 1090 1091 try Comment.appendCount(allocator, inner_box, entry.comments.count, "comment", "comments"); 1092 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, self.data.viewer != null, comment); 1093 try Comment.appendWindowNav(allocator, inner_box, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1094 } 1095 } 1096 1097 if (!description_page) { 1098 var spacer = try Spacer.init(allocator); 1099 errdefer spacer.deinit(allocator); 1100 try inner_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1101 } 1102 1103 _ = self.focusFirst(null); 1104 self.resetScroll(); 1105 } 1106 1107 fn resetScroll(self: *This) void { 1108 const detail_scroll = &self.scroll; 1109 detail_scroll.x = 0; 1110 detail_scroll.y = 0; 1111 detail_scroll.getFocus().version +%= 1; 1112 } 1113 1114 pub fn inputWithExit(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !Exit { 1115 self.exit = .none; 1116 const key: Key = switch (raw_key) { 1117 .scroll_up => .arrow_up, 1118 .scroll_down => .arrow_down, 1119 else => raw_key, 1120 }; 1121 1122 // a focused assignee input keeps its editing keys 1123 if (key == .home or key == .end) if (self.focusedAssigneeInput(root_focus)) |text_input| { 1124 try text_input.input(allocator, key, root_focus); 1125 return self.exit; 1126 }; 1127 1128 switch (key) { 1129 .page_up => self.pageDetail(root_focus, -10), 1130 .page_down => self.pageDetail(root_focus, 10), 1131 .home => self.jump(root_focus, false), 1132 .end => self.jump(root_focus, true), 1133 // tab steps between the links of a focused description or 1134 // comment; the web leaves it to the browser 1135 .tab, .back_tab => if (self.session.is_terminal) if (self.focusedChild(root_focus)) |index| if (self.focusedFrame(index)) |frame| { 1136 try frame.frame.stepLink(allocator, root_focus, &self.scroll, frame.x, frame.y, key == .tab); 1137 }, 1138 else => { 1139 const entry = self.entry orelse return self.exit; 1140 if (self.data.comment_page != null and std.mem.eql(u8, entry.id, self.data.selected_id)) { 1141 self.commentInput(key, root_focus); 1142 } else if (self.toolRowFocused(root_focus)) { 1143 try self.toolRowInput(allocator, key, root_focus); 1144 } else { 1145 try self.childInput(allocator, key, root_focus); 1146 } 1147 }, 1148 } 1149 return self.exit; 1150 } 1151 1152 // the assignee row's input, when it holds focus 1153 fn focusedAssigneeInput(self: *This, root_focus: *Focus) ?*wgt.TextInput { 1154 const child_index = self.focusedChild(root_focus) orelse return null; 1155 const child = &self.inner().children.values()[child_index].widget; 1156 if (child.* != .box or !isAssigneeRow(&child.box)) return null; 1157 const text_input = &child.box.children.values()[0].widget.text_input; 1158 return if (root_focus.grandchild_id == text_input.getFocus().id) text_input else null; 1159 } 1160 1161 fn childInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1162 const child_index = self.focusedChild(root_focus) orelse return; 1163 const inner_box = self.inner(); 1164 const child_id = inner_box.children.keys()[child_index]; 1165 const child = &inner_box.children.values()[child_index].widget; 1166 1167 if (child_id == self.title_id) { 1168 switch (key) { 1169 .arrow_left => self.exit = .list, 1170 .arrow_up => if (!self.moveVertical(root_focus, false)) { 1171 self.exit = .header; 1172 }, 1173 .arrow_down => _ = self.moveVertical(root_focus, true), 1174 else => {}, 1175 } 1176 return; 1177 } 1178 if (child_id == self.author_id) { 1179 switch (key) { 1180 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 1181 self.exit = .list; 1182 }, 1183 .arrow_right => _ = self.moveHorizontal(root_focus, true), 1184 .arrow_up => _ = self.moveVertical(root_focus, false), 1185 .arrow_down => _ = self.moveVertical(root_focus, true), 1186 else => {}, 1187 } 1188 return; 1189 } 1190 if (child_id == self.description_id) { 1191 self.descriptionInput(key, root_focus); 1192 return; 1193 } 1194 1195 switch (child.*) { 1196 .word_flow => |*labels| self.labelsInput(child_index, labels, key, root_focus), 1197 .copyable_text => |*copyable| switch (key) { 1198 .arrow_up => if (!self.moveVertical(root_focus, false)) { 1199 self.exit = .header; 1200 }, 1201 .arrow_down => _ = self.moveVertical(root_focus, true), 1202 .arrow_left => if (copyable.selected == 0) { 1203 self.exit = .list; 1204 } else try copyable.input(allocator, key, root_focus), 1205 else => try copyable.input(allocator, key, root_focus), 1206 }, 1207 .box => |*row| if (isAssigneeRow(row)) { 1208 try self.assigneeInput(allocator, row, key, root_focus); 1209 } else if (Attachment.removeId(row)) |attachment_id| { 1210 if (self.removableRowInput(row, key, root_focus)) try self.removeEvent(allocator, .attach, attachment_id); 1211 } else if (unassignEmail(row)) |encoded| { 1212 if (self.removableRowInput(row, key, root_focus)) try self.unassign(allocator, std.Uri.percentDecodeInPlace(try self.session.page_arena.allocator().dupe(u8, encoded))); 1213 } else self.commentInput(key, root_focus), 1214 else => self.commentInput(key, root_focus), 1215 } 1216 } 1217 1218 fn toolRowInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1219 const row = self.toolRow(); 1220 const child_id = row.getFocus().child_id orelse return; 1221 const cur = row.children.getIndex(child_id) orelse return; 1222 const action: []const u8 = switch (row.children.values()[cur].widget.getFocus().kind) { 1223 .custom => |custom| custom, 1224 else => "", 1225 }; 1226 const on_primary = std.mem.eql(u8, action, "submit"); 1227 // the notify button is the row's only one with its own action 1228 const on_notify = std.mem.startsWith(u8, action, ui.submit_action_prefix); 1229 switch (key) { 1230 .arrow_left => { 1231 var previous = cur; 1232 while (previous > 0) { 1233 previous -= 1; 1234 if (row.children.values()[previous].widget.getFocus().mode != .all) continue; 1235 root_focus.setFocus(row.children.keys()[previous]); 1236 return; 1237 } 1238 self.exit = .list; 1239 }, 1240 .arrow_right => { 1241 var next = cur + 1; 1242 while (next < row.children.count()) : (next += 1) { 1243 if (row.children.values()[next].widget.getFocus().mode != .all) continue; 1244 root_focus.setFocus(row.children.keys()[next]); 1245 return; 1246 } 1247 }, 1248 .arrow_up => self.exit = .header, 1249 .arrow_down => _ = self.moveVertical(root_focus, true), 1250 .enter => if (on_primary) { 1251 try self.primaryAction(allocator); 1252 } else if (on_notify) try self.changeNotify(allocator), 1253 .mouse => |mouse| if (inp.leftClickOn(root_focus, child_id, mouse)) { 1254 if (on_primary) { 1255 try self.primaryAction(allocator); 1256 } else if (on_notify) try self.changeNotify(allocator); 1257 }, 1258 else => {}, 1259 } 1260 } 1261 1262 fn primaryAction(self: *This, allocator: std.mem.Allocator) !void { 1263 try self.toggleStatus(allocator); 1264 } 1265 1266 fn descriptionInput(self: *This, key: Key, root_focus: *Focus) void { 1267 switch (key) { 1268 .arrow_left => self.exit = .list, 1269 .arrow_up => _ = self.moveVertical(root_focus, false), 1270 .arrow_down => _ = self.moveVertical(root_focus, true), 1271 else => {}, 1272 } 1273 } 1274 1275 fn commentInput(self: *This, key: Key, root_focus: *Focus) void { 1276 switch (key) { 1277 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 1278 self.exit = .list; 1279 }, 1280 .arrow_right => _ = self.moveHorizontal(root_focus, true), 1281 .arrow_up => _ = self.moveVertical(root_focus, false), 1282 .arrow_down => _ = self.moveVertical(root_focus, true), 1283 else => {}, 1284 } 1285 } 1286 1287 fn pageDetail(self: *This, root_focus: *Focus, delta: isize) void { 1288 if (!self.session.is_terminal) return; 1289 const detail_scroll = &self.scroll; 1290 detail_scroll.y += delta; 1291 detail_scroll.clampToContent(); 1292 self.focusVisible(root_focus, delta > 0); 1293 } 1294 1295 fn jump(self: *This, root_focus: *Focus, to_end: bool) void { 1296 if (self.session.is_terminal) { 1297 const detail_scroll = &self.scroll; 1298 detail_scroll.y = if (to_end) std.math.maxInt(isize) else 0; 1299 detail_scroll.clampToContent(); 1300 self.focusVisible(root_focus, to_end); 1301 } else { 1302 const focus = self.inner().getFocus(); 1303 var chosen: ?usize = null; 1304 for (focus.children.keys(), focus.children.values()) |id, child| { 1305 if (child.focus.mode != .all) continue; 1306 chosen = id; 1307 if (!to_end) break; 1308 } 1309 if (chosen) |id| root_focus.setFocus(id); 1310 } 1311 } 1312 1313 fn rectVisible(self: *This, rect: layout.IRect) bool { 1314 const detail_scroll = &self.scroll; 1315 const viewport = detail_scroll.grid orelse return false; 1316 const top = detail_scroll.y; 1317 const bottom = top + @as(isize, @intCast(viewport.size.height - detail_scroll.bar_h)); 1318 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1319 } 1320 1321 fn focusVisible(self: *This, root_focus: *Focus, last: bool) void { 1322 const inner_box = self.inner(); 1323 var offset: usize = 0; 1324 while (offset < inner_box.children.count()) : (offset += 1) { 1325 const child_index = if (last) inner_box.children.count() - 1 - offset else offset; 1326 if (self.focusVisibleChild(child_index, root_focus, last)) return; 1327 } 1328 } 1329 1330 fn focusVisibleChild(self: *This, child_index: usize, root_focus: *Focus, last: bool) bool { 1331 const inner_box = self.inner(); 1332 const child = &inner_box.children.values()[child_index]; 1333 const child_rect = child.rect orelse return false; 1334 var chosen: ?usize = null; 1335 if (child.widget.getFocus().mode == .all and self.rectVisible(child_rect)) { 1336 const id = inner_box.children.keys()[child_index]; 1337 chosen = id; 1338 if (!last) { 1339 root_focus.setFocus(id); 1340 return true; 1341 } 1342 } 1343 1344 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1345 if (focus_child.focus.mode != .all) continue; 1346 const rect: layout.IRect = .{ 1347 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1348 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1349 .size = focus_child.rect.size, 1350 }; 1351 if (!self.rectVisible(rect)) continue; 1352 chosen = id; 1353 if (!last) break; 1354 } 1355 if (chosen) |id| { 1356 root_focus.setFocus(id); 1357 return true; 1358 } 1359 return false; 1360 } 1361 1362 // the markdown frame focused in child `child_index`, placed in the scroll's 1363 // content 1364 fn focusedFrame(self: *This, child_index: usize) ?struct { frame: *Markdown.Frame, x: isize, y: isize } { 1365 const child = &self.inner().children.values()[child_index]; 1366 const rect = child.rect orelse return null; 1367 switch (child.widget) { 1368 .markdown_frame => |*frame| return .{ .frame = frame, .x = rect.x, .y = rect.y }, 1369 .repo_comment => |*comment| { 1370 if (!comment.bodyFocused()) return null; 1371 const row = comment.rowRect(true) orelse return null; 1372 return .{ .frame = comment.body(), .x = rect.x + row.x, .y = rect.y + row.y }; 1373 }, 1374 else => return null, 1375 } 1376 } 1377 1378 fn focusedChild(self: *This, root_focus: *Focus) ?usize { 1379 const inner_box = self.inner(); 1380 var id = root_focus.grandchild_id orelse return null; 1381 while (inner_box.getFocus().children.get(id)) |child| { 1382 if (child.parent_id == inner_box.getFocus().id) return inner_box.children.getIndex(id); 1383 id = child.parent_id; 1384 } 1385 return null; 1386 } 1387 1388 fn childFocusable(self: *This, child_index: usize) bool { 1389 const focus = self.inner().children.values()[child_index].widget.getFocus(); 1390 if (focus.mode == .all) return true; 1391 for (focus.children.values()) |item| { 1392 if (item.focus.mode == .all) return true; 1393 } 1394 return false; 1395 } 1396 1397 // focus child `child_index`, its last part when `last`, entered from above 1398 // when `down` 1399 fn focusChild(self: *This, child_index: usize, last: bool, down: bool, root_focus: *Focus) void { 1400 const inner_box = self.inner(); 1401 const child = &inner_box.children.values()[child_index]; 1402 var target = inner_box.children.keys()[child_index]; 1403 var rect = child.rect orelse return; 1404 switch (child.widget) { 1405 .repo_comment => |*comment| { 1406 if (comment.rowRect(last)) |row_rect| { 1407 rect.x += row_rect.x; 1408 rect.y += row_rect.y; 1409 rect.size = row_rect.size; 1410 } 1411 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1412 }, 1413 // a row enters on its first cell from either direction; the tool 1414 // row keeps the cell it was on 1415 .box => |*box| if (box.children.count() > 0) { 1416 target = if (child_index == tool_row_index) 1417 box.getFocus().child_id orelse return 1418 else 1419 box.children.keys()[0]; 1420 root_focus.setFocus(target); 1421 }, 1422 else => root_focus.setFocus(target), 1423 } 1424 widget.scrollToEnteredRow(&self.scroll, rect, down); 1425 } 1426 1427 fn moveVertical(self: *This, root_focus: *Focus, down: bool) bool { 1428 const inner_box = self.inner(); 1429 const current = self.focusedChild(root_focus) orelse return false; 1430 // a tall description or comment body is scrolled through before focus 1431 // leaves it 1432 if (self.focusedRect(current)) |rect| if (widget.scrollThroughRow(&self.scroll, rect, if (down) 1 else -1)) return true; 1433 if (inner_box.children.values()[current].widget == .repo_comment) { 1434 const comment = &inner_box.children.values()[current].widget.repo_comment; 1435 if (down and !comment.bodyFocused()) { 1436 self.focusChild(current, true, true, root_focus); 1437 return true; 1438 } 1439 if (!down and comment.bodyFocused()) { 1440 self.focusChild(current, false, false, root_focus); 1441 return true; 1442 } 1443 } 1444 1445 var next = current; 1446 while (true) { 1447 if (down) { 1448 next += 1; 1449 if (next >= inner_box.children.count()) return false; 1450 } else { 1451 if (next == 0) return false; 1452 next -= 1; 1453 } 1454 if (!self.childFocusable(next)) continue; 1455 self.focusChild(next, !down, down, root_focus); 1456 return true; 1457 } 1458 } 1459 1460 // the focused part of child `child_index`, in the scroll's content: a 1461 // comment's metadata or body row, else the whole child 1462 fn focusedRect(self: *This, child_index: usize) ?layout.IRect { 1463 const child = &self.inner().children.values()[child_index]; 1464 var rect = child.rect orelse return null; 1465 if (child.widget == .repo_comment) { 1466 const comment = &child.widget.repo_comment; 1467 const row = comment.rowRect(comment.bodyFocused()) orelse return null; 1468 rect.x += row.x; 1469 rect.y += row.y; 1470 rect.size = row.size; 1471 } 1472 return rect; 1473 } 1474 1475 fn moveHorizontal(self: *This, root_focus: *Focus, right: bool) bool { 1476 const inner_box = self.inner(); 1477 const current = self.focusedChild(root_focus) orelse return false; 1478 const child = &inner_box.children.values()[current]; 1479 if (child.widget == .repo_comment) { 1480 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1481 if (moved) if (child.rect) |rect| self.scroll.scrollToRect(rect); 1482 return moved; 1483 } 1484 const row = switch (child.widget) { 1485 .box => |*box| box, 1486 else => return false, 1487 }; 1488 if (!widget.moveInRow(row, root_focus, right)) return false; 1489 if (child.rect) |rect| self.scroll.scrollToRect(rect); 1490 return true; 1491 } 1492 1493 // the input takes typing and assigns on enter 1494 fn assigneeInput(self: *This, allocator: std.mem.Allocator, row: *wgt.Box(Widget), key: Key, root_focus: *Focus) !void { 1495 const text_input = &row.children.values()[0].widget.text_input; 1496 switch (key) { 1497 .arrow_up => _ = self.moveVertical(root_focus, false), 1498 .arrow_down => _ = self.moveVertical(root_focus, true), 1499 .arrow_left => if (text_input.cursor == 0) { 1500 self.exit = .list; 1501 } else try text_input.input(allocator, key, root_focus), 1502 .enter => { 1503 const text = try text_input.text(allocator); 1504 defer allocator.free(text); 1505 try self.assign(allocator, text); 1506 }, 1507 else => try text_input.input(allocator, key, root_focus), 1508 } 1509 } 1510 1511 // arrows move along a row that ends in a ✕. returns whether enter or a 1512 // click hit the ✕. 1513 fn removableRowInput(self: *This, row: *wgt.Box(Widget), key: Key, root_focus: *Focus) bool { 1514 const remove_id = row.children.keys()[row.children.count() - 1]; 1515 switch (key) { 1516 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 1517 self.exit = .list; 1518 }, 1519 .arrow_right => _ = self.moveHorizontal(root_focus, true), 1520 .arrow_up => _ = self.moveVertical(root_focus, false), 1521 .arrow_down => _ = self.moveVertical(root_focus, true), 1522 .enter => return row.getFocus().child_id == remove_id, 1523 .mouse => |mouse| return inp.leftClickOn(root_focus, remove_id, mouse), 1524 else => {}, 1525 } 1526 return false; 1527 } 1528 1529 fn labelsInput(self: *This, child_index: usize, labels: *WordFlow, key: Key, root_focus: *Focus) void { 1530 const child_id = labels.focus.child_id orelse return; 1531 const cur = labels.indexOfFocusId(child_id) orelse return; 1532 const count = labels.text_boxes.items.len; 1533 switch (key) { 1534 .arrow_left => if (cur > 0) self.focusLabel(child_index, labels, root_focus, cur - 1) else { 1535 self.exit = .list; 1536 }, 1537 .arrow_right => if (cur + 1 < count) self.focusLabel(child_index, labels, root_focus, cur + 1), 1538 .arrow_up => if (labels.rowStep(cur, false)) |index| { 1539 self.focusLabel(child_index, labels, root_focus, index); 1540 } else { 1541 _ = self.moveVertical(root_focus, false); 1542 }, 1543 .arrow_down => if (labels.rowStep(cur, true)) |index| { 1544 self.focusLabel(child_index, labels, root_focus, index); 1545 } else { 1546 _ = self.moveVertical(root_focus, true); 1547 }, 1548 .home => self.focusLabel(child_index, labels, root_focus, 0), 1549 .end => self.focusLabel(child_index, labels, root_focus, count - 1), 1550 else => {}, 1551 } 1552 } 1553 1554 fn focusLabel(self: *This, child_index: usize, labels: *WordFlow, root_focus: *Focus, item: usize) void { 1555 root_focus.setFocus(labels.text_boxes.items[item].getFocus().id); 1556 if (self.session.is_terminal and item < labels.rects.items.len) { 1557 if (self.inner().children.values()[child_index].rect) |flow_rect| { 1558 var rect = labels.rects.items[item]; 1559 rect.x += flow_rect.x; 1560 rect.y += flow_rect.y; 1561 self.scroll.scrollToRect(rect); 1562 } 1563 } 1564 } 1565 1566 fn toolRowFocused(self: *This, root_focus: *Focus) bool { 1567 return self.focusedChild(root_focus) == tool_row_index; 1568 } 1569 1570 pub fn focusFirst(self: *This, root_focus: ?*Focus) bool { 1571 const inner_box = self.inner(); 1572 for (inner_box.children.values(), 0..) |*child, child_index| { 1573 switch (child.widget) { 1574 .box => |*box| { 1575 for (box.children.keys(), box.children.values()) |id, *item| { 1576 if (item.widget.getFocus().mode != .all) continue; 1577 box.getFocus().child_id = id; 1578 break; 1579 } else continue; 1580 }, 1581 else => if (!self.childFocusable(child_index)) continue, 1582 } 1583 if (root_focus) |focus| 1584 self.focusChild(child_index, false, true, focus) 1585 else 1586 inner_box.getFocus().child_id = inner_box.children.keys()[child_index]; 1587 return true; 1588 } 1589 return false; 1590 } 1591 1592 pub fn atTop(self: *This, root_focus: *Focus) bool { 1593 const focused = self.focusedChild(root_focus) orelse return false; 1594 for (0..self.inner().children.count()) |child_index| { 1595 if (!self.childFocusable(child_index)) continue; 1596 return focused == child_index; 1597 } 1598 return false; 1599 } 1600 1601 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 1602 if (comptime wasm) return; 1603 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1604 const author = actor.author; 1605 1606 if (comptime supports_drafts) { 1607 const entry = self.entry orelse return; 1608 if (event_kind == kind and entryDraft(entry)) { 1609 try Data.removeDraft(self.session, allocator, author, &id); 1610 const route = draftsRoute(self.data.identity) orelse return; 1611 try self.session.navigate(route); 1612 return; 1613 } 1614 } 1615 1616 const io = self.session.io orelse return; 1617 const source = self.data.repo_source orelse return; 1618 if (!try source.canModify(io, allocator, actor, event_kind, &id)) return; 1619 const thread_id = evt.parseEventId((self.entry orelse return).id) catch return; 1620 1621 switch (source.repo_kind) { 1622 inline else => |repo_kind| { 1623 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1624 defer any_repo.deinit(io, allocator); 1625 switch (any_repo) { 1626 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 1627 } 1628 }, 1629 } 1630 1631 const id_hex = std.fmt.bytesToHex(id, .lower); 1632 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 1633 try self.session.navigate(route); 1634 } 1635 1636 // assign the typed email, or the email of the user the text names 1637 fn assign(self: *This, allocator: std.mem.Allocator, text: []const u8) !void { 1638 if (comptime wasm) return; 1639 const entry = self.entry orelse return; 1640 const io = self.session.io orelse return; 1641 const source = self.data.repo_source orelse return; 1642 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1643 const thread_id = try evt.parseEventId(entry.id); 1644 if (!try source.canModify(io, allocator, actor, kind, &thread_id)) return; 1645 1646 var arena = std.heap.ArenaAllocator.init(allocator); 1647 defer arena.deinit(); 1648 const failure: ?ui.Session.FormFeedback.AssignmentFailure = blk: { 1649 const moment = if (self.session.data.host_kind == .local) null else self.session.haxy_moment; 1650 const email = switch (try ui.assigneeEmail(moment, &arena, text)) { 1651 .email => |email| email, 1652 .failure => |value| break :blk value, 1653 }; 1654 const thread_id_hex = std.fmt.bytesToHex(thread_id, .lower); 1655 switch (source.repo_kind) { 1656 inline else => |repo_kind| { 1657 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1658 defer any_repo.deinit(io, allocator); 1659 switch (any_repo) { 1660 inline else => |*repo| _ = evt.Assignment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id_hex, email, actor.author) catch |err| switch (err) { 1661 error.AlreadyAssigned => break :blk .already_assigned, 1662 error.InvalidFields => break :blk .not_an_email, 1663 else => |e| return e, 1664 }, 1665 } 1666 }, 1667 } 1668 break :blk null; 1669 }; 1670 if (failure) |value| { 1671 self.session.data.form_feedback = .{ .assignment = .{ .failure = value, .text = try self.session.arena.allocator().dupe(u8, text) } }; 1672 return; 1673 } 1674 try self.session.navigate(ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1675 } 1676 1677 fn unassign(self: *This, allocator: std.mem.Allocator, email: []const u8) !void { 1678 if (comptime wasm) return; 1679 const entry = self.entry orelse return; 1680 const io = self.session.io orelse return; 1681 const source = self.data.repo_source orelse return; 1682 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1683 const thread_id = try evt.parseEventId(entry.id); 1684 if (!try source.canModify(io, allocator, actor, kind, &thread_id)) return; 1685 1686 switch (source.repo_kind) { 1687 inline else => |repo_kind| { 1688 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1689 defer any_repo.deinit(io, allocator); 1690 switch (any_repo) { 1691 inline else => |*repo| evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, .assign, &evt.Assignment.idOf(&thread_id, email), actor.author) catch |err| switch (err) { 1692 // a stale button; the refresh below shows the row as it is 1693 error.EventNotFound => {}, 1694 else => |e| return e, 1695 }, 1696 } 1697 }, 1698 } 1699 try self.session.navigate(ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1700 } 1701 1702 // subscribe to the thread or mute it, as its button says 1703 fn changeNotify(self: *This, allocator: std.mem.Allocator) !void { 1704 if (comptime wasm) return; 1705 const entry = self.entry orelse return; 1706 const mode = entry.notify_action orelse return; 1707 const io = self.session.io orelse return; 1708 const users_dir = self.session.users_dir orelse return; 1709 const source = self.data.repo_source orelse return; 1710 const actor = (try self.session.authorize(self.data.identity, .read, null)) orelse return; 1711 // a server actor always has both ids 1712 const user_id = actor.user_id orelse unreachable; 1713 const location: evt.RepoLocation = .{ 1714 .owner_id = actor.repo_user_id orelse unreachable, 1715 .repo_id = try evt.parseEventId(std.Io.Dir.path.basename(source.path)), 1716 }; 1717 const thread_id = try evt.parseEventId(entry.id); 1718 switch (mode) { 1719 .subscribe => try notify.subscribe(io, allocator, users_dir, &user_id, location, &thread_id, actor.author), 1720 .mute => try notify.mute(io, allocator, users_dir, &user_id, location, &thread_id, actor.author), 1721 } 1722 try self.session.navigate(ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1723 } 1724 1725 fn toggleStatus(self: *This, allocator: std.mem.Allocator) !void { 1726 if (comptime (!has_status or wasm)) return; 1727 const entry = self.entry orelse return; 1728 const io = self.session.io orelse return; 1729 const source = self.data.repo_source orelse return; 1730 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1731 const author = actor.author; 1732 const status = (statusChange(entryStatus(entry)) orelse return).status; 1733 const id = try evt.parseEventId(entry.id); 1734 if (!try source.canModify(io, allocator, actor, kind, &id)) return; 1735 1736 switch (source.repo_kind) { 1737 inline else => |repo_kind| { 1738 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1739 defer any_repo.deinit(io, allocator); 1740 switch (any_repo) { 1741 inline else => |*repo| try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id, .{ .status = status }, author), 1742 } 1743 }, 1744 } 1745 1746 const route = listRoute(self.data.identity, status, entry.id) orelse return; 1747 try self.session.navigate(route); 1748 } 1749 }; 1750 } 1751 1752 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 1753 const Self = Data; 1754 const HeaderType = Data.Header; 1755 const Window = Data.Window; 1756 const Entry = Data.Entry; 1757 const Event = Data.Event; 1758 const feedback_tag: std.meta.Tag(ui.Session.FormFeedback) = switch (kind) { 1759 .issue => .issue, 1760 .patch => .patch, 1761 .discuss => .discussion, 1762 else => unreachable, 1763 }; 1764 const FeedbackFailure = if (kind == .patch) ui.Session.FormFeedback.PatchFailure else ui.Session.FormFeedback.ThreadFailure; 1765 const Status = Data.Status; 1766 const has_status = @hasField(Event, "status"); 1767 const supports_conflicts = Event.merge_policy == .field_conflicts; 1768 const supports_drafts = @hasField(Self, "drafts"); 1769 const supports_merge = @hasDecl(Data, "mergeRoute"); 1770 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 1771 const ViewKind = Data.ViewKind; 1772 const thread_name = Data.thread_name; 1773 const DetailType = Data.Detail; 1774 1775 return struct { 1776 const This = @This(); 1777 // a vertical box: the header tabs on top, then a stack holding a 1778 // master-detail split (thread list + description pane) per status list, 1779 // plus the labels view. 1780 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 1781 data: *const Self, 1782 session: *ui.Session, 1783 1784 const header_index: usize = 0; 1785 const stack_index: usize = 1; 1786 // indices within the stack, 1:1 with the header tabs. 1787 const status_count = @typeInfo(Status).@"enum".field_names.len; 1788 const labels_view_index: usize = status_count; 1789 // the new-thread form, or the edit, comment, or resolve form when the page was 1790 // loaded at one of their urls. 1791 const form_view_index: usize = labels_view_index + 1; 1792 // the conflicts split; the tab and stack child exist only when the repo 1793 // has conflicted threads. 1794 const drafts_view_index: usize = form_view_index + 1; 1795 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 1796 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 1797 // indices within a split (the horizontal box inside the stack). 1798 const list_index: usize = 0; 1799 const detail_index: usize = 1; 1800 const list_max_width: usize = 35; 1801 const detail_min_width: usize = 40; 1802 1803 pub fn viewIndex(view: ViewKind) usize { 1804 const name = @tagName(view); 1805 if (std.mem.eql(u8, name, "labels")) return labels_view_index; 1806 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; 1807 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 1808 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 1809 if (std.mem.eql(u8, name, "description")) unreachable; 1810 inline for (@typeInfo(Status).@"enum".field_names, 0..) |status_name, index| { 1811 if (std.mem.eql(u8, name, status_name)) return index; 1812 } 1813 unreachable; 1814 } 1815 1816 const Confirmation = enum { remove, publish, merge }; 1817 1818 fn confirmation(view: ViewKind) ?Confirmation { 1819 if (view == .remove) return .remove; 1820 if (supports_drafts and std.mem.eql(u8, @tagName(view), "publish")) return .publish; 1821 if (supports_merge and std.mem.eql(u8, @tagName(view), "merge")) return .merge; 1822 return null; 1823 } 1824 1825 // the status a status split lists; the conflicts split has none (its 1826 // threads carry their own). 1827 fn splitStatus(index: usize) Status { 1828 return @fromBackingInt(@intCast(index)); 1829 } 1830 1831 fn entryConflicted(entry: Entry) bool { 1832 return if (supports_conflicts) entry.conflicted else false; 1833 } 1834 1835 fn entryStatus(entry: Entry) Status { 1836 if (comptime has_status) return Data.statusKind(entry.record.event); 1837 return @fromBackingInt(@intCast(0)); 1838 } 1839 1840 fn entryDraft(entry: Entry) bool { 1841 return if (supports_drafts) entry.draft else false; 1842 } 1843 1844 fn listRoute(identity: []const u8, status: Status, selected: []const u8) ?ui.RoutablePage { 1845 return Data.listRoute(identity, status, selected); 1846 } 1847 1848 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 1849 return if (supports_drafts) Data.draftsRoute(identity) else null; 1850 } 1851 1852 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1853 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 1854 } 1855 1856 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1857 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 1858 } 1859 1860 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1861 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 1862 } 1863 1864 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 1865 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 1866 } 1867 1868 // a list link keeps the search the page carries 1869 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, id: []const u8, search: []const u8) ![]const u8 { 1870 var route = listRoute(identity, status, id) orelse return error.RouteTooLong; 1871 if (search.len != 0) route = route.withSearch(search) orelse return error.RouteTooLong; 1872 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1873 } 1874 1875 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 1876 const selected = std.mem.eql(u8, id, data.selected_id); 1877 const route = (if (selected and data.description_page) 1878 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 1879 else if (selected) 1880 if (data.comment_page) |page| 1881 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 1882 else 1883 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 1884 else 1885 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 1886 return page_arena.allocator().print("ai:{s}", .{try route.toUrl(page_arena)}); 1887 } 1888 1889 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 1890 if (supports_conflicts and status_maybe == null and !drafts) { 1891 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 1892 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1893 } 1894 if (drafts and id.len == 0) { 1895 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 1896 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1897 } 1898 return listLink(page_arena, data.identity, status_maybe orelse @fromBackingInt(@intCast(0)), id, data.search orelse ""); 1899 } 1900 1901 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 1902 var outer = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 1903 errdefer outer.deinit(allocator); 1904 1905 // the tabs at the top. 1906 { 1907 var hdr = try Data.initHeader(allocator, session, data); 1908 errdefer hdr.deinit(allocator); 1909 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 1910 } 1911 1912 // the stack enters `outer` before its children enter it, so an error 1913 // frees each child exactly once. 1914 { 1915 var stack = try wgt.Stack(Widget).init(allocator); 1916 errdefer stack.deinit(allocator); 1917 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1918 } 1919 const stack = &outer.children.values()[stack_index].widget.stack; 1920 1921 inline for (@typeInfo(Status).@"enum".field_values) |value| { 1922 var split = try initSplit(allocator, session, data, @fromBackingInt(@intCast(value)), false); 1923 errdefer split.deinit(allocator); 1924 try stack.children.put(allocator, split.getFocus().id, split); 1925 } 1926 1927 // the labels view, or the empty message without labels 1928 if (labelsEmpty(data)) { 1929 var center = try widget.initEmpty(allocator); 1930 errdefer center.deinit(allocator); 1931 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1932 } else { 1933 var tf = try WordFlow.init(allocator); 1934 errdefer tf.deinit(allocator); 1935 var items: std.ArrayList(WordFlow.Item) = .empty; 1936 defer items.deinit(allocator); 1937 for (data.labels) |label| { 1938 const search = try srch_thrd.withFilter(session.page_arena.allocator(), data.search orelse "", srch_thrd.label_prefix, label); 1939 try items.append(allocator, .{ .text = label, .link = try listLink(session.page_arena, data.identity, @fromBackingInt(@intCast(0)), "", search) }); 1940 } 1941 try tf.setItems(allocator, items.items); 1942 try stack.children.put(allocator, tf.getFocus().id, .{ .word_flow = tf }); 1943 } 1944 1945 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1946 // its place, prefilled with the selected thread's content. a viewer 1947 // who can't write in this tab gets the unauthorized view instead, 1948 // while local mode always writes. 1949 if (session.data.host_kind == .local or data.viewer != null) { 1950 if (confirmation(data.view)) |confirmation_kind| { 1951 const aa = session.page_arena.allocator(); 1952 const route = switch (confirmation_kind) { 1953 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1954 .publish => publishRoute(data.identity, data.selected_id), 1955 .merge => mergeRoute(data.identity, data.selected_id), 1956 } orelse return error.RouteTooLong; 1957 const page_url = try route.toUrl(session.page_arena); 1958 const action = try aa.print("form:{s}", .{page_url}); 1959 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1960 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1961 var buttons: [2]ConfirmationButton = undefined; 1962 buttons[0] = .{ .label = switch (confirmation_kind) { 1963 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1964 .publish => "publish patch", 1965 .merge => "merge patch", 1966 } }; 1967 var button_count: usize = 1; 1968 if (confirmation_kind == .merge) { 1969 if (comptime supports_merge) { 1970 const availability = if (data.selectedThread()) |entry| 1971 if (Data.canMerge(entry.*, session)) entry.mergeability else pch.Mergeability{} 1972 else 1973 pch.Mergeability{}; 1974 buttons[0].bottom_label = mergeBottomLabel(availability.source); 1975 if (availability.source != .clean) buttons[0].action = ""; 1976 buttons[1] = .{ 1977 .label = "squash and merge patch", 1978 .bottom_label = mergeBottomLabel(availability.squash), 1979 .action = if (availability.squash == .clean) try aa.print("{s}{s}/squash", .{ ui.submit_action_prefix, page_url }) else "", 1980 }; 1981 button_count += 1; 1982 } else unreachable; 1983 } 1984 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1985 errdefer center.deinit(allocator); 1986 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1987 } else if (data.view == .new_comment or data.view == .edit_comment) { 1988 const editing = data.view == .edit_comment; 1989 const route = if (editing) 1990 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1991 else 1992 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1993 const page_url = try route.toUrl(session.page_arena); 1994 const action = try session.page_arena.allocator().print("form:{s}", .{page_url}); 1995 const page = data.comment_page; 1996 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1997 const parent_route = if (editing) 1998 if (top_level) 1999 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 2000 else if (page) |p| 2001 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 2002 else 2003 null 2004 else if (page) |p| 2005 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 2006 else 2007 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 2008 const parent_author: ui.Author = if (editing) 2009 if (top_level) 2010 if (data.selectedThread()) |entry| entry.author else .unknown 2011 else if (page) |p| 2012 p.selected.parent_author orelse .unknown 2013 else 2014 .unknown 2015 else if (page) |p| 2016 p.selected.author 2017 else if (data.selectedThread()) |entry| 2018 entry.author 2019 else 2020 .unknown; 2021 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 2022 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 2023 errdefer form.deinit(allocator); 2024 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 2025 } else if (supports_conflicts and data.view == .resolve) { 2026 // on the web the page grows to the form's height and the 2027 // browser scrolls it; the terminal scrolls the form itself 2028 var form_widget: Widget = blk: { 2029 var form = try initResolveForm(allocator, session, data); 2030 errdefer form.deinit(allocator); 2031 break :blk if (session.is_terminal) 2032 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 2033 else 2034 .{ .box = form }; 2035 }; 2036 errdefer form_widget.deinit(allocator); 2037 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 2038 } else { 2039 const aa = session.page_arena.allocator(); 2040 const route = if (data.view == .edit) 2041 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 2042 else 2043 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 2044 const action = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}); 2045 const record = if (data.view == .edit) 2046 (if (data.selectedThread()) |entry| &entry.record else null) 2047 else 2048 null; 2049 var form = try initThreadForm(allocator, session, data, action, record); 2050 errdefer form.deinit(allocator); 2051 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 2052 } 2053 } else { 2054 var message = try ui.Unauthorized.View.init(allocator); 2055 errdefer message.deinit(allocator); 2056 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 2057 } 2058 2059 if (supports_drafts) { 2060 var split = try initSplit(allocator, session, data, null, true); 2061 errdefer split.deinit(allocator); 2062 try stack.children.put(allocator, split.getFocus().id, split); 2063 } 2064 2065 // the conflicts split; its tab exists only when the repo has 2066 // conflicts, so skip the child too to keep the stack 1:1 with the 2067 // tabs. 2068 if (supports_conflicts and data.conflicts.count > 0) { 2069 var split = try initSplit(allocator, session, data, null, false); 2070 errdefer split.deinit(allocator); 2071 try stack.children.put(allocator, split.getFocus().id, split); 2072 } 2073 2074 // the stack starts on the page's view. 2075 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 2076 2077 // focus entering the view lands on the tabs first. 2078 outer.getFocus().child_id = outer.children.keys()[header_index]; 2079 2080 return .{ 2081 .box = outer, 2082 .data = data, 2083 .session = session, 2084 }; 2085 } 2086 2087 // the master-detail split showing `status`'s window, or the conflicts 2088 // window when null. an empty first window shows the empty message instead. 2089 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !Widget { 2090 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; 2091 if (windowEmpty(win)) return .{ .center = try widget.initEmpty(allocator) }; 2092 2093 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2094 errdefer box.deinit(allocator); 2095 2096 // the thread list (one focusable row per title), plus a "next" link that 2097 // reloads the page rooted at the following thread. 2098 { 2099 var list_scroll = blk: { 2100 var list_box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 2101 errdefer list_box.deinit(allocator); 2102 if (win.prev_id) |prev| 2103 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 2104 for (win.items) |entry| { 2105 const label: []const u8 = if (entry.comments.count > 0) 2106 try session.page_arena.allocator().print(" {d} comments ", .{entry.comments.count}) 2107 else 2108 ""; 2109 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 2110 } 2111 if (win.next_id) |next| 2112 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 2113 // select the window's first thread (past a leading "previous" 2114 // row) so its description shows on load. 2115 if (win.items.len > 0) 2116 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 2117 else if (list_box.children.count() > 0) 2118 list_box.getFocus().child_id = list_box.children.keys()[0]; 2119 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 2120 }; 2121 errdefer list_scroll.deinit(allocator); 2122 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 } }); 2123 } 2124 2125 // frame the detail pane inside the master-detail split, below the 2126 // way back to the list, shown only while the list is hidden 2127 { 2128 var frame = try wgt.Box(Widget).init(allocator, .{ .border = .hidden, .direction = .vert }); 2129 errdefer frame.deinit(allocator); 2130 frame.getFocus().mode = .mouse; 2131 try widget.addListBack(allocator, &frame, "← back to " ++ thread_name ++ " list"); 2132 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 2133 errdefer detail_view.deinit(allocator); 2134 frame.getFocus().child_id = detail_view.getFocus().id; 2135 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 2136 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 2137 } 2138 2139 // the page opens on the list, unless the url names a thread 2140 box.getFocus().child_id = box.children.keys()[if (data.selected_id.len > 0) detail_index else list_index]; 2141 return .{ .box = box }; 2142 } 2143 2144 // a thread form, plus the target branch for patches, prefilled from 2145 // `record` when given. its form: subtree makes the web renderer POST 2146 // the inputs to `action`'s route. 2147 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 2148 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2149 errdefer box.deinit(allocator); 2150 box.getFocus().kind = .{ .custom = action }; 2151 2152 const saved_form = session.formFeedback(feedback_tag); 2153 const saved_fields = if (saved_form) |saved| saved.fields else null; 2154 2155 { 2156 var title = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " title " }, .name = "title", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2157 errdefer title.deinit(allocator); 2158 title.getFocus().mode = .all; 2159 if (saved_fields) |saved| 2160 try title.setContent(allocator, saved.title) 2161 else if (record) |r| 2162 try title.setContent(allocator, r.event.title); 2163 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2164 box.getFocus().child_id = title.getFocus().id; 2165 } 2166 2167 { 2168 var labels = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " labels (separate with spaces) " }, .name = "labels", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2169 errdefer labels.deinit(allocator); 2170 labels.getFocus().mode = .all; 2171 if (saved_fields) |saved| 2172 try labels.setContent(allocator, saved.labels) 2173 else if (record) |r| 2174 try labels.setContent(allocator, r.event.labels); 2175 try box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .text_input = labels }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2176 } 2177 2178 { 2179 var description = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " description (markdown supported) " }, .name = "description", .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 2180 errdefer description.deinit(allocator); 2181 description.getFocus().mode = .all; 2182 if (saved_fields) |saved| 2183 try description.setContent(allocator, saved.description) 2184 else if (record) |r| 2185 try description.setContent(allocator, r.event.description); 2186 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 2187 } 2188 2189 if (supports_drafts and record == null) { 2190 var source = try Data.Source.init(allocator, session, if (saved_fields) |saved| saved.source_branch else "", if (saved_fields) |saved| saved.source_kind else .fork); 2191 errdefer source.deinit(allocator); 2192 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .patch_source = source }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2193 } 2194 2195 if (supports_drafts) { 2196 var target = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " target branch " }, .name = "target_branch", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2197 errdefer target.deinit(allocator); 2198 target.getFocus().mode = .all; 2199 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 2200 try target.setContent(allocator, content); 2201 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2202 } 2203 2204 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 2205 2206 return box; 2207 } 2208 2209 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) { 2210 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2211 errdefer box.deinit(allocator); 2212 box.getFocus().kind = .{ .custom = action }; 2213 2214 { 2215 var parent = try ui.authorBox(allocator, session.page_arena, author); 2216 errdefer parent.deinit(allocator); 2217 parent.options.top_label.text = " replying to "; 2218 parent.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 2219 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2220 box.getFocus().child_id = parent.getFocus().id; 2221 } 2222 2223 { 2224 var body = try wgt.TextInput.init(allocator, .{ 2225 .top_label = .{ .text = " comment (markdown supported) " }, 2226 .name = "body", 2227 .visible_width = null, 2228 .round_corners = true, 2229 .render_content = session.is_terminal, 2230 .multiline = true, 2231 .visible_height = 5, 2232 .scroll = .{ .fill = true }, 2233 }); 2234 errdefer body.deinit(allocator); 2235 body.getFocus().mode = .all; 2236 if (initial_body) |text| try body.setContent(allocator, text); 2237 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 2238 } 2239 2240 try addSubmitButtonLabeled(allocator, &box, "submit"); 2241 return box; 2242 } 2243 2244 const ConfirmationButton = struct { 2245 label: []const u8, 2246 bottom_label: []const u8 = "", 2247 action: []const u8 = "submit", 2248 }; 2249 2250 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 2251 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 2252 errdefer box.deinit(allocator); 2253 box.getFocus().kind = .{ .custom = action }; 2254 2255 var prompt = try wgt.Text.init(allocator, "are you sure?"); 2256 errdefer prompt.deinit(allocator); 2257 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 2258 2259 for (buttons, 0..) |options, index| { 2260 var button = try wgt.TextBox.init(allocator, options.label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = options.bottom_label } }); 2261 errdefer button.deinit(allocator); 2262 button.getFocus().mode = .all; 2263 button.getFocus().kind = .{ .custom = options.action }; 2264 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 2265 if (index == 0) box.getFocus().child_id = button.getFocus().id; 2266 } 2267 2268 return Center.init(allocator, .{ .box = box }); 2269 } 2270 2271 // a form's submit button, then a spacer absorbing the leftover 2272 // min-height the box hands its last child, so the button keeps its 2273 // natural height 2274 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 2275 { 2276 var submit = try SubmitButton.initLabeled(allocator, label); 2277 errdefer submit.deinit(allocator); 2278 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2279 } 2280 { 2281 var spacer = try Spacer.init(allocator); 2282 errdefer spacer.deinit(allocator); 2283 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 2284 } 2285 } 2286 2287 // the conflict resolve form. the "use this" links are navigations that 2288 // flip the url's theirs: pick, reloading the prefills; one submit 2289 // settles every conflict. the form: subtree makes the web renderer POST 2290 // to the resolve route. 2291 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 2292 const aa = session.page_arena.allocator(); 2293 // init only picks the resolve view once it has read the conflict 2294 const conflict = if (data.conflict) |*c| c else unreachable; 2295 2296 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2297 errdefer box.deinit(allocator); 2298 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 2299 box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 2300 2301 if (conflict.title) |*fc| { 2302 try addLabel(allocator, &box, "title conflict:"); 2303 try addFieldConflict(allocator, &box, session, data, "title", fc); 2304 } 2305 if (conflict.labels) |*fc| { 2306 try addLabel(allocator, &box, "labels conflict:"); 2307 try addFieldConflict(allocator, &box, session, data, "labels", fc); 2308 } 2309 if (comptime @hasField(Data.Conflict, "status")) { 2310 if (conflict.status) |*fc| { 2311 try addLabel(allocator, &box, "status conflict:"); 2312 try addAtomicConflict(allocator, &box, session, data, "status", fc); 2313 } 2314 } 2315 if (comptime @hasField(Data.Conflict, "revision")) { 2316 if (conflict.revision) |*fc| { 2317 try addLabel(allocator, &box, "revision conflict:"); 2318 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 2319 } 2320 if (conflict.target_branch) |*fc| { 2321 try addLabel(allocator, &box, "target branch conflict:"); 2322 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 2323 } 2324 } 2325 2326 if (conflict.description) |*desc| { 2327 try addLabel(allocator, &box, "description conflict:"); 2328 var hunk_index: usize = 0; 2329 for (desc.chunks, 0..) |*chunk, chunk_index| { 2330 switch (chunk.*) { 2331 .same => |text| { 2332 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .word }); 2333 errdefer tb.deinit(allocator); 2334 tb.getFocus().mode = .all; 2335 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2336 }, 2337 // an auto-resolved chunk: only one side changed it, so its 2338 // version stands, shown distinctly with its author 2339 .auto => |auto| { 2340 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 2341 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 2342 const label = switch (author) { 2343 .user_name, .email => |name| try aa.print(" {s} by {s} ", .{ verb, name }), 2344 .unknown => try aa.print(" {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 2345 }; 2346 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2347 errdefer tb.deinit(allocator); 2348 tb.getFocus().mode = .all; 2349 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2350 }, 2351 .conflict => |hunk| { 2352 // a blank row on each side sets the group apart from 2353 // the flowing shared and auto-resolved chunks 2354 if (chunk_index > 0) try addGap(allocator, &box); 2355 const name = try aa.print("d{d}", .{hunk_index}); 2356 hunk_index += 1; 2357 const picked_theirs = data.theirsPicked(name); 2358 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 2359 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 2360 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true }); 2361 errdefer resolution_input.deinit(allocator); 2362 resolution_input.getFocus().mode = .all; 2363 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 2364 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2365 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 2366 }, 2367 } 2368 } 2369 } 2370 2371 try addSubmitButtonLabeled(allocator, &box, "submit"); 2372 2373 box.getFocus().child_id = box.children.keys()[0]; 2374 return box; 2375 } 2376 2377 // one conflicted scalar field: both sides' version rows, then the 2378 // resolution input prefilled from the picked side. 2379 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2380 const aa = session.page_arena.allocator(); 2381 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2382 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2383 2384 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2385 errdefer resolution_input.deinit(allocator); 2386 resolution_input.getFocus().mode = .all; 2387 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2388 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2389 } 2390 2391 // atomic fields can only pick one complete side 2392 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2393 const aa = session.page_arena.allocator(); 2394 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2395 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2396 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2397 .border = .single, 2398 .round_corners = true, 2399 .wrap_kind = .word, 2400 .top_label = .{ .text = " resolution " }, 2401 }); 2402 errdefer selected.deinit(allocator); 2403 selected.getFocus().mode = .all; 2404 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2405 } 2406 2407 // a blank row setting a conflict group apart from its neighbors 2408 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2409 var text = try wgt.Text.init(allocator, " "); 2410 errdefer text.deinit(allocator); 2411 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2412 } 2413 2414 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2415 var label = try SectionLabel.init(allocator, content); 2416 errdefer label.deinit(allocator); 2417 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2418 } 2419 2420 // one side of a conflict: the "use this" link on the left, the version 2421 // itself as a labeled read-only box. 2422 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2423 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2424 errdefer row.deinit(allocator); 2425 2426 { 2427 const use_label = "use this"; 2428 var use = try wgt.TextBox.init(allocator, use_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 2429 errdefer use.deinit(allocator); 2430 use.getFocus().mode = .all; 2431 use.getFocus().kind = .{ .custom = link }; 2432 try row.children.put(allocator, use.getFocus().id, .{ 2433 .widget = .{ .text_box = use }, 2434 .rect = null, 2435 .min_size = .{ .width = use_label.len + 2, .height = null }, 2436 .max_size = .{ .width = use_label.len + 2, .height = null }, 2437 }); 2438 } 2439 2440 { 2441 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2442 errdefer tb.deinit(allocator); 2443 tb.getFocus().mode = .all; 2444 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2445 } 2446 2447 row.getFocus().child_id = row.children.keys()[0]; 2448 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2449 } 2450 2451 // the label of one side's version box, naming its author 2452 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2453 return switch (author) { 2454 .user_name, .email => |name| try aa.print(" edited by {s} ", .{name}), 2455 .unknown => if (ours) " current version " else " their version ", 2456 }; 2457 } 2458 2459 // the a: link that reloads the resolve page with `name` picked from ours 2460 // or theirs, reprefilling its resolution input 2461 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2462 const aa = session.page_arena.allocator(); 2463 var picks: std.ArrayList(u8) = .empty; 2464 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2465 while (it.next()) |pick| { 2466 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2467 if (picks.items.len > 0) try picks.append(aa, ','); 2468 try picks.appendSlice(aa, pick); 2469 } 2470 if (theirs) { 2471 if (picks.items.len > 0) try picks.append(aa, ','); 2472 try picks.appendSlice(aa, name); 2473 } 2474 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2475 return aa.print("a:{s}", .{try route.toUrl(session.page_arena)}); 2476 } 2477 2478 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2479 var row = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = bottom_label } }); 2480 errdefer row.deinit(allocator); 2481 row.getFocus().mode = .all; 2482 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2483 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2484 } 2485 2486 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2487 self.box.deinit(allocator); 2488 } 2489 2490 fn header(self: *This) *HeaderType { 2491 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2492 } 2493 2494 fn viewStack(self: *This) *wgt.Stack(Widget) { 2495 return &self.box.children.values()[stack_index].widget.stack; 2496 } 2497 2498 // `index`'s master-detail split inside the stack. 2499 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2500 return &self.viewStack().children.values()[index].box; 2501 } 2502 2503 // the labels view's flow inside the stack. 2504 fn labelsView(self: *This) *WordFlow { 2505 return &self.viewStack().children.values()[labels_view_index].word_flow; 2506 } 2507 2508 // no labels, which shows the empty message 2509 fn labelsEmpty(data: *const Self) bool { 2510 return data.labels.len == 0; 2511 } 2512 2513 // the non-confirmation form inside the stack 2514 fn formBox(self: *This) ?*wgt.Box(Widget) { 2515 return switch (self.viewStack().children.values()[form_view_index]) { 2516 .box => |*box| box, 2517 // the resolve form sits inside a scroll on the terminal 2518 .scroll => |*scroll| &scroll.child.box, 2519 else => null, 2520 }; 2521 } 2522 2523 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2524 if (supports_conflicts and self.data.view == .resolve) return null; 2525 return switch (self.data.view) { 2526 .new_comment, .edit_comment => null, 2527 else => self.formBox(), 2528 }; 2529 } 2530 2531 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2532 for (form.children.values()) |*child| switch (child.widget) { 2533 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2534 else => {}, 2535 }; 2536 return error.MissingFormField; 2537 } 2538 2539 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2540 for (form.children.values()) |*child| { 2541 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2542 } 2543 return null; 2544 } 2545 2546 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2547 return switch (self.viewStack().children.values()[form_view_index]) { 2548 .center => |*center| switch (center.child.*) { 2549 .box => |*box| box, 2550 else => null, 2551 }, 2552 else => null, 2553 }; 2554 } 2555 2556 // the resolve form's scroll on the terminal (the web page scrolls itself) 2557 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2558 return switch (self.viewStack().children.values()[form_view_index]) { 2559 .scroll => |*scroll| scroll, 2560 else => null, 2561 }; 2562 } 2563 2564 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2565 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2566 } 2567 2568 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2569 return &self.listScroll(index).child.box; 2570 } 2571 2572 fn detailFrame(self: *This, index: usize) *wgt.Box(Widget) { 2573 return &self.resultsBox(index).children.values()[detail_index].widget.box; 2574 } 2575 2576 fn detail(self: *This, index: usize) *DetailType { 2577 return &@field(self.detailFrame(index).children.values()[widget.list_pane_index].widget, Data.detail_widget_name); 2578 } 2579 2580 fn window(self: *This, index: usize) *const Window { 2581 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2582 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2583 return self.data.window(splitStatus(index)); 2584 } 2585 2586 // an empty first window, which shows the empty message 2587 fn windowEmpty(win: *const Window) bool { 2588 return win.items.len == 0 and win.prev_id == null; 2589 } 2590 2591 fn stackSelectedIndex(self: *This) ?usize { 2592 const stack = self.viewStack(); 2593 const cid = stack.getFocus().child_id orelse return null; 2594 return stack.children.getIndex(cid); 2595 } 2596 2597 // the stack's selected master-detail split (null when the labels view, the 2598 // new-thread form, or the empty message shows). 2599 fn selectedSplitIndex(self: *This) ?usize { 2600 const idx = self.stackSelectedIndex() orelse return null; 2601 const split = idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index; 2602 return if (split and !windowEmpty(self.window(idx))) idx else null; 2603 } 2604 2605 fn detailActive(self: *This, index: usize) bool { 2606 const rb = self.resultsBox(index); 2607 const cid = rb.getFocus().child_id orelse return false; 2608 return rb.children.getIndex(cid) == detail_index; 2609 } 2610 2611 fn headerActive(self: *This) bool { 2612 const cid = self.box.getFocus().child_id orelse return false; 2613 return self.box.children.getIndex(cid) == header_index; 2614 } 2615 2616 fn labelsViewActive(self: *This) bool { 2617 if (self.headerActive()) return false; 2618 return self.stackSelectedIndex() == labels_view_index; 2619 } 2620 2621 fn formViewActive(self: *This) bool { 2622 if (self.headerActive()) return false; 2623 return self.stackSelectedIndex() == form_view_index; 2624 } 2625 2626 // the selected thread's index, or null when a window-navigation row is 2627 // selected (a leading "previous" row shifts the thread rows down by one). 2628 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2629 const lb = self.listBox(index); 2630 const cid = lb.getFocus().child_id orelse return null; 2631 const idx = lb.children.getIndex(cid) orelse return null; 2632 const win = self.window(index); 2633 const lead: usize = if (win.prev_id != null) 1 else 0; 2634 if (idx < lead or idx - lead >= win.items.len) return null; 2635 return idx - lead; 2636 } 2637 2638 // swap the detail pane to the selected thread when it changes 2639 fn refreshDetail(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2640 const selected = self.selectedThreadIndex(index) orelse return; 2641 const entry = self.window(index).items[selected]; 2642 const changed = if (self.detail(index).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2643 if (!changed) return; 2644 try self.detail(index).setEntry(allocator, entry); 2645 widget.setListBackLink(self.detailFrame(index), try rowLink(self.session.page_arena, self.data, entry.id)); 2646 } 2647 2648 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2649 self.clearGrid(); 2650 2651 // each header tab maps 1:1 to a stack child by position 2652 if (self.header().getSelectedIndex()) |index| { 2653 const stack = self.viewStack(); 2654 stack.getFocus().child_id = stack.children.keys()[index]; 2655 } 2656 2657 if (self.selectedSplitIndex()) |i| { 2658 // whether the detail pane fits beside the list, which decides 2659 // the list cap below and whether the detail carries a way back 2660 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2661 try self.refreshDetail(allocator, i); 2662 widget.showListBack(self.detailFrame(i), !both_panes_fit); 2663 2664 // the selected list row shows a border (the focused TextBox 2665 // upgrades it to a double border itself); the rest stay borderless. 2666 const lb = self.listBox(i); 2667 for (lb.children.keys(), lb.children.values()) |id, *child| { 2668 switch (child.widget) { 2669 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2670 else => {}, 2671 } 2672 } 2673 2674 // cap the list at list_max_width only while the detail pane fits 2675 // beside it. the box drops the detail when the width can't hold 2676 // both minimums, so when it's that narrow we lift the cap and let 2677 // the list fill the whole width. 2678 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2679 2680 // stretch the detail pane across the rest of the width so it fills 2681 // the area rather than shrinking to its content; its scroll fills 2682 // the pane. 2683 if (constraint.max_size.width) |w| { 2684 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2685 } else { 2686 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2687 } 2688 } 2689 2690 // refresh the form inputs' entries in the session's focus-id -> input 2691 // map with this frame's addresses, so the web/wasm form handling can 2692 // find them by focus id 2693 if (self.threadForm()) |form| { 2694 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2695 (try formField(form, "title")).options.top_label.text = if (failure == .required_title) " title (required) " else " title "; 2696 if (comptime supports_drafts) { 2697 if (formWidget(form, .patch_source)) |source| { 2698 source.field().options.top_label.text = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2699 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2700 try submit.setLabel(allocator, switch (source.selectedKind()) { 2701 .fork => "submit draft", 2702 .branch => "submit patch", 2703 }); 2704 } 2705 (try formField(form, "target_branch")).options.top_label.text = 2706 if (failure == .invalid_target_branch) " target branch (invalid) " else if (self.data.view == .edit and failure == .same_branch) " target branch (same as source) " else if (self.data.view == .edit and failure == .unrelated_branches) " target branch (unrelated to source) " else if (self.data.view == .edit and failure == .invalid_source_branch) " target branch (source branch not found) " else " target branch "; 2707 } 2708 } 2709 if (self.formBox()) |form| { 2710 const inputs_arena = self.session.arena.allocator(); 2711 for (form.children.values()) |*child| switch (child.widget) { 2712 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2713 else => {}, 2714 }; 2715 } 2716 2717 try self.box.build(allocator, constraint, root_focus); 2718 } 2719 2720 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2721 // wheel input moves focus like the arrows 2722 const key: Key = switch (raw_key) { 2723 .scroll_up => .arrow_up, 2724 .scroll_down => .arrow_down, 2725 else => raw_key, 2726 }; 2727 2728 if (self.headerActive()) { 2729 // down from the sub-header re-enters the stack if the selected 2730 // view has something to focus; enter in the search box submits 2731 // the query; other keys move within the sub-header. 2732 if (inp.vertDirection(key) == .down) { 2733 const enterable = if (self.stackSelectedIndex()) |idx| 2734 if (idx == labels_view_index) 2735 !labelsEmpty(self.data) 2736 else if (idx == form_view_index) 2737 // nothing focusable in the unauthorized view 2738 self.viewStack().children.values()[idx] != .unauthorized 2739 else 2740 !windowEmpty(self.window(idx)) 2741 else 2742 false; 2743 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2744 } else if (key == .enter) { 2745 if (try self.header().submittedText(allocator)) |text| { 2746 defer allocator.free(text); 2747 try self.submitSearch(text); 2748 } 2749 } else { 2750 try self.header().input(allocator, key, root_focus); 2751 } 2752 return; 2753 } 2754 if (self.labelsViewActive()) { 2755 self.labelsViewInput(key, root_focus); 2756 return; 2757 } 2758 if (self.formViewActive()) { 2759 if (confirmation(self.data.view) != null) { 2760 try self.confirmationFormInput(allocator, key, root_focus); 2761 } else if (supports_conflicts and self.data.view == .resolve) { 2762 try self.resolveInput(allocator, key, root_focus); 2763 } else { 2764 try self.formInput(allocator, key, root_focus); 2765 } 2766 return; 2767 } 2768 const i = self.selectedSplitIndex() orelse return; 2769 if (self.detailActive(i)) { 2770 // the way back to the list, by enter or a click 2771 if (widget.listBackId(self.detailFrame(i))) |id| { 2772 if (inp.activated(root_focus, id, key)) return self.focusList(i, root_focus); 2773 if (root_focus.grandchild_id == id) return switch (key) { 2774 .arrow_left => self.focusList(i, root_focus), 2775 .arrow_up => self.focusHeader(root_focus), 2776 .arrow_down => _ = self.detail(i).focusFirst(root_focus), 2777 else => {}, 2778 }; 2779 } 2780 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2781 .none => {}, 2782 .list => self.focusList(i, root_focus), 2783 .header => if (widget.listBackId(self.detailFrame(i))) |id| root_focus.setFocus(id) else self.focusHeader(root_focus), 2784 } 2785 } else { 2786 try self.listInput(allocator, i, key, root_focus); 2787 } 2788 } 2789 2790 // navigate to the typed query's results, keeping the status list the 2791 // page shows. an empty box clears a filtered page and does nothing on 2792 // any other one. 2793 fn submitSearch(self: *This, text: []const u8) !void { 2794 if (text.len == 0 and self.data.search == null) return; 2795 const selected = self.header().getSelectedIndex() orelse 0; 2796 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2797 const route = listRoute(self.data.identity, status, "") orelse return; 2798 try self.session.navigate(route.withSearch(text) orelse return); 2799 } 2800 2801 // arrow keys move the label selection; up from the top row crosses to the 2802 // header tabs. 2803 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2804 const tf = self.labelsView(); 2805 const cid = tf.focus.child_id orelse return; 2806 const cur = tf.indexOfFocusId(cid) orelse return; 2807 const count = tf.text_boxes.items.len; 2808 switch (key) { 2809 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2810 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2811 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2812 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2813 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2814 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2815 else => {}, 2816 } 2817 } 2818 2819 fn listInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 2820 // up/down (and the scroll wheel) move the selection a row; page up/down 2821 // jump a fixed amount. right/Enter cross into the detail pane. up from 2822 // the top row crosses into the header tabs. 2823 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2824 const lb = self.listBox(index); 2825 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2826 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2827 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2828 return; 2829 } 2830 switch (key) { 2831 .enter, .arrow_right => self.focusDetail(index, root_focus), 2832 // when the window is too narrow to lay out the detail pane beside 2833 // the list, a click on a row opens it like enter. the row was just 2834 // selected, so the detail is swapped to it ahead of the build. 2835 .mouse => |mouse| if (self.resultsBox(index).children.values()[detail_index].rect == null and widget.clickOnSelectedRow(self.listBox(index), root_focus, mouse)) { 2836 try self.refreshDetail(allocator, index); 2837 self.focusDetail(index, root_focus); 2838 }, 2839 else => {}, 2840 } 2841 } 2842 2843 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2844 const form = self.formBox() orelse return; 2845 const cid = form.getFocus().child_id orelse return; 2846 const cur = form.children.getIndex(cid) orelse return; 2847 const child = &form.children.values()[cur]; 2848 2849 if (child.widget == .text_input) { 2850 const text_input = &child.widget.text_input; 2851 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2852 return child.widget.input(allocator, key, root_focus); 2853 } 2854 2855 const on_submit = child.widget == .submit_button; 2856 switch (key) { 2857 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2858 root_focus.setFocus(form.children.keys()[i]) 2859 else 2860 self.focusHeader(root_focus), 2861 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2862 .enter => if (on_submit) return self.submitForm(allocator), 2863 .mouse => |mouse| if (on_submit) { 2864 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2865 }, 2866 else => {}, 2867 } 2868 try child.widget.input(allocator, key, root_focus); 2869 } 2870 2871 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2872 const form = self.confirmationForm() orelse return; 2873 const child_id = form.getFocus().child_id orelse return; 2874 const index = form.children.getIndex(child_id) orelse return; 2875 switch (key) { 2876 .arrow_up, .back_tab => if (index > 1) 2877 root_focus.setFocus(form.children.keys()[index - 1]) 2878 else 2879 self.focusHeader(root_focus), 2880 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2881 .enter => try self.submitConfirmation(allocator, index), 2882 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2883 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2884 try self.submitConfirmation(allocator, button_index); 2885 break; 2886 } 2887 }, 2888 else => {}, 2889 } 2890 } 2891 2892 // the multiline hunk inputs keep enter and any up/down that has a row to 2893 // move to; enter on a "use this" link is a navigation the host follows 2894 // before this runs. 2895 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2896 const form = self.formBox() orelse return; 2897 const cid = form.getFocus().child_id orelse return; 2898 const cur = form.children.getIndex(cid) orelse return; 2899 const child = &form.children.values()[cur]; 2900 2901 if (child.widget == .text_input) { 2902 const ti = &child.widget.text_input; 2903 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2904 return child.widget.input(allocator, key, root_focus); 2905 } 2906 2907 const on_submit = child.widget == .submit_button; 2908 2909 switch (key) { 2910 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2911 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2912 .arrow_left, .arrow_right => switch (child.widget) { 2913 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2914 else => try child.widget.input(allocator, key, root_focus), 2915 }, 2916 .enter => if (on_submit) try self.submitResolution(allocator), 2917 .mouse => |mouse| if (on_submit) { 2918 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2919 }, 2920 else => try child.widget.input(allocator, key, root_focus), 2921 } 2922 } 2923 2924 // a multiline input keeps enter and any up/down that has a row to move to 2925 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2926 return switch (key) { 2927 .enter => true, 2928 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2929 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2930 else => false, 2931 }; 2932 } 2933 2934 // the neighboring form control, skipping spacers and blank rows 2935 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2936 var i = cur; 2937 while (true) { 2938 if (down) { 2939 i += 1; 2940 if (i >= form.children.count()) return null; 2941 } else { 2942 if (i == 0) return null; 2943 i -= 1; 2944 } 2945 const child = &form.children.values()[i]; 2946 if (child.hidden) continue; 2947 switch (child.widget) { 2948 .spacer, .text => continue, 2949 else => return i, 2950 } 2951 } 2952 } 2953 2954 // focus the resolve-form child at `i` (a version row focuses its selected 2955 // child) and keep it visible on the terminal 2956 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2957 const child = &form.children.values()[i]; 2958 switch (child.widget) { 2959 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2960 else => root_focus.setFocus(form.children.keys()[i]), 2961 } 2962 if (self.session.is_terminal) { 2963 if (self.resolveScroll()) |sc| { 2964 if (child.rect) |rect| sc.scrollToRect(rect); 2965 } 2966 } 2967 } 2968 2969 // re-emit the conflicted thread's event with the resolved content; any 2970 // event on the thread settles its conflict entry. this is the terminal 2971 // path; the web posts the form to the resolve route. 2972 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2973 if (comptime wasm) return; 2974 const io = self.session.io orelse return; 2975 const src = self.data.repo_source orelse return; 2976 const entry = self.data.selectedThread() orelse return; 2977 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2978 const form = self.formBox() orelse return; 2979 2980 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2981 var title: ?[]u8 = null; 2982 var labels: ?[]u8 = null; 2983 var hunks: std.ArrayList([]const u8) = .empty; 2984 defer { 2985 if (title) |t| allocator.free(t); 2986 if (labels) |t| allocator.free(t); 2987 for (hunks.items) |h| allocator.free(h); 2988 hunks.deinit(allocator); 2989 } 2990 for (form.children.values()) |*child| switch (child.widget) { 2991 .text_input => |*ti| { 2992 const text = try ti.text(allocator); 2993 errdefer allocator.free(text); 2994 if (std.mem.eql(u8, ti.options.name, "title")) { 2995 title = text; 2996 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2997 labels = text; 2998 } else { 2999 try hunks.append(allocator, text); 3000 } 3001 }, 3002 else => {}, 3003 }; 3004 3005 const id_bytes = try evt.parseEventId(entry.id); 3006 3007 switch (src.repo_kind) { 3008 inline else => |repo_kind| { 3009 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3010 defer any_repo.deinit(io, allocator); 3011 switch (any_repo) { 3012 inline else => |*repo| { 3013 var resolution: Event.Resolve = .{ 3014 .title = title, 3015 .labels = labels, 3016 .hunks = hunks.items, 3017 }; 3018 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 3019 const change = Event.Update{ .resolve = resolution }; 3020 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 3021 // leave the form up for correction 3022 error.InvalidFields => return, 3023 else => |e| return e, 3024 }; 3025 }, 3026 } 3027 }, 3028 } 3029 3030 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3031 try self.session.navigate(route); 3032 } 3033 3034 // commit a new or edited comment and navigate to its permalink. this is the 3035 // terminal path; the web posts the form to its comment route. 3036 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 3037 if (comptime wasm) return; 3038 const io = self.session.io orelse return; 3039 const src = self.data.repo_source orelse return; 3040 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3041 const author = actor.author; 3042 const form = self.formBox() orelse return; 3043 const body_input = try formField(form, "body"); 3044 const body = try body_input.text(allocator); 3045 defer allocator.free(body); 3046 if (!evt.Comment.fieldsValid(body)) return; 3047 3048 var thread_id: [evt.event_id_size * 2]u8 = undefined; 3049 @memcpy(&thread_id, self.data.selected_id); 3050 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 3051 var parent: [evt.event_id_size * 2]u8 = undefined; 3052 @memcpy(&parent, self.data.comment_id); 3053 break :blk parent; 3054 }; 3055 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3056 const editing = self.data.view == .edit_comment; 3057 if (editing) { 3058 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3059 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3060 } 3061 switch (src.repo_kind) { 3062 inline else => |repo_kind| { 3063 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3064 defer any_repo.deinit(io, allocator); 3065 switch (any_repo) { 3066 inline else => |*repo| if (editing) { 3067 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3068 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3069 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3070 } else { 3071 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &parent_id, body, author); 3072 }, 3073 } 3074 }, 3075 } 3076 3077 body_input.clear(allocator); 3078 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3079 try self.session.navigate(route); 3080 } 3081 3082 // terminal form submissions; the web posts to the forms' routes instead 3083 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3084 switch (self.data.view) { 3085 .new_comment, .edit_comment => try self.submitComment(allocator), 3086 .edit => try self.submitEditedThread(allocator), 3087 else => try self.submitNewThread(allocator), 3088 } 3089 } 3090 3091 // commit the new thread to the repo's events branch and navigate to it. 3092 // this is the terminal path; the web posts the form to the thread route, 3093 // so the wasm side never runs (or compiles) the repo access below. 3094 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3095 if (comptime wasm) return; 3096 const io = self.session.io orelse return; 3097 const src = self.data.repo_source orelse return; 3098 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3099 3100 const form = self.threadForm() orelse return; 3101 const title_input = try formField(form, "title"); 3102 const labels_input = try formField(form, "labels"); 3103 const description_input = try formField(form, "description"); 3104 3105 const title = try title_input.text(allocator); 3106 defer allocator.free(title); 3107 const labels = try labels_input.text(allocator); 3108 defer allocator.free(labels); 3109 const description = try description_input.text(allocator); 3110 defer allocator.free(description); 3111 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3112 defer if (comptime supports_drafts) allocator.free(target_branch); 3113 3114 if (!Event.fieldsValid(title, labels)) { 3115 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3116 return; 3117 } 3118 3119 if (comptime supports_drafts) { 3120 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3121 const source_branch = switch (source.selectedKind()) { 3122 .fork => null, 3123 .branch => try source.field().text(allocator), 3124 }; 3125 defer if (source_branch) |branch| allocator.free(branch); 3126 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3127 const failure = FeedbackFailure.fromError(err) orelse return err; 3128 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3129 return; 3130 }; 3131 title_input.clear(allocator); 3132 labels_input.clear(allocator); 3133 description_input.clear(allocator); 3134 (try formField(form, "target_branch")).clear(allocator); 3135 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3136 try self.session.navigate(route); 3137 return; 3138 } 3139 3140 var id_bytes: [evt.event_id_size]u8 = undefined; 3141 io.random(&id_bytes); 3142 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3143 3144 const event = evt.EventWithId{ 3145 .id = event_id_hex, 3146 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3147 .author = actor.author, 3148 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3149 .title = title, 3150 .description = description, 3151 .labels = labels, 3152 }), 3153 }; 3154 3155 switch (src.repo_kind) { 3156 inline else => |repo_kind| { 3157 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3158 defer any_repo.deinit(io, allocator); 3159 switch (any_repo) { 3160 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3161 } 3162 }, 3163 } 3164 3165 // wipe the form so a return visit starts fresh 3166 title_input.clear(allocator); 3167 labels_input.clear(allocator); 3168 description_input.clear(allocator); 3169 3170 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), &event_id_hex) orelse return; 3171 try self.session.navigate(route); 3172 } 3173 3174 // re-emit the selected thread's event with the form's content (status 3175 // preserved), then reload the thread's page. this is the terminal path; 3176 // the web posts the form to the edit route. 3177 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3178 if (comptime wasm) return; 3179 const io = self.session.io orelse return; 3180 const entry = self.data.selectedThread() orelse return; 3181 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3182 const author = actor.author; 3183 3184 const form = self.threadForm() orelse return; 3185 const title_input = try formField(form, "title"); 3186 const labels_input = try formField(form, "labels"); 3187 const description_input = try formField(form, "description"); 3188 3189 const title = try title_input.text(allocator); 3190 defer allocator.free(title); 3191 const labels = try labels_input.text(allocator); 3192 defer allocator.free(labels); 3193 const description = try description_input.text(allocator); 3194 defer allocator.free(description); 3195 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3196 defer if (comptime supports_drafts) allocator.free(target_branch); 3197 3198 if (!Event.fieldsValid(title, labels)) { 3199 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3200 return; 3201 } 3202 const src = self.data.repo_source orelse return; 3203 3204 if (comptime supports_drafts) { 3205 if (entryDraft(entry.*)) { 3206 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3207 const failure = FeedbackFailure.fromError(err) orelse return err; 3208 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3209 return; 3210 }; 3211 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3212 try self.session.navigate(route); 3213 return; 3214 } 3215 } 3216 3217 const id_bytes = try evt.parseEventId(entry.id); 3218 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3219 3220 switch (src.repo_kind) { 3221 inline else => |repo_kind| { 3222 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3223 defer any_repo.deinit(io, allocator); 3224 switch (any_repo) { 3225 inline else => |*repo| if (supports_drafts) { 3226 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3227 .title = title, 3228 .labels = labels, 3229 .description = description, 3230 .target_branch = target_branch, 3231 } }, author) catch |err| { 3232 const failure = FeedbackFailure.fromError(err) orelse return err; 3233 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3234 return; 3235 }; 3236 } else if (has_status) 3237 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3238 .title = title, 3239 .labels = labels, 3240 .description = description, 3241 } }, author) 3242 else 3243 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3244 } 3245 }, 3246 } 3247 3248 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3249 try self.session.navigate(route); 3250 } 3251 3252 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3253 const aa = self.session.arena.allocator(); 3254 const source = if (supports_drafts) blk: { 3255 const form = self.threadForm() orelse break :blk null; 3256 break :blk formWidget(form, .patch_source); 3257 } else null; 3258 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3259 .failure = failure, 3260 .fields = if (comptime supports_drafts) .{ 3261 .title = try aa.dupe(u8, title), 3262 .labels = try aa.dupe(u8, labels), 3263 .description = try aa.dupe(u8, description), 3264 .target_branch = try aa.dupe(u8, target_branch), 3265 .source_branch = if (source) |value| try value.field().text(aa) else "", 3266 .source_kind = if (source) |value| value.selectedKind() else .fork, 3267 } else .{ 3268 .title = try aa.dupe(u8, title), 3269 .labels = try aa.dupe(u8, labels), 3270 .description = try aa.dupe(u8, description), 3271 }, 3272 }); 3273 } 3274 3275 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3276 switch (confirmation(self.data.view) orelse return) { 3277 .publish => if (comptime supports_drafts) { 3278 if (comptime wasm) return; 3279 const entry = self.data.selectedThread() orelse return; 3280 if (!entryDraft(entry.*)) return; 3281 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3282 try self.data.publishDraft(self.session, allocator, author, entry.id); 3283 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), entry.id) orelse return; 3284 try self.session.navigate(route); 3285 } else unreachable, 3286 .merge => if (comptime supports_merge) { 3287 if (comptime wasm) return; 3288 const entry = self.data.selectedThread() orelse return; 3289 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3290 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3291 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3292 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3293 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3294 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3295 try self.session.navigate(route); 3296 return; 3297 }, 3298 else => return err, 3299 }; 3300 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3301 const route = listRoute(self.data.identity, merged, entry.id) orelse return; 3302 try self.session.navigate(route); 3303 } else unreachable, 3304 .remove => try self.submitRemove(allocator), 3305 } 3306 } 3307 3308 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3309 const comment = self.data.comment_id.len != 0; 3310 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3311 const id = evt.parseEventId(event_id) catch return; 3312 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3313 } 3314 3315 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3316 if (comptime wasm) return; 3317 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3318 const author = actor.author; 3319 3320 if (comptime supports_drafts) { 3321 const selected = self.data.selectedThread() orelse return; 3322 if (event_kind == kind and entryDraft(selected.*)) { 3323 try Data.removeDraft(self.session, allocator, author, &id); 3324 const route = draftsRoute(self.data.identity) orelse return; 3325 try self.session.navigate(route); 3326 return; 3327 } 3328 } 3329 3330 const io = self.session.io orelse return; 3331 const src = self.data.repo_source orelse return; 3332 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3333 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3334 3335 switch (src.repo_kind) { 3336 inline else => |repo_kind| { 3337 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3338 defer any_repo.deinit(io, allocator); 3339 switch (any_repo) { 3340 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 3341 } 3342 }, 3343 } 3344 3345 const id_hex = std.fmt.bytesToHex(id, .lower); 3346 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3347 try self.session.navigate(route); 3348 } 3349 3350 // enter the detail pane, landing on the way back to the list when it shows 3351 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3352 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3353 _ = self.detail(index).focusFirst(root_focus); 3354 } 3355 3356 // return to the list. 3357 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3358 root_focus.setFocus(self.listScroll(index).getFocus().id); 3359 } 3360 3361 // cross to the header tabs above the stack. 3362 fn focusHeader(self: *This, root_focus: *Focus) void { 3363 root_focus.setFocus(self.box.children.keys()[header_index]); 3364 } 3365 3366 pub fn clearGrid(self: *This) void { 3367 self.box.clearGrid(); 3368 } 3369 3370 pub fn getGrid(self: This) ?Grid { 3371 return self.box.getGrid(); 3372 } 3373 3374 pub fn getFocus(self: *This) *Focus { 3375 return self.box.getFocus(); 3376 } 3377 3378 // for the parent's "scroll up at the top jumps to the header" check: at the 3379 // top only while the header tabs hold focus, so up from the split 3380 // crosses into the tabs first. 3381 pub fn atTop(self: *This) bool { 3382 return self.headerActive(); 3383 } 3384 }; 3385 } 3386