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