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