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