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