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