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