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