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