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