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