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