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