╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1 const std = @import("std"); 2 const ui = @import("../ui.zig"); 3 const xit = @import("xit"); 4 const xitui = xit.xitui; 5 const wgt = xitui.widget; 6 const layout = xitui.layout; 7 const Key = xitui.input.Key; 8 const Grid = xitui.grid.Grid; 9 const Focus = xitui.focus.Focus; 10 const inp = @import("input.zig"); 11 12 pub const thread = @import("widget/thread.zig"); 13 14 pub const Widget = union(enum) { 15 text: wgt.Text, 16 box: wgt.Box(Widget), 17 text_box: wgt.TextBox, 18 text_input: wgt.TextInput, 19 scroll: wgt.Scroll(Widget), 20 stack: wgt.Stack(Widget), 21 flow_box: FlowBox, 22 flow_box_scroll: FlowBox.Scroll, 23 word_flow: WordFlow, 24 rich_text: RichText, 25 spacer: Spacer, 26 center: Center, 27 section_label: SectionLabel, 28 submit_button: SubmitButton, 29 background: AnsiBackground, 30 home: ui.Home.View, 31 user: ui.User.View, 32 repo: ui.Repo.View, 33 fork: ui.Fork.View, 34 quit: ui.Quit.View, 35 unauthorized: ui.Unauthorized.View, 36 not_found: ui.NotFound.View, 37 title: ui.Title.View, 38 sub_title: ui.SubTitle.View, 39 home_header: ui.Home.Header.View, 40 user_header: ui.User.Header.View, 41 user_repos: ui.User.ReposView, 42 repo_header: ui.Repo.Header.View, 43 fork_header: ui.Fork.Header.View, 44 search_box: SearchBox, 45 radio: Radio, 46 search_header: SearchHeader, 47 header_scroll: HeaderScroll, 48 repo_issues_header: ui.Repo.Issues.Header, 49 repo_patches_header: ui.Repo.Patches.Header, 50 repo_discussions_header: ui.Repo.Discussions.Header, 51 repo_files: ui.Repo.Files.View, 52 markdown: ui.Repo.Markdown.View, 53 markdown_heading: ui.Repo.Markdown.Heading, 54 markdown_gutter: ui.Repo.Markdown.Gutter, 55 markdown_frame: ui.Repo.Markdown.Frame, 56 repo_commits: ui.Repo.Commits.View, 57 diff_view: ui.Repo.Diff.View, 58 repo_refs: ui.Repo.Refs.View, 59 repo_issues: ui.Repo.Issues.View, 60 repo_patches: ui.Repo.Patches.View, 61 repo_discussions: ui.Repo.Discussions.View, 62 repo_issue_detail: ui.Repo.Issues.Detail, 63 repo_patch_detail: ui.Repo.Patches.Detail, 64 repo_discussion_detail: ui.Repo.Discussions.Detail, 65 repo_events_header: ui.Repo.Events.Header.View, 66 repo_events: ui.Repo.Events.View, 67 repo_undo: ui.Repo.Undo.View, 68 repo_settings: ui.Repo.Settings.View, 69 repo_settings_form: ui.Repo.Settings.Form, 70 repo_members: ui.Repo.Members.View, 71 repo_branches: ui.Repo.Branches.View, 72 repo_comment: ui.Repo.Comment.Item, 73 copyable_text: CopyableText, 74 patch_source: ui.Repo.Patches.Source, 75 home_about: ui.Home.About.View, 76 home_users: ui.Home.Users.View, 77 new_repo: ui.NewRepo.View, 78 new_user: ui.NewUser.View, 79 user_login: ui.UserLogin.View, 80 user_settings: ui.UserSettings.View, 81 user_keys: ui.UserKeys.View, 82 user_notifications: ui.UserNotifications.View, 83 delete_form: ui.Delete.View, 84 footer: Footer, 85 86 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 87 switch (self.*) { 88 inline else => |*case| case.deinit(allocator), 89 } 90 } 91 92 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 93 switch (self.*) { 94 inline else => |*case| try case.build(allocator, constraint, root_focus), 95 } 96 } 97 98 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 99 switch (self.*) { 100 inline else => |*case| try case.input(allocator, key, root_focus), 101 } 102 } 103 104 pub fn clearGrid(self: *Widget) void { 105 switch (self.*) { 106 inline else => |*case| case.clearGrid(), 107 } 108 } 109 110 pub fn getGrid(self: Widget) ?Grid { 111 switch (self) { 112 inline else => |*case| return case.getGrid(), 113 } 114 } 115 116 pub fn getFocus(self: *Widget) *Focus { 117 switch (self.*) { 118 inline else => |*case| return case.getFocus(), 119 } 120 } 121 122 // whether moving up should return to the page header 123 pub fn atTop(self: *Widget, root_focus: *Focus) bool { 124 return switch (self.*) { 125 inline .home_about, .home_users, .user_repos, .flow_box_scroll, .new_repo, .new_user, .user_login, .user_notifications, .quit, .repo_files, .repo_commits, .diff_view, .repo_refs, .repo_issues, .repo_patches, .repo_discussions, .repo_events, .repo_undo, .repo_settings, .repo_settings_form, .repo_members, .repo_branches, .user_keys => |*view| view.atTop(), 126 inline .repo_patch_detail, .user_settings, .delete_form => |*view| view.atTop(root_focus), 127 else => false, 128 }; 129 } 130 }; 131 132 // whether `mouse` is a left click on the selected row of `list`. the host 133 // selects a clicked row before forwarding the press. 134 pub fn clickOnSelectedRow(list: *wgt.Box(Widget), root_focus: *Focus, mouse: xitui.input.Mouse) bool { 135 const cid = list.getFocus().child_id orelse return false; 136 return inp.leftClickOn(root_focus, cid, mouse); 137 } 138 139 // a detail pane's way back to a list that isn't shown beside it. it leads the 140 // pane's frame, above the pane's content, hidden while the list shows. 141 const list_back_index = 0; 142 pub const list_pane_index = 1; 143 144 pub fn addListBack(allocator: std.mem.Allocator, frame: *wgt.Box(Widget), label: []const u8) !void { 145 var back = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 146 errdefer back.deinit(allocator); 147 back.getFocus().mode = .all; 148 try frame.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null, .hidden = true }); 149 } 150 151 // point the way back at the shown list row's in-page link, so both hosts hand 152 // its press and enter to the view instead of navigating 153 pub fn setListBackLink(frame: *wgt.Box(Widget), link: []const u8) void { 154 frame.children.values()[list_back_index].widget.text_box.getFocus().kind = .{ .custom = link }; 155 } 156 157 pub fn showListBack(frame: *wgt.Box(Widget), shown: bool) void { 158 frame.children.values()[list_back_index].hidden = !shown; 159 if (!shown and frame.getFocus().child_id == frame.children.keys()[list_back_index]) 160 frame.getFocus().child_id = frame.children.keys()[list_pane_index]; 161 } 162 163 // the way back's focus id while it shows 164 pub fn listBackId(frame: *wgt.Box(Widget)) ?usize { 165 return if (frame.children.values()[list_back_index].hidden) null else frame.children.keys()[list_back_index]; 166 } 167 168 // enter the pane on the way back. selecting it (then focusing the frame) also 169 // handles the too-narrow case where the pane isn't laid out yet. 170 pub fn focusListBack(frame: *wgt.Box(Widget), root_focus: *Focus) void { 171 frame.getFocus().child_id = frame.children.keys()[list_back_index]; 172 root_focus.setFocus(frame.getFocus().id); 173 } 174 175 pub const back_button_width = 3; 176 const back_button_height = 3; 177 const back_kind = "back"; 178 179 // draw a text box as the selected one of its group: bordered and inverted, 180 // while the rest stay borderless 181 pub fn markSelected(text_box: *wgt.TextBox, selected: bool) void { 182 text_box.options.border = if (selected) .single else .hidden; 183 text_box.options.invert = selected; 184 } 185 186 // explanatory text atop a view's rows: a frame that takes focus like a row, so 187 // the arrows scroll it into view, then a blank line 188 pub fn initAbout(allocator: std.mem.Allocator, spans: []const RichText.Span) !wgt.Box(Widget) { 189 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 190 errdefer row.deinit(allocator); 191 { 192 var frame = try wgt.Box(Widget).init(allocator, .{ .border = .single, .round_corners = true, .direction = .vert, .top_label = .{ .text = " note " } }); 193 errdefer frame.deinit(allocator); 194 frame.getFocus().mode = .all; 195 { 196 var text = try RichText.init(allocator, spans); 197 errdefer text.deinit(allocator); 198 try frame.children.put(allocator, text.getFocus().id, .{ .widget = .{ .rich_text = text }, .rect = null, .min_size = null }); 199 } 200 row.getFocus().child_id = frame.getFocus().id; 201 try row.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = null }); 202 } 203 { 204 var blank = try wgt.Text.init(allocator, " "); 205 errdefer blank.deinit(allocator); 206 try row.children.put(allocator, blank.getFocus().id, .{ .widget = .{ .text = blank }, .rect = null, .min_size = null }); 207 } 208 return row; 209 } 210 211 // double the about frame's border while it has focus 212 pub fn markAboutFocused(row: *wgt.Box(Widget), root_focus: *const Focus) void { 213 const frame = &row.children.values()[0].widget.box; 214 frame.options.border = if (root_focus.grandchild_id == frame.getFocus().id) .double else .single; 215 } 216 217 pub fn addBackButton(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session) !void { 218 if (!session.is_terminal) return; 219 var back = try wgt.TextBox.init(allocator, "←", .{ .border = .hidden, .wrap_kind = .none }); 220 errdefer back.deinit(allocator); 221 back.getFocus().kind = .{ .custom = back_kind }; 222 back.getFocus().mode = .mouse; 223 try box.children.put(allocator, back.getFocus().id, .{ 224 .widget = .{ .text_box = back }, 225 .rect = null, 226 .min_size = .{ .width = back_button_width, .height = back_button_height }, 227 .max_size = .{ .width = back_button_width, .height = back_button_height }, 228 .hidden = session.back != .available, 229 }); 230 } 231 232 pub fn setBackButtonVisible(box: *wgt.Box(Widget), visible: bool) void { 233 for (box.children.values()) |*child| { 234 if (isBackButton(child.widget.getFocus())) { 235 child.hidden = !visible; 236 continue; 237 } 238 switch (child.widget) { 239 .box => |*nested| setBackButtonVisible(nested, visible), 240 else => {}, 241 } 242 } 243 } 244 245 pub fn isBackButton(focus: *Focus) bool { 246 return switch (focus.kind) { 247 .custom => |kind| std.mem.eql(u8, kind, back_kind), 248 else => false, 249 }; 250 } 251 252 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 253 // `scroll` to keep it visible. in the terminal, a row taller than the view is 254 // scrolled through before focus leaves it. 255 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 256 const keys = box.children.keys(); 257 if (keys.len == 0) return; 258 const cur_id = box.getFocus().child_id orelse return; 259 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 260 // home and end jump straight to the ends 261 if (@abs(delta) < keys.len) if (box.children.values()[@intCast(cur)].rect) |rect| if (scrollThroughRow(scroll, rect, delta)) return; 262 const last: isize = @intCast(keys.len - 1); 263 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 264 if (next == @as(usize, @intCast(cur))) return; 265 root_focus.setFocus(keys[next]); 266 if (box.children.values()[next].rect) |rect| scrollToEnteredRow(scroll, rect, delta > 0); 267 } 268 269 // bring `rect`, a row in `scroll`'s content just entered from above (`down`) 270 // or below, into view. a row taller than the view keeps the view while the 271 // edge it was entered from is in it, so the following steps scroll through it, 272 // and otherwise shows that edge. 273 pub fn scrollToEnteredRow(scroll: *wgt.Scroll(Widget), rect: layout.IRect, down: bool) void { 274 const grid = scroll.grid orelse return; 275 // a row wider than the view keeps the horizontal position 276 const target: layout.IRect = if (rect.size.width > grid.size.width - scroll.bar_w) .{ .x = scroll.x, .y = rect.y, .size = .{ .width = 0, .height = rect.size.height } } else rect; 277 const h: isize = @intCast(grid.size.height - scroll.bar_h); 278 if (@as(isize, @intCast(rect.size.height)) <= h) return scroll.scrollToRect(target); 279 const edge = if (down) rect.y else rectBottom(rect) - 1; 280 if (edge >= scroll.y and edge < scroll.y + h) return; 281 scroll.y = if (down) rect.y else rectBottom(rect) - h; 282 } 283 284 // step across the boxes of `box`'s selected row, false when it isn't a row or 285 // has no box that way. 286 pub fn moveInSelectedRow(box: *wgt.Box(Widget), root_focus: *Focus, right: bool) bool { 287 const selected = box.getFocus().child_id orelse return false; 288 return switch ((box.children.getPtr(selected) orelse return false).widget) { 289 .box => |*row| moveInRow(row, root_focus, right), 290 else => false, 291 }; 292 } 293 294 // scroll a step through `rect`, a row in `scroll`'s content, when it extends 295 // past the view in `delta`'s direction. false when it doesn't, and on the web, 296 // where scrollFocusedWidget in script.js handles scrolling. 297 pub fn scrollThroughRow(scroll: *wgt.Scroll(Widget), rect: layout.IRect, delta: isize) bool { 298 const step_lines = 3; // how far a step scrolls (see stepLines in script.js for the web equivalent) 299 const h = viewHeight(scroll) orelse return false; 300 const hidden = if (delta > 0) rectBottom(rect) - (scroll.y + h) else scroll.y - rect.y; 301 if (hidden <= 0) return false; 302 scroll.y += std.math.clamp(delta * step_lines, -hidden, hidden); 303 return true; 304 } 305 306 // the terminal view's height, or null on the web 307 fn viewHeight(scroll: *wgt.Scroll(Widget)) ?isize { 308 if (scroll.options.web_native) return null; 309 const grid = scroll.grid orelse return null; 310 return @intCast(grid.size.height - scroll.bar_h); 311 } 312 313 fn rectBottom(rect: layout.IRect) isize { 314 return rect.y + @as(isize, @intCast(rect.size.height)); 315 } 316 317 // focus the box beside the selected one in `row`, false when there is none that way. 318 pub fn moveInRow(row: *wgt.Box(Widget), root_focus: *Focus, right: bool) bool { 319 const selected = row.getFocus().child_id orelse return false; 320 const index = row.children.getIndex(selected) orelse return false; 321 const target = if (right) index + 1 else index -| 1; 322 if (target == index or target >= row.children.count()) return false; 323 root_focus.setFocus(row.children.keys()[target]); 324 return true; 325 } 326 327 // a repo tab's placeholder while the repo has no commits, with the remote to 328 // push to for a viewer who can 329 pub fn initNoCommits(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !Center { 330 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 331 errdefer box.deinit(allocator); 332 333 // built first so the text can center over it 334 var remote_maybe: ?CopyableText = if (session.data.host_kind == .server and handle.canWrite()) if (session.data.git_ssh_url) |ssh_url| blk: { 335 // only repo pages flag a repo without commits 336 const identity = switch (handle.location) { 337 .repo => |identity| identity, 338 .fork => unreachable, 339 }; 340 const aa = session.page_arena.allocator(); 341 const url = try aa.print("{s}/{s}", .{ ssh_url, identity }); 342 break :blk try CopyableText.init(allocator, session, &.{.{ 343 .text = url, 344 .copyable_text = try aa.print("git remote add origin {s}", .{url}), 345 .label = " git remote add origin ", 346 }}, 0); 347 } else null else null; 348 errdefer if (remote_maybe) |*remote| remote.deinit(allocator); 349 350 { 351 var text_widget: Widget = blk: { 352 var text_box = try wgt.TextBox.init(allocator, "no commits yet", .{ .border = null, .wrap_kind = .none }); 353 errdefer text_box.deinit(allocator); 354 break :blk if (remote_maybe != null) .{ .center = try Center.init(allocator, .{ .text_box = text_box }) } else .{ .text_box = text_box }; 355 }; 356 errdefer text_widget.deinit(allocator); 357 // over the remote widget, the text centers across its width 358 const size: ?layout.MaybeSize = if (remote_maybe) |*remote| .{ .width = remote.minWidth(), .height = 1 } else null; 359 try box.children.put(allocator, text_widget.getFocus().id, .{ .widget = text_widget, .rect = null, .min_size = size, .max_size = size }); 360 } 361 362 if (remote_maybe) |*remote| { 363 { 364 var gap = try wgt.Text.init(allocator, " "); 365 errdefer gap.deinit(allocator); 366 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 367 } 368 const min_width = remote.minWidth(); 369 box.getFocus().child_id = remote.getFocus().id; 370 try box.children.put(allocator, remote.getFocus().id, .{ .widget = .{ .copyable_text = remote.* }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 371 // the box owns it now 372 remote_maybe = null; 373 } 374 375 return Center.init(allocator, .{ .box = box }); 376 } 377 378 const empty_label = "nothing to see here"; 379 380 // the message a list or tab shows in place of its empty first window 381 pub fn initEmpty(allocator: std.mem.Allocator) !Center { 382 var text_box = try wgt.TextBox.init(allocator, empty_label, .{ .border = null, .wrap_kind = .none }); 383 errdefer text_box.deinit(allocator); 384 return Center.init(allocator, .{ .text_box = text_box }); 385 } 386 387 pub const ConfirmationButton = struct { 388 label: []const u8, 389 bottom_label: []const u8 = "", 390 action: []const u8 = "submit", 391 }; 392 393 // "are you sure?" over the buttons, in a form posting to `action` 394 pub fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 395 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 396 errdefer box.deinit(allocator); 397 box.getFocus().kind = .{ .custom = action }; 398 399 var prompt = try wgt.Text.init(allocator, "are you sure?"); 400 errdefer prompt.deinit(allocator); 401 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 402 403 for (buttons, 0..) |options, index| { 404 var button = try wgt.TextBox.init(allocator, options.label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = options.bottom_label } }); 405 errdefer button.deinit(allocator); 406 button.getFocus().mode = .all; 407 button.getFocus().kind = .{ .custom = options.action }; 408 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 409 if (index == 0) box.getFocus().child_id = button.getFocus().id; 410 } 411 412 return Center.init(allocator, .{ .box = box }); 413 } 414 415 pub const FlowBox = struct { 416 focus: *Focus, 417 grid: ?Grid, 418 text_boxes: std.ArrayList(wgt.TextBox), 419 // backs each link item's `.custom` focus kind (e.g. "a:/user/foo"). reset 420 // wholesale on each setItems so there's no per-string ownership to track. 421 arena: std.heap.ArenaAllocator, 422 // column count from the last build — FlowBox.Scroll.input uses it so arrow 423 // up/down can step by a row's worth of items. 424 last_cols: usize, 425 options: Options, 426 427 const border_rows: usize = 2; 428 429 pub const Options = struct { 430 cell_width: usize = 40, 431 cell_height: usize = 3, 432 }; 433 434 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 435 return .{ 436 .focus = try Focus.create(allocator, .container), 437 .grid = null, 438 .text_boxes = .empty, 439 .arena = std.heap.ArenaAllocator.init(allocator), 440 .last_cols = 1, 441 .options = options, 442 }; 443 } 444 445 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 446 self.focus.destroy(allocator); 447 if (self.grid) |*grid| { 448 grid.deinit(); 449 self.grid = null; 450 } 451 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 452 self.text_boxes.deinit(allocator); 453 self.arena.deinit(); 454 } 455 456 // one flow item: its display text plus an optional link and bottom label. a 457 // non-empty `link` sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, 458 // which the web renderer turns into an anchor. `bold` covers the text and the label. 459 pub const Item = struct { text: []const u8, link: []const u8 = "", bottom_label: []const u8 = "", bold: bool = false }; 460 461 // the text box copies the item text; links and bottom labels are copied into 462 // the arena, so the caller's slices needn't outlive this call. 463 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 464 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 465 self.text_boxes.clearAndFree(allocator); 466 467 // the old text boxes and their borrowed links are gone now, so the link 468 // arena can be reclaimed. 469 _ = self.arena.reset(.retain_capacity); 470 const aa = self.arena.allocator(); 471 472 self.focus.clear(); 473 self.focus.child_id = null; 474 475 for (items) |item| { 476 const style: Grid.Style = .{ .bold = item.bold }; 477 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = try aa.dupe(u8, item.bottom_label), .style = style }, .style = style }); 478 errdefer text_box.deinit(allocator); 479 text_box.getFocus().mode = .all; 480 481 if (item.link.len > 0) { 482 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 483 } 484 485 try self.text_boxes.append(allocator, text_box); 486 } 487 488 if (self.text_boxes.items.len > 0) { 489 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 490 } 491 } 492 493 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 494 self.clearGrid(); 495 self.focus.clear(); 496 497 const cell_width = self.options.cell_width; 498 const max_width = constraint.max_size.width orelse cell_width; 499 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 500 self.last_cols = cols; 501 502 const count = self.text_boxes.items.len; 503 if (count == 0) return; 504 const slot_height = self.options.cell_height + border_rows; 505 if (slot_height == 0) return; 506 507 // build at the slot size so every text box fits a single grid cell 508 for (self.text_boxes.items) |*tb| { 509 tb.options.border = if (root_focus.grandchild_id == tb.getFocus().id) .double else .hidden; 510 try tb.build(allocator, .{ 511 .min_size = .{ .width = cell_width, .height = null }, 512 .max_size = .{ .width = cell_width, .height = slot_height }, 513 }, root_focus); 514 } 515 516 const rows = (count + cols - 1) / cols; 517 const content_height = rows * slot_height; 518 const total_width = cols * cell_width; 519 if (total_width == 0 or content_height == 0) return; 520 521 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 522 errdefer grid.deinit(); 523 524 for (self.text_boxes.items, 0..) |*tb, i| { 525 const tb_grid = tb.getGrid() orelse continue; 526 const col = i % cols; 527 const row = i / cols; 528 const cell_x = col * cell_width; 529 const cell_y = row * slot_height; 530 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 531 try grid.drawGrid(tb_grid, cell_x, cell_y); 532 } 533 534 self.grid = grid; 535 } 536 537 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 538 _ = self; 539 _ = allocator; 540 _ = key; 541 _ = root_focus; 542 } 543 544 pub fn clearGrid(self: *FlowBox) void { 545 if (self.grid) |*grid| { 546 grid.deinit(); 547 self.grid = null; 548 } 549 for (self.text_boxes.items) |*tb| tb.clearGrid(); 550 } 551 552 pub fn getGrid(self: FlowBox) ?Grid { 553 return self.grid; 554 } 555 556 pub fn getFocus(self: *FlowBox) *Focus { 557 return self.focus; 558 } 559 560 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 561 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 562 const slot_height = self.options.cell_height + border_rows; 563 const col = index % self.last_cols; 564 const row = index / self.last_cols; 565 return .{ 566 .x = @intCast(col * self.options.cell_width), 567 .y = @intCast(row * slot_height), 568 .size = .{ .width = self.options.cell_width, .height = slot_height }, 569 }; 570 } 571 572 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 573 for (self.text_boxes.items, 0..) |tb, i| { 574 if (tb.focus.id == focus_id) return i; 575 } 576 return null; 577 } 578 579 pub const Scroll = struct { 580 scroll: wgt.Scroll(Widget), 581 582 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 583 var layout_inner = try FlowBox.init(allocator, options); 584 errdefer layout_inner.deinit(allocator); 585 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 586 errdefer scroll.deinit(allocator); 587 return .{ .scroll = scroll }; 588 } 589 590 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 591 self.scroll.deinit(allocator); 592 } 593 594 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 595 self.scroll.x = 0; 596 self.scroll.y = 0; 597 try self.inner().setItems(allocator, items); 598 } 599 600 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 601 try self.scroll.build(allocator, constraint, root_focus); 602 } 603 604 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 605 _ = allocator; 606 const in = self.inner(); 607 const child_id = in.focus.child_id orelse return; 608 const current_index = in.indexOfFocusId(child_id) orelse return; 609 const count = in.text_boxes.items.len; 610 if (count == 0) return; 611 const cols = in.last_cols; 612 const slot_height = in.options.cell_height + FlowBox.border_rows; 613 614 var index = current_index; 615 switch (key) { 616 .arrow_up => index -|= cols, 617 .arrow_down => if (index + cols < count) { 618 index += cols; 619 }, 620 .arrow_left => index -|= 1, 621 .arrow_right => if (index + 1 < count) { 622 index += 1; 623 }, 624 .home => index = 0, 625 .end => index = count - 1, 626 .page_up => { 627 if (self.scroll.grid) |grid| if (slot_height > 0) { 628 const rows_per_page = grid.size.height / slot_height; 629 index -|= rows_per_page * cols; 630 }; 631 }, 632 .page_down => { 633 if (self.scroll.grid) |grid| if (slot_height > 0) { 634 const rows_per_page = grid.size.height / slot_height; 635 index = @min(index + rows_per_page * cols, count - 1); 636 }; 637 }, 638 // scroll wheel moves the focused cell by a full row so the 639 // viewport (via scrollToRect) follows in row-sized steps, 640 // matching how a scroll wheel feels in a grid view. 641 .scroll_up => index -|= cols, 642 .scroll_down => if (index + cols < count) { 643 index += cols; 644 }, 645 else => {}, 646 } 647 648 if (index != current_index) { 649 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 650 if (in.cellRect(index)) |rect| { 651 self.scroll.scrollToRect(rect); 652 } 653 } 654 } 655 656 pub fn clearGrid(self: *Scroll) void { 657 self.scroll.clearGrid(); 658 } 659 660 pub fn getGrid(self: Scroll) ?Grid { 661 return self.scroll.getGrid(); 662 } 663 664 pub fn getFocus(self: *Scroll) *Focus { 665 return self.scroll.getFocus(); 666 } 667 668 pub fn atTop(self: Scroll) bool { 669 const in = self.scroll.child.flow_box; 670 const child_id = in.focus.child_id orelse return false; 671 return in.indexOfFocusId(child_id) == 0; 672 } 673 674 fn inner(self: *Scroll) *FlowBox { 675 return &self.scroll.child.flow_box; 676 } 677 }; 678 }; 679 680 // a left-to-right flow of variable-width focusable items that wraps at the 681 // available width, like words in a paragraph. unlike FlowBox there is no cell 682 // grid: each item is as wide as its text. selection movement is driven by the 683 // owning view via indexOfFocusId/verticalNeighbor. 684 pub const WordFlow = struct { 685 focus: *Focus, 686 grid: ?Grid, 687 text_boxes: std.ArrayList(wgt.TextBox), 688 // last build's item rects (content space), for vertical navigation and 689 // scroll-into-view. 690 rects: std.ArrayList(layout.IRect), 691 // backs each link item's `.custom` focus kind. 692 arena: std.heap.ArenaAllocator, 693 694 pub const Item = struct { text: []const u8, link: []const u8 = "", top_label: []const u8 = "" }; 695 696 pub fn init(allocator: std.mem.Allocator) !WordFlow { 697 return .{ 698 .focus = try Focus.create(allocator, .container), 699 .grid = null, 700 .text_boxes = .empty, 701 .rects = .empty, 702 .arena = std.heap.ArenaAllocator.init(allocator), 703 }; 704 } 705 706 pub fn deinit(self: *WordFlow, allocator: std.mem.Allocator) void { 707 self.focus.destroy(allocator); 708 if (self.grid) |*grid| { 709 grid.deinit(); 710 self.grid = null; 711 } 712 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 713 self.text_boxes.deinit(allocator); 714 self.rects.deinit(allocator); 715 self.arena.deinit(); 716 } 717 718 // the text box copies the item text; links and top labels are copied into the 719 // arena, so the caller's slices needn't outlive this call. 720 pub fn setItems(self: *WordFlow, allocator: std.mem.Allocator, items: []const Item) !void { 721 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 722 self.text_boxes.clearAndFree(allocator); 723 724 _ = self.arena.reset(.retain_capacity); 725 const aa = self.arena.allocator(); 726 727 self.focus.clear(); 728 self.focus.child_id = null; 729 730 for (items) |item| { 731 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = try aa.dupe(u8, item.top_label) } }); 732 errdefer text_box.deinit(allocator); 733 text_box.getFocus().mode = .all; 734 735 if (item.link.len > 0) { 736 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 737 } 738 739 try self.text_boxes.append(allocator, text_box); 740 } 741 742 if (self.text_boxes.items.len > 0) { 743 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 744 } 745 } 746 747 pub fn build(self: *WordFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 748 self.clearGrid(); 749 self.focus.clear(); 750 751 if (self.text_boxes.items.len == 0) return; 752 const max_width = constraint.max_size.width orelse 80; 753 if (max_width == 0) return; 754 755 // lay the items out left to right, wrapping to a new row when the next 756 // one wouldn't fit. 757 self.rects.clearRetainingCapacity(); 758 var x: usize = 0; 759 var y: usize = 0; 760 var row_height: usize = 0; 761 var total_width: usize = 0; 762 for (self.text_boxes.items) |*tb| { 763 try tb.build(allocator, .{ 764 .min_size = .{ .width = null, .height = null }, 765 .max_size = .{ .width = max_width, .height = null }, 766 }, root_focus); 767 const tb_grid_maybe = tb.getGrid(); 768 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 769 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 770 if (x > 0 and x + w > max_width) { 771 x = 0; 772 y += row_height; 773 row_height = 0; 774 } 775 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 776 if (h > row_height) row_height = h; 777 x += w; 778 if (x > total_width) total_width = x; 779 } 780 781 const total_height = y + row_height; 782 if (total_width == 0 or total_height == 0) return; 783 784 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 785 errdefer grid.deinit(); 786 787 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 788 const tb_grid = tb.getGrid() orelse continue; 789 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 790 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 791 } 792 793 self.grid = grid; 794 } 795 796 pub fn input(self: *WordFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 797 _ = self; 798 _ = allocator; 799 _ = key; 800 _ = root_focus; 801 } 802 803 pub fn clearGrid(self: *WordFlow) void { 804 if (self.grid) |*grid| { 805 grid.deinit(); 806 self.grid = null; 807 } 808 for (self.text_boxes.items) |*tb| tb.clearGrid(); 809 } 810 811 pub fn getGrid(self: WordFlow) ?Grid { 812 return self.grid; 813 } 814 815 pub fn getFocus(self: *WordFlow) *Focus { 816 return self.focus; 817 } 818 819 pub fn indexOfFocusId(self: WordFlow, focus_id: usize) ?usize { 820 for (self.text_boxes.items, 0..) |tb, i| { 821 if (tb.focus.id == focus_id) return i; 822 } 823 return null; 824 } 825 826 // the first item of the row below (when `downward`) or above, or null at 827 // the flow's edge. items are laid out in order, so y is nondecreasing. 828 pub fn rowStep(self: WordFlow, index: usize, downward: bool) ?usize { 829 const rects = self.rects.items; 830 if (index >= rects.len) return null; 831 const cur_y = rects[index].y; 832 if (downward) { 833 for (rects[index + 1 ..], index + 1..) |r, i| { 834 if (r.y > cur_y) return i; 835 } 836 return null; 837 } 838 // walk the earlier rows; the last one visited is the row above. 839 var prev_first: ?usize = null; 840 var i: usize = 0; 841 while (i < rects.len and rects[i].y < cur_y) { 842 prev_first = i; 843 const y = rects[i].y; 844 while (i < rects.len and rects[i].y == y) i += 1; 845 } 846 return prev_first; 847 } 848 }; 849 850 // a word-wrapped paragraph of styled runs. each line a link touches is a 851 // focusable piece, and a focused link draws all its pieces inverted. 852 pub const RichText = struct { 853 focus: *Focus, 854 grid: ?Grid, 855 // backs the decoded content and the link targets 856 arena: std.heap.ArenaAllocator, 857 runes: []const u21, 858 // each rune's span 859 span_of: []const usize, 860 styles: []const Grid.Style, 861 // each span's link index, null outside a link 862 span_links: []const ?usize, 863 links: []const []const u8, 864 // each link's piece focuses in line order, kept across builds so a link 865 // keeps its ids when the text rewraps 866 link_focuses: []std.ArrayList(*Focus), 867 lines: std.ArrayList(wgt.Line), 868 // scratch for each build's link pieces 869 pieces: std.ArrayList(Piece), 870 871 pub const Span = struct { 872 text: []const u8, 873 style: Grid.Style = .{}, 874 // the focus kind a click or enter follows; empty for plain text 875 link: []const u8 = "", 876 }; 877 878 const Piece = struct { 879 link: usize, 880 rect: layout.URect, 881 }; 882 883 pub fn init(allocator: std.mem.Allocator, spans: []const Span) !RichText { 884 var arena = std.heap.ArenaAllocator.init(allocator); 885 errdefer arena.deinit(); 886 const aa = arena.allocator(); 887 888 var runes: std.ArrayList(u21) = .empty; 889 var span_of: std.ArrayList(usize) = .empty; 890 const styles = try aa.alloc(Grid.Style, spans.len); 891 const span_links = try aa.alloc(?usize, spans.len); 892 var links: std.ArrayList([]const u8) = .empty; 893 for (spans, 0..) |span, i| { 894 styles[i] = span.style; 895 span_links[i] = null; 896 if (span.link.len > 0) { 897 // adjacent spans with the same target are one link 898 const same = i > 0 and spans[i - 1].link.len > 0 and std.mem.eql(u8, spans[i - 1].link, span.link); 899 if (!same) try links.append(aa, try aa.dupe(u8, span.link)); 900 span_links[i] = links.items.len - 1; 901 } 902 var utf8 = (try std.unicode.Utf8View.init(span.text)).iterator(); 903 while (utf8.nextCodepoint()) |codepoint| { 904 try runes.append(aa, codepoint); 905 try span_of.append(aa, i); 906 } 907 } 908 909 const link_focuses = try aa.alloc(std.ArrayList(*Focus), links.items.len); 910 @memset(link_focuses, .empty); 911 912 return .{ 913 .focus = try Focus.create(allocator, .container), 914 .grid = null, 915 .arena = arena, 916 .runes = runes.items, 917 .span_of = span_of.items, 918 .styles = styles, 919 .span_links = span_links, 920 .links = links.items, 921 .link_focuses = link_focuses, 922 .lines = .empty, 923 .pieces = .empty, 924 }; 925 } 926 927 pub fn deinit(self: *RichText, allocator: std.mem.Allocator) void { 928 self.focus.destroy(allocator); 929 self.clearGrid(); 930 for (self.link_focuses) |*focuses| { 931 for (focuses.items) |focus| focus.destroy(allocator); 932 focuses.deinit(allocator); 933 } 934 self.pieces.deinit(allocator); 935 self.lines.deinit(allocator); 936 self.arena.deinit(); 937 } 938 939 // the url a terminal can open for a link: raw links to absolute urls 940 fn terminalLink(link: []const u8) ?[]const u8 { 941 if (!std.mem.startsWith(u8, link, ui.raw_link_prefix)) return null; 942 const url = link[ui.raw_link_prefix.len..]; 943 return if (std.mem.startsWith(u8, url, "/")) null else url; 944 } 945 946 // the link one of whose pieces has focus id `id` 947 fn linkOf(self: *const RichText, id: ?usize) ?usize { 948 for (self.link_focuses, 0..) |focuses, l| { 949 for (focuses.items) |focus| if (focus.id == id) return l; 950 } 951 return null; 952 } 953 954 pub fn build(self: *RichText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 955 self.clearGrid(); 956 self.focus.clear(); 957 const max_width = constraint.max_size.width; 958 if (max_width == 0 or constraint.max_size.height == 0) return; 959 const focused_link = self.linkOf(root_focus.grandchild_id); 960 961 try wgt.wrapLines(allocator, &self.lines, self.runes, .word, max_width); 962 // trailing spaces are dropped so a wrapped link doesn't highlight them 963 for (self.lines.items) |*line| { 964 while (line.end > line.start and self.runes[line.end - 1] == ' ') { 965 line.end -= 1; 966 line.width -= 1; 967 } 968 } 969 const visible = @min(self.lines.items.len, constraint.max_size.height orelse self.lines.items.len); 970 var width: usize = 1; 971 for (self.lines.items[0..visible]) |line| width = @max(width, line.width); 972 if (max_width) |w| width = @min(width, w); 973 974 var grid = try Grid.init(allocator, .{ .width = width, .height = visible }); 975 errdefer grid.deinit(); 976 self.pieces.clearRetainingCapacity(); 977 for (self.lines.items[0..visible], 0..) |line, y| { 978 var x: usize = 0; 979 for (self.runes[line.start..line.end], line.start..) |rune, i| { 980 const rune_width = xitui.width.cellWidth(rune); 981 if (x + rune_width > width) break; 982 const link = self.span_links[self.span_of[i]]; 983 const cell = try grid.cell(x, y); 984 cell.style = self.styles[self.span_of[i]]; 985 if (link) |l| { 986 cell.link = terminalLink(self.links[l]); 987 if (l == focused_link) cell.style.inverted = true; 988 // extend this line's piece of the link, or start one 989 const items = self.pieces.items; 990 if (items.len > 0 and items[items.len - 1].link == l and items[items.len - 1].rect.y == y) { 991 items[items.len - 1].rect.size.width += rune_width; 992 } else { 993 try self.pieces.append(allocator, .{ .link = l, .rect = .{ .x = x, .y = y, .size = .{ .width = rune_width, .height = 1 } } }); 994 } 995 } 996 try grid.setRune(x, y, rune); 997 x += rune_width; 998 } 999 } 1000 1001 // a link's pieces are consecutive, since its runes are 1002 var piece_index: usize = 0; 1003 for (self.pieces.items, 0..) |piece, i| { 1004 piece_index = if (i > 0 and self.pieces.items[i - 1].link == piece.link) piece_index + 1 else 0; 1005 const focuses = &self.link_focuses[piece.link]; 1006 if (piece_index == focuses.items.len) { 1007 const focus = try Focus.create(allocator, .{ .custom = self.links[piece.link] }); 1008 errdefer focus.destroy(allocator); 1009 focus.mode = .all; 1010 try focuses.append(allocator, focus); 1011 } 1012 try self.focus.addChild(allocator, focuses.items[piece_index], piece.rect.size, piece.rect.x, piece.rect.y); 1013 } 1014 1015 // focus rests on a link's first piece, which stays while the link shows 1016 if (focused_link) |l| if (self.link_focuses[l].items.len > 0) { 1017 const first = self.link_focuses[l].items[0].id; 1018 if (self.focus.children.contains(first)) { 1019 root_focus.grandchild_id = first; 1020 self.focus.child_id = first; 1021 } 1022 }; 1023 1024 self.grid = grid; 1025 } 1026 1027 pub fn input(self: *RichText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1028 _ = self; 1029 _ = allocator; 1030 _ = key; 1031 _ = root_focus; 1032 } 1033 1034 pub fn clearGrid(self: *RichText) void { 1035 if (self.grid) |*grid| { 1036 grid.deinit(); 1037 self.grid = null; 1038 } 1039 } 1040 1041 pub fn getGrid(self: RichText) ?Grid { 1042 return self.grid; 1043 } 1044 1045 pub fn getFocus(self: *RichText) *Focus { 1046 return self.focus; 1047 } 1048 }; 1049 1050 // an invisible widget that fills the horizontal space granted by its parent. 1051 // used inside Box(horiz) with a min_size so the box reserves space for the 1052 // children that follow, pushing them to the right. 1053 pub const Spacer = struct { 1054 focus: *Focus, 1055 grid: ?Grid, 1056 1057 pub fn init(allocator: std.mem.Allocator) !Spacer { 1058 return .{ 1059 .focus = try Focus.create(allocator, .container), 1060 .grid = null, 1061 }; 1062 } 1063 1064 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1065 self.focus.destroy(allocator); 1066 if (self.grid) |*grid| { 1067 grid.deinit(); 1068 self.grid = null; 1069 } 1070 } 1071 1072 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1073 _ = root_focus; 1074 self.clearGrid(); 1075 const width = constraint.max_size.width orelse return; 1076 if (width == 0) return; 1077 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1078 } 1079 1080 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1081 _ = self; 1082 _ = allocator; 1083 _ = key; 1084 _ = root_focus; 1085 } 1086 1087 pub fn clearGrid(self: *Spacer) void { 1088 if (self.grid) |*grid| { 1089 grid.deinit(); 1090 self.grid = null; 1091 } 1092 } 1093 1094 pub fn getGrid(self: Spacer) ?Grid { 1095 return self.grid; 1096 } 1097 1098 pub fn getFocus(self: *Spacer) *Focus { 1099 return self.focus; 1100 } 1101 }; 1102 1103 // a sub-header's search box: a labeled text input whose enter the owning view 1104 // turns into a navigation. shared by the files, commits and thread tabs. 1105 pub const SearchBox = struct { 1106 text_input: wgt.TextInput, 1107 session: *ui.Session, 1108 1109 // the text width, narrowed toward the minimum when the row is tight 1110 const min_width = 18; 1111 const max_width = 30; 1112 1113 // the room the box asks its row for 1114 pub const min_size: layout.MaybeSize = .{ .width = min_width + 2, .height = 3 }; 1115 1116 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, label: []const u8, name: []const u8, text_value: ?[]const u8) !SearchBox { 1117 var text_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = label }, .name = name, .round_corners = true, .visible_width = max_width, .render_content = session.is_terminal }); 1118 errdefer text_input.deinit(allocator); 1119 text_input.getFocus().mode = .all; 1120 if (text_value) |value| try text_input.setContent(allocator, value); 1121 return .{ .text_input = text_input, .session = session }; 1122 } 1123 1124 pub fn deinit(self: *SearchBox, allocator: std.mem.Allocator) void { 1125 self.text_input.deinit(allocator); 1126 } 1127 1128 pub fn build(self: *SearchBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1129 // the row leaves room for its other children's minimums, so this is what can be spared 1130 const room = (constraint.max_size.width orelse max_width + 2) -| 2; 1131 self.text_input.options.visible_width = std.math.clamp(room, min_width, max_width); 1132 self.text_input.options.bottom_label.text = if (root_focus.grandchild_id == self.getFocus().id) " press enter " else ""; 1133 try self.text_input.build(allocator, constraint, root_focus); 1134 if (!self.session.is_terminal) { 1135 try self.session.text_inputs.put(self.session.arena.allocator(), self.getFocus().id, &self.text_input); 1136 } 1137 } 1138 1139 pub fn input(self: *SearchBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1140 try self.text_input.input(allocator, key, root_focus); 1141 } 1142 1143 // the typed text; the caller owns it 1144 pub fn text(self: *SearchBox, allocator: std.mem.Allocator) ![]u8 { 1145 return self.text_input.text(allocator); 1146 } 1147 1148 // whether the cursor sits past the last character, where right-arrow leaves 1149 // the box rather than moving within it 1150 pub fn atEnd(self: *SearchBox) bool { 1151 return self.text_input.cursor == self.text_input.content.items.len; 1152 } 1153 1154 pub fn clearGrid(self: *SearchBox) void { 1155 self.text_input.clearGrid(); 1156 } 1157 1158 pub fn getGrid(self: SearchBox) ?Grid { 1159 return self.text_input.getGrid(); 1160 } 1161 1162 pub fn getFocus(self: *SearchBox) *Focus { 1163 return self.text_input.getFocus(); 1164 } 1165 }; 1166 1167 // a wrapping row of bordered options posted as one form field. the selection 1168 // is the focused option, so a click selects on the web too. 1169 pub const Radio = struct { 1170 // holds the options' flow, framed under the label when there is one 1171 box: wgt.Box(Widget), 1172 name: []const u8, 1173 values: []const []const u8, 1174 1175 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, name: []const u8, values: []const []const u8, initial: []const u8, label: ?[]const u8) !Radio { 1176 var box = try wgt.Box(Widget).init(allocator, .{ 1177 .border = if (label != null) .single else null, 1178 .round_corners = true, 1179 .direction = .vert, 1180 .top_label = .{ .text = label orelse "" }, 1181 }); 1182 errdefer box.deinit(allocator); 1183 1184 var flow = try WordFlow.init(allocator); 1185 errdefer flow.deinit(allocator); 1186 const pa = session.page_arena.allocator(); 1187 const items = try pa.alloc(WordFlow.Item, values.len); 1188 for (items, values) |*item, value| item.* = .{ .text = value, .link = try pa.print(ui.radio_prefix ++ "{s}={s}", .{ name, value }) }; 1189 try flow.setItems(allocator, items); 1190 for (flow.text_boxes.items, values) |*option, value| { 1191 if (std.mem.eql(u8, value, initial)) flow.focus.child_id = option.getFocus().id; 1192 } 1193 1194 box.getFocus().child_id = flow.getFocus().id; 1195 try box.children.put(allocator, flow.getFocus().id, .{ .widget = .{ .word_flow = flow }, .rect = null, .min_size = null }); 1196 return .{ .box = box, .name = name, .values = values }; 1197 } 1198 1199 pub fn deinit(self: *Radio, allocator: std.mem.Allocator) void { 1200 self.box.deinit(allocator); 1201 } 1202 1203 fn optionFlow(self: *Radio) *WordFlow { 1204 return &self.box.children.values()[0].widget.word_flow; 1205 } 1206 1207 // the focused option's value 1208 pub fn selected(self: *Radio) []const u8 { 1209 const options = self.optionFlow(); 1210 return self.values[options.indexOfFocusId(options.focus.child_id orelse unreachable) orelse unreachable]; 1211 } 1212 1213 pub fn build(self: *Radio, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1214 self.clearGrid(); 1215 const options = self.optionFlow(); 1216 const selected_id = options.focus.child_id; 1217 for (options.text_boxes.items) |*option| { 1218 option.options.border = if (option.getFocus().id == selected_id) .single else .hidden; 1219 } 1220 try self.box.build(allocator, constraint, root_focus); 1221 } 1222 1223 // left and right step through the options in order, however they wrap 1224 pub fn input(self: *Radio, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1225 _ = allocator; 1226 const options = self.optionFlow(); 1227 const index = options.indexOfFocusId(options.focus.child_id orelse return) orelse return; 1228 const option_boxes = options.text_boxes.items; 1229 switch (key) { 1230 .arrow_left => if (index > 0) root_focus.setFocus(option_boxes[index - 1].getFocus().id), 1231 .arrow_right => if (index + 1 < option_boxes.len) root_focus.setFocus(option_boxes[index + 1].getFocus().id), 1232 else => {}, 1233 } 1234 } 1235 1236 pub fn clearGrid(self: *Radio) void { 1237 self.box.clearGrid(); 1238 } 1239 1240 pub fn getGrid(self: Radio) ?Grid { 1241 return self.box.getGrid(); 1242 } 1243 1244 pub fn getFocus(self: *Radio) *Focus { 1245 return self.box.getFocus(); 1246 } 1247 }; 1248 1249 // a sub-header's row, which scrolls sideways once its children's minimum widths 1250 // outgrow the window, like the page header: natively on the web, following the 1251 // focused child in the terminal 1252 pub const HeaderScroll = struct { 1253 scroll: wgt.Scroll(Widget), 1254 1255 // an empty row, which the caller fills through `row` 1256 pub fn init(allocator: std.mem.Allocator, session: *const ui.Session) !HeaderScroll { 1257 // the outer box hands the row a max width, which the scroll alone can't 1258 var outer = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1259 errdefer outer.deinit(allocator); 1260 { 1261 var row_box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1262 errdefer row_box.deinit(allocator); 1263 try outer.children.put(allocator, row_box.getFocus().id, .{ .widget = .{ .box = row_box }, .rect = null, .min_size = null }); 1264 outer.getFocus().child_id = row_box.getFocus().id; 1265 } 1266 return .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = outer }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }) }; 1267 } 1268 1269 pub fn deinit(self: *HeaderScroll, allocator: std.mem.Allocator) void { 1270 self.scroll.deinit(allocator); 1271 } 1272 1273 fn rowChild(self: *const HeaderScroll) *wgt.Box(Widget).Child { 1274 return &self.scroll.child.box.children.values()[0]; 1275 } 1276 1277 pub fn row(self: *const HeaderScroll) *wgt.Box(Widget) { 1278 return &self.rowChild().widget.box; 1279 } 1280 1281 pub fn build(self: *HeaderScroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1282 self.clearGrid(); 1283 const row_box = self.row(); 1284 // bring the focused child into view where the last build put it. one 1285 // wider than the window shows its start, since chasing both its edges 1286 // would scroll back and forth on every build. 1287 if (!self.scroll.options.web_native) if (row_box.getFocus().child_id) |id| if (row_box.children.get(id)) |child| if (child.rect) |rect| { 1288 var target = rect; 1289 if (self.scroll.viewport) |viewport| target.size.width = @min(rect.size.width, viewport.width); 1290 self.scroll.scrollToRect(target); 1291 }; 1292 1293 // the window's width, or the children's minimums when they need more. 1294 // only a max, since a min would stretch the row's last laid out child. 1295 const width: ?usize = if (constraint.max_size.width) |viewport| blk: { 1296 var needed: usize = 0; 1297 for (row_box.children.values()) |child| { 1298 if (child.min_size) |min_size| needed += min_size.width orelse 0; 1299 } 1300 break :blk @max(viewport, needed); 1301 } else null; 1302 self.rowChild().max_size = .{ .width = width, .height = null }; 1303 try self.scroll.build(allocator, constraint, root_focus); 1304 } 1305 1306 pub fn input(self: *HeaderScroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1307 try self.scroll.input(allocator, key, root_focus); 1308 } 1309 1310 pub fn clearGrid(self: *HeaderScroll) void { 1311 self.scroll.clearGrid(); 1312 } 1313 1314 pub fn getGrid(self: HeaderScroll) ?Grid { 1315 return self.scroll.getGrid(); 1316 } 1317 1318 pub fn getFocus(self: *HeaderScroll) *Focus { 1319 return self.scroll.getFocus(); 1320 } 1321 }; 1322 1323 // a tab's sub-header: an optional search box on the left, then a spacer, then 1324 // the clone url on the right. the owning view turns a submitted query into a 1325 // route, so route building stays with the tab that knows its own url. 1326 pub const SearchHeader = struct { 1327 scroll: HeaderScroll, 1328 session: *ui.Session, 1329 1330 pub fn init( 1331 allocator: std.mem.Allocator, 1332 session: *ui.Session, 1333 handle: ui.RepoHandle, 1334 label: []const u8, 1335 name: []const u8, 1336 text: ?[]const u8, 1337 show_input: bool, 1338 ) !SearchHeader { 1339 var scroll = try HeaderScroll.init(allocator, session); 1340 errdefer scroll.deinit(allocator); 1341 const box = scroll.row(); 1342 1343 // the search box only appears where an index can serve it. 1344 if (show_input) { 1345 var search_box = try SearchBox.init(allocator, session, label, name, text); 1346 errdefer search_box.deinit(allocator); 1347 box.getFocus().child_id = search_box.getFocus().id; 1348 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 1349 } 1350 1351 // the spacer pushes the clone url to the right edge. 1352 { 1353 var spacer = try Spacer.init(allocator); 1354 errdefer spacer.deinit(allocator); 1355 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1356 } 1357 1358 if (session.data.host_kind == .server) { 1359 if (try CopyableText.initClone(allocator, session, handle)) |value| { 1360 var clone_url = value; 1361 errdefer clone_url.deinit(allocator); 1362 const min_width = clone_url.minWidth(); 1363 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 1364 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 1365 } 1366 } 1367 1368 return .{ .scroll = scroll, .session = session }; 1369 } 1370 1371 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 1372 self.scroll.deinit(allocator); 1373 } 1374 1375 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1376 self.clearGrid(); 1377 try self.scroll.build(allocator, constraint, root_focus); 1378 } 1379 1380 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1381 const current = self.scroll.row().getFocus().child_id orelse return; 1382 if (self.searchBox()) |search_box| { 1383 if (current == search_box.getFocus().id) { 1384 // tab, and right-arrow past the last character, step to the 1385 // clone url beside the box 1386 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1387 if (self.cloneUrl()) |clone_url| { 1388 root_focus.setFocus(clone_url.getFocus().id); 1389 return; 1390 } 1391 } 1392 return search_box.input(allocator, key, root_focus); 1393 } 1394 } 1395 const clone_url = self.cloneUrl() orelse return; 1396 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1397 if (self.searchBox()) |search_box| { 1398 root_focus.setFocus(search_box.getFocus().id); 1399 return; 1400 } 1401 } 1402 try clone_url.input(allocator, key, root_focus); 1403 } 1404 1405 // the typed query when enter should submit it: the box exists and holds 1406 // focus. the caller owns the returned text. 1407 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1408 const search_box = self.searchBox() orelse return null; 1409 if (self.scroll.row().getFocus().child_id != search_box.getFocus().id) return null; 1410 return try search_box.text(allocator); 1411 } 1412 1413 // enter the header, preferring the search box. 1414 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1415 if (self.searchBox()) |search_box| { 1416 root_focus.setFocus(search_box.getFocus().id); 1417 return true; 1418 } 1419 const clone_url = self.cloneUrl() orelse return false; 1420 root_focus.setFocus(clone_url.getFocus().id); 1421 return true; 1422 } 1423 1424 pub fn hasFocusable(self: *SearchHeader) bool { 1425 return self.cloneUrl() != null or self.searchBox() != null; 1426 } 1427 1428 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1429 for (self.scroll.row().children.values()) |*child| switch (child.widget) { 1430 .copyable_text => |*copyable_text| return copyable_text, 1431 else => {}, 1432 }; 1433 return null; 1434 } 1435 1436 fn searchBox(self: *SearchHeader) ?*SearchBox { 1437 for (self.scroll.row().children.values()) |*child| switch (child.widget) { 1438 .search_box => |*search_box| return search_box, 1439 else => {}, 1440 }; 1441 return null; 1442 } 1443 1444 pub fn clearGrid(self: *SearchHeader) void { 1445 self.scroll.clearGrid(); 1446 } 1447 1448 pub fn getGrid(self: SearchHeader) ?Grid { 1449 return self.scroll.getGrid(); 1450 } 1451 1452 pub fn getFocus(self: *SearchHeader) *Focus { 1453 return self.scroll.getFocus(); 1454 } 1455 }; 1456 1457 // a one-row, non-focusable status bar showing the current page's url 1458 pub const Footer = struct { 1459 focus: *Focus, 1460 grid: ?Grid, 1461 session: *ui.Session, 1462 arena: std.heap.ArenaAllocator, 1463 url: []const u8 = "", 1464 url_width: usize = 0, 1465 1466 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1467 return .{ 1468 .focus = try Focus.create(allocator, .container), 1469 .grid = null, 1470 .session = session, 1471 .arena = std.heap.ArenaAllocator.init(allocator), 1472 }; 1473 } 1474 1475 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1476 self.focus.destroy(allocator); 1477 if (self.grid) |*grid| grid.deinit(); 1478 self.arena.deinit(); 1479 } 1480 1481 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1482 _ = root_focus; 1483 self.clearGrid(); 1484 const width = constraint.max_size.width orelse return; 1485 if (width == 0) return; 1486 1487 _ = self.arena.reset(.retain_capacity); 1488 self.url = ""; 1489 self.url_width = 0; 1490 const aa = self.arena.allocator(); 1491 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1492 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1493 self.url = text; 1494 1495 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1496 errdefer grid.deinit(); 1497 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1498 var i: usize = 0; 1499 while (utf8.nextCodepoint()) |ch| { 1500 const rune_width = xitui.width.cellWidth(ch); 1501 if (i + rune_width > width) break; 1502 // a terminal hyperlink to the page 1503 (try grid.cell(i, 0)).link = text; 1504 try grid.setRune(i, 0, ch); 1505 i += rune_width; 1506 } 1507 self.url_width = i; 1508 self.grid = grid; 1509 } 1510 1511 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1512 _ = allocator; 1513 switch (key) { 1514 .mouse => |mouse| { 1515 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1516 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1517 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1518 }, 1519 else => {}, 1520 } 1521 } 1522 1523 pub fn clearGrid(self: *Footer) void { 1524 if (self.grid) |*grid| { 1525 grid.deinit(); 1526 self.grid = null; 1527 } 1528 } 1529 1530 pub fn getGrid(self: Footer) ?Grid { 1531 return self.grid; 1532 } 1533 1534 pub fn getFocus(self: *Footer) *Focus { 1535 return self.focus; 1536 } 1537 }; 1538 1539 // a single-child wrapper that builds the child at its natural size and 1540 // positions its grid in the middle of the area granted by the parent 1541 // a bordered label, indented past the content it precedes 1542 pub const SectionLabel = struct { 1543 box: wgt.Box(Widget), 1544 1545 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1546 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1547 errdefer box.deinit(allocator); 1548 1549 { 1550 var gap = try wgt.Text.init(allocator, " "); 1551 errdefer gap.deinit(allocator); 1552 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1553 } 1554 1555 { 1556 var tb = try wgt.TextBox.init(allocator, content, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1557 errdefer tb.deinit(allocator); 1558 tb.getFocus().mode = .all; 1559 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1560 } 1561 1562 box.getFocus().child_id = box.children.keys()[1]; 1563 return .{ .box = box }; 1564 } 1565 1566 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1567 self.box.deinit(allocator); 1568 } 1569 1570 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1571 try self.box.build(allocator, constraint, root_focus); 1572 } 1573 1574 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1575 _ = self; 1576 _ = allocator; 1577 _ = key; 1578 _ = root_focus; 1579 } 1580 1581 pub fn clearGrid(self: *SectionLabel) void { 1582 self.box.clearGrid(); 1583 } 1584 1585 pub fn getGrid(self: SectionLabel) ?Grid { 1586 return self.box.getGrid(); 1587 } 1588 1589 pub fn getFocus(self: *SectionLabel) *Focus { 1590 return self.box.getFocus(); 1591 } 1592 }; 1593 1594 // a submit button, indented past its form's content. the web form POSTs 1595 // the button's form; terminal hosts detect presses through `buttonId`. 1596 pub const SubmitButton = struct { 1597 box: wgt.Box(Widget), 1598 1599 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1600 return initLabeled(allocator, "submit"); 1601 } 1602 1603 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1604 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1605 errdefer box.deinit(allocator); 1606 1607 { 1608 var gap = try wgt.Text.init(allocator, " "); 1609 errdefer gap.deinit(allocator); 1610 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1611 } 1612 1613 { 1614 var button = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1615 errdefer button.deinit(allocator); 1616 button.getFocus().mode = .all; 1617 // the renderer distinguishes plain clickables from buttons that 1618 // should POST to a server route by this kind. 1619 button.getFocus().kind = .{ .custom = "submit" }; 1620 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1621 } 1622 1623 box.getFocus().child_id = box.children.keys()[1]; 1624 return .{ .box = box }; 1625 } 1626 1627 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1628 self.box.deinit(allocator); 1629 } 1630 1631 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1632 const button = &self.box.children.values()[1]; 1633 try button.widget.text_box.setContent(allocator, label); 1634 button.min_size = .{ .width = label.len + 2, .height = null }; 1635 } 1636 1637 pub fn setBorder(self: *SubmitButton, border: wgt.Border) void { 1638 self.box.children.values()[1].widget.text_box.options.border = border; 1639 } 1640 1641 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1642 try self.box.build(allocator, constraint, root_focus); 1643 } 1644 1645 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1646 _ = self; 1647 _ = allocator; 1648 _ = key; 1649 _ = root_focus; 1650 } 1651 1652 pub fn clearGrid(self: *SubmitButton) void { 1653 self.box.clearGrid(); 1654 } 1655 1656 pub fn getGrid(self: SubmitButton) ?Grid { 1657 return self.box.getGrid(); 1658 } 1659 1660 pub fn getFocus(self: *SubmitButton) *Focus { 1661 return self.box.getFocus(); 1662 } 1663 1664 // the button's focus id, for click hit-testing 1665 pub fn buttonId(self: *const SubmitButton) usize { 1666 return self.box.children.keys()[1]; 1667 } 1668 }; 1669 1670 // keep `rect`, in a centered widget's space, in view within its scroll 1671 pub fn scrollToCenteredRect(scroll: *wgt.Scroll(Widget), rect: layout.IRect) void { 1672 const center = &scroll.child.center; 1673 const center_grid = center.getGrid() orelse return; 1674 const child_grid = center.child.getGrid() orelse return; 1675 const offset_y: isize = @intCast((center_grid.size.height -| child_grid.size.height) / 2); 1676 scroll.scrollToRect(.{ .x = rect.x, .y = rect.y + offset_y, .size = rect.size }); 1677 } 1678 1679 pub const Center = struct { 1680 focus: *Focus, 1681 grid: ?Grid, 1682 child: *Widget, 1683 1684 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1685 const child = try allocator.create(Widget); 1686 errdefer allocator.destroy(child); 1687 child.* = child_widget; 1688 return .{ 1689 .focus = try Focus.create(allocator, .container), 1690 .grid = null, 1691 .child = child, 1692 }; 1693 } 1694 1695 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1696 self.focus.destroy(allocator); 1697 if (self.grid) |*grid| { 1698 grid.deinit(); 1699 self.grid = null; 1700 } 1701 self.child.deinit(allocator); 1702 allocator.destroy(self.child); 1703 } 1704 1705 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1706 self.clearGrid(); 1707 self.getFocus().clear(); 1708 1709 // build the child without forcing it to fill min_size; it sizes 1710 // itself to its content within the available max. 1711 try self.child.build(allocator, .{ 1712 .min_size = .{ .width = null, .height = null }, 1713 .max_size = constraint.max_size, 1714 }, root_focus); 1715 1716 const child_grid = self.child.getGrid() orelse return; 1717 1718 // prefer max when given; otherwise grow to max(min, child) so that 1719 // when only a min is set (e.g. the wasm path passes viewport rows 1720 // as min_height with no max), we can still vertically center while 1721 // letting taller content extend past the min. 1722 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 1723 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 1724 if (width == 0 or height == 0) return; 1725 1726 const offset_x: usize = (width -| child_grid.size.width) / 2; 1727 const offset_y: usize = (height -| child_grid.size.height) / 2; 1728 1729 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1730 errdefer grid.deinit(); 1731 try grid.drawGrid(child_grid, offset_x, offset_y); 1732 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1733 self.getFocus().child_id = self.child.getFocus().id; 1734 1735 self.grid = grid; 1736 } 1737 1738 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1739 try self.child.input(allocator, key, root_focus); 1740 } 1741 1742 pub fn clearGrid(self: *Center) void { 1743 if (self.grid) |*grid| { 1744 grid.deinit(); 1745 self.grid = null; 1746 } 1747 self.child.clearGrid(); 1748 } 1749 1750 pub fn getGrid(self: Center) ?Grid { 1751 return self.grid; 1752 } 1753 1754 pub fn getFocus(self: *Center) *Focus { 1755 return self.focus; 1756 } 1757 }; 1758 1759 // renders truecolor ANSI art 1760 pub const AnsiArt = struct { 1761 focus: *Focus, 1762 grid: ?Grid, 1763 content: []const u8, 1764 1765 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1766 return .{ 1767 .focus = try Focus.create(allocator, .container), 1768 .grid = null, 1769 .content = content, 1770 }; 1771 } 1772 1773 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1774 self.focus.destroy(allocator); 1775 if (self.grid) |*grid| { 1776 grid.deinit(); 1777 self.grid = null; 1778 } 1779 } 1780 1781 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1782 _ = root_focus; 1783 self.clearGrid(); 1784 1785 // parse into rows of cells, then copy into a rectangular grid 1786 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1787 defer { 1788 for (rows.items) |*row| row.deinit(allocator); 1789 rows.deinit(allocator); 1790 } 1791 var row: std.ArrayList(Grid.Cell) = .empty; 1792 errdefer row.deinit(allocator); 1793 1794 var style: Grid.Style = .{}; 1795 var width: usize = 0; 1796 const content = self.content; 1797 var i: usize = 0; 1798 while (i < content.len) { 1799 const byte = content[i]; 1800 if (byte == '\n') { 1801 width = @max(width, row.items.len); 1802 try rows.append(allocator, row); 1803 row = .empty; 1804 i += 1; 1805 } else if (byte == '\r') { 1806 // git may checkout the ansi art with crlf line endings on windows 1807 i += 1; 1808 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1809 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1810 var j = i + 2; 1811 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1812 if (j >= content.len) { 1813 i = content.len; // malformed trailing escape; stop 1814 } else { 1815 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1816 i = j + 1; 1817 } 1818 } else { 1819 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1820 const end = @min(content.len, i + len); 1821 const rune = content[i..end]; 1822 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1823 // a foreground color does not make a space visible. only an 1824 // effective background makes it opaque; foreground state often 1825 // remains active through the padding at the end of a row. 1826 const effective_background = if (style.inverted) style.fg else style.bg; 1827 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1828 try row.append(allocator, .{ 1829 .rune = if (transparent) null else codepoint, 1830 .style = style, 1831 }); 1832 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1833 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1834 } 1835 i = end; 1836 } 1837 } 1838 // a trailing row with content but no closing newline 1839 if (row.items.len > 0) { 1840 width = @max(width, row.items.len); 1841 try rows.append(allocator, row); 1842 } else { 1843 row.deinit(allocator); 1844 } 1845 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1846 1847 const height = rows.items.len; 1848 if (width == 0 or height == 0) return; 1849 1850 const clamped_w = @min(width, constraint.max_size.width orelse width); 1851 const clamped_h = @min(height, constraint.max_size.height orelse height); 1852 1853 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1854 errdefer grid.deinit(); 1855 for (rows.items[0..clamped_h], 0..) |r, y| { 1856 const n = @min(r.items.len, clamped_w); 1857 for (r.items[0..n], 0..) |cell, x| { 1858 if (cell.continuation) continue; 1859 (try grid.cell(x, y)).style = cell.style; 1860 try grid.setRune(x, y, cell.rune); 1861 } 1862 } 1863 self.grid = grid; 1864 } 1865 1866 pub fn clearGrid(self: *AnsiArt) void { 1867 if (self.grid) |*grid| { 1868 grid.deinit(); 1869 self.grid = null; 1870 } 1871 } 1872 1873 pub fn getGrid(self: AnsiArt) ?Grid { 1874 return self.grid; 1875 } 1876 1877 pub fn getFocus(self: *AnsiArt) *Focus { 1878 return self.focus; 1879 } 1880 1881 fn applySgr(style: *Grid.Style, params: []const u8) void { 1882 var nums: [16]u32 = undefined; 1883 var n: usize = 0; 1884 var it = std.mem.splitScalar(u8, params, ';'); 1885 while (it.next()) |tok| { 1886 if (n >= nums.len) break; 1887 // an empty parameter (e.g. bare "\x1b[m") means 0 1888 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1889 n += 1; 1890 } 1891 if (n == 0) { 1892 style.* = .{}; 1893 return; 1894 } 1895 var i: usize = 0; 1896 while (i < n) : (i += 1) { 1897 switch (nums[i]) { 1898 0 => style.* = .{}, 1899 7 => style.inverted = true, 1900 27 => style.inverted = false, 1901 39 => style.fg = null, 1902 49 => style.bg = null, 1903 38, 48 => { 1904 // truecolor form: 38;2;r;g;b — anything else is ignored 1905 if (i + 4 < n and nums[i + 1] == 2) { 1906 const c = Grid.Color{ .rgb = .{ 1907 .r = @truncate(nums[i + 2]), 1908 .g = @truncate(nums[i + 3]), 1909 .b = @truncate(nums[i + 4]), 1910 } }; 1911 if (nums[i] == 38) style.fg = c else style.bg = c; 1912 i += 4; 1913 } 1914 }, 1915 else => {}, 1916 } 1917 } 1918 } 1919 }; 1920 1921 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1922 pub const AnsiBackground = struct { 1923 grid: ?Grid, 1924 child: *Widget, 1925 art: AnsiArt, 1926 session: *ui.Session, 1927 1928 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1929 var cw = child_widget; 1930 const child = allocator.create(Widget) catch |e| { 1931 cw.deinit(allocator); 1932 return e; 1933 }; 1934 child.* = cw; 1935 errdefer { 1936 child.deinit(allocator); 1937 allocator.destroy(child); 1938 } 1939 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1940 } 1941 1942 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1943 if (self.grid) |*grid| grid.deinit(); 1944 self.art.deinit(allocator); 1945 self.child.deinit(allocator); 1946 allocator.destroy(self.child); 1947 } 1948 1949 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1950 self.clearGrid(); 1951 1952 // make the wrapped page fill the whole available area 1953 try self.child.build(allocator, .{ 1954 .min_size = .{ 1955 .width = constraint.max_size.width orelse constraint.min_size.width, 1956 .height = constraint.max_size.height orelse constraint.min_size.height, 1957 }, 1958 .max_size = constraint.max_size, 1959 }, root_focus); 1960 1961 const foreground = self.child.getGrid() orelse return; 1962 if (!self.session.color) return; 1963 // terminals show the art only once they report a background to fade it toward 1964 const backdrop: ?Grid.Color.Rgb = if (self.session.is_terminal) self.session.terminal_background orelse return else null; 1965 self.art.content = self.session.data.ansi_art; 1966 try self.art.build(allocator, .{ 1967 .min_size = .{ .width = null, .height = null }, 1968 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1969 }, root_focus); 1970 // the web fades the art with css opacity instead 1971 const art_backdrop = backdrop orelse return; 1972 if (self.art.grid) |*art_grid| { 1973 for (art_grid.cells) |*cell| { 1974 cell.style.fg = fadeColor(cell.style.fg, art_backdrop); 1975 cell.style.bg = fadeColor(cell.style.bg, art_backdrop); 1976 } 1977 self.grid = try artBehind(allocator, foreground, art_grid.*); 1978 } 1979 } 1980 1981 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1982 try self.child.input(allocator, key, root_focus); 1983 } 1984 1985 pub fn clearGrid(self: *AnsiBackground) void { 1986 if (self.grid) |*grid| { 1987 grid.deinit(); 1988 self.grid = null; 1989 } 1990 self.child.clearGrid(); 1991 self.art.clearGrid(); 1992 } 1993 1994 pub fn getGrid(self: AnsiBackground) ?Grid { 1995 return self.grid orelse self.child.getGrid(); 1996 } 1997 1998 pub fn getFocus(self: *AnsiBackground) *Focus { 1999 return self.child.getFocus(); 2000 } 2001 2002 // a foreground cell counts as blank if it's empty or a bare space without styling 2003 fn cellIsBlank(cell: Grid.Cell) bool { 2004 if (cell.continuation) return false; 2005 if (cell.rune) |rune| { 2006 return rune == ' ' and 2007 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2008 } 2009 return true; 2010 } 2011 2012 // the art is faded toward the background at this opacity so foreground 2013 // text stays legible over it 2014 pub const art_opacity = 25; // percent 2015 2016 // the art parser only emits truecolor, so a palette color never occurs 2017 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 2018 const c = color orelse return null; 2019 return switch (c) { 2020 .rgb => |v| v, 2021 else => null, 2022 }; 2023 } 2024 2025 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 2026 const v = artRgb(color) orelse return color; 2027 const blend = struct { 2028 fn f(c: u8, b: u8) u8 { 2029 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 2030 } 2031 }.f; 2032 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 2033 } 2034 2035 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2036 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2037 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2038 // snap such channels clear of that range so the backdrop doesn't blink or 2039 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2040 // actually render truecolor. 2041 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2042 const v = artRgb(color) orelse return color; 2043 const snap = struct { 2044 fn f(c: u8) u8 { 2045 return if (c >= 1 and c <= 9) 10 else c; 2046 } 2047 }.f; 2048 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 2049 } 2050 2051 // composites ANSI art behind a foreground grid 2052 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 2053 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2054 errdefer out.deinit(); 2055 2056 const anchor_x = foreground.size.width -| art_grid.size.width; 2057 for (0..art_grid.size.height) |y| { 2058 for (0..art_grid.size.width) |x| { 2059 const src = (try art_grid.cell(x, y)).*; 2060 if (src.rune == null) continue; 2061 const dst = try out.cell(anchor_x + x, y); 2062 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 2063 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 2064 // blank cells take the art; occupied cells take its background 2065 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 2066 dst.style = src.style; 2067 dst.style.fg = sgrSafe(dst.style.fg); 2068 dst.style.bg = sgrSafe(dst.style.bg); 2069 try out.setRune(anchor_x + x, y, src.rune); 2070 } else { 2071 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 2072 dst.style.bg = art_bg; 2073 if (next_dst) |next| next.style.bg = art_bg; 2074 } 2075 } 2076 } 2077 return out; 2078 } 2079 }; 2080 2081 pub const CopyableText = struct { 2082 pub const Choice = struct { 2083 selector: []const u8 = "", 2084 text: []const u8, 2085 copyable_text: ?[]const u8 = null, 2086 label: []const u8 = "", 2087 bottom_label: []const u8 = "", 2088 }; 2089 2090 box: wgt.Box(Widget), 2091 session: *ui.Session, 2092 choices: []Choice, 2093 selected: usize = 0, 2094 2095 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 2096 if (choices.len == 0) return error.MissingCopyableText; 2097 2098 const owned_choices = try allocator.dupe(Choice, choices); 2099 errdefer allocator.free(owned_choices); 2100 2101 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2102 errdefer box.deinit(allocator); 2103 2104 if (choices.len > 1) for (choices) |choice| { 2105 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 2106 errdefer selector_box.deinit(allocator); 2107 selector_box.getFocus().mode = .all; 2108 try box.children.put(allocator, selector_box.getFocus().id, .{ .widget = .{ .text_box = selector_box }, .rect = null, .min_size = .{ .width = choice.selector.len + 2, .height = 3 } }); 2109 }; 2110 2111 var text_input = try wgt.TextInput.init(allocator, .{ 2112 .border = .single, 2113 .round_corners = true, 2114 .top_label = .{ .text = choices[initial].label }, 2115 .bottom_label = .{ .text = choices[initial].bottom_label }, 2116 .read_only = true, 2117 .render_content = session.is_terminal, 2118 .visible_width = null, 2119 }); 2120 errdefer text_input.deinit(allocator); 2121 text_input.getFocus().mode = .all; 2122 try text_input.setContent(allocator, choices[initial].text); 2123 box.getFocus().child_id = text_input.getFocus().id; 2124 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 2125 2126 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 2127 } 2128 2129 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 2130 const aa = session.page_arena.allocator(); 2131 var choices: [2]Choice = undefined; 2132 var count: usize = 0; 2133 var ssh_index: ?usize = null; 2134 switch (handle.location) { 2135 .repo => |identity| { 2136 const http_url = if (handle.read_access == .public) session.data.git_http_url else null; 2137 if (http_url) |base| { 2138 const url = try aa.print("{s}/{s}", .{ base, identity }); 2139 choices[count] = .{ 2140 .selector = "http", 2141 .text = url, 2142 .copyable_text = try aa.print("git clone {s}", .{url}), 2143 .label = " clone ", 2144 }; 2145 count += 1; 2146 } 2147 if (session.data.git_ssh_url) |ssh_url| { 2148 ssh_index = count; 2149 const url = try aa.print("{s}/{s}", .{ ssh_url, identity }); 2150 choices[count] = .{ 2151 .selector = "ssh", 2152 .text = url, 2153 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2154 .label = " clone ", 2155 }; 2156 count += 1; 2157 } 2158 }, 2159 .fork => |f| if (session.data.git_ssh_url) |ssh_url| { 2160 const url = try aa.print("{s}/{s}+{s}", .{ ssh_url, f.identity, f.id }); 2161 choices[count] = .{ 2162 .text = url, 2163 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2164 .label = " clone ", 2165 }; 2166 count += 1; 2167 }, 2168 } 2169 if (count == 0) return null; 2170 // a viewer who can push starts on the ssh url 2171 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 2172 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 2173 } 2174 2175 pub fn minWidth(self: *const CopyableText) usize { 2176 var width = maxWidth(self.choices) + 2; 2177 if (self.choices.len > 1) { 2178 for (self.choices) |choice| width += choice.selector.len + 2; 2179 } 2180 return width; 2181 } 2182 2183 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 2184 self.box.deinit(allocator); 2185 allocator.free(self.choices); 2186 } 2187 2188 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2189 self.clearGrid(); 2190 2191 if (self.choices.len > 1) { 2192 if (root_focus.grandchild_id) |id| { 2193 for (0..self.choices.len) |i| { 2194 if (id != self.selector(i).getFocus().id) continue; 2195 if (i != self.selected) try self.select(allocator, i); 2196 root_focus.setFocus(self.textInput().getFocus().id); 2197 break; 2198 } 2199 } 2200 2201 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2202 for (0..self.choices.len) |i| { 2203 self.selector(i).options.border = if (i == self.selected) 2204 (if (focused) .double else .single) 2205 else 2206 .hidden; 2207 } 2208 } 2209 self.textInput().options.border = .single; 2210 self.textInput().cursor = 0; 2211 self.textInput().scroll_offset = 0; 2212 try self.box.build(allocator, constraint, root_focus); 2213 2214 if (!self.session.is_terminal) { 2215 const aa = self.session.arena.allocator(); 2216 const id = self.textInput().getFocus().id; 2217 try self.session.text_inputs.put(aa, id, self.textInput()); 2218 } 2219 } 2220 2221 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2222 switch (key) { 2223 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2224 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2225 .enter => if (self.session.is_terminal) self.show(), 2226 .mouse => |mouse| { 2227 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2228 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2229 try self.select(allocator, i); 2230 root_focus.setFocus(self.textInput().getFocus().id); 2231 return; 2232 }; 2233 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2234 }, 2235 else => {}, 2236 } 2237 } 2238 2239 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2240 self.selected = selected; 2241 const choice = self.choices[selected]; 2242 const text_input = self.textInput(); 2243 text_input.options.top_label.text = choice.label; 2244 text_input.options.bottom_label.text = choice.bottom_label; 2245 try text_input.setContent(allocator, choice.text); 2246 } 2247 2248 fn show(self: *CopyableText) void { 2249 const choice = self.choices[self.selected]; 2250 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2251 } 2252 2253 fn textInput(self: *CopyableText) *wgt.TextInput { 2254 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2255 } 2256 2257 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2258 return &self.box.children.values()[index].widget.text_box; 2259 } 2260 2261 fn maxWidth(choices: []const Choice) usize { 2262 var width: usize = 0; 2263 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2264 return width; 2265 } 2266 2267 pub fn clearGrid(self: *CopyableText) void { 2268 self.box.clearGrid(); 2269 } 2270 2271 pub fn getGrid(self: CopyableText) ?Grid { 2272 return self.box.getGrid(); 2273 } 2274 2275 pub fn getFocus(self: *CopyableText) *Focus { 2276 return self.box.getFocus(); 2277 } 2278 }; 2279