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