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