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