diff --git a/.github/workflows/canvaslink.yml b/.github/workflows/canvaslink.yml new file mode 100644 index 000000000..dba77a9f2 --- /dev/null +++ b/.github/workflows/canvaslink.yml @@ -0,0 +1,48 @@ +name: CanvasLink Android + +on: + pull_request: + workflow_dispatch: + +permissions: + contents: read + +jobs: + contracts: + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: + node-version: 22 + cache: npm + - run: npm ci --ignore-scripts + - run: npx playwright install --with-deps chromium + - run: npm run test:canvaslink + - run: npm run typecheck + + android-debug: + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: + node-version: 22 + - uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: '17' + - uses: android-actions/setup-android@v3 + with: + packages: platform-tools + log-accepted-android-sdk-licenses: 'false' + - run: sdkmanager 'platforms;android-35' 'build-tools;35.0.0' + - run: node integrations/canvaslink/scripts/build-android.cjs + - name: Upload APK (ephemeral CI signature; official releases use the owner key) + uses: actions/upload-artifact@v4 + with: + name: CanvasLink-Android + path: integrations/canvaslink/dist/CanvasLink-*.apk + retention-days: 30 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index be0b07f17..ca898bfa1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -139,6 +139,22 @@ jobs: with: path: release-artifacts merge-multiple: true + - name: Include the pinned CanvasLink installers + env: + GH_TOKEN: ${{ github.token }} + run: | + canvaslink_version="$(node -p "require('./integrations/canvaslink/package.json').version")" + gh release download "canvaslink-v${canvaslink_version}" --repo "${GITHUB_REPOSITORY}" \ + --pattern '*.apk' --pattern 'canvaslink-release.json' --dir release-artifacts + node --input-type=module <<'JS' + import { createHash } from 'node:crypto'; + import { readFileSync } from 'node:fs'; + import { CANVASLINK_ANDROID_RELEASE as release } from './src/shared/canvasLinkInstall.ts'; + const apk = readFileSync(`release-artifacts/CanvasLink-${release.version}.apk`); + if (createHash('sha256').update(apk).digest('hex') !== release.sha256) { + throw new Error('CanvasLink APK does not match the pinned installation checksum'); + } + JS - name: Verify release tag and artifact set run: node scripts/verify-release-artifacts.mjs "${GITHUB_REF_NAME}" release-artifacts - name: Create GitHub release diff --git a/CHANGELOG.md b/CHANGELOG.md index 36535428a..2aabbca0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ [English](CHANGELOG.md) · [Русский](CHANGELOG.ru.md) · [简体中文](CHANGELOG.zh-CN.md) +## 1.8.3 + +- Added canvas Auto-sort by task, status, project or grid, with scoped movement and undo (#163, @BIackFIame). +- Added animated ASCII terminal backgrounds, Fine Dots rendering, scene creation, presets and a quick-hide shortcut (#164, @CLOSETTTY). +- Fixed native browser bounds and input coordinates at non-default renderer zoom (#165, @teo-nex). +- Added CanvasLink 1.0 for Android: LAN window streaming, touch and terminal input, pairing and explicit revocation (#166, @teo-nex). Desktop access is off on startup. +- Retained Codex prompt-delivery and account-scoped quota fixes already merged in #158 (@pepechka-kun). +- Integration fixes preserve backgrounds across F8, prevent terminal glyph ghosting, keep zoomed-out cards opaque, repair background-settings scrolling, and add official reset/reload icons and button motion. CanvasLink gains LAN/VPN network selection, bounded reconnects without input replay, a CanvasTTY-style mobile shell and non-selectable controls. +- Original contributor commits are preserved. Maintainer integration and follow-up patches are recorded separately; #159, #160, #161 and #162 are not included. + ## 1.8.1 - Fixed the Files reader rejecting all regular files on macOS: `/dev/fd` device paths are not treated as resolved file targets, and the opened file's identity is checked against its current path before reading. The 1.8.0 build was not published. diff --git a/CHANGELOG.ru.md b/CHANGELOG.ru.md index 4904afd26..63f99df81 100644 --- a/CHANGELOG.ru.md +++ b/CHANGELOG.ru.md @@ -2,6 +2,16 @@ [English](CHANGELOG.md) · [Русский](CHANGELOG.ru.md) · [简体中文](CHANGELOG.zh-CN.md) +## 1.8.3 + +- Автосортировка окон по задаче, состоянию, проекту или сетке с отменой перемещения: #163, @BIackFIame. +- Анимированные ASCII-фоны терминалов, Fine Dots, создание сцен, пресеты и быстрое скрытие: #164, @CLOSETTTY. +- Исправление координат и размера встроенного браузера при изменении масштаба интерфейса: #165, @teo-nex. +- CanvasLink 1.0 для Android: изображение окна по локальной сети, касания, ввод в терминал, сопряжение и отзыв доступа: #166, @teo-nex. После запуска доступ выключен. +- Сохранены уже принятые исправления отправки заданий Codex и устаревших данных лимитов: #158, @pepechka-kun. +- Наши патчи исправляют восстановление фонов после F8, наложение символов, прозрачность отдалённых карточек и прокрутку настроек; добавляют SVG-иконки и анимации кнопок. Для CanvasLink улучшены выбор локальной сети при VPN, ограниченное ожидание подключения без повторения ввода и мобильный интерфейс в стиле CanvasTTY. +- Исходные коммиты участников сохранены; интеграционные правки выделены отдельно. #159, #160, #161 и #162 не включены. + ## 1.8.1 - Исправлен запрет чтения обычных файлов на macOS: пути устройств `/dev/fd` больше не считаются адресами самих файлов; перед чтением проверяется идентичность открытого файла и текущего пути. Сборка 1.8.0 не была опубликована. diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md index fda65adbf..d74f5ac09 100644 --- a/CHANGELOG.zh-CN.md +++ b/CHANGELOG.zh-CN.md @@ -2,6 +2,16 @@ [English](CHANGELOG.md) · [Русский](CHANGELOG.ru.md) · [简体中文](CHANGELOG.zh-CN.md) +## 1.8.3 + +- 新增按任务、状态、项目或网格自动排列窗口,并可撤销移动(#163,@BIackFIame)。 +- 新增 ASCII 动态终端背景、Fine Dots、场景创建、预设和快速隐藏快捷键(#164,@CLOSETTTY)。 +- 修复界面缩放后内置浏览器的大小与输入坐标(#165,@teo-nex)。 +- 新增 Android CanvasLink 1.0,支持局域网窗口画面、触控、终端输入、配对与撤销授权(#166,@teo-nex)。桌面端每次启动时默认关闭访问。 +- 保留 #158 中已合并的 Codex 提示发送与账户额度修复(@pepechka-kun)。 +- 集成修复包括 F8 背景恢复、字符残影、缩小卡片的不透明背景、设置滚动、官方 SVG 图标和按钮动效。CanvasLink 增加 VPN 环境下的局域网选择、连接超时和不重放输入的恢复机制,以及 CanvasTTY 风格的移动界面。 +- 保留原作者提交,维护者集成修复单独记录;不包含 #159、#160、#161 或 #162。 + ## 1.8.1 - 修复 macOS 拒绝读取所有普通文件的问题:不再将 `/dev/fd` 设备路径当作文件实际路径,读取前核对已打开文件与当前路径的身份。1.8.0 构建未发布。 diff --git a/README.md b/README.md index 5efdb8790..873eeadd3 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,10 @@ npm run dev The opt-in **Settings → Controls → Even G2** integration adapts CanvasTTY's terminal and AI-agent workflows to Even G2 glasses. Pair with six digits on the same local network, approve access on the computer, then read responses on the glasses HUD, dictate through local Nemotron speech recognition, and create, rename or close shared sessions. The companion includes a More agents picker and supports the desktop's provider list. Its source lives in `integrations/even-g2` and builds with the desktop. See [setup, distribution status and acceptance limits](docs/even-g2.md); a compatible public installer and Even Hub approval are still pending. +## CanvasLink 1.0 for Android + +**Settings → CanvasLink** streams the current CanvasTTY window to an Android phone or tablet on the same LAN, with touch, direct terminal keyboard input and a shortcut bar. Access starts off; pairing uses a short-lived six-digit code. The desktop host is integrated, with no separate application. See [setup, APK builds and platform limits](docs/canvaslink.md). Original CanvasLink implementation: @teo-nex (#166). The integrated LAN/VPN path has a Linux/Android user trial; final cross-platform device acceptance remains limited. + ## Web companion The opt-in [Web companion](docs/mobile-companion.md) shares selected sessions with Android, iOS, and desktop browsers through **Tailscale Serve HTTPS**, not Funnel. CanvasTTY binds its companion endpoint to `127.0.0.1:3481` in this mode; pair with a short-lived code and approve each browser on the desktop. Open **Settings → Controls → Web companion** for setup, grants, and revocation. End-to-end Tailscale Serve deployment on Windows, Linux, and macOS remains unverified. diff --git a/docs/README.md b/docs/README.md index 3e0c19d1d..5a4c007bb 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,6 +12,7 @@ CanvasTTY is a spatial Electron desktop for real local terminals and AI-agent CL | [Built-in browser and audit log](browser.md) | Canvas controls, settings, agent access, website/file boundaries, activity, and persistent redacted audit files | | [File browser and reader](files.md) | Opening a Files card, session and folder roots, supported content, read limits, quick open, persistence, and read-only security boundaries | | [Web companion](mobile-companion.md) | Tailscale Serve HTTPS setup, browser pairing, session grants, revocation, and security limits | +| [CanvasLink for Android (alpha)](canvaslink.md) | Full-window LAN streaming, touch/keyboard input, APK build, security boundaries and platform limits | | [Installing, releases, and local data](installing-and-security.md) | Installer formats, unsigned-preview caveats, credential boundaries, and release checks | | [Widget authoring](widget-authoring.md) | Source-level extension paths, visual grammar, process boundaries, and an AI-agent brief | | [Pixel terminal themes (Russian)](pixel-skin-packs.ru.md) | Ten-PNG ZIP layout, terminal apertures, app import, and local agent CLI | diff --git a/docs/UI_CONTRACT.md b/docs/UI_CONTRACT.md index 7acaf490d..94e865c2d 100644 --- a/docs/UI_CONTRACT.md +++ b/docs/UI_CONTRACT.md @@ -64,6 +64,7 @@ This contract preserves the approved MVP concept and prevents feature ownership - The Files viewer renders Markdown (`.md`, `.markdown`, `.mdown`, `.mkd`) as formatted GitHub-Flavored Markdown with fenced code blocks syntax-highlighted, and syntax-highlights recognized code files by the language detected from their extension while unknown languages stay plain monospaced text. All rendered output is sanitized: raw HTML and scripts never render and event-handler attributes and dangerous link schemes such as `javascript:` are neutralized. Activating an `http(s)` link opens it in the built-in browser while other schemes are inert, only embedded `data:` images render while remote or relative sources fall back to alt text or a placeholder without a network request, and text or code above the rich-rendering size threshold falls back to plain text with a notice. - An authenticated connection or heartbeat alone never shows agent presence. A branded badge appears only after that agent actually issues a browser command, and its cursor appears only after a real pointer position exists. Claude uses `#D97757`, Codex `#10A37F`, Qwen `#6D44E8`, Kimi `#7C5CFC`, OpenCode `#5A5858`, Hermes `#D6A700`, and an unknown provider `#7A8291`. Browser Settings separately controls indicator visibility without revoking browser access; the agent-access kill switch revokes the connection itself. - With window snapping enabled, drag and resize use a hidden `10px` grid and a `10px` magnetic threshold for neighboring edges, centers, and a consistent `20px` gap. +- The empty-canvas context menu also lists Files and a **Tools** submenu between Browser and Settings. **Tools** holds a single **Auto-sort** action whose criteria (by task, i.e. subagents to the right of the session that spawned them; by status; by project folder; grid) are radio rows in that submenu; there is no separate layouts surface, toolbar, or saved preset. Auto-sort moves the windows of the current multi-selection when it holds at least two, or else every terminal, plugin, and Browser window that is not completely inside a region (companion mascots move only when selected); sticky notes, materials, and Files cards never move. The sorted block keeps the snapping grid and `20px` gap when snapping is on, and shifts right or down until it is clear of HOME, regions, notes, materials, Files cards, and windows it does not move. The moved windows show the existing selection outline for a few seconds or until the next press; it is presentation-only and does not select them. They glide into place unless reduced motion is requested, and **Undo auto-sort** stays in the same submenu while any of them is still where the sort put it. Undo restores only those untouched windows; there is no global undo shortcut, so terminal and text inputs keep their keys. The criterion is remembered only for the running session. - One trusted context dispatcher owns right-clicks on the canvas. Truly empty canvas offers region creation, a sticky note, the Settings-configured launcher submenu, Browser, and Settings. Holding RMB on empty canvas also opens the configurable radial quick launcher: moving toward an action and releasing activates it, while a stationary right-click continues to open the standard empty-canvas menu. Region and note right-clicks remain context-specific. A region offers rename, color, a note at the pressed point, and region-only deletion. A note frame offers edit, bring-to-front, and deletion. Terminal, Browser, plugin, textarea/input, and contenteditable surfaces keep their native context menus; middle-button panning is unchanged. `Cmd/Ctrl+K` opens a searchable palette over live sessions and the same global creation/launch actions. Regions persist independently of terminal restore, render behind HOME and windows, resize from every edge/corner, and move every terminal, plugin, Browser, or note whose complete bounds were inside the region at drag start. Attached contents follow the region continuously during the gesture and persist once at pointer release; deleting a region never deletes its contents. Sticky notes persist bounded text and geometry, snap like other windows, and resize from every edge/corner. General Settings exposes independent **Save colored regions after exit** and **Save notes after exit** controls. Both default on to preserve existing profiles. Turning either off removes only that collection from disk persistence without deleting its live canvas objects; turning it back on saves the current collection, while leaving it off starts the next launch with that collection empty. - Context menus, region editors, the radial launcher, and the command palette use official Lucide/provider assets and shared application color tokens. Their dimensions follow root `--ui-scale`. Appearance owns the `0.85–1.25` scale in `0.05` steps and the sage/lilac/night system palette; the scale changes UI chrome rather than canvas zoom or terminal font size. Agents owns the independent context-menu and radial-launcher subsets and their safe reset defaults. - Settings uses the approved two-pane modal grammar: a persistent icon-labelled sidebar for all seven sections, a separate current-section header, and a scrollable content pane. Settings must not fall back to the legacy light slide-over with one horizontal segmented tab strip. Simple settings use compact copy/control rows; all Settings chrome reads the active system tokens, official Lucide assets, and root `--ui-scale`. diff --git a/docs/UI_CONTRACT.ru.md b/docs/UI_CONTRACT.ru.md index 33926a2da..28339d575 100644 --- a/docs/UI_CONTRACT.ru.md +++ b/docs/UI_CONTRACT.ru.md @@ -58,6 +58,7 @@ - Drag по пустому canvas всегда выполняет pan. В свежем профиле обычный scroll над пустым canvas перемещает его по обеим осям, а pinch и `Cmd/Ctrl + scroll` масштабируют вокруг указателя. **Зум обычным скроллом** возвращает исторический wheel zoom и автоматически включается при миграции существующего профиля. Только focused input-виджет прерывает canvas wheel/pinch; над unfocused или нефокусируемым виджетом события остаются канвасу. **Перехват wheel/pinch над виджетами** имеет режимы Off, On и Key: Off сохраняет владение focused-виджета, On постоянно передаёт канвасу только wheel/pinch, а Key делает это при удержании комбинации. Поэтому focused live Browser page прокручивается обычным scroll в Off или при отпущенном Key; unfocused Browser, On, активный Key binding, полный override, pinch и `Cmd/Ctrl + scroll` принадлежат канвасу. Выбранный в начале Browser wheel-sequence владелец сохраняется до 250 мс без wheel-событий. Browser summary и placeholder всегда принадлежат канвасу. Свежий профиль использует Key с `Command` на macOS или `Ctrl` на остальных платформах. Отдельный **Перехват навигации канвасом** (по умолчанию `Option` на macOS и `Alt` на остальных платформах) передаёт wheel/pinch и drag; одиночный Command/Ctrl разрешён. При удержании полного override все canvas-поверхности показывают `grab`, а начатый pan — `grabbing` до pointer up/cancel даже после отпускания комбинации. - Мини-карта центрирует камеру по нажатию без изменения масштаба. В Controls выбирается **Клик** (по умолчанию, движение указателя игнорируется) или **Перетаскивание** (мини-карта захватывает указатель и непрерывно двигает камеру); клавиши-стрелки работают в обоих режимах. - Canvas двигается RTS-style, пока указатель находится в пределах `56px` от viewport edge над пустым canvas; скорость линейно возрастает до `900px/s` на самой границе. Edge panning выключен по умолчанию и включается в Settings. Motion останавливается над interactive surfaces и во время drag-pan. +- Контекстное меню пустого канваса содержит подменю **Инструменты** с одним действием **Автосортировка**. Критерии (по задачам — субагенты справа от запустившей их сессии; по статусу; по папке проекта; сеткой) — пункты-переключатели в том же подменю, без отдельной панели, тулбара или сохранённых раскладок. Сортируются окна текущего множественного выделения, если их хотя бы два, а иначе — все окна терминалов, плагинов и браузера, которые не лежат целиком внутри региона (маскоты — только если выделены); заметки, материалы и карточки Files не двигаются. При включённой привязке блок держит сетку и зазор `20px`, и сдвигается вправо или вниз, пока не перестанет задевать HOME, регионы, заметки, материалы, карточки Files и неперемещаемые окна. Перемещённые окна на несколько секунд (или до следующего нажатия) получают обычную обводку выделения — только визуально, без выделения — и плавно переезжают, если не включено уменьшение движения. Пункт **Отменить автосортировку** остаётся в подменю, пока хотя бы одно окно стоит там, куда его поставила сортировка, и возвращает только нетронутые окна. Глобального сочетания отмены нет: клавиши остаются у терминалов и полей ввода. Выбранный критерий помнится только до перезапуска. - Dialog close actions остаются внутри своей header/control row с одинаковым inset и не перекрывают field, outline или panel boundary. ## Приёмочные проверки diff --git a/docs/UI_CONTRACT.zh-CN.md b/docs/UI_CONTRACT.zh-CN.md index 9baea3f06..99618b3e3 100644 --- a/docs/UI_CONTRACT.zh-CN.md +++ b/docs/UI_CONTRACT.zh-CN.md @@ -57,6 +57,7 @@ - Window snapping 开启时,drag/resize 使用隐藏 `10px` grid、相邻 edge/center 的 `10px` magnetic threshold,以及一致的 `20px` gap。 - 在空白 canvas 上 drag 始终执行 pan。新配置中,空白 canvas 上的普通 scroll 会沿两个轴移动,pinch 与 `Cmd/Ctrl + scroll` 会以指针为中心缩放。**Use scroll wheel to zoom** 恢复历史 wheel zoom,并在迁移现有配置时自动启用。只有 focused input widget 会中断 canvas wheel/pinch;在 unfocused 或不可聚焦 widget 上,事件仍归 canvas。**Wheel/pinch capture over widgets** 有 Off、On、Key 三种模式:Off 保留 focused-widget ownership,On 始终只交给 canvas,Key 仅在按住绑定键时交给 canvas。因此,focused live Browser page 在 Off 或 Key 未按下时会由普通 scroll 原生滚动;unfocused Browser、On、激活的 Key binding、完整 override、pinch 与 `Cmd/Ctrl + scroll` 都归 canvas。Browser wheel sequence 开始时选定的 owner 会保持到 250 ms 没有 wheel event。Browser summary 与 placeholder 始终归 canvas。新配置默认使用 Key,在 macOS 为 `Command`,其他平台为 `Ctrl`。独立的 **Canvas navigation override**(macOS 默认 `Option`,其他平台默认 `Alt`)接管 wheel/pinch 与 drag,也允许单独 Command/Ctrl。按住完整 override 时所有 canvas surface 显示 `grab`;已开始的 pan 会显示 `grabbing`,并由 canvas 持有到 pointer up/cancel。 - 指针停在空白 canvas 的 viewport edge `56px` 范围内时,canvas 以 RTS 风格 edge-pan;速度线性增加,到边缘达到 `900px/s`。Edge panning 默认关闭,在 Settings 启用。指针位于 interactive surface 或正在 drag-pan 时暂停。 +- 空白画布的右键菜单包含 **Tools** 子菜单,其中只有一个 **Auto-sort** 操作。排序条件(按任务:子代理放在启动它的会话右侧;按状态;按项目文件夹;网格)是同一子菜单中的单选行,不提供单独的面板、工具栏或保存的布局。当前多选中至少有两个窗口时,Auto-sort 只移动这些窗口;否则,移动所有未完全位于区域内的终端、插件与 Browser 窗口(吉祥物仅在被选中时移动);便签、素材与 Files 卡片从不移动。开启吸附时排列保持 10px 网格与 `20px` 间距,并向右或向下移动,直到不再覆盖 HOME、区域、便签、素材、Files 卡片和未参与排序的窗口。被移动的窗口在几秒内(或直到下一次按下)显示现有的选中轮廓,这只是视觉提示,不会选中它们;在未要求减少动态效果时平滑移动。只要仍有窗口停在排序放置的位置,**Undo auto-sort** 就保留在同一子菜单中,并且只恢复这些未被改动的窗口。没有全局撤销快捷键,终端与文本输入保留自己的按键。所选条件只在本次运行期间记住。 - Dialog close action 留在自己的 header/control row 内,使用一致 inset,不覆盖 field、outline 或 panel boundary。 ## 验收检查 diff --git a/docs/canvaslink-security-audit-2026-10-26.md b/docs/canvaslink-security-audit-2026-10-26.md new file mode 100644 index 000000000..1fee75ffe --- /dev/null +++ b/docs/canvaslink-security-audit-2026-10-26.md @@ -0,0 +1,101 @@ +# CanvasLink: bounded security and failure-path audit + +Reviewed on 2026-10-26, starting at `f643b31c476e490904437c95c13f0db33ad7c310`. +This report covers the embedded host, Android transport/direct-input assets, +pairing/channel implementation, IPC ownership and installation contract. It is +not a penetration-test certification or acceptance of an installed release. + +## Confirmed findings and fixes + +Severity below is a local assessment of the demonstrated behavior, not a CVSS score. + +| Finding | Preconditions and impact | Fix and regression evidence | +| --- | --- | --- | +| Medium: session expiry did not revoke an established video peer | An already paired client could stop polling and keep receiving video after the advertised 12-hour session lifetime. Request/input checks rejected expired credentials, but no passive expiry path stopped media. This did not bypass the PIN. | `Pairing.expire()` removes credentials before invoking revocation; requests and outgoing signals invoke it, and the host invokes it every 200 ms independently of client traffic and UI health polling. Tests cover the exact expiry boundary, one-time revocation, silent clients and rejected expired requests/queues. Timer delivery still depends on the Electron main event loop. | +| Medium: revocation waited for an asynchronous UI refresh | A stalled or rejected settings-state read delayed or prevented closing a live peer after a valid revoke event. | The capture worker closes tracks, peer and input ownership synchronously before refreshing its UI. A deferred/rejected refresh test proves media and control stop first. | +| Low: delayed signalling could undo client suspension/disconnect | In-flight offers could create a new peer while hidden or locally disconnected. A delayed native pairing callback could also re-arm the client after explicit disconnect. These were stale messages from an existing authenticated connection, not unauthenticated remote pairing. | Ignore offers/disconnect signals unless paired and visible; ignore pairing completions after explicit disconnect until a new connect request. Tests cover unpaired, hidden and disconnected clients, delayed pairing completion and negotiation completion after disconnect. | +| Reliability: stale SDP failure overwrote current connection state | Failure of a superseded offer could mark a healthy new connection as failed. | Peer generations and checks after asynchronous SDP steps isolate stale completion/errors. | +| Reliability: buffered pointer motion survived session close | A queued animation-frame callback could submit old pointer movement to the next connection. | Close discards buffered movement and cancels its frame callback. No clicks, text or keys are replayed. | +| Input safety: one-shot Ctrl leaked through other commits | A Unicode/multi-character IME commit or native key could leave Ctrl armed, turning the next letter into an unexpected shortcut such as Ctrl+C. | Consume Ctrl on each committed text/key event. Browser fixture covers Cyrillic, emoji, multi-character text and native arrow input. | + +Before fixes, the new regressions failed in seven cases in the first targeted +run and two further cases in the revocation-focused run. These were application +failures. A separate HTTP fixture initially lacked Node's `Buffer` global; that +test-harness error was corrected and is not an application finding. + +## Boundaries exercised + +- Real SRP mutual authentication and AES-GCM request/response code, using + synthetic keys and messages. Wrong PIN, used/expired code, attempt limits, + wrong address, tampering, reflection and replay are rejected. +- The actual HTTP request handler, driven by in-memory Node request streams: + public source addresses, browser origins (including `null`), unsupported + routes/methods, non-JSON content types, invalid JSON and oversized bodies are + rejected. An authenticated signal reaches the renderer once; a forged, + replayed, unsupported or revoked signal does not. +- IPC owner/main-frame checks and serialized settings operations, including a + window closing while a command waits. Capture is scoped to the owned window; + no global input injector or shell execution was added. +- Android navigation/resource allowlists, disabled file/content access, denied + WebView media permissions and encrypted app-private session storage were + inspected. Those native boundaries were not penetration-tested on hardware. +- Installation remains pinned to the published APK hash/certificate and stops + on signing conflicts. No private signing files or pairing credentials were + copied into the source tree or report. + +## Verification of this patch + +- **115/115 CanvasLink tests passed**, no skips; 12 tests added to the previous 103. +- **3/3 desktop installation-prompt tests passed**. +- TypeScript checks, production desktop bundles and the source/built-output + secret audit passed. +- The Android development APK compiled with SDK 35 / JDK 17; apksigner verified + its v2/v3 signatures. This is a local development artifact, not a release APK. +- Tests use simulated WebRTC/Electron objects and browser fixtures; HTTP tests + do not open a network listener. They prove the demonstrated control flow, + not real GPU capture or physical input on Windows/Linux/Android. + +The installed desktop/tablet and public `0.3.0-alpha.1` APK were not replaced. +The client-side fixes require a subsequent APK build/release; desktop fixes +require a CanvasTTY build containing this patch. No new release is claimed. + +## Open dependency issues + +Fresh `npm audit --package-lock-only --json` reports **6 vulnerable packages: +5 high, 1 moderate**, matching an audit of the unchanged upstream baseline +`0fa6895d3675b40ec4dc75886826dd2db234c234` with its own manifest/lockfile. +The package names are Electron, Vite, undici, zod, source-map-js and fast-uri. +An installed-tree audit reports 18 affected packages including dependent parent +packages; this is a different audit tree/count, not 12 newly introduced root +advisories. CanvasLink's bonjour/SRP dependencies are not listed by these audits. + +`npm audit --omit=dev` reports zero, but that **does not establish runtime safety**: +Electron is declared as a development dependency and is shipped in the desktop. +Its pinned version here is 43.7.7. The maintainer's +[Electron advisory](https://github.com/electron/electron/security/advisories/GHSA-jr5w-w6x2-84p9) +describes compromised intermediate TLS certificates; 43.8.1 contains the fix +for this release line. Exploitation requires intercepted TLS traffic and a +relevant certificate. This audit did not demonstrate exploitation in CanvasTTY. +Vite's +[file-serving advisory](https://github.com/vitejs/vite/security/advisories/GHSA-rq7h-c2jc-7f22) +requires an exposed development server and additional path/import conditions; +it is not evidence that CanvasLink's separate JSON listener serves files. + +Dependency updates and their desktop/native packaging validation remain open; +this patch does not silently change the upstream Electron/build toolchain. + +## Remaining acceptance and review gaps + +- Physical source-port acceptance on macOS, Windows, Linux and Android remains + pending. Prior prototype acceptance is not acceptance of this patch or APK. +- Native Android logout clears persisted credentials only after its queued + network revoke attempt. Abrupt process death during an offline logout and + pairing/restore cancellation need a native lifecycle regression and follow-up; + the JavaScript disconnect guard does not prove durable native logout. +- No independent cryptographic review of the SRP implementation or WebRTC stack + was performed. Passing protocol tests is not such a review. +- A paired device controls the entire owned CanvasTTY window, including its + terminals and settings. Pairing is a broad trust grant, not terminal sandboxing. + LAN/source-address checks do not protect against a compromised paired device. +- No claim of denial-of-service resistance under adversarial LAN load, + dependency remediation, zero vulnerabilities or release readiness is made. diff --git a/docs/canvaslink-verification.md b/docs/canvaslink-verification.md new file mode 100644 index 000000000..660dcdbed --- /dev/null +++ b/docs/canvaslink-verification.md @@ -0,0 +1,99 @@ +# CanvasLink alpha: verification scope + +Follow-up: [2026-10-26 security/failure-path audit](canvaslink-security-audit-2026-10-26.md) +records six fixed findings, 115 passing integration tests, dependency issues and +the remaining native/runtime acceptance gaps. The older counts below describe +the earlier source-port and distribution snapshots. + +The source port is based on upstream commit +`0fa6895d3675b40ec4dc75886826dd2db234c234` (package version 1.8.1). +It does not replace the user's installed CanvasTTY 1.8.2 or its active preview. + +## Prior prototype acceptance + +The user manually exercised the macOS/Android prototype and confirmed it worked +before publication preparation. Its installed APK SHA-256 was +`48b967d7102c8e733666a630562b420e5bee5ac3bc734ad065d7a89ffd038783`. +Prior bounded device checks covered real terminal input/ACKs, Settings touch, +native selects, resume, Wi-Fi and high-density capture on Android 12. +This is prototype evidence, not device acceptance of this source-port package. + +## Source-port checks + +- General upstream suite in an isolated source snapshot with a clean environment: + 2,221 passed, 27 skipped, zero failures. No Go toolchain was present; native-helper + parity/release coverage is not claimed. +- 103 CanvasLink tests passed: pairing/authentication/replay rejection, input + ordering and uncertainty, reconnect races, direct-input/gesture fixtures, + select/touch behavior, graphics bounds, icons and IPC/lifecycle ownership. +- TypeScript node and renderer checks passed. +- Production desktop bundles built with the integrated settings section, + preload API and copied host assets. +- Android 0.3.0-alpha.1 debug APK built; apksigner verified v2/v3 signatures. + Instrumentation entry points are excluded from the normal APK. +- Even G2: 47 tests passed; its access and pairing implementation is unchanged. +- Source and built-output secret audit passed. +- npm audit found six advisories (five high, one moderate). An audit of the + unchanged upstream lockfile reports the same six dependency names; no + dependency-security remediation is claimed by this feature port. + +The general upstream suite initially exposed a path-handling bug in existing +hook tests when the checkout path contains spaces (URL `.pathname` retains +`%20`). The affected hook tests passed through a temporary path without spaces +using Node's `--preserve-symlinks` and `--preserve-symlinks-main`. The final full +run used a real source copy without spaces and removed inherited agent-capability +environment variables. This port does not change unrelated hook handling. + +The Android HTML/CSS/JS/font assets are byte-identical to the accepted prototype. +The new normal APK omits instrumentation and carries the alpha version: +SHA-256 `509e44e12d0be251db6ed323e03035a0e2d16897c519a439679e8533ff5934fb`. +It has not replaced the installed APK. + +A macOS directory package reached ASAR creation; inspection found the host assets, +bonjour-service and secure-remote-password and no acceptance instrumentation. +Final ad-hoc signing failed with a local “internal error in Code Signing +subsystem”. No signing certificate/keychain access was requested. This is not a +successfully signed distributable. The standalone hidden Electron smoke also +failed to start to completion in this environment (timeout with no script output), +including an attempt from a disposable unprotected directory. Its runtime result +is **unverified**; the installed prototype remains the only user-accepted runtime. + +## Required release acceptance + +Real packaged-build acceptance for this source port remains pending. CI and +synthetic fixtures do not prove GPU capture, Android IME, TalkBack, rotation, +host firewalls or real device input on Windows/Linux. Official desktop release +signing, store distribution and a 30 FPS guarantee are outside this alpha. + +The `CanvasLink alpha` workflow runs the contracts and builds a downloadable +development APK. Its CI signature is ephemeral; it is not an update channel. +Run `node scripts/smoke-canvaslink.mjs` after the desktop build to exercise real +Electron preload/IPC and host enable/revoke/disable against a disposable hidden +fixture. It does not send input to user terminals or provide video acceptance. + +## Community APK distribution and onboarding (2026-10-26) + +- [CanvasLink](https://github.com/teo-nex/CanvasLink) is a separate public + distribution repository; application source remains in this PR. +- Version `0.3.0-alpha.1` is the APK from successful CI run `40235936427`, source + commit `eba7baf1f3db716b6600a605f1fda07c39e9119f`, re-signed locally with a + persistent distribution key. All ZIP entries outside `META-INF` match the CI + input byte for byte. Normal APKs contain no acceptance instrumentation. +- Distribution APK SHA-256: + `0534857896b2e22d7e832a8aaa6146b3fc6acb19f3df857cc9d3e8c1caf5a760`. + Public signing certificate SHA-256: + `0235b1aee591e4f0e24e59484927ead112889521a1b92e40959076d03635b3cb`. + The private key and password remain outside both repositories. +- Settings can copy a bilingual, pinned installation prompt and open the guide. + Copying does not enable the host, mutate pairing, install an APK or expose the + pairing code/device name. Three handler tests cover both locales and clipboard + failure; all passed. They use the repository's React test stand-in, not a live + desktop clipboard or physical Android device. +- The 103 integration tests, TypeScript checks, production build and source/built + output secret audit passed again after the settings change. +- The agent prompt selects one ADB device, verifies the APK hash, preserves app + data and stops on signature/downgrade conflicts. USB debugging and the initial + Android RSA confirmation belong to the device owner. +- Earlier debug signatures may conflict with the new distribution signature. + No APK was installed on the accepted tablet and no installed desktop was replaced. + Publication is distinct from physical acceptance of this exact APK/desktop port. diff --git a/docs/canvaslink.md b/docs/canvaslink.md new file mode 100644 index 000000000..f3c95e638 --- /dev/null +++ b/docs/canvaslink.md @@ -0,0 +1,169 @@ +# CanvasLink 1.0 for Android + +CanvasLink streams the **whole current CanvasTTY window** to an Android phone or +tablet on the same private IPv4 LAN. Touch, terminal keyboard input and shortcuts +are delivered to that window. This includes Settings and any sensitive content +visible in it. Enable access only for a device you trust. + +The host lives in **Settings → CanvasLink**. There is no separate desktop app to +install. It starts **off** on every desktop launch. The existing Web companion +and Even G2 integration retain their separate pairing and permissions. + +## Connect + +1. Install [CanvasLink 1.0](https://github.com/howdeploy/CanvasTTY/releases/tag/canvaslink-v1.0.0) + (Android 8+). **Settings → CanvasLink → Copy prompt for agent** copies USB/ADB + installation instructions with the exact APK URL and checksum. Enable USB + debugging and approve the RSA dialog on Android yourself. The agent installs + on one selected device and preserves data; it must stop on signing conflicts. + Use CanvasTTY 1.8.3 or newer. For an existing **CanvasLink DEV** installation, + choose `CanvasLink-1.0.0-dev.apk`; it preserves the DEV package ID and key. + Older alpha APKs can have incompatible signatures: stop on a signing conflict + and keep existing data. The DEV package can be installed alongside the alpha. +2. Open Settings → CanvasLink on the desktop and choose **Enable access**. +3. Open CanvasLink on Android, select the discovered computer and enter the six + digits shown on the desktop. The code expires after two minutes and is used + once. If discovery is blocked, the Android connection screen has a manual + private IPv4 address option. Both devices must be on the same non-isolated LAN. +4. Tap a terminal to open the Android keyboard. Text goes directly into the + focused terminal; Enter executes it. The shortcut bar includes paste and + interrupt. Zoom/pan can bring a terminal into view. +5. **Forget device** revokes its session and generates a new code. **Turn off + access** closes the listener, discovery, video and input. A desktop restart + requires fresh pairing. Sessions expire after twelve hours and are bound to + the client's address; a changed address can require pairing again. + +The app distinguishes processed, pending and uncertain input. A disconnect does +not automatically replay a command. Check the terminal before retrying uncertain +input. Clipboard paste uses the Android clipboard; it does not read the desktop +clipboard. + +## Local networks and VPNs + +Android selects a non-VPN Wi-Fi or Ethernet network with a route to the private +computer address. A sole local network's IPv4 gateway can also reach neighbouring +private subnets. HTTP signalling uses that network explicitly without a proxy; +there is no cellular fallback. Android 13+ also restricts discovery to these +networks; older versions retain system discovery and the manual address option. +Process binding requests the local network for new WebView sockets, but WebRTC +routing in the device's WebView must be verified on hardware. Changing networks +closes the old input channel and retries signalling without replaying input. +A changed client address may still require fresh pairing because sessions +remain address-bound. + +On Linux, desktop discovery selects physical network interfaces, including +bridges/VLANs linked to them. Other platforms filter common virtual adapter +names; macOS retains native DNS-SD publication. Bonjour advertisements are sent +on each selected LAN IPv4 interface and contain only its address. Multiple LAN +addresses may appear as separate computer entries. If discovery is unavailable, +use **Computer not found?** on Android and enter the computer's LAN IPv4 and port, +for example `192.168.0.171:3499`. + +Pairing success does not mean video/control is connected. A connection attempt +has a 30-second deadline; **Check connection** starts another attempt. Host capture +or negotiation errors are reported to the phone instead of leaving it waiting. + +VPNs must permit local traffic, including mDNS and WebRTC traffic in addition to +TCP 3499. A VPN kill switch or Android's **Block connections without VPN** can +prevent direct LAN access; CanvasLink does not change those settings. Enable the +VPN's local-network allowance or exclude CanvasLink where the VPN supports it. +These changes do not establish compatibility with every VPN or WebView version. + +## Mobile interface + +The Android shell follows CanvasTTY's Sage tokens: graphite panels, sage actions, +flat surfaces and short labels. Pairing is a compact form. During a session, only +the terminal key row reserves space; connection state and diagnostics live in the +session menu. Errors and delivery uncertainty remain visible when relevant. +Button labels are not selectable. Pairing fields and diagnostic text still +support selection; terminal gestures and native IME keep their own input path. + +## Transport and boundaries + +- Host listener: private IPv4 clients on TCP 3499; mDNS `_canvaslink._tcp` discovery. + Do not expose or forward this port to the Internet. Networks may require an + inbound firewall allowance. No firewall rule or startup service is installed. +- No cloud service, STUN or TURN. Pairing uses SRP-6a; the code is not sent as + plaintext. Signalling uses AES-256-GCM with authenticated sequence numbers; + WebRTC media/data use DTLS-SRTP/SCTP encryption. The HTTP endpoint is not HTTPS; + the application protocol supplies the encryption. +- Pairing attempts and request sizes are bounded. A code authorizes the **whole + window**, not a selected-session grant. Treat the Android device as a keyboard + and pointer with the same authority as the person at that window. +- Electron capture is restricted to CanvasTTY's own main frame; input uses its + `webContents`, never OS-wide keyboard or pointer injection. This integrated + path does not require Accessibility control of other applications. +- A hidden isolated renderer handles media. Its IPC and the settings IPC reject + other renderers and child frames. Private test sockets, startup automation, + instrumentation entry points and global input helpers are not shipped. + +## Build + +From the repository root: + +```sh +npm ci +npx playwright install chromium +npm run test:canvaslink +npm run typecheck +npm run build +``` + +For the APK, install JDK 17 and Android SDK platform 35/build-tools 35.0.0, set +`JAVA_HOME` and `ANDROID_SDK_ROOT` (or `ANDROID_HOME`), then run: + +```sh +npm run build:canvaslink:android +``` + +For a separate **CanvasLink DEV** installation that preserves an existing app, +run `npm run build:canvaslink:android -- --dev`. This produces +`integrations/canvaslink/dist/CanvasLink-1.0.0-dev.apk` with package ID +`io.nexcore.canvaslink.dev`. Use the DEV app when testing modified source. + +Output: `integrations/canvaslink/dist/CanvasLink-1.0.0.apk` (or +`CanvasLink-1.0.0-dev.apk` with `--dev`). No Gradle or Android +Java dependencies are downloaded by the build script. A local development key +is generated under the ignored `.runtime` directory. CI produces an APK artifact +with an ephemeral development signature. Different builds can have different +keys and may not install as an update over one another. Keep your existing app +and its data until you have planned the transition. + +The original CanvasLink implementation and alpha distribution are by @teo-nex: +[#166](https://github.com/howdeploy/CanvasTTY/pull/166) and the +[community distribution repository](https://github.com/teo-nex/CanvasLink). +CanvasLink 1.0 is distributed with this project using an owner-held persistent +key. Its URL, checksum and public certificate fingerprint are pinned in +`src/shared/canvasLinkInstall.ts`. The private key is never committed. CI APKs +retain ephemeral signatures and are separate from the pinned release APK. + +Maintainer follow-ups add LAN/VPN routing, bounded negotiation and recovery, +the CanvasTTY mobile shell, and versioned normal/DEV APKs. Original contributor +commits are retained in the integration history. + +## Quality and platform status + +The prototype was physically tested on macOS with an Android 12 tablet and +accepted by its user before this source port. That acceptance applies to the +prototype APK/preview, not automatically to a newly packaged desktop or APK. +See [the port verification record](canvaslink-verification.md). + +Windows and Linux have not received physical device acceptance. Their shared +Electron path and JavaScript discovery implementation are candidates for testing, +not a verified compatibility promise. CI tests contracts and builds an APK; it +cannot establish real IME, GPU capture, firewall or touch behavior on those hosts. + +Capture first tries the owned renderer stream and falls back to bounded raw +snapshots (up to 2560×1600). The prototype's observed fallback was around 3–6 FPS; +30 FPS is not promised. High density can improve text clarity at a cost in CPU +and bandwidth. Normal desktop density is unchanged. For an isolated developer +run, `--force-device-scale-factor=2` supplies density at process start, or +`--canvaslink-high-density` requests one relaunch with that switch. Never enable +this while preserving an active terminal session without planning the restart. + +Before promotion beyond alpha: test a packaged build with a physical Android +device on macOS, Windows and Linux; include discovery, pairing/revocation, +Settings touch, native selects, IME composition, Ctrl+C/paste, Unicode, rotation, +background/resume, network loss, minimized/closed windows and capture quality. + +Краткая инструкция по-русски: [canvaslink.ru.md](canvaslink.ru.md). diff --git a/docs/canvaslink.ru.md b/docs/canvaslink.ru.md new file mode 100644 index 000000000..264e6d030 --- /dev/null +++ b/docs/canvaslink.ru.md @@ -0,0 +1,67 @@ +# CanvasLink 1.0: Android по локальной сети + +Экспериментальная интеграция: планшет показывает **всё текущее окно CanvasTTY** +и управляет им. Доступ включает терминалы, настройки и видимые секреты. +Подключайте только своё доверенное устройство. + +1. Установите [CanvasLink 1.0 на Android 8+](https://github.com/howdeploy/CanvasTTY/releases/tag/canvaslink-v1.0.0). + В **Настройки → CanvasLink** есть кнопка **«Скопировать промпт агенту»**: + она копирует инструкцию по USB/ADB с точной ссылкой и SHA-256 APK. + Владелец включает USB-отладку и подтверждает RSA-запрос на Android; + агент устанавливает приложение на выбранное устройство с сохранением данных. + Нужна CanvasTTY 1.8.3 или новее. Для обновления существующего **CanvasLink DEV** + выберите `CanvasLink-1.0.0-dev.apk`: сохраняются идентификатор приложения и ключ. + При конфликте подписи старой alpha остановитесь, не удаляя данные; + DEV-приложение можно установить рядом с ней. +2. На компьютере откройте **Настройки → CanvasLink → Включить доступ**. +3. Откройте CanvasLink на планшете, выберите найденный компьютер и введите код. + Оба устройства должны быть в одной локальной сети без изоляции клиентов. + Ручной адрес нужен только при недоступном обнаружении. +4. Коснитесь терминала: появится клавиатура. Ввод передаётся напрямую; + Enter выполняет команду. В панели есть вставка и прерывание. +5. «Забыть устройство» отзывает сессию. «Выключить доступ» останавливает сеть, + видео и управление. При новом запуске компьютера доступ выключен. + +Одноразовый код действует две минуты. Сессия — до двенадцати часов; смена адреса +может потребовать нового подключения. При обрыве команды не повторяются +автоматически: сначала проверьте терминал. Вставка берёт буфер Android. + +При включённом VPN Android выбирает локальную сеть Wi-Fi/Ethernet с маршрутом +к адресу ПК; HTTP использует её напрямую без прокси и без перехода на мобильную +сеть. На Android 13+ поиск также ограничен этими сетями; на старых версиях +сохранены системный поиск и ручной адрес. На Linux обнаружение ПК привязано +к физическим LAN-интерфейсам. Если компьютер не найден, раскройте +**«Компьютер не находится?»** на телефоне и введите его локальный IPv4 с портом, +например `192.168.0.171:3499`. + +Сопряжение и соединение видео/управления — разные этапы. Ожидание подключения +ограничено 30 секундами; кнопка **«Проверить связь»** запускает новую попытку. +При смене сети старый канал ввода закрывается; команды не воспроизводятся. +Маршрутизацию WebRTC внутри WebView нужно отдельно проверить на устройстве. + +VPN должен разрешать локальные подключения: одного TCP-порта 3499 недостаточно +для обнаружения и WebRTC. Kill switch и системное **«Блокировать соединения без +VPN»** могут запрещать прямую связь. CanvasLink не меняет эти настройки: +разрешите локальную сеть или добавьте приложение в исключения, если VPN это +поддерживает. Совместимость со всеми VPN и версиями WebView не гарантирована. + +Отдельное приложение на компьютере не требуется. Очки Even G2 и Web companion +сохраняют свои отдельные коды и разрешения. Порт 3499 не открывайте в Интернет. + +Исходная версия проверялась пользователем на Mac с планшетом Android 12; +интеграционная LAN/VPN-версия получила положительный отзыв на Linux с Android. +Это отдельные пользовательские проверки; полная проверка всех платформ +ещё не завершена, плавность 30 FPS не обещается. +Отладочные APK разных сборок могут иметь разные подписи и не устанавливаться +поверх друг друга. Рабочую установленную версию сохраняйте до проверки новой. + +Исходная реализация CanvasLink — вклад @teo-nex в #166; предыдущая alpha +доступна в его репозитории [CanvasLink](https://github.com/teo-nex/CanvasLink). +Версия 1.0 публикуется здесь с постоянной подписью владельца проекта. +URL, SHA-256 и отпечаток сертификата закреплены в `src/shared/canvasLinkInstall.ts`. +Наши дополнения: LAN/VPN-маршрутизация, ограниченное ожидание подключения, +восстановление без повторения ввода, мобильная оболочка и сборки normal/DEV. +Исходные коммиты автора сохранены. Приватный ключ в Git не включается. +При несовместимой подписи агент останавливается: удаление приложения и данных не выполняется. + +Команды сборки, устройство защиты и границы проверки: [canvaslink.md](canvaslink.md). diff --git a/docs/getting-started.md b/docs/getting-started.md index 73e52dca7..c57f3b4f8 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -33,6 +33,14 @@ npm run dev Launch modes are **Auto** (the default), **Manual**, **Accept edits**, **Plan** and **Bypass**, each offered only where that CLI supports it; the protection layers around them are described in [Installing and security](installing-and-security.md#agent-protection-layers). **Bypass** (YOLO) disables provider safety prompts where the provider supports such a mode, still inside base protection and agent isolation. For OpenCode, CanvasTTY applies a launch-only inline `permission: "allow"` override while preserving the rest of the merged OpenCode configuration. Hermes receives its native `--yolo` flag, while Grok Build receives its native `--always-approve` flag for that launch. CanvasTTY asks you to acknowledge it once per CLI and never gives it to a subagent; use it only in a directory you are willing to let the agent modify. +## Animated terminal backgrounds + +Choose a background in **Settings → Appearance → Terminal backgrounds**. To see the animation in OpenCode, you must select its system theme: enter `/theme` inside OpenCode and choose **system**. It uses the terminal's default background; regular OpenCode themes paint their own opaque background over the animation. See [OpenCode themes](https://docs.opencode.ai/docs/themes/). + +For other TUI agents that paint their own screen background, choose the equivalent system-theme, terminal-default-background or transparency mode, where supported. This is needed to show the animated background, not to run the agent normally in CanvasTTY. Individual TUI panels, input fields and selections may retain their own backgrounds. + +**Terminal backgrounds → Adjust → Text readability** adds a dark backdrop behind text. If it looks like dark strips over the animation, reduce **Darkness behind text** or turn readability off to compare. Clipped or missing parts of the glyphs themselves are not an expected effect. + ## Terminal input and controls - Press any live terminal card to select and focus it. Input focus is independent from selection, so future multi-selection can keep one unambiguous keyboard and wheel target. diff --git a/docs/getting-started.ru.md b/docs/getting-started.ru.md index 64d15927d..0627105a3 100644 --- a/docs/getting-started.ru.md +++ b/docs/getting-started.ru.md @@ -33,6 +33,14 @@ npm run dev Режимы запуска — **Авто** (по умолчанию), **Вручную**, **Правки**, **План** и **Обход**; каждый предлагается только там, где его поддерживает CLI. Слои защиты вокруг них описаны в разделе [Установка и безопасность](installing-and-security.ru.md#слои-защиты-агентов). **Обход** (YOLO) отключает защитные запросы провайдера там, где провайдер такой режим поддерживает, и всё равно остаётся внутри базовой защиты и изоляции агентов. Для OpenCode CanvasTTY добавляет только на время запуска inline override `permission: "allow"`, сохраняя остальную объединённую конфигурацию OpenCode. Hermes получает штатный `--yolo`, а Grok Build — штатный `--always-approve` только для этого запуска. CanvasTTY просит подтвердить его один раз для каждого CLI и никогда не передаёт субагенту; используйте его только в каталоге, который вы готовы отдать агенту на изменение. +## Анимированные фоны терминалов + +Фон выбирается в **Настройки → Оформление → Фоны терминалов**. Чтобы анимация была видна в OpenCode, обязательно выберите внутри него системную тему: введите `/theme` и выберите **system**. Эта тема использует стандартный фон терминала; обычные темы OpenCode рисуют собственную непрозрачную заливку поверх анимации. Подробнее: [темы OpenCode](https://docs.opencode.ai/docs/themes/). + +Для других TUI-агентов, которые заливают экран своим цветом, выберите аналогичный режим системной темы, стандартного фона терминала или прозрачности, если он поддерживается. Это условие отображения анимированного фона, а не требование для обычного запуска агента в CanvasTTY. Собственная заливка отдельных панелей, поля ввода и выделений TUI может сохраняться. + +Параметр **Фоны терминалов → Настройка → Читаемость текста** добавляет затемнение под текстом. Если оно выглядит как тёмные полоски на анимации, уменьшите **Затемнение под текстом** или выключите читаемость, чтобы сравнить. Обрезанные или пропавшие части самих символов не являются ожидаемым эффектом. + ## Ввод и управление терминалом - Нажмите на любую живую карточку терминала, чтобы выбрать и сфокусировать её. Input focus не зависит от selection, поэтому будущий multi-selection сохранит одну однозначную цель для keyboard и wheel. diff --git a/docs/getting-started.zh-CN.md b/docs/getting-started.zh-CN.md index 5f5ccdf9b..ac7545c6a 100644 --- a/docs/getting-started.zh-CN.md +++ b/docs/getting-started.zh-CN.md @@ -33,6 +33,14 @@ npm run dev 启动模式有 **Auto**(默认)、**Manual**、**Accept edits**、**Plan** 与 **Bypass**,只在对应 CLI 支持时提供;它们外面的防护层见[安装与安全](installing-and-security.zh-CN.md#智能体防护层)。在服务商支持的情况下,**Bypass**(YOLO)会关闭其安全确认提示,但仍处于基础防护与智能体隔离之内。对于 OpenCode,CanvasTTY 只在本次启动中添加 inline `permission: "allow"` override,并保留其余合并后的 OpenCode 配置;Hermes 接收原生 `--yolo`,Grok Build 接收原生 `--always-approve`,且都只作用于本次启动。CanvasTTY 会要求你为每个 CLI 确认一次,且不会交给子智能体;只在你愿意让智能体改动的目录中使用它。 +## 终端动画背景 + +在 **Settings → Appearance → Terminal backgrounds** 中选择背景。要在 OpenCode 中显示动画,必须选择其系统主题:在 OpenCode 中输入 `/theme` 并选择 **system**。该主题使用终端默认背景;普通 OpenCode 主题会在动画上绘制自己的不透明背景。参见 [OpenCode 主题文档](https://docs.opencode.ai/docs/themes/)。 + +对于会自行填充屏幕背景的其他 TUI 智能体,请在支持的情况下选择对应的系统主题、终端默认背景或透明模式。这是显示动画背景的条件,并非在 CanvasTTY 中正常运行智能体的要求。TUI 的面板、输入框和选中区域仍可能保留各自的背景。 + +**Terminal backgrounds → Adjust → Text readability** 会在文字后面添加暗色衬底。如果动画上出现暗色条带,可降低 **Darkness behind text**,或关闭可读性选项进行比较。字符本身被截断或缺失并非预期效果。 + ## 终端输入与控制 - 点击任意实时终端卡片即可选中并聚焦它。Input focus 与 selection 相互独立,因此未来加入 multi-selection 后仍能保持唯一明确的 keyboard 与 wheel 目标。 diff --git a/electron-builder.yml b/electron-builder.yml index ab72403ef..78076ed60 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -73,6 +73,8 @@ electronFuses: enableNodeCliInspectArguments: false enableEmbeddedAsarIntegrityValidation: true extraResources: + - from: node_modules/ascii.rest/LICENSE + to: licenses/ascii-rest.txt - from: assets/mascot-pipeline to: mascot-pipeline filter: diff --git a/electron.vite.config.ts b/electron.vite.config.ts index eefc35363..8b058fb9c 100644 --- a/electron.vite.config.ts +++ b/electron.vite.config.ts @@ -1,4 +1,5 @@ import { resolve } from "node:path"; +import { cpSync, mkdirSync } from "node:fs"; import react from "@vitejs/plugin-react"; import { defineConfig, externalizeDepsPlugin } from "electron-vite"; @@ -11,7 +12,14 @@ export default defineConfig({ || "" ) }, - plugins: [externalizeDepsPlugin()], + plugins: [externalizeDepsPlugin(), { + name: "canvaslink-host-assets", + closeBundle() { + const destination = resolve("out/main/canvaslink/host"); + mkdirSync(destination, { recursive: true }); + cpSync(resolve("integrations/canvaslink/host"), destination, { recursive: true }); + } + }], build: { rollupOptions: { input: resolve("src/main/index.ts") @@ -37,6 +45,7 @@ export default defineConfig({ renderer: { root: resolve("src/renderer"), plugins: [react()], + worker: { format: "es" }, // electron-vite leaves every bundle unminified; the renderer's (React, xterm and the app, about 1.8 MB) // is parsed on every window load, so it is minified. Source maps stay off, as before. build: { diff --git a/integrations/canvaslink/.gitignore b/integrations/canvaslink/.gitignore new file mode 100644 index 000000000..172582f4f --- /dev/null +++ b/integrations/canvaslink/.gitignore @@ -0,0 +1,7 @@ +node_modules/ +build/ +dist/ +.runtime/ +*.log +verification/*.png +verification/icon-previews.json diff --git a/integrations/canvaslink/LICENSE b/integrations/canvaslink/LICENSE new file mode 100644 index 000000000..d57e4c69a --- /dev/null +++ b/integrations/canvaslink/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 CanvasLink contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/integrations/canvaslink/THIRD_PARTY_NOTICES.md b/integrations/canvaslink/THIRD_PARTY_NOTICES.md new file mode 100644 index 000000000..68d498468 --- /dev/null +++ b/integrations/canvaslink/THIRD_PARTY_NOTICES.md @@ -0,0 +1,24 @@ +# Dependencies + +- Electron 43.7.7 — MIT; Chromium and other bundled notices remain inside the + Electron distribution (`LICENSE`, `LICENSES.chromium.html`). +- bonjour-service 1.3.0 — MIT. +- secure-remote-password 0.3.1 — MIT. + +Transitive dependency licenses remain in their installed package directories. +The CanvasTTY desktop bundle retains production dependency licenses and Electron's +original bundled notices. Android uses platform APIs and contains no embedded +third-party Java libraries. No RustDesk or spacedesk source or binaries are used. + +## Onest + +Bundled Android UI font, from google/fonts/ofl/onest (Onest Project Authors). +SIL Open Font License 1.1; full license: `android/assets/onest-OFL.txt`. +Source: https://github.com/google/fonts/tree/main/ofl/onest + +## Lucide icons + +Android inline SVG symbols reuse official Lucide geometry from CanvasTTY's +`src/renderer/src/assets/icons/lucide`. Keyboard, corner-down-left and delete were +retrieved from https://github.com/lucide-icons/lucide/tree/main/icons on 2026-10-26. +ISC / MIT notices are bundled as `android/assets/lucide-LICENSE.txt`. diff --git a/integrations/canvaslink/android/AndroidManifest.xml b/integrations/canvaslink/android/AndroidManifest.xml new file mode 100644 index 000000000..8f2b77a32 --- /dev/null +++ b/integrations/canvaslink/android/AndroidManifest.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/integrations/canvaslink/android/assets/client.js b/integrations/canvaslink/android/assets/client.js new file mode 100644 index 000000000..0a62c1bf5 --- /dev/null +++ b/integrations/canvaslink/android/assets/client.js @@ -0,0 +1,50 @@ +(()=>{ + 'use strict'; + const $=id=>document.getElementById(id),api=window.CanvasLinkClient,input=window.CanvasLinkInput; + let state=api.getState(),selected='',signature='',lastPointer=null,touchTimer,noticeTimer; + const put=(id,value)=>{if($(id)&&$(id).textContent!==value)$(id).textContent=value;}; + const errors={'delivery-uncertain':'Доставка не подтверждена. Проверьте терминал. Ввод остановлен, повтора не будет.','input-busy':'Канал занят. Ввод остановлен. Коснитесь терминала, чтобы продолжить.','not-connected':'Связь прервана. Ввод остановлен.','text-target-required':'Коснитесь терминала или текстового поля.','invalid-code':'Введите шесть цифр из Настройки → CanvasLink.','input-rejected':'Компьютер отклонил ввод.','lan-connection-timeout':'Сопряжение выполнено, но канал видео и управления не подключился за 30 секунд. Проверьте общую локальную сеть и доступ к ней в настройках VPN, затем повторите подключение.','lan-network-unavailable':'Локальная сеть недоступна. Подключитесь к Wi-Fi или Ethernet.','host-connection-failed':'Компьютеру не удалось подготовить передачу окна. Проверьте CanvasTTY и повторите подключение.','ice-timeout':'Не удалось подготовить прямое соединение. Проверьте доступ к локальной сети в настройках VPN и повторите подключение.'}; + function notice(value){put('input-status',errors[value]||value);$('input-status').hidden=!value;clearTimeout(noticeTimer);if(value)noticeTimer=setTimeout(()=>{$('input-status').hidden=true;},7000);} + function updateConnect(){const busy=['connecting','pairing','resuming'].includes(state.connection);$('connect').disabled=busy||!/^\d{6}$/.test($('pin').value)||(!selected&&!($('manual').open&&$('address').value.trim()));$('connect').querySelector('span').textContent=busy?'Подключаем…':'Подключиться';} + function renderComputers(s){ + const list=s.computers||[];if(!list.some(c=>c.id===selected))selected=list.length===1?list[0].id:''; + const next=JSON.stringify(list.map(c=>[c.id,c.name])); + if(next!==signature){signature=next;$('computers').replaceChildren();for(const c of list){const button=document.createElement('button');button.type='button';button.dataset.computerId=c.id;button.className='computer';const name=document.createElement('span');name.textContent=String(c.name||'CanvasTTY').replace(/^CanvasLink\s+/,'');button.append($('computer-icon').content.cloneNode(true),name,$('computer-check').content.cloneNode(true));button.onclick=()=>{selected=c.id;renderComputers(state);updateConnect();};$('computers').append(button);}} + for(const b of $('computers').querySelectorAll('button'))b.setAttribute('aria-pressed',String(b.dataset.computerId===selected)); + $('pair-status').hidden=!!list.length;put('pair-status',s.discovery==='searching'?'Ищем компьютер в вашей Wi-Fi сети…':'Включите CanvasLink в настройках CanvasTTY на компьютере.'); + } + function render(s){ + state=s;const show=s.connection==='connected'||['connecting','reconnecting','suspended'].includes(s.connection)||(!['idle','error'].includes(s.connection)&&!$('workspace').hidden); + $('workspace').hidden=!show;$('pairing').hidden=show;document.body.classList.toggle('is-workspace',show);renderComputers(s);updateConnect(); + const labels={connected:'Подключено',connecting:'Подключаем…',pairing:'Проверяем код…',resuming:'Восстанавливаем…',reconnecting:'Нет связи',suspended:'На паузе'}; + put('connection-status',labels[s.connection]||'Нет связи');$('connection-pill').dataset.state=s.connection; + for(const button of $('controls').querySelectorAll('button'))if(button.id!=='menu-toggle')button.disabled=s.connection!=='connected'; + const stats=s.stats||{}; + const wait=s.connection!=='connected'||!stats.framesDecoded; // A static frame remains usable; do not cover it with an opaque waiting card. + $('waiting').hidden=!wait;$('resume').hidden=!['reconnecting','suspended'].includes(s.connection); + put('waiting-title',s.connection==='suspended'?'Соединение на паузе':s.connection==='reconnecting'?'Восстанавливаем связь':'Открываем CanvasTTY'); + put('waiting-copy',s.connection==='reconnecting'?'Ввод остановлен. Ничего не отправляем повторно.':s.connection==='connecting'?'Сопряжение выполнено. Подключаем видео и управление по локальной сети…':'Изображение появится здесь.'); + put('metrics',`Видео ${stats.width||'—'} × ${stats.height||'—'}\nКадров/с ${Number.isFinite(stats.framesPerSecond)?stats.framesPerSecond.toFixed(1):'—'}\nRTT канала ${Number.isFinite(stats.rtt)?Math.round(stats.rtt)+' мс':'—'}\nОжидают ACK ${s.pending||0}`); + const error=s.error?errors[s.error]||s.error:'';put('pair-error',show?'':error);put('stream-error',show?error:'');$('stream-error').hidden=!show||!error; + } + $('pair-form').onsubmit=e=>{e.preventDefault();try{if(api.connect({code:$('pin').value,computerId:selected||undefined,host:$('manual').open?$('address').value.trim()||undefined:undefined})){$('pin').value='';document.activeElement?.blur();api.hideKeyboard();}}catch(error){put('pair-error',errors[error.message]||error.message);}updateConnect();}; + $('pin').oninput=updateConnect;$('address').oninput=updateConnect;$('manual').ontoggle=updateConnect;$('rescan').onclick=()=>api.discover();$('resume').onclick=()=>api.resume(); + // Pointer-down never steals the native terminal IME focus. + $('controls').addEventListener('pointerdown',e=>{if(e.target.closest('button'))e.preventDefault();}); + document.addEventListener('contextmenu',e=>{if(e.target.closest('button,summary,.keybar'))e.preventDefault();}); + for(const b of document.querySelectorAll('[data-key]'))b.onclick=()=>input.key(b.dataset.key,b.dataset.control?['control']:[]); + $('ctrl').onclick=()=>input.toggleControl();$('paste').onclick=()=>input.paste(); + $('keyboard-toggle').onclick=()=>{if(input.active())input.stop();else notice('Коснитесь терминала — клавиатура откроется сама.');}; + $('menu-toggle').onclick=()=>{input.stop();$('session-menu').showModal();};$('menu-close').onclick=()=>$('session-menu').close(); + $('diagnostics-toggle').onclick=()=>{$('session-menu').close();$('diagnostics').showModal();};$('diagnostics-close').onclick=()=>$('diagnostics').close(); + $('disconnect').onclick=()=>{$('session-menu').close();$('disconnect-dialog').showModal();};$('cancel-disconnect').onclick=()=>$('disconnect-dialog').close();$('confirm-disconnect').onclick=()=>{$('disconnect-dialog').close();input.stop();api.disconnect();}; + for(const id of ['fit','mode','reset-view'])$(id).addEventListener('click',()=>{$('session-menu').close();}); + window.addEventListener('canvaslink:tap',e=>{if(document.querySelector('dialog[open]')||state.connection!=='connected')return;input.open(e.detail.focus);}); + window.addEventListener('canvaslink:notice',e=>notice(e.detail)); + window.addEventListener('canvaslink:input-state',e=>{const d=e.detail;$('ctrl').setAttribute('aria-pressed',String(d.control));$('keyboard-toggle').setAttribute('aria-pressed',String(d.active));$('keyboard-toggle').setAttribute('aria-label',d.active?'Скрыть клавиатуру':'Как открыть клавиатуру');$('workspace').classList.toggle('is-typing',d.active);if(d.composition!==undefined){put('preedit',d.composition);$('preedit').hidden=!d.composition;}if(d.focus)state={...state,source:{...state.source,...d.focus,target:d.focus.kind}};}); + function pointerPosition(p){const video=$('video'),b=video.getBoundingClientRect(),v=$('viewport').getBoundingClientRect(),w=video.videoWidth||state.stats?.width||1280,h=video.videoHeight||state.stats?.height||800,k=Math.min(b.width/w,b.height/h);return{x:b.left-v.left+(b.width-w*k)/2+p.x*w*k,y:b.top-v.top+(b.height-h*k)/2+p.y*h*k};} + function paint(p,pulse){const q=pointerPosition(p);$('local-cursor').hidden=!p.trackpad;$('local-cursor').style.transform=`translate(${q.x-13}px,${q.y-13}px)`;if(pulse&&p.action==='down'){const f=$('touch-feedback');clearTimeout(touchTimer);f.hidden=false;f.style.transform=`translate(${q.x-16}px,${q.y-16}px)`;touchTimer=setTimeout(()=>{f.hidden=true;},280);}} + window.addEventListener('canvaslink:pointer',e=>{lastPointer=e.detail;paint(lastPointer,true);});window.addEventListener('canvaslink:view',e=>{if(lastPointer){lastPointer.trackpad=e.detail.trackpad;paint(lastPointer,false);}}); + const resize=()=>{document.documentElement.style.setProperty('--app-height',Math.max(120,Math.round(window.visualViewport?.height||innerHeight))+'px');if(lastPointer)paint(lastPointer,false);};window.visualViewport?.addEventListener('resize',resize);window.addEventListener('resize',resize);resize(); + api.attachVideo($('video'));api.subscribe(render); +})(); diff --git a/integrations/canvaslink/android/assets/delivery.js b/integrations/canvaslink/android/assets/delivery.js new file mode 100644 index 000000000..cb1537500 --- /dev/null +++ b/integrations/canvaslink/android/assets/delivery.js @@ -0,0 +1,25 @@ +/* Transport contract v2. No DOM dependencies; shared by the client and tests. */ +(function(root,factory){if(typeof module==='object')module.exports=factory();else root.CanvasLinkDelivery=factory();})(globalThis,()=>{ + class Delivery { + constructor({channel,onChange=()=>{},timeout=65000,setTimer=(fn,ms)=>setTimeout(fn,ms),clearTimer=token=>clearTimeout(token),id=()=>crypto.randomUUID()}){Object.assign(this,{channel,onChange,timeout,setTimer,clearTimer,id});this.pending=new Map();this.sent=0;this.acked=0;this.seq=0;} + send(input){ + const dc=this.channel(); + if(!dc||dc.readyState!=='open')return Promise.reject(Error('not-connected')); + if(dc.bufferedAmount>65536||this.pending.size>=64)return Promise.reject(Error('input-busy')); + const id=this.id(); + return new Promise((resolve,reject)=>{ + const entry={resolve,reject,input,timer:this.setTimer(()=>this.finish(id,false,'delivery-uncertain'),this.timeout)}; + this.pending.set(id,entry); + try{dc.send(JSON.stringify({...input,id,seq:++this.seq}));this.sent++;this.onChange({id,status:'pending',kind:input.type,pending:this.pending.size});} + catch{this.finish(id,false,'not-sent');} + }); + } + finish(id,ok,error,focus){const entry=this.pending.get(id);if(!entry)return false;this.pending.delete(id);this.clearTimer(entry.timer);if(ok)this.acked++; + this.onChange({id,status:ok?'processed':error==='delivery-uncertain'?'uncertain':'failed',kind:entry.input.type,error:error||'',pending:this.pending.size}); + if(ok)entry.resolve({id,status:'processed',...(focus?{focus}:{})});else entry.reject(Error(error||'input-rejected'));return true; + } + receive(value){if(!['input-ok','input-error'].includes(value?.type))return false;return this.finish(value.id,value.type==='input-ok',value.error,value.focus);} + close(){for(const id of [...this.pending.keys()])this.finish(id,false,'delivery-uncertain');} + } + return {Delivery}; +}); diff --git a/integrations/canvaslink/android/assets/direct-input.js b/integrations/canvaslink/android/assets/direct-input.js new file mode 100644 index 000000000..ec04faa66 --- /dev/null +++ b/integrations/canvaslink/android/assets/direct-input.js @@ -0,0 +1,41 @@ +/* Ordered, immediate terminal input. Only the peer owns the queue; never replay. */ +(()=>{ + const api=window.CanvasLinkClient; + let token='',control=false,inflight=0; + const notify=detail=>window.dispatchEvent(new CustomEvent('canvaslink:input-state',{detail:{active:!!token,control,inflight,...detail}})); + function stop(focus){token='';control=false;api.hideKeyboard();notify({composition:'',...(focus?{focus}:{})});} + function fail(error){stop();window.dispatchEvent(new CustomEvent('canvaslink:notice',{detail:error.message||String(error)}));} + function send(value){ + if(api.getState().connection!=='connected')return Promise.reject(Error('not-connected')); + const owner=token;inflight++;notify(); + let promise;try{promise=api.sendInput(value);}catch(error){promise=Promise.reject(error);} + return Promise.resolve(promise).then(result=>{if(result?.status!=='processed')throw Error('delivery-uncertain');return result;}).catch(error=>{if(owner===token)fail(error);throw error;}).finally(()=>{inflight--;notify();}); + } + window.CanvasLinkInput=Object.freeze({ + open(focus){if(!focus?.editable)return stop(focus);token=crypto.randomUUID();control=false;notify({focus,composition:''});if(typeof Native?.showKeyboard==='function')Native.showKeyboard(token);}, + stop, + toggleControl(){control=!control;notify();}, + key(key,modifiers=[]){ + if(control&&!modifiers.includes('control'))modifiers=[...modifiers,'control'];control=false;notify(); + if(typeof Native?.terminalKey==='function'&&token)Native.terminalKey(key,JSON.stringify(modifiers));else send({type:'key',key,modifiers}).catch(()=>{}); + }, + paste(){control=false;notify();if(token&&typeof Native?.paste==='function')Native.paste();else window.dispatchEvent(new CustomEvent('canvaslink:notice',{detail:'Сначала коснитесь терминала или текстового поля для вставки.'}));}, + receive(value){ + if(!token||value?.token!==token)return; + if(value.type==='composition'){notify({composition:value.text||''});return;} + if(value.type==='notice'){window.dispatchEvent(new CustomEvent('canvaslink:notice',{detail:value.text}));return;} + if(value.type==='paste'){control=false;notify();send({type:'text',text:value.text}).catch(()=>{});return;} + if(value.type==='text'){ + const useControl=control;control=false;notify(); + if(useControl&&[...value.text].length===1&&/^[a-z]$/i.test(value.text)){send({type:'key',key:value.text.toLowerCase(),modifiers:['control']}).catch(()=>{});} + else send({type:'text',text:value.text}).catch(()=>{}); + } + if(value.type==='key'){ + let modifiers=value.modifiers||[];if(control&&!modifiers.includes('control'))modifiers=[...modifiers,'control']; + control=false;notify();send({type:'key',key:value.key,modifiers}).catch(()=>{}); + } + }, + active:()=>!!token + }); + api.subscribe(state=>{if(state.connection!=='connected'&&token)stop();}); +})(); diff --git a/integrations/canvaslink/android/assets/gestures.js b/integrations/canvaslink/android/assets/gestures.js new file mode 100644 index 000000000..c31fbef60 --- /dev/null +++ b/integrations/canvaslink/android/assets/gestures.js @@ -0,0 +1,289 @@ +/* Local interaction layer. CanvasLinkClient v2 and the pointer DOM contract stay stable. */ +(() => { + 'use strict'; + const $ = id => document.getElementById(id); + const api = window.CanvasLinkClient; + const viewport = $('viewport'), video = $('video'); + const touches = new Map(); + const clamp = (value, min, max) => Math.max(min, Math.min(max, value)); + const editor = target => target instanceof Element && (target.isContentEditable || + Boolean(target.closest('input,textarea,select,[contenteditable]:not([contenteditable="false"])'))); + const control = target => target instanceof Element && + Boolean(target.closest('button,input,textarea,select,a,summary,[role="button"],[contenteditable]')); + let scale = 1, offset = {x: 0, y: 0}, trackpad = false; + let pointer = {x: .5, y: .5}, drag = false, press = null, pinch = null; + let composing = false, pairFrame = 0, holdTimer = 0; + let tapEpoch=0, focusRect=null, focusCaret=null, focusKind='',focusFont=14,focusViewportWidth=0,focusContext=null,knownTerminal=null,autoFramePending=true,focusedView=false,beforeFocus=null,remoteSurface='canvas',remoteViewportWidth=0,dragRects=[]; + const validRect=r=>r&&['x','y','width','height'].every(k=>Number.isFinite(r[k]))&&r.width>0&&r.height>0; + + const connected = () => api.getState().connection === 'connected'; + const canInput = () => connected() && !composing && !editor(document.activeElement); + // Never queue, retry, or turn a rejected/uncertain delivery into another input. + function deliver(send) { + try { return Promise.resolve(send()).catch(() => undefined); } catch (_) { return Promise.resolve(); } + } + function send(value, cleanup = false) { + if (cleanup ? connected() : canInput()) return deliver(() => api.sendInput(value)); + } + function mouse(action, at, extra = {}) { + // A held button must still be released if focus has just entered an editor. + const cleanup = action === 'up' && drag; + if (!(cleanup ? connected() : canInput())) return; + pointer = {x: at.x, y: at.y}; + window.dispatchEvent(new CustomEvent('canvaslink:pointer', { + detail: {...pointer, action, trackpad} + })); + return send({type: 'pointer', action, ...pointer, button: 0, ...extra}, cleanup); + } + + // The element fills the viewport, but object-fit:contain can leave letterboxes. + // Its transformed bounding box already includes our centre-origin scale/offset. + function contentBox() { + const rect = video.getBoundingClientRect(); + const vw = video.videoWidth || 1280, vh = video.videoHeight || 800; + const fit = Math.min(rect.width / vw, rect.height / vh); + const width = vw * fit, height = vh * fit; + return {rect, width, height, left: rect.left + (rect.width - width) / 2, + top: rect.top + (rect.height - height) / 2}; + } + function position(x, y) { + const box = contentBox(); + return {x: clamp((x - box.left) / Math.max(1, box.width), 0, 1), + y: clamp((y - box.top) / Math.max(1, box.height), 0, 1)}; + } + function transform() { + video.style.transform = `translate(${offset.x}px,${offset.y}px) scale(${scale})`; + const percentage = `${Math.round(scale * 100)}%`; + if($('view-toggle')){$('view-toggle').disabled=!connected()||!focusedView&&scale===1&&(!knownTerminal||remoteSurface==='settings');$('view-toggle').setAttribute('aria-label',focusedView||scale>1?'Показать весь Canvas':'Приблизить активный терминал');$('view-toggle-label').textContent=focusedView||scale>1?'Весь Canvas':'Терминал';} + if ($('zoom-label')) $('zoom-label').textContent = percentage; + $('fit').setAttribute('aria-label', `${percentage} — ${scale === 1 ? 'увеличить окно' : 'вписать окно целиком'}`); + if ($('mode-label')) $('mode-label').textContent = trackpad ? 'Тачпад' : 'Касание'; + $('mode').setAttribute('aria-pressed', String(trackpad)); + if ($('gesture-hint')) $('gesture-hint').textContent = trackpad + ? 'Палец — курсор · касание — клик · удержание — перетаскивание · два пальца — прокрутка · щипок — масштаб' + : 'Касание — клик · движение — перетаскивание · два пальца — масштаб и обзор'; + window.dispatchEvent(new CustomEvent('canvaslink:view', { + detail: {scale, offset: {...offset}, trackpad,...(focusedView?{target:focusKind}:{})} + })); + } + function zoomView(nextScale, anchor, nextAnchor = anchor, base = {scale, offset: {...offset}}) { + autoFramePending=false;focusedView=false; + const box = contentBox(), bounds = viewport.getBoundingClientRect(); + const centre = {x: box.rect.left + box.rect.width / 2 - offset.x, + y: box.rect.top + box.rect.height / 2 - offset.y}; + const width = box.width / scale, height = box.height / scale; + scale = clamp(nextScale, 1, 8); + const ratio = scale / base.scale; + const maxX = Math.max(0, (width * scale - bounds.width) / 2); + const maxY = Math.max(0, (height * scale - bounds.height) / 2); + offset = { + x: clamp(nextAnchor.x - centre.x - (anchor.x - centre.x - base.offset.x) * ratio, -maxX, maxX), + y: clamp(nextAnchor.y - centre.y - (anchor.y - centre.y - base.offset.y) * ratio, -maxY, maxY) + }; + transform(); + } + + function frameFocus(){ + if(!focusedView||!focusRect)return; + const r=viewport.getBoundingClientRect(),vw=video.videoWidth||1280,vh=video.videoHeight||800; + const fit=Math.min(r.width/vw,r.height/vh),width=vw*fit,height=vh*fit; + const field=focusKind==='text-field',typing=!!window.CanvasLinkInput?.active(); + const remoteWidth=focusViewportWidth||vw; + scale=field?clamp(Math.min(r.width*.92/(Math.max(.08,focusRect.width)*width),14/(width/remoteWidth*Math.max(10,focusFont))),1,4):clamp(Math.min(r.width*.96/(Math.max(.08,focusRect.width)*width),typing?8:r.height*.92/(Math.max(.08,focusRect.height)*height)),1,8); + const maxX=Math.max(0,(width*scale-r.width)/2),maxY=Math.max(0,(height*scale-r.height)/2); + const context=field&&validRect(focusContext)&&focusContext.width*width*scale{ + const d=e.detail; + if(d.focus){remoteSurface=d.focus.surface||'canvas';dragRects=d.focus.dragRects||[];} + if(['terminal','text-field'].includes(d.focus?.kind)&&d.focus.editable&&validRect(d.focus.rect)){ + if(!focusedView)beforeFocus={scale,offset:{...offset}}; + focusRect=d.focus.rect;focusCaret=d.focus.caret;focusKind=d.focus.kind;focusFont=d.focus.fontSize||14;focusViewportWidth=d.focus.viewport?.width||0;focusContext=d.focus.contextRect;focusedView=true;frameFocus(); + }else if(d.focus){ + // Non-text controls keep the surrounding Settings context. + if(focusedView&&beforeFocus){scale=beforeFocus.scale;offset=beforeFocus.offset;transform();} + focusedView=false;beforeFocus=null;focusRect=null;focusCaret=null; + }else if(!d.active){if(focusedView&&focusKind==='text-field'&&beforeFocus){scale=beforeFocus.scale;offset=beforeFocus.offset;transform();beforeFocus=null;focusedView=false;}else if(focusedView&&focusKind==='terminal')frameFocus();} + }); + new ResizeObserver(()=>{if(focusedView)frameFocus();else transform();}).observe(viewport); + video.addEventListener('resize',()=>{if(focusedView)frameFocus();}); + $('view-toggle')?.addEventListener('click',()=>{autoFramePending=false;if(focusedView||scale>1){window.CanvasLinkInput?.stop();focusedView=false;scale=1;offset={x:0,y:0};transform();}else if(knownTerminal&&remoteSurface!=='settings'){focusKind='terminal';focusRect=knownTerminal.rect;focusCaret=knownTerminal.caret;focusViewportWidth=knownTerminal.viewport?.width||0;focusedView=true;frameFocus();}}); + + const stopHold = () => { clearTimeout(holdTimer); holdTimer = 0; }; + function updatePair() { + pairFrame = 0; + if (touches.size !== 2 || !pinch || !canInput()) return; + const [a, b] = [...touches.values()]; + const x = (a.x + b.x) / 2, y = (a.y + b.y) / 2; + const distance = Math.hypot(a.x - b.x, a.y - b.y); + if (trackpad && !pinch.kind) { + if (Math.abs(distance - pinch.distance) > 8 && Math.abs(distance / pinch.distance - 1) > .06) pinch.kind = 'zoom'; + else if (Math.hypot(x - pinch.x, y - pinch.y) > 6) pinch.kind = 'scroll'; + else return; + } + if (trackpad && pinch.kind === 'scroll') { + focusedView=false; + const dx = clamp(pinch.lastX - x, -2000, 2000), dy = clamp(pinch.lastY - y, -2000, 2000); + if (dx || dy) send({type: 'scroll', ...pointer, dx, dy}); + } else { + zoomView(pinch.scale * distance / pinch.distance, {x: pinch.x, y: pinch.y}, {x, y}, pinch); + } + pinch.lastX = x; pinch.lastY = y; + } + function flushPair() { + if (pairFrame) { cancelAnimationFrame(pairFrame); updatePair(); } + } + function cancelGesture() { + stopHold(); + if (pairFrame) { cancelAnimationFrame(pairFrame); pairFrame = 0; } + if (drag && connected()) mouse('up', pointer); + drag = false; press = null; pinch = null; + const ids = [...touches.keys()]; + touches.clear(); // Clear before releasing capture: loss can arrive synchronously. + for (const id of ids) { + try { if (viewport.hasPointerCapture(id)) viewport.releasePointerCapture(id); } catch (_) {} + } + } + + $('fit').onclick = () => { + cancelGesture(); autoFramePending=false;focusedView=false;scale = scale === 1 ? 2 : 1; offset = {x: 0, y: 0}; transform(); + }; + $('mode').onclick = () => { cancelGesture(); trackpad = !trackpad; transform(); }; + // Optional hook: also works while the parent UI is being upgraded in place. + document.addEventListener('click', e => { + if (e.target instanceof Element && e.target.closest('#reset-view')) { + cancelGesture(); autoFramePending=false;focusedView=false;scale = 1; offset = {x: 0, y: 0}; transform(); + } + }); + if (!viewport.hasAttribute('tabindex')) viewport.tabIndex = 0; + viewport.addEventListener('pointerdown', e => { + if (!canInput()) { e.preventDefault(); return; } + if (e.button !== 0 || control(e.target) || e.target.closest?.('#waiting')) return; + e.preventDefault();++tapEpoch;viewport.focus({preventScroll: true}); + try { viewport.setPointerCapture(e.pointerId); } catch (_) { cancelGesture(); return; } + touches.set(e.pointerId, {x: e.clientX, y: e.clientY}); + if (touches.size === 1) { + const at=position(e.clientX,e.clientY); + const onSlider=dragRects.some(r=>validRect(r)&&at.x>=r.x&&at.x<=r.x+r.width&&at.y>=r.y&&at.y<=r.y+r.height); + press = {x: e.clientX, y: e.clientY, travel: 0,scroll:!trackpad&&remoteSurface==='settings'&&!onSlider,at}; + if (trackpad && e.pointerType !== 'mouse') { + const held = press; + holdTimer = setTimeout(() => { + holdTimer = 0; + if (press === held && touches.size === 1 && held.travel < 8 && canInput()) { + drag = true; mouse('down', pointer); + } + }, 450); + } + } else if (touches.size === 2) { + stopHold(); + if (drag) mouse('up', pointer); + drag = false; press = null; + const [a, b] = [...touches.values()]; + pinch = {distance: Math.max(1, Math.hypot(a.x - b.x, a.y - b.y)), scale, + x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, lastX: (a.x + b.x) / 2, + lastY: (a.y + b.y) / 2, offset: {...offset}, kind: null}; + } + }); + viewport.addEventListener('pointermove', e => { + const previous = touches.get(e.pointerId); + if (!previous) return; + touches.set(e.pointerId, {x: e.clientX, y: e.clientY}); + if (touches.size === 2 && pinch) { + // Classify the settled frame, not half of a two-pointer update. Scroll uses + // centroid deltas, so moving both fingers never doubles the remote scroll. + if (!pairFrame) pairFrame = requestAnimationFrame(updatePair); + return; + } + if (!press) return; + press.travel = Math.max(press.travel, Math.hypot(e.clientX - press.x, e.clientY - press.y)); + if (press.travel >= 8) stopHold(); + if (trackpad) { + const r = viewport.getBoundingClientRect(); + pointer = {x: clamp(pointer.x + (e.clientX - previous.x) / Math.max(1, r.width), 0, 1), + y: clamp(pointer.y + (e.clientY - previous.y) / Math.max(1, r.height), 0, 1)}; + mouse('move', pointer, drag ? {drag: true} : {}); + } else if(press.scroll&&press.travel>8){ + focusedView=false; + const k=(remoteViewportWidth||video.videoWidth)/Math.max(1,contentBox().width); + send({type:'scroll',...press.at,dx:clamp((previous.x-e.clientX)*k,-2000,2000),dy:clamp((previous.y-e.clientY)*k,-2000,2000)}); + } else if (press.travel > 8 || drag) { + if (!drag) { mouse('down', position(press.x, press.y)); drag = true; } + mouse('move', position(e.clientX, e.clientY), {drag: true}); + } + }); + function release(e) { + if (!touches.has(e.pointerId)) return; + stopHold(); + if (e.type === 'pointerup') flushPair(); + else if (pairFrame) { cancelAnimationFrame(pairFrame); pairFrame = 0; } + if (drag && e.type === 'pointercancel') mouse('up', pointer); + if (touches.size === 1 && press && e.type !== 'pointercancel') { + const at = trackpad ? pointer : position(e.clientX, e.clientY); + if (drag) mouse('up', at); + else if (press.travel < 8 && Math.hypot(e.clientX - press.x, e.clientY - press.y) < 8) { + const epoch=tapEpoch; + mouse('down', at);Promise.resolve(mouse('up', at)).then(receipt=>{ + if(epoch===tapEpoch&&connected()&&receipt?.status==='processed')window.dispatchEvent(new CustomEvent('canvaslink:tap',{detail:{focus:receipt.focus,at}})); + }); + } + } + touches.delete(e.pointerId); + if (touches.size < 2) pinch = null; + // No remaining finger may turn a pair gesture into a tap or resumed drag. + press = null; drag = false; + } + viewport.addEventListener('pointerup', release); + viewport.addEventListener('pointercancel', release); + viewport.addEventListener('lostpointercapture', e => { + // The video's implicit capture is transferred to the viewport. Its own loss + // bubbles here but must NOT cancel the viewport's newly captured gesture. + if (e.target === viewport) release({pointerId: e.pointerId, type: 'pointercancel'}); + }); + window.addEventListener('blur', cancelGesture); + document.addEventListener('visibilitychange', () => { if (document.hidden) cancelGesture(); }); + api.subscribe(state => { + if(state.connection!=='connected'){++tapEpoch;cancelGesture();focusedView=false;focusCaret=null;return;} + if(state.source?.viewport?.width>0)remoteViewportWidth=state.source.viewport.width; + if(state.source?.surface){remoteSurface=state.source.surface;dragRects=state.source.dragRects||[];} + const terminal=state.source?.target==='terminal'&&validRect(state.source.rect)?state.source:state.source?.terminalView; + if(terminal&&validRect(terminal.rect)){knownTerminal=terminal;if(autoFramePending&&remoteSurface!=='settings'){autoFramePending=false;focusKind='terminal';focusRect=knownTerminal.rect;focusCaret=knownTerminal.caret;focusViewportWidth=knownTerminal.viewport?.width||0;focusedView=true;frameFocus();}} + if(remoteSurface==='settings'&&focusedView&&focusKind==='terminal'){focusedView=false;scale=1;offset={x:0,y:0};transform();} + // Health may follow an already selected cursor, but can never open an IME. + // Manual pan/zoom/scroll disables following until another confirmed tap. + if(focusedView&&focusKind==='terminal'&&state.source?.target==='terminal'&&validRect(state.source.rect)&&validRect(state.source.caret)){focusRect=state.source.rect;focusCaret=state.source.caret;frameFocus();} + }); + document.addEventListener('focusin', e => { if (editor(e.target)) cancelGesture(); }); + window.addEventListener('compositionstart', () => { composing = true; cancelGesture(); }); + window.addEventListener('compositionend', () => { composing = false; }); + + viewport.addEventListener('wheel', e => { + if (!canInput() || control(e.target) || e.target.closest?.('#waiting')) return; + e.preventDefault(); + const dx = e.deltaX * (e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? viewport.clientWidth : 1); + const dy = e.deltaY * (e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? viewport.clientHeight : 1); + // Chromium exposes physical trackpad pinch as Ctrl+wheel, not touch pointers. + if (e.ctrlKey) { cancelGesture(); zoomView(scale * Math.exp(-dy * .002), {x: e.clientX, y: e.clientY}); return; } + focusedView=false;send({type: 'scroll', ...(trackpad ? pointer : position(e.clientX, e.clientY)), + dx: clamp(dx, -2000, 2000), dy: clamp(dy, -2000, 2000)}); + }, {passive: false}); + const terminalKeys = new Set(['Enter', 'Tab', 'Escape', 'Backspace', 'Delete', + 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Home', 'End', 'PageUp', 'PageDown']); + window.addEventListener('keydown', e => { + if (document.querySelector('dialog[open]') || !canInput() || e.defaultPrevented || e.isComposing || e.keyCode === 229 || editor(e.target)) return; + if (!(e.target instanceof Element) || control(e.target)) return; + if (e.target !== $('workspace') && !viewport.contains(e.target)) return; + if (e.key === 'Enter' && e.repeat) { e.preventDefault(); return; } + const modifiers = [e.ctrlKey ? 'control' : null, e.shiftKey ? 'shift' : null, + e.altKey ? 'alt' : null, e.metaKey ? 'meta' : null].filter(Boolean); + let sendKey; + if (terminalKeys.has(e.key)) sendKey = () => api.sendKey(e.key, modifiers); + else if ((e.ctrlKey || e.metaKey) && 'cavzdl'.includes(e.key.toLowerCase())) sendKey = () => api.sendKey(e.key.toLowerCase(), modifiers); + else if ([...e.key].length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) sendKey = () => api.sendText(e.key); + if (sendKey) { e.preventDefault(); deliver(sendKey); } + }); +})(); diff --git a/integrations/canvaslink/android/assets/index.html b/integrations/canvaslink/android/assets/index.html new file mode 100644 index 000000000..593a54d16 --- /dev/null +++ b/integrations/canvaslink/android/assets/index.html @@ -0,0 +1,99 @@ + + + + + + + CanvasLink + + + + + + + +
+
CanvasLink
+
+

Подключение к CanvasTTY

+

Телефон и компьютер должны быть в одной локальной сети.

+
+
Компьютер
+
+

Ищем компьютер…

+ + +

CanvasTTY → Настройки → CanvasLink

+ + +
Компьютер не находится?

Проверь локальную сеть или введи адрес компьютера.

+
+
+
+
+

Рабочая область CanvasTTY

+
+ + + + +

Открываем CanvasTTY

Ожидаем изображение.

+ + +
+ +
+ +
+
Подключаем…
+

Касание — клик. Два пальца — масштаб и обзор.

+ +
+ +

Диагностика

+

RTT — задержка канала управления.

+
+ +

Завершить сеанс?

Терминалы останутся открыты на компьютере.

+
+
+ + + diff --git a/integrations/canvaslink/android/assets/lucide-LICENSE.txt b/integrations/canvaslink/android/assets/lucide-LICENSE.txt new file mode 100644 index 000000000..718bb3f0e --- /dev/null +++ b/integrations/canvaslink/android/assets/lucide-LICENSE.txt @@ -0,0 +1,43 @@ +ISC License + +Copyright (c) 2026 Lucide Icons and Contributors + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +--- + +The following Lucide icons are derived from the Feather project: + +airplay, alert-circle, alert-octagon, alert-triangle, aperture, arrow-down-circle, arrow-down-left, arrow-down-right, arrow-down, arrow-left-circle, arrow-left, arrow-right-circle, arrow-right, arrow-up-circle, arrow-up-left, arrow-up-right, arrow-up, at-sign, calendar, cast, check, chevron-down, chevron-left, chevron-right, chevron-up, chevrons-down, chevrons-left, chevrons-right, chevrons-up, circle, clipboard, clock, code, columns, command, compass, corner-down-left, corner-down-right, corner-left-down, corner-left-up, corner-right-down, corner-right-up, corner-up-left, corner-up-right, crosshair, database, divide-circle, divide-square, dollar-sign, download, external-link, feather, frown, hash, headphones, help-circle, info, italic, key, layout, life-buoy, link-2, link, loader, lock, log-in, log-out, maximize, meh, minimize, minimize-2, minus-circle, minus-square, minus, monitor, moon, more-horizontal, more-vertical, move, music, navigation-2, navigation, octagon, pause-circle, percent, plus-circle, plus-square, plus, power, radio, rss, search, server, share, shopping-bag, sidebar, smartphone, smile, square, table-2, tablet, target, terminal, trash-2, trash, triangle, tv, type, upload, x-circle, x-octagon, x-square, x, zoom-in, zoom-out + +The MIT License (MIT) (for the icons listed above) + +Copyright (c) 2013-present Cole Bemis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/integrations/canvaslink/android/assets/onest-OFL.txt b/integrations/canvaslink/android/assets/onest-OFL.txt new file mode 100644 index 000000000..f480c31d2 --- /dev/null +++ b/integrations/canvaslink/android/assets/onest-OFL.txt @@ -0,0 +1,93 @@ +Copyright 2021 The Onest Project Authors (https://github.com/googlefonts/onest) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/integrations/canvaslink/android/assets/onest.ttf b/integrations/canvaslink/android/assets/onest.ttf new file mode 100644 index 000000000..477cb3102 Binary files /dev/null and b/integrations/canvaslink/android/assets/onest.ttf differ diff --git a/integrations/canvaslink/android/assets/style.css b/integrations/canvaslink/android/assets/style.css new file mode 100644 index 000000000..97ac9bb28 --- /dev/null +++ b/integrations/canvaslink/android/assets/style.css @@ -0,0 +1,124 @@ +/* CanvasTTY Sage tokens: src/renderer/src/styles/tokens.css. */ +:root { + color-scheme:dark; + font-family:"Trebuchet MS","Segoe UI",sans-serif; + font-synthesis:none; + --canvas:#aaa7a2; + --surface:#353442; + --surface-soft:#424251; + --text:#f8f7f1; + --text-dark:#30313d; + --muted:rgba(255,255,255,.7); + --primary:#b8cf99; + --primary-hover:#c6dca8; + --danger:#e3615e; + --line:rgba(248,247,241,.13); + --app-height:100dvh; +} +* {box-sizing:border-box;-webkit-tap-highlight-color:transparent} +[hidden] {display:none!important} +html,body {margin:0;min-height:100%;background:var(--surface);color:var(--text)} +body {-webkit-user-select:none;user-select:none;-webkit-touch-callout:none;font-size:14px;line-height:1.5} +body.is-workspace {height:var(--app-height);overflow:hidden;overscroll-behavior:none} +button,input,summary {font:inherit} +button,summary,button * {-webkit-user-select:none;user-select:none;-webkit-touch-callout:none} +button {display:inline-flex;align-items:center;justify-content:center;gap:10px;min-height:48px;padding:10px 16px;border:0;border-radius:9px;color:var(--text);background:var(--surface-soft);font-weight:600;cursor:pointer;touch-action:manipulation} +button:active:not(:disabled) {background:#525262} +button:disabled {opacity:.4;cursor:default} +button:focus-visible,summary:focus-visible,input:focus-visible {outline:2px solid var(--primary);outline-offset:3px} +button[aria-pressed=true],.primary {background:var(--primary);color:var(--text-dark)} +button[aria-pressed=true]:active,.primary:active:not(:disabled) {background:var(--primary-hover)} +.danger {color:#ffb5b1} +.icon {width:20px;height:20px;flex:none;fill:none;stroke:currentColor;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;pointer-events:none} +.icon-definitions {position:absolute;width:0;height:0;overflow:hidden;pointer-events:none} +.icon-button {width:48px;min-width:48px;padding:0} +.sr-only {position:absolute!important;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap;border:0} +.subtle,.hint {color:var(--muted)} +.subtle {margin:12px 0 24px} +.hint {font-size:12px;line-height:1.5;margin:10px 0 20px} +.pairing-screen {min-height:var(--app-height);padding:env(safe-area-inset-top) max(20px,env(safe-area-inset-right)) max(24px,env(safe-area-inset-bottom)) max(20px,env(safe-area-inset-left));background:var(--canvas)} +.pair-header {max-width:440px;margin:auto;min-height:88px;display:flex;align-items:center;color:var(--text-dark)} +.brand {font-size:20px;font-weight:700} +.pair-card {width:100%;max-width:440px;margin:8px auto 0;padding:28px;background:var(--surface);border-radius:20px} +.pair-card h1 {font-size:24px;font-weight:700;line-height:1.25;margin:0} +.field-heading {display:flex;align-items:center;justify-content:space-between;gap:12px;margin:0 0 8px;font-weight:600} +.field-heading .icon-button {background:transparent;color:var(--muted)} +.computers {display:grid;gap:8px} +.computer {width:100%;justify-content:flex-start;text-align:left;min-height:56px;padding:12px;gap:12px;border:2px solid transparent;font-weight:400} +.computer>span {flex:1;min-width:0;overflow-wrap:anywhere} +.computer[aria-pressed=true] {background:var(--surface-soft);border-color:var(--primary);color:var(--text)} +.computer-check {margin-left:auto;color:var(--primary);visibility:hidden} +.computer[aria-pressed=true] .computer-check {visibility:visible} +.field-label {display:block;margin:20px 0 10px;font-weight:600} +input {width:100%;min-height:52px;border:1px solid var(--line);border-radius:9px;background:#292935;padding:12px 14px;color:var(--text);font-size:16px;-webkit-user-select:text;user-select:text;touch-action:auto} +input::placeholder {color:rgba(248,247,241,.4);opacity:1} +#pin {font-family:ui-monospace,monospace;font-size:28px;letter-spacing:.3em;text-align:center;padding-left:calc(14px + .3em)} +#connect {width:100%;justify-content:space-between} +.error-message {margin:16px 0 0;color:#ffb5b1;font-size:13px;overflow-wrap:anywhere} +.error-message:empty {display:none} +details {margin-top:20px;border-top:1px solid var(--line)} +summary {display:flex;align-items:center;justify-content:space-between;gap:12px;min-height:52px;list-style:none;cursor:pointer;font-size:13px;color:var(--muted);touch-action:manipulation} +summary::-webkit-details-marker {display:none} +summary .icon {width:16px;height:16px} +details[open] summary .icon {transform:rotate(180deg)} +details .field-label {margin-top:0} +.workspace {height:var(--app-height);width:100%;display:flex;flex-direction:column;overflow:hidden;background:var(--surface)} +.remote-viewport {flex:1;min-height:0;position:relative;overflow:hidden;touch-action:none;background:#20212b} +#video {position:absolute;inset:0;width:100%;height:100%;object-fit:contain;transform-origin:center;pointer-events:none} +.keybar {display:flex;align-items:center;gap:6px;flex:0 0 auto;min-width:0;min-height:56px;padding:3px max(6px,env(safe-area-inset-right)) max(3px,env(safe-area-inset-bottom)) max(6px,env(safe-area-inset-left));background:var(--surface)} +.keybar button {flex:0 0 auto;height:44px;min-height:44px;min-width:44px;padding:0 12px;font:600 13px/1 "Trebuchet MS","Segoe UI",sans-serif;white-space:nowrap} +.keybar .icon-button {width:44px;padding:0} +.keybar>.icon-button {background:transparent;color:var(--muted)} +.keybar button[aria-pressed=true] {background:var(--primary);color:var(--text-dark)} +.keys {display:flex;flex:1;min-width:0;gap:6px;align-items:center;overflow-x:auto;overflow-y:hidden;overscroll-behavior-x:contain;scrollbar-width:none;padding:3px 2px} +.keys::-webkit-scrollbar {display:none} +.keybar .enter-key {color:var(--primary);min-width:48px} +.direction-left {transform:rotate(180deg)} +.direction-up {transform:rotate(-90deg)} +.direction-down {transform:rotate(90deg)} +.local-cursor {position:absolute;left:0;top:0;width:26px;height:26px;color:var(--text);filter:drop-shadow(0 1px 2px #000);pointer-events:none} +.local-cursor .icon {width:26px;height:26px} +.touch-feedback {position:absolute;left:0;top:0;width:32px;height:32px;border-radius:50%;border:2px solid var(--primary);pointer-events:none} +.preedit {position:absolute;left:16px;bottom:12px;max-width:calc(100% - 32px);padding:6px 12px;background:var(--surface);border-radius:9px;text-decoration:underline;font:16px/1.4 ui-monospace,monospace;white-space:pre-wrap;overflow:hidden} +.waiting {position:absolute;inset:0;display:flex;flex-direction:column;justify-content:center;align-items:center;background:#20212b;padding:24px;text-align:center;z-index:4} +.waiting>.icon {width:28px;height:28px;color:var(--muted)} +.waiting h2 {font-size:20px;line-height:1.3;font-weight:600;margin:20px 0 8px} +.waiting p {font-size:14px;color:var(--muted);max-width:340px;margin:0 0 20px} +.toast {position:absolute;bottom:12px;left:50%;transform:translateX(-50%);max-width:min(560px,calc(100% - 32px));width:max-content;max-height:60%;overflow:auto;z-index:6;background:var(--surface);border:1px solid var(--line);border-radius:9px;padding:12px 16px;font-size:13px;margin:0;overflow-wrap:anywhere} +.toast.error {color:#ffb5b1;border-color:rgba(227,97,94,.5)} +.sheet {color:var(--text);background:var(--surface);border:0;border-radius:20px;padding:20px;width:min(440px,calc(100% - 32px));max-height:calc(var(--app-height) - 32px);overflow:auto;box-shadow:0 16px 48px #0005} +.sheet::backdrop {background:rgba(24,24,32,.6)} +.sheet-heading {display:flex;align-items:center;justify-content:space-between;gap:16px;margin:-4px -4px 0 0} +.sheet-heading .icon-button {background:transparent;color:var(--muted)} +.sheet h2 {font-size:20px;line-height:1.3;font-weight:700;margin:0} +.connection-state {font-size:12px;color:var(--muted);margin:4px 0 12px} +.menu-actions {display:grid;gap:8px;margin-top:20px} +.menu-actions>button {justify-content:space-between;min-height:52px;text-align:left;font-weight:400} +.menu-actions>button>span:last-child {color:var(--muted)} +.menu-actions>button[aria-pressed=true]>span:last-child {color:var(--text-dark)} +.sheet pre {font:13px/2 ui-monospace,monospace;margin:20px 0;white-space:pre-wrap;-webkit-user-select:text;user-select:text} +.sheet-actions {display:flex;gap:8px;margin-top:24px;flex-wrap:wrap} +.sheet-actions button {flex:1;min-width:120px} +@media(hover:hover) { + button:hover:not(:disabled) {background:#525262} + button.primary:hover:not(:disabled),button[aria-pressed=true]:hover {background:var(--primary-hover)} + .computer[aria-pressed=true]:hover {background:#525262} +} +@media(max-width:380px) { + .pairing-screen {padding-left:16px;padding-right:16px} + .pair-card {padding:20px} + .pair-card h1 {font-size:22px} + .keybar {gap:4px} +} +@media(max-height:500px) { + .pair-header {min-height:56px} + .pair-card {margin-top:0} + .sheet {padding:16px;max-height:calc(var(--app-height) - 16px)} +} +@media(prefers-reduced-motion:no-preference) { + button {transition:background-color .12s ease,color .12s ease,opacity .12s ease} + dialog[open] {animation:sheet-in .14s ease-out} + .touch-feedback {animation:touch .28s ease-out} + @keyframes sheet-in {from{opacity:0;translate:0 6px}to{opacity:1;translate:0 0}} + @keyframes touch {from{opacity:1;scale:.7}to{opacity:0;scale:1.3}} +} diff --git a/integrations/canvaslink/android/assets/transport.js b/integrations/canvaslink/android/assets/transport.js new file mode 100644 index 000000000..110fa6c8c --- /dev/null +++ b/integrations/canvaslink/android/assets/transport.js @@ -0,0 +1,117 @@ +/* Public designer API v2. This file owns secure transport and delivery state. */ +(()=>{ + let pc,dc,video,paired=false,acceptPairing=true,acceptOffers=false,move,moveFrame,peerEpoch=0,stats={},lastFrames=0,lastFrameAt=0,draftTouched=false,connectionTimer,retryTimer; + const listeners=new Set(),computers=new Map(); + let state={version:2,connection:'idle',discovery:'searching',computers:[],computer:'',error:'',pending:0,delivery:null,stats:{},source:null,draft:''}; + const snapshot=()=>JSON.parse(JSON.stringify(state)); + const update=patch=>{Object.assign(state,patch);for(const listener of listeners)listener(snapshot());}; + const signal=message=>Native.signal(JSON.stringify(message)); + const delivery=new CanvasLinkDelivery.Delivery({channel:()=>dc,onChange:value=>{if(['pointer','scroll'].includes(value.kind)){state.pending=value.pending;return;}update({delivery:value,pending:value.pending});}}); + const raw=value=>{if(dc?.readyState==='open'&&dc.bufferedAmount<65536)dc.send(JSON.stringify(value));}; + function stopWaiting(){clearTimeout(connectionTimer);connectionTimer=null;} + function waitForConnection(){ + if(connectionTimer||!paired||document.hidden)return; + // Offers and polling retries share one deadline, so they cannot prolong it forever. + connectionTimer=setTimeout(()=>{connectionTimer=null;if(!paired||document.hidden)return;acceptOffers=false;close();update({connection:'reconnecting',error:'lan-connection-timeout'});},30000); + } + function connected(){stopWaiting();clearTimeout(retryTimer);retryTimer=null;update({connection:'connected',error:''});} + function close(){clearTimeout(retryTimer);retryTimer=null;peerEpoch++;if(moveFrame)cancelAnimationFrame(moveFrame);moveFrame=null;move=null;delivery.close();dc=null;const previous=pc;pc=null;previous?.close();stats={};lastFrames=0;lastFrameAt=0;window.CanvasLink.stats={};update({stats:{},source:null});if(video)video.srcObject=null;} + function retry(current){ + if(current!==pc||!paired||document.hidden)return; + delivery.close();update({connection:'reconnecting'});waitForConnection(); + clearTimeout(retryTimer);retryTimer=setTimeout(()=>{retryTimer=null;if(paired&&!document.hidden&¤t===pc&&(current.connectionState!=='connected'||dc?.readyState!=='open'))signal({type:'ping'});},2500); + } + function resume(){if(!paired||document.hidden)return;stopWaiting();close();acceptOffers=true;update({connection:'reconnecting',error:''});waitForConnection();signal({type:'ping'});} + async function offer(message){ + close();waitForConnection();update({connection:'connecting',error:''}); + const epoch=peerEpoch; + try{ + const current=pc=new RTCPeerConnection({iceServers:[]}); + current.ontrack=event=>{if(current!==pc)return;if(video){video.srcObject=event.streams[0];video.play().catch(()=>{});}}; + current.ondatachannel=event=>{ + if(current!==pc)return;dc=event.channel; + dc.onopen=()=>{if(current===pc)connected();}; + dc.onclose=()=>retry(current); + dc.onmessage=event=>{ + if(current!==pc||typeof event.data!=='string'||event.data.length>40000)return; + let value;try{value=JSON.parse(event.data);}catch{return;} + if(value.type==='source-state'){update({source:{capture:value.capture,windowFocused:!!value.windowFocused,target:value.target,surface:value.surface||'canvas',viewport:value.viewport||null,terminalView:value.terminalView||null,dragRects:value.dragRects||[],rect:value.rect||null,caret:value.caret||null}});return;} + if(value.type==='pong')stats.rtt=Math.round(performance.now()-value.at); + if(delivery.receive(value))return; + if(value.type==='fixture-state'){window.CanvasLink.fixture=value;Native.acceptance(JSON.stringify(value));} + }; + }; + current.onconnectionstatechange=()=>{ + if(current!==pc)return; + if(current.connectionState==='connected'&&dc?.readyState==='open')connected(); + if(['failed','disconnected'].includes(current.connectionState))retry(current); + }; + await current.setRemoteDescription(message);if(current!==pc)return; + const answer=await current.createAnswer();if(current!==pc)return; + await current.setLocalDescription(answer);if(current!==pc)return; + await new Promise((resolve,reject)=>{if(current.iceGatheringState==='complete')return resolve();const timer=setTimeout(()=>reject(Error('ice-timeout')),12000);current.onicegatheringstatechange=()=>{if(current.iceGatheringState==='complete'){clearTimeout(timer);resolve();}};}); + if(current===pc)signal({type:'answer',sdp:current.localDescription.sdp}); + }catch(error){if(epoch===peerEpoch){acceptOffers=false;stopWaiting();close();update({connection:paired?'reconnecting':'error',error:error.message});}} + } + function connect({code,computerId,host}={}){ + const address=host||computers.get(computerId)?.host||(computers.size===1?[...computers.values()][0].host:''); + if(!address){update({error:computers.size?'choose-computer':'computer-not-found'});return false;} + if(!/^\d{6}$/.test(code||'')){update({error:'invalid-code'});return false;} + stopWaiting();close();paired=false;acceptOffers=false;acceptPairing=true;update({connection:'pairing',computer:address,error:''});Native.connect(address,code);return true; + } + function sendInput(input){ + if(input.type==='pointer'&&input.action==='move'){ + // Coalesce motion only; never replay or coalesce text, clicks, or keys. + move=input;if(!moveFrame)moveFrame=requestAnimationFrame(()=>{moveFrame=null;const m=move;move=null;delivery.send(m).catch(()=>{});});return Promise.resolve({status:'coalesced'}); + } + if(move){cancelAnimationFrame(moveFrame);moveFrame=null;const m=move;move=null;delivery.send(m).catch(()=>{});} + return delivery.send(input); + } + const api=window.CanvasLinkClient=Object.freeze({ + version:2,getState:snapshot,subscribe(listener){listeners.add(listener);listener(snapshot());return()=>listeners.delete(listener);}, + attachVideo(element){video=element;},connect,discover(){update({discovery:'searching'});Native.discover();}, + disconnect(){acceptPairing=false;acceptOffers=false;paired=false;stopWaiting();close();Native.disconnect();update({connection:'idle',error:''});}, + sendInput,sendText(text){if(typeof text!=='string'||!text.length||text.length>8000)return Promise.reject(Error('invalid-text'));return sendInput({type:'text',text});}, + sendKey(key,modifiers=[]){return sendInput({type:'key',key,modifiers});}, + setDraft(text){draftTouched=true;state.draft=String(text).slice(0,8000);return Native.saveDraft(state.draft);}, + hideKeyboard(){Native.hideKeyboard();},resume + }); + window.CanvasLink={stats:{},fixture:null,connectForTest:(host,code)=>connect({host,code}),requestFixture:requestId=>raw({type:'stats',...(typeof requestId==='string'&&requestId.length<=80?{requestId}:{})}),receive(event){ + const value=event.value; + if(event.type==='discovered'){computers.set(value.id,{...value});update({discovery:'ready',computers:[...computers.values()]});} + if(event.type==='lost'){computers.delete(value);update({computers:[...computers.values()]});} + if(event.type==='discovery')update({discovery:value}); + if(event.type==='draft'&&!draftTouched)update({draft:value||''}); + if(event.type==='paired'){if(!acceptPairing)return;stopWaiting();close();paired=true;acceptOffers=true;waitForConnection();update({connection:'connecting',computer:value.host,error:''});} + if(event.type==='signal'){ + if(!paired||document.hidden)return; + if(value.type==='offer'&&acceptOffers)void offer(value); + if(value.type==='disconnect'){acceptOffers=false;stopWaiting();close();update({connection:'reconnecting',error:value.reason==='host-connection-failed'?'host-connection-failed':state.error});} + } + if(event.type==='error')update({connection:paired?'reconnecting':'error',error:String(value)}); + if(event.type==='network')update({connection:paired?'reconnecting':'idle',error:String(value)}); + if(event.type==='network-changed'){ + acceptOffers=false;stopWaiting();close(); + if(paired){update({connection:'reconnecting',error:value==='offline'?'lan-network-unavailable':''});if(value==='ready')resume();} + } + // A reconnect ping sent while Wi-Fi is down cannot reach the host. Retry + // only signalling once encrypted polling succeeds; input is never replayed. + if(event.type==='network-restored'&&paired&&!document.hidden&&state.connection!=='connected'){acceptOffers=true;waitForConnection();signal({type:'ping'});} + if(event.type==='disconnected'){paired=false;acceptOffers=false;stopWaiting();close();update({connection:'idle'});} + }}; + document.addEventListener('visibilitychange',()=>{if(!paired)return;if(document.hidden){acceptOffers=false;stopWaiting();signal({type:'disconnect'});close();update({connection:'suspended'});}else resume();}); + setInterval(async()=>{ + const current=pc;if(!current)return; + try{ + raw({type:'ping',at:performance.now(),videoFrames:stats.framesDecoded||0});const reports=await current.getStats();if(current!==pc)return;let inbound,selected; + reports.forEach(r=>{if(r.type==='inbound-rtp'&&r.kind==='video')inbound=r;if(r.type==='candidate-pair'&&r.state==='succeeded'&&r.nominated)selected=r;}); + if(inbound){ + const now=performance.now(),frames=inbound.framesDecoded||0;if(frames>lastFrames){lastFrameAt=now;lastFrames=frames;} + stats={...stats,framesDecoded:frames,framesPerSecond:inbound.framesPerSecond||0,width:inbound.frameWidth||0,height:inbound.frameHeight||0,inputs:delivery.sent,ackedInputs:delivery.acked,state:current.connectionState,bytes:inbound.bytesReceived||0,stalled:state.source?state.source.capture==='stalled':!!lastFrameAt&&now-lastFrameAt>15000}; + if(selected){const local=reports.get(selected.localCandidateId),remote=reports.get(selected.remoteCandidateId);stats.transport=local?.protocol||'';stats.local=local?.address||'';stats.remote=remote?.address||'';stats.candidateType=remote?.candidateType||'';} + window.CanvasLink.stats=stats;Native.diagnostics(JSON.stringify(stats));update({stats:{...stats}}); + } + }catch{} + },1000); + Native.discover();Native.restore();Native.loadDraft(); +})(); diff --git a/integrations/canvaslink/android/res/drawable/ic_launcher_foreground.xml b/integrations/canvaslink/android/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 000000000..6ab56d856 --- /dev/null +++ b/integrations/canvaslink/android/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,10 @@ + + + + + + diff --git a/integrations/canvaslink/android/res/drawable/ic_launcher_monochrome.xml b/integrations/canvaslink/android/res/drawable/ic_launcher_monochrome.xml new file mode 100644 index 000000000..a483831b3 --- /dev/null +++ b/integrations/canvaslink/android/res/drawable/ic_launcher_monochrome.xml @@ -0,0 +1,10 @@ + + + + + + diff --git a/integrations/canvaslink/android/res/mipmap-anydpi-v26/ic_launcher.xml b/integrations/canvaslink/android/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 000000000..51deb97dc --- /dev/null +++ b/integrations/canvaslink/android/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1 @@ + diff --git a/integrations/canvaslink/android/res/mipmap-anydpi-v33/ic_launcher.xml b/integrations/canvaslink/android/res/mipmap-anydpi-v33/ic_launcher.xml new file mode 100644 index 000000000..3d1008cb4 --- /dev/null +++ b/integrations/canvaslink/android/res/mipmap-anydpi-v33/ic_launcher.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/integrations/canvaslink/android/res/values/colors.xml b/integrations/canvaslink/android/res/values/colors.xml new file mode 100644 index 000000000..e50c4c575 --- /dev/null +++ b/integrations/canvaslink/android/res/values/colors.xml @@ -0,0 +1 @@ +#121814 diff --git a/integrations/canvaslink/android/src/io/nexcore/canvaslink/AcceptanceInstrumentation.java b/integrations/canvaslink/android/src/io/nexcore/canvaslink/AcceptanceInstrumentation.java new file mode 100644 index 000000000..1aa894c58 --- /dev/null +++ b/integrations/canvaslink/android/src/io/nexcore/canvaslink/AcceptanceInstrumentation.java @@ -0,0 +1,185 @@ +package io.nexcore.canvaslink; + +import android.app.Instrumentation; +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.os.SystemClock; +import android.view.MotionEvent; +import org.json.*; +import java.util.concurrent.*; + +/** Local adb-only acceptance. Never logs pairing material or captured content. */ +public final class AcceptanceInstrumentation extends Instrumentation { + private Bundle arguments; private MainActivity activity; + void stage(String name){Bundle b=new Bundle();b.putString("stage",name);sendStatus(0,b);} + @Override public void onCreate(Bundle args){arguments=args;start();} + String js(String code) throws Exception { + CountDownLatch latch=new CountDownLatch(1);String[] out={"null"}; + runOnMainSync(()->activity.web.evaluateJavascript(code,value->{out[0]=value;latch.countDown();})); + if(!latch.await(5,TimeUnit.SECONDS))throw new Exception("WebView evaluation timeout");return out[0]; + } + void tap(String selector) throws Exception { + tap(selector,null); + } + void tap(String selector, String point) throws Exception { + boolean[] focused={false};runOnMainSync(()->focused[0]=activity.web.hasWindowFocus()); + if(!focused[0])throw new Exception("Touch Activity is not foreground"); + String coordinate="{x:r.x+r.width/2,y:r.y+r.height/2}"; + if(point!=null){String[] xy=point.split(",");double x=Double.parseDouble(xy[0]),y=Double.parseDouble(xy[1]);if(x<0||x>1||y<0||y>1)throw new Exception("Invalid normalized point");coordinate="(()=>{const f=Math.min(r.width/e.videoWidth,r.height/e.videoHeight),w=e.videoWidth*f,h=e.videoHeight*f;return {x:r.x+(r.width-w)/2+w*"+x+",y:r.y+(r.height-h)/2+h*"+y+"}})()";} + String rect=js("(()=>{const e=document.querySelector("+JSONObject.quote(selector)+");e.scrollIntoView({block:'center',inline:'center'});const r=e.getBoundingClientRect(),p="+coordinate+";return {...p,dpr:devicePixelRatio,w:innerWidth,h:innerHeight}})()"); + JSONObject p=new JSONObject(rect);int[] offset=new int[2];runOnMainSync(()->activity.web.getLocationOnScreen(offset)); + if(p.getDouble("x")<0||p.getDouble("x")>=p.getDouble("w")||p.getDouble("y")<0||p.getDouble("y")>=p.getDouble("h"))throw new Exception("Touch target outside viewport: "+selector); + float x=(float)(p.getDouble("x")*p.getDouble("dpr"))+offset[0]; + float y=(float)(p.getDouble("y")*p.getDouble("dpr"))+offset[1]; + // CSS px and WebView device px differ by density, including viewport scaling. + long at=SystemClock.uptimeMillis();MotionEvent down=MotionEvent.obtain(at,at,MotionEvent.ACTION_DOWN,x,y,0); + MotionEvent up=MotionEvent.obtain(at,at+60,MotionEvent.ACTION_UP,x,y,0); + down.setSource(android.view.InputDevice.SOURCE_TOUCHSCREEN);up.setSource(android.view.InputDevice.SOURCE_TOUCHSCREEN); + if(!getUiAutomation().injectInputEvent(down,true)||!getUiAutomation().injectInputEvent(up,true))throw new Exception("Android rejected touch: "+selector); + down.recycle();up.recycle(); + SystemClock.sleep(120); + } + void commitText(String text) throws Exception { + boolean[] committed={false};runOnMainSync(()->{ + android.view.inputmethod.InputConnection input=activity.web.onCreateInputConnection(new android.view.inputmethod.EditorInfo()); + if(input!=null){input.performContextMenuAction(android.R.id.selectAll);committed[0]=input.commitText(text,1);} + }); + SystemClock.sleep(200); + if(!committed[0])throw new Exception("Android IME commit was rejected"); + stage("android-ime-commit"); + } + void menu(String id)throws Exception{tap("#menu-toggle");tap("#"+id);SystemClock.sleep(400);} + int decodedFrames()throws Exception{return new JSONObject(js("window.CanvasLink.stats")).optInt("framesDecoded");} + void waitVideoAfterTouch(int before)throws Exception{ + for(int i=0;i<120;i++){if(decodedFrames()>before)return;SystemClock.sleep(100);} + throw new Exception("Decoded video did not advance after real remote touch"); + } + String freshNativePoint()throws Exception{ + java.io.File directory=activity.getExternalFilesDir(null);if(directory==null)throw new Exception("Owned external acceptance directory unavailable"); + java.io.File target=new java.io.File(directory,"acceptance-target.json");stage("native-target-request"); + for(int i=0;i<80&&!target.exists();i++)SystemClock.sleep(100); + if(!target.exists())throw new Exception("Fresh owned terminal target unavailable"); + JSONObject p=new JSONObject(new String(java.nio.file.Files.readAllBytes(target.toPath()),java.nio.charset.StandardCharsets.UTF_8));target.delete(); + return p.getDouble("x")+","+p.getDouble("y"); + } + void waitTerminalKeyboard()throws Exception{ + for(int i=0;i<60;i++){ + boolean[] visible={false};runOnMainSync(()->{TerminalWebView view=(TerminalWebView)activity.web;visible[0]=view.terminalActive()&&(android.os.Build.VERSION.SDK_INT<30||view.getRootWindowInsets()!=null&&view.getRootWindowInsets().isVisible(android.view.WindowInsets.Type.ime()));}); + if(visible[0]&&"true".equals(js("CanvasLinkInput.active()"))){SystemClock.sleep(400);return;} + SystemClock.sleep(100); + } + boolean[] nativeState={false,false};runOnMainSync(()->{nativeState[0]=((TerminalWebView)activity.web).terminalActive();nativeState[1]=activity.web.getRootWindowInsets()!=null&&activity.web.getRootWindowInsets().isVisible(android.view.WindowInsets.Type.ime());}); + throw new Exception("Terminal keyboard inactive: native="+nativeState[0]+",visible="+nativeState[1]+",client="+js("JSON.stringify({active:CanvasLinkInput.active(),delivery:CanvasLinkClient.getState().delivery,notice:document.getElementById('stream-error').textContent,source:CanvasLinkClient.getState().source})")); + } + @Override public void onStart(){ + Bundle result=new Bundle(); + try{ + stage("launch"); + Intent intent=new Intent(getTargetContext(),MainActivity.class);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + getTargetContext().startActivity(intent); + for(int i=0;i<100&&MainActivity.active==null;i++)SystemClock.sleep(100); + activity=MainActivity.active;if(activity==null)throw new Exception("Activity launch timeout");stage("activity-ready"); + for(int i=0;i<80&&!"true".equals(js("!!window.CanvasLink"));i++)SystemClock.sleep(100); + String host=arguments.getString("host"),pin=arguments.getString("pin"); + boolean restoring="restore".equals(arguments.getString("mode")); + boolean discoveryFlow="true".equals(arguments.getString("discovery")); + if(!restoring){ + if(pin==null)throw new Exception("pin argument required"); + if(discoveryFlow){ + String selected=null,port=arguments.getString("port"); + for(int i=0;i<150&&selected==null;i++){ + JSONArray computers=new JSONObject(js("window.CanvasLinkClient.getState()")).getJSONArray("computers"); + for(int j=0;jc.id==="+JSONObject.quote(chosen)+")")); + tap("#computers button:nth-child("+(index+1)+")");SystemClock.sleep(400); + tap("#pin");SystemClock.sleep(200);commitText(pin); + if(!"true".equals(js("document.getElementById('pin').value.length===6")))throw new Exception("PIN IME input did not reach form; focused="+js("document.activeElement.id")+", length="+js("document.getElementById('pin').value.length")); + runOnMainSync(()->((android.view.inputmethod.InputMethodManager)activity.getSystemService(android.content.Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(activity.web.getWindowToken(),0));SystemClock.sleep(400); + tap("#connect");stage("discovery-and-code-form");result.putBoolean("discoveryForm",true); + }else{ + if(host==null)throw new Exception("host required without discovery"); + js("window.CanvasLink.connectForTest("+JSONObject.quote(host)+","+JSONObject.quote(pin)+")"); + } + } + stage("connecting"); + // A static owned window legitimately sends one initial image. Confirm + // subsequent live updates after an actual remote action, below. + int minimumFrames=("native".equals(arguments.getString("mode"))||"settings".equals(arguments.getString("mode")))?1:9; + JSONObject stats=null; + for(int i=0;i<200;i++){ + SystemClock.sleep(200); + try{stats=new JSONObject(js("window.CanvasLink.stats"));}catch(Exception ignored){} + if(stats!=null&&stats.optInt("framesDecoded")>=minimumFrames&&"connected".equals(stats.optString("state")))break; + } + if(stats==null||stats.optInt("framesDecoded")=4){complete=true;break;} + } + if(!complete)throw new Exception("Host did not finish all native input events"); + stage("native-text-and-enter-sent"); + if("true".equals(arguments.getString("snapshot")))SystemClock.sleep(4000); + if("true".equals(arguments.getString("controls"))){ + String encoded=arguments.getString("proofDir64"); + if(encoded==null)throw new Exception("Owned control proof directory required"); + String directory=new String(android.util.Base64.decode(encoded,android.util.Base64.DEFAULT),java.nio.charset.StandardCharsets.UTF_8); + new NativeControlChecks(this).run(directory,result); + } + } + if("settings".equals(arguments.getString("mode")))new SettingsTouchChecks(this).run(result); + if(isFixture){ + menu("fit"); + if(!"true".equals(js("getComputedStyle(document.getElementById('video')).transform.startsWith('matrix(2')")))throw new Exception("Zoom control failed"); + menu("fit");menu("mode"); + if(!"true".equals(js("document.getElementById('mode').getAttribute('aria-pressed')==='true'")))throw new Exception("Trackpad mode failed"); + menu("mode");result.putBoolean("zoomAndModeControls",true); + tap("#viewport");stage("touch-sent");waitTerminalKeyboard(); + commitText("Проверка CanvasLink 42"); + SystemClock.sleep(600);tap("[data-key=Enter]"); + stage("text-and-key-sent"); + JSONObject fixture=null; + for(int i=0;i<40;i++){ + js("window.CanvasLink.requestFixture()");SystemClock.sleep(150); + try{fixture=new JSONObject(js("window.CanvasLink.fixture"));}catch(Exception ignored){} + if(fixture!=null&&fixture.optInt("clicks")>0&&fixture.optString("text").contains("Проверка CanvasLink 42")&&fixture.optJSONArray("keys").toString().contains("Enter"))break; + } + if(fixture==null||fixture.optInt("clicks")<1||!fixture.optString("text").contains("Проверка CanvasLink 42")||!fixture.optJSONArray("keys").toString().contains("Enter"))throw new Exception("Fixture input roundtrip failed: clicks="+(fixture==null?-1:fixture.optInt("clicks"))+", text="+(fixture!=null&&fixture.optString("text").contains("Проверка CanvasLink 42"))+", Enter="+(fixture!=null&&fixture.optJSONArray("keys").toString().contains("Enter"))); + result.putBoolean("touchRoundtrip",true);result.putBoolean("unicodeTextRoundtrip",true);result.putBoolean("enterRoundtrip",true); + if("true".equals(arguments.getString("ux")))new DeviceUxChecks(this,activity).run(result); + } + if("true".equals(arguments.getString("draftCheck")))throw new Exception("Removed composer draft flow; use direct-input UX acceptance"); + result.putString("stats",js("window.CanvasLink.stats"));result.putBoolean("fixture",isFixture);result.putBoolean("restored",restoring);result.putString("status","PASS"); + finish(Activity.RESULT_OK,result); + }catch(Exception e){result.putString("status","FAIL");result.putString("error",e.getMessage());finish(Activity.RESULT_CANCELED,result);} + } +} diff --git a/integrations/canvaslink/android/src/io/nexcore/canvaslink/DeviceUxChecks.java b/integrations/canvaslink/android/src/io/nexcore/canvaslink/DeviceUxChecks.java new file mode 100644 index 000000000..55bb43fe9 --- /dev/null +++ b/integrations/canvaslink/android/src/io/nexcore/canvaslink/DeviceUxChecks.java @@ -0,0 +1,89 @@ +package io.nexcore.canvaslink; + +import android.content.pm.ActivityInfo; +import android.os.Bundle; +import android.os.ParcelFileDescriptor; +import android.os.SystemClock; +import android.view.InputDevice; +import android.view.KeyEvent; +import android.view.MotionEvent; +import android.view.inputmethod.InputConnection; +import org.json.JSONObject; +import java.io.FileInputStream; + +/** Additional adb-only checks on the owned fixture, never a user's terminal. */ +final class DeviceUxChecks { + final AcceptanceInstrumentation test; final MainActivity activity; + DeviceUxChecks(AcceptanceInstrumentation test,MainActivity activity){this.test=test;this.activity=activity;} + boolean value(String expression)throws Exception{return "true".equals(test.js(expression));} + void require(boolean ok,String message)throws Exception{if(!ok)throw new Exception(message);} + void until(String expression,int timeout,String failure)throws Exception{ + long end=SystemClock.uptimeMillis()+timeout; + while(SystemClock.uptimeMillis(){const r=document.getElementById('viewport').getBoundingClientRect();return {x:r.x+r.width*"+x+",y:r.y+r.height*"+y+",d:devicePixelRatio}})()")); + int[] origin=new int[2];test.runOnMainSync(()->activity.web.getLocationOnScreen(origin)); + return new float[]{(float)(r.getDouble("x")*r.getDouble("d"))+origin[0],(float)(r.getDouble("y")*r.getDouble("d"))+origin[1]}; + } + void motion(long down,int action,float[][] coordinates)throws Exception{ + MotionEvent.PointerProperties[] properties=new MotionEvent.PointerProperties[coordinates.length]; + MotionEvent.PointerCoords[] points=new MotionEvent.PointerCoords[coordinates.length]; + for(int i=0;i{const v=document.getElementById('viewport').getBoundingClientRect(),bar=document.getElementById('controls').getBoundingClientRect(),rows=[...document.querySelectorAll('#controls button')].map(e=>e.getBoundingClientRect().top);return Math.abs(v.width-innerWidth)<2&&Math.abs(v.height+56-innerHeight)<2&&bar.height===56&&new Set(rows).size===1&&!document.getElementById('compose');})()"),"Direct workspace geometry failed with real IME"); + } + void rotate(int orientation)throws Exception{ + test.runOnMainSync(()->activity.setRequestedOrientation(orientation)); + until(orientation==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT?"screen.height>screen.width":"screen.width>screen.height",6000,"Android orientation did not change");SystemClock.sleep(500); + } + void run(Bundle result)throws Exception{ + test.stage("ux-gestures");test.menu("reset-view"); + test.js("window.__gestureAudit=[];for(const type of ['pointerdown','pointerup','pointercancel','lostpointercapture','canvaslink:pointer'])document.addEventListener(type,e=>{const a=window.__gestureAudit;a.push({type,id:e.pointerId,target:e.target?.id,action:e.detail?.action});if(a.length>40)a.shift();},true)"); + int before=clicks();drag(false,false);int observed=clicks();require(observed==before+1,"Direct drag press mismatch before="+before+", after="+observed+", state="+test.js("JSON.stringify({pending:CanvasLinkClient.getState().pending,ime:CanvasLinkInput.active(),focus:document.activeElement.id,mode:document.getElementById('mode').getAttribute('aria-pressed'),events:window.__gestureAudit})")); + before=clicks();drag(false,true);test.tap("#video","0.5,0.5");require(clicks()==before+2,"Cancellation left held button or duplicate tap");result.putBoolean("nativeDragAndCancel",true); + test.menu("mode");before=clicks();int acks=acked();drag(false,false);require(clicks()==before&&value("!document.getElementById('local-cursor').hidden"),"Trackpad movement clicked or had no cursor");until("CanvasLink.stats.ackedInputs>"+acks,4000,"Trackpad move missing"); + before=clicks();drag(true,false);require(clicks()==before+1,"Hold-drag press count wrong");test.menu("reset-view");before=clicks();acks=acked();pair(false);require(clicks()==before,"Scroll created click");until("CanvasLink.stats.ackedInputs>"+acks,4000,"Native scroll missing");result.putBoolean("nativeTrackpadAndScroll",true); + test.menu("mode");before=clicks();pair(true);require(value("new DOMMatrix(getComputedStyle(document.getElementById('video')).transform).a>1.25"),"Pinch did not zoom");require(clicks()==before,"Pinch ghost click");test.menu("reset-view");result.putBoolean("nativePinchAndFit",true); + test.stage("ux-direct-ime");test.tap("#video","0.5,0.5");test.waitTerminalKeyboard(); + int oldLength=fixture().getString("text").length();boolean[] accepted={false}; + test.runOnMainSync(()->{InputConnection retired=activity.web.onCreateInputConnection(new android.view.inputmethod.EditorInfo());InputConnection input=activity.web.onCreateInputConnection(new android.view.inputmethod.EditorInfo());accepted[0]=input!=null&&!retired.commitText("STALE",1)&&input.setComposingText("日本🙂X",1)&&input.deleteSurroundingText(1,0)&&input.deleteSurroundingTextInCodePoints(1,0)&&((android.view.inputmethod.BaseInputConnection)input).getEditable().toString().equals("日本")&&input.commitText("日本語🙂",1)&&input.finishComposingText();if(input!=null){input.deleteSurroundingText(1,0);input.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_ENTER));input.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_ENTER));}}); + require(accepted[0],"Native composition/commit failed");SystemClock.sleep(500);JSONObject remote=fixture();require(remote.getString("text").substring(oldLength).equals("日本語"),"Composition was duplicated or lost after emoji Backspace");require(remote.getJSONArray("keys").toString().contains("Backspace"),"Native deletion missing");result.putBoolean("directCompositionUnicodeDeleteEnter",true); + int unchanged=acked();test.runOnMainSync(()->((TerminalWebView)activity.web).paste(new String(new char[8001]).replace('\0','x')));until("document.getElementById('input-status').textContent.includes('8000')",4000,"Oversized paste feedback missing");require(value("CanvasLinkClient.getState().connection==='connected'&&CanvasLinkInput.active()"),"Oversized paste interrupted terminal connection");require(acked()==unchanged,"Oversized paste sent remote input");result.putBoolean("staleConnectionCodePointDeleteOversizedPaste",true); + int previous=activity.getRequestedOrientation();try{for(int orientation:new int[]{ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE}){rotate(orientation);test.waitTerminalKeyboard();workspaceFits();}}finally{test.runOnMainSync(()->activity.setRequestedOrientation(previous));SystemClock.sleep(700);}result.putBoolean("singleRowRealImeBothOrientations",true); + shell("input keyevent KEYCODE_BACK");until("!CanvasLinkInput.active()",4000,"Android Back did not retire terminal input");test.tap("#video","0.5,0.5");test.waitTerminalKeyboard();result.putBoolean("keyboardBackDismissAndRetap",true); + test.stage("ux-background");int hostLength=fixture().getString("text").length();shell("input keyevent KEYCODE_HOME");until("document.hidden&&['suspended','reconnecting'].includes(CanvasLinkClient.getState().connection)",8000,"Background not suspended");shell("am start -n io.nexcore.canvaslink/.MainActivity");until("!document.hidden&&CanvasLinkClient.getState().connection==='connected'&&CanvasLink.stats.framesDecoded>8",30000,"Foreground video missing");require(fixture().getString("text").length()==hostLength,"Background replayed text");require(value("!CanvasLinkInput.active()"),"Old native input owner survived background");result.putBoolean("backgroundNoReplay",true); + test.stage("ux-wifi-loss");try{shell("svc wifi disable");until("CanvasLinkClient.getState().connection!=='connected'",20000,"Wi-Fi loss not detected");require(value("!CanvasLinkInput.active()"),"Offline IME not stopped");}finally{shell("svc wifi enable");}until("CanvasLinkClient.getState().connection==='connected'&&CanvasLink.stats.framesDecoded>8",40000,"Wi-Fi video recovery missing");require(fixture().getString("text").length()==hostLength,"Wi-Fi recovery replayed text");result.putBoolean("wifiLossRecoveryNoReplay",true);test.stage("ux-complete"); + } +} diff --git a/integrations/canvaslink/android/src/io/nexcore/canvaslink/GraphicsChecks.java b/integrations/canvaslink/android/src/io/nexcore/canvaslink/GraphicsChecks.java new file mode 100644 index 000000000..a071f4b5b --- /dev/null +++ b/integrations/canvaslink/android/src/io/nexcore/canvaslink/GraphicsChecks.java @@ -0,0 +1,37 @@ +package io.nexcore.canvaslink; + +import android.os.Bundle; +import android.os.SystemClock; +import org.json.JSONArray; + +/** ADB-only visual acceptance; all view controls use real Android touches. */ +final class GraphicsChecks { + final AcceptanceInstrumentation test; + GraphicsChecks(AcceptanceInstrumentation test){this.test=test;} + void until(String expression,String error)throws Exception{ + long end=SystemClock.uptimeMillis()+12000; + while(SystemClock.uptimeMillis()f.family==='Onest'&&f.status==='loaded')","Bundled Onest font was not consumed by WebView"); + until("document.getElementById('view-toggle-label').textContent==='Весь Canvas'&&!!CanvasLinkClient.getState().source?.terminalView","Initial terminal view did not follow source geometry"); + if(!"false".equals(test.js("CanvasLinkInput.active()")))throw new Exception("Visual framing must not start terminal input"); + until("(()=>{const s=CanvasLinkClient.getState().source.terminalView,e=document.getElementById('video'),r=e.getBoundingClientRect(),v=document.getElementById('viewport').getBoundingClientRect(),k=Math.min(r.width/e.videoWidth,r.height/e.videoHeight),left=r.left+(r.width-e.videoWidth*k)/2+s.rect.x*e.videoWidth*k,top=r.top+(r.height-e.videoHeight*k)/2+s.rect.y*e.videoHeight*k;return left>=v.left-2&&top>=v.top-2&&left+s.rect.width*e.videoWidth*k<=v.right+2&&top+s.rect.height*e.videoHeight*k<=v.bottom+2;})()","Initial framing cuts off the selected terminal"); + result.putBoolean("automaticTerminalViewWithoutIme",true);result.putBoolean("bundledFontLoaded",true); + test.stage("graphics-terminal-view-ready");SystemClock.sleep(700); + int sent=Integer.parseInt(test.js("CanvasLink.stats.inputs||0")); + test.tap("#view-toggle"); + until("document.getElementById('zoom-label').textContent==='100%'","Overview button did not restore Canvas"); + SystemClock.sleep(1200); + if(!"100%".equals(new JSONArray("["+test.js("document.getElementById('zoom-label').textContent")+"]").getString(0)))throw new Exception("Source health overrode explicit overview"); + test.stage("graphics-overview-ready");SystemClock.sleep(700);test.tap("#view-toggle"); + until("document.getElementById('view-toggle-label').textContent==='Весь Canvas'","Terminal button did not restore framing"); + if(!"false".equals(test.js("CanvasLinkInput.active()"))||Integer.parseInt(test.js("CanvasLink.stats.inputs||0"))!=sent)throw new Exception("Local view button sent remote input"); + result.putBoolean("viewToggleTouchRoundtripWithoutRemoteInput",true); + String encoded=new JSONArray("["+test.js("(()=>{const v=document.getElementById('video'),c=document.createElement('canvas');c.width=v.videoWidth;c.height=v.videoHeight;c.getContext('2d').drawImage(v,0,0);return c.toDataURL('image/png').split(',')[1];})()")+"]").getString(0); + byte[] frame=android.util.Base64.decode(encoded,android.util.Base64.DEFAULT); + java.nio.file.Files.write(new java.io.File(MainActivity.active.getExternalFilesDir(null),"graphics-decoded-frame.png").toPath(),frame); + result.putBoolean("decodedVideoFrameSavedPrivately",true);test.stage("graphics-decoded-frame-ready");SystemClock.sleep(500); + } +} diff --git a/integrations/canvaslink/android/src/io/nexcore/canvaslink/MainActivity.java b/integrations/canvaslink/android/src/io/nexcore/canvaslink/MainActivity.java new file mode 100644 index 000000000..7e55c0e01 --- /dev/null +++ b/integrations/canvaslink/android/src/io/nexcore/canvaslink/MainActivity.java @@ -0,0 +1,289 @@ +package io.nexcore.canvaslink; + +import android.app.Activity; +import android.os.Bundle; +import android.os.Build; +import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkCapabilities; +import android.net.NetworkRequest; +import android.net.LinkProperties; +import android.net.RouteInfo; +import android.net.nsd.NsdManager; +import android.net.nsd.NsdServiceInfo; +import android.net.wifi.WifiManager; +import android.webkit.*; +import android.view.WindowManager; +import android.util.Log; +import org.json.*; +import java.io.*; +import java.net.*; +import java.nio.charset.StandardCharsets; +import java.security.SecureRandom; +import java.util.*; +import java.util.concurrent.*; +import javax.crypto.*; +import javax.crypto.spec.*; + +public final class MainActivity extends Activity { + static volatile MainActivity active; + WebView web; + private final ScheduledExecutorService network = Executors.newSingleThreadScheduledExecutor(); + private final SecureRandom random = new SecureRandom(); + private NsdManager nsd; private NsdManager.DiscoveryListener discovery; + private WifiManager.MulticastLock multicast; + private final Map found = new HashMap<>(); + private final ArrayDeque resolving = new ArrayDeque<>(); + private final android.os.Handler ui = new android.os.Handler(android.os.Looper.getMainLooper()); + private boolean resolveBusy=false; private int discoveryEpoch=0; + private final ExecutorService storage=Executors.newSingleThreadExecutor(); + private SessionVault drafts; + private String base, id; private byte[] key; private long sent=0, received=0; + private long reserved=0; private SessionVault vault; + private ScheduledFuture polling; private int errors=0; private long networkFailureAt=0; + private ConnectivityManager connectivity; private ConnectivityManager.NetworkCallback localNetworks; + private volatile Network localNetwork; private String localAddresses=""; private volatile boolean destroyed=false; + volatile String acceptance = "", diagnostics = "{}"; + @Override public void onCreate(Bundle saved) { + super.onCreate(saved); + connectivity=(ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE); + watchLocalNetworks(); + getWindow().setStatusBarColor(0xff353442); getWindow().setNavigationBarColor(0xff353442); + web = new TerminalWebView(this); setContentView(web); + web.setBackgroundColor(0xff353442); + WebSettings s=web.getSettings(); s.setJavaScriptEnabled(true); s.setDomStorageEnabled(false); + s.setAllowFileAccess(false); s.setAllowContentAccess(false); s.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW); + s.setMediaPlaybackRequiresUserGesture(false); s.setSupportZoom(false); + web.setWebChromeClient(new WebChromeClient() { + @Override public boolean onConsoleMessage(ConsoleMessage m) { + if (m.messageLevel()==ConsoleMessage.MessageLevel.ERROR) Log.e("CanvasLink", "Web client error at line " + m.lineNumber()); + return true; + } + @Override public void onPermissionRequest(PermissionRequest r) { r.deny(); } + }); + web.setWebViewClient(new WebViewClient() { + @Override public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { + android.net.Uri u=request.getUrl(); + if (!"https".equals(u.getScheme()) || !"appassets.androidplatform.net".equals(u.getHost()) || !u.getPath().startsWith("/client/")) return blocked(); + String file=u.getPath().substring(8); + if (!Arrays.asList("index.html","client.js","style.css","delivery.js","transport.js","gestures.js","direct-input.js","onest.ttf").contains(file)) return blocked(); + try { return new WebResourceResponse(file.endsWith(".js")?"application/javascript":file.endsWith(".css")?"text/css":file.endsWith(".ttf")?"font/ttf":"text/html", "UTF-8", getAssets().open(file)); } + catch(Exception e) { return blocked(); } + } + @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { return true; } + }); + web.addJavascriptInterface(new Bridge(), "Native"); + web.loadUrl("https://appassets.androidplatform.net/client/index.html"); + nsd=(NsdManager)getSystemService(NSD_SERVICE); + vault=new SessionVault(this);drafts=new SessionVault(this,"draft"); + synchronized(MainActivity.class){active=this;} + } + private static WebResourceResponse blocked() { return new WebResourceResponse("text/plain","UTF-8",403,"Blocked",Collections.emptyMap(),new ByteArrayInputStream(new byte[0])); } + private void emit(String type, Object value) { + if(destroyed)return; + JSONObject event=new JSONObject(); try { event.put("type",type); event.put("value",value); }catch(JSONException ignored){} + runOnUiThread(() -> {if(!destroyed)web.evaluateJavascript("window.CanvasLink&&window.CanvasLink.receive("+event.toString()+")",null);}); + } + private void watchLocalNetworks(){ + localNetworks=new ConnectivityManager.NetworkCallback(){ + private void refresh(){if(!destroyed)network.execute(()->{if(base==null||id==null)return;try{selectLocalNetwork(new URL(base));}catch(Exception e){emit("network",e.getMessage());}});} + @Override public void onAvailable(Network value){refresh();} + @Override public void onCapabilitiesChanged(Network value,NetworkCapabilities capabilities){refresh();} + @Override public void onLinkPropertiesChanged(Network value,LinkProperties properties){refresh();} + @Override public void onLost(Network value){if(value.equals(localNetwork))emit("network-changed","offline");refresh();} + }; + try{connectivity.registerNetworkCallback(localNetworkRequest(),localNetworks,ui);} + catch(SecurityException e){localNetworks=null;} + } + private NetworkRequest localNetworkRequest(){ + return new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI) + .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET).addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN).build(); + } + private synchronized Network selectLocalNetwork(URL url) throws Exception { + if(destroyed)throw new IOException("Приложение закрыто"); + InetAddress address=InetAddress.getByName(url.getHost()); + if(address.isLoopbackAddress())return null; + Network selected=null,fallback=null;String addresses="",fallbackAddresses="";int prefix=0,fallbacks=0; + for(Network candidate:connectivity.getAllNetworks()){ + NetworkCapabilities capabilities=connectivity.getNetworkCapabilities(candidate); + if(capabilities==null||!capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)|| + !(capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)||capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)))continue; + LinkProperties properties=connectivity.getLinkProperties(candidate);if(properties==null)continue; + boolean defaultRoute=false; + for(RouteInfo route:properties.getRoutes()){ + int bits=route.getDestination().getPrefixLength(); + if(bits==0&&route.getDestination().getAddress() instanceof Inet4Address)defaultRoute=true; + if(bits>0&&(bits>prefix||bits==prefix&&candidate.equals(localNetwork))&&route.getDestination().contains(address)){ + selected=candidate;prefix=bits;addresses=properties.getLinkAddresses().toString(); + } + } + if(defaultRoute){fallbacks++;fallback=candidate;fallbackAddresses=properties.getLinkAddresses().toString();} + } + // A sole physical LAN can reach a neighbouring private subnet via its router. + if(selected==null&&fallbacks==1){selected=fallback;addresses=fallbackAddresses;} + if(selected==null){ + if(localNetwork!=null){localNetwork=null;emit("network-changed","offline");} + // Keep the old process binding on loss: never fall back to cellular/VPN. + throw new IOException("Нет локального маршрута к ПК. Подключите устройства к одной сети Wi-Fi и разрешите доступ к локальной сети в VPN."); + } + if(!selected.equals(localNetwork)||!addresses.equals(localAddresses)){ + if(localNetwork!=null)emit("network-changed","offline"); + try{synchronized(MainActivity.class){if(destroyed||active!=this||!connectivity.bindProcessToNetwork(selected))throw new SecurityException();}} + catch(SecurityException e){throw new IOException("Не удалось выбрать локальную сеть. Разрешите CanvasLink доступ к локальной сети в настройках VPN.",e);} + localNetwork=selected;localAddresses=addresses; + // WebRTC runs in WebView; process binding is best effort for its sockets. + if(id!=null)emit("network-changed","ready"); + } + return selected; + } + private String localBase(String host) throws Exception { + if (!host.matches("[0-9.]+:[0-9]{1,5}")) throw new Exception("Нужен локальный IPv4-адрес и порт"); + String[] hp=host.split(":"); String[] parts=hp[0].split("\\."); + if(parts.length!=4) throw new Exception("Неверный адрес"); + int[] a=new int[4]; for(int i=0;i<4;i++){a[i]=Integer.parseInt(parts[i]); if(a[i]<0||a[i]>255)throw new Exception("Неверный адрес");} + boolean local=a[0]==10||a[0]==127||(a[0]==192&&a[1]==168)||(a[0]==172&&a[1]>=16&&a[1]<=31)||(a[0]==169&&a[1]==254); + int p=Integer.parseInt(hp[1]); if(!local||p<1024||p>65535) throw new Exception("Разрешена только локальная сеть"); + return "http://"+hp[0]+":"+p; + } + private JSONObject post(String route, JSONObject payload) throws Exception { + URL url=new URL(base+route);Network selected=selectLocalNetwork(url); + HttpURLConnection c=(HttpURLConnection)(selected==null?url.openConnection(Proxy.NO_PROXY):selected.openConnection(url,Proxy.NO_PROXY)); + c.setConnectTimeout(4000);c.setReadTimeout(5000);c.setInstanceFollowRedirects(false); + c.setRequestMethod("POST");c.setRequestProperty("Content-Type","application/json");c.setDoOutput(true); + byte[] bytes=payload.toString().getBytes(StandardCharsets.UTF_8);c.setFixedLengthStreamingMode(bytes.length); + try { + try(OutputStream out=c.getOutputStream()){out.write(bytes);} + if(c.getResponseCode()!=200) throw new Exception("Компьютер отклонил подключение. Проверьте код и срок его действия."); + try(InputStream in=c.getInputStream();ByteArrayOutputStream out=new ByteArrayOutputStream()){ + byte[] buffer=new byte[4096];int n;while((n=in.read(buffer))!=-1){out.write(buffer,0,n);if(out.size()>150000)throw new Exception("Ответ слишком большой");} + return new JSONObject(out.toString("UTF-8")); + } + }catch(IOException e){throw new IOException("Нет связи с ПК по локальной сети. Проверьте Wi-Fi и разрешение локальных подключений в VPN.",e);} + finally{c.disconnect();} + } + private JSONObject exchange(JSONObject value) throws Exception { + if(id==null||key==null)throw new Exception("Нет сопряжения"); + if(sent+1>=reserved)reserve(); + long seq=++sent;byte[] iv=new byte[12];random.nextBytes(iv); + Cipher cipher=Cipher.getInstance("AES/GCM/NoPadding"); + cipher.init(Cipher.ENCRYPT_MODE,new SecretKeySpec(key,"AES"),new GCMParameterSpec(128,iv)); + cipher.updateAAD(Srp.utf8("CanvasLink/1/"+id+"/"+seq+"/request")); + JSONObject packet=new JSONObject().put("id",id).put("seq",seq).put("iv",Srp.hex(iv)) + .put("data",android.util.Base64.encodeToString(cipher.doFinal(Srp.utf8(value.toString())),android.util.Base64.NO_WRAP)); + JSONObject response=post("/channel",packet); + long next=response.getLong("seq");if(!id.equals(response.getString("id"))||next!=seq||next<=received)throw new SecurityException("Повторный или неподтверждённый ответ"); + String nonce=response.getString("iv");if(!nonce.matches("[0-9a-f]{24}"))throw new SecurityException("Неверный пакет"); + cipher.init(Cipher.DECRYPT_MODE,new SecretKeySpec(key,"AES"),new GCMParameterSpec(128,Srp.bytes(nonce))); + cipher.updateAAD(Srp.utf8("CanvasLink/1/"+id+"/"+next+"/response")); + byte[] plain=cipher.doFinal(android.util.Base64.decode(response.getString("data"),android.util.Base64.NO_WRAP)); + received=next;return new JSONObject(new String(plain,StandardCharsets.UTF_8)); + } + private void connect(String host,String pin){ + network.execute(()->{try{ + if(polling!=null)polling.cancel(false);id=null;key=null; + base=localBase(host);if(!pin.matches("[0-9]{6}"))throw new Exception("Введите шесть цифр"); + Srp srp=new Srp(); + JSONObject challenge=post("/pair/start",new JSONObject().put("A",srp.publicHex()).put("name",(Build.MANUFACTURER+" "+Build.MODEL))); + if(!Srp.USER.equals(challenge.getString("user")))throw new SecurityException("Неверный протокол"); + Srp.Session result=srp.derive(pin,challenge.getString("salt"),challenge.getString("B")); + JSONObject finish=post("/pair/finish",new JSONObject().put("id",challenge.getString("id")).put("proof",Srp.hex(result.proof))); + result.verify(finish.getString("proof")); + String nextId=finish.getString("id");if(!nextId.matches("[0-9a-f]{32}"))throw new SecurityException("Неверный идентификатор"); + id=nextId;key=result.channelKey;sent=0;received=0;reserved=0;errors=0;reserve(); + emit("paired",new JSONObject().put("host",host)); + runOnUiThread(()->getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)); + startPolling(); + }catch(Exception e){emit("error",e.getMessage());}}); + } + private void reserve() throws Exception { + reserved=sent+10000; + vault.save(new JSONObject().put("host",base.substring(7)).put("id",id).put("key",Srp.hex(key)).put("counter",reserved)); + } + private void startPolling(){ + if(polling!=null)polling.cancel(false); + networkFailureAt=0;errors=0; + polling=network.scheduleWithFixedDelay(()->{try{ + JSONObject response=exchange(new JSONObject().put("type","poll"));boolean recovered=errors>0;errors=0;networkFailureAt=0; + if(recovered)emit("network-restored",true); + JSONArray messages=response.getJSONArray("messages");for(int i=0;i=60000){polling.cancel(false);vault.clear();id=null;key=null;emit("disconnected",true);emit("error","Соединение потеряно. Создайте новый код на компьютере.");}}},0,200,TimeUnit.MILLISECONDS); + } + public final class Bridge { + @JavascriptInterface public boolean saveDraft(String text){if(text.length()>8000)return false;try{synchronized(drafts){drafts.save(new JSONObject().put("text",text));}return true;}catch(Exception e){emit("error","Не удалось сохранить черновик");return false;}} + @JavascriptInterface public void loadDraft(){storage.execute(()->{try{JSONObject saved=drafts.load();emit("draft",saved==null?"":saved.optString("text"));}catch(Exception ignored){}});} + @JavascriptInterface public void showKeyboard(String token){runOnUiThread(()->((TerminalWebView)web).openTerminal(token));} + @JavascriptInterface public void terminalKey(String key,String modifiers){runOnUiThread(()->((TerminalWebView)web).key(key,modifiers));} + @JavascriptInterface public void paste(){runOnUiThread(()->{android.content.ClipboardManager clipboard=(android.content.ClipboardManager)getSystemService(CLIPBOARD_SERVICE);if(clipboard.hasPrimaryClip()&&clipboard.getPrimaryClip().getItemCount()>0){CharSequence value=clipboard.getPrimaryClip().getItemAt(0).coerceToText(MainActivity.this);((TerminalWebView)web).paste(value==null?"":value.toString());}else ((TerminalWebView)web).paste("");});} + @JavascriptInterface public void hideKeyboard(){runOnUiThread(()->((TerminalWebView)web).closeTerminal());} + @JavascriptInterface public void connect(String host,String pin){MainActivity.this.connect(host,pin);} + @JavascriptInterface public void signal(String json){ + if(json.length()>90000)return; + network.execute(()->{try{exchange(new JSONObject().put("type","signal").put("message",new JSONObject(json)));}catch(Exception e){emit("error",e.getMessage());}}); + } + @JavascriptInterface public void discover(){runOnUiThread(()->startDiscovery());} + @JavascriptInterface public void restore(){network.execute(()->{try{ + JSONObject saved=vault.load();if(saved==null)return; + base=localBase(saved.getString("host"));id=saved.getString("id");key=Srp.bytes(saved.getString("key")); + if(!id.matches("[0-9a-f]{32}")||key.length!=32)throw new Exception("Invalid saved pairing"); + sent=saved.getLong("counter");received=0;reserve(); + exchange(new JSONObject().put("type","signal").put("message",new JSONObject().put("type","ping"))); + emit("paired",new JSONObject().put("host",saved.getString("host")));startPolling(); + runOnUiThread(()->getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)); + }catch(Exception e){vault.clear();id=null;key=null;emit("network","Введите новый код подключения на компьютере.");}});} + @JavascriptInterface public void disconnect(){network.execute(()->{try{exchange(new JSONObject().put("type","revoke"));}catch(Exception ignored){} if(polling!=null)polling.cancel(false);id=null;key=null;vault.clear();emit("disconnected",true);});runOnUiThread(()->getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON));} + @JavascriptInterface public void diagnostics(String json){if(json.length()<2000){diagnostics=json;Log.i("CanvasLink", "STATS "+json);}} + @JavascriptInterface public void acceptance(String json){if(json.length()<2000)acceptance=json;} + } + private void resolveNext(final int epoch){ + if(epoch!=discoveryEpoch||resolveBusy||resolving.isEmpty())return; + final NsdServiceInfo service=resolving.remove();resolveBusy=true; + nsd.resolveService(service,new NsdManager.ResolveListener(){ + public void onResolveFailed(NsdServiceInfo s,int c){ui.post(()->{if(epoch!=discoveryEpoch)return;resolveBusy=false;emit("discovery","resolving-failed");ui.postDelayed(()->resolveNext(epoch),500);});} + public void onServiceResolved(NsdServiceInfo s){ui.post(()->{ + if(epoch!=discoveryEpoch)return;resolveBusy=false; + try{ + String address=s.getHost().getHostAddress()+":"+s.getPort(); + // Host advertises a private IPv4 hint for API26-32 resolvers that + // return only one IPv6 address. This is discovery, never authority: + // localBase and the existing SRP proof remain mandatory. + byte[] hint=s.getAttributes().get("ipv4"); + if(!(s.getHost() instanceof Inet4Address)&&hint!=null)address=new String(hint,StandardCharsets.UTF_8)+":"+s.getPort(); + localBase(address);found.put(s.getServiceName(),address); + emit("discovered",new JSONObject().put("id",s.getServiceName()).put("name",s.getServiceName()).put("host",address)); + }catch(Exception e){emit("discovery","unsupported-address");} + resolveNext(epoch); + });} + }); + } + private void startDiscovery(){ + if(discovery!=null){ + try{nsd.stopServiceDiscovery(discovery);}catch(Exception ignored){} + discovery=null;discoveryEpoch++;resolving.clear();resolveBusy=false; + for(String name:found.keySet())emit("lost",name);found.clear(); + ui.postDelayed(()->startDiscovery(),500);return; + } + final int epoch=++discoveryEpoch; + WifiManager wifi=(WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE); + if(multicast==null){multicast=wifi.createMulticastLock("CanvasLink discovery");multicast.setReferenceCounted(false);} + if(!multicast.isHeld())multicast.acquire(); + discovery=new NsdManager.DiscoveryListener(){ + public void onDiscoveryStarted(String t){if(epoch==discoveryEpoch)emit("discovery","searching");} + public void onDiscoveryStopped(String t){} + public void onStartDiscoveryFailed(String t,int c){ui.post(()->{if(epoch!=discoveryEpoch)return;discovery=null;emit("discovery","unavailable");if(multicast.isHeld())multicast.release();});} + public void onStopDiscoveryFailed(String t,int c){} + public void onServiceLost(NsdServiceInfo service){ui.post(()->{if(epoch!=discoveryEpoch)return;found.remove(service.getServiceName());resolving.removeIf(s->s.getServiceName().equals(service.getServiceName()));emit("lost",service.getServiceName());});} + public void onServiceFound(NsdServiceInfo service){ui.post(()->{if(epoch!=discoveryEpoch||!service.getServiceName().startsWith("CanvasLink "))return;resolving.add(service);resolveNext(epoch);});} + }; + try{ + if(Build.VERSION.SDK_INT>=33)nsd.discoverServices("_canvaslink._tcp.",NsdManager.PROTOCOL_DNS_SD,localNetworkRequest(),getMainExecutor(),discovery); + else nsd.discoverServices("_canvaslink._tcp.",NsdManager.PROTOCOL_DNS_SD,discovery); + }catch(Exception e){discovery=null;emit("discovery","unavailable");} + } + @Override protected void onDestroy(){ + synchronized(MainActivity.class){destroyed=true;if(active==this){active=null;connectivity.bindProcessToNetwork(null);}} + if(localNetworks!=null)try{connectivity.unregisterNetworkCallback(localNetworks);}catch(Exception ignored){} + if(discovery!=null)try{nsd.stopServiceDiscovery(discovery);}catch(Exception ignored){} + if(multicast!=null&&multicast.isHeld())multicast.release(); + discoveryEpoch++;ui.removeCallbacksAndMessages(null);storage.shutdown();network.shutdownNow();web.destroy();super.onDestroy(); + } +} diff --git a/integrations/canvaslink/android/src/io/nexcore/canvaslink/NativeControlChecks.java b/integrations/canvaslink/android/src/io/nexcore/canvaslink/NativeControlChecks.java new file mode 100644 index 000000000..9565b1eac --- /dev/null +++ b/integrations/canvaslink/android/src/io/nexcore/canvaslink/NativeControlChecks.java @@ -0,0 +1,96 @@ +package io.nexcore.canvaslink; + +import android.os.Bundle; +import android.os.SystemClock; + +/** ADB-only controls against the controller's disposable CanvasTTY terminal. */ +final class NativeControlChecks { + final AcceptanceInstrumentation test; + NativeControlChecks(AcceptanceInstrumentation test){this.test=test;} + void until(String expression,String error)throws Exception{ + long end=SystemClock.uptimeMillis()+10000; + while(SystemClock.uptimeMillis()"+before,"Control was not acknowledged: "+key); + test.waitTerminalKeyboard(); + } + void text(String value)throws Exception{ + if(!"true".equals(test.js("CanvasLinkInput.active()")))throw new Exception("Direct IME must remain active during terminal editing"); + int before=Integer.parseInt(test.js("CanvasLink.stats.ackedInputs"));test.commitText(value); + until("CanvasLink.stats.ackedInputs>"+before+"&&CanvasLinkClient.getState().pending===0","Direct text was not acknowledged"); + } + String quote(String text){return "'"+text.replace("'","'\\''")+"'";} + void cursorVisible()throws Exception{ + until("(()=>{const c=CanvasLinkClient.getState().source?.caret;if(!c)return false;const e=document.getElementById('video'),r=e.getBoundingClientRect(),v=document.getElementById('viewport').getBoundingClientRect(),k=Math.min(r.width/e.videoWidth,r.height/e.videoHeight),x=r.left+(r.width-e.videoWidth*k)/2+(c.x+c.width/2)*e.videoWidth*k,y=r.top+(r.height-e.videoHeight*k)/2+(c.y+c.height/2)*e.videoHeight*k;return x>v.left&&xv.top+8&&yMainActivity.active.setRequestedOrientation(orientation)); + until(orientation==android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT?"screen.height>screen.width":"screen.width>screen.height","Native orientation not applied");test.waitTerminalKeyboard();cursorVisible(); + until("(()=>{const v=document.getElementById('viewport').getBoundingClientRect(),b=document.getElementById('controls').getBoundingClientRect();return Math.abs(v.height+56-innerHeight)<2&&b.height===56&&new Set([...document.querySelectorAll('#controls button')].map(e=>e.getBoundingClientRect().top)).size===1;})()","Native IME workspace geometry failed"); + test.stage(orientation==android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT?"native-real-portrait-ready":"native-real-landscape-ready");android.os.SystemClock.sleep(900); + }}finally{test.runOnMainSync(()->MainActivity.active.setRequestedOrientation(previous));} + result.putBoolean("actualCursorAndSingleRowWithRealImeBothOrientations",true); + } + void run(String directory,Bundle result)throws Exception{ + if(directory==null||!directory.startsWith("/")||directory.indexOf('\n')>=0)throw new Exception("Owned proof directory required"); + cursorVisible();orientations(result); + test.stage("native-five-real-retaps"); + for(int i=0;i<5;i++){test.menu("reset-view");test.tap("#video",test.freshNativePoint());test.waitTerminalKeyboard();cursorVisible();} + result.putInt("realTerminalRetaps",5); + test.stage("native-control-cancel-and-history"); + text("touch "+quote(directory+"/must-not-exist"));key("c");key("Escape");key("ArrowUp");key("ArrowDown");key("c"); + test.stage("native-control-editing"); + text("printf '%s\\n' > "+quote(directory+"/controls-edit.txt")+" 'OKX'"); + key("ArrowLeft");key("Backspace");key("ArrowRight");key("Enter"); + test.stage("native-control-tab"); + text("cd "+quote(directory)+"; : > canvaslink-tab-proof.txt");key("Enter"); + text("printf '%s\\n' 'TAB' > canvaslink-tab-pro");key("Tab");key("Enter"); + test.stage("native-control-no-duplicate-enter"); + for(int i=1;i<=5;i++){text("printf '%s\\n' '"+i+"' >> "+quote(directory+"/controls-once.txt"));key("Enter");} + test.stage("native-direct-composition-paste"); + text("printf '%s\\n' '"); + final android.content.ClipData[] oldClip={null}; + boolean[] direct={false},clipboardChanged={false};try{test.runOnMainSync(()->{ + android.view.inputmethod.InputConnection input=MainActivity.active.web.onCreateInputConnection(new android.view.inputmethod.EditorInfo()); + direct[0]=input!=null&&input.setComposingText("日本X",1)&&input.deleteSurroundingText(1,0)&&input.commitText("日本語🙂X",1)&&input.finishComposingText()&&input.deleteSurroundingText(1,0); + android.content.ClipboardManager clipboard=(android.content.ClipboardManager)MainActivity.active.getSystemService(android.content.Context.CLIPBOARD_SERVICE); + oldClip[0]=clipboard.getPrimaryClip(); + clipboard.setPrimaryClip(android.content.ClipData.newPlainText("CanvasLink acceptance"," · ёЖ")); + clipboardChanged[0]=true; + }); + if(!direct[0])throw new Exception("Native direct composing input failed"); + until("CanvasLinkClient.getState().pending===0","Composition did not finish");test.tap("#paste"); + until("CanvasLinkClient.getState().pending===0","Clipboard input did not finish"); + test.runOnMainSync(()->((android.content.ClipboardManager)MainActivity.active.getSystemService(android.content.Context.CLIPBOARD_SERVICE)).setPrimaryClip(android.content.ClipData.newPlainText("CanvasLink acceptance","a"))); + test.tap("#ctrl");test.tap("#paste");until("CanvasLinkClient.getState().pending===0","Literal one-letter paste did not finish"); + }finally{if(clipboardChanged[0])test.runOnMainSync(()->{android.content.ClipboardManager c=(android.content.ClipboardManager)MainActivity.active.getSystemService(android.content.Context.CLIPBOARD_SERVICE);if(oldClip[0]!=null)c.setPrimaryClip(oldClip[0]);else if(android.os.Build.VERSION.SDK_INT>=28)c.clearPrimaryClip();});} + result.putBoolean("literalClipboardWithLatchedCtrl",true); + text("' > "+quote(directory+"/controls-direct.txt")); + test.runOnMainSync(()->MainActivity.active.web.onCreateInputConnection(new android.view.inputmethod.EditorInfo()).performEditorAction(android.view.inputmethod.EditorInfo.IME_ACTION_NONE)); + until("CanvasLinkClient.getState().pending===0","Native IME Enter did not finish"); + result.putBoolean("directCompositionUnicodeBackspaceClipboardImeEnter",true); + test.stage("native-physical-ime-key");text("printf '%s\\n' '"); + test.runOnMainSync(()->((android.view.inputmethod.InputMethodManager)MainActivity.active.getSystemService(android.content.Context.INPUT_METHOD_SERVICE)).restartInput(MainActivity.active.web)); + test.waitTerminalKeyboard();test.stage("native-physical-ime-key-ready");android.os.SystemClock.sleep(800); + android.accessibilityservice.AccessibilityServiceInfo info=test.getUiAutomation().getServiceInfo();info.flags|=android.accessibilityservice.AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS;test.getUiAutomation().setServiceInfo(info); + android.graphics.Rect keyRect=null;StringBuilder labels=new StringBuilder(); + for(android.view.accessibility.AccessibilityWindowInfo window:test.getUiAutomation().getWindows())if(window.getType()==android.view.accessibility.AccessibilityWindowInfo.TYPE_INPUT_METHOD){keyRect=findImeKey(window.getRoot(),"a",labels);if(keyRect!=null)break;} + if(keyRect==null){result.putString("imeKeyLabels",labels.toString());throw new Exception("Real keyboard letter a not exposed; inspect IME-only labels");} + long at=android.os.SystemClock.uptimeMillis();for(int action:new int[]{android.view.MotionEvent.ACTION_DOWN,android.view.MotionEvent.ACTION_UP}){android.view.MotionEvent e=android.view.MotionEvent.obtain(at,android.os.SystemClock.uptimeMillis(),action,keyRect.centerX(),keyRect.centerY(),0);e.setSource(android.view.InputDevice.SOURCE_TOUCHSCREEN);if(!test.getUiAutomation().injectInputEvent(e,true))throw new Exception("IME key touch rejected");e.recycle();android.os.SystemClock.sleep(80);} + android.os.SystemClock.sleep(400);text("' > "+quote(directory+"/controls-ime-key.txt"));key("Enter");result.putBoolean("physicalKeyboardKeyTouched",true); + result.putBoolean("nativeToolbarControlsAcknowledged",true); + result.putInt("nativeSingleEnterCommands",5); + } + android.graphics.Rect findImeKey(android.view.accessibility.AccessibilityNodeInfo node,String wanted,StringBuilder labels){ + if(node==null)return null;String text=node.getText()==null?"":node.getText().toString(),desc=node.getContentDescription()==null?"":node.getContentDescription().toString(); + if(labels.length()<1500&&(!text.isEmpty()||!desc.isEmpty()))labels.append('[').append(text).append('|').append(desc).append(']'); + android.graphics.Rect r=new android.graphics.Rect();node.getBoundsInScreen(r);if(node.isVisibleToUser()&&!r.isEmpty()&&(wanted.equalsIgnoreCase(text)||wanted.equalsIgnoreCase(desc)))return r; + for(int i=0;i WebRTC -> ordinary compiled CanvasTTY Settings. */ +final class SettingsTouchChecks { + final AcceptanceInstrumentation test; + final DeviceUxChecks gestures; + SettingsTouchChecks(AcceptanceInstrumentation test){this.test=test;gestures=new DeviceUxChecks(test,MainActivity.active);} + JSONObject probe()throws Exception{ + java.io.File file=new java.io.File(MainActivity.active.getExternalFilesDir(null),"acceptance-settings.json"); + file.delete();test.stage("remote-settings-request"); + for(int i=0;i<80&&!file.exists();i++)SystemClock.sleep(100); + if(!file.exists())throw new Exception("Read-only Settings geometry unavailable"); + JSONObject value=new JSONObject(new String(java.nio.file.Files.readAllBytes(file.toPath()),java.nio.charset.StandardCharsets.UTF_8));file.delete();return value; + } + JSONObject rect(JSONObject p,String name)throws Exception{ + JSONObject item=name.startsWith("settings-tab-")?p.getJSONObject("tabs").optJSONObject(name):p.getJSONObject("controls").optJSONObject(name); + if(item==null||item.isNull("rect"))throw new Exception("Settings target not visible: "+name); + JSONObject r=item.getJSONObject("rect");if(!r.optBoolean("hit"))throw new Exception("Settings target covered: "+name);return r; + } + void tap(JSONObject p,String name)throws Exception{JSONObject r=rect(p,name);test.tap("#video",(r.getDouble("x")+r.getDouble("width")/2)+","+(r.getDouble("y")+r.getDouble("height")/2));SystemClock.sleep(500);} + float[] point(double x,double y)throws Exception{ + JSONObject r=new JSONObject(test.js("(()=>{const e=document.getElementById('video'),r=e.getBoundingClientRect(),k=Math.min(r.width/e.videoWidth,r.height/e.videoHeight);return {x:r.x+(r.width-e.videoWidth*k)/2+e.videoWidth*k*"+x+",y:r.y+(r.height-e.videoHeight*k)/2+e.videoHeight*k*"+y+",d:devicePixelRatio}})()")); + int[] origin=new int[2];test.runOnMainSync(()->MainActivity.active.web.getLocationOnScreen(origin));return new float[]{(float)(r.getDouble("x")*r.getDouble("d"))+origin[0],(float)(r.getDouble("y")*r.getDouble("d"))+origin[1]}; + } + void swipe(JSONObject r,double direction)throws Exception{ + double x=r.getDouble("x")+r.getDouble("width")*.9,y=r.getDouble("y"),h=r.getDouble("height"); + float[] a=point(x,y+h*(direction>0?.75:.25)),b=point(x,y+h*(direction>0?.25:.75)); + long down=SystemClock.uptimeMillis();gestures.motion(down,MotionEvent.ACTION_DOWN,new float[][]{a}); + for(int i=1;i<=16;i++){SystemClock.sleep(25);float f=i/16f;gestures.motion(down,MotionEvent.ACTION_MOVE,new float[][]{{a[0]+(b[0]-a[0])*f,a[1]+(b[1]-a[1])*f}});} + gestures.motion(down,MotionEvent.ACTION_UP,new float[][]{b});SystemClock.sleep(600); + } + JSONObject reveal(JSONObject p,String name)throws Exception{ + for(int i=0;i<28;i++){JSONObject c=p.getJSONObject("controls").optJSONObject(name);if(c!=null&&!c.isNull("rect")&&c.getJSONObject("rect").optBoolean("hit"))return p; + JSONObject scroll=p.getJSONObject("scroll");if(scroll.getDouble("top")+scroll.getDouble("height")>=scroll.getDouble("total")-2)break; + swipe(rect(p,"content"),1);p=probe(); + }throw new Exception("Cannot reveal Settings control: "+name); + } + void noKeyboard(String context)throws Exception{ + boolean[] visible={false};test.runOnMainSync(()->visible[0]=MainActivity.active.web.getRootWindowInsets()!=null&&MainActivity.active.web.getRootWindowInsets().isVisible(android.view.WindowInsets.Type.ime())); + if(visible[0]||!"false".equals(test.js("CanvasLinkInput.active()")))throw new Exception("Non-text Settings control opened IME: "+context); + if(!"true".equals(test.js("document.getElementById('zoom-label').textContent==='100%'")))throw new Exception("Non-text Settings control changed Canvas zoom: "+context); + } + void dragRange(JSONObject r)throws Exception{ + float[] a=point(r.getDouble("x")+r.getDouble("width")*.3,r.getDouble("y")+r.getDouble("height")/2),b=point(r.getDouble("x")+r.getDouble("width")*.65,r.getDouble("y")+r.getDouble("height")/2); + long down=SystemClock.uptimeMillis();gestures.motion(down,MotionEvent.ACTION_DOWN,new float[][]{a});for(int i=1;i<=12;i++){SystemClock.sleep(25);float f=i/12f;gestures.motion(down,MotionEvent.ACTION_MOVE,new float[][]{{a[0]+(b[0]-a[0])*f,a[1]+(b[1]-a[1])*f}});}gestures.motion(down,MotionEvent.ACTION_UP,new float[][]{b});SystemClock.sleep(400); + } + void run(Bundle result)throws Exception{ + test.stage("settings-touch-open");test.menu("reset-view");JSONObject p=probe();int framesBeforeTouch=test.decodedFrames();tap(p,"open");p=probe(); + if(!p.optBoolean("open"))throw new Exception("Touch did not open Settings");result.putBoolean("remoteSettingsOpened",true);noKeyboard("open"); + test.waitVideoAfterTouch(framesBeforeTouch);result.putBoolean("remoteSettingsVideoRespondedToTouch",true); + tap(p,"settings-tab-general");p=probe();boolean wasOn=p.getJSONObject("controls").getJSONObject("toggleOn").getBoolean("selected"); + tap(p,wasOn?"toggleOff":"toggleOn");p=probe();if(p.getJSONObject("controls").getJSONObject("toggleOn").getBoolean("selected")==wasOn)throw new Exception("Touch toggle did not change");noKeyboard("toggle"); + tap(p,wasOn?"toggleOn":"toggleOff");result.putBoolean("remoteToggleChangedAndRestored",true); + tap(probe(),"settings-tab-appearance");p=probe();if(!"settings-panel-appearance".equals(p.optString("tab")))throw new Exception("Touch did not select Appearance"); + double before=p.getJSONObject("scroll").getDouble("top");swipe(rect(p,"content"),1);p=probe(); + if(p.getJSONObject("scroll").getDouble("top")<=before+20)throw new Exception("One-finger Settings swipe did not scroll remote content"); + result.putBoolean("remoteSettingsOneFingerScroll",true);noKeyboard("scroll"); + for(int i=0;i<12&&(p.getJSONObject("controls").optJSONObject("range")==null||p.getJSONObject("controls").getJSONObject("range").isNull("rect"));i++){swipe(rect(p,"content"),1);p=probe();} + double oldRange=p.getJSONObject("controls").getJSONObject("range").getDouble("value"); + tap(p,"range");noKeyboard("range");p=probe();double beforeDrag=p.getJSONObject("controls").getJSONObject("range").getDouble("value");JSONObject slider=rect(p,"range"); + String fresh="(()=>{const p="+slider.toString()+",x=p.x+p.width/2,y=p.y+p.height/2;return CanvasLinkClient.getState().source.dragRects.some(r=>x>=r.x&&x<=r.x+r.width&&y>=r.y&&y<=r.y+r.height)})()"; + if(!"true".equals(test.js(fresh)))throw new Exception("Slider geometry stayed stale after UI resize"); + dragRange(slider);p=probe();double selected=p.getJSONObject("controls").getJSONObject("range").getDouble("value");if(Math.abs(selected-beforeDrag)<.001)throw new Exception("Actual range drag did not change value");result.putBoolean("remoteRangeTouchDrag",true);noKeyboard("range drag"); + test.tap("[data-key=ArrowRight]");SystemClock.sleep(300);p=probe();double next=p.getJSONObject("controls").getJSONObject("range").getDouble("value"); + if(next<=selected)throw new Exception("Range ArrowRight failed without IME"); + for(int i=0;i<60&&Math.abs(next-oldRange)>.001;i++){test.tap(next>oldRange?"[data-key=ArrowLeft]":"[data-key=ArrowRight]");SystemClock.sleep(120);p=probe();next=p.getJSONObject("controls").getJSONObject("range").getDouble("value");} + if(Math.abs(next-oldRange)>.001)throw new Exception("Range could not be restored"); + result.putBoolean("remoteRangeNoKeyboard",true);result.putBoolean("remoteRangeScreenKeysChangedAndRestored",true); + test.stage("settings-text-and-select");tap(probe(),"settings-tab-agents");p=reveal(probe(),"preset");tap(p,"preset");p=reveal(probe(),"text"); + JSONObject textRect=rect(p,"text");test.tap("#video",(textRect.getDouble("x")+textRect.getDouble("width")*.96)+","+(textRect.getDouble("y")+textRect.getDouble("height")/2));test.waitTerminalKeyboard();test.stage("settings-text-ime-ready");SystemClock.sleep(800); + JSONObject legibility=new JSONObject(test.js("(()=>{const e=document.getElementById('video'),r=e.getBoundingClientRect(),k=Math.min(r.width/e.videoWidth,r.height/e.videoHeight);return {font:"+p.getJSONObject("controls").getJSONObject("text").getDouble("fontSize")+"*e.videoWidth/(CanvasLinkClient.getState().source.viewport?.width||e.videoWidth)*k,scale:new DOMMatrix(getComputedStyle(e).transform).a}})()"));if(legibility.getDouble("font")<11.5||legibility.getDouble("scale")>4.01)throw new Exception("Settings field too small or excessively zoomed with real IME"); + test.commitText(" · CL touch");SystemClock.sleep(400);p=probe();if(!p.getJSONObject("controls").getJSONObject("text").optBoolean("testMarker"))throw new Exception("Text field did not receive direct IME commit"); + test.tap("#keyboard-toggle");SystemClock.sleep(400);noKeyboard("text edit ended");result.putBoolean("remoteTextReadableWithImeAndOverviewRestored",true);p=probe();tap(p,"save");SystemClock.sleep(500); + tap(probe(),"settings-tab-general");tap(probe(),"settings-tab-agents");p=reveal(probe(),"text");if(!p.getJSONObject("controls").getJSONObject("text").optBoolean("testMarker"))throw new Exception("Settings text did not persist across tab reopen");result.putBoolean("remoteTextDirectImeSavedAndReopened",true); + p=reveal(p,"select");int prior=p.getJSONObject("controls").getJSONObject("select").getInt("index");tap(p,"select");noKeyboard("select");test.stage("settings-select-open");SystemClock.sleep(800); + p=probe();int choice=prior==0?1:0;tap(p,"option-"+choice);p=probe(); + if(p.getJSONObject("controls").getJSONObject("select").getInt("index")!=choice)throw new Exception("Select did not change through actual option touch");result.putBoolean("remoteSelectOptionTouch",true); + tap(p,"select");test.tap("[data-key=ArrowDown]");test.tap("[data-key=Enter]");SystemClock.sleep(400);p=probe(); + int keyboardChoice=p.getJSONObject("controls").getJSONObject("select").getInt("index");if(keyboardChoice==choice)throw new Exception("Select did not change through screen keys");result.putBoolean("remoteSelectScreenKeys",true); + tap(p,"select");test.tap("[data-key=ArrowUp]");test.tap("[data-key=Escape]");p=probe();if(p.getJSONObject("controls").getJSONObject("select").getInt("index")!=keyboardChoice||p.getJSONObject("controls").has("option-0"))throw new Exception("Select Escape did not dismiss without applying");result.putBoolean("remoteSelectEscape",true);noKeyboard("select options"); + tap(p,"save");tap(probe(),"settings-tab-general");tap(probe(),"settings-tab-agents");p=reveal(probe(),"select");if(p.getJSONObject("controls").getJSONObject("select").getInt("index")!=keyboardChoice)throw new Exception("Select value did not persist");result.putBoolean("remoteSelectSavedAndReopened",true); + tap(p,"remove");SystemClock.sleep(400); + tap(probe(),"close");if(probe().optBoolean("open"))throw new Exception("Touch did not close Settings");result.putBoolean("remoteSettingsClosed",true); + } +} diff --git a/integrations/canvaslink/android/src/io/nexcore/canvaslink/Srp.java b/integrations/canvaslink/android/src/io/nexcore/canvaslink/Srp.java new file mode 100644 index 000000000..0c4c92cf1 --- /dev/null +++ b/integrations/canvaslink/android/src/io/nexcore/canvaslink/Srp.java @@ -0,0 +1,61 @@ +package io.nexcore.canvaslink; + +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.SecureRandom; +import java.util.Arrays; + +/** SRP-6a/SHA-256 interoperability with secure-remote-password 0.3.1. + * Fixed RFC 5054 2048-bit group. PIN is never sent to the network. */ +public final class Srp { + static final String PRIME = "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC319294" + + "3DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310D" + + "CD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FB" + + "D5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF74" + + "7359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A" + + "436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D" + + "5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E73" + + "03CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB6" + + "94B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73"; + static final BigInteger N = new BigInteger(PRIME, 16), G = BigInteger.valueOf(2); + static final String USER = "CanvasLink/v1"; + final BigInteger secret, publicValue; + public Srp() { byte[] a = new byte[32]; new SecureRandom().nextBytes(a); secret = new BigInteger(1, a); publicValue = G.modPow(secret, N); } + Srp(String fixedSecret) { secret = new BigInteger(fixedSecret, 16); publicValue = G.modPow(secret, N); } + public String publicHex() { return hex(pad(publicValue, 256)); } + public static byte[] utf8(String value) { return value.getBytes(StandardCharsets.UTF_8); } + public static byte[] hash(byte[]... values) { + try { MessageDigest d = MessageDigest.getInstance("SHA-256"); for (byte[] v : values) d.update(v); return d.digest(); } + catch (Exception e) { throw new IllegalStateException(e); } + } + public static String hex(byte[] value) { StringBuilder s = new StringBuilder(); for (byte b : value) s.append(String.format(java.util.Locale.ROOT, "%02x", b & 255)); return s.toString(); } + public static byte[] bytes(String value) { + if (value == null || !value.matches("[0-9a-fA-F]+") || value.length() % 2 != 0) throw new IllegalArgumentException("Invalid hex"); + byte[] out = new byte[value.length() / 2]; for (int i = 0; i < out.length; i++) out[i] = (byte) Integer.parseInt(value.substring(i*2, i*2+2), 16); return out; + } + static byte[] pad(BigInteger value, int length) { + byte[] raw = value.toByteArray(), out = new byte[length]; + int count = Math.min(length, raw.length); System.arraycopy(raw, raw.length-count, out, length-count, count); return out; + } + public Session derive(String pin, String saltHex, String serverHex) { + if (!pin.matches("[0-9]{6}") || !saltHex.matches("[0-9a-f]{64}") || !serverHex.matches("[0-9a-f]{512}")) throw new IllegalArgumentException("Invalid SRP challenge"); + byte[] salt = bytes(saltHex), a = pad(publicValue, 256), b = bytes(serverHex), n = bytes(PRIME), g = {2}; + BigInteger B = new BigInteger(1, b); + if (B.mod(N).equals(BigInteger.ZERO)) throw new IllegalArgumentException("Invalid server ephemeral"); + BigInteger k = new BigInteger(1, hash(n, g)); + BigInteger x = new BigInteger(1, hash(salt, hash(utf8(USER + ":" + pin)))); + BigInteger u = new BigInteger(1, hash(a, b)); + if (u.equals(BigInteger.ZERO)) throw new IllegalArgumentException("Invalid scrambling parameter"); + BigInteger S = B.subtract(k.multiply(G.modPow(x, N))).mod(N).modPow(secret.add(u.multiply(x)), N); + byte[] K = hash(pad(S, 256)), xor = hash(n), hg = hash(g); + for (int i=0; ishowTerminal(next),150);postDelayed(()->showTerminal(next),350); + } + private void showTerminal(String owner){if(owner.equals(token)&&!owner.isEmpty()){requestFocus();ime().showSoftInput(this,InputMethodManager.SHOW_IMPLICIT);}} + void closeTerminal(){token="";connection=null;ime().hideSoftInputFromWindow(getWindowToken(),0);ime().restartInput(this);} + boolean terminalActive(){return !token.isEmpty();} + @Override public boolean onTouchEvent(MotionEvent event){ + String owner=token;boolean handled=super.onTouchEvent(event); + // Blink dismisses its DOM keyboard when a toolbar button is touched. Our + // terminal input connection still owns focus, so keep the terminal IME up. + // Menu/explicit hide invalidates owner before this delayed callback runs. + if(event.getActionMasked()==MotionEvent.ACTION_UP&&!owner.isEmpty())postDelayed(()->showTerminal(owner),150); + return handled; + } + private void event(JSONObject value){ + if(token.isEmpty())return; + try{value.put("token",token);}catch(JSONException ignored){} + evaluateJavascript("window.CanvasLinkInput&&window.CanvasLinkInput.receive("+value.toString()+")",null); + } + private void text(String value){if(value.isEmpty())return;try{event(new JSONObject().put("type","text").put("text",value));}catch(JSONException ignored){}} + void key(String key,String modifiers){ + if(connection!=null)connection.finishComposingText(); + try{event(new JSONObject().put("type","key").put("key",key).put("modifiers",new JSONArray(modifiers)));}catch(JSONException ignored){} + } + void paste(String value){ + if(value.isEmpty()){try{event(new JSONObject().put("type","notice").put("text","Буфер обмена пуст"));}catch(JSONException ignored){}return;} + if(value.length()>8000){try{event(new JSONObject().put("type","notice").put("text","В буфере больше 8000 символов"));}catch(JSONException ignored){}return;} + if(connection!=null)connection.finishComposingText(); + try{event(new JSONObject().put("type","paste").put("text",value));}catch(JSONException ignored){} + } + @Override public boolean onCheckIsTextEditor(){return terminalActive()||super.onCheckIsTextEditor();} + @Override public InputConnection onCreateInputConnection(EditorInfo out){ + if(!terminalActive())return super.onCreateInputConnection(out); + out.inputType=InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS|InputType.TYPE_TEXT_FLAG_MULTI_LINE; + out.imeOptions=EditorInfo.IME_FLAG_NO_EXTRACT_UI|EditorInfo.IME_FLAG_NO_FULLSCREEN|EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING|EditorInfo.IME_ACTION_NONE; + out.initialSelStart=out.initialSelEnd=0; + return connection=new TerminalConnection(token); + } + private boolean hardware(KeyEvent e){ + if(!terminalActive()||e.getKeyCode()==KeyEvent.KEYCODE_BACK)return false; + String k=null; + switch(e.getKeyCode()){ + case KeyEvent.KEYCODE_ENTER:k="Enter";break;case KeyEvent.KEYCODE_DEL:k="Backspace";break; + case KeyEvent.KEYCODE_FORWARD_DEL:k="Delete";break;case KeyEvent.KEYCODE_TAB:k="Tab";break; + case KeyEvent.KEYCODE_ESCAPE:k="Escape";break;case KeyEvent.KEYCODE_DPAD_LEFT:k="ArrowLeft";break; + case KeyEvent.KEYCODE_DPAD_RIGHT:k="ArrowRight";break;case KeyEvent.KEYCODE_DPAD_UP:k="ArrowUp";break; + case KeyEvent.KEYCODE_DPAD_DOWN:k="ArrowDown";break;case KeyEvent.KEYCODE_MOVE_HOME:k="Home";break; + case KeyEvent.KEYCODE_MOVE_END:k="End";break;case KeyEvent.KEYCODE_PAGE_UP:k="PageUp";break; + case KeyEvent.KEYCODE_PAGE_DOWN:k="PageDown";break; + } + int code=e.getUnicodeChar(); + if(k==null&&code==0)return false; + if(e.getAction()!=KeyEvent.ACTION_DOWN)return true; + JSONArray mods=new JSONArray();if(e.isCtrlPressed())mods.put("control");if(e.isAltPressed())mods.put("alt");if(e.isShiftPressed())mods.put("shift");if(e.isMetaPressed())mods.put("meta"); + if(k!=null)key(k,mods.toString()); + else if(e.isCtrlPressed()||e.isMetaPressed()){ + int base=e.getUnicodeChar(0);if(base>0)key(new String(Character.toChars(base)).toLowerCase(java.util.Locale.ROOT),mods.toString()); + }else if(code>0&&code<=Character.MAX_CODE_POINT){if(connection!=null)connection.finishComposingText();text(new String(Character.toChars(code)));} + return true; + } + @Override public boolean dispatchKeyEvent(KeyEvent e){return hardware(e)||super.dispatchKeyEvent(e);} + @Override public boolean onKeyPreIme(int code,KeyEvent event){ + if(code==KeyEvent.KEYCODE_BACK&&terminalActive()){ + if(event.getAction()==KeyEvent.ACTION_UP){closeTerminal();evaluateJavascript("window.CanvasLinkInput&&window.CanvasLinkInput.stop()",null);} + return true; + } + return super.onKeyPreIme(code,event); + } + private final class TerminalConnection extends BaseInputConnection { + final String owner;final Editable composing=new SpannableStringBuilder(); + TerminalConnection(String owner){super(TerminalWebView.this,true);this.owner=owner;Selection.setSelection(composing,0);} + private boolean valid(){return connection==this&&owner.equals(token)&&!token.isEmpty();} + @Override public Editable getEditable(){return composing;} + private void preedit(){try{event(new JSONObject().put("type","composition").put("text",composing.toString()));}catch(JSONException ignored){}ime().updateSelection(TerminalWebView.this,Selection.getSelectionStart(composing),Selection.getSelectionEnd(composing),getComposingSpanStart(composing),getComposingSpanEnd(composing));} + private void clear(){composing.clear();Selection.setSelection(composing,0);preedit();} + @Override public boolean setComposingText(CharSequence value,int cursor){if(!valid())return false;super.setComposingText(value,cursor);preedit();return true;} + @Override public boolean commitText(CharSequence value,int cursor){if(!valid())return false;String committed=value.toString();clear();if("\n".equals(committed))key("Enter","[]");else text(committed);return true;} + @Override public boolean finishComposingText(){if(!valid())return false;String value=composing.toString();super.finishComposingText();clear();text(value);return true;} + @Override public boolean deleteSurroundingText(int before,int after){ + if(!valid()||before<0||after<0||before>8000||after>8000)return false; + if(composing.length()>0){int cursor=Math.max(0,Selection.getSelectionEnd(composing));int from=Math.max(0,cursor-before),to=Math.min(composing.length(),cursor+after);composing.delete(from,to);Selection.setSelection(composing,from);preedit();return true;} + for(int i=0;i8000||after>8000)return false; + if(composing.length()==0)return deleteSurroundingText(before,after); + int cursor=Math.max(0,Selection.getSelectionEnd(composing)); + int left=Character.codePointCount(composing,0,cursor),right=Character.codePointCount(composing,cursor,composing.length()); + int from=Character.offsetByCodePoints(composing,cursor,-Math.min(before,left)),to=Character.offsetByCodePoints(composing,cursor,Math.min(after,right)); + composing.delete(from,to);Selection.setSelection(composing,from);preedit();return true; + } + @Override public boolean sendKeyEvent(KeyEvent event){return valid()&&hardware(event);} + @Override public boolean performEditorAction(int action){if(!valid())return false;key("Enter","[]");return true;} + @Override public boolean performContextMenuAction(int action){ + if(!valid()||action!=android.R.id.paste)return false; + android.content.ClipboardManager clipboard=(android.content.ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE); + if(clipboard.hasPrimaryClip()&&clipboard.getPrimaryClip().getItemCount()>0){CharSequence value=clipboard.getPrimaryClip().getItemAt(0).coerceToText(getContext());if(value!=null)paste(value.toString());}return true; + } + } +} diff --git a/integrations/canvaslink/design/canvaslink-icon.svg b/integrations/canvaslink/design/canvaslink-icon.svg new file mode 100644 index 000000000..b9f3ffcb1 --- /dev/null +++ b/integrations/canvaslink/design/canvaslink-icon.svg @@ -0,0 +1,10 @@ + + + + + CanvasLink — linked windows + A wide remote window and a square touch surface interlock through two open rails. Critical geometry stays within the central 66dp adaptive safe circle. + + + + diff --git a/integrations/canvaslink/host/discovery.cjs b/integrations/canvaslink/host/discovery.cjs new file mode 100644 index 000000000..bb83a98e1 --- /dev/null +++ b/integrations/canvaslink/host/discovery.cjs @@ -0,0 +1,32 @@ +const {spawn}=require('node:child_process'); +const {lanAddresses}=require('./network.cjs'); +// macOS mDNSResponder owns multicast publication. UDP-only JS Bonjour can +// report "up" while its records remain invisible to native DNS-SD browsers. +function publish({name,port,ipv4=lanAddresses()[0]||'',embedded=false}){ + if(process.platform==='darwin'){ + const child=spawn('/usr/bin/dns-sd',['-R',name,'_canvaslink._tcp','local.',String(port),'v=2','ipv4='+ipv4,'host='+(embedded?'canvastty':'standalone')],{stdio:'ignore'}); + child.on('error',()=>{}); + return {destroy(){child.kill('SIGTERM');}}; + } + const {Bonjour}=require('bonjour-service'),publishers=new Map();let destroyed=false; + const stop=bonjour=>bonjour.unpublishAll(()=>bonjour.destroy()); + const refresh=()=>{ + const addresses=lanAddresses(); + for(const [address,bonjour] of publishers)if(!addresses.includes(address)){publishers.delete(address);stop(bonjour);} + for(const address of addresses){ + if(publishers.has(address))continue; + // Bind multicast to the LAN adapter instead of the VPN's default route. + const onError=error=>console.warn('CanvasLink discovery unavailable:',error.code||error.message); + const bonjour=new Bonjour({interface:address,bind:'0.0.0.0'},onError); + bonjour.server.mdns.on('error',onError); + const service=bonjour.publish({name:`${name} (${address})`,host:`canvaslink-${address.replaceAll('.','-')}.local`,type:'canvaslink',port,disableIPv6:true,txt:{v:'2',ipv4:address,host:embedded?'canvastty':'standalone'}}); + const records=service.records.bind(service); + // Bonjour 1.3 enumerates every adapter; advertise only this LAN endpoint. + service.records=()=>records().filter(record=>record.type!=='AAAA'&&(record.type!=='A'||record.data===address)); + publishers.set(address,bonjour); + } + }; + refresh();const timer=setInterval(refresh,5000);timer.unref(); + return {destroy(){if(destroyed)return;destroyed=true;clearInterval(timer);for(const bonjour of publishers.values())stop(bonjour);publishers.clear();}}; +} +module.exports={publish}; diff --git a/integrations/canvaslink/host/display-scale.cjs b/integrations/canvaslink/host/display-scale.cjs new file mode 100644 index 000000000..49dc23ee0 --- /dev/null +++ b/integrations/canvaslink/host/display-scale.cjs @@ -0,0 +1,13 @@ +// Optional high-density startup; the desktop must opt in explicitly. +// Never change density or restart the default desktop process. +function configureRenderScale(app,argv=process.argv){ + if(app.isReady())throw Error('render-scale-requires-startup'); + const scale=argv.find(arg=>arg.startsWith('--force-device-scale-factor=')); + if(scale){if(!['--force-device-scale-factor=1','--force-device-scale-factor=2'].includes(scale))throw Error('invalid-render-scale');return;} + // Electron 43/macOS reads this switch before JavaScript: appendSwitch at the + // top of main still leaves DPR=1. A normal preview launch restarts once with + // the process-start switch. The owned acceptance launcher already passes it. + app.relaunch({args:[...argv.slice(1),'--force-device-scale-factor=2']}); + app.exit(0); +} +module.exports={configureRenderScale}; diff --git a/integrations/canvaslink/host/embedded.cjs b/integrations/canvaslink/host/embedded.cjs new file mode 100644 index 000000000..684ffe81a --- /dev/null +++ b/integrations/canvaslink/host/embedded.cjs @@ -0,0 +1,138 @@ +// CanvasTTY main-process integration. No global input or desktop capture. +const {app,BrowserWindow,ipcMain,session,nativeImage}=require('electron'); +const http=require('node:http'),os=require('node:os'),path=require('node:path'),fs=require('node:fs'); +const {publish}=require('./discovery.cjs'); +const {lanAddresses:addresses}=require('./network.cjs'); +const {Pairing,privateAddress}=require('./security.cjs'); +const {InputQueue}=require('./input-queue.cjs'); +const {focusOwnedWindow}=require('./owned-focus.cjs'); +const {OwnedPointer}=require('./owned-pointer.cjs'); +const {installSettingsHost}=require('./settings-host.cjs'); +const {validateInput}=require('./native-input.cjs'); +const {selectPointerScript,closeSelectScript}=require('./remote-select.cjs'); +const {createSnapshot}=require('./snapshot.cjs'); +function install({getWindow,glasses}={}){ + let target,panel,server,bonjour,pairing,port,frameTimer,healthTimer,enabled=false; + const queues=new Map(),epochs=new Map(),pointer=new OwnedPointer();let bound=false,captureMode='tab',captureError='',captureQuality=null,frameSequence=0,pendingFrame=0,healthBusy=false,selectPointer=false,sourceSurface='canvas',lastHealth=0;let report={phase:'disabled'},lastFrame=0,startFallback=()=>{}; + const runtime=()=>path.join(app.getPath('userData'),'canvaslink'); + const option=name=>{const i=process.argv.indexOf(name);return i<0?undefined:process.argv[i+1];}; + const closeRemoteSelect=()=>{selectPointer=false;if(target&&!target.isDestroyed())target.webContents.executeJavaScript(closeSelectScript).catch(()=>{});}; + const ownWindow=()=>getWindow?.()||BrowserWindow.getAllWindows().find(w=>!w.getParentWindow()&&/\/renderer\/index\.html/.test(w.webContents.getURL())); + const settingsSnapshot=()=>({enabled,phase:enabled?report.phase:'disabled',pin:enabled?pairing.pin:'',expires:enabled?pairing.expires:0,devices:enabled?[...pairing.sessions.values()].filter(s=>s.expires>Date.now()).map(s=>({name:s.name})):[]}); + installSettingsHost({ipcMain,getWindow:ownWindow,snapshot:settingsSnapshot,command:async action=>{ + if(action==='enable')await open(); + else if(action==='disable')disable(); + else{if(!enabled)throw Error('canvaslink-disabled');pairing.rotate();report={phase:'waiting',inputs:0,error:''};events({type:'state'});} + return settingsSnapshot(); + }}); + const events=value=>{if(panel&&!panel.isDestroyed())panel.webContents.send('link:event',value);}; + const source=()=>({id:'embedded:'+target.id,name:target.getTitle(),pid:process.pid}); + const state=()=>{const [width,height]=target.getContentSize();return {pin:pairing.pin,expires:pairing.expires,addresses:addresses(),port,fixture:false,embedded:true,nativeCapture:true,captureMode,glassesSupported:!!glasses,width,height,source:target.getTitle(),connected:[...pairing.sessions.values()].map(s=>({id:s.id,name:s.name})),report};}; + const trusted=event=>{if(!panel||event.sender!==panel.webContents||event.senderFrame!==panel.webContents.mainFrame)throw Error('untrusted-renderer');}; + const authorized=id=>enabled&&pairing?.sessions.has(id)&&pairing.sessions.get(id).expires>Date.now()&&target&&!target.isDestroyed(); + function safeSignal(message){if(!message||!['offer','answer','disconnect','ping'].includes(message.type))throw Error('invalid-signal');if(['offer','answer'].includes(message.type)&&(typeof message.sdp!=='string'||message.sdp.length>80000))throw Error('invalid-sdp');return message;} + function stopFrames(){clearInterval(healthTimer);clearInterval(frameTimer);frameTimer=null;try{if(target&&!target.isDestroyed())target.webContents.endFrameSubscription();}catch{}} + const {focusScript}=require('./focus.cjs'); + async function apply(input,active){ + validateInput(input);if(!target||target.isDestroyed())throw Error('window-closed'); + if(target.isMinimized())throw Error('window-minimized'); + // Electron routes these events solely to its own renderer. Never use CGEvent + // or system keystrokes, and never inject into another app's focused window. + // The pairing child must not retain key-window focus over its parent. + if(panel&&!panel.isDestroyed()&&panel.isVisible())panel.hide(); + await focusOwnedWindow({app,target,active}); + if(!target.isFocused())throw Error('canvas-focus-required'); + const wc=target.webContents,[width,height]=target.getContentSize(); + const point={x:Math.min(width-1,Math.round(input.x*width)),y:Math.min(height-1,Math.round(input.y*height))}; + if(input.type==='text'){const focus=await wc.executeJavaScript(focusScript);if(!active())throw Error('session-ended');if(!focus.editable)throw Error('text-target-required');await wc.insertText(input.text);return;} + if(input.type==='pointer'){ + if(input.action==='down')selectPointer=input.button!==2&&await wc.executeJavaScript(selectPointerScript(point.x,point.y)); + if(!active()){await wc.executeJavaScript(closeSelectScript);throw Error('session-ended');} + if(!selectPointer)pointer.send(wc,{...point,type:{down:'mouseDown',up:'mouseUp',move:'mouseMove'}[input.action],button:input.button===2?'right':'left',clickCount:1,modifiers:input.drag?['leftButtonDown']:[]});if(input.action==='up'){ + selectPointer=false; + // Input dispatch is asynchronous. Read focus after the renderer has handled + // the click and settled its layout, rather than acknowledging old focus. + const focus=await wc.executeJavaScript(`new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(()=>resolve(${focusScript}))))`); + sourceSurface=focus.surface;if(!active())throw Error('session-ended');return {focus};}return;} + if(input.type==='scroll'){wc.sendInputEvent({...point,type:'mouseWheel',deltaX:-input.dx,deltaY:-input.dy,canScroll:true});return;} + const keyCode={Enter:'Return',ArrowLeft:'Left',ArrowRight:'Right',ArrowUp:'Up',ArrowDown:'Down'}[input.key]||input.key; + wc.sendInputEvent({type:'keyDown',keyCode,modifiers:input.modifiers});wc.sendInputEvent({type:'keyUp',keyCode,modifiers:input.modifiers}); + } + function reply(res,status,body){res.writeHead(status,{'Content-Type':'application/json','Cache-Control':'no-store','X-Content-Type-Options':'nosniff'});res.end(JSON.stringify(body));} + async function serve(req,res){ + if(!enabled||!privateAddress(req.socket.remoteAddress)||req.headers.origin||req.method!=='POST'||!['/pair/start','/pair/finish','/channel'].includes(req.url)){reply(res,403,{error:'forbidden'});return;} + if(!/^application\/json(?:;|$)/i.test(req.headers['content-type']||'')){reply(res,415,{error:'json-required'});return;} + try{let size=0,chunks=[];for await(const chunk of req){size+=chunk.length;if(size>131072)throw Error('too-large');chunks.push(chunk);}const body=JSON.parse(Buffer.concat(chunks).toString());let result; + if(req.url==='/pair/start')result=pairing.start(body,req.socket.remoteAddress); + else if(req.url==='/pair/finish')result=pairing.finish(body,req.socket.remoteAddress); + else result=pairing.receive(body,req.socket.remoteAddress,(s,message)=>{ + if(message.type==='poll')return {messages:s.queue.splice(0),source:target.getTitle()}; + if(message.type==='signal'){events({type:'signal',id:s.id,message:safeSignal(message.message)});return {ok:true};} + if(message.type==='revoke'){pairing.rotate();events({type:'state'});return {ok:true};}throw Error('unsupported-operation'); + });reply(res,200,result); + }catch{if(!res.headersSent)reply(res,403,{error:'request-rejected'});} + } + function bindIPC(){ + if(bound)return;bound=true; + ipcMain.handle('link:input-session',(event,id,token)=>{trusted(event);if(token===null){epochs.delete(id);pointer.release();selectPointer=false;target?.webContents.executeJavaScript(closeSelectScript).catch(()=>{});}else{if(!authorized(id))throw Error('not-authorized');pointer.release();epochs.set(id,token);queues.delete(id);}return true;}); + ipcMain.handle('link:capture-fallback',(event,reason)=>{trusted(event);captureMode='snapshot-fallback';captureError=String(reason||'').replace(/[^A-Za-z]/g,'').slice(0,60);startFallback();return true;}); + ipcMain.on('link:frame-consumed',(event,sequence)=>{trusted(event);if(sequence===pendingFrame)pendingFrame=0;}); + ipcMain.handle('link:glasses',async(event,command)=>{trusted(event);if(!glasses||!['state','begin-pairing','cancel-pairing'].includes(command))throw Error('unsupported-glasses-command');return command==='state'?glasses.state():glasses.command({type:command});}); + ipcMain.handle('link:state',event=>{trusted(event);return state();}); + ipcMain.handle('link:permissions',event=>{trusted(event);return {platform:process.platform,embedded:true};}); + ipcMain.handle('link:settings',event=>{trusted(event);throw Error('not-required-for-owned-window');}); + ipcMain.handle('link:sources',event=>{trusted(event);return [source()];}); + ipcMain.handle('link:select',(event,id)=>{trusted(event);if(id!==source().id)throw Error('invalid-source');return state();}); + ipcMain.handle('link:rotate',event=>{trusted(event);pairing.rotate();return state();}); + ipcMain.handle('link:signal',(event,id,message)=>{trusted(event);if(!authorized(id))throw Error('not-authorized');pairing.queue(id,safeSignal(message));}); + ipcMain.handle('link:input',(event,id,input)=>{ + trusted(event);if(!authorized(id))throw Error('not-authorized');if(typeof input?.id!=='string'||!/^[A-Za-z0-9-]{1,80}$/.test(input.id))throw Error('invalid-input-id'); + let q=queues.get(id);if(!q){const active=()=>authorized(id)&&epochs.get(id)===input.epoch;q=new InputQueue({active,apply:value=>apply(value,active),reply:result=>{const resolve=q.waiters.get(result.id);if(resolve){q.waiters.delete(result.id);resolve(result);}}});q.waiters=new Map();queues.set(id,q);} + if(q.waiters.has(input.id))throw Error('input-in-progress');return new Promise(resolve=>{q.waiters.set(input.id,resolve);q.receive(input);}); + }); + ipcMain.on('link:report',(event,value)=>{ + if(!panel||panel.isDestroyed())return; + trusted(event);if(!value||typeof value.phase!=='string')return; + report={phase:value.phase.slice(0,80),inputs:Number(value.inputs)||0,error:String(value.error||'').slice(0,200)}; + }); + } + async function open(){ + if(enabled)return; + target=ownWindow(); + if(!target||target.isDestroyed())throw Error('canvas-window-unavailable'); + fs.mkdirSync(runtime(),{recursive:true,mode:0o700}); + pairing=new Pairing({onPair:s=>{events({type:'paired',id:s.id});},onRevoke:id=>{epochs.delete(id);queues.delete(id);pointer.release();closeRemoteSelect();events({type:'revoke',id});}}); + server=http.createServer(serve);server.requestTimeout=10000;server.headersTimeout=5000;server.maxConnections=32; + try{await new Promise((resolve,reject)=>{server.once('error',reject);server.listen(Number(option('--canvaslink-port')||3499),'0.0.0.0',resolve);});}catch(error){server.close();throw error;} + try{ + port=server.address().port;enabled=true;captureMode='tab';captureError='';lastFrame=0;pendingFrame=0;bindIPC(); + const captureSession=session.fromPartition('canvaslink-owned-capture'); + panel=new BrowserWindow({width:600,height:700,show:false,parent:target,title:'CanvasTTY · Телефон / планшет',webPreferences:{session:captureSession,preload:path.join(__dirname,'preload.cjs'),contextIsolation:true,sandbox:true,nodeIntegration:false,backgroundThrottling:false}}); + // Older Electron reports display capture as media with an empty mediaTypes + // array. Actual camera/microphone requests remain denied in this own session. + captureSession.setPermissionCheckHandler((contents,permission)=>contents===panel?.webContents&&permission==='display-capture'); + captureSession.setPermissionRequestHandler((contents,permission,callback,details)=>callback(contents===panel?.webContents&&(permission==='display-capture'||permission==='media'&&Array.isArray(details.mediaTypes)&&details.mediaTypes.length===0))); + captureSession.setDisplayMediaRequestHandler((request,callback)=>{try{if(!enabled||request.frame!==panel.webContents.mainFrame||target.isDestroyed())return callback({});callback({video:target.webContents.mainFrame});}catch{callback({});}}); + panel.webContents.setWindowOpenHandler(()=>({action:'deny'}));panel.webContents.on('will-navigate',e=>e.preventDefault()); + panel.on('close',event=>{if(enabled){event.preventDefault();panel.hide();}}); + bonjour=publish({name:`CanvasLink ${os.hostname().split('.')[0]}`,port,ipv4:addresses()[0]||'',embedded:true}); + await panel.loadFile(path.join(__dirname,'index.html')); + startFallback=()=>{if(frameTimer)return;let busy=false; + const frame=image=>{ + const now=Date.now();if(!enabled||!pairing.sessions.size||pendingFrame||now-lastFrame<100||image.isEmpty())return;lastFrame=now; + const encoded=createSnapshot(image,nativeImage);if(!encoded)return; + captureQuality={format:encoded.format,width:encoded.width,height:encoded.height,nativeScale:encoded.scaleFactor,bytes:encoded.pixels.length,encodeMs:Date.now()-now}; + pendingFrame=++frameSequence;events({type:'frame',sequence:pendingFrame,width:encoded.width,height:encoded.height,pixels:encoded.pixels}); + }; + target.webContents.beginFrameSubscription(false,frame); + frameTimer=setInterval(async()=>{if(busy||pendingFrame||!pairing.sessions.size||Date.now()-lastFrame<100||target.isDestroyed())return;busy=true;try{frame(await target.webContents.capturePage(undefined,{stayHidden:true,stayAwake:false}));}catch{}finally{busy=false;}},100); + }; + healthTimer=setInterval(async()=>{pairing.expire();if(healthBusy||!pairing.sessions.size||target.isDestroyed()||sourceSurface!=='settings'&&Date.now()-lastHealth<1000)return;healthBusy=true;lastHealth=Date.now();try{const focus=await target.webContents.executeJavaScript(focusScript);sourceSurface=focus.surface;events({type:'health',value:{capture:captureMode==='tab'?(target.webContents.isBeingCaptured()?'active':'stalled'):(Date.now()-lastFrame<3000?'active':'stalled'),windowFocused:target.isFocused(),target:focus.kind,surface:focus.surface,viewport:focus.viewport,terminalView:focus.terminalView,dragRects:focus.dragRects,rect:focus.rect,caret:focus.caret}});}catch{}finally{healthBusy=false;}},200); + target.removeListener('closed',disable);target.once('closed',disable); + }catch(error){disable();throw error;} + } + function disable(){enabled=false;pairing?.rotate();pointer.release();closeRemoteSelect();stopFrames();bonjour?.destroy();server?.close();if(panel&&!panel.isDestroyed())panel.destroy();queues.clear();epochs.clear();} + app.on('before-quit',()=>{disable();}); + return {open,disable}; +} +module.exports={install}; diff --git a/integrations/canvaslink/host/focus.cjs b/integrations/canvaslink/host/focus.cjs new file mode 100644 index 000000000..5dda9e91b --- /dev/null +++ b/integrations/canvaslink/host/focus.cjs @@ -0,0 +1,18 @@ +// Geometry only; no field values or surrounding text cross the input channel. +function readFocus(){ + const e=document.activeElement,panel=document.querySelector('.settings-panel--open'); + const editable=!!e&&!e.disabled&&!e.readOnly&&(e.tagName==='TEXTAREA'||e.tagName==='INPUT'&&['text','search','email','url','tel','password','number'].includes(e.type)||e.isContentEditable); + const kind=e?.classList.contains('xterm-helper-textarea')?'terminal':editable?'text-field':e?.matches('input,select,button,[role=button],[role=slider]')?'control':'canvas'; + const node=kind==='terminal'?e.closest('.xterm')?.querySelector('.xterm-screen'):editable?e:null; + const r=node?.getBoundingClientRect(),c=kind==='terminal'?e.getBoundingClientRect():null; + const normalized=b=>b&&b.width>0&&b.height>0?{x:b.x/innerWidth,y:b.y/innerHeight,width:b.width/innerWidth,height:b.height/innerHeight}:null; + const clipped=e=>{const r=e.getBoundingClientRect();let x=Math.max(0,r.left),y=Math.max(0,r.top),right=Math.min(innerWidth,r.right),bottom=Math.min(innerHeight,r.bottom);for(let p=e.parentElement;p;p=p.parentElement){const s=getComputedStyle(p);if(/hidden|auto|scroll|clip/.test(s.overflow+s.overflowX+s.overflowY)){const b=p.getBoundingClientRect();x=Math.max(x,b.left);y=Math.max(y,b.top);right=Math.min(right,b.right);bottom=Math.min(bottom,b.bottom);}}return normalized({x,y,width:right-x,height:bottom-y});}; + const dragRects=panel?[...panel.querySelectorAll('input[type=range]')].filter(e=>!e.disabled).map(clipped).filter(Boolean):[]; + const screens=panel?[]:[...document.querySelectorAll('.xterm-screen')].filter(node=>clipped(node)); + const selected=screens.filter(node=>node.closest('.terminal-card--selected')); + const candidate=selected.length===1?selected[0]:screens.length===1?screens[0]:null; + const terminalView=candidate?{rect:clipped(candidate),viewport:{width:innerWidth,height:innerHeight}}:null; + const context=kind==='text-field'?e.closest('.api-profile-row,.provider-secret,.setting-group'):null; + return {editable,kind,terminalView,surface:panel?'settings':'canvas',viewport:{width:innerWidth,height:innerHeight},dragRects,rect:normalized(r),contextRect:context?clipped(context):null,fontSize:editable?parseFloat(getComputedStyle(e).fontSize)||14:null,caret:r&&c&&c.x>=r.x-2&&c.y>=r.y-2&&c.right<=r.right+2&&c.bottom<=r.bottom+2?normalized(c):null}; +} +module.exports={focusScript:'('+readFocus.toString()+')()'}; diff --git a/integrations/canvaslink/host/frames.js b/integrations/canvaslink/host/frames.js new file mode 100644 index 000000000..de05b4467 --- /dev/null +++ b/integrations/canvaslink/host/frames.js @@ -0,0 +1,11 @@ +// NativeImage pixels are BGRA with premultiplied alpha; ImageData expects RGBA. +// The main process waits for frameConsumed before sending another full frame. +(function(root){ + function rgba(pixels,width,height){ + if(!ArrayBuffer.isView(pixels)||pixels.byteLength!==width*height*4||width<1||height<1||width>2560||height>1600)throw Error('invalid-bitmap-frame'); + const data=new Uint8ClampedArray(pixels); + for(let i=0;i + +CanvasLink + +
+
C↗

CanvasLink

Рабочее окно. На расстоянии касания.

LOCAL / 01
+
01 — ПОДКЛЮЧЕНИЕ ANDROID
··· ···
Подготовка…
+
02 — ОКНО ДЛЯ ПЕРЕДАЧИ

На macOS при первом запуске потребуется разрешение на запись экрана. Передаётся выбранное окно CanvasTTY.

+
03 — ПРЯМОЕ СОЕДИНЕНИЕ

Откройте CanvasLink на Android, выберите компьютер и введите код. Оба устройства должны быть в одной локальной сети.

+
Ожидание планшета
+ + +
CANVASTTY → ANDROIDЛокально · Без облака
+
diff --git a/integrations/canvaslink/host/input-queue.cjs b/integrations/canvaslink/host/input-queue.cjs new file mode 100644 index 000000000..959aeebdd --- /dev/null +++ b/integrations/canvaslink/host/input-queue.cjs @@ -0,0 +1,23 @@ +const {validateInput}=require('./native-input.cjs'); +// One peer owns one ordered queue. Keep IDs for the whole peer lifetime: an old +// text/Enter request can never execute twice, even after its ACK was lost. +class InputQueue { + constructor({apply,active,reply,limit=64,maxIds=256}){Object.assign(this,{apply,active,reply,limit,maxIds});this.tail=Promise.resolve();this.ids=new Map();this.pending=0;this.highWater=0;} + receive(value){ + const id=value?.id; + if(typeof id!=='string'||!/^[A-Za-z0-9-]{1,80}$/.test(id))return; + const previous=this.ids.get(id);if(previous){if(previous.result)this.reply(previous.result);return;} + const respond=result=>{this.reply(result);}; + if(!Number.isSafeInteger(value.seq)||value.seq<=this.highWater){respond({type:'input-error',id,error:'input-expired'});return;} + if(!this.active()||this.pending>=this.limit){respond({type:'input-error',id,error:'input-busy'});return;} + try{validateInput(value);}catch(error){respond({type:'input-error',id,error:error.message});return;} + this.highWater=value.seq;const entry={};this.ids.set(id,entry);this.pending++; + for(const [oldId,old] of this.ids){if(this.ids.size<=this.maxIds)break;if(old.result)this.ids.delete(oldId);} + this.tail=this.tail.catch(()=>{}).then(async()=>{ + try{if(!this.active())throw Error('session-ended');const result=await this.apply(value);entry.result={type:'input-ok',id,...(result?.focus?{focus:result.focus}:{})};} + catch(error){entry.result={type:'input-error',id,error:error.message||'input-rejected'};} + finally{this.pending--;if(this.active())respond(entry.result);} + }); + } +} +module.exports={InputQueue}; diff --git a/integrations/canvaslink/host/native-input.cjs b/integrations/canvaslink/host/native-input.cjs new file mode 100644 index 000000000..49bbccc16 --- /dev/null +++ b/integrations/canvaslink/host/native-input.cjs @@ -0,0 +1,11 @@ +const KEYS = new Set(['Enter', 'Tab', 'Escape', 'Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Home', 'End', 'PageUp', 'PageDown', 'c', 'a', 'v', 'z', 'd', 'l']); +function validateInput(e) { + if (!e || !['pointer', 'scroll', 'key', 'text'].includes(e.type)) throw Error('invalid-input'); + if (['pointer', 'scroll'].includes(e.type) && (![e.x, e.y].every(n => Number.isFinite(n) && n >= 0 && n <= 1))) throw Error('invalid-coordinates'); + if (e.type === 'pointer' && !['down', 'up', 'move'].includes(e.action)) throw Error('invalid-pointer'); + if (e.type === 'pointer' && ![0, 2].includes(e.button)) throw Error('invalid-button'); + if (e.type === 'scroll' && (![e.dx, e.dy].every(n => Number.isFinite(n) && Math.abs(n) <= 2000))) throw Error('invalid-scroll'); + if (e.type === 'text' && (typeof e.text !== 'string' || !e.text.length || e.text.length > 8000)) throw Error('invalid-text'); + if (e.type === 'key' && (!Array.isArray(e.modifiers) || !(KEYS.has(e.key)||(/^[a-z]$/.test(e.key)&&e.modifiers.some(m=>m==='control'||m==='meta'))) || e.modifiers.length > 4 || e.modifiers.some(m => !['control', 'shift', 'alt', 'meta'].includes(m)))) throw Error('invalid-key'); +} +module.exports = { validateInput }; diff --git a/integrations/canvaslink/host/network.cjs b/integrations/canvaslink/host/network.cjs new file mode 100644 index 000000000..bf3e8f368 --- /dev/null +++ b/integrations/canvaslink/host/network.cjs @@ -0,0 +1,25 @@ +const os=require('node:os'),fs=require('node:fs'); +const {privateAddress}=require('./security.cjs'); + +function physicalInterface(name,seen=new Set()){ + if(seen.has(name))return false;seen.add(name); + const root='/sys/class/net/'+name; + try{ + if(fs.readFileSync(root+'/operstate','utf8').trim()==='down')return false; + if(fs.existsSync(root+'/device'))return true; + return fs.readdirSync(root).some(link=>link.startsWith('lower_')&&physicalInterface(link.slice(6),seen)); + }catch{return false;} +} +function lanAddresses(){ + const addresses=[]; + for(const [name,entries] of Object.entries(os.networkInterfaces())){ + if(process.platform==='linux'){ + if(!physicalInterface(name))continue; + }else if(/^(?:lo\d*$|utun|tun|tap|ppp|ipsec|docker|veth|virbr|vEthernet|vmnet|vboxnet)|vpn|wireguard|tailscale|zerotier|happ/i.test(name))continue; + for(const entry of entries||[]){ + if((entry.family==='IPv4'||entry.family===4)&&!entry.internal&&privateAddress(entry.address))addresses.push(entry.address); + } + } + return [...new Set(addresses)].sort(); +} +module.exports={lanAddresses}; diff --git a/integrations/canvaslink/host/owned-focus.cjs b/integrations/canvaslink/host/owned-focus.cjs new file mode 100644 index 000000000..358f1a5a5 --- /dev/null +++ b/integrations/canvaslink/host/owned-focus.cjs @@ -0,0 +1,18 @@ +// Activation is asynchronous on macOS. Wait only for our own window; never send +// system keys or continue a revoked input after waiting for activation. +async function focusOwnedWindow({app,target,active,timeout=1000,now=Date.now,wait=ms=>new Promise(resolve=>setTimeout(resolve,ms))}){ + const check=()=>{if(!active())throw Error('session-ended');if(target.isDestroyed())throw Error('window-closed');if(target.isMinimized())throw Error('window-minimized');}; + check(); + const request=()=>{app.focus({steal:true});target.show();target.focus();}; + if(!target.isFocused())request(); + const deadline=now()+timeout;let retryAt=now()+100; + while(!target.isFocused()){ + check();if(now()>=deadline)throw Error('canvas-focus-required'); + // Hiding the pairing child may asynchronously resign the app's key window + // after the first activation request. Reassert only this own window, bounded. + if(now()>=retryAt){request();retryAt=now()+100;} + await wait(20); + } + check();target.webContents.focus(); +} +module.exports={focusOwnedWindow}; diff --git a/integrations/canvaslink/host/owned-pointer.cjs b/integrations/canvaslink/host/owned-pointer.cjs new file mode 100644 index 000000000..f469c481f --- /dev/null +++ b/integrations/canvaslink/host/owned-pointer.cjs @@ -0,0 +1,19 @@ +// Only the owned renderer receives events; cancellation never activates a window. +class OwnedPointer { + constructor(){this.held=new Map();} + send(contents,event){ + contents.sendInputEvent(event); + if(event.type==='mouseDown')this.held.set(event.button,{contents,event}); + else if(event.type==='mouseUp')this.held.delete(event.button); + else if(event.type==='mouseMove')for(const state of this.held.values()){ + if(state.contents===contents)state.event={...state.event,x:event.x,y:event.y}; + } + } + release(){ + const held=[...this.held.values()];this.held.clear(); + for(const {contents,event} of held){ + try{if(!contents.isDestroyed())contents.sendInputEvent({...event,type:'mouseUp',modifiers:[],clickCount:1});}catch{} + } + } +} +module.exports={OwnedPointer}; diff --git a/integrations/canvaslink/host/preload.cjs b/integrations/canvaslink/host/preload.cjs new file mode 100644 index 000000000..a96acbd9e --- /dev/null +++ b/integrations/canvaslink/host/preload.cjs @@ -0,0 +1,17 @@ +const { contextBridge, ipcRenderer } = require('electron'); +contextBridge.exposeInMainWorld('host', { + frameConsumed:sequence=>ipcRenderer.send('link:frame-consumed',sequence), + captureFallback:reason=>ipcRenderer.invoke('link:capture-fallback',reason), + glasses: command => ipcRenderer.invoke('link:glasses',command), + state: () => ipcRenderer.invoke('link:state'), + permissions: () => ipcRenderer.invoke('link:permissions'), + settings: kind => ipcRenderer.invoke('link:settings',kind), + sources: () => ipcRenderer.invoke('link:sources'), + select: id => ipcRenderer.invoke('link:select', id), + rotate: () => ipcRenderer.invoke('link:rotate'), + signal: (id, message) => ipcRenderer.invoke('link:signal', id, message), + inputSession: (id, token) => ipcRenderer.invoke('link:input-session',id,token), + input: (id, event) => ipcRenderer.invoke('link:input', id, event), + report: report => ipcRenderer.send('link:report', report), + onEvent: fn => { const listener = (_event, value) => fn(value); ipcRenderer.on('link:event', listener); return () => ipcRenderer.removeListener('link:event', listener); } +}); diff --git a/integrations/canvaslink/host/remote-select.cjs b/integrations/canvaslink/host/remote-select.cjs new file mode 100644 index 000000000..ce5737428 --- /dev/null +++ b/integrations/canvaslink/host/remote-select.cjs @@ -0,0 +1,30 @@ +// Native OS select popups are outside an owned WebContents stream. Open a +// renderer-local list only for an authenticated remote pointer on a select. +function selectAtPoint(x,y){ + const select=document.elementFromPoint(x,y)?.closest('select'); + if(!select||select.disabled||select.multiple||select.size>1)return false; + window.__canvasLinkSelect?.close(); + const box=document.createElement('div'),r=select.getBoundingClientRect(),style=getComputedStyle(select); + box.setAttribute('role','listbox');box.setAttribute('aria-label',select.getAttribute('aria-label')||select.labels?.[0]?.textContent||'Выбор значения'); + box.dataset.canvaslinkSelect=''; + Object.assign(box.style,{position:'fixed',zIndex:'2147483646',boxSizing:'border-box',width:Math.min(innerWidth-16,Math.max(220,r.width))+'px',maxHeight:Math.min(320,innerHeight-16)+'px',overflowY:'auto',padding:'6px',border:'1px solid #626773',borderRadius:'10px',background:'#20232b',color:'#f3f4f6',boxShadow:'0 8px 32px #0008',font:style.font}); + const entries=[...select.options].map((option,index)=>({option,index,disabled:option.disabled||option.parentElement?.tagName==='OPTGROUP'&&option.parentElement.disabled})); + let current=select.selectedIndex,closed=false; + const close=(restore=true)=>{if(closed)return;closed=true;box.remove();document.removeEventListener('pointerdown',outside,true);document.removeEventListener('keydown',keys,true);window.removeEventListener('resize',dismiss);if(window.__canvasLinkSelect?.box===box)delete window.__canvasLinkSelect;if(restore&&select.isConnected)select.focus({preventScroll:true});}; + const dismiss=()=>close(false); + const choose=index=>{if(entries[index]?.disabled)return;select.selectedIndex=index;select.dispatchEvent(new Event('input',{bubbles:true}));select.dispatchEvent(new Event('change',{bubbles:true}));close();}; + const paint=()=>{for(const button of box.children){const active=Number(button.dataset.index)===current;button.setAttribute('aria-selected',String(active));button.style.background=active?'#485660':'transparent';}box.querySelector('[aria-selected=true]')?.scrollIntoView({block:'nearest'});}; + const outside=event=>{if(!box.contains(event.target))close(false);}; + const keys=event=>{ + if(!['ArrowDown','ArrowUp','Home','End','Enter','Escape','Tab'].includes(event.key))return; + if(event.key==='Tab'){close();return;} + event.preventDefault();event.stopImmediatePropagation(); + if(event.key==='Escape'){close();return;}if(event.key==='Enter'){choose(current);return;} + const available=entries.filter(e=>!e.disabled).map(e=>e.index);if(!available.length)return; + const at=available.indexOf(current);current=event.key==='Home'?available[0]:event.key==='End'?available.at(-1):available[Math.max(0,Math.min(available.length-1,at+(event.key==='ArrowDown'?1:-1)))];paint(); + }; + for(const entry of entries){const button=document.createElement('button');button.type='button';button.setAttribute('role','option');button.dataset.index=String(entry.index);button.textContent=entry.option.label;button.disabled=entry.disabled;Object.assign(button.style,{display:'block',width:'100%',minHeight:'40px',padding:'9px 12px',border:'0',borderRadius:'6px',textAlign:'left',color:entry.disabled?'#858a94':'inherit',font:'inherit',cursor:entry.disabled?'default':'pointer',whiteSpace:'normal'});button.addEventListener('click',()=>choose(entry.index));box.append(button);} + document.body.append(box);const b=box.getBoundingClientRect();box.style.left=Math.max(8,Math.min(r.left,innerWidth-b.width-8))+'px';box.style.top=Math.max(8,r.bottom+b.height+8<=innerHeight?r.bottom+4:r.top-b.height-4)+'px'; + document.addEventListener('pointerdown',outside,true);document.addEventListener('keydown',keys,true);window.addEventListener('resize',dismiss);window.__canvasLinkSelect={box,close};select.focus({preventScroll:true});paint();return true; +} +module.exports={selectPointerScript:(x,y)=>'('+selectAtPoint.toString()+')('+JSON.stringify(x)+','+JSON.stringify(y)+')',closeSelectScript:'window.__canvasLinkSelect?.close(false)'}; diff --git a/integrations/canvaslink/host/renderer.js b/integrations/canvaslink/host/renderer.js new file mode 100644 index 000000000..e2511519b --- /dev/null +++ b/integrations/canvaslink/host/renderer.js @@ -0,0 +1,167 @@ +const $ = id => document.getElementById(id); +let state, stream, peer, channel, activeId, inputs = 0, generation = 0, firstFrameTimer; +let fixtureText = '', fixtureClicks = 0, fixtureKeys = [], fixturePointer = { x: .5, y: .5 }; +function report(phase, error = '') { window.host.report({ phase, inputs, error }); } +async function refreshState() { + state = await window.host.state(); + if(state.embedded){$('source-section').hidden=true;$('mac-permissions').hidden=true;$('device-tabs').hidden=!state.glassesSupported;} + $('pin').textContent = state.pin ? state.pin.slice(0, 3) + ' ' + state.pin.slice(3) : 'СВЯЗАНО'; + $('addresses').textContent = state.addresses.map(ip => `${ip}:${state.port}`).join('\n') || 'Нет локального IPv4-адреса'; + $('status').textContent = state.connected.length ? `Подключено: ${state.connected[0].name} · ${state.source || 'выберите окно'}` : 'Ожидание планшета'; +} +function closePeer() { + clearTimeout(firstFrameTimer); + if(activeId)window.host.inputSession(activeId,null).catch(()=>{}); + generation++; activeId = null; channel?.close(); peer?.close(); stream?.getTracks().forEach(t => t.stop()); + channel = null; peer = null; stream = null; +} +const iceReady = pc => new Promise((resolve, reject) => { + if (pc.iceGatheringState === 'complete') return resolve(); + const timer = setTimeout(() => { pc.removeEventListener('icegatheringstatechange', listener); reject(Error('Не удалось подготовить локальное соединение')); }, 12000); + function listener() { if (pc.iceGatheringState === 'complete') { clearTimeout(timer); pc.removeEventListener('icegatheringstatechange', listener); resolve(); } } + pc.addEventListener('icegatheringstatechange', listener); +}); +async function start(id) { + closePeer(); activeId = id; + const token=generation; + let captured; + const current=()=>token===generation&&activeId===id; + $('error').textContent = ''; + try { + if (!state.source) return; + if (state.embedded) { + try{if(['jpeg-fallback','snapshot-fallback'].includes(state.captureMode))throw Error('SnapshotCapture');captured=await navigator.mediaDevices.getDisplayMedia({video:{width:{max:2560},height:{max:1600},frameRate:{ideal:30,max:30}},audio:false});} + catch(error){if(!current())return;const canvas=$('fixture');canvas.width=state.width;canvas.height=state.height;captured=canvas.captureStream(30);await window.host.captureFallback(error.name);} + } else if (state.fixture) captured = $('fixture').captureStream(30); + else captured = await navigator.mediaDevices.getDisplayMedia({ video: { frameRate: 30 }, audio: false }); + if (!current()) { captured.getTracks().forEach(t => t.stop()); return; } + stream=captured; + const pc = peer = new RTCPeerConnection({ iceServers: [] }); + const track = stream.getVideoTracks()[0]; track.contentHint = 'detail'; + const sender = pc.addTrack(track, stream); + const codecs = RTCRtpSender.getCapabilities('video')?.codecs || []; + const h264 = codecs.filter(c => c.mimeType.toLowerCase() === 'video/h264'); + const transceiver = pc.getTransceivers()[0]; + if (h264.length) transceiver.setCodecPreferences([...h264, ...codecs.filter(c => !h264.includes(c))]); + const dc = channel = pc.createDataChannel('input', { ordered: true });let receivedVideoFrames; + await window.host.inputSession(id,String(token)); + if(!current())return; + dc.onmessage = message => { + if (typeof message.data !== 'string' || message.data.length > 40000 || !current()) return; + let value; try { value = JSON.parse(message.data); } catch { return; } + if (value.type === 'ping') { if(Number.isSafeInteger(value.videoFrames)&&value.videoFrames>=0)receivedVideoFrames=value.videoFrames;if (dc.readyState === 'open') dc.send(JSON.stringify({ type: 'pong', at: value.at })); return; } + if (value.type === 'stats') { if (state.fixture && dc.readyState === 'open') dc.send(JSON.stringify({ type: 'fixture-state', ...(typeof value.requestId==='string'&&value.requestId.length<=80?{requestId:value.requestId}:{}), clicks: fixtureClicks, text: fixtureText, keys: fixtureKeys })); return; } + if(typeof value.id!=='string')return; + // Main owns validation, deduplication and cancellation of queued events. + window.host.input(id, {...value,epoch:String(token)}).then(result=>{ + if(token!==generation||activeId!==id)return; + if(result.type==='input-ok'){inputs++;report('streaming');} + else{$('error').textContent=result.error;report('input-error',result.error);} + if(dc.readyState==='open')dc.send(JSON.stringify(result)); + }).catch(error=>{if(token===generation&&dc.readyState==='open')dc.send(JSON.stringify({type:'input-error',id:value.id,error:error.message}));}); + }; + dc.onopen = () => { + if(!current())return; $('status').textContent = 'Видео и управление подключены'; report('streaming'); + // Some owned-tab captures produce no initial frame when the window is + // unchanged or occluded. A connected data channel alone is not video. + if(state.embedded)firstFrameTimer=setTimeout(async()=>{ + let fallback; + try{ + if(!current())return; + const stats=await pc.getStats();let sent=0; + stats.forEach(s=>{if(s.type==='outbound-rtp'&&(s.kind==='video'||s.mediaType==='video'))sent+=s.framesEncoded||s.framesSent||0;}); + if((sent&&receivedVideoFrames!==0)||!current())return; + const canvas=$('fixture');canvas.width=state.width;canvas.height=state.height; + fallback=canvas.captureStream(10); + await window.host.captureFallback('NoInitialVideo'); + if(!current()){fallback.getTracks().forEach(t=>t.stop());return;} + const replacement=fallback.getVideoTracks()[0];replacement.contentHint='detail'; + await sender.replaceTrack(replacement); + if(!current()){fallback.getTracks().forEach(t=>t.stop());return;} + const previous=stream;stream=fallback; + previous.getTracks().forEach(t=>{t.onended=null;t.stop();}); + replacement.onended=()=>{if(current()){window.host.signal(id,{type:'disconnect'});closePeer();report('capture-ended');}}; + }catch(error){fallback?.getTracks().forEach(t=>t.stop());if(current()){closePeer();report('capture-error',error.message);}} + },5000); + }; + dc.onclose = () => { if(token===generation&&activeId===id){closePeer();report('disconnected');} }; + pc.onconnectionstatechange = () => { if (['failed', 'disconnected'].includes(pc.connectionState)&&token===generation&&activeId===id) { const phase=pc.connectionState; $('status').textContent = 'Соединение прервано — переподключение с планшета'; closePeer();report(phase); } }; + track.onended = () => { if (current()) { window.host.signal(id, { type: 'disconnect' }); closePeer(); report('capture-ended'); } }; + await pc.setLocalDescription(await pc.createOffer()); + if(!current())return; + await iceReady(pc); + if(!current())return; + await window.host.signal(id, { type: 'offer', sdp: pc.localDescription.sdp }); + if(!current())return; + const params = sender.getParameters(); + if (params.encodings?.length) { params.encodings[0].maxBitrate = 18000000; params.degradationPreference='maintain-resolution'; await sender.setParameters(params); } + if(current())report('offered'); + } catch (error) { if(!current()){captured?.getTracks().forEach(t=>t.stop());return;} $('error').textContent = error.message; report('capture-error', error.message); closePeer();await window.host.signal(id,{type:'disconnect',reason:'host-connection-failed'}).catch(()=>{}); } +} +$('refresh').onclick = async () => { + try { + $('error').textContent = ''; const sources = await window.host.sources(); $('source').replaceChildren(); + for (const s of sources) { const option = document.createElement('option'); option.value = s.id; option.textContent = s.name; $('source').append(option); } + if (!sources.length) throw Error('Окно CanvasTTY не найдено. Проверьте разрешение на запись экрана.'); + await window.host.select(sources[0].id); await refreshState(); + } catch (error) { $('error').textContent = error.message; } +}; +$('source').onchange = async () => { await window.host.select($('source').value); await refreshState(); }; +$('rotate').onclick = async () => { closePeer(); await window.host.rotate(); await refreshState(); report('waiting'); }; +$('screen-settings').onclick=()=>window.host.settings('screen'); +$('input-settings').onclick=()=>window.host.settings('input'); +window.host.permissions().then(p=>{if(p.platform==='darwin'&&!p.embedded){$('mac-permissions').hidden=false;$('permission-status').textContent=`Экран: ${p.screen==='granted'?'доступ есть':'нужен доступ'}. Управление: ${p.inputAccess?'доступ есть':'нужен доступ'}.`;}}).catch(()=>{}); +window.host.onEvent(async event => { + if(event.type==='show-glasses'){$('glasses-tab').click();return;} + if(event.type==='health'){if(channel?.readyState==='open')channel.send(JSON.stringify({type:'source-state',...event.value}));return;} + if(event.type==='frame'){ + if(event.pixels){try{if(state?.embedded&&activeId){const c=$('fixture');if(c.width!==event.width||c.height!==event.height){c.width=event.width;c.height=event.height;}context.putImageData(new ImageData(window.CanvasLinkFrames.rgba(event.pixels,c.width,c.height),c.width,c.height),0,0);}}finally{window.host.frameConsumed(event.sequence);}return;} + return; + } + if (event.type === 'fixture-input') { + const e = event.input; + if (e.type === 'text') fixtureText += e.text; + if (e.type === 'key') { fixtureKeys.push(e.key); if (e.key === 'Backspace') fixtureText = [...fixtureText].slice(0, -1).join(''); } + if (e.type === 'pointer') { fixturePointer = e; if (e.action === 'down') fixtureClicks++; } + return; + } + // Revocation must stop media and input before a fallible asynchronous UI read. + if (event.type === 'revoke') { + if (activeId === event.id) { closePeer(); report('waiting'); } + try { await refreshState(); } catch (error) { $('error').textContent = error.message; } + return; + } + await refreshState(); + if (event.type === 'paired' || event.type === 'source') { + if (state.connected[0] && state.source) await start(state.connected[0].id); + } + if (event.type === 'signal') { + if (event.message.type === 'answer' && peer && activeId === event.id) await peer.setRemoteDescription(event.message); + if (event.message.type === 'ping') await start(event.id); + if (event.message.type === 'disconnect' && activeId === event.id) closePeer(); + } +}); +setInterval(() => { if (state) $('expiry').textContent = state.pin ? `Действует ещё ${Math.max(0, Math.ceil((state.expires - Date.now()) / 1000))} с` : 'Код использован · связь зашифрована'; }, 1000); +const context = $('fixture').getContext('2d'); +setInterval(() => { + if (!state?.fixture) return; + const c = context; c.fillStyle = '#101a14'; c.fillRect(0, 0, 1280, 800); + c.fillStyle = '#d6ef88'; c.font = 'bold 42px monospace'; c.fillText('CanvasLink / live acceptance', 50, 80); + c.fillStyle = '#a8b6ac'; c.font = '24px monospace'; c.fillText('Android ↔ Mac · WebRTC · LAN', 50, 128); + c.fillStyle = '#eaf1e5'; c.font = '28px monospace'; + c.fillText(`Clicks: ${fixtureClicks} Keys: ${fixtureKeys.join(' ').slice(-60)}`, 50, 230); + c.fillText('Text:', 50, 310); + fixtureText.match(/.{1,65}/gu)?.slice(-6).forEach((l, i) => c.fillText(l, 50, 360 + i * 36)); + c.fillStyle = '#d6ef88'; c.beginPath(); c.arc(60 + (Date.now() % 5000) / 5, 700, 14, 0, Math.PI * 2); c.fill(); + c.strokeStyle = '#ed9272'; c.strokeRect(fixturePointer.x * 1280 - 12, fixturePointer.y * 800 - 12, 24, 24); + c.fillStyle = '#798d80'; c.font = '18px monospace'; c.fillText(new Date().toISOString(), 50, 770); +}, 33); +refreshState().then(async () => { if (state.fixture||state.embedded) await $('refresh').onclick(); report('waiting'); }); + +async function glasses(command){ + try{const s=await window.host.glasses(command);$('glasses-code').textContent=s.pairing?.code||'';$('glasses-status').textContent=s.pairing?'Код для очков действует 2 минуты. Подтверждение устройства — в настройках Even G2.':'Используются существующие настройки и разрешения Even G2.';} + catch{$('glasses-status').textContent='Сначала включите Even G2 и выберите доступные сессии в настройках CanvasTTY.';} +} +$('glasses-tab').onclick=()=>{$('phone-panel').hidden=true;$('glasses-panel').hidden=false;glasses('state');}; +$('phone-tab').onclick=()=>{$('phone-panel').hidden=false;$('glasses-panel').hidden=true;}; +$('glasses-begin').onclick=()=>glasses('begin-pairing');$('glasses-cancel').onclick=()=>glasses('cancel-pairing'); diff --git a/integrations/canvaslink/host/security.cjs b/integrations/canvaslink/host/security.cjs new file mode 100644 index 000000000..7ca4f562c --- /dev/null +++ b/integrations/canvaslink/host/security.cjs @@ -0,0 +1,98 @@ +const crypto = require('node:crypto'); +const srp = require('secure-remote-password/server'); +const client = require('secure-remote-password/client'); + +const USER = 'CanvasLink/v1'; +const hex = n => crypto.randomBytes(n).toString('hex'); +function privateAddress(ip) { + ip = String(ip || '').replace(/^::ffff:/, ''); + if (ip === '::1') return true; + if (!/^\d+\.\d+\.\d+\.\d+$/.test(ip)) return false; + const a = ip.split('.').map(Number); + return a.every(v => v >= 0 && v <= 255) && + (a[0] === 127 || a[0] === 10 || (a[0] === 192 && a[1] === 168) || + (a[0] === 172 && a[1] >= 16 && a[1] <= 31) || (a[0] === 169 && a[1] === 254)); +} +function seal(key, id, seq, direction, value) { + const iv = crypto.randomBytes(12); + const cipher = crypto.createCipheriv('aes-256-gcm', key, iv); + cipher.setAAD(Buffer.from(`CanvasLink/1/${id}/${seq}/${direction}`)); + const data = Buffer.concat([cipher.update(JSON.stringify(value)), cipher.final(), cipher.getAuthTag()]); + return { id, seq, iv: iv.toString('hex'), data: data.toString('base64') }; +} +function open(key, id, packet, direction) { + if (!packet || packet.id !== id || !Number.isSafeInteger(packet.seq) || packet.seq < 1 || + !/^[0-9a-f]{24}$/.test(packet.iv) || typeof packet.data !== 'string' || packet.data.length > 131072 || + !/^[A-Za-z0-9+/]+={0,2}$/.test(packet.data)) throw Error('invalid-packet'); + const data = Buffer.from(packet.data, 'base64'); + if (data.length < 17) throw Error('invalid-packet'); + const cipher = crypto.createDecipheriv('aes-256-gcm', key, Buffer.from(packet.iv, 'hex')); + cipher.setAAD(Buffer.from(`CanvasLink/1/${id}/${packet.seq}/${direction}`)); + cipher.setAuthTag(data.subarray(-16)); + return JSON.parse(Buffer.concat([cipher.update(data.subarray(0, -16)), cipher.final()]).toString()); +} +class Pairing { + constructor({ now = Date.now, onPair = () => {}, onRevoke = () => {} } = {}) { + this.now = now; this.onPair = onPair; this.onRevoke = onRevoke; + this.sessions = new Map(); this.rotate(); + } + rotate() { + const revoked = [...this.sessions.keys()]; + this.sessions.clear(); + for (const id of revoked) this.onRevoke(id); + this.pending = new Map(); this.attempts = 0; this.starts = 0; + this.pin = String(crypto.randomInt(0, 1000000)).padStart(6, '0'); + this.expires = this.now() + 120000; + this.salt = client.generateSalt(); + this.verifier = client.deriveVerifier(client.derivePrivateKey(this.salt, USER, this.pin)); + } + expire() { + const now = this.now(); + for (const [id, session] of this.sessions) { + if (session.expires > now) continue; + this.sessions.delete(id); this.onRevoke(id); + } + for (const [id, pending] of this.pending) if (pending.until <= now) this.pending.delete(id); + } + start(body, address) { + if (this.now() >= this.expires || this.attempts >= 5 || this.starts >= 30) throw Error('pairing-closed'); + if (!body || !/^[0-9a-f]{512}$/.test(body.A) || typeof body.name !== 'string' || body.name.length > 80) + throw Error('invalid-pairing'); + if (BigInt('0x' + body.A) === 0n) throw Error('invalid-pairing'); + this.starts++; + const id = hex(16), ephemeral = srp.generateEphemeral(this.verifier); + this.pending.set(id, { ...ephemeral, A: body.A, name: body.name.replace(/[\x00-\x1f]/g, ''), address, until: this.now() + 30000 }); + return { id, salt: this.salt, B: ephemeral.public, user: USER }; + } + finish(body, address) { + const p = this.pending.get(body?.id); + if (p) this.pending.delete(body.id); + if (!p || p.address !== address || this.now() >= p.until || this.now() >= this.expires || this.attempts >= 5) + throw Error('pairing-closed'); + this.attempts++; + if (!/^[0-9a-f]{64}$/.test(body.proof)) throw Error('invalid-proof'); + const result = srp.deriveSession(p.secret, p.A, this.salt, USER, this.verifier, body.proof); + const key = crypto.createHash('sha256').update(Buffer.from(result.key, 'hex')).update('CanvasLink/channel/1').digest(); + const id = hex(16); + this.expires = 0; this.pending.clear(); this.pin = ''; + const session = { id, key, name: p.name, address, incoming: 0, outgoing: 0, expires: this.now() + 12 * 3600000, queue: [] }; + this.sessions.set(id, session); this.onPair(session); + return { id, proof: result.proof }; + } + receive(packet, address, handle) { + this.expire(); + const session = this.sessions.get(packet?.id); + if (!session || session.expires <= this.now() || session.address !== address || packet.seq <= session.incoming) + throw Error('unauthorized'); + const value = open(session.key, session.id, packet, 'request'); + session.incoming = packet.seq; // Authenticated requests are consumed once, including rejected operations. + const result = handle(session, value); + return seal(session.key, session.id, packet.seq, 'response', result); + } + queue(id, message) { + this.expire(); + const session = this.sessions.get(id); + if (session && session.queue.length < 16) session.queue.push(message); + } +} +module.exports = { USER, hex, privateAddress, seal, open, Pairing }; diff --git a/integrations/canvaslink/host/settings-host.cjs b/integrations/canvaslink/host/settings-host.cjs new file mode 100644 index 000000000..2a574855f --- /dev/null +++ b/integrations/canvaslink/host/settings-host.cjs @@ -0,0 +1,11 @@ +// The desktop settings API is available only to the owning CanvasTTY main frame. +function installSettingsHost({ipcMain,getWindow,snapshot,command}){ + const trusted=event=>{const target=getWindow();if(!target||target.isDestroyed()||event.sender!==target.webContents||event.senderFrame!==target.webContents.mainFrame)throw Error('untrusted-settings-renderer');}; + let pending=Promise.resolve(); + ipcMain.handle('canvaslink-settings:state',event=>{trusted(event);return snapshot();}); + ipcMain.handle('canvaslink-settings:command',(event,action)=>{ + trusted(event);if(!['enable','disable','new-code','revoke'].includes(action))throw Error('invalid-settings-command'); + const work=pending.then(()=>{trusted(event);return command(action);});pending=work.catch(()=>{});return work; + }); +} +module.exports={installSettingsHost}; diff --git a/integrations/canvaslink/host/settings.css b/integrations/canvaslink/host/settings.css new file mode 100644 index 000000000..59d6dd33c --- /dev/null +++ b/integrations/canvaslink/host/settings.css @@ -0,0 +1,9 @@ +.canvaslink-settings{max-width:54rem;color:var(--text);line-height:1.5} +.canvaslink-settings__intro{padding:.3rem 0 1.6rem}.canvaslink-settings__eyebrow{font:600 .7rem var(--font-mono,monospace);letter-spacing:.1em;color:var(--primary)} +.canvaslink-settings h3{font-size:1.05em;margin:0 0 .4rem}.canvaslink-settings__intro h3{font-size:1.55em;letter-spacing:-.025em}.canvaslink-settings p{margin:.4rem 0;color:var(--text-muted-on-dark)} +.canvaslink-settings__card{padding:1.4rem;margin-bottom:1rem;border:1px solid color-mix(in srgb,var(--text) 13%,transparent);border-radius:1rem;background:color-mix(in srgb,var(--surface-soft) 65%,var(--surface))} +.canvaslink-settings__row{display:flex;gap:1.2rem;align-items:center;justify-content:space-between}.canvaslink-settings__status{font-size:.85em}.canvaslink-settings__connection{margin-top:1.2rem;border-top:1px solid color-mix(in srgb,var(--text) 10%,transparent);padding-top:.8rem} +.canvaslink-settings ol{padding-left:1.3rem;font-size:.9em;color:var(--text-muted-on-dark)}.canvaslink-settings li+li{margin-top:.45rem}.canvaslink-settings__code-block{display:flex;flex-direction:column;align-items:flex-start;gap:.35rem;margin:1.2rem 0}.canvaslink-settings__code-block>span,.canvaslink-settings small{font-size:.8em;color:var(--text-muted-on-dark)} +.canvaslink-settings__code{font:600 clamp(1.8rem,4vw,2.8rem)/1.3 var(--font-mono,monospace);letter-spacing:.14em;color:var(--text);font-variant-numeric:tabular-nums}.canvaslink-settings__button{min-height:48px;padding:.65rem 1rem;border:1px solid color-mix(in srgb,var(--text) 25%,transparent);border-radius:.65rem;color:var(--text);background:transparent;font:inherit;font-size:.85em;cursor:pointer}.canvaslink-settings__button:hover{background:color-mix(in srgb,var(--text) 8%,transparent)}.canvaslink-settings__button--primary{border-color:var(--primary);background:var(--primary);color:var(--text-dark)}.canvaslink-settings__button--primary:hover{background:var(--primary-hover)}.canvaslink-settings__button:focus-visible{outline:2px solid var(--primary);outline-offset:3px}.canvaslink-settings__button:disabled{opacity:.55;cursor:wait} +.canvaslink-settings__card--quiet p{font-size:.85em}.canvaslink-settings__note{font-size:.9em}.canvaslink-settings__devices h4{margin:.8rem 0 .4rem}.canvaslink-settings__error{color:var(--danger)!important} +@media(max-width:760px){.canvaslink-settings__row{align-items:flex-start;flex-direction:column}.canvaslink-settings__card{padding:1rem}} diff --git a/integrations/canvaslink/host/snapshot.cjs b/integrations/canvaslink/host/snapshot.cjs new file mode 100644 index 000000000..3183c06b8 --- /dev/null +++ b/integrations/canvaslink/host/snapshot.cjs @@ -0,0 +1,20 @@ +// Transfer bounded BGRA pixels inside Electron. Only WebRTC performs lossy +// encoding; do not spend CPU encoding/decoding JPEG or PNG on the same Mac. +function createSnapshot(image,nativeImage,{maxWidth=2560,maxHeight=1600}={}){ + if(image.isEmpty())return null; + const scaleFactor=Math.max(1,...image.getScaleFactors().filter(s=>Number.isFinite(s)&&s>0)); + let pixels=image.toBitmap({scaleFactor}),size=image.getSize(scaleFactor); + if(pixels.length!==size.width*size.height*4){ + size={width:Math.round(size.width*scaleFactor),height:Math.round(size.height*scaleFactor)}; + if(pixels.length!==size.width*size.height*4)throw Error('invalid-snapshot-size'); + } + const factor=Math.min(1,maxWidth/size.width,maxHeight/size.height); + if(factor<1){ + const normalized=nativeImage.createFromBitmap(pixels,{...size,scaleFactor:1}); + size={width:Math.max(1,Math.floor(size.width*factor)),height:Math.max(1,Math.floor(size.height*factor))}; + pixels=normalized.resize({...size,quality:'best'}).toBitmap({scaleFactor:1}); + if(pixels.length!==size.width*size.height*4)throw Error('invalid-resized-snapshot'); + } + return {pixels,...size,scaleFactor,format:'bgra'}; +} +module.exports={createSnapshot}; diff --git a/integrations/canvaslink/host/style.css b/integrations/canvaslink/host/style.css new file mode 100644 index 000000000..ad166cf86 --- /dev/null +++ b/integrations/canvaslink/host/style.css @@ -0,0 +1 @@ +:root{color-scheme:dark;--bg:#121814;--panel:#1d2720;--line:#37463a;--ink:#eaf1e5;--muted:#a0afa2;--accent:#d6ef88}*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--ink);font:15px -apple-system,BlinkMacSystemFont,sans-serif}main{padding:32px;max-width:720px;margin:auto}header{display:flex;gap:14px;align-items:center;margin-bottom:32px}.mark{width:52px;height:52px;background:var(--accent);color:var(--bg);display:grid;place-items:center;font:700 30px Georgia;position:relative}.mark span{font:18px monospace;position:absolute;right:3px;top:2px}h1{font-size:25px;font-weight:550;letter-spacing:-1px;margin:0}header p{font-size:12px;color:var(--muted);margin:5px 0}.tag{margin-left:auto;font:10px monospace;color:var(--muted)}section{border-top:1px solid var(--line);padding:20px 0}.eyebrow{font:10px monospace;letter-spacing:1.6px;color:var(--muted);margin-bottom:16px}.pair{background:var(--panel);padding:22px;border:1px solid var(--line)}#pin{font:56px ui-monospace,Menlo,monospace;letter-spacing:8px;color:var(--accent);margin:16px 0}.pair-foot{display:flex;align-items:center;justify-content:space-between;font-size:12px;color:var(--muted)}button,select{font:inherit;color:var(--ink);background:transparent;border:1px solid var(--line);border-radius:4px;min-height:42px;padding:8px 12px}button{cursor:pointer}button:hover{border-color:var(--accent);color:var(--accent)}button:focus-visible,select:focus-visible{outline:2px solid var(--accent);outline-offset:3px}.row{display:flex;gap:10px}select{min-width:0;flex:1}.hint{color:var(--muted);font-size:12px;line-height:1.6;margin-bottom:0}.mono{font:14px ui-monospace,Menlo,monospace;line-height:1.7}#status{padding:14px;background:var(--panel);border-left:3px solid var(--accent);font-size:13px}#error{color:#ffb4a2;font-size:12px;line-height:1.6;white-space:pre-wrap;margin-top:10px}footer{display:flex;justify-content:space-between;margin-top:24px;font:10px monospace;color:var(--muted)} diff --git a/integrations/canvaslink/package.json b/integrations/canvaslink/package.json new file mode 100644 index 000000000..1663881bc --- /dev/null +++ b/integrations/canvaslink/package.json @@ -0,0 +1,19 @@ +{ + "name": "@canvastty/canvaslink", + "version": "1.0.0", + "private": true, + "type": "commonjs", + "license": "MIT", + "scripts": { + "test": "node --test tests/*.test.cjs", + "build:android": "node scripts/build-android.cjs" + }, + "dependencies": { + "bonjour-service": "1.3.0", + "secure-remote-password": "0.3.1" + }, + "devDependencies": { + "playwright": "1.64.0", + "sharp": "0.35.5" + } +} diff --git a/integrations/canvaslink/scripts/build-android.cjs b/integrations/canvaslink/scripts/build-android.cjs new file mode 100644 index 000000000..0bc5f05ab --- /dev/null +++ b/integrations/canvaslink/scripts/build-android.cjs @@ -0,0 +1,51 @@ +// SDK-only local build. No Gradle downloads, key uploads or device installation. +const fs = require('node:fs'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); +const root = path.resolve(__dirname, '..'); +const dev = process.argv.includes('--dev'); +const { version } = require('../package.json'); +const sdk = process.env.ANDROID_SDK_ROOT || process.env.ANDROID_HOME; +if (!sdk) throw Error('Set ANDROID_SDK_ROOT or ANDROID_HOME to an Android SDK with platform 35 and build-tools 35.0.0.'); +const jdk = process.env.JAVA_HOME; +const javaTool = name => jdk ? path.join(jdk, 'bin', name + (process.platform === 'win32' ? '.exe' : '')) : name; +const tools = path.join(sdk, 'build-tools', '35.0.0'); +const androidTool = name => path.join(tools, name + (process.platform === 'win32' ? (['d8', 'apksigner'].includes(name) ? '.bat' : '.exe') : '')); +const platform = path.join(sdk, 'platforms', 'android-35', 'android.jar'); +if (!fs.existsSync(platform)) throw Error('Missing Android SDK platform 35. See docs/canvaslink.md.'); +const build = path.join(root, 'build', dev ? 'android-dev' : 'android'), dist = path.join(root, 'dist'), runtime = path.join(root, '.runtime'); +fs.rmSync(build, { recursive: true, force: true }); +for (const dir of [build, dist, runtime, path.join(build, 'classes'), path.join(build, 'dex')]) fs.mkdirSync(dir, { recursive: true, mode: 0o700 }); +function run(binary, args) { + const result = spawnSync(binary, args, { stdio: 'inherit', shell: process.platform === 'win32' && binary.endsWith('.bat') }); + if (result.error) throw result.error; + if (result.status !== 0) throw Error(`${path.basename(binary)} exited ${result.status}`); +} +// Device acceptance instrumentation remains source-only and is never shipped. +const files = ['MainActivity', 'TerminalWebView', 'Srp', 'SessionVault'].map(name => path.join(root, 'android', 'src', 'io', 'nexcore', 'canvaslink', name + '.java')); +run(javaTool('javac'), ['--release', '8', '-cp', platform, '-d', path.join(build, 'classes'), ...files]); +run(javaTool('jar'), ['cf', path.join(build, 'classes.jar'), '-C', path.join(build, 'classes'), '.']); +run(androidTool('d8'), ['--release', '--min-api', '26', '--lib', platform, '--output', path.join(build, 'dex'), path.join(build, 'classes.jar')]); +const compiled = path.join(build, 'resources.zip'); +run(androidTool('aapt2'), ['compile', '--dir', path.join(root, 'android', 'res'), '-o', compiled]); +let manifest = path.join(root, 'android', 'AndroidManifest.xml'); +if (dev) { + const contents = fs.readFileSync(manifest, 'utf8') + .replace('package="io.nexcore.canvaslink"', 'package="io.nexcore.canvaslink.dev"') + .replace('android:label="CanvasLink"', 'android:label="CanvasLink DEV"') + .replace('android:name=".MainActivity"', 'android:name="io.nexcore.canvaslink.MainActivity"'); + manifest = path.join(build, 'AndroidManifest.xml'); + fs.writeFileSync(manifest, contents); +} +run(androidTool('aapt2'), ['link', '-o', path.join(build, 'base.apk'), '--manifest', manifest, '-I', platform, '-A', path.join(root, 'android', 'assets'), compiled]); +run(javaTool('jar'), ['uf', path.join(build, 'base.apk'), '-C', path.join(build, 'dex'), 'classes.dex']); +run(androidTool('zipalign'), ['-f', '-p', '4', path.join(build, 'base.apk'), path.join(build, 'aligned.apk')]); +const keystore = path.join(runtime, 'debug.keystore'); +if (!fs.existsSync(keystore)) { + run(javaTool('keytool'), ['-genkeypair', '-keystore', keystore, '-alias', 'canvaslink-debug', '-storepass', 'android', '-keypass', 'android', '-keyalg', 'RSA', '-keysize', '2048', '-validity', '3650', '-dname', 'CN=CanvasLink Local Development']); + fs.chmodSync(keystore, 0o600); +} +const apk = path.join(dist, `CanvasLink-${version}${dev ? '-dev' : ''}.apk`); +run(androidTool('apksigner'), ['sign', '--ks', keystore, '--ks-key-alias', 'canvaslink-debug', '--ks-pass', 'pass:android', '--key-pass', 'pass:android', '--out', apk, path.join(build, 'aligned.apk')]); +run(androidTool('apksigner'), ['verify', '--verbose', apk]); +console.log(`Built ${path.relative(root, apk)} (locally signed; retain the key for future updates)`); diff --git a/integrations/canvaslink/scripts/ui-test-harness.cjs b/integrations/canvaslink/scripts/ui-test-harness.cjs new file mode 100644 index 000000000..cd8c8387b --- /dev/null +++ b/integrations/canvaslink/scripts/ui-test-harness.cjs @@ -0,0 +1,32 @@ +'use strict'; +const fs=require('node:fs');const path=require('node:path'); +const {chromium}=require(process.env.CANVASLINK_PLAYWRIGHT || 'playwright'); +const root=path.resolve(__dirname,'..'); +function fixtureTransport(){ + const state={version:2,connection:'idle',discovery:'ready',computers:[{id:'test-computer',name:'CanvasLink Тестовый компьютер',host:'127.0.0.1:3499'}],computer:'',error:'',pending:0,delivery:null,draft:'',stats:{width:1280,height:800,framesDecoded:2,framesPerSecond:0,rtt:0},source:{capture:'active',windowFocused:true,target:'terminal'}}; + const listeners=new Set(),calls=[];let behavior={saveDraft:true,deferText:false,focus:null};let pendingText;let nativeToken='';window.Native={showKeyboard(token){nativeToken=token;calls.push({type:'show-keyboard'});},terminalKey(key,modifiers){CanvasLinkInput.receive({token:nativeToken,type:'key',key,modifiers:JSON.parse(modifiers)});},paste(){CanvasLinkInput.receive({token:nativeToken,type:'paste',text:behavior.clipboard||''});}}; + const snapshot=()=>JSON.parse(JSON.stringify(state));const emit=patch=>{Object.assign(state,patch);listeners.forEach(fn=>fn(snapshot()));}; + const ensure=()=>{if(state.connection!=='connected')throw Error('not-connected');}; + window.__uiFixture={calls,emit,ime:value=>CanvasLinkInput.receive({...value,token:nativeToken}),configure:patch=>Object.assign(behavior,patch),resolveText(status='processed'){if(!pendingText)throw Error('No pending send');const id='fixture-text';emit({pending:0,delivery:{id,kind:'text',status}});pendingText.resolve({id,status});pendingText=null;},rejectText(message='delivery-uncertain'){emit({pending:0,delivery:{id:'fixture-text',kind:'text',status:'uncertain'}});pendingText?.reject(Error(message));pendingText=null;}}; + const api=window.CanvasLinkClient={version:2,getState:snapshot,subscribe(fn){listeners.add(fn);fn(snapshot());return()=>listeners.delete(fn);},attachVideo(video){ + const canvas=document.createElement('canvas');canvas.width=1280;canvas.height=800;const c=canvas.getContext('2d'); + c.fillStyle='#161d18';c.fillRect(0,0,1280,800);c.fillStyle='#202a22';c.fillRect(0,0,190,800);c.fillStyle='#d6ef88';c.font='bold 23px sans-serif';c.fillText('CanvasTTY',28,54);c.fillStyle='#aab6a5';c.font='18px sans-serif';c.fillText('Тестовая сцена',28,117);c.fillText('Интерфейс',28,162);c.fillText('Жесты',28,206); + c.strokeStyle='#3e4b3f';c.lineWidth=2;c.strokeRect(235,105,990,570);c.fillStyle='#edf1e6';c.font='32px sans-serif';c.fillText('Рабочее пространство',273,165);c.fillStyle='#aab6a5';c.font='22px sans-serif';c.fillText('Собственная сцена для проверки интерфейса',273,220);c.font='18px sans-serif';c.fillText('Нет подключения к компьютеру. Нет реального терминала.',273,264); + c.fillStyle='#28352a';for(let i=0;i<3;i++)c.fillRect(273+i*300,320,270,210);c.fillStyle='#d6ef88';c.font='20px sans-serif';c.fillText('Касание',297,365);c.fillText('Масштаб',597,365);c.fillText('Ввод',897,365);c.fillStyle='#879682';c.font='15px sans-serif';c.fillText('UI FIXTURE · НЕ СНИМОК ПОДКЛЮЧЁННОГО КОМПЬЮТЕРА',273,720); + video.srcObject=canvas.captureStream(2);video.play().catch(()=>{}); + },discover(){calls.push({type:'discover'});emit({discovery:'searching'});},connect(options){calls.push({type:'connect',computerId:options.computerId,manual:Boolean(options.host),codeLength:options.code?.length});if(!/^\d{6}$/.test(options.code||'')){emit({error:'invalid-code'});return false;}emit({connection:'pairing',error:''});return true;},setDraft(text){calls.push({type:'draft',text});state.draft=String(text).slice(0,8000);return behavior.saveDraft;},sendText(text){calls.push({type:'text',text});try{ensure();}catch(error){return Promise.reject(error);}emit({pending:1,delivery:{id:'fixture-text',kind:'text',status:'pending'}});if(behavior.deferText)return new Promise((resolve,reject)=>{pendingText={resolve,reject};});emit({pending:0,delivery:{id:'fixture-text',kind:'text',status:'processed'}});return Promise.resolve({id:'fixture-text',status:'processed'});},sendKey(key,modifiers=[]){calls.push({type:'key',key,modifiers});try{ensure();return Promise.resolve({id:'fixture-key',status:'processed'});}catch(error){return Promise.reject(error);}},sendInput(input){calls.push({type:'input',input});if(input.type==='text')return api.sendText(input.text);try{ensure();return Promise.resolve({id:'fixture-input',status:'processed',...(input.action==='up'&&behavior.focus?{focus:behavior.focus}:{})});}catch(error){return Promise.reject(error);}},hideKeyboard(){calls.push({type:'hide-keyboard'});},resume(){calls.push({type:'resume'});},disconnect(){calls.push({type:'disconnect'});emit({connection:'idle'});}}; +} +async function openUI({width=1024,height=768,touch=false,connected=false}={}){ + const browser=await chromium.launch({headless:true});const context=await browser.newContext({viewport:{width,height},isMobile:touch,hasTouch:touch}); + const errors=[];const page=await context.newPage();page.setDefaultTimeout(7000);page.on('pageerror',e=>errors.push(e.message));page.on('console',event=>{if(event.type()==='error')errors.push(event.text());}); + await page.route('https://appassets.androidplatform.net/client/**',route=>{ + const file=new URL(route.request().url()).pathname.split('/').pop(); + if(file==='transport.js')return route.fulfill({contentType:'application/javascript',body:'('+fixtureTransport.toString()+')();'}); + if(!['index.html','style.css','client.js','gestures.js','delivery.js','direct-input.js','onest.ttf'].includes(file))return route.abort(); + return route.fulfill({contentType:file.endsWith('.js')?'application/javascript':file.endsWith('.css')?'text/css':file.endsWith('.ttf')?'font/ttf':'text/html',body:fs.readFileSync(path.join(root,'android/assets',file))}); + }); + await page.goto('https://appassets.androidplatform.net/client/index.html'); + if(connected)await page.evaluate(()=>__uiFixture.emit({connection:'connected'})); + return {browser,context,page,errors}; +} +module.exports={openUI,root}; diff --git a/integrations/canvaslink/tests/control-delivery.test.cjs b/integrations/canvaslink/tests/control-delivery.test.cjs new file mode 100644 index 000000000..42a8e4dc9 --- /dev/null +++ b/integrations/canvaslink/tests/control-delivery.test.cjs @@ -0,0 +1,107 @@ +const {test}=require('node:test'); +const assert=require('node:assert/strict'); +const {InputQueue}=require('../host/input-queue.cjs'); +const {Delivery}=require('../android/assets/delivery.js'); +const input=(seq,extra={})=>({id:`input-${seq}`,seq,type:'key',key:'Enter',modifiers:[],...extra}); +const deferred=()=>{let resolve;return {promise:new Promise(r=>resolve=r),release:()=>resolve()};}; + +test('an in-flight duplicate with a different payload cannot execute twice',async()=>{ + const gate=deferred(),applied=[],replies=[]; + const q=new InputQueue({active:()=>true,apply:async e=>{applied.push(e.text);await gate.promise;},reply:e=>replies.push(e)}); + q.receive(input(1,{type:'text',text:'original'})); + q.receive(input(2,{id:'input-1',type:'text',text:'changed'})); + gate.release();await q.tail; + q.receive(input(3,{id:'input-1',type:'text',text:'third'})); + assert.deepEqual(applied,['original']);assert.equal(replies.length,2); + assert(replies.every(e=>e.type==='input-ok'&&e.id==='input-1')); +}); + +test('queue saturation rejects explicitly and accepts fresh input after draining',async()=>{ + const gate=deferred(),applied=[],replies=[]; + const q=new InputQueue({limit:2,active:()=>true,apply:async e=>{applied.push(e.id);await gate.promise;},reply:e=>replies.push(e)}); + q.receive(input(1));q.receive(input(2));q.receive(input(3)); + assert.deepEqual(replies,[{type:'input-error',id:'input-3',error:'input-busy'}]); + gate.release();await q.tail;q.receive(input(4));await q.tail; + assert.deepEqual(applied,['input-1','input-2','input-4']);assert.equal(q.pending,0); +}); + +test('invalid payload and unsafe sequence cannot consume a future valid sequence',async()=>{ + const applied=[],replies=[]; + const q=new InputQueue({active:()=>true,apply:async e=>applied.push(e.seq),reply:e=>replies.push(e)}); + q.receive(input(9,{type:'text',text:''}));q.receive(input(Number.MAX_SAFE_INTEGER+1)); + q.receive(input(1));q.receive(input(3));q.receive(input(2));await q.tail; + assert.deepEqual(applied,[1,3]); + assert.deepEqual(replies.filter(e=>e.type==='input-error').map(e=>e.error),['invalid-text','input-expired','input-expired']); +}); + +test('a failed operation does not poison the ordered queue or replay on duplicate',async()=>{ + const applied=[],replies=[]; + const q=new InputQueue({active:()=>true,apply:async e=>{applied.push(e.seq);if(e.seq===1)throw Error('text-target-required');},reply:e=>replies.push(e)}); + q.receive(input(1));q.receive(input(2));await q.tail;q.receive(input(1)); + assert.deepEqual(applied,[1,2]);assert.equal(replies.at(-1).error,'text-target-required');assert.equal(q.pending,0); +}); + +test('revocation drains queued work without sending stale ACKs into a new peer',async()=>{ + let active=true;const gate=deferred(),applied=[],replies=[]; + const q=new InputQueue({active:()=>active,apply:async e=>{applied.push(e.seq);await gate.promise;},reply:e=>replies.push(e)}); + q.receive(input(1));q.receive(input(2));await Promise.resolve();await Promise.resolve(); + active=false;gate.release();await q.tail; + assert.deepEqual(applied,[1]);assert.deepEqual(replies,[]);assert.equal(q.pending,0); +}); + +test('500 ordered operations and duplicate retries execute once beyond the ID cache',async()=>{ + const applied=[],replies=[]; + const q=new InputQueue({maxIds:8,active:()=>true,apply:async e=>applied.push(e.seq),reply:e=>replies.push(e)}); + for(let n=1;n<=500;n++){q.receive(input(n));q.receive(input(n));await q.tail;} + for(let n=1;n<=500;n++)q.receive(input(n));await q.tail; + assert.deepEqual(applied,Array.from({length:500},(_,i)=>i+1));assert.equal(q.pending,0);assert(q.ids.size<=8); + assert.equal(replies.filter(e=>e.error==='input-expired').length,492); +}); + +test('send exception is not-sent, clears its timer and never retries',async()=>{ + let sends=0,clears=0; + const d=new Delivery({channel:()=>({readyState:'open',bufferedAmount:0,send(){sends++;throw Error('closed');}}),id:()=> 'send-failed',setTimer:()=>42,clearTimer:t=>{assert.equal(t,42);clears++;}}); + await assert.rejects(d.send({type:'key',key:'Enter',modifiers:[]}),/not-sent/); + assert.equal(sends,1);assert.equal(clears,1);assert.equal(d.pending.size,0);assert.equal(d.sent,0); +}); + +test('64 pending operations enforce backpressure; one ACK frees one slot',async()=>{ + let id=0;const messages=[]; + const d=new Delivery({channel:()=>({readyState:'open',bufferedAmount:0,send:v=>messages.push(JSON.parse(v))}),id:()=>`pending-${++id}`,setTimer:()=>0,clearTimer(){}}); + const operations=Array.from({length:64},()=>d.send({type:'key',key:'Enter',modifiers:[]}).catch(e=>e.message)); + await assert.rejects(d.send({type:'text',text:'must stay local'}),/input-busy/); + d.receive({type:'input-ok',id:'pending-1'}); + const last=d.send({type:'key',key:'Escape',modifiers:[]}).catch(e=>e.message); + assert.equal(messages.length,65);assert.equal(messages.at(-1).seq,65); + d.close();await Promise.all([...operations,last]);assert.equal(d.pending.size,0); +}); + +test('late, duplicate and malformed ACKs cannot turn uncertainty into success',async()=>{ + const changes=[];let expire; + const d=new Delivery({channel:()=>({readyState:'open',bufferedAmount:0,send(){}}),id:()=> 'late',onChange:e=>changes.push(e),setTimer:fn=>{expire=fn;return 1;},clearTimer(){}}); + const operation=d.send({type:'text',text:'draft'});expire();await assert.rejects(operation,/delivery-uncertain/); + for(const ack of [null,{}, {type:'input-ok',id:'late'},{type:'input-error',id:'late',error:'rejected'}])assert.equal(d.receive(ack),false); + assert.deepEqual(changes.map(e=>e.status),['pending','uncertain']);assert.equal(d.acked,0); +}); + +test('host rejection is explicit and does not retry text or Enter',async()=>{ + const sent=[];let id=0; + const d=new Delivery({channel:()=>({readyState:'open',bufferedAmount:0,send:v=>sent.push(JSON.parse(v))}),id:()=>`rejected-${++id}`,setTimer:()=>0,clearTimer(){}}); + for(const payload of [{type:'text',text:'draft'},{type:'key',key:'Enter',modifiers:[]}]){ + const operation=d.send(payload);d.receive({type:'input-error',id:`rejected-${id}`,error:'canvas-focus-required'}); + await assert.rejects(operation,/canvas-focus-required/); + } + assert.equal(sent.length,2);assert.equal(d.pending.size,0);assert.equal(d.acked,0); +}); + +test('Focus metadata belongs to the acknowledged click and remains immutable on duplicate replay',async()=>{ + const replies=[];let call=0; + const q=new InputQueue({active:()=>true,apply:async()=>{call++;return{focus:{editable:true,kind:'terminal',rect:{x:.1,y:.2,width:.5,height:.6}}};},reply:e=>replies.push(e)}); + const click=input(1,{type:'pointer',action:'up',button:0,x:.5,y:.5});q.receive(click);await q.tail;q.receive(click); + assert.equal(call,1);assert.deepEqual(replies[0].focus,{editable:true,kind:'terminal',rect:{x:.1,y:.2,width:.5,height:.6}});assert.deepEqual(replies[1],replies[0]); +}); +test('Delivery returns focus only from its own processed ACK, ignoring unrelated source-state',async()=>{ + const d=new Delivery({channel:()=>({readyState:'open',bufferedAmount:0,send(){}}),id:()=> 'tap-focus',setTimer:()=>0,clearTimer(){}}); + const result=d.send({type:'pointer',action:'up',button:0,x:.5,y:.5});assert.equal(d.receive({type:'source-state',focus:{editable:true}}),false); + d.receive({type:'input-ok',id:'tap-focus',focus:{editable:false,kind:'canvas'}});assert.equal((await result).focus.editable,false); +}); diff --git a/integrations/canvaslink/tests/delivery.test.cjs b/integrations/canvaslink/tests/delivery.test.cjs new file mode 100644 index 000000000..8cc910304 --- /dev/null +++ b/integrations/canvaslink/tests/delivery.test.cjs @@ -0,0 +1,8 @@ +const {test}=require('node:test'),assert=require('node:assert/strict'); +const {Delivery}=require('../android/assets/delivery.js'); +const {InputQueue}=require('../host/input-queue.cjs'); +test('offline and backpressure reject explicitly, without sending text',async()=>{let channel;const d=new Delivery({channel:()=>channel});await assert.rejects(d.send({type:'text',text:'x'}),/not-connected/);channel={readyState:'open',bufferedAmount:70000,send(){throw Error('must not send');}};await assert.rejects(d.send({type:'text',text:'x'}),/input-busy/);assert.equal(d.pending.size,0);}); +test('only matching ACK completes text and close marks uncertain without replay',async()=>{const messages=[],changes=[];let seq=0;const d=new Delivery({channel:()=>({readyState:'open',bufferedAmount:0,send:v=>messages.push(JSON.parse(v))}),onChange:e=>changes.push(e),id:()=>String(++seq)});const sent=d.send({type:'text',text:'Промпт'});assert.equal(d.receive({type:'input-ok',id:'foreign'}),false);assert.equal(d.pending.size,1);d.receive({type:'input-ok',id:'1'});assert.equal((await sent).status,'processed');const next=d.send({type:'key',key:'Enter',modifiers:[]});d.close();await assert.rejects(next,/delivery-uncertain/);assert.equal(messages.length,2);assert.equal(changes.at(-1).status,'uncertain');}); +test('ACK timeout retains uncertainty; no automatic retry',async()=>{let expire;const d=new Delivery({channel:()=>({readyState:'open',bufferedAmount:0,send(){}}),id:()=> 'x',setTimer:fn=>{expire=fn;return 1;},clearTimer(){}});const p=d.send({type:'text',text:'x'});expire();await assert.rejects(p,/delivery-uncertain/);assert.equal(d.sent,1);}); +test('ordered input executes once, rejects expired IDs after bounded cache',async()=>{const applied=[],replies=[];const q=new InputQueue({apply:async v=>applied.push(v.id),active:()=>true,reply:r=>replies.push(r),maxIds:1});const input=n=>({id:String(n),seq:n,type:'key',key:'Enter',modifiers:[]});q.receive(input(1));q.receive(input(1));await q.tail;q.receive(input(2));await q.tail;q.receive(input(3));await q.tail;q.receive(input(1));await q.tail;assert.deepEqual(applied,['1','2','3']);assert.equal(replies.at(-1).error,'input-expired');}); +test('revoking a peer cancels queued text before application',async()=>{let active=true,release;const applied=[];const gate=new Promise(r=>release=r);const q=new InputQueue({active:()=>active,apply:async v=>{applied.push(v.id);await gate;},reply(){}});q.receive({id:'a',seq:1,type:'text',text:'a'});q.receive({id:'b',seq:2,type:'text',text:'b'});await Promise.resolve();await Promise.resolve();active=false;release();await q.tail;assert.deepEqual(applied,['a']);}); diff --git a/integrations/canvaslink/tests/embedded-lifecycle.test.cjs b/integrations/canvaslink/tests/embedded-lifecycle.test.cjs new file mode 100644 index 000000000..3dc8e3f98 --- /dev/null +++ b/integrations/canvaslink/tests/embedded-lifecycle.test.cjs @@ -0,0 +1,110 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const { createRequire } = require('node:module'); +const { EventEmitter } = require('node:events'); +const { Readable } = require('node:stream'); +const crypto = require('node:crypto'); +const srp = require('secure-remote-password/client'); +const { seal, open, USER } = require('../host/security.cjs'); +const file = path.join(__dirname, '../host/embedded.cjs'); +function fixture({ failLoad = false } = {}) { + const counters = { listeners: 0, discovery: 0, panels: 0, close: 0, destroy: 0 }; + const handlers = new Map(), timers = new Set(), app = new EventEmitter(); + app.getPath = () => '/unused-test-runtime'; + const target = new EventEmitter(); target.isDestroyed = () => false; + target.webContents = { mainFrame: {}, executeJavaScript: async () => {}, endFrameSubscription() {} }; + target.getContentSize = () => [1000, 700]; target.getTitle = () => 'Owned fixture'; + let panel, captureHandler, pairing, serve; + const events = []; + class Panel extends EventEmitter { + constructor() { super(); counters.panels++; panel = this; this.webContents = new EventEmitter(); this.webContents.mainFrame = {}; this.webContents.setWindowOpenHandler = () => {}; this.webContents.send = (channel, value) => events.push(value); } + isDestroyed() { return !!this.destroyed; } + destroy() { this.destroyed = true; counters.destroy++; } + hide() {} + async loadFile() { if (failLoad) throw Error('fixture-load-failed'); } + } + const captureSession = { setPermissionCheckHandler(fn) { this.check = fn; }, setPermissionRequestHandler() {}, setDisplayMediaRequestHandler(fn) { captureHandler = fn; } }; + const electron = { app, BrowserWindow: Panel, nativeImage: {}, session: { fromPartition: () => captureSession }, ipcMain: { handle: (key, fn) => handlers.set(key, fn), on: (key, fn) => handlers.set(key, fn) } }; + const localRequire = createRequire(file); + const replacements = { + electron, + 'node:fs': { mkdirSync() {} }, + 'node:http': { createServer(handler) { serve=handler;return { requestTimeout: 0, once() {}, listen(port, host, done) { counters.listeners++; done(); }, address: () => ({ port: 3499 }), close() { counters.close++; } }; } }, + './discovery.cjs': { publish() { counters.discovery++; return { destroy() {} }; } }, + './security.cjs': { ...localRequire('./security.cjs'), Pairing: class extends localRequire('./security.cjs').Pairing { constructor(options) { super(options); pairing = this; } } } + }; + const context = { require: name => Object.hasOwn(replacements, name) ? replacements[name] : localRequire(name), module: { exports: {} }, __dirname: path.dirname(file), process, Buffer, setInterval(fn) { const token = { fn }; timers.add(token); return token; }, clearInterval(token) { timers.delete(token); } }; + vm.runInNewContext(fs.readFileSync(file, 'utf8'), context); + context.module.exports.install({ getWindow: () => target }); + const event = { sender: target.webContents, senderFrame: target.webContents.mainFrame }; + const request=async ({route='/channel',method='POST',address='192.168.2.23',headers={},body={}}={})=>{ + const req=Readable.from([Buffer.from(typeof body==='string'?body:JSON.stringify(body))]); + Object.assign(req,{method,url:route,socket:{remoteAddress:address},headers:{'content-type':'application/json',...headers}}); + const result={};await serve(req,{headersSent:false,writeHead(status,headers){result.status=status;result.headers=headers;},end(body){result.body=JSON.parse(body);}});return result; + }; + return { counters, handlers, event, target, timers, captureSession, events, request, get pairing() { return pairing; }, get panel() { return panel; }, capture: request => { let result; captureHandler(request, value => result = value); return result; }, state: () => handlers.get('canvaslink-settings:state')(event), command: action => handlers.get('canvaslink-settings:command')(event, action) }; +} +test('Installing CanvasLink is inert until an owning settings command enables access', async () => { + const f = fixture(); + assert.equal(f.state().enabled, false); assert.equal(f.state().pin, ''); + assert.deepEqual(f.counters, { listeners: 0, discovery: 0, panels: 0, close: 0, destroy: 0 }); + assert.equal(f.timers.size, 0); + await f.command('enable'); assert.equal(f.state().enabled, true); + assert.equal(f.counters.listeners, 1); assert.equal(f.counters.panels, 1); + await f.command('enable'); assert.equal(f.counters.listeners, 1); + await f.command('disable'); assert.equal(f.state().enabled, false); assert.equal(f.timers.size, 0); assert.equal(f.panel.destroyed, true); +}); +test('Capture grants only the owned CanvasTTY main frame and stops after disable', async () => { + const f = fixture(); await f.command('enable'); + assert.equal(f.capture({ frame: f.panel.webContents.mainFrame }).video, f.target.webContents.mainFrame); + assert.equal(f.capture({ frame: {} }).video, undefined); + assert.equal(f.captureSession.check({}, 'display-capture'), false); + assert.equal(f.captureSession.check(f.panel.webContents, 'display-capture'), true); + assert.equal(f.captureSession.check(f.panel.webContents, 'media'), false); + await f.command('disable'); assert.equal(f.capture({ frame: f.panel.webContents.mainFrame }).video, undefined); +}); +test('A failed capture-worker load closes the listener and allows a clean retry', async () => { + const f = fixture({ failLoad: true }); + await assert.rejects(f.command('enable'), /fixture-load-failed/); + assert.equal(f.state().enabled, false); assert.equal(f.counters.close, 1); assert.equal(f.timers.size, 0); assert.equal(f.panel.destroyed, true); + await assert.rejects(f.command('enable'), /fixture-load-failed/); + assert.equal(f.counters.listeners, 2); assert.equal(f.counters.close, 2); +}); +test('The host revokes an expired video session even when the client sends nothing', async () => { + const f=fixture();await f.command('enable'); + f.pairing.sessions.set('expired',{id:'expired',name:'Silent peer',expires:0,queue:[]}); + for(const timer of f.timers)await timer.fn(); + assert.equal(f.pairing.sessions.has('expired'),false); + assert.equal(f.events.filter(e=>e.type==='revoke'&&e.id==='expired').length,1); + for(const timer of f.timers)await timer.fn(); + assert.equal(f.events.filter(e=>e.type==='revoke').length,1); + await f.command('disable'); +}); +test('The HTTP boundary rejects browser origins, public peers, unsupported routes and oversized bodies',async()=>{ + const f=fixture();await f.command('enable'); + for(const options of [{address:'8.8.8.8'},{headers:{origin:'https://attacker.invalid'}},{headers:{origin:'null'}},{method:'GET'},{route:'/exec'},{body:'{'},{body:' '.repeat(131073)}]){ + const response=await f.request(options);assert.equal(response.status,403,JSON.stringify(options).slice(0,150)); + assert.equal(response.headers['Cache-Control'],'no-store'); + } + assert.equal((await f.request({headers:{'content-type':'text/plain'}})).status,415); + assert.equal(f.events.length,0);assert.equal(f.pairing.sessions.size,0);await f.command('disable'); +}); +test('Only mutually authenticated, fresh and address-bound HTTP signals reach the renderer',async()=>{ + const f=fixture();await f.command('enable'); + const a=srp.generateEphemeral(),challenge=(await f.request({route:'/pair/start',body:{A:a.public,name:'Synthetic security test'}})).body; + const session=srp.deriveSession(a.secret,challenge.B,challenge.salt,USER,srp.derivePrivateKey(challenge.salt,USER,f.pairing.pin)); + const finish=await f.request({route:'/pair/finish',body:{id:challenge.id,proof:session.proof}});assert.equal(finish.status,200);srp.verifySession(a.public,session,finish.body.proof); + const id=finish.body.id,key=crypto.createHash('sha256').update(Buffer.from(session.key,'hex')).update('CanvasLink/channel/1').digest(); + const signal=seq=>seal(key,id,seq,'request',{type:'signal',message:{type:'ping'}}); + const first=signal(1);assert.equal((await f.request({body:first,address:'192.168.2.24'})).status,403); + const response=await f.request({body:first});assert.equal(response.status,200);assert.deepEqual(open(key,id,response.body,'response'),{ok:true}); + assert.equal((await f.request({body:first})).status,403); + assert.equal((await f.request({body:{...signal(2),data:Buffer.alloc(32).toString('base64')}})).status,403); + assert.equal((await f.request({body:seal(key,id,2,'request',{type:'signal',message:{type:'exec',command:'must-not-run'}})})).status,403); + assert.equal(f.events.filter(e=>e.type==='signal').length,1); + await f.command('revoke');assert.equal((await f.request({body:signal(3)})).status,403); + assert.equal(f.events.filter(e=>e.type==='signal').length,1);await f.command('disable'); +}); diff --git a/integrations/canvaslink/tests/frontend.test.cjs b/integrations/canvaslink/tests/frontend.test.cjs new file mode 100644 index 000000000..7ea097a73 --- /dev/null +++ b/integrations/canvaslink/tests/frontend.test.cjs @@ -0,0 +1,55 @@ +const {test}=require('node:test');const assert=require('node:assert/strict');const {openUI}=require('../scripts/ui-test-harness.cjs'); +const focus={editable:true,kind:'terminal',rect:{x:.2,y:.2,width:.6,height:.6}}; +async function typing(t,options={}){const ui=await openUI({connected:true,touch:true,...options});t.after(async()=>{await ui.browser.close();assert.deepEqual(ui.errors,[]);});await ui.page.evaluate(focus=>__uiFixture.configure({focus}),options.focus||focus);await ui.page.locator('#viewport').click();await ui.page.waitForFunction(()=>__uiFixture.calls.some(c=>c.type==='show-keyboard'));return ui;} +const commands=page=>page.evaluate(()=>__uiFixture.calls.filter(c=>c.type==='input'&&['key','text'].includes(c.input.type)).map(c=>c.input)); +test('Tap opens direct native input only after processed pointer-up confirms editable target',async t=>{const {page}=await typing(t);assert.equal(await page.locator('#compose,#text,#send,.session-badge').count(),0);assert.equal(await page.locator('#keyboard-toggle').getAttribute('aria-pressed'),'true');assert.equal(await page.locator('#ctrl').evaluate(e=>getComputedStyle(e).userSelect),'none');await page.evaluate(()=>__uiFixture.ime({type:'text',text:'Привет 🙂'}));assert.deepEqual(await commands(page),[{type:'text',text:'Привет 🙂'}]);}); +test('A canvas tap never opens a keyboard from stale polled source.target',async t=>{const ui=await openUI({connected:true});t.after(()=>ui.browser.close());await ui.page.locator('#viewport').click();assert.equal(await ui.page.evaluate(()=>__uiFixture.calls.filter(c=>c.type==='show-keyboard').length),0);}); +test('Composition is displayed locally; one commit is sent and Enter follows it directly',async t=>{const {page}=await typing(t);await page.evaluate(()=>{__uiFixture.ime({type:'composition',text:'日本'});});assert.equal(await page.locator('#preedit').textContent(),'日本');assert.deepEqual(await commands(page),[]);await page.evaluate(()=>{__uiFixture.ime({type:'text',text:'日本語'});__uiFixture.ime({type:'composition',text:''});__uiFixture.ime({type:'key',key:'Enter',modifiers:[]});});assert.deepEqual(await commands(page),[{type:'text',text:'日本語'},{type:'key',key:'Enter',modifiers:[]}]);}); +test('Native delete, arrows and Enter preserve order without a second editor',async t=>{const {page}=await typing(t);await page.evaluate(()=>{__uiFixture.ime({type:'text',text:'ab'});for(const key of ['Backspace','ArrowLeft','Enter'])__uiFixture.ime({type:'key',key,modifiers:[]});});assert.deepEqual((await commands(page)).map(v=>v.key||v.text),['ab','Backspace','ArrowLeft','Enter']);}); +test('Uncertain delivery stops IME and reconnect never replays committed input or Enter',async t=>{const {page}=await typing(t);await page.evaluate(()=>{__uiFixture.configure({deferText:true});__uiFixture.ime({type:'text',text:'uncertain'});__uiFixture.rejectText();});await page.waitForFunction(()=>!CanvasLinkInput.active());await page.evaluate(()=>{__uiFixture.emit({connection:'reconnecting'});__uiFixture.emit({connection:'connected'});__uiFixture.ime({type:'key',key:'Enter',modifiers:[]});});assert.deepEqual(await commands(page),[{type:'text',text:'uncertain'}]);assert.match(await page.locator('#input-status').textContent(),/подтвержд|Проверьте/);}); +test('Ctrl is one-shot and Paste sends Android clipboard as text with no implicit Enter',async t=>{const {page}=await typing(t);await page.locator('#ctrl').click();await page.evaluate(()=>__uiFixture.ime({type:'text',text:'c'}));assert.equal(await page.locator('#ctrl').getAttribute('aria-pressed'),'false');await page.evaluate(()=>__uiFixture.configure({clipboard:'текст 🙂'}));await page.locator('#paste').click();assert.deepEqual(await commands(page),[{type:'key',key:'c',modifiers:['control']},{type:'text',text:'текст 🙂'}]);}); +test('A late native callback from the old keyboard token is ignored after a new target tap',async t=>{const {page}=await typing(t);await page.evaluate(()=>{const f=Native.showKeyboard;Native.showKeyboard=t=>{window.__lastToken=t;f(t);};CanvasLinkInput.open({editable:true,kind:'terminal'});window.__oldToken=__lastToken;CanvasLinkInput.open({editable:true,kind:'terminal'});CanvasLinkInput.receive({token:__oldToken,type:'text',text:'old'});});assert.deepEqual(await commands(page),[]);}); +test('One computer is automatic; IP is secondary; pairing hint names actual native Settings',async t=>{const {browser,page}=await openUI();t.after(()=>browser.close());assert.equal(await page.locator('#computers button').getAttribute('aria-pressed'),'true');assert.equal(await page.locator('#manual').evaluate(e=>e.open),false);assert.match(await page.locator('#pin-hint').textContent(),/Настройки.*CanvasLink/);await page.locator('#pin').fill('1'.repeat(6));await page.locator('#connect').click();assert.equal(await page.locator('#pin').inputValue(),'');}); +test('Only a single 56px bind row reserves workspace space across keyboard sizes',async t=>{for(const [width,height] of [[390,844],[844,390],[844,240],[1280,800],[768,1024],[320,320]]){const {browser,page}=await openUI({connected:true,width,height});try{const g=await page.evaluate(()=>{const r=document.getElementById('viewport').getBoundingClientRect(),b=document.getElementById('controls').getBoundingClientRect();const rows=[...document.getElementById('controls').querySelectorAll('button')].map(e=>e.getBoundingClientRect().top);return{r:{width:r.width,height:r.height},b:{top:b.top,height:b.height},rows,overflow:document.documentElement.scrollWidth>innerWidth};});assert.equal(g.r.width,width);assert.equal(g.r.height,height-56);assert.equal(g.b.height,56);assert.equal(new Set(g.rows).size,1);assert.equal(g.overflow,false);}finally{await browser.close();}}}); +test('Selected terminal keeps usable width and focus when IME shrinks the viewport',async t=>{const {page}=await typing(t,{width:1024,height:768});await page.setViewportSize({width:1024,height:360});await page.waitForTimeout(50);const g=await page.evaluate(()=>{const r=document.getElementById('video').getBoundingClientRect(),v=document.getElementById('viewport').getBoundingClientRect(),k=Math.min(r.width/1280,r.height/800);return {terminalWidth:1280*k*.6,viewportWidth:v.width,viewportHeight:v.height};});assert.ok(g.terminalWidth>=g.viewportWidth*.9);assert.equal(g.viewportHeight,304);}); +test('Diagnostics is a sheet off the working canvas, RTT explicitly ms',async t=>{const {page}=await typing(t);assert.equal(await page.locator('#metrics').isVisible(),false);await page.evaluate(()=>__uiFixture.emit({stats:{rtt:17.4,framesDecoded:20}}));await page.locator('#menu-toggle').click();await page.locator('#diagnostics-toggle').click();assert.match(await page.locator('#metrics').textContent(),/RTT канала\s+17 мс/);}); + +test('An empty terminal cursor and a moving bottom cursor stay visible with the IME',async t=>{ + const selected={...focus,caret:{x:.25,y:.22,width:.012,height:.025}}; + const {page}=await typing(t,{width:1024,height:768,focus:selected}); + const visible=()=>page.evaluate(()=>{const c=CanvasLinkClient.getState().source?.caret||window.__caret;const r=document.getElementById('video').getBoundingClientRect(),v=document.getElementById('viewport').getBoundingClientRect(),k=Math.min(r.width/1280,r.height/800),y=r.top+(r.height-800*k)/2+(c.y+c.height/2)*800*k;return y>v.top+8&&ywindow.__caret=c,selected.caret); + for(const [width,height] of [[1024,360],[390,310],[1024,768]]){await page.setViewportSize({width,height});await page.waitForTimeout(70);assert.ok(await visible(),'top cursor must stay visible after IME or rotation');} + await page.evaluate(focus=>__uiFixture.emit({source:{target:'terminal',rect:focus.rect,caret:{x:.65,y:.77,width:.012,height:.025}}}),selected); + await page.waitForTimeout(30);assert.ok(await visible(),'health must follow the active selected cursor'); +}); +test('Cursor following never overrides a manual view or opens a new keyboard',async t=>{ + const {page}=await typing(t);await page.locator('#menu-toggle').click();await page.locator('#reset-view').click(); + const before=await page.locator('#video').evaluate(e=>getComputedStyle(e).transform); + await page.evaluate(focus=>__uiFixture.emit({source:{target:'terminal',rect:focus.rect,caret:{x:.2,y:.21,width:.01,height:.02}}}),focus); + assert.equal(await page.locator('#video').evaluate(e=>getComputedStyle(e).transform),before);assert.equal(await page.evaluate(()=>CanvasLinkInput.active()),false); +}); + +test('Explicit paste is literal even when one-shot Ctrl was latched',async t=>{const {page}=await typing(t);await page.locator('#ctrl').click();await page.evaluate(()=>__uiFixture.configure({clipboard:'a'}));await page.locator('#paste').click();assert.deepEqual(await commands(page),[{type:'text',text:'a'}]);assert.equal(await page.locator('#ctrl').getAttribute('aria-pressed'),'false');}); +test('One-shot Ctrl cannot leak through a Unicode commit or native key into the next letter',async t=>{ + const {page}=await typing(t); + for(const text of ['Ж','🙂','word']){ + await page.locator('#ctrl').click();await page.evaluate(text=>{__uiFixture.ime({type:'text',text});__uiFixture.ime({type:'text',text:'c'});},text); + assert.equal(await page.locator('#ctrl').getAttribute('aria-pressed'),'false'); + } + await page.locator('#ctrl').click();await page.evaluate(()=>{__uiFixture.ime({type:'key',key:'ArrowLeft',modifiers:[]});__uiFixture.ime({type:'text',text:'c'});}); + assert.deepEqual(await commands(page),['Ж','c','🙂','c','word','c'].map(text=>({type:'text',text})).concat([{type:'key',key:'ArrowLeft',modifiers:['control']},{type:'text',text:'c'}])); +}); + +test('Initial terminal framing is visual only and overview stays under user control',async t=>{ + const ui=await openUI({connected:true,width:1024,height:768,touch:true});t.after(()=>ui.browser.close()); + const source={target:'canvas',surface:'canvas',terminalView:{rect:{x:.3,y:.2,width:.5,height:.5},viewport:{width:1440,height:900}}}; + await ui.page.evaluate(source=>__uiFixture.emit({source}),source); + assert.equal(await ui.page.evaluate(()=>CanvasLinkInput.active()),false); + assert.equal(await ui.page.locator('#view-toggle-label').textContent(),'Весь Canvas'); + assert.notEqual(await ui.page.locator('#video').evaluate(e=>getComputedStyle(e).transform),'matrix(1, 0, 0, 1, 0, 0)'); + await ui.page.locator('#view-toggle').click();await ui.page.evaluate(source=>__uiFixture.emit({source}),source); + assert.equal(await ui.page.locator('#video').evaluate(e=>getComputedStyle(e).transform),'matrix(1, 0, 0, 1, 0, 0)'); + await ui.page.locator('#view-toggle').click();assert.equal(await ui.page.evaluate(()=>CanvasLinkInput.active()),false); + assert.equal(await ui.page.evaluate(()=>__uiFixture.calls.filter(c=>c.type==='show-keyboard').length),0); +}); diff --git a/integrations/canvaslink/tests/gestures-ui.test.cjs b/integrations/canvaslink/tests/gestures-ui.test.cjs new file mode 100644 index 000000000..9896caeec --- /dev/null +++ b/integrations/canvaslink/tests/gestures-ui.test.cjs @@ -0,0 +1,322 @@ +'use strict'; +const {test}=require('node:test'); +const assert=require('node:assert/strict'); +const {openUI}=require('../scripts/ui-test-harness.cjs'); + +async function fixture(t,options={}) { + const ui=await openUI({width:1024,height:900,touch:true,connected:true,...options}); + t.after(async()=>{const unhandled=await ui.page.evaluate(()=>window.__unhandled||[]);await ui.browser.close();assert.deepEqual(ui.errors,[],'browser page/console errors');assert.deepEqual(unhandled,[],'unhandled API promises');}); + await ui.page.waitForFunction(()=>document.querySelector('#video').videoWidth===1280); + await ui.page.evaluate(()=>{ + window.__gestureEvents=[];window.__viewEvents=[];window.__unhandled=[]; + window.addEventListener('canvaslink:pointer',e=>__gestureEvents.push(e.detail)); + window.addEventListener('canvaslink:view',e=>__viewEvents.push(e.detail)); + window.addEventListener('unhandledrejection',e=>__unhandled.push(String(e.reason))); + }); + return ui; +} +test('fit/mode/reset preserve instrumentation, labels, icons and publish view state',async t=>{ + const {page}=await fixture(t); + await page.evaluate(()=>{ + for(const [button,id,label]of [['fit','zoom-label','По размеру'],['mode','mode-label','Касание']]){ + if(!document.getElementById(id))document.getElementById(button).innerHTML=''+label+''; + } + if(!document.getElementById('reset-view')){const b=document.createElement('button');b.id='reset-view';b.textContent='По размеру';document.querySelector('#controls').append(b);} + if(!document.getElementById('gesture-hint')){const p=document.createElement('p');p.id='gesture-hint';document.querySelector('#controls').append(p);} + }); + await clear(page);await menu(page,'fit'); + const fit=await page.evaluate(()=>({matrix:getComputedStyle(document.getElementById('video')).transform,label:document.getElementById('zoom-label').textContent,view:__viewEvents.at(-1)})); + assert.match(fit.matrix,/matrix\(2, 0, 0, 2,/);assert.match(fit.label,/200|2/); + assert.deepEqual(fit.view,{scale:2,offset:{x:0,y:0},trackpad:false}); + await menu(page,'mode'); + assert.equal(await page.locator('#mode').getAttribute('aria-pressed'),'true'); + assert.equal(await page.locator('#mode-label').textContent(),'Тачпад'); + assert.equal(await page.locator('#mode-label').count(),1,'mode label is preserved'); + assert.match(await page.locator('#gesture-hint').textContent(),/дв[ау]|2|пальц/i); + await menu(page,'reset-view'); + assert.deepEqual(await page.evaluate(()=>__viewEvents.at(-1)),{scale:1,offset:{x:0,y:0},trackpad:true}); + await menu(page,'mode');assert.equal(await page.locator('#mode-label').textContent(),'Касание'); +}); + +test('direct drag releases exactly once on viewport capture loss but ignores bubbled video loss',async t=>{ + const {page}=await fixture(t);const a=await area(page);await clear(page); + await page.mouse.move(a.x-40,a.y);await page.mouse.down();await page.mouse.move(a.x+40,a.y); + await page.evaluate(()=>document.querySelector('#video').dispatchEvent(new PointerEvent('lostpointercapture',{pointerId:1,bubbles:true}))); + assert.equal((await inputs(page)).filter(c=>c.input?.action==='up').length,0,'child capture transfer is not cancellation'); + await page.evaluate(()=>document.querySelector('#viewport').releasePointerCapture(1)); + await page.mouse.move(a.x+45,a.y); + assert.equal((await inputs(page)).filter(c=>c.input?.action==='up').length,1,'capture loss releases before physical pointerup'); + await page.mouse.up(); + const values=(await inputs(page)).map(c=>c.input); + assert.equal(values.filter(c=>c.action==='down').length,1);assert.equal(values.filter(c=>c.action==='up').length,1,'capture loss releases remote down'); + assert.equal(values.at(-1).action,'up','nothing follows the release'); + assert.deepEqual(await page.evaluate(()=>__gestureEvents.map(e=>({x:e.x,y:e.y,action:e.action,trackpad:e.trackpad}))),values.map(e=>({x:e.x,y:e.y,action:e.action,trackpad:false}))); +}); + +test('blur ends a drag and a connection interruption cannot replay its tail',async t=>{ + const {page}=await fixture(t);const a=await area(page);await clear(page); + await page.mouse.move(a.x-40,a.y);await page.mouse.down();await page.mouse.move(a.x+40,a.y); + await page.evaluate(()=>window.dispatchEvent(new Event('blur'))); + assert.deepEqual((await inputs(page)).filter(c=>c.input?.action!=='move').map(c=>c.input.action),['down','up']); + await page.mouse.up();await clear(page); + await page.mouse.move(a.x-40,a.y);await page.mouse.down();await page.mouse.move(a.x+40,a.y); + await page.evaluate(()=>__uiFixture.emit({connection:'reconnecting'}));await clear(page); + await page.evaluate(()=>__uiFixture.emit({connection:'connected'})); + await page.mouse.move(a.x+60,a.y);await page.mouse.up(); + assert.deepEqual(await inputs(page),[],'interrupted gesture is discarded, not resumed'); +}); + +test('editing suppresses pointer/wheel input and workspace touch restores safe keyboard ownership',async t=>{ + const {page}=await fixture(t);const a=await area(page); + await page.evaluate(()=>{const e=document.createElement('input');e.id='local-test-editor';document.body.append(e);e.focus();});await clear(page); + await page.touchscreen.tap(a.x,a.y);await page.mouse.move(a.x,a.y);await page.mouse.wheel(1,20); + assert.deepEqual(await inputs(page),[],'no remote gestures while editing a prompt'); + await page.evaluate(()=>document.getElementById('local-test-editor').remove());await page.touchscreen.tap(a.x,a.y);await clear(page); + assert.equal(await page.evaluate(()=>document.activeElement.id),'viewport','workspace takes explicit keyboard focus'); + await page.keyboard.down('Enter');await page.keyboard.down('Enter');await page.keyboard.up('Enter'); + const keys=await inputs(page);assert.deepEqual(keys,[{type:'key',key:'Enter',modifiers:[]}],'hardware autorepeat never repeats Enter'); +}); + +test('trackpad two-finger pinch zooms locally without scrolling or a ghost tap',async t=>{ + const {page,context}=await fixture(t);const a=await area(page);await menu(page,'mode');await clear(page); + const touch=await touchSession(context,page); + await touch('touchStart',[[0,a.x-50,a.y],[1,a.x+50,a.y]]); + await touch('touchMove',[[0,a.x-90,a.y],[1,a.x+90,a.y]]); + await page.evaluate(()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)))); + await touch('touchEnd',[[1,a.x+90,a.y]]);await touch('touchEnd',[]); + assert.deepEqual(await inputs(page),[],'pinch is local-only and never clicks'); + const view=await page.evaluate(()=>__viewEvents.at(-1));assert.ok(view&&view.scale>1.6&&view.scale<2,'trackpad pinch zooms'); + await page.touchscreen.tap(a.x,a.y); + assert.deepEqual((await inputs(page)).map(c=>c.input.action),['down','up'],'next independent tap still works'); +}); + +test('direct pinch keeps the off-centre content anchor and clamps local pan bounds',async t=>{ + const {page,context}=await fixture(t);const a=await area(page);const x=a.x+80,y=a.y; + const touch=await touchSession(context,page);await clear(page); + await page.touchscreen.tap(x,y);const before=(await inputs(page))[0].input;await clear(page); + await touch('touchStart',[[0,x-50,y],[1,x+50,y]]); + await touch('touchMove',[[0,x-100,y],[1,x+100,y]]); + await page.evaluate(()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)))); + await touch('touchEnd',[]); + const view=await page.evaluate(()=>__viewEvents.at(-1)); + assert.ok(Math.abs(view.scale-2)<.02);assert.ok(Math.abs(view.offset.x+80)<2,'anchor compensation'); + assert.deepEqual(await inputs(page),[],'direct pinch is local-only'); + await page.touchscreen.tap(x,y);const after=(await inputs(page))[0].input; + assert.ok(Math.abs(before.x-after.x)<.003&&Math.abs(before.y-after.y)<.003,'same content remains under the pinch centre'); + await menu(page,'fit');await clear(page); + await touch('touchStart',[[0,a.x-50,y],[1,a.x+50,y]]); + await touch('touchMove',[[0,a.x-20,y+30],[1,a.x+80,y+30]]); + await touch('touchEnd',[]); + assert.deepEqual(await page.evaluate(()=>__viewEvents.at(-1)?.offset),{x:0,y:0},'fit cannot be panned outside the viewport'); +}); + +test('trackpad motion returning to its origin is not mistaken for a tap',async t=>{ + const {page,context}=await fixture(t);const a=await area(page);await menu(page,'mode');await clear(page); + const touch=await touchSession(context,page); + await touch('touchStart',[[0,a.x,a.y]]);await touch('touchMove',[[0,a.x+70,a.y]]);await touch('touchMove',[[0,a.x,a.y]]);await touch('touchEnd',[]); + const values=(await inputs(page)).map(c=>c.input); + assert.ok(values.length>0);assert.ok(values.every(e=>e.action==='move'),'movement never becomes a click on release'); +}); + +test('trackpad long-press drag releases once on touchcancel and cannot fire after a second finger',async t=>{ + const {page,context}=await fixture(t);const a=await area(page);await menu(page,'mode');await clear(page);const touch=await touchSession(context,page); + await touch('touchStart',[[0,a.x,a.y]]); + await page.waitForFunction(()=>__uiFixture.calls.some(c=>c.input?.action==='down'),{},{timeout:1500}); + await touch('touchMove',[[0,a.x+40,a.y+20]]);await touch('touchCancel',[]); + assert.deepEqual((await inputs(page)).filter(c=>c.input.action!=='move').map(c=>c.input.action),['down','up']); + assert.ok((await inputs(page)).some(c=>c.input.action==='move'&&c.input.drag===true),'held cursor moves as a drag'); + await clear(page);await touch('touchStart',[[0,a.x-30,a.y]]); + await touch('touchStart',[[0,a.x-30,a.y],[1,a.x+30,a.y]]);await touch('touchEnd',[]); + await page.waitForTimeout(550);assert.deepEqual(await inputs(page),[],'no delayed long-press after multitouch'); +}); + +test('editing and mode switches safely release an existing down before discarding its tail',async t=>{ + const {page}=await fixture(t);let a=await area(page);await page.evaluate(()=>{const e=document.createElement('input');e.id='local-test-editor';document.body.append(e);});await page.locator('#viewport').focus();a=await area(page);await clear(page); + await page.mouse.move(a.x-40,a.y);await page.mouse.down();await page.mouse.move(a.x+40,a.y); + await page.locator('#local-test-editor').focus(); + assert.deepEqual((await inputs(page)).filter(c=>c.input.action!=='move').map(c=>c.input.action),['down','up'],'editor focus releases held remote button'); + await page.mouse.up();await page.locator('#viewport').focus();await clear(page); + await page.mouse.move(a.x-40,a.y);await page.mouse.down();await page.mouse.move(a.x+40,a.y); + await page.evaluate(()=>document.querySelector('#mode').click()); + assert.deepEqual((await inputs(page)).filter(c=>c.input.action!=='move').map(c=>c.input.action),['down','up'],'switching mode releases with the original mode'); + await clear(page);await page.mouse.move(a.x+70,a.y);await page.mouse.up();assert.deepEqual(await inputs(page),[]); +}); + +test('Ctrl-wheel pinch zooms locally at the pointer instead of sending remote scroll',async t=>{ + const {page}=await fixture(t);const a=await area(page);await clear(page); + await page.mouse.move(a.x+60,a.y);await page.keyboard.down('Control');await page.mouse.wheel(0,-150);await page.keyboard.up('Control'); + await page.evaluate(()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)))); + assert.deepEqual(await inputs(page),[],'browser trackpad pinch must not scroll the remote terminal'); + const v=await page.evaluate(()=>__viewEvents.at(-1));assert.ok(v?.scale>1);assert.ok(v.offset.x<0,'zoom anchor follows the wheel pointer'); +}); + +test('scroll deltas normalize wheel units and clamp the v2 payload',async t=>{ + const {page}=await fixture(t);const a=await area(page);await clear(page); + await page.mouse.move(a.x,a.y);await page.mouse.wheel(9999,-9999); + await page.waitForFunction(()=>__uiFixture.calls.some(c=>c.input?.type==='scroll')); + assert.deepEqual((await inputs(page))[0].input,{type:'scroll',x:.5,y:.5,dx:2000,dy:-2000}); + await clear(page); + await page.evaluate(()=>document.querySelector('#viewport').dispatchEvent(new WheelEvent('wheel',{clientX:500,clientY:300,deltaMode:1,deltaX:1,deltaY:2,bubbles:true,cancelable:true}))); + const line=(await inputs(page))[0].input;assert.equal(line.dx,16);assert.equal(line.dy,32); +}); + +test('two-finger trackpad translation scrolls once per centroid change and never clicks',async t=>{ + const {page,context}=await fixture(t);const a=await area(page);await menu(page,'mode');await clear(page);const touch=await touchSession(context,page); + await touch('touchStart',[[0,a.x-50,a.y],[1,a.x+50,a.y]]); + await touch('touchMove',[[0,a.x-30,a.y+30],[1,a.x+70,a.y+30]]); + await page.evaluate(()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)))); + await touch('touchEnd',[[1,a.x+70,a.y+30]]);await touch('touchMove',[[1,a.x+80,a.y+40]]);await touch('touchEnd',[]); + assert.deepEqual((await inputs(page)).map(c=>c.input),[{type:'scroll',x:.5,y:.5,dx:-20,dy:-30}]); + assert.deepEqual(await page.evaluate(()=>__viewEvents),[],'translation does not change local zoom'); +}); + +test('Settings swipe scrolls under the finger while slider movement stays a drag',async t=>{ + const {page,context}=await fixture(t);const a=await area(page),touch=await touchSession(context,page); + await page.evaluate(()=>__uiFixture.emit({source:{target:'control',surface:'settings',dragRects:[]}}));await clear(page); + await touch('touchStart',[[0,a.x,a.y+40]]);await touch('touchMove',[[0,a.x,a.y-40]]);await touch('touchEnd',[]); + const scroll=(await inputs(page)).map(c=>c.input);assert.ok(scroll.length>0);assert.ok(scroll.every(c=>c.type==='scroll'));assert.ok(scroll[0].dy>0); + await page.evaluate(()=>__uiFixture.emit({source:{target:'control',surface:'settings',dragRects:[{x:0,y:0,width:1,height:1}]}}));await clear(page); + await touch('touchStart',[[0,a.x-40,a.y]]);await touch('touchMove',[[0,a.x+40,a.y]]);await touch('touchEnd',[]); + const slider=(await inputs(page)).map(c=>c.input);assert.deepEqual(slider.filter(c=>c.action!=='move').map(c=>c.action),['down','up']);assert.ok(slider.every(c=>c.type==='pointer')); +}); +test('Settings scrolling uses remote CSS units rather than high density video pixels',async t=>{ + const {page,context}=await fixture(t);const a=await area(page),touch=await touchSession(context,page); + async function swipe(width){ + await page.evaluate(width=>__uiFixture.emit({source:{target:'control',surface:'settings',viewport:{width,height:800},dragRects:[]}}),width);await clear(page); + await touch('touchStart',[[0,a.x,a.y+40]]);await touch('touchMove',[[0,a.x,a.y-40]]);await touch('touchEnd',[]); + return (await inputs(page)).filter(c=>c.input.type==='scroll').reduce((sum,c)=>sum+c.input.dy,0); + } + const normal=await swipe(1280),dense=await swipe(640);assert.ok(normal>0);assert.ok(Math.abs(dense*2-normal)<.001); +}); + +test('pointer geometry uses contained video content at fit and zoom across portrait/landscape',async t=>{ + const {page}=await fixture(t,{width:390,height:844}); + for(const size of [{width:390,height:844},{width:844,height:390},{width:1280,height:800}]){ + await page.setViewportSize(size); + await page.evaluate(()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)))); + for(let zoom=0;zoom<2;zoom++){ + if(zoom)await menu(page,'fit'); + const g=await page.evaluate(()=>{const v=document.querySelector('#video'),r=v.getBoundingClientRect(),p=document.querySelector('#viewport').getBoundingClientRect(),f=Math.min(r.width/v.videoWidth,r.height/v.videoHeight);return {left:r.left+(r.width-v.videoWidth*f)/2,top:r.top+(r.height-v.videoHeight*f)/2,width:v.videoWidth*f,height:v.videoHeight*f,p:{left:p.left,top:p.top,width:p.width,height:p.height}};}); + const at={x:g.left+g.width*.6,y:g.top+g.height*.55};assert.ok(at.xc.input);assert.deepEqual(values.map(e=>e.action),['down','up'],JSON.stringify({size,zoom,at,g,dom:await page.evaluate(({x,y})=>({at:document.elementFromPoint(x,y)?.outerHTML,active:document.activeElement.id,innerWidth,innerHeight}),at)})); + for(const e of values){assert.ok(Math.abs(e.x-.6)<.006&&Math.abs(e.y-.55)<.006,'coordinates map to actual object-fit content');assert.equal(e.button,0);} + if(zoom)await menu(page,'fit'); + } + } +}); + +test('direct touch drag emits one down/move/up chain and cancel has no tap',async t=>{ + const {page,context}=await fixture(t);const a=await area(page);const touch=await touchSession(context,page);await clear(page); + await touch('touchStart',[[0,a.x-40,a.y]]);await touch('touchMove',[[0,a.x,a.y]]);await touch('touchMove',[[0,a.x+40,a.y+20]]);await touch('touchEnd',[]); + let values=(await inputs(page)).map(c=>c.input);assert.deepEqual(values.map(e=>e.action),['down','move','move','up']);assert.ok(values.every(e=>e.x>=0&&e.x<=1&&e.y>=0&&e.y<=1)); + await clear(page);await touch('touchStart',[[0,a.x,a.y]]);await touch('touchCancel',[]);assert.deepEqual(await inputs(page),[]); +}); + +test('sync throws and rejected API promises are consumed without input replay',async t=>{ + const {page}=await fixture(t);const a=await area(page);await clear(page); + await page.evaluate(()=>{CanvasLinkClient.sendInput=()=>Promise.reject(Error('fixture rejection'));CanvasLinkClient.sendText=()=>Promise.reject(Error('fixture rejection'));CanvasLinkClient.sendKey=()=>{throw Error('fixture sync error');};}); + await page.touchscreen.tap(a.x,a.y);await page.keyboard.press('a');await page.keyboard.press('Enter'); + await page.evaluate(()=>{CanvasLinkClient.sendInput=()=>{throw Error('fixture sync error');};}); + await page.touchscreen.tap(a.x,a.y);await page.waitForTimeout(100); + assert.deepEqual(await page.evaluate(()=>__unhandled),[]);assert.deepEqual(await inputs(page),[]); +}); + +test('capture acquisition failure cancels input without a browser exception',async t=>{ + const {page,errors}=await fixture(t);const a=await area(page);await clear(page); + await page.evaluate(()=>document.querySelector('#viewport').setPointerCapture=()=>{throw new DOMException('Fixture capture race','NotFoundError');}); + await page.mouse.move(a.x,a.y);await page.mouse.down();await page.mouse.move(a.x+40,a.y);await page.mouse.up(); + assert.deepEqual(await inputs(page),[]);assert.deepEqual(errors,[],'capture race is handled'); +}); + +test('a third finger suppresses the whole multitouch sequence until every contact ends',async t=>{ + const {page,context}=await fixture(t);const a=await area(page);const touch=await touchSession(context,page);await clear(page); + await touch('touchStart',[[0,a.x-50,a.y],[1,a.x+50,a.y]]); + await touch('touchStart',[[0,a.x-50,a.y],[1,a.x+50,a.y],[2,a.x,a.y+50]]); + await touch('touchEnd',[[0,a.x-50,a.y],[1,a.x+50,a.y]]); + await touch('touchMove',[[0,a.x-90,a.y],[1,a.x+90,a.y]]); + await page.evaluate(()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)))); + await touch('touchEnd',[]); + assert.deepEqual(await inputs(page),[]);assert.deepEqual(await page.evaluate(()=>__viewEvents),[],'no resumed pair or zoom jump after third contact'); + await page.touchscreen.tap(a.x,a.y);assert.deepEqual((await inputs(page)).map(c=>c.input.action),['down','up']); +}); + +test('an open HTML dialog owns Escape and hardware keys instead of the terminal',async t=>{ + const {page}=await fixture(t);await clear(page); + const prevented=await page.evaluate(()=>{ + document.querySelector('#diagnostics').show();const v=document.querySelector('#viewport');v.focus(); + const e=new KeyboardEvent('keydown',{key:'Escape',bubbles:true,cancelable:true});v.dispatchEvent(e);return e.defaultPrevented; + }); + assert.equal(prevented,false,'gesture layer must not cancel dialog Escape');assert.deepEqual(await inputs(page),[]); + await page.evaluate(()=>document.querySelector('#diagnostics').close()); + for(const [button,dialog]of [['#diagnostics-toggle','#diagnostics'],['#disconnect','#disconnect-dialog']]){ + await page.locator('#menu-toggle').click();await page.locator(button).click();await page.waitForFunction(selector=>document.querySelector(selector).open,dialog); + await page.keyboard.press('Escape');await page.waitForFunction(selector=>!document.querySelector(selector).open,dialog); + } + assert.deepEqual(await inputs(page),[],'real native dialog Escape does not send a remote key'); +}); + +test('zoom stays bounded and survives composer/viewport resize without resetting the view',async t=>{ + const {page}=await fixture(t);const a=await area(page);await menu(page,'fit'); + const before=await page.locator('#video').evaluate(el=>el.style.transform); + await page.setViewportSize({width:1024,height:650}); + await page.evaluate(()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)))); + assert.equal(await page.locator('#video').evaluate(el=>el.style.transform),before,'keyboard/layout changes preserve the user view'); + const v=await area(page);await clear(page); + await page.mouse.move(v.x,v.y);await page.keyboard.down('Control');await page.mouse.wheel(0,-99999); + await page.evaluate(()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)))); + assert.equal(await page.evaluate(()=>__viewEvents.at(-1).scale),8); + await page.mouse.wheel(0,99999);await page.keyboard.up('Control'); + await page.evaluate(()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)))); + assert.equal(await page.evaluate(()=>__viewEvents.at(-1).scale),1);assert.deepEqual(await inputs(page),[]); +}); + +test('trackpad cursor starts at the last direct tap and relative movement stays normalized',async t=>{ + const {page,context}=await fixture(t);const a=await area(page);await page.touchscreen.tap(a.x+40,a.y);const start=(await inputs(page))[0].input; + await menu(page,'mode');await clear(page);const touch=await touchSession(context,page); + await touch('touchStart',[[0,a.x,a.y]]);await touch('touchMove',[[0,a.x+50,a.y+20]]);await touch('touchEnd',[]); + const values=(await inputs(page)).map(c=>c.input);assert.ok(values.length>0&&values.every(v=>v.action==='move')); + assert.ok(Math.abs(values.at(-1).x-(start.x+50/a.width))<.005,'relative movement has no cursor jump'); + assert.ok(values.every(v=>v.x>=0&&v.x<=1&&v.y>=0&&v.y<=1)); +}); + +const inputs=page=>page.evaluate(()=>__uiFixture.calls.filter(c=>['input','key','text'].includes(c.type))); +const clear=page=>page.evaluate(()=>{__uiFixture.calls.length=0;__gestureEvents.length=0;__viewEvents.length=0;}); +async function area(page){const b=await page.locator('#viewport').boundingBox();assert.ok(b&&b.width>0&&b.height>0,'visible viewport');return {x:b.x+b.width/2,y:b.y+b.height/2,width:b.width,height:b.height,left:b.x,top:b.y};} +async function touchSession(context,page){ + const cdp=await context.newCDPSession(page); + return async(type,points)=>cdp.send('Input.dispatchTouchEvent',{type,touchPoints:points.map(([id,x,y])=>({id,x,y,radiusX:2,radiusY:2,force:1}))}); +} + +test('connection boundary blocks gestures and never replays them on reconnect',async t=>{ + const {page}=await fixture(t);const a=await area(page); + await page.evaluate(()=>__uiFixture.emit({connection:'reconnecting'}));await clear(page); + await page.touchscreen.tap(a.x,a.y);await page.mouse.move(a.x,a.y);await page.mouse.wheel(30,80); + await page.evaluate(()=>__uiFixture.emit({connection:'suspended'})); + await page.touchscreen.tap(a.x,a.y); + await page.evaluate(()=>__uiFixture.emit({connection:'connected'})); + assert.deepEqual(await inputs(page),[],'no offline call or delayed replay'); +}); + +test('hardware keys leave native button activation and editor/IME handling intact',async t=>{ + const {page}=await fixture(t);await clear(page); + await page.locator('#menu-toggle').click();await page.locator('#mode').focus();await page.keyboard.press('Enter'); + assert.equal(await page.locator('#mode').getAttribute('aria-pressed'),'true','Enter activates the UI button'); + assert.deepEqual(await inputs(page),[],'UI activation is not remote Enter'); + await page.evaluate(()=>{const e=document.createElement('input');e.id='local-test-editor';document.body.append(e);e.focus();}); + await page.keyboard.type('safe');await page.keyboard.press('Enter'); + assert.deepEqual(await inputs(page),[],'composer owns hardware input'); + await page.evaluate(()=>{ + const host=document.createElement('div');host.innerHTML='
edit
'; + document.body.append(host); + for(const selector of ['#test-select','#test-edit span']){ + document.querySelector(selector).dispatchEvent(new KeyboardEvent('keydown',{key:'Enter',bubbles:true,cancelable:true})); + } + const v=document.querySelector('#viewport');v.tabIndex=0;v.focus(); + v.dispatchEvent(new KeyboardEvent('keydown',{key:'Enter',isComposing:true,bubbles:true,cancelable:true})); + v.dispatchEvent(new KeyboardEvent('keydown',{key:'a',keyCode:229,bubbles:true,cancelable:true})); + }); + assert.deepEqual(await inputs(page),[],'select, contenteditable and IME are never intercepted'); +}); + +async function menu(page,id){await page.locator('#menu-toggle').click();await page.locator('#'+id).click();} diff --git a/integrations/canvaslink/tests/graphics-quality.test.cjs b/integrations/canvaslink/tests/graphics-quality.test.cjs new file mode 100644 index 000000000..36f0791f9 --- /dev/null +++ b/integrations/canvaslink/tests/graphics-quality.test.cjs @@ -0,0 +1,29 @@ +const {test}=require('node:test'),assert=require('node:assert/strict'); +const {createSnapshot}=require('../host/snapshot.cjs'),{rgba}=require('../host/frames.js'); +test('High density is configured at startup without changing CSS zoom or accepting unbounded factors',()=>{ + const {configureRenderScale}=require('../host/display-scale.cjs');let actual,exited=false; + const app={isReady:()=>false,relaunch:options=>actual=options,exit:code=>{assert.equal(code,0);exited=true;}}; + configureRenderScale(app,['preview','--private-profile']);assert.deepEqual(actual,{args:['--private-profile','--force-device-scale-factor=2']});assert(exited); + for(const scale of [1,2])configureRenderScale({...app,relaunch(){throw Error('Relaunch loop');}},['preview','--force-device-scale-factor='+scale]); + assert.throws(()=>configureRenderScale({...app,isReady:()=>true}),/requires-startup/); + for(const scale of [0,1.5,3,Infinity])assert.throws(()=>configureRenderScale(app,['preview','--force-device-scale-factor='+scale]),/invalid-render-scale/); +}); +test('Raw snapshots preserve Retina pixels without JPEG or PNG encoding',()=>{ + const image={isEmpty:()=>false,getScaleFactors:()=>[1,2],getSize:()=>({width:8,height:5}),toBitmap:options=>{assert.equal(options.scaleFactor,2);return Buffer.alloc(16*10*4);},toJPEG(){throw Error('Lossy intermediate');},toPNG(){throw Error('Unnecessary compression');}}; + const value=createSnapshot(image,{createFromBitmap(){throw Error('Unnecessary resize');}}); + assert.equal(value.width,16);assert.equal(value.height,10);assert.equal(value.format,'bgra'); +}); +test('Capture bounds apply to physical pixels, including high DPI',()=>{ + const image={isEmpty:()=>false,getScaleFactors:()=>[2],getSize:()=>({width:8,height:5}),toBitmap:()=>Buffer.alloc(16*10*4)}; + const value=createSnapshot(image,{createFromBitmap(pixels,options){assert.equal(options.scaleFactor,1);assert.equal(options.width,16);assert.equal(options.height,10);return {resize(size){assert.equal(size.width,8);assert.equal(size.height,5);return {toBitmap:()=>Buffer.alloc(8*5*4)};}};}},{maxWidth:8,maxHeight:5}); + assert.equal(value.width,8);assert.equal(value.height,5); +}); +test('Raw conversion preserves red/blue order and restores premultiplied alpha',()=>{ + const original=Uint8Array.from([0,0,255,255,255,0,0,255,10,20,40,128]); + assert.deepEqual([...rgba(original,3,1)],[255,0,0,255,0,0,255,255,80,40,20,128]);assert.equal(original[0],0); +}); +test('Malformed and unbounded pixel frames are rejected before canvas allocation',()=>{ + assert.throws(()=>rgba(new Uint8Array(4),2560,1600),/invalid-bitmap/); + assert.throws(()=>rgba(new Uint8Array(0),0,0),/invalid-bitmap/); + assert.throws(()=>rgba(new Uint8Array(2561*4),2561,1),/invalid-bitmap/); +}); diff --git a/integrations/canvaslink/tests/host-peer.test.cjs b/integrations/canvaslink/tests/host-peer.test.cjs new file mode 100644 index 000000000..70a6e56a1 --- /dev/null +++ b/integrations/canvaslink/tests/host-peer.test.cjs @@ -0,0 +1,76 @@ +const {test}=require('node:test'),assert=require('node:assert/strict'); +const fs=require('node:fs'),path=require('node:path'),vm=require('node:vm'); +const source=fs.readFileSync(path.join(__dirname,'../host/renderer.js'),'utf8'); +const tick=()=>new Promise(resolve=>setImmediate(resolve)); +async function host(){ + const elements=new Map(),captures=[],peers=[],signals=[],sessions=[],timers=[],fallbacks=[]; + const stream=()=>{const track={stopped:false,stop(){this.stopped=true;}};return {track,getVideoTracks:()=>[track],getTracks:()=>[track]};}; + const element=id=>{if(!elements.has(id))elements.set(id,{hidden:false,textContent:'',replaceChildren(){},append(){},getContext:()=>({}),captureStream:()=>{const s=stream();fallbacks.push(s);return s;}});return elements.get(id);}; + class Peer{ + constructor(){peers.push(this);this.iceGatheringState='complete';this.connectionState='new';} + addTrack(track){this.track=track;return {getParameters:()=>({encodings:[]}),replaceTrack:async value=>{this.track=value;}};} + async getStats(){return new Map([['video',{type:'outbound-rtp',kind:'video',framesEncoded:this.frames||0}]]);} + getTransceivers(){return [{setCodecPreferences(){}}];} + createDataChannel(){return this.channel={readyState:'connecting',close(){this.readyState='closed';},send(){}};} + async createOffer(){return {type:'offer',sdp:'owned-test-offer'};} + async setLocalDescription(value){this.localDescription=value;} + close(){this.closed=true;} + } + let event; + const context={window:{CanvasLinkFrames:require('../host/frames.js'),host:{state:async()=>({embedded:true,source:'Owned target',addresses:[],connected:[],width:100,height:100}),permissions:async()=>({platform:'linux'}),sources:async()=>[{id:'owned',name:'Owned target'}],select:async()=>{},onEvent:handler=>event=handler,report(){},captureFallback:async()=>{},inputSession:async(...args)=>sessions.push(args),signal:async(...args)=>signals.push(args)}},document:{getElementById:element,createElement:()=>({})},navigator:{mediaDevices:{getDisplayMedia:()=>new Promise((resolve,reject)=>captures.push({resolve,reject}))}},RTCPeerConnection:Peer,RTCRtpSender:{getCapabilities:()=>({codecs:[]})},setInterval(){},setTimeout:fn=>{const t={fn};timers.push(t);return t;},clearTimeout:t=>{if(t)t.cancelled=true;}}; + vm.runInNewContext(source,context);await tick(); + return {event,captures,peers,signals,sessions,stream,timers,fallbacks,bridge:context.window.host}; +} +test('Overlapping reconnect capture resolves only the newest generation',async()=>{ + const h=await host(),first=h.event({type:'signal',id:'device',message:{type:'ping'}});await tick(); + const second=h.event({type:'signal',id:'device',message:{type:'ping'}});await tick(); + assert.equal(h.captures.length,2);const old=h.stream(),current=h.stream(); + h.captures[1].resolve(current);await second;h.captures[0].resolve(old);await first; + assert.equal(h.peers.length,1,'a stale capture created a second peer'); + assert.equal(h.peers[0].track,current.track);assert.equal(old.track.stopped,true);assert.equal(current.track.stopped,false); + assert.equal(h.signals.length,1); +}); +test('A delayed ended event from the old track cannot close the new peer',async()=>{ + const h=await host();let run=h.event({type:'signal',id:'device',message:{type:'ping'}});await tick(); + const old=h.stream();h.captures[0].resolve(old);await run; + run=h.event({type:'signal',id:'device',message:{type:'ping'}});await tick(); + const current=h.stream();h.captures[1].resolve(current);await run; + old.track.onended();assert.equal(current.track.stopped,false);assert.equal(h.peers[1].closed,undefined); + assert.equal(h.signals.filter(([,value])=>value.type==='disconnect').length,0); +}); +test('A connected tab with no first frame switches to owned snapshot capture',async()=>{ + const h=await host(),run=h.event({type:'signal',id:'device',message:{type:'ping'}});await tick(); + const capture=h.stream();h.captures[0].resolve(capture);await run; + const p=h.peers[0];p.channel.onopen();await h.timers[0].fn(); + assert.equal(h.fallbacks.length,1);assert.equal(p.track,h.fallbacks[0].track); + assert.equal(capture.track.stopped,true);assert.equal(p.track.stopped,false); +}); +test('A tab that already encoded video keeps its original capture',async()=>{ + const h=await host(),run=h.event({type:'signal',id:'device',message:{type:'ping'}});await tick(); + const capture=h.stream();h.captures[0].resolve(capture);await run; + const p=h.peers[0];p.frames=1;p.channel.onopen();await h.timers[0].fn(); + assert.equal(h.fallbacks.length,0);assert.equal(p.track,capture.track);assert.equal(capture.track.stopped,false); +}); +test('Zero decoded-frame feedback recovers even when the sender encoded frames',async()=>{ + const h=await host(),run=h.event({type:'signal',id:'device',message:{type:'ping'}});await tick(); + const capture=h.stream();h.captures[0].resolve(capture);await run; + const p=h.peers[0];p.frames=8;p.channel.readyState='open';p.channel.onopen(); + p.channel.onmessage({data:JSON.stringify({type:'ping',at:1,videoFrames:0})});await h.timers[0].fn(); + assert.equal(h.fallbacks.length,1);assert.equal(capture.track.stopped,true); +}); +test('An old initial-frame watchdog cannot replace a reconnected capture',async()=>{ + const h=await host();let run=h.event({type:'signal',id:'device',message:{type:'ping'}});await tick();h.captures[0].resolve(h.stream());await run; + let resolve;h.peers[0].getStats=()=>new Promise(r=>resolve=r);h.peers[0].channel.onopen();const waiting=h.timers[0].fn(); + run=h.event({type:'signal',id:'device',message:{type:'ping'}});await tick();const capture=h.stream();h.captures[1].resolve(capture);await run; + resolve(new Map());await waiting; + assert.equal(h.timers[0].cancelled,true);assert.equal(h.fallbacks.length,0);assert.equal(h.peers[1].track,capture.track); +}); +test('Revocation stops tracks and control before any settings refresh, even if refresh fails',async()=>{ + const h=await host(),run=h.event({type:'signal',id:'device',message:{type:'ping'}});await tick(); + const capture=h.stream();h.captures[0].resolve(capture);await run; + let refresh;h.bridge.state=()=>new Promise((resolve,reject)=>refresh={reject}); + const revoke=h.event({type:'revoke',id:'device'}).catch(error=>error); + assert.equal(capture.track.stopped,true,'revocation waited for UI state');assert.equal(h.peers[0].closed,true); + assert.equal(h.sessions.at(-1)[1],null); + refresh.reject(Error('state-unavailable'));await revoke; +}); diff --git a/integrations/canvaslink/tests/icon.test.cjs b/integrations/canvaslink/tests/icon.test.cjs new file mode 100644 index 000000000..c135e1dd7 --- /dev/null +++ b/integrations/canvaslink/tests/icon.test.cjs @@ -0,0 +1,162 @@ +'use strict'; + +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); +const root = path.resolve(__dirname, '..'); +const sharp = require(process.env.CANVASLINK_SHARP || 'sharp'); +const read = file => fs.readFileSync(path.join(root, file), 'utf8'); +const exists = file => fs.existsSync(path.join(root, file)); +const ns = 'http://schemas.android.com/apk/res/android'; + +function xml(text) { + const parsed = spawnSync('python3', ['-c', + 'import sys,json,xml.etree.ElementTree as E\nr=E.fromstring(sys.stdin.read())\nprint(json.dumps([{"tag":e.tag,"attrs":e.attrib,"text":e.text} for e in r.iter()]))'], + { input: text, encoding: 'utf8' }); + assert.equal(parsed.status, 0, `XML must be well formed: ${parsed.stderr}`); + return JSON.parse(parsed.stdout); +} + +function source() { + assert.ok(exists('design/canvaslink-icon.svg'), 'Original CanvasLink SVG is missing'); + const text = read('design/canvaslink-icon.svg'); + const nodes = xml(text); + return { text, nodes, paths: nodes.filter(node => node.tag.endsWith('}path')) }; +} + +function foregroundSvg(paths) { + return `${paths.map(p => ``).join('')}`; +} + +// Source geometry is checked independently of Android conversion and launcher masks. +test('icon: original two-window mark uses the UI palette and stays inside the 66dp safe circle', async () => { + const { text, nodes, paths } = source(); + assert.match(text, /SPDX-License-Identifier: MIT/); + assert.match(text, /Original geometry created for CanvasLink/); + assert.deepEqual(nodes[0].attrs.viewBox, '0 0 108 108'); + assert.deepEqual(paths.map(p => p.attrs.id), ['desktop-window', 'touch-window']); + assert.deepEqual(paths.map(p => p.attrs.fill.toLowerCase()), ['#edf1e6', '#d6ef88']); + const background = nodes.find(n => n.tag.endsWith('}rect')); + assert.equal(background.attrs.fill.toLowerCase(), '#121814'); + assert.equal(background.attrs.width, '108'); + assert.equal(background.attrs.height, '108'); + assert.ok(!/<(?:text|image|use|filter|linearGradient|radialGradient)\b|\b(?:stroke|transform)=/i.test(text), 'No fonts, imported art, effects or ambiguous stroked bounds'); + const { data, info } = await sharp(Buffer.from(foregroundSvg(paths)), { density: 720 }).ensureAlpha().raw().toBuffer({ resolveWithObject: true }); + let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity, maxRadius = 0, ink = 0; + const scale = info.width / 108; + for (let y = 0; y < info.height; y++) for (let x = 0; x < info.width; x++) { + if (data[(y * info.width + x) * 4 + 3] < 128) continue; + const px = (x + .5) / scale, py = (y + .5) / scale; + minX = Math.min(minX, px); maxX = Math.max(maxX, px); + minY = Math.min(minY, py); maxY = Math.max(maxY, py); + maxRadius = Math.max(maxRadius, Math.hypot(px - 54, py - 54)); + ink++; + } + assert.ok(ink > 0, 'Foreground must not be empty'); + assert.ok(maxRadius <= 33, `Critical mark exceeds 66dp safe circle: radius ${maxRadius}`); + assert.ok(maxX - minX >= 46 && maxX - minX <= 54, 'Two-window silhouette needs a strong, centered width'); + assert.ok(maxY - minY >= 44 && maxY - minY <= 50, 'Two-window silhouette needs a strong, centered height'); + assert.ok(Math.abs((minX + maxX) / 2 - 54) < 1, 'Horizontal optical center'); + assert.ok(Math.abs((minY + maxY) / 2 - 54) < 1, 'Vertical optical center'); +}); + +test('icon: API26 adaptive resources reproduce the source without changing existing identifiers or colors', () => { + const { paths } = source(); + const foreground = xml(read('android/res/drawable/ic_launcher_foreground.xml')); + const attr = key => `{${ns}}${key}`; + assert.equal(foreground[0].tag, 'vector'); + for (const dimension of ['width', 'height']) assert.equal(foreground[0].attrs[attr(dimension)], '108dp'); + for (const dimension of ['viewportWidth', 'viewportHeight']) assert.equal(foreground[0].attrs[attr(dimension)], '108'); + const vectorPaths = foreground.filter(n => n.tag === 'path'); + assert.equal(vectorPaths.length, 2, 'Adaptive foreground must contain both linked window rails'); + vectorPaths.forEach((p, index) => { + assert.equal(p.attrs[attr('pathData')], paths[index].attrs.d, 'Android and SVG geometry must be identical'); + assert.equal(p.attrs[attr('fillColor')].toLowerCase(), paths[index].attrs.fill.toLowerCase()); + assert.ok(!p.attrs[attr('strokeWidth')], 'Use unambiguous filled contours'); + }); + const adaptive = xml(read('android/res/mipmap-anydpi-v26/ic_launcher.xml')); + assert.equal(adaptive[0].tag, 'adaptive-icon'); + assert.equal(adaptive.find(n => n.tag === 'background').attrs[attr('drawable')], '@color/icon_background'); + assert.equal(adaptive.find(n => n.tag === 'foreground').attrs[attr('drawable')], '@drawable/ic_launcher_foreground'); + assert.ok(!adaptive.some(n => n.tag === 'monochrome'), 'API26 resource must not depend on API33 tags'); + const colors = xml(read('android/res/values/colors.xml')); + assert.equal(colors.find(n => n.attrs.name === 'icon_background').text.trim(), '#121814'); + const manifest = xml(read('android/AndroidManifest.xml')); + const app = manifest.find(n => n.tag === 'application'); + assert.equal(app.attrs[attr('icon')], '@mipmap/ic_launcher'); + assert.equal(app.attrs[attr('roundIcon')], '@mipmap/ic_launcher'); + +}); + +test('icon: API33 themed icon uses the same safe silhouette without a filled background', () => { + assert.ok(exists('android/res/mipmap-anydpi-v33/ic_launcher.xml'), 'API33 adaptive icon with monochrome layer is missing'); + assert.ok(exists('android/res/drawable/ic_launcher_monochrome.xml'), 'Themed foreground vector is missing'); + const attr = key => `{${ns}}${key}`; + const adaptive = xml(read('android/res/mipmap-anydpi-v33/ic_launcher.xml')); + assert.equal(adaptive[0].tag, 'adaptive-icon'); + assert.equal(adaptive.find(n => n.tag === 'background').attrs[attr('drawable')], '@color/icon_background'); + assert.equal(adaptive.find(n => n.tag === 'foreground').attrs[attr('drawable')], '@drawable/ic_launcher_foreground'); + assert.equal(adaptive.find(n => n.tag === 'monochrome').attrs[attr('drawable')], '@drawable/ic_launcher_monochrome'); + const monochrome = xml(read('android/res/drawable/ic_launcher_monochrome.xml')); + assert.equal(monochrome[0].tag, 'vector'); + assert.equal(monochrome[0].attrs[attr('viewportWidth')], '108'); + assert.equal(monochrome[0].attrs[attr('viewportHeight')], '108'); + const rails = monochrome.filter(n => n.tag === 'path'); + assert.equal(rails.length, 2, 'Themed icon must not add a solid-background path'); + rails.forEach((rail, index) => { + assert.equal(rail.attrs[attr('pathData')], source().paths[index].attrs.d); + assert.equal(rail.attrs[attr('fillColor')].toLowerCase(), '#ffffff'); + }); +}); + +test('icon: production-vector previews preserve every rail under square, circle and squircle masks at 24–512px', async () => { + assert.ok(exists('verification/icon-preview.cjs'), 'Reproducible Android-vector preview renderer is missing'); + const rendered = spawnSync(process.execPath, ['verification/icon-preview.cjs'], { cwd: root, encoding: 'utf8' }); + assert.equal(rendered.status, 0, rendered.stderr); + const manifest = JSON.parse(read('verification/icon-previews.json')); + assert.deepEqual(manifest.adaptiveViewport, { x: 18, y: 18, size: 72, layerSize: 108 }); + assert.equal(manifest.geometrySource, 'android/res/drawable/ic_launcher_foreground.xml'); + const expected = [24, 32, 48, 64, 128, 512].flatMap(size => ['square', 'circle', 'squircle'].map(mask => `${mask}-${size}`)); + assert.deepEqual(manifest.previews.map(p => `${p.mask}-${p.width}`), expected); + const crypto = require('node:crypto'); + for (const preview of manifest.previews) { + const bytes = fs.readFileSync(path.join(root, preview.file)); + assert.equal(crypto.createHash('sha256').update(bytes).digest('hex'), preview.sha256, preview.file); + const metadata = await sharp(bytes).metadata(); + assert.equal(metadata.width, preview.width); + assert.equal(metadata.height, preview.width); + const { data, info } = await sharp(bytes).ensureAlpha().raw().toBuffer({ resolveWithObject: true }); + const pixel = (x, y) => [...data.subarray((y * info.width + x) * 4, (y * info.width + x) * 4 + 4)]; + assert.equal(pixel(0, 0)[3], preview.mask === 'square' ? 255 : 0); + assert.equal(pixel(Math.floor(info.width / 2), Math.floor(info.width / 2))[3], 255); + if (preview.width >= 128) { + const diagonalAlpha = pixel(Math.floor(info.width * .1), Math.floor(info.width * .1))[3]; + assert.equal(diagonalAlpha, preview.mask === 'circle' ? 0 : 255, 'Mask must match circle vs exponent-4 squircle'); + } + if (preview.width === 24) { + let cream = 0, lime = 0; + for (let i = 0; i < data.length; i += 4) { + if (data[i] > 200 && data[i + 1] > 200 && data[i + 2] > 190 && data[i + 3] > 200) cream++; + if (data[i] > 170 && data[i + 1] > 200 && data[i + 2] < 170 && data[i + 3] > 200) lime++; + } + assert.ok(cream >= 15 && lime >= 15, `Both rails must remain visible at 24px: cream=${cream}, lime=${lime}`); + } + } + const raw = async mask => sharp(path.join(root, `verification/icon-${mask}-512.png`)).ensureAlpha().raw().toBuffer(); + const square = await raw('square'); + for (const mask of ['circle', 'squircle']) { + const masked = await raw(mask); + let checked = 0; + for (let i = 0; i < square.length; i += 4) { + if (square[i] === 18 && square[i + 1] === 24 && square[i + 2] === 20) continue; + assert.deepEqual(masked.subarray(i, i + 4), square.subarray(i, i + 4), `${mask} must not clip any foreground pixel`); + checked++; + } + assert.ok(checked > 1000); + } + const sheet = await sharp(path.join(root, 'verification/icon-contact-sheet.png')).metadata(); + assert.equal(sheet.width, 1320); + assert.equal(sheet.height, 1080); +}); diff --git a/integrations/canvaslink/tests/owned-focus.test.cjs b/integrations/canvaslink/tests/owned-focus.test.cjs new file mode 100644 index 000000000..789291011 --- /dev/null +++ b/integrations/canvaslink/tests/owned-focus.test.cjs @@ -0,0 +1,7 @@ +const {test}=require('node:test'),assert=require('node:assert/strict'); +const {focusOwnedWindow}=require('../host/owned-focus.cjs'); +function fixture(){let tick=0,focused=false,active=true,requests=0,renderer=0;return {app:{focus(){requests++;}},target:{isDestroyed:()=>false,isMinimized:()=>false,isFocused:()=>focused,show(){},focus(){},webContents:{focus(){renderer++;}}},active:()=>active,now:()=>tick,wait:async ms=>{tick+=ms;if(tick>=60)focused=true;},revoke(){active=false;},get requests(){return requests;},get renderer(){return renderer;}};} +test('Own window activation can settle asynchronously before renderer input is focused',async()=>{const f=fixture();await focusOwnedWindow(f);assert.equal(f.requests,1);assert.equal(f.renderer,1);}); +test('Revocation while awaiting activation cannot focus the input renderer',async()=>{const f=fixture();f.wait=async()=>{f.revoke();};await assert.rejects(focusOwnedWindow(f),/session-ended/);assert.equal(f.renderer,0);}); +test('An unactivatable own window fails within the bounded wait',async()=>{const f=fixture();let tick=0;f.now=()=>tick;f.wait=async ms=>{tick+=ms;};await assert.rejects(focusOwnedWindow({...f,timeout:40}),/canvas-focus-required/);assert.equal(f.renderer,0);assert.equal(f.requests,1);}); +test('A child-window resignation can be followed by another bounded own activation',async()=>{const f=fixture();let tick=0;f.now=()=>tick;f.target.isFocused=()=>f.requests>=2;f.wait=async ms=>{tick+=ms;};await focusOwnedWindow(f);assert.equal(f.requests,2);assert.equal(f.renderer,1);}); diff --git a/integrations/canvaslink/tests/owned-pointer.test.cjs b/integrations/canvaslink/tests/owned-pointer.test.cjs new file mode 100644 index 000000000..f7595688a --- /dev/null +++ b/integrations/canvaslink/tests/owned-pointer.test.cjs @@ -0,0 +1,16 @@ +const {test}=require('node:test'),assert=require('node:assert/strict'); +const {OwnedPointer}=require('../host/owned-pointer.cjs'); +test('revocation releases both held buttons at their latest owned-renderer position',()=>{ + const events=[],contents={isDestroyed:()=>false,sendInputEvent:e=>events.push(e)},p=new OwnedPointer(); + p.send(contents,{type:'mouseDown',button:'left',x:1,y:2});p.send(contents,{type:'mouseDown',button:'right',x:1,y:2}); + p.send(contents,{type:'mouseMove',button:'left',x:8,y:9,modifiers:['leftButtonDown']});p.release();p.release(); + assert.deepEqual(events.slice(3).map(e=>[e.type,e.button,e.x,e.y,e.modifiers]),[['mouseUp','left',8,9,[]],['mouseUp','right',8,9,[]]]); +}); +test('a released or destroyed target cannot receive a duplicate cancellation event',()=>{ + const events=[];let destroyed=false;const contents={isDestroyed:()=>destroyed,sendInputEvent:e=>events.push(e)},p=new OwnedPointer(); + p.send(contents,{type:'mouseDown',button:'left'});p.send(contents,{type:'mouseUp',button:'left'});p.release();assert.equal(events.length,2); + p.send(contents,{type:'mouseDown',button:'right'});destroyed=true;p.release();assert.equal(events.length,3);assert.equal(p.held.size,0); +}); +test('failed mouseDown is never recorded as an applied press',()=>{ + const p=new OwnedPointer();assert.throws(()=>p.send({sendInputEvent(){throw Error('closed');}},{type:'mouseDown',button:'left'}));assert.equal(p.held.size,0); +}); diff --git a/integrations/canvaslink/tests/remote-select.test.cjs b/integrations/canvaslink/tests/remote-select.test.cjs new file mode 100644 index 000000000..f316213f7 --- /dev/null +++ b/integrations/canvaslink/tests/remote-select.test.cjs @@ -0,0 +1,8 @@ +const {test}=require('node:test'); +const assert=require('node:assert/strict'); +const {openUI}=require('../scripts/ui-test-harness.cjs'); +const {selectPointerScript,closeSelectScript}=require('../host/remote-select.cjs'); +async function fixture(t){const ui=await openUI();t.after(()=>ui.browser.close());await ui.page.setContent('');await ui.page.evaluate(()=>{window.changes=0;document.querySelector('select').addEventListener('change',()=>changes++);});return ui;} +test('Remote select renders options in the captured page and actual clicks dispatch change',async t=>{const ui=await fixture(t);assert.equal(await ui.page.evaluate(selectPointerScript(50,40)),true);assert.equal(await ui.page.getByRole('listbox').getByRole('option',{name:'B',exact:true}).isDisabled(),true);assert.equal(await ui.page.getByRole('listbox').locator('button').nth(2).isDisabled(),true);await ui.page.getByRole('listbox').getByRole('option',{name:'',exact:true}).click();assert.equal(await ui.page.locator('select').inputValue(),'');assert.equal(await ui.page.evaluate(()=>changes),1);assert.equal(await ui.page.getByRole('listbox').count(),0);}); +test('Remote select keys skip disabled options; Escape, outside tap and disconnect dismiss safely',async t=>{const ui=await fixture(t);await ui.page.evaluate(selectPointerScript(50,40));await ui.page.keyboard.press('ArrowDown');await ui.page.keyboard.press('Enter');assert.equal(await ui.page.locator('select').inputValue(),'');await ui.page.evaluate(selectPointerScript(50,40));await ui.page.keyboard.press('ArrowUp');await ui.page.keyboard.press('Escape');assert.equal(await ui.page.locator('select').inputValue(),'');await ui.page.evaluate(selectPointerScript(50,40));await ui.page.locator('#outside').click();assert.equal(await ui.page.getByRole('listbox').count(),0);await ui.page.evaluate(selectPointerScript(50,40));await ui.page.evaluate(closeSelectScript);assert.equal(await ui.page.getByRole('listbox').count(),0);assert.equal(await ui.page.evaluate(()=>changes),1);}); +test('Ordinary pointers and disabled selects retain normal browser handling',async t=>{const ui=await fixture(t);assert.equal(await ui.page.evaluate(selectPointerScript(600,400)),false);await ui.page.locator('select').evaluate(e=>e.disabled=true);assert.equal(await ui.page.evaluate(selectPointerScript(50,40)),false);}); diff --git a/integrations/canvaslink/tests/security.test.cjs b/integrations/canvaslink/tests/security.test.cjs new file mode 100644 index 000000000..2007e7932 --- /dev/null +++ b/integrations/canvaslink/tests/security.test.cjs @@ -0,0 +1,71 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const srp = require('secure-remote-password/client'); +const crypto = require('node:crypto'); +const { Pairing, seal, open, USER, privateAddress } = require('../host/security.cjs'); +const { validateInput } = require('../host/native-input.cjs'); +function authenticate(p, pin=p.pin) { + const a=srp.generateEphemeral(); const start=p.start({A:a.public,name:'Synthetic Android'},'192.168.2.23'); + const client=srp.deriveSession(a.secret,start.B,start.salt,USER,srp.derivePrivateKey(start.salt,USER,pin)); + const finish=p.finish({id:start.id,proof:client.proof},'192.168.2.23');srp.verifySession(a.public,client,finish.proof); + const key=crypto.createHash('sha256').update(Buffer.from(client.key,'hex')).update('CanvasLink/channel/1').digest(); + return {id:finish.id,key}; +} +test('SRP mutual authentication, encrypted request/reply, one use PIN',()=>{ + const p=new Pairing(),pin=p.pin,c=authenticate(p); + assert.equal(p.pin,'');assert.throws(()=>authenticate(p,pin)); + const packet=seal(c.key,c.id,1,'request',{type:'poll'}); + const response=p.receive(packet,'192.168.2.23',()=>({ok:true})); + assert.deepEqual(open(c.key,c.id,response,'response'),{ok:true}); + assert.throws(()=>p.receive(packet,'192.168.2.23',()=>({ok:true})),/unauthorized/); +}); +test('wrong PIN, expiration, host revocation and address changes fail closed',()=>{ + let now=1000;const p=new Pairing({now:()=>now}); + assert.throws(()=>authenticate(p,p.pin==='000000'?'000001':'000000')); + now+=120001;assert.throws(()=>authenticate(p)); + p.rotate();const c=authenticate(p),packet=seal(c.key,c.id,1,'request',{type:'poll'}); + assert.throws(()=>p.receive(packet,'192.168.2.99',()=>({ok:true}))); + p.rotate();assert.throws(()=>p.receive(packet,'192.168.2.23',()=>({ok:true}))); +}); +test('tampering, reflection and untrusted input do not become actions',()=>{ + const p=new Pairing(),c=authenticate(p),packet=seal(c.key,c.id,1,'request',{type:'poll'}); + let calls=0;const corrupt={...packet,data:Buffer.alloc(80).toString('base64')}; + assert.throws(()=>p.receive(corrupt,'192.168.2.23',()=>{calls++;}));assert.equal(calls,0); + assert.throws(()=>open(c.key,c.id,packet,'response')); + for(const input of [{type:'exec',command:'sh'},{type:'text',text:'x'.repeat(8001)},{type:'pointer',x:2,y:0,button:0,action:'down'},{type:'key',key:'unknown',modifiers:[]}])assert.throws(()=>validateInput(input)); + validateInput({type:'text',text:'Проверка'}); +}); +test('only private IPv4 and loopback peers are accepted',()=>{ + for(const a of ['127.0.0.1','::1','::ffff:192.168.2.23','10.1.2.3','172.31.2.3'])assert.equal(privateAddress(a),true); + for(const a of ['8.8.8.8','192.168.999.1','example.com','172.32.2.3',''])assert.equal(privateAddress(a),false); +}); +test('pairing attempts are globally bounded, not reset by a new peer address',()=>{ + const p=new Pairing();for(let i=0;i<5;i++)assert.throws(()=>authenticate(p,p.pin==='000000'?'000001':'000000')); + assert.throws(()=>authenticate(p),/pairing-closed/); + p.rotate();const ephemeral=srp.generateEphemeral();for(let i=0;i<30;i++)p.start({A:ephemeral.public,name:'Synthetic'},'192.168.2.'+(i+1)); + assert.throws(()=>p.start({A:ephemeral.public,name:'Synthetic'},'192.168.2.99'),/pairing-closed/); +}); +test('counter gaps after Android crash are accepted; old replies and expired sessions are not',()=>{ + let now=1000;const p=new Pairing({now:()=>now}),c=authenticate(p); + const packet=seal(c.key,c.id,10001,'request',{type:'poll'}); + const response=p.receive(packet,'192.168.2.23',()=>({ok:true}));assert.equal(response.seq,10001); + assert.throws(()=>p.receive(seal(c.key,c.id,2,'request',{}),'192.168.2.23',()=>({})),/unauthorized/); + now+=12*3600000; + assert.throws(()=>p.receive(seal(c.key,c.id,10002,'request',{}),'192.168.2.23',()=>({})),/unauthorized/); +}); +test('Expired sessions revoke live access once, including without a client request',()=>{ + let now=1000;const revoked=[]; + const p=new Pairing({now:()=>now,onRevoke:id=>{assert.equal(p.sessions.has(id),false);revoked.push(id);}}),c=authenticate(p); + now+=12*3600000-1;p.expire();assert.deepEqual(revoked,[]); + now++;p.expire();p.expire();p.rotate(); + assert.deepEqual(revoked,[c.id]);assert.equal(p.sessions.size,0); +}); +test('Neither requests nor queued signals retain expired sessions',()=>{ + for(const operation of ['request','queue']){ + let now=1000;const revoked=[],p=new Pairing({now:()=>now,onRevoke:id=>revoked.push(id)}),c=authenticate(p); + now+=12*3600000; + if(operation==='request')assert.throws(()=>p.receive(seal(c.key,c.id,1,'request',{}),'192.168.2.23',()=>assert.fail('expired operation executed')),/unauthorized/); + else p.queue(c.id,{type:'offer',sdp:'expired'}); + assert.deepEqual(revoked,[c.id]);assert.equal(p.sessions.size,0); + } +}); diff --git a/integrations/canvaslink/tests/settings-host.test.cjs b/integrations/canvaslink/tests/settings-host.test.cjs new file mode 100644 index 000000000..20a06e076 --- /dev/null +++ b/integrations/canvaslink/tests/settings-host.test.cjs @@ -0,0 +1,26 @@ +const {test}=require('node:test'),assert=require('node:assert/strict'); +const {installSettingsHost}=require('../host/settings-host.cjs'); +function fixture(command=async action=>action){ + const handlers=new Map(),mainFrame={},contents={mainFrame},target={webContents:contents,isDestroyed:()=>false}; + installSettingsHost({ipcMain:{handle:(name,handler)=>handlers.set(name,handler)},getWindow:()=>target,snapshot:()=>({enabled:false}),command}); + return {handlers,target,event:{sender:contents,senderFrame:mainFrame}}; +} +test('Settings state and commands reject other renderers, child frames and unknown actions',()=>{ + const f=fixture();for(const channel of ['canvaslink-settings:state','canvaslink-settings:command']){ + for(const event of [{sender:{},senderFrame:f.event.senderFrame},{sender:f.event.sender,senderFrame:{}}])assert.throws(()=>f.handlers.get(channel)(event,'enable'),/untrusted-settings-renderer/); + } + assert.throws(()=>f.handlers.get('canvaslink-settings:command')(f.event,'shell'),/invalid-settings-command/); + assert.deepEqual(f.handlers.get('canvaslink-settings:state')(f.event),{enabled:false}); +}); +test('Settings operations are serialized and a failed enable does not block later commands',async()=>{ + const order=[];let release;const gate=new Promise(resolve=>release=resolve); + const f=fixture(async action=>{order.push(action);if(action==='enable'){await gate;throw Error('bind failed');}return action;}); + const command=f.handlers.get('canvaslink-settings:command');const first=command(f.event,'enable'),second=command(f.event,'disable'); + await new Promise(resolve=>setImmediate(resolve));assert.deepEqual(order,['enable']);release();await assert.rejects(first,/bind failed/);assert.equal(await second,'disable');assert.deepEqual(order,['enable','disable']); +}); +test('A queued settings operation cannot execute after its owning window closes',async()=>{ + let release;const gate=new Promise(resolve=>release=resolve),order=[]; + const f=fixture(async action=>{order.push(action);if(action==='enable')await gate;});const command=f.handlers.get('canvaslink-settings:command'); + const first=command(f.event,'enable'),second=command(f.event,'disable');await new Promise(resolve=>setImmediate(resolve)); + f.target.isDestroyed=()=>true;release();await first;await assert.rejects(second,/untrusted-settings-renderer/);assert.deepEqual(order,['enable']); +}); diff --git a/integrations/canvaslink/tests/settings-touch.test.cjs b/integrations/canvaslink/tests/settings-touch.test.cjs new file mode 100644 index 000000000..6fce267b5 --- /dev/null +++ b/integrations/canvaslink/tests/settings-touch.test.cjs @@ -0,0 +1,39 @@ +const {test}=require('node:test'); +const assert=require('node:assert/strict'); +const {openUI}=require('../scripts/ui-test-harness.cjs'); +const {focusScript}=require('../host/focus.cjs'); +test('Only actual text inputs are editable; Settings controls do not summon a text editor',async t=>{ + const ui=await openUI();t.after(()=>ui.browser.close()); + await ui.page.setContent('
x
'); + for(const [id,editable]of Object.entries({range:false,check:false,color:false,file:false,select:false,button:false,text:true,password:true,area:true,readonly:false,rich:true})){ + await ui.page.locator('#'+id).focus();const value=await ui.page.evaluate(focusScript);assert.equal(value.editable,editable,id);assert.equal(value.surface,'settings');assert.ok(value.dragRects.length===1); + } +}); +test('Screen arrows, Enter, Esc and Ctrl work after selecting a nontext control',async t=>{ + const ui=await openUI({connected:true,touch:true,width:390,height:844});t.after(()=>ui.browser.close()); + await ui.page.evaluate(()=>__uiFixture.configure({focus:{kind:'control',editable:false,surface:'settings'}}));await ui.page.locator('#viewport').click(); + assert.equal(await ui.page.evaluate(()=>CanvasLinkInput.active()),false); + for(const key of ['ArrowDown','Enter','Escape'])await ui.page.locator('[data-key='+key+']').click(); + await ui.page.locator('#ctrl').click();await ui.page.locator('[data-key=c]').click(); + const keys=await ui.page.evaluate(()=>__uiFixture.calls.filter(c=>c.type==='input'&&c.input.type==='key').map(c=>c.input)); + assert.deepEqual(keys.map(c=>c.key),['ArrowDown','Enter','Escape','c']);assert.deepEqual(keys[3].modifiers,['control']); + assert.equal(await ui.page.evaluate(()=>__uiFixture.calls.filter(c=>c.type==='show-keyboard').length),0); +}); +test('Paste remains accessible without horizontal scrolling on a narrow phone',async t=>{ + const ui=await openUI({connected:true,width:320,height:640});t.after(()=>ui.browser.close()); + const r=await ui.page.locator('#paste').boundingBox();assert.ok(r.x>=0&&r.x+r.width<=320);assert.match(await ui.page.locator('#paste').getAttribute('aria-label'),/Вставить.*без Enter/); + await ui.page.locator('#paste').click();assert.match(await ui.page.locator('#input-status').textContent(),/коснитесь.*поля/);assert.equal(await ui.page.evaluate(()=>__uiFixture.calls.filter(c=>c.type==='input').length),0); +}); +test('A Settings text field preserves context instead of terminal-style zoom',async t=>{ + const ui=await openUI({connected:true,touch:true});t.after(()=>ui.browser.close()); + await ui.page.evaluate(()=>__uiFixture.configure({focus:{editable:true,kind:'text-field',surface:'settings',rect:{x:.7,y:.5,width:.08,height:.03}}})); + await ui.page.locator('#viewport').click();const zoom=parseInt(await ui.page.locator('#zoom-label').textContent());assert.ok(zoom>=100&&zoom<=400);assert.equal(await ui.page.evaluate(()=>CanvasLinkInput.active()),true); +}); +test('Text fields remain readable with IME and restore overview when editing ends',async t=>{ + const ui=await openUI({connected:true,touch:true,width:1280,height:800});t.after(()=>ui.browser.close()); + await ui.page.evaluate(()=>__uiFixture.configure({focus:{editable:true,kind:'text-field',surface:'settings',fontSize:12,rect:{x:.35,y:.68,width:.21,height:.033},contextRect:{x:.34,y:.67,width:.45,height:.14}}})); + await ui.page.locator('#viewport').click();await ui.page.setViewportSize({width:1280,height:320});await ui.page.waitForTimeout(100); + const geometry=await ui.page.evaluate(()=>{const v=document.getElementById('video'),r=v.getBoundingClientRect();return {font:12*Math.min(r.width/v.videoWidth,r.height/v.videoHeight),scale:new DOMMatrix(getComputedStyle(v).transform).a};}); + assert.ok(geometry.font>=12,JSON.stringify(geometry));assert.ok(geometry.scale<=4); + await ui.page.evaluate(()=>CanvasLinkInput.stop());assert.equal(await ui.page.locator('#zoom-label').textContent(),'100%'); +}); diff --git a/integrations/canvaslink/tests/transport-recovery.test.cjs b/integrations/canvaslink/tests/transport-recovery.test.cjs new file mode 100644 index 000000000..6aa280881 --- /dev/null +++ b/integrations/canvaslink/tests/transport-recovery.test.cjs @@ -0,0 +1,106 @@ +const {test}=require('node:test'),assert=require('node:assert/strict'),vm=require('node:vm'),fs=require('node:fs'),path=require('node:path'); +const source=fs.readFileSync(path.join(__dirname,'../android/assets/transport.js'),'utf8'); +function client({remoteDescription}={}){ + const signals=[],sent=[],peers=[],intervals=[],events=new Map(),frames=new Map(),timers=new Set();let frameId=0; + class Peer{ + constructor(){peers.push(this);this.connectionState='new';this.iceGatheringState='complete';} + async setRemoteDescription(value){if(remoteDescription)await remoteDescription(value);} async createAnswer(){return {type:'answer',sdp:'local'};} async setLocalDescription(value){this.localDescription=value;} close(){this.closed=true;} + } + const context={window:{},document:{hidden:false,addEventListener:(type,handler)=>events.set(type,handler)},Native:{signal:json=>signals.push(JSON.parse(json)),disconnect(){},diagnostics(){},discover(){},restore(){},loadDraft(){}},CanvasLinkDelivery:{Delivery:class{close(){}send(value){sent.push(value);return Promise.resolve();}receive(){}}},RTCPeerConnection:Peer,setInterval:fn=>intervals.push(fn),setTimeout(fn,delay){const timer={fn,delay};timers.add(timer);return timer;},clearTimeout:timer=>timers.delete(timer),requestAnimationFrame:fn=>{frames.set(++frameId,fn);return frameId;},cancelAnimationFrame:id=>frames.delete(id),performance:{now:()=>1}}; + vm.runInNewContext(source,context);return {...context,signals,sent,peers,intervals,events,frames,timers,receive:context.window.CanvasLink.receive,api:context.window.CanvasLinkClient}; +} +test('Successful encrypted polling after an outage retries signalling without replaying input',()=>{ + const c=client();c.receive({type:'paired',value:{host:'192.168.2.22:3497'}});c.receive({type:'network',value:'offline'}); + c.receive({type:'network-restored',value:true});assert.deepEqual(c.signals,[{type:'ping'}]);assert.deepEqual(c.sent,[]); +}); +test('Network recovery cannot resume an unpaired or background client',()=>{ + const c=client();c.receive({type:'network-restored',value:true});c.receive({type:'paired',value:{host:'local'}});c.document.hidden=true;c.receive({type:'network-restored',value:true});assert.deepEqual(c.signals,[]); +}); +test('An existing open data channel that recovers clears reconnecting status',async()=>{ + const c=client();c.receive({type:'paired',value:{host:'local'}});c.receive({type:'signal',value:{type:'offer',sdp:'remote'}});await new Promise(resolve=>setImmediate(resolve)); + const p=c.peers[0],channel={readyState:'open'};p.ondatachannel({channel});channel.onopen();c.receive({type:'network',value:'offline'});assert.equal(c.api.getState().connection,'reconnecting'); + p.connectionState='connected';p.onconnectionstatechange();assert.equal(c.api.getState().connection,'connected');assert.equal(c.api.getState().error,''); +}); +test('Closing a peer clears its video counters and target status before reconnect',async()=>{ + const c=client();c.receive({type:'paired',value:{host:'local'}});c.receive({type:'signal',value:{type:'offer',sdp:'remote'}});await new Promise(resolve=>setImmediate(resolve)); + const peer=c.peers[0],channel={readyState:'open',send(){}};peer.ondatachannel({channel});channel.onopen(); + channel.onmessage({data:JSON.stringify({type:'source-state',capture:'active',windowFocused:true,target:'terminal'})}); + peer.getStats=async()=>new Map([['video',{type:'inbound-rtp',kind:'video',framesDecoded:17}]]);await c.intervals[0](); + assert.equal(c.api.getState().stats.framesDecoded,17);assert.equal(c.api.getState().source.target,'terminal'); + c.receive({type:'signal',value:{type:'disconnect'}}); + assert.equal(c.api.getState().source,null);assert.equal(Object.keys(c.api.getState().stats).length,0); + assert.equal(Object.keys(c.window.CanvasLink.stats).length,0); +}); +test('Video feedback reports zero before decoding and the current peer frame count after decoding',async()=>{ + const c=client();c.receive({type:'paired',value:{host:'local'}});c.receive({type:'signal',value:{type:'offer',sdp:'remote'}});await new Promise(resolve=>setImmediate(resolve)); + const packets=[],p=c.peers[0],channel={readyState:'open',bufferedAmount:0,send:s=>packets.push(JSON.parse(s))};p.ondatachannel({channel});channel.onopen(); + p.getStats=async()=>new Map([['v',{type:'inbound-rtp',kind:'video',framesDecoded:7}]]); + await c.intervals[0]();await c.intervals[0]();assert.equal(packets[0].videoFrames,0);assert.equal(packets[1].videoFrames,7); + c.receive({type:'signal',value:{type:'offer',sdp:'new'}});await new Promise(resolve=>setImmediate(resolve)); + const fresh=c.peers[1];fresh.ondatachannel({channel});fresh.getStats=async()=>new Map();await c.intervals[0]();assert.equal(packets.at(-1).videoFrames,0); +}); +test('Late offers cannot reopen an unpaired, explicitly disconnected or hidden client',async()=>{ + for(const status of ['unpaired','disconnected','hidden']){ + const c=client();if(status!=='unpaired')c.receive({type:'paired',value:{host:'local'}}); + if(status==='disconnected')c.api.disconnect(); + if(status==='hidden'){c.document.hidden=true;c.events.get('visibilitychange')();} + const before=c.api.getState().connection; + c.receive({type:'signal',value:{type:'offer',sdp:'late'}});await new Promise(resolve=>setImmediate(resolve)); + assert.equal(c.peers.length,0,status);assert.equal(c.api.getState().connection,before); + c.receive({type:'signal',value:{type:'disconnect'}});assert.equal(c.api.getState().connection,before); + } +}); +test('A rejected old negotiation cannot replace the new connected peer status',async()=>{ + let rejectOld;const c=client({remoteDescription:value=>value.sdp==='old'?new Promise((resolve,reject)=>rejectOld=reject):undefined}); + c.receive({type:'paired',value:{host:'local'}});c.receive({type:'signal',value:{type:'offer',sdp:'old'}}); + c.receive({type:'signal',value:{type:'offer',sdp:'new'}});await new Promise(resolve=>setImmediate(resolve)); + const fresh=c.peers[1],channel={readyState:'open'};fresh.ondatachannel({channel});channel.onopen(); + rejectOld(Error('old-peer-closed'));await new Promise(resolve=>setImmediate(resolve)); + assert.equal(c.api.getState().connection,'connected');assert.equal(c.api.getState().error,'');assert.equal(fresh.closed,undefined); + assert.equal(c.signals.filter(value=>value.type==='answer').length,1); +}); +test('Closing a session discards buffered pointer motion before any new peer',()=>{ + const c=client();c.receive({type:'paired',value:{host:'local'}}); + c.api.sendInput({type:'pointer',action:'move',x:.5,y:.5,button:0});assert.equal(c.frames.size,1); + c.api.disconnect();for(const fn of c.frames.values())fn(); + assert.deepEqual(c.sent,[]);assert.equal(c.frames.size,0); +}); +test('A delayed native pairing callback cannot undo explicit disconnect',async()=>{ + const c=client();c.api.disconnect(); + c.receive({type:'paired',value:{host:'old-host'}});c.receive({type:'signal',value:{type:'offer',sdp:'old'}}); + await new Promise(resolve=>setImmediate(resolve));assert.equal(c.peers.length,0);assert.equal(c.api.getState().connection,'idle'); +}); +test('A negotiation completing after disconnect sends no answer',async()=>{ + let resolveOld;const c=client({remoteDescription:()=>new Promise(resolve=>resolveOld=resolve)}); + c.receive({type:'paired',value:{host:'local'}});c.receive({type:'signal',value:{type:'offer',sdp:'old'}}); + c.api.disconnect();resolveOld();await new Promise(resolve=>setImmediate(resolve)); + assert.deepEqual(c.signals,[]);assert.equal(c.api.getState().connection,'idle'); +}); + +test('A connection deadline rejects late offers until an explicit retry',async()=>{ + const c=client();c.receive({type:'paired',value:{host:'local'}}); + const deadline=[...c.timers][0];assert.equal(deadline.delay,30000); + c.timers.delete(deadline);deadline.fn(); + assert.equal(c.api.getState().connection,'reconnecting');assert.equal(c.api.getState().error,'lan-connection-timeout'); + c.receive({type:'signal',value:{type:'offer',sdp:'late'}});await new Promise(resolve=>setImmediate(resolve)); + assert.equal(c.peers.length,0); + c.receive({type:'signal',value:{type:'disconnect'}});assert.equal(c.api.getState().error,'lan-connection-timeout'); + c.api.resume();assert.deepEqual(c.signals,[{type:'ping'}]);assert.equal(c.timers.size,1); +}); +test('Replacing an offer retains the original deadline and connecting clears it',async()=>{ + const c=client();c.receive({type:'paired',value:{host:'local'}});const deadline=[...c.timers][0]; + c.receive({type:'signal',value:{type:'offer',sdp:'first'}});await new Promise(resolve=>setImmediate(resolve)); + c.receive({type:'signal',value:{type:'offer',sdp:'second'}});await new Promise(resolve=>setImmediate(resolve)); + assert.equal(c.timers.size,1);assert.equal([...c.timers][0],deadline); + c.peers[1].ondatachannel({channel:{readyState:'open'}});c.peers[1].connectionState='connected';c.peers[1].onconnectionstatechange(); + assert.equal(c.api.getState().connection,'connected');assert.equal(c.timers.size,0); +}); +test('A LAN network change closes the old peer and discards buffered pointer input',async()=>{ + const c=client();c.receive({type:'paired',value:{host:'local'}}); + c.receive({type:'signal',value:{type:'offer',sdp:'first'}});await new Promise(resolve=>setImmediate(resolve)); + c.api.sendInput({type:'pointer',action:'move',x:.5,y:.5,button:0}); + c.receive({type:'network-changed',value:'offline'}); + assert.equal(c.peers[0].closed,true);assert.equal(c.frames.size,0);assert.equal(c.signals.filter(s=>s.type==='ping').length,0); + c.receive({type:'network-changed',value:'ready'});assert.equal(c.signals.filter(s=>s.type==='ping').length,1); + assert.deepEqual(c.sent,[]); +}); diff --git a/integrations/canvaslink/verification/icon-preview.cjs b/integrations/canvaslink/verification/icon-preview.cjs new file mode 100644 index 000000000..ba5aa6f52 --- /dev/null +++ b/integrations/canvaslink/verification/icon-preview.cjs @@ -0,0 +1,128 @@ +'use strict'; +// SPDX-License-Identifier: MIT +// Offline renderer. Uses only the supplied installed sharp; does not start a browser or device. +const fs = require('node:fs'); +const path = require('node:path'); +const crypto = require('node:crypto'); +const { spawnSync } = require('node:child_process'); +const sharp = require(process.env.CANVASLINK_SHARP || 'sharp'); +const root = path.resolve(__dirname, '..'); +const out = __dirname; +const geometrySource = 'android/res/drawable/ic_launcher_foreground.xml'; +const read = file => fs.readFileSync(path.join(root, file), 'utf8'); +const sha256 = bytes => crypto.createHash('sha256').update(bytes).digest('hex'); + +function parse(text) { + const result = spawnSync('python3', ['-c', + 'import sys,json,xml.etree.ElementTree as E\nr=E.fromstring(sys.stdin.read())\nprint(json.dumps([{"tag":e.tag,"attrs":e.attrib,"text":e.text} for e in r.iter()]))'], + { input: text, encoding: 'utf8' }); + if (result.status !== 0) throw new Error(result.stderr); + return JSON.parse(result.stdout); +} + +const android = name => `{http://schemas.android.com/apk/res/android}${name}`; +const rails = parse(read(geometrySource)).filter(n => n.tag === 'path').map(n => ({ + fill: n.attrs[android('fillColor')], d: n.attrs[android('pathData')] +})); +const background = parse(read('android/res/values/colors.xml')).find(n => n.attrs.name === 'icon_background').text.trim(); +const pathTags = (tint = null) => rails.map(p => ``).join(''); + +function maskShape(mask) { + if (mask === 'circle') return ''; + if (mask === 'square') return ''; + if (mask !== 'squircle') throw new Error(`Unknown mask: ${mask}`); + const points = []; + for (let i = 0; i <= 256; i++) { + const t = i / 256 * Math.PI * 2, c = Math.cos(t), s = Math.sin(t); + const x = 54 + 36 * Math.sign(c) * Math.sqrt(Math.abs(c)); + const y = 54 + 36 * Math.sign(s) * Math.sqrt(Math.abs(s)); + points.push(`${i ? 'L' : 'M'}${x.toFixed(6)} ${y.toFixed(6)}`); + } + return ``; +} + +function maskedSvg(size, mask, tint = null, bg = background) { + return Buffer.from(`${maskShape(mask)}${pathTags(tint)}`); +} + +async function safeZoneMetrics() { + const image = Buffer.from(`${pathTags()}`); + const { data, info } = await sharp(image).ensureAlpha().raw().toBuffer({ resolveWithObject: true }); + let radius = 0, minX = 108, minY = 108, maxX = 0, maxY = 0; + for (let y = 0; y < info.height; y++) for (let x = 0; x < info.width; x++) { + if (data[(y * info.width + x) * 4 + 3] < 128) continue; + const px = (x + .5) / 10, py = (y + .5) / 10; + radius = Math.max(radius, Math.hypot(px - 54, py - 54)); + minX = Math.min(minX, px); maxX = Math.max(maxX, px); + minY = Math.min(minY, py); maxY = Math.max(maxY, py); + } + return { samplePixelsPerDp: 10, alphaThreshold: 128, maxRadiusDp: +radius.toFixed(4), safeRadiusDp: 33, + boundsDp: { minX, minY, maxX, maxY } }; +} + +async function main() { + const previews = []; + const images = new Map(); + for (const size of [24, 32, 48, 64, 128, 512]) for (const mask of ['square', 'circle', 'squircle']) { + const png = await sharp(maskedSvg(size, mask)).png().toBuffer(); + const file = `verification/icon-${mask}-${size}.png`; + fs.writeFileSync(path.join(root, file), png); + images.set(`${mask}-${size}`, png); + previews.push({ file, mask, width: size, height: size, sha256: sha256(png) }); + } + const themed = await sharp(maskedSvg(128, 'circle', '#213216', '#d6ef88')).png().toBuffer(); + fs.writeFileSync(path.join(out, 'icon-monochrome-128.png'), themed); + const sourcePreview = await sharp(path.join(root, 'design/canvaslink-icon.svg')).resize(512, 512).png().toBuffer(); + fs.writeFileSync(path.join(out, 'icon-source-square-512.png'), sourcePreview); + const safeSvg = Buffer.from(`${pathTags()}`); + const safeZone = await sharp(safeSvg).png().toBuffer(); + fs.writeFileSync(path.join(out, 'icon-safe-zone.png'), safeZone); + + const labels = []; + const overlays = []; + const text = (x, y, value, size = 16, color = '#95a592', weight = 400) => `${value}`; + labels.push(text(60, 65, 'CanvasLink', 36, '#edf1e6', 600)); + labels.push(text(60, 101, 'LINKED WINDOWS / ORIGINAL VECTOR MARK', 13, '#a9bc9c', 500)); + for (const [index, mask] of ['square', 'circle', 'squircle'].entries()) { + const left = 60 + index * 416; + labels.push(text(left, 145, `${String(index + 1).padStart(2, '0')} / ${mask.toUpperCase()}`, 13, '#d6ef88', 600)); + overlays.push({ input: await sharp(images.get(`${mask}-512`)).resize(320, 320).toBuffer(), left: left + 16, top: 170 }); + labels.push(text(left, 530, mask === 'square' ? 'Remote window + touch surface' : mask === 'circle' ? 'Circle mask / no critical clipping' : 'Exponent-4 squircle / no clipping', 15, '#edf1e6')); + labels.push(text(left, 621, 'SMALL-SIZE CHECK / 1x', 12, '#a9bc9c', 500)); + for (const [i, size] of [24, 32, 48, 64].entries()) { + const center = left + [36, 104, 188, 292][i]; + overlays.push({ input: images.get(`${mask}-${size}`), left: center - size / 2, top: 688 - size / 2 }); + labels.push(text(center - 9, 750, String(size), 13)); + } + } + labels.push(''); + overlays.push({ input: themed, left: 60, top: 844 }); + labels.push(text(216, 878, 'API 33+ / THEMED SILHOUETTE', 13, '#d6ef88', 600)); + labels.push(text(216, 910, 'Same geometry; launcher chooses the tint.', 16, '#edf1e6')); + labels.push(text(216, 940, 'Example colors, not a device capture.', 13)); + overlays.push({ input: safeZone, left: 1044, top: 819 }); + labels.push(text(696, 873, '108dp layer / 72dp viewport', 16, '#edf1e6')); + labels.push(text(696, 905, '66dp critical safe circle', 16, '#d6ef88')); + labels.push(text(696, 937, 'Filled rails / no text / no effects', 13)); + labels.push(text(60, 1039, 'OFFLINE ANDROID-VECTOR RENDERS / 24–512px / NO DEVICE INSTALL', 12)); + const sheetSvg = Buffer.from(`${labels.join('')}`); + const sheet = await sharp(sheetSvg).composite(overlays).png().toBuffer(); + fs.writeFileSync(path.join(out, 'icon-contact-sheet.png'), sheet); + const manifest = { + geometrySource, + sourceSvg: 'design/canvaslink-icon.svg', + sourceSha256: sha256(fs.readFileSync(path.join(root, 'design/canvaslink-icon.svg'))), + vectorSha256: sha256(fs.readFileSync(path.join(root, geometrySource))), + adaptiveViewport: { x: 18, y: 18, size: 72, layerSize: 108 }, + safeZone: await safeZoneMetrics(), + maskNote: 'Circle and exponent-4 superellipse are geometric preview masks, not OEM launcher captures.', + license: 'MIT; original project-specific geometry, no third-party artwork or fonts embedded.', + renderer: { sharp: sharp.versions.sharp, librsvg: sharp.versions.rsvg }, + previews, + contactSheet: { file: 'verification/icon-contact-sheet.png', width: 1320, height: 1080, sha256: sha256(sheet) } + }; + fs.writeFileSync(path.join(out, 'icon-previews.json'), JSON.stringify(manifest, null, 2) + '\n'); + console.log(JSON.stringify({ previews: previews.length, sheet: manifest.contactSheet.file, safeZone: manifest.safeZone })); +} + +main().catch(error => { console.error(error.message); process.exitCode = 1; }); diff --git a/package-lock.json b/package-lock.json index d8b21c057..fa8a1283e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,19 +1,21 @@ { "name": "canvastty", - "version": "1.8.1", + "version": "1.8.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "canvastty", - "version": "1.8.1", + "version": "1.8.3", "hasInstallScript": true, "license": "MIT", "workspaces": [ - "integrations/even-g2" + "integrations/even-g2", + "integrations/canvaslink" ], "dependencies": { "@xterm/headless": "6.0.0", + "bonjour-service": "1.3.0", "electron-updater": "6.8.9", "hast-util-to-jsx-runtime": "2.3.6", "lowlight": "3.3.0", @@ -37,6 +39,7 @@ "@xterm/addon-web-links": "0.12.0", "@xterm/addon-webgl": "0.19.0", "@xterm/xterm": "6.0.0", + "ascii.rest": "0.4.0", "electron": "43.7.7", "electron-builder": "26.15.3", "electron-vite": "5.0.0", @@ -48,6 +51,19 @@ "vite": "7.3.6" } }, + "integrations/canvaslink": { + "name": "@canvastty/canvaslink", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "bonjour-service": "1.3.0", + "secure-remote-password": "0.3.1" + }, + "devDependencies": { + "playwright": "1.64.0", + "sharp": "0.35.5" + } + }, "integrations/even-g2": { "name": "canvastty-even-g2", "version": "0.5.6", @@ -527,6 +543,10 @@ "node": ">=6.9.0" } }, + "node_modules/@canvastty/canvaslink": { + "resolved": "integrations/canvaslink", + "link": true + }, "node_modules/@electron-internal/extract-zip": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@electron-internal/extract-zip/-/extract-zip-1.0.5.tgz", @@ -844,6 +864,17 @@ "node": ">=14.14" } }, + "node_modules/@emnapi/runtime": { + "version": "1.11.3", + "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.11.3.tgz", + "integrity": "sha512-Xz4Tpyki7XyrpbUK1jR1AhdAdaXyhhY4lZ3neLodmhpuWfy2PAQN5B46sAiU4liOXGLkHypn/qU+jvfWSCYYLA==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, "node_modules/@esbuild/aix-ppc64": { "version": "0.25.12", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.12.tgz", @@ -1313,6 +1344,533 @@ "url": "https://github.com/sponsors/ayuhito" } }, + "node_modules/@img/colour": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@img/colour/-/colour-1.1.0.tgz", + "integrity": "sha512-Td76q7j57o/tLVdgS746cYARfSyxk8iEfRxewL9h4OMzYhbW4TAcppl0mT4eyqXddh6L/jwoM75mo7ixa/pCeQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/@img/sharp-darwin-arm64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-darwin-arm64/-/sharp-darwin-arm64-0.35.5.tgz", + "integrity": "sha512-QRUlFQ0WxvdWyqqG/WtI3iupfD5rBzmCHXSdPsY91sAtVtTo7Q4cb6zOccZ3gqEqkr0f1As1ehLqmEpDsRf+lg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-darwin-arm64": "1.3.4" + } + }, + "node_modules/@img/sharp-darwin-x64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-darwin-x64/-/sharp-darwin-x64-0.35.5.tgz", + "integrity": "sha512-+BR255RhDlpygUpOc/Jdt1nT6DQ3XG/ERo5wbcdOf5Q320dKtPCKPLR1LJs9VGXRaMa8l1uUa0tkCNOXiAxZUw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-darwin-x64": "1.3.4" + } + }, + "node_modules/@img/sharp-freebsd-wasm32": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-freebsd-wasm32/-/sharp-freebsd-wasm32-0.35.5.tgz", + "integrity": "sha512-Y/z91nEZ4uIBX5X3nfTovjU9lHNKFYbL2lpHCLVNmXQK03VIZvXBBt0KxbPGp2SdGSF+2mQU4e+hQaWOt86iAw==", + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "dependencies": { + "@img/sharp-wasm32": "0.35.5" + }, + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-darwin-arm64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-arm64/-/sharp-libvips-darwin-arm64-1.3.4.tgz", + "integrity": "sha512-5R89nBYiRdUlSWJxPhO+GVtaXzXSxKnRu/xqMn3KTA3L9EB9Oy/P+Nn2f2vlhPuUdy/Zusb2DarbyTpGCfEDuw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "darwin" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-darwin-x64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-x64/-/sharp-libvips-darwin-x64-1.3.4.tgz", + "integrity": "sha512-iR2OKH80yi0U+dUplyh3/xdpFvps6YkCwsXenIJxqxR1v9o+xtKTGbS9H7cps+2Vxjc8B1j96p75NmTGjIhtpQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "darwin" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-arm": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm/-/sharp-libvips-linux-arm-1.3.4.tgz", + "integrity": "sha512-LmRtTsOHuvM2+wlO2Db37dx5MiZhB0FvSunciw48YjdOkZz9KAiRbm8ujeMOA1INqmei5NapFxYEK1D1ZSidmw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-arm64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm64/-/sharp-libvips-linux-arm64-1.3.4.tgz", + "integrity": "sha512-Y3dgX/6lE2QhQb+Gxy0WZxfg9MEm/JBjamZpS2IklP7xIQoKN4hzAm7KcMVGtaVDt3neE9OKBC7vAfonA/Lr1A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-ppc64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-ppc64/-/sharp-libvips-linux-ppc64-1.3.4.tgz", + "integrity": "sha512-Le6boB8Tai0Nis+gIxIpKx68UDVVIqdR8Tin5Yf1z2LJJQLDJvCDRqRu+jC2qCoD+eIomonmOwB4smBRxfVpYQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-riscv64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-riscv64/-/sharp-libvips-linux-riscv64-1.3.4.tgz", + "integrity": "sha512-aHkkIEHPRdQEegJN20MLmGtxYD9R2wQr3Cwpddnu5+YKMt6Uzax7S9h5gpZTo8wyrGuZSlfQ63OevL5mTyOC7Q==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-s390x": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-s390x/-/sharp-libvips-linux-s390x-1.3.4.tgz", + "integrity": "sha512-ra/mB6MikESDUO7Yg+Mi95bFBb9GsObURuhnOv3OqknjGe9sZrG8tCe9q0xSIGrtLgvgw0gKnFWcK4blSgQOuQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-x64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-x64/-/sharp-libvips-linux-x64-1.3.4.tgz", + "integrity": "sha512-GJ//SSXbnwSDes02umB3nDJLFcQzw8a18V8fyhqr6tV515tOEMdImjjxj1AoafMRz56F3PHgftnj1QEKSU1zkw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linuxmusl-arm64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-arm64/-/sharp-libvips-linuxmusl-arm64-1.3.4.tgz", + "integrity": "sha512-hvulFwtjUcagsis6BBxHwGFwWoNZjgYmULGVrZcyfNbjA8hKILbRxGg15/7w5HDyXHXUos/j6baAWqnCyQ2DWA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linuxmusl-x64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-x64/-/sharp-libvips-linuxmusl-x64-1.3.4.tgz", + "integrity": "sha512-6zXKeE/p39I1AmA3cJG35eyBGNqNddLnUXjhwBnsGjFPWqf5VKkDBEqaEkPDoTEtkxwi2vv8Tcr2mDyP4So7Fg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-linux-arm": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm/-/sharp-linux-arm-0.35.5.tgz", + "integrity": "sha512-LEaXK2WdXVK5ykcw0buWyPMsmLLL2vpHLD6yrNSW+JGEL3BZPA4tpKN6iaMc4AxTTAoaX/sU1rOL51lcIz48ZQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-arm64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm64/-/sharp-linux-arm64-0.35.5.tgz", + "integrity": "sha512-LYVx5JTsOM2CBzmxreh+nl64/3H6Xb09iSLknqH47z2T2DFFxDeFLP5y4dJwe6H7uGQlHPyEEtIqyo3DYsRwdQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm64": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-ppc64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-ppc64/-/sharp-linux-ppc64-0.35.5.tgz", + "integrity": "sha512-QVxAAq8evVRI9ia2vqgwrmWucn5Dfv+JdWzj75pD8omHLPSP7f8p20O8jxzjCcuCEQEOtYOZUmX1hkiZ0kdevA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-ppc64": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-riscv64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-riscv64/-/sharp-linux-riscv64-0.35.5.tgz", + "integrity": "sha512-LtdreXguaavKODPIfzJ4kffx7UNt1omwtK0rch4EBbbSTXPnxWmYSayXdLJw0fJzQ97kHt1gL/yh4tvU+nCyRQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-riscv64": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-s390x": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-s390x/-/sharp-linux-s390x-0.35.5.tgz", + "integrity": "sha512-UZasTOFiYzotTsGOCu42BfUzP6Tu6Do/947iRm1RsLKvlllxwGcn4RN27LibGWceix4Y+Pmw3jsnTcCQIgWjqA==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-s390x": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-x64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-x64/-/sharp-linux-x64-0.35.5.tgz", + "integrity": "sha512-SxFtLTeJInhAA9Q836kux2vZNeOBQEx658qvbboZScr0wIARym3IcGmW7KpVD5sbVg0Ojy+udFQdayYIZyoNog==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-x64": "1.3.4" + } + }, + "node_modules/@img/sharp-linuxmusl-arm64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-arm64/-/sharp-linuxmusl-arm64-0.35.5.tgz", + "integrity": "sha512-9HbMclmI1zlNkFRs3z9/eBtDjfD0sGlrX1z6b1qwmiFY5ElDLh4BC0LPBdVp7z1DXFiKlIcznf+ZlsuZzLxQqg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-arm64": "1.3.4" + } + }, + "node_modules/@img/sharp-linuxmusl-x64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-x64/-/sharp-linuxmusl-x64-0.35.5.tgz", + "integrity": "sha512-4KOphqB035HrVdqLZfCgMzzERrQkkzOwRhl4OAkRO1YCldbaFjySXMaK534Mo0V+LndnlJk+sbUyLeU0ULyD1A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-x64": "1.3.4" + } + }, + "node_modules/@img/sharp-wasm32": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-wasm32/-/sharp-wasm32-0.35.5.tgz", + "integrity": "sha512-Ptsga1su4tQx+LLF1ECS9U6nz5kmrXKo6XVbtR48Ke3ZRxxgaWBu7IDtEe1quo8hiupwm6WFqxVlXaSf7IINGQ==", + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later AND MIT", + "optional": true, + "dependencies": { + "@emnapi/runtime": "^1.11.3" + }, + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-webcontainers-wasm32": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-webcontainers-wasm32/-/sharp-webcontainers-wasm32-0.35.5.tgz", + "integrity": "sha512-hfhF/FmoQyTUkA0bIKFOtw536BQSeBMe6BF6QyWlrPxT754+TFLaZ7sKKTfvvM0yJgKgaYTwnFCIZ/GuDw5SUA==", + "cpu": [ + "wasm32" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "dependencies": { + "@img/sharp-wasm32": "0.35.5" + }, + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-arm64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-arm64/-/sharp-win32-arm64-0.35.5.tgz", + "integrity": "sha512-X4t7g+7ZA5DKblCBEXGjUqqemj4vczING/5viFwAL8h4N3qYeyjwdCvRLHi4EdOUI+2Z7UFlp1VM+p/AuEtm6Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-ia32": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-ia32/-/sharp-win32-ia32-0.35.5.tgz", + "integrity": "sha512-5Zm82LoBc43nhwNybZlG7Y1KO//Zhsn306fQl29ZOuStHLGTo3BWL83q3cznX0poxSAMuYL1On/BHBxkBeKr6A==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-x64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-x64/-/sharp-win32-x64-0.35.5.tgz", + "integrity": "sha512-x76eH0vEiHlcMQu8Y8IenntaACtddpT6W0wmXtWrnKcnKI7ME5DdgqhAD6SEWOEl1v2zDvkZDhFA9KnURwpfqg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, "node_modules/@inquirer/ansi": { "version": "2.0.8", "resolved": "https://registry.npmjs.org/@inquirer/ansi/-/ansi-2.0.8.tgz", @@ -2133,6 +2691,12 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@leichtgewicht/ip-codec": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@leichtgewicht/ip-codec/-/ip-codec-2.0.5.tgz", + "integrity": "sha512-Vo+PSpZG2/fmgmiNzYK9qWRh8h/CHrwD0mo1h1DzL4yzHNSfWYujGTYsWGreD000gcgmZ7K4Ys6Tx9TxtsKdDw==", + "license": "MIT" + }, "node_modules/@malept/cross-spawn-promise": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@malept/cross-spawn-promise/-/cross-spawn-promise-2.0.0.tgz", @@ -3791,6 +4355,24 @@ "integrity": "sha512-arycdkxgK1cj6s03GDb96tlCxOl1n3kg9M2OHseUc6Pqyqp+lgfceFPmG507eI5V+oxOSEnlOw/dFc7LXBXF4Q==", "license": "MIT" }, + "node_modules/ascii.rest": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/ascii.rest/-/ascii.rest-0.4.0.tgz", + "integrity": "sha512-lqKcesbm3Ij5ENvgs/CTRWF3HrjAogOoMJdqOh8rlRmdzG3uRSyqS/3chfnjJfcIeqj69MWI8+NstMZ/Br9Ivw==", + "dev": true, + "license": "MIT", + "bin": { + "ascii.rest": "dist/cli.js" + }, + "peerDependencies": { + "react": ">=18" + }, + "peerDependenciesMeta": { + "react": { + "optional": true + } + } + }, "node_modules/asn1js": { "version": "3.0.10", "resolved": "https://registry.npmjs.org/asn1js/-/asn1js-3.0.10.tgz", @@ -3907,6 +4489,16 @@ "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==", "license": "MIT" }, + "node_modules/bonjour-service": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/bonjour-service/-/bonjour-service-1.3.0.tgz", + "integrity": "sha512-3YuAUiSkWykd+2Azjgyxei8OWf8thdn8AITIog2M4UICzoqfjlqr64WIjEXZllf/W6vK1goqleSR6brGomxQqA==", + "license": "MIT", + "dependencies": { + "fast-deep-equal": "^3.1.3", + "multicast-dns": "^7.2.5" + } + }, "node_modules/boolean": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/boolean/-/boolean-3.2.0.tgz", @@ -4689,6 +5281,18 @@ "js-yaml": "^4.1.0" } }, + "node_modules/dns-packet": { + "version": "5.6.1", + "resolved": "https://registry.npmjs.org/dns-packet/-/dns-packet-5.6.1.tgz", + "integrity": "sha512-l4gcSouhcgIKRvyy99RNVOgxXiicE+2jZoNmaNmZ6JXiGajBOJAesk1OBlJuM5k2c+eudGdLxDqXuPCKIj6kpw==", + "license": "MIT", + "dependencies": { + "@leichtgewicht/ip-codec": "^2.0.1" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/dotenv": { "version": "16.6.1", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.6.1.tgz", @@ -5169,7 +5773,6 @@ "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", - "dev": true, "license": "MIT" }, "node_modules/fast-string-truncated-width": { @@ -6303,7 +6906,6 @@ "os": [ "android" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -6325,7 +6927,6 @@ "os": [ "darwin" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -6347,7 +6948,6 @@ "os": [ "darwin" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -6369,7 +6969,6 @@ "os": [ "freebsd" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -6391,7 +6990,6 @@ "os": [ "linux" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -6413,7 +7011,6 @@ "os": [ "linux" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -6435,7 +7032,6 @@ "os": [ "linux" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -6457,7 +7053,6 @@ "os": [ "linux" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -6479,7 +7074,6 @@ "os": [ "linux" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -6501,7 +7095,6 @@ "os": [ "win32" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -6523,7 +7116,6 @@ "os": [ "win32" ], - "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -7651,6 +8243,19 @@ "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", "license": "MIT" }, + "node_modules/multicast-dns": { + "version": "7.2.5", + "resolved": "https://registry.npmjs.org/multicast-dns/-/multicast-dns-7.2.5.tgz", + "integrity": "sha512-2eznPJP8z2BFLX50tf0LuODrpINqP1RVIm/CObbTcBRITQgmC/TjcREF1NeTBzIcR5XO/ukWo+YHOjBbFwIupg==", + "license": "MIT", + "dependencies": { + "dns-packet": "^5.2.2", + "thunky": "^1.0.2" + }, + "bin": { + "multicast-dns": "cli.js" + } + }, "node_modules/mute-stream": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-3.0.0.tgz", @@ -8163,6 +8768,35 @@ "url": "https://paulmillr.com/funding/" } }, + "node_modules/playwright": { + "version": "1.64.0", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.64.0.tgz", + "integrity": "sha512-kVzTnYFYEyQ9RGzBjNCd2+yEqWmVbxl3AHPynv3qQ6JTjv4a+kwRcnqQwIpuP3NnrnIrE/Q6wWAIAvMfIE2/Yg==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.64.0" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/playwright-core": { + "version": "1.64.0", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.64.0.tgz", + "integrity": "sha512-T9r+MZkTECl2+oUcZ26YgZzTrYidTsIhYYkJix+6iDOymOb1FB2RLLSOxRGnZGv5DStt+aduIVbt+E8WygXYpg==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/plist": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/plist/-/plist-3.1.0.tgz", @@ -8885,6 +9519,69 @@ "node": ">= 0.4" } }, + "node_modules/sharp": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.35.5.tgz", + "integrity": "sha512-Ywn4OnzGukp7CDMrp08RQ50YKmuwG47brZgIVPTvBaaAfQlRlygrRqSrxdCiL9M+LlzLBiJ68IR1QqvzHyjC7g==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@img/colour": "^1.1.0", + "detect-libc": "^2.1.2", + "semver": "^7.8.5" + }, + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-darwin-arm64": "0.35.5", + "@img/sharp-darwin-x64": "0.35.5", + "@img/sharp-freebsd-wasm32": "0.35.5", + "@img/sharp-libvips-darwin-arm64": "1.3.4", + "@img/sharp-libvips-darwin-x64": "1.3.4", + "@img/sharp-libvips-linux-arm": "1.3.4", + "@img/sharp-libvips-linux-arm64": "1.3.4", + "@img/sharp-libvips-linux-ppc64": "1.3.4", + "@img/sharp-libvips-linux-riscv64": "1.3.4", + "@img/sharp-libvips-linux-s390x": "1.3.4", + "@img/sharp-libvips-linux-x64": "1.3.4", + "@img/sharp-libvips-linuxmusl-arm64": "1.3.4", + "@img/sharp-libvips-linuxmusl-x64": "1.3.4", + "@img/sharp-linux-arm": "0.35.5", + "@img/sharp-linux-arm64": "0.35.5", + "@img/sharp-linux-ppc64": "0.35.5", + "@img/sharp-linux-riscv64": "0.35.5", + "@img/sharp-linux-s390x": "0.35.5", + "@img/sharp-linux-x64": "0.35.5", + "@img/sharp-linuxmusl-arm64": "0.35.5", + "@img/sharp-linuxmusl-x64": "0.35.5", + "@img/sharp-webcontainers-wasm32": "0.35.5", + "@img/sharp-win32-arm64": "0.35.5", + "@img/sharp-win32-ia32": "0.35.5", + "@img/sharp-win32-x64": "0.35.5" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/sharp/node_modules/semver": { + "version": "7.8.5", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz", + "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==", + "dev": true, + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", @@ -9158,6 +9855,12 @@ "fs-extra": "^10.0.0" } }, + "node_modules/thunky": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/thunky/-/thunky-1.1.0.tgz", + "integrity": "sha512-eHY7nBftgThBqOyHGVN+l8gF0BucP09fMo0oO/Lb0w1OF80dJv+lDVpXG60WMQvkcxAkNybKsrEIE3ZtKGmPrA==", + "license": "MIT" + }, "node_modules/tiny-async-pool": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/tiny-async-pool/-/tiny-async-pool-1.3.0.tgz", diff --git a/package.json b/package.json index 524b53037..8e92aab02 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,15 @@ { "name": "canvastty", - "version": "1.8.1", + "version": "1.8.3", "private": true, "description": "A spatial desktop for local AI agents and real terminal sessions.", "desktopName": "CanvasTTY", "author": "howdeploy", "license": "MIT", "homepage": "https://github.com/howdeploy/CanvasTTY", - "diagnostics": { "reportUrl": "https://canvastty.deploychan.webcam/canvastty/reports" }, + "diagnostics": { + "reportUrl": "https://canvastty.deploychan.webcam/canvastty/reports" + }, "repository": { "type": "git", "url": "https://github.com/howdeploy/CanvasTTY.git" @@ -46,10 +48,13 @@ "build:helpers": "node scripts/build-native-helpers.mjs", "pack:even": "npm run pack --workspace integrations/even-g2", "smoke:terminal-hidden": "node scripts/smoke-hidden-terminal-electron.mjs", - "bench:terminal-hidden-renderer": "node scripts/smoke-hidden-terminal-electron.mjs --benchmark" + "bench:terminal-hidden-renderer": "node scripts/smoke-hidden-terminal-electron.mjs --benchmark", + "test:canvaslink": "npm test --workspace @canvastty/canvaslink", + "build:canvaslink:android": "npm run build:android --workspace @canvastty/canvaslink" }, "dependencies": { "@xterm/headless": "6.0.0", + "bonjour-service": "1.3.0", "electron-updater": "6.8.9", "hast-util-to-jsx-runtime": "2.3.6", "lowlight": "3.3.0", @@ -73,6 +78,7 @@ "@xterm/addon-web-links": "0.12.0", "@xterm/addon-webgl": "0.19.0", "@xterm/xterm": "6.0.0", + "ascii.rest": "0.4.0", "electron": "43.7.7", "electron-builder": "26.15.3", "electron-vite": "5.0.0", @@ -92,6 +98,7 @@ "node-pty@1.1.0": true }, "workspaces": [ - "integrations/even-g2" + "integrations/even-g2", + "integrations/canvaslink" ] } diff --git a/scripts/smoke-canvaslink.mjs b/scripts/smoke-canvaslink.mjs new file mode 100644 index 000000000..89d39899d --- /dev/null +++ b/scripts/smoke-canvaslink.mjs @@ -0,0 +1,68 @@ +// Real Electron + built preload/host wiring, using hidden disposable windows. +import assert from 'node:assert/strict'; +import { spawn, spawnSync } from 'node:child_process'; +import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; +import { createRequire } from 'node:module'; +import { tmpdir } from 'node:os'; +import { join, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { electronSmokeLaunchBlockReason } from './lib/electron-smoke-launch-guard.mjs'; +const require = createRequire(import.meta.url), electron = require('electron'); +const root = resolve(fileURLToPath(new URL('..', import.meta.url))); +if (typeof electron === 'string') { + const reason = electronSmokeLaunchBlockReason(process.platform, process.env); + if (reason) throw Error(reason); + const profile = await mkdtemp(join(process.platform === 'win32' ? tmpdir() : '/tmp', 'ct-link-')); + const env = Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith('CANVASTTY_') && !['ELECTRON_RUN_AS_NODE', 'ELECTRON_RENDERER_URL'].includes(name))); + const log = join(profile, 'electron.log'); + const ownedPids = () => process.platform !== 'darwin' ? [] : spawnSync('/bin/ps', ['-axo', 'pid=,comm='], { encoding: 'utf8' }).stdout.split('\n').flatMap(line => { const match = line.match(/^\s*(\d+)\s+(.*)$/); return match && match[2] === electron ? [Number(match[1])] : []; }); + const before = new Set(ownedPids()); + const args = [fileURLToPath(import.meta.url), `--user-data-dir=${profile}`, '--canvaslink-port', '0']; + if (process.platform === 'linux' && process.env.CI === 'true') args.push('--no-sandbox'); + try { + const child = process.platform === 'darwin' + ? spawn('/usr/bin/open', ['-n', '-W', '-g', '-j', resolve(electron, '../../..'), '--stdout', log, '--stderr', log, '--args', ...args], { env, stdio: ['ignore', 'pipe', 'pipe'] }) + : spawn(electron, args, { env, stdio: ['ignore', 'pipe', 'pipe'] }); + let output = ''; child.stdout.on('data', chunk => output += chunk); + child.stderr.on('data', chunk => { output += chunk; }); + const timer = setTimeout(() => child.kill('SIGTERM'), 30000); + const code = await new Promise((done, reject) => { child.on('error', reject); child.on('exit', done); }); + clearTimeout(timer); + if (process.platform === 'darwin') output += await readFile(log, 'utf8').catch(() => ''); + if (code !== 0 || !output.includes('CANVASTTY_CANVASLINK_SMOKE_OK')) throw Error(`Electron exit=${code}; output: ${output.slice(-5000) || '(no output before timeout)'}`); + console.log('CANVASTTY_CANVASLINK_SMOKE_OK (real built preload, IPC, enable/revoke/disable, untrusted-window rejection)'); + } finally { + for (const pid of ownedPids()) if (!before.has(pid)) { try { process.kill(pid, 'SIGTERM'); } catch {} } + await rm(profile, { recursive: true, force: true }); + } +} else { + const { app, BrowserWindow } = electron; + app.setName('CanvasTTY CanvasLink isolated smoke'); + const profileArg = process.argv.find(arg => arg.startsWith('--user-data-dir=')); + assert.ok(profileArg); app.setPath('userData', profileArg.split('=').slice(1).join('=')); + app.on('window-all-closed', () => {}); + let owned; + const { install } = require(join(root, 'out/main/canvaslink/host/embedded.cjs')); + install({ getWindow: () => owned }); + try { + await app.whenReady(); app.dock?.hide(); + const page = join(app.getPath('userData'), 'fixture.html'); + await writeFile(page, 'CanvasLink isolated fixture'); + const create = () => new BrowserWindow({ show: false, webPreferences: { preload: join(root, 'out/preload/index.cjs'), contextIsolation: true, sandbox: true, nodeIntegration: false } }); + owned = create(); await owned.loadFile(page); + const call = script => owned.webContents.executeJavaScript(script); + assert.equal((await call('window.canvasTTY.canvasLink.state()')).enabled, false); + let state = await call('window.canvasTTY.canvasLink.command("enable")'); + assert.equal(state.enabled, true); assert.match(state.pin, /^\d{6}$/); + const stranger = create(); await stranger.loadFile(page); + const rejected = await stranger.webContents.executeJavaScript('window.canvasTTY.canvasLink.command("disable").then(()=>false,()=>true)'); + assert.equal(rejected, true); stranger.destroy(); + state = await call('window.canvasTTY.canvasLink.command("revoke")'); + assert.equal(state.enabled, true); assert.equal(state.devices.length, 0); + state = await call('window.canvasTTY.canvasLink.command("disable")'); + assert.equal(state.enabled, false); assert.equal(state.pin, ''); + assert.equal(await call('window.canvasTTY.canvasLink.command("new-code").then(()=>false,()=>true)'), true); + console.log('CANVASTTY_CANVASLINK_SMOKE_OK'); + owned.destroy(); app.quit(); + } catch (error) { console.error(error); app.exit(1); } +} diff --git a/src/main/index.ts b/src/main/index.ts index b9832dcee..9df9e089e 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -101,6 +101,9 @@ import { mainWindowChromeOptions } from "./windowChrome"; import { markMainBoot, mainBootMarks } from "./bootMarks"; import { attachEditContextMenu } from "./editContextMenu"; import { macApplicationMenuTemplate } from "./macApplicationMenu"; +import { configureCanvasLinkStartup, installCanvasLink } from "./services/canvasLink"; + +configureCanvasLinkStartup(app); if (process.env.CANVASTTY_USER_DATA_DIR) { if (!isAbsolute(process.env.CANVASTTY_USER_DATA_DIR)) throw new Error("CANVASTTY_USER_DATA_DIR must be absolute"); @@ -1210,6 +1213,7 @@ function attentionStatusLabel(status: "needs_approval" | "failed", locale: Local } if (hasSingleInstanceLock) { + installCanvasLink(() => mainWindow); void app.whenReady() .then(() => { markMainBoot("appReady"); diff --git a/src/main/ipc/registerIpc.ts b/src/main/ipc/registerIpc.ts index 00d3feb6c..fd5cee03d 100644 --- a/src/main/ipc/registerIpc.ts +++ b/src/main/ipc/registerIpc.ts @@ -311,7 +311,7 @@ export function registerIpc(ipcMain: IpcRegistrar, { }); ipcMain.on(IPC.canvasNavigationOwnerWheel, (event, input: unknown) => { assertMainRenderer(event, getMainWindow); - browser.beginRendererWheelSequence(input); + browser.beginRendererWheelSequence(input, event.sender.getZoomFactor()); event.returnValue = true; }); ipcMain.on(IPC.canvasNavigationPointerGesture, (event, active: boolean) => { @@ -890,7 +890,7 @@ export function registerIpc(ipcMain: IpcRegistrar, { }); ipcMain.on(IPC.browserSetViewport, (event, bounds) => { assertMainRenderer(event, getMainWindow); - browser.setViewport(bounds); + browser.setViewport(bounds, event.sender.getZoomFactor()); }); ipcMain.on(IPC.browserPageWheelDecision, (event, input: unknown) => { event.returnValue = browser.decidePageWheel(event.sender, input); diff --git a/src/main/services/BrowserService.ts b/src/main/services/BrowserService.ts index 5148151e8..e52c8f604 100644 --- a/src/main/services/BrowserService.ts +++ b/src/main/services/BrowserService.ts @@ -40,7 +40,7 @@ import { BrowserCanvasPointerRouter } from "./browser/BrowserCanvasPointerRouter import { BrowserCanvasSinkViewportController } from "./browser/BrowserCanvasSinkViewport.ts"; import { BrowserAuditStore } from "./browser/BrowserAuditStore.ts"; import { browserPageWheelReply, type BrowserPageWheelReply } from "./browser/BrowserCanvasWheel.ts"; -import { clipBrowserViewportBounds, normalizeBrowserViewportBounds, sameBrowserViewport } from "./browser/BrowserViewport.ts"; +import { browserCanvasEventToCss, clipBrowserViewportBounds, normalizeBrowserViewportBounds, sameBrowserViewport } from "./browser/BrowserViewport.ts"; import { BrowserCore, type BrowserCoreHost, type BrowserCoreTab, type BrowserOpenedTab } from "./browser/BrowserCore.ts"; import { BrowserEngineTabs, type BrowserEngineProvider } from "./browser/BrowserEngineTabs.ts"; import type { EngineFallbackReason } from "./browser/BrowserErrors.ts"; @@ -83,7 +83,7 @@ const BACKGROUND_TAB_SIZE = { width: 1280, height: 800 }; // Mirrors the `.browser-card { border-radius: 17px }` declaration in src/renderer/src/styles/app.css: the // stylesheet owns this visual property, so the two have to stay in sync by hand. const CARD_CORNER_RADIUS = 17; -// normalizeBrowserViewportBounds clamps canvasScale to 0.5..3; this bound only guards a bogus payload. +// Bound the native corner radius even when renderer zoom multiplies the canvas scale. const MAX_PAGE_CORNER_RADIUS = CARD_CORNER_RADIUS * 3; /** Card corner radius in device-independent pixels at the current canvas scale. */ @@ -271,7 +271,8 @@ export class BrowserService { shouldDeferIdleEnd: () => this.canvasPointers.hasFreezePointerRelay, sendWheel: (payload) => { const owner = this.getOwner(); - if (owner && !owner.isDestroyed()) owner.webContents.send(IPC.browserCanvasWheel, payload); + if (owner && !owner.isDestroyed()) owner.webContents.send(IPC.browserCanvasWheel, + browserCanvasEventToCss(payload, owner.webContents.getZoomFactor())); }, sendFreezeFrame: (payload) => { const owner = this.getOwner(); @@ -297,7 +298,8 @@ export class BrowserService { endWheelSequence: () => this.canvasGestures.endSequence(), sendNavigationPointer: (payload) => { const owner = this.getOwner(); - if (owner && !owner.isDestroyed()) owner.webContents.send(IPC.browserCanvasNavigationPointer, payload); + if (owner && !owner.isDestroyed()) owner.webContents.send(IPC.browserCanvasNavigationPointer, + browserCanvasEventToCss(payload, owner.webContents.getZoomFactor())); } }); @@ -403,8 +405,8 @@ export class BrowserService { this.canvasGestures.handlePageWheel(sender, input); } - beginRendererWheelSequence(input: unknown): void { - this.canvasGestures.beginRendererSequence(input); + beginRendererWheelSequence(input: unknown, rendererZoomFactor = 1): void { + this.canvasGestures.beginRendererSequence(input, rendererZoomFactor); } async setRestoreTabs(enabled: boolean): Promise { @@ -537,8 +539,8 @@ export class BrowserService { return this.getState(); } - setViewport(bounds: BrowserViewportBounds): void { - const normalized = normalizeBrowserViewportBounds(bounds); + setViewport(bounds: BrowserViewportBounds, rendererZoomFactor = 1): void { + const normalized = normalizeBrowserViewportBounds(bounds, rendererZoomFactor); if (!normalized) return; const previous = this.viewport; // The card reports its rectangle on every camera step, resize and layout pass; one that rounds to the same @@ -987,7 +989,7 @@ export class BrowserService { clientY: this.viewport.y + mouse.y, clickCount: pointerType === "down" || pointerType === "up" ? Math.max(1, mouse.clickCount ?? 1) : 0 }; - owner.webContents.send(IPC.browserCanvasPointer, payload); + owner.webContents.send(IPC.browserCanvasPointer, browserCanvasEventToCss(payload, owner.webContents.getZoomFactor())); } }); diff --git a/src/main/services/SettingsStore.ts b/src/main/services/SettingsStore.ts index a4828a440..20175c618 100644 --- a/src/main/services/SettingsStore.ts +++ b/src/main/services/SettingsStore.ts @@ -2,6 +2,8 @@ import { homedir } from "node:os"; import { dirname, join } from "node:path"; import { mkdir, readFile, rename, writeFile } from "node:fs/promises"; import { isHomeMediaPath } from "./homeMedia.ts"; +import { normalizeBackgroundOverrides, normalizeBackgroundPresets } from "../../shared/terminalBackgroundAppearance.ts"; +import { isTerminalBackgroundId } from "../../shared/terminalBackground.ts"; import type { AgentProviderId, AgentCliAvailability, @@ -380,6 +382,25 @@ function createDefaults(systemLocale: string, platform: string): AppSettings { canvasBackground: "none", pattern: "dots", terminalBorderSkin: "classic", + terminalBackground: "none", + terminalBackgroundBrightness: 45, + terminalBackgroundContrast: 100, + terminalBackgroundSaturation: 100, + terminalBackgroundPaused: false, + terminalBackgroundSpeed: 1, + terminalBackgroundScale: 100, + terminalBackgroundOffsetX: 0, + terminalBackgroundOffsetY: 0, + terminalBackgroundReadability: true, + terminalBackgroundReadabilityStrength: 86, + terminalBackgroundPauseOnOutput: false, + terminalBackgroundFocusDimming: false, + terminalBackgroundSynchronized: false, + terminalBackgroundPauseInactive: false, + terminalBackgroundOverrides: {}, + terminalBackgroundPresets: [], + terminalBackgroundFavorites: [], + terminalBackgroundDeleted: [], terminalSkinDetail: "detailed", terminalSkinAnimationEnabled: true, appSkin: "classic", @@ -622,6 +643,45 @@ export function normalizeSettings( pattern: PATTERNS.has(source.pattern as CanvasPatternId) ? source.pattern as CanvasPatternId : fallback.pattern, + terminalBackground: isTerminalBackgroundId(source.terminalBackground) + ? source.terminalBackground : fallback.terminalBackground ?? "none", + terminalBackgroundBrightness: typeof source.terminalBackgroundBrightness === "number" && Number.isFinite(source.terminalBackgroundBrightness) + ? Math.min(100, Math.max(10, source.terminalBackgroundBrightness)) : fallback.terminalBackgroundBrightness ?? 45, + terminalBackgroundContrast: typeof source.terminalBackgroundContrast === "number" && Number.isFinite(source.terminalBackgroundContrast) + ? Math.min(200, Math.max(0, source.terminalBackgroundContrast)) : fallback.terminalBackgroundContrast ?? 100, + terminalBackgroundSaturation: typeof source.terminalBackgroundSaturation === "number" && Number.isFinite(source.terminalBackgroundSaturation) + ? Math.min(200, Math.max(0, source.terminalBackgroundSaturation)) : fallback.terminalBackgroundSaturation ?? 100, + terminalBackgroundPaused: typeof source.terminalBackgroundPaused === "boolean" + ? source.terminalBackgroundPaused : fallback.terminalBackgroundPaused ?? false, + terminalBackgroundSpeed: typeof source.terminalBackgroundSpeed === "number" && Number.isFinite(source.terminalBackgroundSpeed) + ? Math.min(2, Math.max(0.25, source.terminalBackgroundSpeed)) : fallback.terminalBackgroundSpeed ?? 1, + terminalBackgroundScale: typeof source.terminalBackgroundScale === "number" && Number.isFinite(source.terminalBackgroundScale) + ? Math.min(300, Math.max(25, source.terminalBackgroundScale)) : fallback.terminalBackgroundScale ?? 100, + terminalBackgroundOffsetX: typeof source.terminalBackgroundOffsetX === "number" && Number.isFinite(source.terminalBackgroundOffsetX) + ? Math.min(50, Math.max(-50, source.terminalBackgroundOffsetX)) : fallback.terminalBackgroundOffsetX ?? 0, + terminalBackgroundOffsetY: typeof source.terminalBackgroundOffsetY === "number" && Number.isFinite(source.terminalBackgroundOffsetY) + ? Math.min(50, Math.max(-50, source.terminalBackgroundOffsetY)) : fallback.terminalBackgroundOffsetY ?? 0, + terminalBackgroundReadability: typeof source.terminalBackgroundReadability === "boolean" + ? source.terminalBackgroundReadability : fallback.terminalBackgroundReadability ?? true, + terminalBackgroundReadabilityStrength: typeof source.terminalBackgroundReadabilityStrength === "number" && Number.isFinite(source.terminalBackgroundReadabilityStrength) + ? Math.min(100, Math.max(0, source.terminalBackgroundReadabilityStrength)) : fallback.terminalBackgroundReadabilityStrength ?? 86, + terminalBackgroundPauseOnOutput: typeof source.terminalBackgroundPauseOnOutput === "boolean" + ? source.terminalBackgroundPauseOnOutput : fallback.terminalBackgroundPauseOnOutput ?? false, + terminalBackgroundFocusDimming: typeof source.terminalBackgroundFocusDimming === "boolean" + ? source.terminalBackgroundFocusDimming : fallback.terminalBackgroundFocusDimming ?? false, + terminalBackgroundSynchronized: typeof source.terminalBackgroundSynchronized === "boolean" + ? source.terminalBackgroundSynchronized : fallback.terminalBackgroundSynchronized ?? false, + terminalBackgroundPauseInactive: typeof source.terminalBackgroundPauseInactive === "boolean" + ? source.terminalBackgroundPauseInactive : fallback.terminalBackgroundPauseInactive ?? false, + terminalBackgroundOverrides: normalizeBackgroundOverrides(source.terminalBackgroundOverrides ?? fallback.terminalBackgroundOverrides), + terminalBackgroundPresets: normalizeBackgroundPresets(source.terminalBackgroundPresets ?? fallback.terminalBackgroundPresets), + terminalBackgroundFavorites: Array.isArray(source.terminalBackgroundFavorites) + ? [...new Set(source.terminalBackgroundFavorites.filter((id: unknown): id is Exclude => + isTerminalBackgroundId(id) && id !== "none"))] + : fallback.terminalBackgroundFavorites ?? [], + terminalBackgroundDeleted: Array.isArray(source.terminalBackgroundDeleted) + ? source.terminalBackgroundDeleted.filter((id: unknown): id is string => typeof id === "string") + : fallback.terminalBackgroundDeleted ?? [], terminalBorderSkin: isTerminalBorderSkinId(source.terminalBorderSkin) ? source.terminalBorderSkin as TerminalBorderSkinId : fallback.terminalBorderSkin, diff --git a/src/main/services/agent-runtime/ProviderRuntimeLaunch.ts b/src/main/services/agent-runtime/ProviderRuntimeLaunch.ts index a945496fe..426ab424b 100644 --- a/src/main/services/agent-runtime/ProviderRuntimeLaunch.ts +++ b/src/main/services/agent-runtime/ProviderRuntimeLaunch.ts @@ -825,7 +825,7 @@ class KimiRuntimeHooks { } } -class HermesRuntimeHooks { +export class HermesRuntimeHooks { private cleaned = false; private readonly path: string; private readonly journalPath: string; @@ -1027,11 +1027,11 @@ function mutateHermesHooks(raw: string, commands: Record, remo isRecord(entry) && entry.command === command && entry.timeout === HOOK_TIMEOUT_SECONDS ))).map((command) => ({ command, timeout: HOOK_TIMEOUT_SECONDS }))]; if (next.length > 0) document.setIn(["hooks", event], next); - else document.deleteIn(["hooks", event]); + else if (document.has("hooks") && document.hasIn(["hooks", event])) document.deleteIn(["hooks", event]); } const nextValue = document.toJS({ maxAliasCount: 100 }) as unknown; if (isRecord(nextValue) && isRecord(nextValue.hooks) && Object.keys(nextValue.hooks).length === 0) { - document.delete("hooks"); + if (document.has("hooks")) document.delete("hooks"); } return document.toString({ lineWidth: 0 }); } diff --git a/src/main/services/browser/BrowserCanvasGestureController.ts b/src/main/services/browser/BrowserCanvasGestureController.ts index 089b7ec2f..ad3916265 100644 --- a/src/main/services/browser/BrowserCanvasGestureController.ts +++ b/src/main/services/browser/BrowserCanvasGestureController.ts @@ -148,15 +148,16 @@ export class BrowserCanvasGestureController { }); } - beginRendererSequence(input: unknown): void { + beginRendererSequence(input: unknown, rendererZoomFactor = 1): void { + if (!Number.isFinite(rendererZoomFactor) || rendererZoomFactor <= 0) return; if (!input || typeof input !== "object" || Array.isArray(input)) return; const values = input as Record; if (!Number.isFinite(values.clientX) || !Number.isFinite(values.clientY)) return; const owner = this.host.getOwner(); if (!owner || owner.isDestroyed()) return; const content = owner.getContentBounds(); - const clientX = values.clientX as number; - const clientY = values.clientY as number; + const clientX = (values.clientX as number) * rendererZoomFactor; + const clientY = (values.clientY as number) * rendererZoomFactor; if (clientX < 0 || clientY < 0 || clientX >= content.width || clientY >= content.height) return; this.beginOwnerSequence({ x: clientX, y: clientY }); } diff --git a/src/main/services/browser/BrowserElectronSmoke.ts b/src/main/services/browser/BrowserElectronSmoke.ts index 48ed324f1..8e44d06e3 100644 --- a/src/main/services/browser/BrowserElectronSmoke.ts +++ b/src/main/services/browser/BrowserElectronSmoke.ts @@ -5,6 +5,7 @@ import { app, BrowserWindow, webContents } from "electron"; import type { BrowserActor, BrowserCommand, BrowserElementRef, BrowserResult } from "../../../shared/contracts.ts"; import type { BrowserService } from "../BrowserService.ts"; import { BROWSER_CANVAS_WHEEL_IDLE_MS } from "./BrowserCanvasFreeze.ts"; +import { clipBrowserViewportBounds, normalizeBrowserViewportBounds } from "./BrowserViewport.ts"; import { waitForBrowserSmokeWheelReady, runBrowserSmokeCleanup, @@ -226,11 +227,150 @@ export async function runBrowserElectronSmoke( if (stale.ok || stale.error?.code !== "STALE_REF") { throw new Error(`Old element reference was not rejected: ${JSON.stringify(stale)}`); } + await assertBrowserRendererZoom(service, `${origin}/next`); } finally { service.core.agentDisconnected(actor); } } +/** A non-default View > Zoom affects CSS pixels, but not native child-view coordinates. */ +async function assertBrowserRendererZoom(service: BrowserService, url: string): Promise { + const contents = webContents.getAllWebContents().find((candidate) => candidate.getURL().startsWith(url)); + const owner = BrowserWindow.getAllWindows().find((candidate) => candidate.getParentWindow() === null); + if (!contents || !owner) throw new Error("Browser zoom smoke could not resolve its WebContents."); + const initialZoom = owner.webContents.getZoomFactor(); + let fixtureStyle: string | undefined; + try { + // Use the actual sandboxed preload/IPC bridge and a transformed DOM slot, as BrowserCard does. + await owner.webContents.executeJavaScript(` + (() => { + const slot = document.createElement('div'); + slot.id = 'browser-zoom-smoke-slot'; + slot.style.cssText = 'position:fixed;left:200px;top:160px;width:420px;height:260px;transform-origin:0 0'; + document.body.append(slot); + globalThis.__canvasttyZoomPointers = []; + globalThis.__canvasttyZoomFreeze = []; + globalThis.__canvasttyZoomPointerOff = window.canvasTTY.browser.onCanvasPointer((event) => { + globalThis.__canvasttyZoomPointers.push(event); + }); + globalThis.__canvasttyZoomFreezeOff = window.canvasTTY.browser.onCanvasFreezeFrame((event) => { + globalThis.__canvasttyZoomFreeze.push(event.active); + }); + })(); + `); + for (const zoom of [1.2 ** -0.5, 0.8, 1, 1.25]) { + owner.webContents.setZoomFactor(zoom); + for (const canvasScale of [0.5, 1, 1.4]) { + const rect = await owner.webContents.executeJavaScript(` + (() => { + const slot = document.getElementById('browser-zoom-smoke-slot'); + slot.style.transform = 'scale(${canvasScale})'; + const rect = slot.getBoundingClientRect(); + window.canvasTTY.browser.setViewport({ x: rect.x, y: rect.y, + width: rect.width, height: rect.height, surface: 'native', canvasScale: ${canvasScale}, + clipBounds: { x: 0, y: 0, width: window.innerWidth, height: window.innerHeight } }); + return { x: rect.x, y: rect.y, width: rect.width, height: rect.height }; + })() + `) as { x: number; y: number; width: number; height: number }; + const expected = { + x: Math.floor(rect.x * zoom), y: Math.floor(rect.y * zoom), + width: Math.ceil((rect.x + rect.width) * zoom) - Math.floor(rect.x * zoom), + height: Math.ceil((rect.y + rect.height) * zoom) - Math.floor(rect.y * zoom) + }; + await waitUntil(async () => { + const clip = owner.contentView.children.find((view) => view.children.some( + (child) => 'webContents' in child && child.webContents === contents + )); + if (!clip) return false; + const actual = clip.getBounds(); + return Object.entries(expected).every(([key, value]) => actual[key as keyof typeof actual] === value) + && Math.abs(contents.getZoomFactor() - zoom * canvasScale) < 0.001; + }).catch(() => { + const bounds = owner.contentView.children.map((view) => view.getBounds()); + throw new Error(`Browser native bounds mismatch at renderer zoom ${zoom}, canvas ${canvasScale}: ` + + JSON.stringify({ expected, actual: bounds, pageZoom: contents.getZoomFactor() })); + }); + } + const clip = owner.contentView.children.find((view) => view.children.some( + (child) => 'webContents' in child && child.webContents === contents + )); + if (!clip) throw new Error("Browser zoom smoke lost the native clip."); + const bounds = clip.getBounds(); + await owner.webContents.executeJavaScript("globalThis.__canvasttyZoomPointers = []; void 0;"); + contents.sendInputEvent({ type: "mouseDown", x: 30, y: 30, button: "left", clickCount: 1 }); + contents.sendInputEvent({ type: "mouseUp", x: 30, y: 30, button: "left", clickCount: 1 }); + await waitUntil(async () => owner.webContents.executeJavaScript( + "globalThis.__canvasttyZoomPointers.some((event) => event.type === 'down')" + )); + const pointer = await owner.webContents.executeJavaScript( + "globalThis.__canvasttyZoomPointers.find((event) => event.type === 'down')" + ) as { clientX: number; clientY: number }; + if (Math.abs(pointer.clientX - (bounds.x + 30) / zoom) > 0.001 + || Math.abs(pointer.clientY - (bounds.y + 30) / zoom) > 0.001) { + throw new Error(`Browser pointer used native coordinates in the zoomed renderer: ${JSON.stringify(pointer)}`); + } + await owner.webContents.executeJavaScript(` + (() => { + globalThis.__canvasttyZoomFreeze = []; + const rect = document.getElementById('browser-zoom-smoke-slot').getBoundingClientRect(); + window.canvasTTY.canvasNavigation.armOwnerWheelSequence( + ${zoom < 1 ? "rect.right - 2" : "rect.left + 2"}, rect.top + rect.height / 2); + })(); + `); + await waitUntil(async () => owner.webContents.executeJavaScript("globalThis.__canvasttyZoomFreeze.includes(true)")); + await waitForWheelIdle(); + } + // Also let the real React card report its own rectangle when View > Zoom changes. + // Small CI displays otherwise hide the native page where the corner HUD overlaps this card. + fixtureStyle = await owner.webContents.insertCSS(".canvas-overlays { display: none !important; }"); + owner.webContents.setZoomFactor(1.2 ** -0.5); + await owner.webContents.executeJavaScript(` + document.getElementById('browser-zoom-smoke-slot')?.remove(); + document.querySelector('.launcher-button--browser').click(); + void 0; + `); + let cardObservation: unknown; + await waitUntil(async () => { + const rect = await owner.webContents.executeJavaScript(` + (() => { + const slot = document.querySelector('.browser-card__viewport'); + if (!slot) return null; + const rect = slot.getBoundingClientRect(); + const clip = slot.closest('.workspace').getBoundingClientRect(); + return { x: rect.x, y: rect.y, width: rect.width, height: rect.height, + clipBounds: { x: clip.x, y: clip.y, width: clip.width, height: clip.height } }; + })() + `) as { x: number; y: number; width: number; height: number } | null; + const clip = owner.contentView.children.find((view) => view.children.some( + (child) => 'webContents' in child && child.webContents === contents + )); + const zoom = owner.webContents.getZoomFactor(); + // The matrix above checks zoom arithmetic independently; the real card also clips to its workspace. + const normalized = rect && normalizeBrowserViewportBounds({ ...rect, surface: "native" }, zoom); + const expected = normalized && clipBrowserViewportBounds(normalized, owner.getContentBounds()); + const actual = clip?.getBounds(); + cardObservation = { rect, zoom, expected, actual }; + if (!expected || !actual) return false; + return Object.entries(expected).every( + ([key, value]) => actual[key as keyof typeof actual] === value + ); + }).catch((cause) => { + throw new Error(`Real BrowserCard viewport mismatch: ${JSON.stringify(cardObservation)}`, { cause }); + }); + console.log("CANVASTTY_BROWSER_ZOOM_SMOKE_READY"); + } finally { + if (fixtureStyle) await owner.webContents.removeInsertedCSS(fixtureStyle); + owner.webContents.setZoomFactor(initialZoom); + await owner.webContents.executeJavaScript(` + document.getElementById('browser-zoom-smoke-slot')?.remove(); + globalThis.__canvasttyZoomPointerOff?.(); + globalThis.__canvasttyZoomFreezeOff?.(); + void 0; + `); + service.setViewport({ x: 0, y: 0, width: 820, height: 620, surface: "native", canvasScale: 1 }); + } +} + async function assertFocusAwarePhysicalWheel(service: BrowserService, url: string): Promise { const contents = webContents.getAllWebContents().find((candidate) => candidate.getURL() === url); const owner = BrowserWindow.getAllWindows().find((candidate) => candidate.getParentWindow() === null); diff --git a/src/main/services/browser/BrowserViewport.ts b/src/main/services/browser/BrowserViewport.ts index 1113860e7..900da0f00 100644 --- a/src/main/services/browser/BrowserViewport.ts +++ b/src/main/services/browser/BrowserViewport.ts @@ -7,25 +7,27 @@ import type { const MIN_BROWSER_PAGE_SCALE = 0.5; const MAX_BROWSER_PAGE_SCALE = 3; -export function normalizeBrowserViewportBounds(value: unknown): BrowserViewportBounds | null { +/** Renderer rectangles are CSS pixels; Electron View bounds are device-independent pixels. */ +export function normalizeBrowserViewportBounds(value: unknown, rendererZoomFactor = 1): BrowserViewportBounds | null { + if (!Number.isFinite(rendererZoomFactor) || rendererZoomFactor <= 0) return null; if (!value || typeof value !== "object") return null; const bounds = value as BrowserViewportBounds; if (![bounds.x, bounds.y, bounds.width, bounds.height].every(Number.isFinite)) return null; if (bounds.surface !== "native" && bounds.surface !== "placeholder" && bounds.surface !== "hidden") { return null; } - const clipBounds = normalizeClipBounds(bounds.clipBounds); + const clipBounds = normalizeClipBounds(bounds.clipBounds, rendererZoomFactor); if (bounds.clipBounds !== undefined && !clipBounds) return null; - const width = Math.max(0, bounds.width); - const height = Math.max(0, bounds.height); - const left = Math.floor(bounds.x); - const top = Math.floor(bounds.y); - const right = width === 0 ? left : Math.ceil(bounds.x + width); - const bottom = height === 0 ? top : Math.ceil(bounds.y + height); + const width = Math.max(0, bounds.width) * rendererZoomFactor; + const height = Math.max(0, bounds.height) * rendererZoomFactor; + const left = Math.floor(bounds.x * rendererZoomFactor); + const top = Math.floor(bounds.y * rendererZoomFactor); + const right = width === 0 ? left : Math.ceil(bounds.x * rendererZoomFactor + width); + const bottom = height === 0 ? top : Math.ceil(bounds.y * rendererZoomFactor + height); const canvasScale = Number.isFinite(bounds.canvasScale) - ? Math.min(MAX_BROWSER_PAGE_SCALE, Math.max(MIN_BROWSER_PAGE_SCALE, bounds.canvasScale!)) - : undefined; + ? Math.min(MAX_BROWSER_PAGE_SCALE, Math.max(MIN_BROWSER_PAGE_SCALE, bounds.canvasScale!)) * rendererZoomFactor + : rendererZoomFactor === 1 ? undefined : rendererZoomFactor; return { x: left, @@ -66,18 +68,23 @@ export function clipBrowserViewportBounds( return { x: left, y: top, width: right - left, height: bottom - top }; } -function normalizeClipBounds(value: unknown): BrowserViewportClipBounds | undefined | null { +/** Native pointer relays return to the renderer's CSS coordinate space, including the zoom anchor. */ +export function browserCanvasEventToCss(event: T, rendererZoomFactor: number): T { + return { ...event, clientX: event.clientX / rendererZoomFactor, clientY: event.clientY / rendererZoomFactor }; +} + +function normalizeClipBounds(value: unknown, rendererZoomFactor: number): BrowserViewportClipBounds | undefined | null { if (value === undefined) return undefined; if (!value || typeof value !== "object") return null; const bounds = value as BrowserViewportClipBounds; if (![bounds.x, bounds.y, bounds.width, bounds.height].every(Number.isFinite)) return null; - const width = Math.max(0, bounds.width); - const height = Math.max(0, bounds.height); - const left = Math.ceil(bounds.x); - const top = Math.ceil(bounds.y); - const right = width === 0 ? left : Math.floor(bounds.x + width); - const bottom = height === 0 ? top : Math.floor(bounds.y + height); + const width = Math.max(0, bounds.width) * rendererZoomFactor; + const height = Math.max(0, bounds.height) * rendererZoomFactor; + const left = Math.ceil(bounds.x * rendererZoomFactor); + const top = Math.ceil(bounds.y * rendererZoomFactor); + const right = width === 0 ? left : Math.floor(bounds.x * rendererZoomFactor + width); + const bottom = height === 0 ? top : Math.floor(bounds.y * rendererZoomFactor + height); return { x: left, y: top, diff --git a/src/main/services/canvasLink.ts b/src/main/services/canvasLink.ts new file mode 100644 index 000000000..922156702 --- /dev/null +++ b/src/main/services/canvasLink.ts @@ -0,0 +1,19 @@ +import { createRequire } from "node:module"; +import type { App, BrowserWindow } from "electron"; + +// Static CJS/HTML assets are copied by electron.vite.config.ts into out/main. +// Keeping these relative to the bundle works in dev and inside app.asar. +const require = createRequire(import.meta.url); + +export function configureCanvasLinkStartup(app: App): void { + if (!process.argv.includes("--canvaslink-high-density")) return; + const { configureRenderScale } = require("./canvaslink/host/display-scale.cjs"); + configureRenderScale(app); +} + +export function installCanvasLink(getWindow: () => BrowserWindow | null): void { + const { install } = require("./canvaslink/host/embedded.cjs"); + // Registers the owning-frame-only settings IPC. No listener, capture or + // discovery starts until the user explicitly enables CanvasLink in Settings. + install({ getWindow }); +} diff --git a/src/preload/index.ts b/src/preload/index.ts index 32f38cf65..49843cd41 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -65,6 +65,10 @@ ipcRenderer.on(IPC.windowOpenUpdates, () => { }); const api: CanvasTTYApi = { + canvasLink: { + state: () => ipcRenderer.invoke("canvaslink-settings:state"), + command: (action) => ipcRenderer.invoke("canvaslink-settings:command", action) + }, mascots: { list: () => ipcRenderer.invoke(IPC.mascotsList), start: (image) => ipcRenderer.invoke(IPC.mascotsStart, image), diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index cb72f6485..fac979f60 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -50,6 +50,8 @@ import { keyboardPresetShortcuts } from "../../shared/contracts"; import { normalizeExternalUrl } from "../../shared/externalUrl"; +import type { TerminalBackgroundId } from "../../shared/terminalBackground"; +import type { TerminalBackgroundAppearance } from "../../shared/terminalBackgroundAppearance"; import { createTerminalBorderSkinStyleController, type TerminalBorderSkinStyleController @@ -149,6 +151,25 @@ const FALLBACK_SETTINGS: AppSettings = { canvasBackground: "none", pattern: "dots", terminalBorderSkin: "classic", + terminalBackground: "none", + terminalBackgroundBrightness: 45, + terminalBackgroundContrast: 100, + terminalBackgroundSaturation: 100, + terminalBackgroundPaused: false, + terminalBackgroundSpeed: 1, + terminalBackgroundScale: 100, + terminalBackgroundOffsetX: 0, + terminalBackgroundOffsetY: 0, + terminalBackgroundReadability: true, + terminalBackgroundReadabilityStrength: 86, + terminalBackgroundPauseOnOutput: false, + terminalBackgroundFocusDimming: false, + terminalBackgroundSynchronized: false, + terminalBackgroundPauseInactive: false, + terminalBackgroundOverrides: {}, + terminalBackgroundPresets: [], + terminalBackgroundFavorites: [], + terminalBackgroundDeleted: [], terminalSkinDetail: "detailed", terminalSkinAnimationEnabled: true, appSkin: "classic", @@ -492,6 +513,11 @@ export function App(): React.JSX.Element { if (!active) return; removedBeforeList.add(id); setSessions((current) => current.filter((session) => session.id !== id)); + setSettings((current) => { + if (!current.terminalBackgroundOverrides[id]) return current; + const { [id]: _, ...rest } = current.terminalBackgroundOverrides; + return { ...current, terminalBackgroundOverrides: rest }; + }); setActiveSessionId((current) => current === id ? null : current); setRenamingSessionId((current) => current === id ? null : current); }); @@ -1524,6 +1550,11 @@ export function App(): React.JSX.Element { const disposeSession = useCallback((id: string, keepEnvironmentData?: boolean): void => { void window.canvasTTY.terminal.dispose(id, keepEnvironmentData === undefined ? undefined : { keepEnvironmentData }); setSessions((current) => current.filter((session) => session.id !== id)); + setSettings((current) => { + if (!current.terminalBackgroundOverrides[id]) return current; + const { [id]: _, ...rest } = current.terminalBackgroundOverrides; + return { ...current, terminalBackgroundOverrides: rest }; + }); setActiveSessionId((current) => current === id ? null : current); setRenamingSessionId((current) => current === id ? null : current); }, []); @@ -1862,6 +1893,25 @@ export function App(): React.JSX.Element { } }, [homeEditDraft, settings.locale, showToast]); + const [backgroundsSuspended, setBackgroundsSuspended] = useState(false); + + const toggleMomEntered = useCallback(() => { + const hasAny = settings.terminalBackground !== "none" || Object.keys(settings.terminalBackgroundOverrides).length > 0; + if (!hasAny && !backgroundsSuspended) { + showToast(settings.locale === "ru" ? "Фоны не настроены" : "No backgrounds configured"); + return; + } + setBackgroundsSuspended((prev) => { + const next = !prev; + showToast( + next + ? (settings.locale === "ru" ? "Ой мама зашла!" : "Mom entered!") + : (settings.locale === "ru" ? "Фоны возвращены" : "Backgrounds restored") + ); + return next; + }); + }, [settings.locale, settings.terminalBackground, settings.terminalBackgroundOverrides, backgroundsSuspended, showToast]); + useEffect(() => { const performShortcut = (shortcut: "home" | "renameWindow" | "toggleFullscreen"): void => { if (shortcut === "toggleFullscreen") { @@ -1884,9 +1934,15 @@ export function App(): React.JSX.Element { if (shortcutReferenceOpen) return; if (handleMacNativeSelectAll(event, window.canvasTTY.window.isMacOS)) return; if (handoffRemarkIds !== null) return; - if (shouldKeepNativeKeyboardInput(event.target, window.canvasTTY.window.isMacOS, event)) return; if (usageHistoryOpen) return; if (event.repeat || isShortcutCaptureTarget(event.target) || isRenameInputTarget(event.target)) return; + if (settings.shortcuts.momEntered && matchesShortcut(event, settings.shortcuts.momEntered)) { + event.preventDefault(); + event.stopPropagation(); + toggleMomEntered(); + return; + } + if (shouldKeepNativeKeyboardInput(event.target, window.canvasTTY.window.isMacOS, event)) return; if (matchesShortcut(event, settings.shortcuts.toggleFullscreen)) { event.preventDefault(); event.stopPropagation(); @@ -1903,6 +1959,7 @@ export function App(): React.JSX.Element { event.preventDefault(); event.stopPropagation(); performShortcut("renameWindow"); + return; } }; @@ -1932,7 +1989,7 @@ export function App(): React.JSX.Element { window.removeEventListener("pointerdown", handlePointerShortcut, true); stopTerminalFocus(); }; - }, [activeSessionId, fullscreenSessionId, goHome, handoffRemarkIds, homeEditDraft, launchProvider, pendingTerminalUrl, settings.locale, settings.shortcuts, settingsOpen, shortcutReferenceOpen, showToast, usageHistoryOpen, toggleSessionFullscreen]); + }, [activeSessionId, fullscreenSessionId, goHome, handoffRemarkIds, homeEditDraft, launchProvider, pendingTerminalUrl, settings.locale, settings.shortcuts, settingsOpen, shortcutReferenceOpen, showToast, usageHistoryOpen, toggleSessionFullscreen, toggleMomEntered]); const appearance = resolveAppearanceSettings(settings); const rootClasses = useMemo( @@ -1982,6 +2039,7 @@ export function App(): React.JSX.Element { {ready && + + + diff --git a/src/renderer/src/assets/icons/lucide/rotate-cw.svg b/src/renderer/src/assets/icons/lucide/rotate-cw.svg new file mode 100644 index 000000000..6795f1576 --- /dev/null +++ b/src/renderer/src/assets/icons/lucide/rotate-cw.svg @@ -0,0 +1,14 @@ + + + + diff --git a/src/renderer/src/assets/icons/lucide/undo-2.svg b/src/renderer/src/assets/icons/lucide/undo-2.svg new file mode 100644 index 000000000..3e93594c0 --- /dev/null +++ b/src/renderer/src/assets/icons/lucide/undo-2.svg @@ -0,0 +1,14 @@ + + + + diff --git a/src/renderer/src/assets/icons/lucide/wrench.svg b/src/renderer/src/assets/icons/lucide/wrench.svg new file mode 100644 index 000000000..362434321 --- /dev/null +++ b/src/renderer/src/assets/icons/lucide/wrench.svg @@ -0,0 +1,13 @@ + + + diff --git a/src/renderer/src/components/CanvasMenuPrimitives.tsx b/src/renderer/src/components/CanvasMenuPrimitives.tsx index 1347093d0..73f4ea983 100644 --- a/src/renderer/src/components/CanvasMenuPrimitives.tsx +++ b/src/renderer/src/components/CanvasMenuPrimitives.tsx @@ -66,9 +66,10 @@ export function CanvasMenuSub({ children }: { children: ReactNode }): React.JSX. return {children}; } -export function CanvasMenuChevron(): React.JSX.Element { +/** `side`: the submenu opens to that side, and the chevron points there; without it, it points down. */ +export function CanvasMenuChevron({ side }: { side?: "left" | "right" } = {}): React.JSX.Element { return ( - + ); diff --git a/src/renderer/src/components/ShortcutReference.tsx b/src/renderer/src/components/ShortcutReference.tsx index 2f736c8af..577c531f8 100644 --- a/src/renderer/src/components/ShortcutReference.tsx +++ b/src/renderer/src/components/ShortcutReference.tsx @@ -22,6 +22,7 @@ export function ShortcutReference({ settings, onClose }: { [[bindings.commandPalette], "commandPalette"], [[bindings.openSettings], "settings"], [[bindings.toggleDetail], "keyboardToggleDetail"], + [[bindings.momEntered], "keyboardMomEntered"], [["Shift + drag"], "marqueeSelectionHint"] ] }, { title: "keyboardNavigation", shortcuts: [ diff --git a/src/renderer/src/components/UiIcon.tsx b/src/renderer/src/components/UiIcon.tsx index 27438ad18..913980ea3 100644 --- a/src/renderer/src/components/UiIcon.tsx +++ b/src/renderer/src/components/UiIcon.tsx @@ -32,13 +32,17 @@ import plusIcon from "../assets/icons/lucide/plus.svg"; import paletteIcon from "../assets/icons/lucide/palette.svg"; import pencilIcon from "../assets/icons/lucide/pencil.svg"; import searchIcon from "../assets/icons/lucide/search.svg"; +import reloadIcon from "../assets/icons/lucide/rotate-cw.svg"; +import resetIcon from "../assets/icons/lucide/rotate-ccw.svg"; import sendIcon from "../assets/icons/lucide/send.svg"; import settingsIcon from "../assets/icons/lucide/settings.svg"; import slidersHorizontalIcon from "../assets/icons/lucide/sliders-horizontal.svg"; import stickyNoteIcon from "../assets/icons/lucide/sticky-note.svg"; import terminalIcon from "../assets/icons/lucide/square-terminal.svg"; import trashIcon from "../assets/icons/lucide/trash-2.svg"; +import undoIcon from "../assets/icons/lucide/undo-2.svg"; import workingIcon from "../assets/icons/lucide/loader-circle.svg"; +import wrenchIcon from "../assets/icons/lucide/wrench.svg"; import zoomInIcon from "../assets/icons/lucide/zoom-in.svg"; import zoomOutIcon from "../assets/icons/lucide/zoom-out.svg"; @@ -62,6 +66,7 @@ export type UiIconName = | "bring-to-front" | "terminal" | "reload" + | "reset" | "arrow" | "chevron" | "download" @@ -84,6 +89,8 @@ export type UiIconName = | "clipboard-paste" | "ellipsis" | "trash" + | "undo" + | "wrench" | "working" | "attention" | "error" @@ -114,7 +121,8 @@ const ICONS: Record = { browser: browserIcon, "bring-to-front": bringToFrontIcon, terminal: terminalIcon, - reload: workingIcon, + reload: reloadIcon, + reset: resetIcon, arrow: arrowIcon, chevron: chevronIcon, download: downloadIcon, @@ -137,6 +145,8 @@ const ICONS: Record = { "clipboard-paste": clipboardPasteIcon, ellipsis: ellipsisIcon, trash: trashIcon, + undo: undoIcon, + wrench: wrenchIcon, working: workingIcon, attention: attentionIcon, error: errorIcon, diff --git a/src/renderer/src/features/browser/BrowserCard.tsx b/src/renderer/src/features/browser/BrowserCard.tsx index 457a772eb..e2c261323 100644 --- a/src/renderer/src/features/browser/BrowserCard.tsx +++ b/src/renderer/src/features/browser/BrowserCard.tsx @@ -568,7 +568,7 @@ export function BrowserCard({ {t(locale, "browserTabCrashed")} {activeTab.crashState ?? t(locale, "browserActionFailed")} - + )} diff --git a/src/renderer/src/features/settings/BackgroundAppearanceManager.tsx b/src/renderer/src/features/settings/BackgroundAppearanceManager.tsx new file mode 100644 index 000000000..33ec78b60 --- /dev/null +++ b/src/renderer/src/features/settings/BackgroundAppearanceManager.tsx @@ -0,0 +1,197 @@ +import { useCallback, useEffect, useRef, useState, type ReactNode } from "react"; +import { TerminalBackgroundPreview } from "./TerminalBackgroundPreview"; +import { SettingGroup, Segmented } from "./SettingsControls"; +import "./backgroundAppearanceManager.css"; +import type { AppSettings, SessionSnapshot } from "../../../../shared/contracts"; +import { normalizeBackgroundAppearance, type TerminalBackgroundAppearance } from "../../../../shared/terminalBackgroundAppearance"; + +export function BackgroundAppearanceManager({ settings, sessions, onChange, children, active = true }: { + settings: AppSettings; + active?: boolean; + sessions: SessionSnapshot[]; + onChange(patch: Partial): Promise; + children(appearance: TerminalBackgroundAppearance, change: (patch: Partial) => void, tab: "catalog" | "adjust", disableAll: () => void): ReactNode; +}): React.JSX.Element { + const ru = settings.locale === "ru"; + const [target, setTarget] = useState(""); + const [name, setName] = useState(""); + const [presetId, setPresetId] = useState(""); + const [error, setError] = useState(""); + const [tab, setTab] = useState("catalog"); + const scope = sessions.some(session => session.id === target) ? target : ""; + const override = scope ? settings.terminalBackgroundOverrides[scope] : undefined; + const appearance = override ?? normalizeBackgroundAppearance(settings); + const hasOverrides = Object.keys(settings.terminalBackgroundOverrides).length > 0; + const overrideCount = Object.keys(settings.terminalBackgroundOverrides).length; + const hasAnyBackground = settings.terminalBackground !== "none" || hasOverrides; + + const pendingPatch = useRef | null>(null); + const debounceTimer = useRef | null>(null); + + const flushSave = useCallback(() => { + if (debounceTimer.current) { + clearTimeout(debounceTimer.current); + debounceTimer.current = null; + } + if (pendingPatch.current) { + const p = pendingPatch.current; + pendingPatch.current = null; + void onChange(p).catch(() => setError(ru ? "Не удалось сохранить настройки." : "Could not save settings.")); + } + }, [onChange, ru]); + + useEffect(() => { + return () => { + flushSave(); + }; + }, [flushSave]); + + const save = async (patch: Partial): Promise => { + flushSave(); + try { await onChange(patch); setError(""); return true; } + catch { setError(ru ? "Не удалось сохранить настройки." : "Could not save settings."); return false; } + }; + + const change = (patch: Partial): void => { + const isImmediate = "terminalBackground" in patch || "terminalBackgroundPaused" in patch; + const nextSettingsPatch = scope ? { + terminalBackgroundOverrides: { + ...settings.terminalBackgroundOverrides, [scope]: { ...appearance, ...patch }, + } + } : (patch as Partial); + + pendingPatch.current = { ...(pendingPatch.current ?? {}), ...nextSettingsPatch }; + + if (isImmediate) { + flushSave(); + } else { + if (debounceTimer.current) clearTimeout(debounceTimer.current); + debounceTimer.current = setTimeout(flushSave, 200); + } + }; + const disableAll = (): void => { + flushSave(); + void save({ + terminalBackground: "none", + terminalBackgroundOverrides: {}, + }); + }; + const presets = settings.terminalBackgroundPresets; + const selected = presets.find(preset => preset.id === presetId); + return
+
+

{ru ? "Фоны терминалов" : "Terminal backgrounds"}

+
+
+ {[["catalog", ru ? "Каталог" : "Catalog"], ["adjust", ru ? "Настройка" : "Adjust"], ["presets", ru ? "Пресеты" : "Presets"]].map(([id, label]) => + )} +
+
+
+

{ru ? "Анимированные сцены внутри окон терминалов. Выберите фон и настройте его с живым предпросмотром." : "Animated scenes inside terminal windows. Choose a background and adjust it with a live preview."}

+ +
+ + {scope ? <> + {override ? (ru ? "У этого терминала своё оформление." : "This terminal has its own appearance.") + : (ru ? "Использует общие настройки. Любая правка создаст отдельное оформление." : "Uses defaults. Any change creates its own appearance.")} +
+ + +
+ : <> + + {hasAnyBackground && ( + + {ru ? (hasOverrides + ? `Внимание: у ${overrideCount} ${overrideCount === 1 ? "терминала" : "терминалов"} задан индивидуальный фон. Кнопка выше отключит фоны сразу на всех терминалах.` + : "Отключает фоновые анимации на всех терминалах.") + : (hasOverrides + ? `Note: ${overrideCount} terminal(s) have individual backgrounds. The button above will disable backgrounds on all terminals.` + : "Disables background animations on all terminals.")} + + )} + } +
+
+ {tab === "catalog" ? children(appearance, change, "catalog", disableAll) :
+ session.id === scope) ?? sessions[0]} /> +
+ {tab === "presets" ? <> + +
+ +
+ + + +
+
{ + event.preventDefault(); + if (!name.trim()) return; + const id = crypto.randomUUID(); + void save({ terminalBackgroundPresets: [...presets, { id, name: name.trim(), appearance: { ...appearance } }] }).then(saved => { + if (saved) { setPresetId(id); setName(""); } + }); + }}> + setName(event.currentTarget.value)} /> + +
+
+
+ : <> + {children(appearance, change, "adjust", disableAll)} + + change({ terminalBackgroundPauseOnOutput: value === "on" })} /> + + + change({ terminalBackgroundFocusDimming: value === "on" })} /> + +

{ru ? "Для всех терминалов" : "For all terminals"}

+ + void save({ terminalBackgroundSynchronized: value === "on" })} /> + + + void save({ terminalBackgroundPauseInactive: value === "on" })} /> + + } +
+
} + {error && {error}} +
; +} diff --git a/src/renderer/src/features/settings/CanvasLinkSettings.css b/src/renderer/src/features/settings/CanvasLinkSettings.css new file mode 100644 index 000000000..982d321ce --- /dev/null +++ b/src/renderer/src/features/settings/CanvasLinkSettings.css @@ -0,0 +1,10 @@ +.canvaslink-settings{max-width:54rem;color:var(--text);line-height:1.5} +.canvaslink-settings__intro{padding:.3rem 0 1.6rem}.canvaslink-settings__eyebrow{font:600 .7rem var(--font-mono,monospace);letter-spacing:.1em;color:var(--primary)} +.canvaslink-settings h3{font-size:1.05em;margin:0 0 .4rem}.canvaslink-settings__intro h3{font-size:1.55em;letter-spacing:-.025em}.canvaslink-settings p{margin:.4rem 0;color:var(--text-muted-on-dark)} +.canvaslink-settings__card{padding:1.4rem;margin-bottom:1rem;border:1px solid color-mix(in srgb,var(--text) 13%,transparent);border-radius:1rem;background:color-mix(in srgb,var(--surface-soft) 65%,var(--surface))} +.canvaslink-settings__row{display:flex;gap:1.2rem;align-items:center;justify-content:space-between}.canvaslink-settings__status{font-size:.85em}.canvaslink-settings__connection{margin-top:1.2rem;border-top:1px solid color-mix(in srgb,var(--text) 10%,transparent);padding-top:.8rem} +.canvaslink-settings ol{padding-left:1.3rem;font-size:.9em;color:var(--text-muted-on-dark)}.canvaslink-settings li+li{margin-top:.45rem}.canvaslink-settings__code-block{display:flex;flex-direction:column;align-items:flex-start;gap:.35rem;margin:1.2rem 0}.canvaslink-settings__code-block>span,.canvaslink-settings small{font-size:.8em;color:var(--text-muted-on-dark)} +.canvaslink-settings__code{font:600 clamp(1.8rem,4vw,2.8rem)/1.3 var(--font-mono,monospace);letter-spacing:.14em;color:var(--text);font-variant-numeric:tabular-nums}.canvaslink-settings__button{min-height:48px;padding:.65rem 1rem;border:1px solid color-mix(in srgb,var(--text) 25%,transparent);border-radius:.65rem;color:var(--text);background:transparent;font:inherit;font-size:.85em;cursor:pointer}.canvaslink-settings__button:hover{background:color-mix(in srgb,var(--text) 8%,transparent)}.canvaslink-settings__button--primary{border-color:var(--primary);background:var(--primary);color:var(--text-dark)}.canvaslink-settings__button--primary:hover{background:var(--primary-hover)}.canvaslink-settings__button:focus-visible{outline:2px solid var(--primary);outline-offset:3px}.canvaslink-settings__button:disabled{opacity:.55;cursor:wait} +.canvaslink-settings__card--quiet p{font-size:.85em}.canvaslink-settings__note{font-size:.9em}.canvaslink-settings__devices h4{margin:.8rem 0 .4rem}.canvaslink-settings__error{color:var(--danger)!important} +.canvaslink-settings__install-actions{display:flex;flex-wrap:wrap;gap:.6rem;margin:.9rem 0} +@media(max-width:760px){.canvaslink-settings__row{align-items:flex-start;flex-direction:column}.canvaslink-settings__card{padding:1rem}} diff --git a/src/renderer/src/features/settings/CanvasLinkSettings.tsx b/src/renderer/src/features/settings/CanvasLinkSettings.tsx new file mode 100644 index 000000000..1caa5e0c2 --- /dev/null +++ b/src/renderer/src/features/settings/CanvasLinkSettings.tsx @@ -0,0 +1,107 @@ +import { useEffect, useRef, useState } from "react"; +import type { LocaleId } from "../../../../shared/contracts"; +import type { CanvasLinkCommand, CanvasLinkState } from "../../../../shared/canvasLink"; +import { CANVASLINK_ANDROID_RELEASE, canvasLinkInstallPrompt } from "../../../../shared/canvasLinkInstall"; +import "./CanvasLinkSettings.css"; + +export function CanvasLinkSettings({ locale, open }: { locale: LocaleId; open: boolean }) { + const [state, setState] = useState(null); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(""); + const [promptCopied, setPromptCopied] = useState(false); + const revision = useRef(0); + const text = (ru: string, en: string) => locale === "ru" ? ru : en; + + function copyInstallPrompt() { + try { + window.canvasTTY.clipboard.writeText(canvasLinkInstallPrompt(locale)); + setPromptCopied(true); + } catch { + setPromptCopied(false); + setError(text("Не удалось скопировать промпт. Откройте инструкцию.", "Could not copy the prompt. Open the installation guide.")); + } + } + + useEffect(() => { + if (!open) return; + let active = true; + let timer: ReturnType; + async function poll() { + const current = revision.current; + try { + const next = await window.canvasTTY.canvasLink.state(); + if (active && current === revision.current) setState(next); + } catch { + if (active) setError(locale === "ru" ? "Не удалось получить состояние подключения." : "Could not read connection status."); + } finally { + if (active) timer = setTimeout(poll, 1000); + } + } + void poll(); + return () => { active = false; clearTimeout(timer); }; + }, [open, locale]); + + async function run(action: CanvasLinkCommand) { + revision.current++; + setBusy(true); + setError(""); + try { setState(await window.canvasTTY.canvasLink.command(action)); } + catch { setError(text("Действие не выполнено. Попробуйте ещё раз.", "The action failed. Try again.")); } + finally { revision.current++; setBusy(false); } + } + const button = (label: string, action: CanvasLinkCommand, primary = false) => ( + + ); + const enabled = !!state?.enabled; + const devices = state?.devices ?? []; + const seconds = Math.max(0, Math.ceil(((state?.expires ?? 0) - Date.now()) / 1000)); + const status = !enabled ? text("Выключено", "Off") : devices.length + ? state?.phase === "streaming" ? text("Подключено · видео и управление", "Connected · video and control") + : text("Устройство связано · подключение", "Device paired · connecting") + : text("Ожидает подключения", "Waiting for connection"); + + return
+
+

{text("ТЕЛЕФОН И ПЛАНШЕТ", "PHONE AND TABLET")}

+

{text("Ваш CanvasTTY на Android", "Your CanvasTTY on Android")}

+

{text("Работайте в этом окне с планшета или телефона. Оба устройства должны быть в одной локальной сети.", "Use this window from your tablet or phone. Both devices must be on the same local network.")}

+
+
+

{text("Установить на Android", "Install on Android")}

+

{text("Подключите телефон или планшет по USB, включите USB-отладку и подтвердите запрос на Android. Скопируйте промпт своему агенту — он проверит APK и установит приложение. После установки USB не нужен.", "Connect your phone or tablet over USB, enable USB debugging and approve the Android prompt. Copy the prompt to your agent: it will verify the APK and install the app. USB is not needed after installation.")}

+
+ + +
+ {promptCopied &&

{text("Промпт скопирован. Вставьте его в чат с агентом на этом компьютере.", "Prompt copied. Paste it into your agent chat on this computer.")}

} +

{text(`Android 8+ · ${CANVASLINK_ANDROID_RELEASE.version} · экспериментальная версия`, `Android 8+ · ${CANVASLINK_ANDROID_RELEASE.version} · experimental version`)}

+
+
+
+

{text("Доступ с Android", "Android access")}

{status}

+ {state && (enabled ? button(text("Выключить доступ", "Turn off access"), "disable") : button(text("Включить доступ", "Enable access"), "enable", true))} +
+ {!state &&

{text("Проверяем состояние…", "Checking status…")}

} + {enabled &&
+
  1. {text("Откройте CanvasLink на Android.", "Open CanvasLink on Android.")}
  2. +
  3. {text("Выберите этот компьютер и введите код. IP-адрес вводить не нужно.", "Select this computer and enter the code. No IP address is needed.")}
+ {state?.pin && seconds > 0 ?
+ {text("Одноразовый код", "One-time code")} + {state.pin.slice(0, 3)} {state.pin.slice(3)} + {text(`Действует ещё ${seconds} с`, `Valid for ${seconds} s`)} +
:

{devices.length ? text("Устройство связано. Код больше не нужен до завершения сессии.", "Device paired. No code is needed until this session ends.") : text("Срок действия кода истёк. Получите новый код.", "The code expired. Get a new code.")}

} + {!devices.length && button(text("Новый код", "New code"), "new-code")} + {devices.length > 0 &&

{text("Связанное устройство", "Paired device")}

+ {devices.map((device, index) =>

{device.name || "Android"}

)} + {button(text("Забыть устройство", "Forget device"), "revoke")}
} +
} +
+
+

{text("Управление с планшета", "Tablet controls")}

+

{text("Коснитесь терминала, чтобы открыть клавиатуру. Ввод передаётся напрямую; Enter запускает команду. При потере связи ввод автоматически не повторяется. Доступ распространяется на всё это окно CanvasTTY.", "Tap a terminal to open the keyboard. Input is sent directly; Enter runs the command. Input is never automatically replayed after a disconnect. Access covers this entire CanvasTTY window.")}

+

{text("Очки Even G2 подключаются отдельно — в разделе «Внешние интеграции». Коды и доступы независимы.", "Even G2 glasses connect separately under External integrations. Their codes and access are independent.")}

+
+ {error &&

{error}

} +
; +} diff --git a/src/renderer/src/features/settings/EvenG2Controls.tsx b/src/renderer/src/features/settings/EvenG2Controls.tsx index 03d837046..0ad9e06ec 100644 --- a/src/renderer/src/features/settings/EvenG2Controls.tsx +++ b/src/renderer/src/features/settings/EvenG2Controls.tsx @@ -443,7 +443,8 @@ export function EvenG2Controls({ ? t(locale, "evenG2AddressAvailable") : t(locale, "evenG2ConnectThisComputerToWi")}
- diff --git a/src/renderer/src/features/settings/SettingsControls.tsx b/src/renderer/src/features/settings/SettingsControls.tsx new file mode 100644 index 000000000..d40e3340a --- /dev/null +++ b/src/renderer/src/features/settings/SettingsControls.tsx @@ -0,0 +1,51 @@ +import type { ReactNode } from "react"; + +export function SettingGroup({ + label, + description, + layout = "field", + children +}: { + label: string; + description?: string; + layout?: "field" | "stacked"; + children: ReactNode; +}): React.JSX.Element { + return ( +
+
+

{label}

+ {description &&

{description}

} +
+
{children}
+
+ ); +} + +export function Segmented({ + value, + options, + wrap = false, + disabled = false, + onChange +}: { + value: string; + options: [string, string][]; + wrap?: boolean; + disabled?: boolean; + onChange(value: string): void; +}): React.JSX.Element { + return ( +
+ {options.map(([optionValue, label]) => ( + + ))} +
+ ); +} diff --git a/src/renderer/src/features/settings/SettingsPanel.tsx b/src/renderer/src/features/settings/SettingsPanel.tsx index db4619189..3fe80d6ac 100644 --- a/src/renderer/src/features/settings/SettingsPanel.tsx +++ b/src/renderer/src/features/settings/SettingsPanel.tsx @@ -1,4 +1,7 @@ +import { CanvasLinkSettings } from "./CanvasLinkSettings"; import { EvenG2Controls } from "./EvenG2Controls"; +import { BackgroundAppearanceManager } from "./BackgroundAppearanceManager"; +import { TerminalBackgroundChoices } from "./TerminalBackgroundChoices"; import { useEffect, useLayoutEffect, useRef, useState } from "react"; import type { AppSettings, @@ -121,7 +124,7 @@ import { } from "../../../../shared/autoMode"; import { MascotsSettings } from "./MascotsSettings"; -type SettingsSection = "general" | "keyboardShortcuts" | "appearance" | "agents" | "controls" | "externalIntegrations" | "browser" | "plugins" | "mascots" | "updates" | "about"; +type SettingsSection = "general" | "keyboardShortcuts" | "appearance" | "agents" | "controls" | "externalIntegrations" | "canvasLink" | "browser" | "plugins" | "mascots" | "updates" | "about"; const SHORTCUT_LABELS = { home: "homeShortcut", renameWindow: "renameWindow", toggleFullscreen: "toggleFullscreen", @@ -130,7 +133,7 @@ const SHORTCUT_LABELS = { terminalCopy: "shortcutCopySelection", terminalPaste: "shortcutPaste", terminalSearch: "terminalSearch", terminalRestart: "shortcutRestartExited", terminalPageUp: "keyboardPageUp", terminalPageDown: "keyboardPageDown", codexSubmit: "keyboardSubmit", codexSubmitAlternate: "keyboardSubmitAlternate", codexSubmitSuper: "keyboardSubmitSuper", - codexNewline: "shortcutLineBreak", codexSelectAll: "keyboardSelectAll" + codexNewline: "shortcutLineBreak", codexSelectAll: "keyboardSelectAll", momEntered: "keyboardMomEntered" } as const; const SETTINGS_SECTIONS: ReadonlyArray<{ @@ -143,6 +146,7 @@ const SETTINGS_SECTIONS: ReadonlyArray<{ { id: "agents", icon: "terminal" }, { id: "controls", icon: "sliders-horizontal" }, { id: "externalIntegrations", icon: "blocks" }, + { id: "canvasLink", icon: "app-window" }, { id: "browser", icon: "browser" }, { id: "plugins", icon: "blocks" }, { id: "mascots", icon: "image-plus" }, @@ -170,9 +174,12 @@ const DEFAULT_PROFILE_LABELS: Record = { }; interface SettingsPanelProps { + sessions: import("../../../../shared/contracts").SessionSnapshot[]; open: boolean; openUpdatesRequest: number; settings: AppSettings; + backgroundsSuspended?: boolean; + onToggleMomEntered?(): void; agentAvailability: AgentCliAvailability | null; onRecheckAgentClis(): Promise; plugins: InstalledPlugin[]; @@ -206,9 +213,12 @@ interface SettingsPanelProps { } export function SettingsPanel({ + sessions, open, openUpdatesRequest, settings, + backgroundsSuspended, + onToggleMomEntered, agentAvailability, onRecheckAgentClis, plugins, @@ -601,7 +611,7 @@ export function SettingsPanel({

{t(locale, "keyboardCaptureHint")}

{([ - ["keyboardCanvas", ["home", "renameWindow", "toggleFullscreen", "commandPalette", "openSettings", "focusUp", "focusDown", "focusLeft", "focusRight", "toggleDetail"]], + ["keyboardCanvas", ["home", "renameWindow", "toggleFullscreen", "commandPalette", "openSettings", "focusUp", "focusDown", "focusLeft", "focusRight", "toggleDetail", "momEntered"]], ["keyboardTerminal", ["terminalCopy", "terminalPaste", "terminalSearch", "terminalRestart", "terminalPageUp", "terminalPageDown"]], ["keyboardCodex", ["codexSubmit", "codexSubmitAlternate", "codexSubmitSuper", "codexNewline", "codexSelectAll"]] ] as const).map(([group, actions]) => ( @@ -769,6 +779,133 @@ export function SettingsPanel({ onChange={(terminalSkinDetail) => void onChange({ terminalSkinDetail: terminalSkinDetail as PixelSkinPreferredDetail })} /> + + {(appearance, change, tab, disableAll) => tab === "catalog" ? <> + + void onChange({ terminalBackgroundFavorites })} + deleted={settings.terminalBackgroundDeleted} + onDeletedChange={(terminalBackgroundDeleted) => void onChange({ terminalBackgroundDeleted })} + onChange={(terminalBackground) => change({ terminalBackground })} + onDisableAll={disableAll} /> +
+
+ + {locale === "ru" ? "Быстро скрыть фоны: «Ой мама зашла»" : "Quick hide backgrounds: \"Mom entered\""} + + + {locale === "ru" ? "Мгновенно гасит фоны со всех окон терминалов и возвращает назад. Настроить бинд можно в «Горячие клавиши»." : "Instantly hides all backgrounds and restores them back. Rebind in Hotkeys."} + +
+ + {settings.shortcuts.momEntered || "F8"} + +
+
+ : <> + {appearance.terminalBackground !== "none" ? <> +

{locale === "ru" ? "Цвет и читаемость" : "Color and readability"}

+ + change({ terminalBackgroundReadability: value === "on" })} /> + + + + + + + + {([ + ["terminalBackgroundContrast", "Контраст фона", "Background contrast"], + ["terminalBackgroundSaturation", "Насыщенность фона", "Background saturation"], + ] as const).map(([key, ru, en]) => ( + + + + ))} + +

{locale === "ru" ? "Масштаб и положение" : "Scale and position"}

+ {([ + ["terminalBackgroundScale", "Масштаб фона", "Background scale", 25, 300, 5], + ["terminalBackgroundOffsetX", "Смещение по горизонтали", "Horizontal offset", -50, 50, 1], + ["terminalBackgroundOffsetY", "Смещение по вертикали", "Vertical offset", -50, 50, 1], + ] as const).map(([key, ru, en, min, max, step]) => ( + + + + ))} + +

{locale === "ru" ? "Воспроизведение" : "Playback"}

+ + change({ terminalBackgroundPaused: value === "off" })} /> + + + + +

{locale === "ru" ? "Экстренное скрытие" : "Panic toggle"}

+ +
+ + {settings.shortcuts.momEntered || "F8"} + + +
+
+ :

{locale === "ru" ? "Выберите фон во вкладке «Каталог», чтобы настроить его оформление." : "Choose a background in the Catalog tab to adjust its appearance."}

} + } +
void onChange({ canvasLauncherItems: [...DEFAULT_CANVAS_LAUNCHER_ITEMS] })} >{t(locale, "resetCanvasLauncher")} @@ -1157,6 +1294,8 @@ export function SettingsPanel({ )} + {section === "canvasLink" && } + {section === "externalIntegrations" && ( )} @@ -1607,55 +1746,7 @@ function PlacementChoices({ ); } -function SettingGroup({ - label, - description, - layout = "field", - children -}: { - label: string; - description?: string; - layout?: "field" | "stacked"; - children: React.ReactNode; -}): React.JSX.Element { - return ( -
-
-

{label}

- {description &&

{description}

} -
-
{children}
-
- ); -} -function Segmented({ - value, - options, - wrap = false, - disabled = false, - onChange -}: { - value: string; - options: [string, string][]; - wrap?: boolean; - disabled?: boolean; - onChange(value: string): void; -}): React.JSX.Element { - return ( -
- {options.map(([optionValue, label]) => ( - - ))} -
- ); -} const BACKGROUND_ASSETS = import.meta.glob("../../assets/theme-backgrounds/*.avif", { eager: true, query: "?url", import: "default" @@ -2155,3 +2246,53 @@ function ColorField({ ); } + +export function SettingGroup({ + label, + description, + layout = "field", + children +}: { + label: string; + description?: string; + layout?: "field" | "stacked"; + children: React.ReactNode; +}): React.JSX.Element { + return ( +
+
+

{label}

+ {description &&

{description}

} +
+
{children}
+
+ ); +} + +export function Segmented({ + value, + options, + wrap = false, + disabled = false, + onChange +}: { + value: string; + options: [string, string][]; + wrap?: boolean; + disabled?: boolean; + onChange(value: string): void; +}): React.JSX.Element { + return ( +
+ {options.map(([optionValue, label]) => ( + + ))} +
+ ); +} diff --git a/src/renderer/src/features/settings/TerminalBackgroundChoices.tsx b/src/renderer/src/features/settings/TerminalBackgroundChoices.tsx new file mode 100644 index 000000000..9d625d2dd --- /dev/null +++ b/src/renderer/src/features/settings/TerminalBackgroundChoices.tsx @@ -0,0 +1,313 @@ +import { memo, useEffect, useRef, useState } from "react"; +import type { LocaleId } from "../../../../shared/contracts"; +import { TERMINAL_BACKGROUND_CATALOG, type TerminalBackgroundId } from "../../../../shared/terminalBackground"; +import "./terminalBackgroundChoices.css"; + +const RUSSIAN_NAMES: Partial> = { + "alpine-dawn": "Альпийский рассвет", + "analog-clock": "Стрелочные часы (Тик-так)", + "aquarium": "Аквариум с рыбками и пузырями", + "aurora": "Северное сияние над тайгой", + "aurora-fjord": "Сияние над фьордом", + "bar-chart": "Столбчатая диаграмма (Рост)", + "big-text": "Гигантский баннер", + "black-hole": "Чёрная дыра Гаргантюа", + "bonsai": "Дерево бонсай (Смена сезонов)", + "boot-log": "Лог загрузки ядра Linux", + "bouncing-balls": "Прыгающие резиновые мячи", + "box-frames": "Рамки и границы ASCII", + "butterfly": "Порхающая бабочка", + "calendar": "Настенный терминальный календарь", + "campfire": "Уютный костёр с искрами", + "candle": "Мерцающая свеча", + "candlesticks": "Японские свечи (Трейдинг)", + "cat": "Спящий рыжий кот", + "cherry-blossom": "Цветущая сакура (Лепестки на ветру)", + "chladni": "Фигуры Хладни (Песок на пластине)", + "coffee": "Чашка горячего кофе (Дымок)", + "contour-map": "Топографическая карта высот", + "cpu-meters": "Загрузка CPU (htop в стиле ASCII)", + "cube": "Вращающийся 3D-куб", + "deep-reef": "Глубоководный риф", + "desert-night": "Пустыня под звёздами", + "digital-clock": "Электронные часы 24ч", + "dissolve": "Рассыпающиеся слова", + "dividers": "Декоративные разделители", + "dna-helix": "Двойная спираль ДНК", + "donut": "Вращающийся бублик (donut.c)", + "doom-fire": "Огонь из DOOM (PSX Fire)", + "double-pendulum": "Хаотический двойной маятник", + "earth": "Вращающаяся Земля", + "earthrise": "Восход Земли над Луной", + "eclipse": "Солнечное затмение (Корона)", + "epicycles": "Эпициклы (Круги рисуют звезду)", + "equalizer": "Музыкальный эквалайзер", + "falling-sand": "Сыпучий песок", + "fern": "Папоротник Барнсли", + "ferris-wheel": "Колесо обозрения", + "file-tree": "Дерево файлов проекта", + "fireflies": "Светлячки в ночной траве", + "fireworks": "Праздничный салют", + "flag": "Флаг развевается на ветру", + "flow-field": "Векторное поле частиц", + "form-controls": "Консольная форма ввода", + "fountain": "Струи фонтана", + "fox": "Лисичка, свернувшаяся калачиком", + "fractal-tree": "Фрактальное дерево на ветру", + "galaxy": "Спиральная галактика", + "gauge": "Стрелочный спидометр / Датчик", + "glider-gun": "Пушка планеров Госпера", + "glitch": "Глитч / Битые пиксели текста", + "glxgears": "Шестерёнки GLXGears (Тест FPS)", + "gyroscope": "Гироскоп в кардановом подвесе", + "harmonograph": "Гармонограф (Кружевные кривые)", + "hawa-mahal": "Дворец ветров (Хава-Махал)", + "heart": "Биение 3D-сердца", + "heartbeat": "Пульс / Кардиограмма (ЭКГ)", + "heatmap": "Гитхаб-контрибуции (Зелёная сетка)", + "hilbert-curve": "Кривая Гильберта", + "hourglass": "Песочные часы (Время утекает)", + "icosahedron": "Проволочный икосаэдр", + "jellyfish": "Парящая глубоководная медуза", + "julia-set": "Фрактал Жюлиа", + "kite": "Воздушный змей на ветру", + "kyoto-dusk": "Сумерки в Киото", + "landscape": "Горное озеро (День и ночь)", + "langtons-ant": "Муравей Лэнгтона", + "lantern-lake": "Озеро с фонариками", + "lava-lamp": "Лавовая лампа (Релакс)", + "lighthouse": "Маяк на скалах (Луч во тьме)", + "lightning": "Грозовая молния в тучах", + "lorenz": "Аттрактор Лоренца (Крылья бабочки)", + "mandelbrot": "Фрактал Мандельброта (Зум в бездну)", + "marine-drive": "Набережная Марин-Драйв", + "marquee": "Бегущая строка (Табло)", + "matrix-rain": "Матрица: цифровой дождь", + "maze": "Генерация лабиринта", + "misty-forest": "Туманный лес", + "mobius-strip": "Лента Мёбиуса", + "moon-phases": "Фазы Луны (От новолуния к полнолунию)", + "morse": "Азбука Морзе (Точки и тире)", + "newtons-cradle": "Колыбель Ньютона (Стук шаров)", + "night-coast": "Ночное побережье", + "not-found": "Ошибка 404 (Летающее привидение)", + "ocean-sunset": "Закат над океаном", + "owl": "Сова на ветке (Моргает)", + "pendulum-wave": "Волна маятников", + "planet": "Сатурн с кольцами", + "plasma": "Олдскульная демосцен-плазма", + "plucked-string": "Колебание струны (Замедленная съёмка)", + "pond-ripples": "Круги на пруду от капель дождя", + "progress-bar": "Индикатор загрузки (99%)", + "radar": "Круговой военный радар", + "rain": "Дождь по стеклу", + "reaction-diffusion": "Реакция-диффузия Тьюринга", + "rocket": "Запуск ракеты на орбиту", + "rotozoomer": "Ротозумер (Пьяная стена)", + "rule-30": "Правило 30 (Клеточный автомат Вольфрама)", + "ruled-mountains": "Горы из линий (Плавный полёт)", + "saptarishi": "Созвездие Большой Медведицы", + "scramble": "Дешифровка текста (Хакер)", + "sea-swell": "Океанская зыбь и парусник", + "sierpinski": "Треугольник Серпинского", + "skeleton": "Скелетон-загрузчик (Шиммер)", + "skyline": "Ночной мегаполис (Огни окон)", + "smoke": "Клубы дыма", + "snake": "Змейка ползёт (Ш-ш-ш)", + "snowfall": "Тихий снегопад и сугробы", + "solar-system": "Солнечная система (Орбиты планет)", + "sparkline": "Живые графики (Спарклайны)", + "sparks": "Искры от болгарки", + "spider": "Паук спускается на паутине", + "spinners": "12 терминальных спиннеров загрузки", + "split-flap": "Табло аэропорта (Шуршание букв)", + "spring": "Пружина с грузиком", + "starfield": "Варп-прыжок сквозь звёзды", + "starlings": "Стая скворцов (Муравейник птиц)", + "storm-plains": "Штормовая равнина", + "sundial": "Солнечные часы Самрат Янтра", + "sunrise": "Рассвет над морским горизонтом", + "synthwave": "Синтвейв: неоновое солнце и сетка", + "taj-dawn": "Рассвет у Тадж-Махала", + "terminal": "Терминальная сессия (Команды и вывод)", + "tesseract": "Четырёхмерный гиперкуб (Тессеракт)", + "three-body": "Задача трёх тел (Орбита-восьмёрка)", + "tokyo-rain": "Токио под дождём", + "torus-knot": "Торический узел", + "train": "Поезд на рельсах с дымом", + "tunnel": "Психоделический 3D-туннель", + "tv-static": "Белый шум телевизора (Рябь)", + "twisted-ring": "Скрученное кольцо с одной гранью", + "typewriter": "Печатная машинка (Стук клавиш)", + "uptime-bar": "Аптайм серверов 99.9%", + "varanasi-ghats": "Гхаты Варанаси", + "vinyl": "Виниловая пластинка под иглой", + "voronoi": "Диаграмма Вороного", + "wave-interference": "Интерференция волн на воде", + "wave-text": "Текстовая волна", + "whale": "Кит ныряет в бездну", + "wind": "Порывы ветра (Стримы воздуха)", + "windmill": "Старая ветряная мельница", +}; + +const CATEGORIES = [ + ["scenes", "Сцены", "Scenes"], ["ui", "Интерфейсы", "UI"], + ["data", "Данные", "Data"], ["type", "Типографика", "Typography"], + ["shapes", "Фигуры", "Shapes"], ["space", "Космос", "Space"], + ["physics", "Физика", "Physics"], ["nature", "Природа", "Nature"], + ["creatures", "Существа", "Creatures"], ["objects", "Предметы", "Objects"], + ["generative", "Генеративная графика", "Generative"], ["effects", "Эффекты", "Effects"] +] as const; + +const POSTERS = import.meta.glob("../../assets/ascii-posters/*.webp", { eager: true, query: "?url", import: "default" }); +const ANIMATIONS = import.meta.glob("../../assets/ascii-animations/*.webp", { eager: true, query: "?url", import: "default" }); + +const ScenePreview = memo(function ScenePreview({ scene, animated }: { scene: Exclude; animated: boolean }): React.JSX.Element { + const [playing, setPlaying] = useState(false); + useEffect(() => { + if (!animated) { setPlaying(false); return; } + const timer = window.setTimeout(() => setPlaying(true), 200); + return () => window.clearTimeout(timer); + }, [animated]); + return ; +}); + +export function BackgroundPreviewFrame({ scene, playing }: { scene: Exclude; playing: boolean }): React.JSX.Element { + return ; +} + +export function TerminalBackgroundChoices({ locale, value, paused, favorites, onFavoritesChange, deleted = [], onDeletedChange, onChange, onDisableAll }: { + locale: LocaleId; + value: TerminalBackgroundId; + paused: boolean; + favorites: Exclude[]; + onFavoritesChange(value: Exclude[]): void; + deleted?: string[]; + onDeletedChange?(value: string[]): void; + onChange(value: TerminalBackgroundId): void; + onDisableAll?(): void; +}): React.JSX.Element { + const [hovered, setHovered] = useState(null); + const [focused, setFocused] = useState(null); + const [query, setQuery] = useState(""); + const [scrolling, setScrolling] = useState(false); + const scrollTimer = useRef | undefined>(undefined); + useEffect(() => () => window.clearTimeout(scrollTimer.current), []); + const [category, setCategory] = useState(() => + TERMINAL_BACKGROUND_CATALOG.find((piece) => piece.id === value)?.category ?? "scenes"); + const search = query.trim().toLowerCase(); + const favoriteIds = new Set(favorites); + const deletedIds = new Set(deleted); + const matches = TERMINAL_BACKGROUND_CATALOG.filter((piece) => { + if (category === "deleted") { + return deletedIds.has(piece.id); + } + if (deletedIds.has(piece.id)) { + return false; + } + return (category === "favorites" ? favoriteIds.has(piece.id) : piece.category === category) + && `${piece.name} ${piece.id} ${piece.note} ${RUSSIAN_NAMES[piece.id] ?? ""}`.toLowerCase().includes(search); + }); + return
+
+ + setQuery(event.currentTarget.value)} /> +
+ {category === "deleted" ? ( + + ) : ( + + )} + {onDisableAll && category !== "deleted" && ( + + )} +
+
+
{ + setScrolling(true); + window.clearTimeout(scrollTimer.current); + scrollTimer.current = window.setTimeout(() => setScrolling(false), 150); + }} + aria-label={locale === "ru" ? "Фоны выбранной категории" : "Backgrounds in the selected category"} tabIndex={0}> +
+ {matches.map(({ id, name }) =>
+ + {category === "deleted" ? ( + onDeletedChange && ( + + ) + ) : ( + onDeletedChange && ( + + ) + )} + +
)} +
+ {!matches.length &&

{category === "favorites" && !favorites.length + ? (locale === "ru" ? "В избранном пока пусто. Добавляйте фоны звёздочкой на карточке." : "No favorites yet. Use the star on a card to add a background.") + : category === "deleted" + ? (locale === "ru" ? "Корзина пуста. Удалённые фоны будут отображаться здесь." : "Trash is empty. Deleted backgrounds will appear here.") + : (locale === "ru" ? "Фоны не найдены" : "No backgrounds found")}

} +
+
; +} diff --git a/src/renderer/src/features/settings/TerminalBackgroundPreview.tsx b/src/renderer/src/features/settings/TerminalBackgroundPreview.tsx new file mode 100644 index 000000000..dcdd41acd --- /dev/null +++ b/src/renderer/src/features/settings/TerminalBackgroundPreview.tsx @@ -0,0 +1,65 @@ +import { useRef, useState, type CSSProperties } from "react"; +import type { LocaleId, SessionSnapshot } from "../../../../shared/contracts"; +import type { TerminalBackgroundAppearance } from "../../../../shared/terminalBackgroundAppearance"; +import { AsciiTerminalBackground } from "../terminal/AsciiTerminalBackground"; +import { Segmented } from "./SettingsControls"; +import "./terminalBackgroundPreview.css"; + +type PreviewShape = "terminal" | "wide" | "square" | "tall"; + +export function backgroundPreviewViewport(shape: PreviewShape, size?: { width: number; height: number }): { width: number; height: number } { + if (shape === "wide") return { width: 960, height: 540 }; + if (shape === "square") return { width: 720, height: 720 }; + if (shape === "tall") return { width: 540, height: 900 }; + return { width: Math.max(240, Math.round(size?.width ?? 960)), height: Math.max(160, Math.round(size?.height ?? 600)) }; +} + +export function TerminalBackgroundPreview({ appearance, locale, synchronized, active, session }: { + appearance: TerminalBackgroundAppearance; + locale: LocaleId; + synchronized: boolean; + active: boolean; + session?: SessionSnapshot; +}): React.JSX.Element { + const ru = locale === "ru"; + const [shape, setShape] = useState("terminal"); + const [outputCount, setOutputCount] = useState(0); + const outputActivity = useRef<(() => void) | null>(null); + const viewport = backgroundPreviewViewport(shape, session?.size); + const readable = appearance.terminalBackgroundReadability; + const lines = ["preview$ npm run build", ru ? "Сборка завершена успешно." : "Build completed successfully.", + outputCount ? (ru ? `Получен вывод #${outputCount}` : `Output received #${outputCount}`) : "preview$ _"]; + return ; +} diff --git a/src/renderer/src/features/settings/backgroundAppearanceManager.css b/src/renderer/src/features/settings/backgroundAppearanceManager.css new file mode 100644 index 000000000..e1d0f2961 --- /dev/null +++ b/src/renderer/src/features/settings/backgroundAppearanceManager.css @@ -0,0 +1,13 @@ +.terminal-background-presets { min-width: 0; display: grid; gap: .65em; } +.terminal-background-preset-save { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: stretch; gap: .6em; } +.terminal-background-action, .terminal-background-reset { min-height: 2.65em; padding: .65em .85em; justify-content: center; font-size: .82em; font-weight: 700; } +.terminal-background-reset { display: inline-flex; align-items: center; gap: .6em; justify-self: start; max-width: 100%; margin: 1em 0 .5em; } +.terminal-background-presets .terminal-background-search { font-size: .85em; } +.terminal-background-presets input { user-select: text; } +.terminal-background-presets .terminal-background-search:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; } +.terminal-background-presets :is(button:disabled, button:disabled:hover), .terminal-background-reset:disabled { opacity: .45; cursor: not-allowed; } +.terminal-background-preset-actions .segmented__button:disabled:hover { background: transparent; color: var(--text-muted-on-dark); } +.terminal-background-preset-actions .segmented__button--active:disabled:hover { background: var(--primary); color: var(--text-dark); } +@media (max-width: 700px) { + .terminal-background-preset-save { grid-template-columns: minmax(0, 1fr); } +} diff --git a/src/renderer/src/features/settings/terminalBackgroundChoices.css b/src/renderer/src/features/settings/terminalBackgroundChoices.css new file mode 100644 index 000000000..c8f1e4a25 --- /dev/null +++ b/src/renderer/src/features/settings/terminalBackgroundChoices.css @@ -0,0 +1,104 @@ +.terminal-background-preview { + position: relative; + isolation: isolate; + height: auto; + aspect-ratio: 2 / 1; + background: #07090f; +} + +.terminal-background-catalog { display: grid; gap: .8em; } +.terminal-background-toolbar { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr) auto; gap: .6em; align-items: stretch; } +.terminal-background-actions { display: flex; gap: .5em; align-items: stretch; } +.terminal-background-search { box-sizing: border-box; min-width: 0; width: 100%; padding: .7em .85em; border: 1px solid var(--text-muted-on-dark); border-radius: 7px; background: var(--surface-soft); color: var(--text); font: inherit; } +.terminal-background-disable { display: flex; align-items: center; justify-content: center; white-space: nowrap; padding: .65em .85em; } +.terminal-background-disable-all { font-size: .88em; } +.terminal-background-results { max-height: 27em; overflow-y: auto; overscroll-behavior: contain; padding: 2px 5px 4px 2px; } +.terminal-background-results:focus-visible { outline: 2px solid var(--secondary); outline-offset: 2px; } + +@media (max-width: 700px) { + .terminal-background-toolbar { grid-template-columns: minmax(0, 1fr) auto; } + .terminal-background-toolbar input { grid-column: 1 / -1; grid-row: 2; } +} + +.terminal-background-preview img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; } + +.terminal-background-tile { position: relative; min-width: 0; } +.terminal-background-tile > .border-skin-choice { width: 100%; height: 100%; } +.terminal-background-favorite { + position: absolute; + top: .9em; + right: .9em; + display: grid; + place-items: center; + width: 2em; + height: 2em; + padding: .35em; + border: 1px solid rgb(255 255 255 / 24%); + border-radius: 6px; + color: #e5e7eb; + background: rgb(7 9 15 / 88%); + cursor: pointer; +} +.terminal-background-favorite svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; } +.terminal-background-favorite:hover { color: #ffd166; border-color: currentColor; } +.terminal-background-favorite[aria-pressed="true"] { color: #ffd166; } +.terminal-background-favorite[aria-pressed="true"] svg { fill: currentColor; } +.terminal-background-favorite:focus-visible { outline: 2px solid var(--secondary); outline-offset: 2px; } + +.terminal-background-delete { + position: absolute; + top: .9em; + left: .9em; + display: grid; + place-items: center; + width: 2em; + height: 2em; + padding: .35em; + border: 1px solid rgb(255 255 255 / 24%); + border-radius: 6px; + color: #e5e7eb; + background: rgb(7 9 15 / 88%); + cursor: pointer; + opacity: 0; + transition: opacity .15s ease, color .15s ease, border-color .15s ease; +} +.terminal-background-tile:hover .terminal-background-delete, +.terminal-background-delete:focus-visible { + opacity: 1; +} +.terminal-background-delete svg { + width: 100%; + height: 100%; + fill: none; + stroke: currentColor; + stroke-width: 1.8; + stroke-linecap: round; + stroke-linejoin: round; +} +.terminal-background-delete:hover { + color: #ef4444; + border-color: #ef4444; +} + +.terminal-background-restore { + position: absolute; + top: .9em; + left: .9em; + display: inline-flex; + align-items: center; + gap: 5px; + padding: .3em .65em; + font-size: 11px; + font-weight: 600; + border: 1px solid rgb(255 255 255 / 35%); + border-radius: 6px; + color: #ffffff; + background: rgb(7 9 15 / 92%); + cursor: pointer; + transition: border-color .15s ease, color .15s ease; +} +.terminal-background-restore:hover { + color: var(--primary); + border-color: var(--primary); +} + diff --git a/src/renderer/src/features/settings/terminalBackgroundPreview.css b/src/renderer/src/features/settings/terminalBackgroundPreview.css new file mode 100644 index 000000000..2a790015c --- /dev/null +++ b/src/renderer/src/features/settings/terminalBackgroundPreview.css @@ -0,0 +1,36 @@ +.background-appearance-editor { --background-editor-surface: color-mix(in srgb, var(--surface) 86%, #000); min-width: 0; container-type: inline-size; margin: 1.6em 0; padding: 1.25em; border: 1px solid color-mix(in srgb, var(--text) 10%, transparent); border-radius: 1em; background: var(--background-editor-surface); } +.background-appearance-heading { position: sticky; top: -.35em; z-index: 3; display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; gap: 1.5em; padding: .7em 0; background: var(--background-editor-surface); border-bottom: 1px solid color-mix(in srgb, var(--text) 9%, transparent); } +.background-appearance-heading h3 { margin: 0; font-size: 1.15em; color: var(--text); } +.background-appearance-description { max-width: 68ch; padding-top: .7em; } +.background-appearance-navigation .segmented { grid-template-columns: repeat(3, minmax(0, 1fr)); } +.background-appearance-navigation .segmented__button--active:hover { background: var(--primary); color: var(--text-dark); } +.background-appearance-subheading { margin: 1.8em 0 .5em; font-size: .9em; font-weight: 700; color: var(--text); } +.background-appearance-controls > .background-appearance-subheading:first-child { margin-top: 1.3em; } +.background-appearance-editor > .setting-group--field:not(.setting-group--stacked) { grid-template-columns: minmax(0, .8fr) minmax(0, 1.2fr); } +.background-appearance-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(220px, .65fr); gap: 1.5em; align-items: start; } +.background-appearance-controls { min-width: 0; grid-column: 1; grid-row: 1; } +.background-appearance-controls .setting-group--field { grid-template-columns: minmax(0, .8fr) minmax(0, 1.2fr); gap: .8em; } +.background-appearance-controls .setting-group--stacked { grid-template-columns: minmax(0, 1fr); } +.terminal-background-live-preview { min-width: 0; grid-column: 2; grid-row: 1; position: sticky; top: 4.7em; display: grid; gap: .7em; padding: .85em; border: 1px solid transparent; border-radius: 7px; background: var(--surface-soft); margin-top: 1.1em; } +.terminal-background-live-preview h3 { margin: 0; font-size: .92em; } +.terminal-background-live-preview > .segmented { grid-template-columns: repeat(4, minmax(0, 1fr)); } +.terminal-background-live-preview .segmented__button { padding-inline: .3em; font-size: .72em; } +.terminal-background-live-preview__window { min-width: 0; overflow: hidden; border: 1px solid color-mix(in srgb, var(--primary) 45%, transparent); border-radius: 7px; } +.terminal-background-live-preview__title { display: flex; gap: .6em; align-items: center; padding: .55em .7em; color: var(--text); background: var(--surface-soft); font-size: .72em; font-weight: 700; } +.terminal-background-live-preview__title > span { color: var(--primary); } +.terminal-background-live-preview__frame { display: flex; justify-content: center; background: #07090f; } +.terminal-background-live-preview__surface { position: relative; width: min(100%, calc(32vh * var(--preview-aspect))); aspect-ratio: var(--preview-aspect); overflow: hidden; outline: 1px solid rgb(255 255 255 / 8%); } +.terminal-background-live-preview__text { position: relative; z-index: 0; margin: 0; padding: .8em; color: #f7f4ec; font: 11px/1.65 Consolas, "DejaVu Sans Mono", monospace; white-space: pre-wrap; overflow-wrap: anywhere; pointer-events: none; } +.terminal-background-live-preview__text > div + div { margin-top: .5em; } +.terminal-background-live-preview__surface--readable .terminal-background-live-preview__text span { background: rgb(2 4 10 / var(--terminal-text-backdrop-strength)); box-shadow: 0 0 3px rgb(2 4 10 / var(--terminal-text-backdrop-strength)); border-radius: 3px; } +.terminal-background-live-preview .terminal-background-action:disabled { opacity: .45; cursor: not-allowed; } +@container (max-width: 680px) { + .background-appearance-layout { display: flex; flex-direction: column; } + .terminal-background-live-preview { position: static; width: 100%; margin-top: 1em; } + .terminal-background-live-preview__surface { width: min(100%, calc(20vh * var(--preview-aspect))); } + .background-appearance-controls { width: 100%; } +} +@container (max-width: 440px) { + .background-appearance-heading { grid-template-columns: minmax(0, 1fr); gap: .6em; } + .background-appearance-editor > .setting-group--field:not(.setting-group--stacked) { grid-template-columns: minmax(0, 1fr); } +} diff --git a/src/renderer/src/features/terminal/AsciiTerminalBackground.tsx b/src/renderer/src/features/terminal/AsciiTerminalBackground.tsx new file mode 100644 index 000000000..78071a29b --- /dev/null +++ b/src/renderer/src/features/terminal/AsciiTerminalBackground.tsx @@ -0,0 +1,125 @@ +import { useEffect, useRef, type MutableRefObject } from "react"; +import { getBackgroundSyncClock, type BackgroundSyncClock } from "./backgroundSyncClock"; +import { createBackgroundOutputPause } from "./backgroundOutputPause"; +import type { TerminalBackgroundId } from "../../../../shared/terminalBackground"; +import type { BackgroundWorkerMessage } from "./asciiBackground.worker"; +import "./asciiTerminalBackground.css"; + +export function AsciiTerminalBackground({ scene, brightness, contrast, saturation, paused, speed, scale, offsetX, offsetY, live, pauseOnOutput, outputActivity, synchronized, viewport, focusDimming, focused, hidden = false }: { + scene: Exclude; + brightness: number; + contrast: number; + saturation: number; + paused: boolean; + speed: number; + scale: number; + offsetX: number; + offsetY: number; + live: boolean; + pauseOnOutput: boolean; + synchronized: boolean; + viewport?: { width: number; height: number }; + outputActivity: MutableRefObject<(() => void) | null>; + focusDimming?: boolean; + focused?: boolean; + hidden?: boolean; +}): React.JSX.Element { + const host = useRef(null); + const viewportRef = useRef(viewport); + viewportRef.current = viewport; + const updateSize = useRef<(() => void) | null>(null); + const pausedRef = useRef(paused); + const pauseOnOutputRef = useRef(pauseOnOutput); + pauseOnOutputRef.current = pauseOnOutput; + const outputPause = useRef | null>(null); + const brightnessRef = useRef(brightness); + const speedRef = useRef(speed); + const synchronizedRef = useRef(synchronized); + synchronizedRef.current = synchronized; + const syncClock = useRef(null); + const syncScene = useRef(scene); + const transformRef = useRef({ scale, offsetX, offsetY }); + const updatePlayback = useRef<(() => void) | null>(null); + pausedRef.current = paused; + brightnessRef.current = brightness; + speedRef.current = speed; + transformRef.current = { scale, offsetX, offsetY }; + useEffect(() => { + const element = host.current; + if (!element || !live) return; + const canvas = document.createElement("canvas"); + element.append(canvas); + const worker = new Worker(new URL("./asciiBackground.worker.ts", import.meta.url), { type: "module" }); + const send = (message: BackgroundWorkerMessage): void => worker.postMessage(message); + const size = (): { width: number; height: number; pixelRatio: number } => { + const width = Math.max(1, viewportRef.current?.width ?? element.clientWidth), height = Math.max(1, viewportRef.current?.height ?? element.clientHeight); + const ratio = Math.min(devicePixelRatio || 1, 2, 2048 / width, 1200 / height); + return { + width: Math.max(1, Math.round(width * ratio)), + height: Math.max(1, Math.round(height * ratio)), + pixelRatio: ratio, + }; + }; + const initialSize = size(); + canvas.width = initialSize.width; + canvas.height = initialSize.height; + const offscreen = canvas.transferControlToOffscreen(); + worker.postMessage({ type: "init", canvas: offscreen, scene, pixelRatio: initialSize.pixelRatio } satisfies BackgroundWorkerMessage, [offscreen]); + worker.onmessage = (event) => { + if (event.data?.type === "error") { + console.warn("ASCII background load failed:", scene, event.data.error); + canvas.style.display = "none"; + } + }; + worker.onerror = (event) => { + console.warn("ASCII background worker failed", scene, event.message); + canvas.style.display = "none"; + }; + const reduced = matchMedia("(prefers-reduced-motion: reduce)"); + let visible = false; + const run = (): void => { + send({ type: "transform", value: transformRef.current }); + send({ type: "speed", value: speedRef.current }); + if (syncScene.current !== scene) { syncClock.current = null; syncScene.current = scene; } + syncClock.current = synchronizedRef.current ? getBackgroundSyncClock(scene, speedRef.current, syncClock.current) : null; + send({ type: "synchronize", clock: syncClock.current }); + send({ type: "playing", value: visible && !pausedRef.current && !outputPause.current?.isPaused() && brightnessRef.current > 0 && !reduced.matches && !document.hidden }); + }; + const pause = createBackgroundOutputPause(run); + pause.setEnabled(pauseOnOutputRef.current); + outputPause.current = pause; + outputActivity.current = pause.activity; + updatePlayback.current = run; + const observer = new IntersectionObserver(([entry]) => { visible = entry.isIntersecting; run(); }); + const resize = (): void => send({ type: "resize", ...size() }); + updateSize.current = resize; + const resizer = new ResizeObserver(resize); + observer.observe(canvas); + resizer.observe(element); + document.addEventListener("visibilitychange", run); + reduced.addEventListener("change", run); + return () => { + pause.dispose(); + if (outputActivity.current === pause.activity) outputActivity.current = null; + if (outputPause.current === pause) outputPause.current = null; + observer.disconnect(); + resizer.disconnect(); + document.removeEventListener("visibilitychange", run); + reduced.removeEventListener("change", run); + worker.terminate(); + canvas.remove(); + updatePlayback.current = null; + updateSize.current = null; + }; + }, [scene, live]); + useEffect(() => { + outputPause.current?.setEnabled(pauseOnOutput); + updatePlayback.current?.(); + }, [paused, brightness, speed, scale, offsetX, offsetY, pauseOnOutput, synchronized]); + useEffect(() => { updateSize.current?.(); }, [viewport?.width, viewport?.height]); + const dimFactor = (focusDimming && focused) ? 0.35 : 1.0; + const effectiveBrightness = Math.max(5, Math.min(100, Math.round(brightness * dimFactor))); + return {!pixelControls && terminalActions} -
+
+ {hasAsciiBackground && ( + + + )} +
{pixelSkinTheme && ( @@ -1437,8 +1522,8 @@ function TerminalCardView({ ); } -function terminalTheme(palette: PaletteId, pixelSkin: string | null = null): { background: string; foreground: string; cursor: string; selectionBackground: string } { - const background = pixelSkin ? "#00000000" : palette === "night" ? "#171a24" : "#202430"; +function terminalTheme(palette: PaletteId, pixelSkin: string | null = null, ascii = false): { background: string; foreground: string; cursor: string; selectionBackground: string } { + const background = pixelSkin || ascii ? "#00000000" : palette === "night" ? "#171a24" : "#202430"; return { background, foreground: "#f7f4ec", diff --git a/src/renderer/src/features/terminal/asciiBackground.worker.ts b/src/renderer/src/features/terminal/asciiBackground.worker.ts new file mode 100644 index 000000000..91065eed8 --- /dev/null +++ b/src/renderer/src/features/terminal/asciiBackground.worker.ts @@ -0,0 +1,143 @@ +import type { AsciiPiece } from "../../../../shared/terminalBackground"; +import { drawAsciiFrame } from "./asciiFrameCanvas"; +import { createAsciiGpuRenderer } from "./asciiFrameGpu"; +import { synchronizedBackgroundDelay, synchronizedBackgroundTime, type BackgroundSyncClock } from "./backgroundSyncClock.ts"; +import type { AsciiFrameTransform } from "./asciiFrameBounds"; + +export type BackgroundWorkerMessage = + | { type: "init"; canvas: OffscreenCanvas; scene: string; pixelRatio?: number } + | { type: "resize"; width: number; height: number; pixelRatio?: number } + | { type: "speed"; value: number } + | { type: "transform"; value: AsciiFrameTransform } + | { type: "synchronize"; clock: BackgroundSyncClock | null } + | { type: "playing"; value: boolean }; + +let canvas: OffscreenCanvas; +let ctx: OffscreenCanvasRenderingContext2D | null; +let gpu: ReturnType; +let piece: AsciiPiece; +let frame: ReturnType | undefined; +let color: Uint8Array | undefined; +let text: string | undefined; +let pixelRatio = 1; +let playing = false, time = 0, last = 0; +let speed = 1; +let syncClock: BackgroundSyncClock | null = null; +let transform: AsciiFrameTransform = { scale: 100, offsetX: 0, offsetY: 0 }; +let timer: ReturnType | undefined; + +function draw(advance = true): void { + if (!frame || (!ctx && !gpu)) return; + try { + if (advance || text === undefined) text = frame(time, { paper: false, color }); + if (gpu) gpu(text, color, pixelRatio, transform); + else if (ctx) drawAsciiFrame(ctx, text, piece.meta, color, pixelRatio, transform); + } catch { + if (gpu) { + gpu = null; + ctx = canvas.getContext("2d", { alpha: false }); + if (ctx && text) drawAsciiFrame(ctx, text, piece.meta, color, pixelRatio, transform); + } + } +} + +function advanceTime(now: number): void { + if (syncClock) { + time = (piece.meta.still ?? 0) + synchronizedBackgroundTime(syncClock, performance.timeOrigin + now, Math.min(piece.meta.fps || 12, 12)); + } else time += Math.min(Math.max((now - last) / 1000, 0), 0.5) * speed; + last = now; +} + +function tick(): void { + timer = undefined; + if (!playing || !frame) return; + const start = performance.now(); + advanceTime(start); + draw(); + const cost = performance.now() - start; + // Leave most of the worker's time free on slower machines. + const delay = syncClock + ? synchronizedBackgroundDelay(syncClock, performance.timeOrigin + performance.now(), Math.min(piece.meta.fps || 12, 12), cost * 3) + : Math.max(1000 / Math.min(piece.meta.fps ?? 12, 12) - cost, cost * 3); + timer = setTimeout(tick, delay); +} + +function run(): void { + clearTimeout(timer); + timer = undefined; + if (playing && frame && piece.meta.fps !== 0) { + last = performance.now(); + timer = setTimeout(tick, 0); + } +} + +self.onmessage = async ({ data }: MessageEvent): Promise => { + if (data.type === "init") { + canvas = data.canvas; + pixelRatio = data.pixelRatio ?? 1; + try { + piece = await import(`ascii.rest/pieces/${data.scene}`); + } catch { + try { + piece = await import(`../../../../../node_modules/ascii.rest/dist/pieces/${data.scene}.js`); + } catch (err) { + if (typeof self.postMessage === "function") { + self.postMessage({ type: "error", error: err instanceof Error ? err.message : String(err) }); + } + return; + } + } + if (typeof canvas.addEventListener === "function") { + canvas.addEventListener("webglcontextlost", (event) => { + event.preventDefault(); + gpu = null; + ctx = canvas.getContext("2d", { alpha: false }); + draw(false); + }); + } + try { + gpu = createAsciiGpuRenderer(canvas, piece.meta); + } catch { + gpu = null; + } + ctx = gpu ? null : canvas.getContext("2d", { alpha: false }); + frame = piece.default({ ...piece.meta.options }); + color = piece.meta.palette ? new Uint8Array((piece.meta.cols ?? 80) * (piece.meta.rows ?? 24)) : undefined; + time = piece.meta.still ?? 0; + if (syncClock) advanceTime(performance.now()); + draw(); + run(); + } else if (data.type === "resize") { + if (canvas.width === data.width && canvas.height === data.height && pixelRatio === (data.pixelRatio ?? 1)) return; + pixelRatio = data.pixelRatio ?? 1; + canvas.width = data.width; + canvas.height = data.height; + draw(false); + } else if (data.type === "transform") { + const finite = (value: number, fallback: number, min: number, max: number): number => + Number.isFinite(value) ? Math.min(max, Math.max(min, value)) : fallback; + const next = { scale: finite(data.value.scale, 100, 25, 300), + offsetX: finite(data.value.offsetX, 0, -50, 50), offsetY: finite(data.value.offsetY, 0, -50, 50) }; + if (next.scale === transform.scale && next.offsetX === transform.offsetX && next.offsetY === transform.offsetY) return; + transform = next; + draw(false); + } else if (data.type === "speed") { + const next = Number.isFinite(data.value) ? Math.min(2, Math.max(0.25, data.value)) : 1; + if (next === speed) return; + if (playing && frame) advanceTime(performance.now()); + speed = next; + } else if (data.type === "synchronize") { + const next = data.clock; + if (syncClock?.origin === next?.origin && syncClock?.time === next?.time && syncClock?.speed === next?.speed) return; + syncClock = next; + if (frame && syncClock) { + advanceTime(performance.now()); + draw(); + } + run(); + } else { + if (playing === data.value) return; + playing = data.value; + run(); + } +}; diff --git a/src/renderer/src/features/terminal/asciiFineDots.ts b/src/renderer/src/features/terminal/asciiFineDots.ts new file mode 100644 index 000000000..73e49024f --- /dev/null +++ b/src/renderer/src/features/terminal/asciiFineDots.ts @@ -0,0 +1,34 @@ +// Unicode Braille bit order, with a fixed spatial threshold for stable video dithering. +export const BRAILLE_DOTS = [ + { x: 0, y: 0, bit: 1, threshold: .0625 }, + { x: 1, y: 0, bit: 8, threshold: .5625 }, + { x: 0, y: 1, bit: 2, threshold: .8125 }, + { x: 1, y: 1, bit: 16, threshold: .3125 }, + { x: 0, y: 2, bit: 4, threshold: .4375 }, + { x: 1, y: 2, bit: 32, threshold: .9375 }, + { x: 0, y: 3, bit: 64, threshold: .6875 }, + { x: 1, y: 3, bit: 128, threshold: .1875 }, +] as const; + +export function appendFineDots(ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D, + char: string, x: number, y: number, height: number): void { + const mask = char.charCodeAt(0) - 0x2800; + if (mask <= 0 || mask > 255) return; + const radius = Math.min(3, height / 4) * .4; + for (const dot of BRAILLE_DOTS) { + if (!(mask & dot.bit)) continue; + const cx = x + (dot.x + .5) * 3, cy = y + (dot.y + .5) * height / 4; + ctx.moveTo(cx + radius, cy); + ctx.arc(cx, cy, radius, 0, Math.PI * 2); + } +} + +export function fineDotSvgDefinitions(height: number): string { + const radius = Math.min(3, height / 4) * .4; + return "" + Array.from({ length: 255 }, (_, i) => { + const mask = i + 1; + const circles = BRAILLE_DOTS.filter(dot => mask & dot.bit).map(dot => + ``).join(""); + return `${circles}`; + }).join("") + ""; +} diff --git a/src/renderer/src/features/terminal/asciiFrameBounds.ts b/src/renderer/src/features/terminal/asciiFrameBounds.ts new file mode 100644 index 000000000..653847bd8 --- /dev/null +++ b/src/renderer/src/features/terminal/asciiFrameBounds.ts @@ -0,0 +1,22 @@ +import type { AsciiPiece } from "../../../../shared/terminalBackground"; + +export interface AsciiFrameTransform { scale: number; offsetX: number; offsetY: number } + +export function asciiFrameBounds(width: number, height: number, meta: AsciiPiece["meta"], pixelRatio = 1, + transform: AsciiFrameTransform = { scale: 100, offsetX: 0, offsetY: 0 }): { x: number; y: number; width: number; height: number } { + const { cols = 80, rows = 24, cell = 2 } = meta; + const sceneWidth = cols * 6; + const sceneHeight = rows * cell * 6; + const fit = Math.min(width / sceneWidth, height / sceneHeight); + const containWidth = sceneWidth * fit; + const containHeight = sceneHeight * fit; + const scale = Math.min(width * 0.8 / sceneWidth, height * 0.8 / sceneHeight, 1.25 * pixelRatio); + const zoom = transform.scale / 100; + const aspect = sceneWidth / sceneHeight; + const coverWidth = Math.max(width, height * aspect); + const coverHeight = Math.max(height, width / aspect); + const artWidth = (meta.contain ? containWidth : meta.fullBleed || cell === 1 ? width : meta.cover ? coverWidth : Math.min(width, cols * 6 * scale)) * zoom; + const artHeight = (meta.contain ? containHeight : meta.fullBleed || cell === 1 ? height : meta.cover ? coverHeight : Math.min(height, rows * cell * 6 * scale)) * zoom; + return { x: (width - artWidth) / 2 + width * transform.offsetX / 100, + y: (height - artHeight) / 2 + height * transform.offsetY / 100, width: artWidth, height: artHeight }; +} diff --git a/src/renderer/src/features/terminal/asciiFrameCanvas.ts b/src/renderer/src/features/terminal/asciiFrameCanvas.ts new file mode 100644 index 000000000..67101b6a7 --- /dev/null +++ b/src/renderer/src/features/terminal/asciiFrameCanvas.ts @@ -0,0 +1,38 @@ +import type { AsciiPiece } from "../../../../shared/terminalBackground"; +import { appendFineDots } from "./asciiFineDots.ts"; +import { asciiFrameBounds, type AsciiFrameTransform } from "./asciiFrameBounds.ts"; + +export function drawAsciiFrame(ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D, text: string, meta: AsciiPiece["meta"], color?: Uint8Array, pixelRatio = 1, transform?: AsciiFrameTransform): void { + const { cols = 80, rows = 24, cell = 2, palette, ground = "#07090f" } = meta; + const width = cols * 6, height = rows * cell * 6; + const bounds = asciiFrameBounds(ctx.canvas.width, ctx.canvas.height, meta, pixelRatio, transform); + ctx.setTransform(1, 0, 0, 1, 0, 0); + ctx.fillStyle = ground; + ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); + ctx.setTransform(bounds.width / width, 0, 0, bounds.height / height, bounds.x, bounds.y); + ctx.font = "10px Consolas, 'DejaVu Sans Mono', monospace"; + ctx.textBaseline = "middle"; + const lines = text.split("\n"); + for (let y = 0; y < Math.min(lines.length, rows); y++) { + const chars = [...lines[y]].slice(0, cols); + ctx.save(); + ctx.beginPath(); + ctx.rect(0, y * cell * 6, width, cell * 6); + ctx.clip(); + for (let x = 0; x < chars.length;) { + const ink = palette?.[color?.[y * cols + x] ?? 0] ?? "#f7f4ec"; + const start = x++; + while (x < chars.length && (palette?.[color?.[y * cols + x] ?? 0] ?? "#f7f4ec") === ink) x++; + const run = chars.slice(start, x).join(""); + if (run.trim()) { + ctx.fillStyle = ink; + if (meta.fineDots) { + ctx.beginPath(); + for (let column = start; column < x; column++) appendFineDots(ctx, chars[column], column * 6, y * cell * 6, cell * 6); + ctx.fill(); + } else ctx.fillText(run, start * 6, (y + 0.5) * cell * 6, (x - start) * 6); + } + } + ctx.restore(); + } +} diff --git a/src/renderer/src/features/terminal/asciiFrameGpu.ts b/src/renderer/src/features/terminal/asciiFrameGpu.ts new file mode 100644 index 000000000..35fc6c8df --- /dev/null +++ b/src/renderer/src/features/terminal/asciiFrameGpu.ts @@ -0,0 +1,133 @@ +import type { AsciiPiece } from "../../../../shared/terminalBackground"; +import { appendFineDots } from "./asciiFineDots.ts"; +import { asciiFrameBounds, type AsciiFrameTransform } from "./asciiFrameBounds.ts"; + +export function createAsciiGpuRenderer(canvas: OffscreenCanvas, meta: AsciiPiece["meta"]): ((text: string, color?: Uint8Array, pixelRatio?: number, transform?: AsciiFrameTransform) => void) | null { + const context = canvas.getContext("webgl2", { alpha: false, antialias: false, depth: false, stencil: false }); + if (!context) return null; + const gl = context; + const { cols = 80, rows = 24, cell = 2, palette = ["#f7f4ec"], ground = "#07090f" } = meta; + const shader = (type: number, source: string): WebGLShader => { + const result = gl.createShader(type)!; + gl.shaderSource(result, source); + gl.compileShader(result); + if (!gl.getShaderParameter(result, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(result) ?? "ASCII shader failed"); + return result; + }; + const program = gl.createProgram()!; + gl.attachShader(program, shader(gl.VERTEX_SHADER, `#version 300 es + void main() { + vec2 p = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2); + gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0); + }`)); + gl.attachShader(program, shader(gl.FRAGMENT_SHADER, `#version 300 es + precision highp float; + uniform sampler2D glyphs, grid, inks; + uniform vec2 viewport, cells, atlasSize, tileSize, glyphSize; + uniform vec4 sceneRect; + uniform float atlasColumns; + uniform vec3 ground; + out vec4 outputColor; + void main() { + vec2 uv = (vec2(gl_FragCoord.x, viewport.y - gl_FragCoord.y) - sceneRect.xy) / sceneRect.zw; + if (any(lessThan(uv, vec2(0.0))) || any(greaterThanEqual(uv, vec2(1.0)))) { + outputColor = vec4(ground, 1.0); + return; + } + vec2 p = uv * cells; + vec4 data = texelFetch(grid, ivec2(p), 0); + float slot = round(data.r * 255.0) + round(data.g * 255.0) * 256.0; + vec2 origin = vec2(mod(slot, atlasColumns), floor(slot / atlasColumns)) * tileSize; + float alpha = texture(glyphs, (origin + vec2(4.0) + fract(p) * glyphSize) / atlasSize).a; + vec3 ink = texelFetch(inks, ivec2(round(data.b * 255.0), 0), 0).rgb; + outputColor = vec4(mix(ground, ink, alpha), 1.0); + }`)); + gl.linkProgram(program); + if (!gl.getProgramParameter(program, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(program) ?? "ASCII program failed"); + gl.useProgram(program); + const uniform = (name: string): WebGLUniformLocation | null => gl.getUniformLocation(program, name); + const viewport = uniform("viewport"), atlasSize = uniform("atlasSize"), tileSize = uniform("tileSize"), glyphSize = uniform("glyphSize"); + const atlasColumns = uniform("atlasColumns"); + const sceneRect = uniform("sceneRect"); + gl.uniform2f(uniform("cells"), cols, rows); + function texture(unit: number, name: string, filter: number): WebGLTexture { + const result = gl.createTexture()!; + gl.activeTexture(gl.TEXTURE0 + unit); + gl.bindTexture(gl.TEXTURE_2D, result); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); + gl.uniform1i(uniform(name), unit); + return result; + } + const glyphTexture = texture(0, "glyphs", gl.LINEAR); + const gridTexture = texture(1, "grid", gl.NEAREST); + const data = new Uint8Array(cols * rows * 4); + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, cols, rows, 0, gl.RGBA, gl.UNSIGNED_BYTE, data); + texture(2, "inks", gl.NEAREST); + const swatches = new OffscreenCanvas(palette.length + 1, 1); + const swatch = swatches.getContext("2d")!; + [...palette, ground].forEach((ink, index) => { swatch.fillStyle = ink; swatch.fillRect(index, 0, 1, 1); }); + const rgba = swatch.getImageData(0, 0, palette.length + 1, 1).data; + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, palette.length, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, rgba.subarray(0, palette.length * 4)); + gl.uniform3f(uniform("ground"), rgba[palette.length * 4] / 255, rgba[palette.length * 4 + 1] / 255, rgba[palette.length * 4 + 2] / 255); + const slots = new Map([[" ", 0]]); + const atlas = new OffscreenCanvas(1, 1); + const ink = atlas.getContext("2d")!; + let atlasCount = 0, scale = 0; + return (text, color, pixelRatio = 1, transform) => { + const bounds = asciiFrameBounds(canvas.width, canvas.height, meta, pixelRatio, transform); + data.fill(0); + let x = 0, y = 0; + for (const char of text) { + if (char === "\n") { x = 0; if (++y >= rows) break; continue; } + if (x >= cols) { x++; continue; } + let slot = slots.get(char); + if (slot === undefined) { slot = slots.size; slots.set(char, slot); } + const index = (y * cols + x) * 4; + data[index] = slot & 255; + data[index + 1] = slot >> 8; + data[index + 2] = color?.[y * cols + x] ?? 0; + x++; + } + const neededScale = Math.max(4, Math.ceil(Math.max(bounds.width / (cols * 6), bounds.height / (rows * cell * 6)))); + if (atlasCount !== slots.size || scale < neededScale) { + scale = Math.max(scale, neededScale); + atlasCount = slots.size; + const across = Math.min(16, slots.size), down = Math.ceil(slots.size / across); + const w = 6 * scale + 8, h = cell * 6 * scale + 8; + atlas.width = across * w; + atlas.height = down * h; + for (const [char, slot] of slots) { + ink.save(); + ink.setTransform(scale, 0, 0, scale, (slot % across) * w + 4, Math.floor(slot / across) * h + 4); + ink.beginPath(); ink.rect(0, 0, 6, cell * 6); ink.clip(); + ink.fillStyle = "white"; + ink.font = "10px Consolas, 'DejaVu Sans Mono', monospace"; + ink.textBaseline = "middle"; + if (meta.fineDots) { + ink.beginPath(); + appendFineDots(ink, char, 0, 0, cell * 6); + ink.fill(); + } else ink.fillText(char, 0, cell * 3, 6); + ink.restore(); + } + gl.activeTexture(gl.TEXTURE0); + gl.bindTexture(gl.TEXTURE_2D, glyphTexture); + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, atlas); + gl.uniform2f(atlasSize, atlas.width, atlas.height); + gl.uniform2f(tileSize, w, h); + gl.uniform2f(glyphSize, 6 * scale, cell * 6 * scale); + gl.uniform1f(atlasColumns, across); + } + gl.activeTexture(gl.TEXTURE1); + gl.bindTexture(gl.TEXTURE_2D, gridTexture); + gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, cols, rows, gl.RGBA, gl.UNSIGNED_BYTE, data); + gl.viewport(0, 0, canvas.width, canvas.height); + gl.uniform2f(viewport, canvas.width, canvas.height); + gl.uniform4f(sceneRect, bounds.x, bounds.y, bounds.width, bounds.height); + gl.drawArrays(gl.TRIANGLES, 0, 3); + gl.flush(); + }; +} diff --git a/src/renderer/src/features/terminal/asciiFrameSvg.ts b/src/renderer/src/features/terminal/asciiFrameSvg.ts new file mode 100644 index 000000000..d6e284409 --- /dev/null +++ b/src/renderer/src/features/terminal/asciiFrameSvg.ts @@ -0,0 +1,38 @@ +import { fineDotSvgDefinitions } from "./asciiFineDots.ts"; +import type { AsciiPiece } from "../../../../shared/terminalBackground"; + +const escape = (value: string): string => value.replaceAll("&", "&") + .replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """); + +export function asciiFrameSvg(text: string, meta: AsciiPiece["meta"], color?: Uint8Array): string { + const { cols = 80, rows = 24, cell = 2, palette, ground = "#07090f" } = meta; + const width = cols * 6, height = rows * cell * 6; + const lines = text.split("\n"); + if (meta.fineDots) { + let dots = ""; + for (let y = 0; y < Math.min(lines.length, rows); y++) { + const chars = [...lines[y]].slice(0, cols); + for (let x = 0; x < chars.length; x++) { + const mask = chars[x].charCodeAt(0) - 0x2800; + if (mask <= 0 || mask > 255) continue; + const ink = palette?.[color?.[y * cols + x] ?? 0] ?? "#f7f4ec"; + dots += ``; + } + } + return `${fineDotSvgDefinitions(cell * 6)}${dots}`; + } + let content = ""; + for (let y = 0; y < Math.min(lines.length, rows); y++) { + const chars = [...lines[y]].slice(0, cols); + let spans = ""; + for (let x = 0; x < chars.length;) { + const ink = palette?.[color?.[y * cols + x] ?? 0] ?? "#f7f4ec"; + const start = x++; + while (x < chars.length && (palette?.[color?.[y * cols + x] ?? 0] ?? "#f7f4ec") === ink) x++; + const run = chars.slice(start, x).join(""); + if (run.trim()) spans += `${escape(run)}`; + } + content += `${spans}`; + } + return `${content}`; +} diff --git a/src/renderer/src/features/terminal/asciiTerminalBackground.css b/src/renderer/src/features/terminal/asciiTerminalBackground.css new file mode 100644 index 000000000..ef514d455 --- /dev/null +++ b/src/renderer/src/features/terminal/asciiTerminalBackground.css @@ -0,0 +1,59 @@ +.terminal-card__surface--ascii { + isolation: isolate; + background: #07090f !important; +} + +.terminal-ascii-background { + color: var(--terminal-foreground, #f7f4ec); + position: absolute; + inset: 0; + z-index: -1; + overflow: hidden; + pointer-events: none; + background: #07090f; +} + +.terminal-ascii-background canvas { + display: block; + width: 100%; + height: 100%; + object-fit: fill; + image-rendering: auto; +} + +.terminal-card__surface--ascii .xterm-viewport { + background: transparent !important; +} + +.terminal-ascii-text-backdrop { + display: none; + position: absolute; + inset: 0; + width: 100%; + height: 100%; + z-index: -1; + pointer-events: none; +} + +.terminal-card__surface--ascii-readable .terminal-ascii-text-backdrop { + display: block; +} + +.terminal-card__surface--ascii-readable .xterm-rows > div > span, +.terminal-card__surface--ascii-readable .terminal-card__pinned-input { + -webkit-text-stroke: 0.4px #02040a; + paint-order: stroke fill; + text-shadow: 0 0 1px #02040a, 0 0 2px #02040a, + 0 0 4px #02040a, 0 1px 6px rgb(2 4 10 / 95%); +} + +.terminal-card__surface--ascii .terminal-card__pinned-input { + background: transparent; + box-shadow: none; +} + +.terminal-card__surface--ascii-readable .terminal-card__pinned-input-row > span { + background: rgb(2 4 10 / var(--terminal-text-backdrop-strength, 0.86)); + border-radius: 3px; + box-shadow: 0 0 3px rgb(2 4 10 / var(--terminal-text-backdrop-strength, 0.86)); +} diff --git a/src/renderer/src/features/terminal/backgroundOutputPause.ts b/src/renderer/src/features/terminal/backgroundOutputPause.ts new file mode 100644 index 000000000..78660db00 --- /dev/null +++ b/src/renderer/src/features/terminal/backgroundOutputPause.ts @@ -0,0 +1,28 @@ +export function createBackgroundOutputPause(onChange: () => void): { + activity(): void; + setEnabled(value: boolean): void; + isPaused(): boolean; + dispose(): void; +} { + let enabled = false; + let paused = false; + let timer: ReturnType | undefined; + const clear = (): void => { clearTimeout(timer); timer = undefined; }; + return { + activity() { + if (!enabled) return; + clear(); + if (!paused) { paused = true; onChange(); } + timer = setTimeout(() => { timer = undefined; paused = false; onChange(); }, 1000); + }, + setEnabled(value) { + enabled = value; + if (!enabled) { + clear(); + if (paused) { paused = false; onChange(); } + } + }, + isPaused: () => paused, + dispose() { enabled = false; paused = false; clear(); }, + }; +} diff --git a/src/renderer/src/features/terminal/backgroundSyncClock.ts b/src/renderer/src/features/terminal/backgroundSyncClock.ts new file mode 100644 index 000000000..d78a26fd7 --- /dev/null +++ b/src/renderer/src/features/terminal/backgroundSyncClock.ts @@ -0,0 +1,25 @@ +export interface BackgroundSyncClock { origin: number; time: number; speed: number } + +const clocks = new Map(); + +export function getBackgroundSyncClock(scene: string, speed: number, previous: BackgroundSyncClock | null): BackgroundSyncClock { + const key = JSON.stringify([scene, speed]); + let clock = clocks.get(key); + if (!clock) { + const now = performance.timeOrigin + performance.now(); + clock = { origin: now, time: previous ? previous.time + Math.max(0, now - previous.origin) / 1000 * previous.speed : 0, speed }; + clocks.set(key, clock); + } + return clock; +} + +export function synchronizedBackgroundTime(clock: BackgroundSyncClock, now: number, fps: number): number { + const period = 1000 / fps; + return clock.time + Math.floor(Math.max(0, now - clock.origin) / period + 1e-8) * period / 1000 * clock.speed; +} + +export function synchronizedBackgroundDelay(clock: BackgroundSyncClock, now: number, fps: number, minimumDelay: number): number { + const period = 1000 / fps; + const next = clock.origin + (Math.floor((now + Math.max(0, minimumDelay) - clock.origin) / period + 1e-8) + 1) * period; + return Math.max(1, next - now); +} diff --git a/src/renderer/src/features/terminal/terminalTextBackdrop.ts b/src/renderer/src/features/terminal/terminalTextBackdrop.ts new file mode 100644 index 000000000..fddbba589 --- /dev/null +++ b/src/renderer/src/features/terminal/terminalTextBackdrop.ts @@ -0,0 +1,78 @@ +import type { IBufferLine, Terminal } from "@xterm/xterm"; + +export function terminalTextRuns(line: IBufferLine | undefined, columns: number): [number, number][] { + const runs: [number, number][] = []; + let start = -1, end = 0; + for (let column = 0; line && column < columns; column++) { + const cell = line.getCell(column); + if (cell?.getChars().trim()) { + if (start < 0) start = column; + end = Math.min(columns, column + Math.max(1, cell.getWidth())); + } else if (start >= 0 && column >= end) { + runs.push([start, end]); + start = -1; + } + } + if (start >= 0) runs.push([start, end]); + return runs; +} + +export function attachTerminalTextBackdrop(terminal: Terminal, host: HTMLElement): () => void { + const screen = terminal.element?.querySelector(".xterm-screen"); + if (!screen) return () => {}; + let canvas: HTMLCanvasElement | undefined; + let frame: number | undefined; + const draw = (): void => { + frame = undefined; + if (!host.classList.contains("terminal-card__surface--ascii-readable")) return; + const width = screen.clientWidth, height = screen.clientHeight; + if (!width || !height || !screen.checkVisibility({ visibilityProperty: true })) return; + if (!canvas) { + canvas = document.createElement("canvas"); + canvas.className = "terminal-ascii-text-backdrop"; + canvas.setAttribute("aria-hidden", "true"); + screen.append(canvas); + } + const ratio = Math.min(devicePixelRatio || 1, 2); + const pixelWidth = Math.round(width * ratio), pixelHeight = Math.round(height * ratio); + if (canvas.width !== pixelWidth || canvas.height !== pixelHeight) { + canvas.width = pixelWidth; + canvas.height = pixelHeight; + } + const context = canvas.getContext("2d"); + if (!context) return; + context.setTransform(ratio, 0, 0, ratio, 0, 0); + context.clearRect(0, 0, width, height); + const configured = Number.parseFloat(getComputedStyle(host).getPropertyValue("--terminal-text-backdrop-strength")); + const strength = Number.isFinite(configured) ? Math.min(1, Math.max(0, configured)) : 0.86; + context.fillStyle = `rgba(2, 4, 10, ${strength})`; + context.shadowColor = `rgba(2, 4, 10, ${Math.min(1, strength * 90 / 86)})`; + context.shadowBlur = 3 * ratio; + const cellWidth = width / terminal.cols, cellHeight = height / terminal.rows; + const buffer = terminal.buffer.active; + for (let row = 0; row < terminal.rows; row++) { + for (const [start, end] of terminalTextRuns(buffer.getLine(buffer.viewportY + row), terminal.cols)) { + context.beginPath(); + context.roundRect(start * cellWidth - 1, row * cellHeight + 1, + (end - start) * cellWidth + 2, Math.max(1, cellHeight - 2), 3); + context.fill(); + } + } + }; + // Terminal events update the mask; background animation never triggers this work. + const schedule = (): void => { + if (frame === undefined && host.classList.contains("terminal-card__surface--ascii-readable")) { + frame = requestAnimationFrame(draw); + } + }; + const subscriptions = [terminal.onRender(schedule), terminal.onScroll(schedule), terminal.onResize(schedule)]; + const observer = new ResizeObserver(schedule); + observer.observe(screen); + schedule(); + return () => { + if (frame !== undefined) cancelAnimationFrame(frame); + subscriptions.forEach(subscription => subscription.dispose()); + observer.disconnect(); + canvas?.remove(); + }; +} diff --git a/src/renderer/src/features/workspace/CanvasAutoSortMenu.tsx b/src/renderer/src/features/workspace/CanvasAutoSortMenu.tsx new file mode 100644 index 000000000..2620fa6e3 --- /dev/null +++ b/src/renderer/src/features/workspace/CanvasAutoSortMenu.tsx @@ -0,0 +1,116 @@ +import type { LocaleId } from "../../../../shared/contracts"; +import { CanvasMenuDivider, CanvasMenuLabel, CanvasMenuRow } from "../../components/CanvasMenuPrimitives"; +import { UiIcon } from "../../components/UiIcon"; +import { t } from "../../lib/i18n"; +import { planCanvasAutoSort } from "./canvasAutoSort"; +import { + CANVAS_AUTO_SORT_MODES, + canvasAutoSortMoves, + canvasAutoSortScope, + canvasAutoSortUndoTargets, + type CanvasAutoSortCandidate, + type CanvasAutoSortHost, + type CanvasAutoSortMode +} from "./canvasAutoSortScope"; +import { parseCanvasLayerId, pluginLayerId, terminalLayerId } from "./canvasSelectionGesture"; + +/** + * The body of the empty-canvas Tools submenu. The context menu loads it, with the layout code, + * the first time Tools opens, so none of it is in the startup bundle. + */ +const LABELS = { + task: "canvasAutoSortTask", + status: "canvasAutoSortStatus", + project: "canvasAutoSortProject", + grid: "canvasAutoSortGrid" +} as const satisfies Record[1]>; + +/** The windows an auto-sort would move right now (see canvasAutoSortScope). */ +export function canvasAutoSortIds(host: CanvasAutoSortHost): string[] { + const mascots = new Set(host.pluginCanvas + .filter((instance) => host.plugins.find((plugin) => plugin.manifest.id === instance.pluginId)?.sourceUrl.startsWith("mascot:")) + .map((instance) => pluginLayerId(instance.id))); + const candidates: CanvasAutoSortCandidate[] = []; + for (const [id, bounds] of host.bounds) { + const kind = parseCanvasLayerId(id)?.kind; + if (kind) candidates.push({ id, kind, bounds, decorative: mascots.has(id) }); + } + return canvasAutoSortScope(candidates, host.selection, host.regions); +} + +/** + * Sort by `mode` and remember the step for undo; the criterion is kept only once a sort happened. + * A sort that moves nothing (the layout already follows the criterion) keeps the earlier undo step. + */ +export function runCanvasAutoSort(host: CanvasAutoSortHost, mode: CanvasAutoSortMode): void { + const plan = planCanvasAutoSort(canvasAutoSortIds(host), host.bounds, host.sessions, terminalLayerId, mode, { + avoid: [host.home, ...host.regions], + snapToGrid: host.snapToGrid + }); + if (!plan) return; + if (!canvasAutoSortMoves(plan)) { + if (mode !== host.state.mode) host.setState({ mode, undo: host.state.undo }); + return; + } + host.commit(plan.after); + host.setState({ mode, undo: { mode, ...plan } }); + host.mark(new Set(plan.after.keys())); +} + +/** Put back the windows that are still where the last sort placed them. */ +export function undoCanvasAutoSort(host: CanvasAutoSortHost): void { + if (!host.state.undo) return; + const restore = canvasAutoSortUndoTargets(host.state.undo, host.bounds); + host.setState({ mode: host.state.mode, undo: null }); + if (restore.size === 0) return; + host.commit(restore); + host.mark(new Set(restore.keys())); +} + +export function CanvasAutoSortMenu({ host, locale, className, onDone }: { + host: CanvasAutoSortHost; + locale: LocaleId; + className: string; + onDone(): void; +}): React.JSX.Element { + const available = canvasAutoSortIds(host).length >= 2; + const undoAvailable = host.state.undo !== null && canvasAutoSortUndoTargets(host.state.undo, host.bounds).size > 0; + const run = (action: () => void): void => { + try { + action(); + } catch (error) { + console.error("Auto-sort failed", error); + host.onError(t(locale, "canvasAutoSortFailed")); + } + onDone(); + }; + return ( +
+
+ {t(locale, "canvasAutoSort")} + {CANVAS_AUTO_SORT_MODES.map((mode) => ( + + ))} + {!available &&

{t(locale, "canvasAutoSortUnavailable")}

} +
+ {undoAvailable && ( + <> + + run(() => undoCanvasAutoSort(host))}> + {t(locale, "canvasAutoSortUndo")} + + + )} +
+ ); +} diff --git a/src/renderer/src/features/workspace/CanvasContextMenu.tsx b/src/renderer/src/features/workspace/CanvasContextMenu.tsx index a152cf7a6..348818151 100644 --- a/src/renderer/src/features/workspace/CanvasContextMenu.tsx +++ b/src/renderer/src/features/workspace/CanvasContextMenu.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef, useState } from "react"; +import { useEffect, useLayoutEffect, useRef, useState } from "react"; import type { CanvasLauncherItemId, LocaleId, @@ -17,13 +17,23 @@ import { CanvasMenuRow, CanvasMenuSub } from "../../components/CanvasMenuPrimitives"; -import { - chooseCanvasSubmenuSide, - type CanvasContextMenuKind, - type CanvasSubmenuSide -} from "./canvasContextRouting"; +import type { CanvasContextMenuKind, CanvasSubmenuSide } from "./canvasContextRouting"; +import { canvasMenuPlacementClearOfPanels, chooseCanvasSubmenuPlacement, chooseCanvasSubmenuSide } from "./canvasMenuPlacement"; +import type { CanvasAutoSortHost } from "./canvasAutoSortScope"; import { CANVAS_REGION_COLORS } from "./canvasRegions"; +type ToolsMenu = typeof import("./CanvasAutoSortMenu").CanvasAutoSortMenu; +/** The Tools submenu body: loaded when the empty-canvas menu opens (or by Tools) and kept for later menus. */ +let loadedToolsMenu: ToolsMenu | null = null; +let toolsMenuLoad: Promise | null = null; +function loadToolsMenu(): Promise { + toolsMenuLoad ??= import("./CanvasAutoSortMenu").then((module) => (loadedToolsMenu = module.CanvasAutoSortMenu), (error: unknown) => { + toolsMenuLoad = null; + throw error; + }); + return toolsMenuLoad; +} + interface CanvasContextMenuProps { kind: CanvasContextMenuKind; position: Point; @@ -52,9 +62,17 @@ interface CanvasContextMenuProps { onBringMaterialToFront(): void; onRemoveMaterial(): void; onSendRemarks: (() => void) | null; + autoSort: CanvasAutoSortHost; onClose(): void; } +/** The visible canvas corner panels (minimap, shortcut hints, Home/zoom controls), wherever Settings put them. */ +function cornerPanelRects(workspace: HTMLElement): DOMRect[] { + return [...workspace.querySelectorAll(".canvas-overlays .canvas-overlay-slot > *")] + .map((panel) => panel.getBoundingClientRect()) + .filter((panel) => panel.width > 0 && panel.height > 0); +} + export function CanvasContextMenu({ kind, position, @@ -83,21 +101,73 @@ export function CanvasContextMenu({ onBringMaterialToFront, onRemoveMaterial, onSendRemarks, + autoSort, onClose }: CanvasContextMenuProps): React.JSX.Element { const menu = useRef(null); const [launcherOpen, setLauncherOpen] = useState(false); + const [toolsOpen, setToolsOpen] = useState(false); + const [ToolsMenu, setToolsMenu] = useState(() => loadedToolsMenu); + // Tools sits low in the menu: near the bottom of the canvas its submenu opens upward. + const [toolsUp, setToolsUp] = useState(false); const [submenuSide, setSubmenuSide] = useState("right"); useEffect(() => { menu.current?.querySelector(".canvas-menu__row")?.focus({ preventScroll: true }); }, [kind]); - const toggleLauncher = (): void => { - if (launcherOpen) { - setLauncherOpen(false); + // Tools is opened from this menu: its body loads while the menu is open, so the submenu shows at once. + useEffect(() => { + if (kind !== "empty") return; + // Loaded since this menu's first render (by an earlier menu): copy it in, nothing else will. + if (loadedToolsMenu) { + setToolsMenu(() => loadedToolsMenu); return; } + let live = true; + loadToolsMenu().then((loaded) => { if (live) setToolsMenu(() => loaded); }, () => undefined); + return () => { live = false; }; + }, [kind]); + // Before the first paint: keep the menu off the corner panels (up, or else sideways), and point the submenu chevrons + // where they open. + const [place, setPlace] = useState({ left: position.x, top: position.y }); + useLayoutEffect(() => { + const root = menu.current; + const workspace = root?.closest(".workspace"); + if (!root || !workspace) { + setPlace({ left: position.x, top: position.y }); + return; + } + const area = workspace.getBoundingClientRect(); + const rect = root.getBoundingClientRect(); + const placed = { left: area.left + position.x, right: area.left + position.x + rect.width, top: area.top + position.y, + bottom: area.top + position.y + rect.height }; + const panels = cornerPanelRects(workspace); + const fontSize = Number.parseFloat(window.getComputedStyle(root).fontSize) || 13; + const clear = canvasMenuPlacementClearOfPanels(placed, panels, + { minTop: area.top + .9 * fontSize, minLeft: area.left + .9 * fontSize, maxRight: area.right - .9 * fontSize }, .6 * fontSize); + setPlace({ left: clear.left - area.left, top: clear.top - area.top }); + }, [kind, position.x, position.y]); + // After the menu has its place: the submenus open toward the side with room. + useLayoutEffect(() => { + chooseSubmenuSide(); + }, [kind, place.left, place.top]); + // An open submenu, once measured, goes where it covers no corner panel: the other direction or side when needed. + useLayoutEffect(() => { + const submenu = menu.current?.querySelector(".canvas-menu__submenu"); + const anchor = submenu?.closest(".canvas-menu__submenu-anchor"); + const workspace = menu.current?.closest(".workspace"); + if (!submenu || !anchor || !workspace) return; + const fontSize = Number.parseFloat(window.getComputedStyle(menu.current!).fontSize) || 13; + const { width, height } = submenu.getBoundingClientRect(); + const placement = chooseCanvasSubmenuPlacement(anchor.getBoundingClientRect(), { width, height }, + workspace.getBoundingClientRect(), cornerPanelRects(workspace), + { gap: .55 * fontSize, offset: .45 * fontSize, clearance: .6 * fontSize }, toolsOpen); + setSubmenuSide(placement.side); + if (toolsOpen) setToolsUp(placement.up); + }, [launcherOpen, toolsOpen, ToolsMenu, place.left, place.top]); + + const chooseSubmenuSide = (): void => { const root = menu.current; const workspace = root?.closest(".workspace"); if (root && workspace) { @@ -109,9 +179,38 @@ export function CanvasContextMenu({ .55 * fontSize )); } + }; + + const toggleLauncher = (): void => { + if (launcherOpen) { + setLauncherOpen(false); + return; + } + setToolsOpen(false); setLauncherOpen(true); }; + // Where an open submenu goes (side, up or down) is chosen once it is measured, in the layout effect above. + const toggleTools = (): void => { + if (toolsOpen) { + setToolsOpen(false); + return; + } + setLauncherOpen(false); + setToolsOpen(true); + if (loadedToolsMenu) { + setToolsMenu(() => loadedToolsMenu); + } else { + loadToolsMenu().then((loaded) => { + setToolsMenu(() => loaded); + }).catch((error: unknown) => { + console.error("Auto-sort failed to load", error); + autoSort.onError(t(locale, "canvasAutoSortFailed")); + setToolsOpen(false); + }); + } + }; + return (
event.preventDefault()} onKeyDown={(event) => { if (event.key === "Escape") { @@ -164,7 +263,7 @@ export function CanvasContextMenu({
} + right={} role="menuitem" aria-haspopup="menu" aria-expanded={launcherOpen} @@ -206,6 +305,24 @@ export function CanvasContextMenu({ {t(locale, "files")} +
+ } + role="menuitem" + aria-haspopup="menu" + aria-expanded={toolsOpen} + onClick={toggleTools} + >{t(locale, "canvasMenuTools")} + {toolsOpen && ToolsMenu && ( + + )} +
= new Set(); +/** How long the windows an auto-sort moved keep their outline, unless the next press clears it. */ +const AUTO_SORT_MARK_MS = 4_000; +const NO_AUTO_SORT: CanvasAutoSortState = { mode: null, undo: null }; const NO_SNAP_TARGETS = (): readonly SessionBounds[] => []; /** The fullscreen layer is outside the scene: its card always draws at scale 1. */ const FULLSCREEN_CAMERA = fixedCameraStore({ x: 0, y: 0, zoom: 1 }); @@ -189,6 +193,7 @@ type RegionMovePreview = { interface WorkspaceCanvasProps { settings: AppSettings; + backgroundsSuspended?: boolean; /** * False for the first frame after startup: restored terminals (xterm), plugin canvas iframes and the browser card * mount right after that frame was painted, so HOME and the canvas show without waiting for them. Their layout @@ -284,7 +289,7 @@ const FileBrowserCard = lazy(() => import("../files/FileBrowserCard").then((modu export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element { const { - settings, mediaData, sessions, limits, limitsLoadState, plugins, browser, + settings, backgroundsSuspended = false, mediaData, sessions, limits, limitsLoadState, plugins, browser, browserViewVisible, homeEditing, camera, onCameraChange, onGoHome, onOpenSettings, onOpenAgent, onOpenTerminal, onOpenBrowser, onOpenTerminalUrl, onFocusSession, activeSessionId, browserSelected, renamingSessionId, fullscreenSessionId, onToggleFullscreen, onSelectSession, @@ -307,6 +312,10 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element const [contextMenu, setContextMenu] = useState(null); const [regionEditor, setRegionEditor] = useState(null); const [commandPaletteOpen, setCommandPaletteOpen] = useState(false); + // Out of the startup bundle, preloaded once the canvas is idle, rendered without a Suspense boundary (see preloadedSurface). + const CanvasContextMenu = usePreloadedSurface(canvasContextMenuSurface, contextMenu !== null); + const CanvasCommandPalette = usePreloadedSurface(canvasCommandPaletteSurface, commandPaletteOpen); + useEffect(() => preloadWhenIdle([canvasContextMenuSurface, canvasCommandPaletteSurface]), []); const [radialLauncher, setRadialLauncher] = useState<{ anchor: Point; pointerAnchor: Point; @@ -321,6 +330,12 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element const [regionMovePreview, setRegionMovePreview] = useState(null); const [marqueeSelection, setMarqueeSelection] = useState>(EMPTY_MARQUEE_SELECTION); + // Auto-sort state lives in renderer memory only: the last criterion for this app session and + // the single undo step. Nothing here is persisted. + const [autoSortState, setAutoSortState] = useState(NO_AUTO_SORT); + // The windows the last sort or undo moved: they glide and keep an outline for a moment. Outline + // only: unlike the marquee selection it never makes them a drag group or narrows the next sort. + const [autoSortMarked, setAutoSortMarked] = useState>(EMPTY_MARQUEE_SELECTION); const [masterPixelSkinSessionIds, setMasterPixelSkinSessionIds] = useState>(() => new Set()); const overlays = useRef(null); const lastPointerClient = useRef(null); @@ -531,6 +546,37 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element const groupDragBasis = useRef(null); + /** Persist new bounds for several layers, each by its own kind; materials go in one batch. */ + const commitLayerBounds = useCallback((next: ReadonlyMap): void => { + const materialBatch: { id: string; bounds: SessionBounds }[] = []; + for (const [layerId, moved] of next) { + const ref = parseCanvasLayerId(layerId); + if (!ref) continue; + // Commit each window by its own identity, including individual Browser cards. + if (ref.kind === "terminal" && ref.targetId !== null) onSessionBoundsChange(ref.targetId, moved); + else if (ref.kind === "plugin" && ref.targetId !== null) onPluginCanvasBoundsChange(ref.targetId, moved); + else if (ref.kind === "note" && ref.targetId !== null) onStickyNoteBoundsChange(ref.targetId, moved); + else if (ref.kind === "files" && ref.targetId !== null) onFileCardBoundsChange(ref.targetId, moved); + else if (ref.kind === "material" && ref.targetId !== null) materialBatch.push({ id: ref.targetId, bounds: moved }); + else if (ref.kind === "browser" && settings.browserCanvas) onBrowserBoundsChange({ ...settings.browserCanvas, ...moved }); + } + if (materialBatch.length > 0) onMaterialBoundsChangeBatch(materialBatch); + }, [onBrowserBoundsChange, onFileCardBoundsChange, onMaterialBoundsChangeBatch, onPluginCanvasBoundsChange, + onSessionBoundsChange, onStickyNoteBoundsChange, settings.browserCanvas]); + + // The outline of a sort clears after a moment, or at the next press anywhere on the canvas. + useEffect(() => { + if (autoSortMarked.size === 0) return; + const clear = (): void => setAutoSortMarked(EMPTY_MARQUEE_SELECTION); + const timer = window.setTimeout(clear, AUTO_SORT_MARK_MS); + const element = viewport.current; + element?.addEventListener("pointerdown", clear, true); + return () => { + window.clearTimeout(timer); + element?.removeEventListener("pointerdown", clear, true); + }; + }, [autoSortMarked]); + const beginGroupDrag = useCallback((layerId: string): void => { const anchor = boundsByLayer.get(layerId); if (!anchor) return; @@ -567,23 +613,11 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element x: anchorPosition.x - anchor.position.x, y: anchorPosition.y - anchor.position.y }; - const materialBatch: { id: string; bounds: SessionBounds }[] = []; - for (const [memberLayerId, memberBounds] of members) { - const moved = translateBounds(memberBounds, rigid); - const ref = parseCanvasLayerId(memberLayerId); - if (!ref) continue; - // Commit each window by its own identity, including individual Browser cards. - if (ref.kind === "terminal" && ref.targetId !== null) onSessionBoundsChange(ref.targetId, moved); - else if (ref.kind === "plugin" && ref.targetId !== null) onPluginCanvasBoundsChange(ref.targetId, moved); - else if (ref.kind === "note" && ref.targetId !== null) onStickyNoteBoundsChange(ref.targetId, moved); - else if (ref.kind === "files" && ref.targetId !== null) onFileCardBoundsChange(ref.targetId, moved); - else if (ref.kind === "material" && ref.targetId !== null) materialBatch.push({ id: ref.targetId, bounds: moved }); - else if (ref.kind === "browser" && settings.browserCanvas) onBrowserBoundsChange({ ...settings.browserCanvas, ...moved }); - } - if (materialBatch.length > 0) onMaterialBoundsChangeBatch(materialBatch); - }, [boundsByLayer, homeBounds, onBrowserBoundsChange, onFileCardBoundsChange, onMaterialBoundsChangeBatch, onPluginCanvasBoundsChange, - - onSessionBoundsChange, onStickyNoteBoundsChange, renderedCanvasRegions, settings.browserCanvas, settings.snapToGrid]); + commitLayerBounds(new Map([...members].map(([memberLayerId, memberBounds]) => [ + memberLayerId, + translateBounds(memberBounds, rigid) + ]))); + }, [boundsByLayer, commitLayerBounds, homeBounds, renderedCanvasRegions, settings.snapToGrid]); const focusController = useCanvasWidgetFocus({ viewport, @@ -786,7 +820,7 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element return clampCanvasMenuPosition( viewportPoint(clientX, clientY), { width: bounds.width, height: bounds.height }, - { width: 300 * settings.uiScale, height: 380 * settings.uiScale } + { width: 300 * settings.uiScale, height: 420 * settings.uiScale } ); }, [settings.uiScale, viewportPoint]); const worldPoint = useCallback((clientX: number, clientY: number): Point => { @@ -1044,12 +1078,33 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element isPixelSkinPackId(settings.canvasBackground) ? settings.canvasBackground : null )?.background; + const getTerminalBackground = (sessionId: string) => { + const o = settings.terminalBackgroundOverrides[sessionId]; + return { + background: o?.terminalBackground ?? settings.terminalBackground, + backgroundBrightness: o?.terminalBackgroundBrightness ?? settings.terminalBackgroundBrightness, + backgroundContrast: o?.terminalBackgroundContrast ?? settings.terminalBackgroundContrast, + backgroundSaturation: o?.terminalBackgroundSaturation ?? settings.terminalBackgroundSaturation, + backgroundPaused: (o?.terminalBackgroundPaused ?? settings.terminalBackgroundPaused) + || (settings.terminalBackgroundPauseInactive && activeSessionId !== sessionId), + backgroundSpeed: o?.terminalBackgroundSpeed ?? settings.terminalBackgroundSpeed, + backgroundScale: o?.terminalBackgroundScale ?? settings.terminalBackgroundScale, + backgroundOffsetX: o?.terminalBackgroundOffsetX ?? settings.terminalBackgroundOffsetX, + backgroundOffsetY: o?.terminalBackgroundOffsetY ?? settings.terminalBackgroundOffsetY, + backgroundReadability: o?.terminalBackgroundReadability ?? settings.terminalBackgroundReadability, + backgroundReadabilityStrength: o?.terminalBackgroundReadabilityStrength ?? settings.terminalBackgroundReadabilityStrength, + backgroundPauseOnOutput: o?.terminalBackgroundPauseOnOutput ?? settings.terminalBackgroundPauseOnOutput, + backgroundFocusDimming: o?.terminalBackgroundFocusDimming ?? settings.terminalBackgroundFocusDimming, + backgroundSynchronized: settings.terminalBackgroundSynchronized, + }; + }; + return (
0 ? "workspace--auto-sorting" : ""}`} onPointerDownCapture={(event) => { if (openRadialLauncher(event)) return; const element = event.target as HTMLElement; @@ -1201,6 +1256,8 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element locale={settings.locale} palette={settings.palette} borderSkin={settings.terminalBorderSkin} + {...getTerminalBackground(session.id)} + backgroundsSuspended={backgroundsSuspended} skinDetail={settings.terminalSkinDetail} camera={camera} stackIndex={canvasLayerZIndex(layerOrder, terminalLayerId(session.id))} @@ -1214,7 +1271,7 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element focusRevision={widgetFocus.id === terminalCanvasWidgetId(session.id) ? widgetFocus.revision : 0} selected={activeSessionId === session.id} forceMasterDetail={masterPixelSkinSessionIds.has(session.id)} - groupSelected={marqueeSelection.has(terminalLayerId(session.id))} + groupSelected={marqueeSelection.has(terminalLayerId(session.id)) || autoSortMarked.has(terminalLayerId(session.id))} renaming={renamingSessionId === session.id} fullscreen={fullscreenSessionId === session.id} hidden={homeEditing} @@ -1262,7 +1319,7 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element else focusController.cancelHover(pluginCanvasWidgetId(instance.id)); }} onCanvasWheel={wheelNavigation.applyCanvasWheel} - groupSelected={marqueeSelection.has(pluginLayerId(instance.id))} + groupSelected={marqueeSelection.has(pluginLayerId(instance.id)) || autoSortMarked.has(pluginLayerId(instance.id))} hidden={homeEditing} /> ); @@ -1301,7 +1358,7 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element onWidgetHoverChange={focusController.hoverBrowser} onClose={onCloseBrowser} onError={onPluginError} - groupSelected={marqueeSelection.has(browserLayerId)} + groupSelected={marqueeSelection.has(browserLayerId) || autoSortMarked.has(browserLayerId)} /> )} {renderedStickyNotes.map((note) => ( @@ -1394,6 +1451,8 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element locale={settings.locale} palette={settings.palette} borderSkin={settings.terminalBorderSkin} + {...getTerminalBackground(session.id)} + backgroundsSuspended={backgroundsSuspended} skinDetail={settings.terminalSkinDetail} camera={FULLSCREEN_CAMERA} stackIndex={9999} @@ -1447,14 +1506,14 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element {homeEditing && (
{t(settings.locale, "homeEditor")} - +
)} - {contextMenu && ( + {contextMenu && CanvasContextMenu && ( setContextMenu(null)} /> )} @@ -1580,7 +1654,7 @@ export function WorkspaceCanvas(props: WorkspaceCanvasProps): React.JSX.Element /> )} - {commandPaletteOpen && ( + {commandPaletteOpen && CanvasCommandPalette && ( , + sessions: readonly CanvasAutoSortSession[], + layerIdOf: (sessionId: string) => string, + mode: CanvasAutoSortMode, + options: CanvasAutoSortOptions +): { before: Map; after: Map } | null { + const sessionByLayer = new Map(sessions.map((session) => [layerIdOf(session.id), session])); + const items: CanvasAutoSortItem[] = []; + for (const id of new Set(ids)) { + const current = bounds.get(id); + if (!current) continue; + const session = sessionByLayer.get(id); + items.push({ + id, + bounds: { position: { ...current.position }, size: { ...current.size } }, + ...(session ? { + ...(session.parentSessionId ? { parentId: layerIdOf(session.parentSessionId) } : {}), + status: session.status, + project: session.cwd, + startedAt: session.startedAt + } : {}) + }); + } + if (items.length < 2) return null; + const sorted = new Set(items.map((item) => item.id)); + const obstacles = [...options.avoid ?? [], ...[...bounds].filter(([id]) => !sorted.has(id)).map(([, value]) => value)]; + return { + before: new Map(items.map((item) => [item.id, item.bounds])), + after: arrangeCanvasAutoSort(items, mode, { avoid: obstacles, snapToGrid: options.snapToGrid }) + }; +} + +/** The contract's snapping gap between neighbouring windows; criterion groups get twice that. */ +const GAP = 20; +const GROUP_GAP = 2 * GAP; +const GRID = 10; + +/** The next start after an edge: the gap past it, rounded up onto the grid when snapping. */ +type Step = (edge: number, gap?: number) => number; + +/** + * The new bounds of every item. The block starts at the top-left corner of the windows' current + * bounding box (on the grid when snapping), so the windows stay where the person was looking. If + * the block would cover anything in `options.avoid` it shifts right or down, whichever is the + * shorter move, until it is clear by at least the gap. With snapping every window lands on the + * grid. Sizes never change. + */ +export function arrangeCanvasAutoSort( + items: readonly CanvasAutoSortItem[], + mode: CanvasAutoSortMode, + options: CanvasAutoSortOptions = {} +): Map { + if (items.length === 0) return new Map(); + const snap = options.snapToGrid === true; + const step: Step = (edge, gap = GAP) => (snap ? Math.ceil((edge + gap) / GRID) * GRID : edge + gap); + const local = arrangeAt(items, mode, step); + const extent = boundingBox([...local.values()]).size; + const current = boundingBox(items.map((item) => item.bounds)).position; + const start = snap + ? { x: Math.round(current.x / GRID) * GRID, y: Math.round(current.y / GRID) * GRID } + : { x: Math.round(current.x), y: Math.round(current.y) }; + const origin = clearSpot(start, extent, options.avoid ?? [], step); + const result = new Map(); + for (const [id, bounds] of local) result.set(id, at(bounds.position.x + origin.x, bounds.position.y + origin.y, bounds)); + return result; +} + +/** + * The nearest place right of or below `start` where a block of `size`, grown by the gap, overlaps + * none of `obstacles`. Each sweep only ever moves past an obstacle's far edge, so it ends after at + * most one move per obstacle. + */ +function clearSpot(start: Point, size: SessionBounds["size"], obstacles: readonly SessionBounds[], step: Step): Point { + const sweep = (axis: "x" | "y"): Point => { + const spot = { ...start }; + for (let moves = 0; moves <= obstacles.length; moves += 1) { + const margin = { position: { x: spot.x - GAP, y: spot.y - GAP }, size: { width: size.width + 2 * GAP, height: size.height + 2 * GAP } }; + const hit = obstacles.find((obstacle) => overlaps(margin, obstacle)); + if (!hit) break; + spot[axis] = step(axis === "x" ? hit.position.x + hit.size.width : hit.position.y + hit.size.height); + } + return spot; + }; + const right = sweep("x"); + const down = sweep("y"); + return down.y - start.y < right.x - start.x ? down : right; +} + +function arrangeAt(items: readonly CanvasAutoSortItem[], mode: CanvasAutoSortMode, step: Step): Map { + const ordered = [...items].sort(readingOrder); + if (mode === "task") return arrangeTaskTree(ordered, step); + if (mode === "grid") return arrangeGrid(ordered, 0, step); + const groups = new Map(); + for (const item of ordered) { + const key = mode === "status" ? statusGroup(item.status) : projectGroup(item.project); + const group = groups.get(key) ?? []; + group.push(item); + groups.set(key, group); + } + const result = new Map(); + let x = 0; + for (const key of [...groups.keys()].sort(compareText)) { + let right = x; + for (const [id, bounds] of arrangeGrid(groups.get(key)!, x, step)) { + result.set(id, bounds); + right = Math.max(right, bounds.position.x + bounds.size.width); + } + x = step(right, GROUP_GAP); + } + return result; +} + +/** Orchestrators in a column, each subagent to the right of the session that spawned it. */ +function arrangeTaskTree(ordered: readonly CanvasAutoSortItem[], step: Step): Map { + const byId = new Map(ordered.map((item) => [item.id, item])); + const children = new Map(); + const roots: CanvasAutoSortItem[] = []; + for (const item of ordered) { + const parent = item.parentId ? byId.get(item.parentId) : undefined; + if (!parent || parent.id === item.id) { + roots.push(item); + continue; + } + const row = children.get(parent.id) ?? []; + row.push(item); + children.set(parent.id, row); + } + const depth = new Map(); + const markDepth = (item: CanvasAutoSortItem, level: number): void => { + if (depth.has(item.id)) return; + depth.set(item.id, level); + for (const child of children.get(item.id) ?? []) markDepth(child, level + 1); + }; + for (const root of roots) markDepth(root, 0); + // A malformed parent cycle has no natural root: break it at its first window in reading order. + for (const item of ordered) { + if (depth.has(item.id)) continue; + roots.push(item); + if (item.parentId) { + children.set(item.parentId, (children.get(item.parentId) ?? []).filter((child) => child.id !== item.id)); + } + markDepth(item, 0); + } + + const levelWidths: number[] = []; + for (const item of ordered) { + const level = depth.get(item.id) ?? 0; + levelWidths[level] = Math.max(levelWidths[level] ?? 0, item.bounds.size.width); + } + const levelX: number[] = [0]; + for (let level = 1; level < levelWidths.length; level += 1) { + levelX[level] = step(levelX[level - 1]! + (levelWidths[level - 1] ?? 0)); + } + + // A subtree's height measured the way `place` stacks it, so grid rounding never makes rows overlap. + const heights = new Map(); + const subtreeHeight = (item: CanvasAutoSortItem): number => { + const cached = heights.get(item.id); + if (cached !== undefined) return cached; + let top = 0; + let bottom = 0; + for (const child of children.get(item.id) ?? []) { + bottom = top + subtreeHeight(child); + top = step(bottom); + } + const height = Math.max(item.bounds.size.height, bottom); + heights.set(item.id, height); + return height; + }; + const result = new Map(); + const place = (item: CanvasAutoSortItem, top: number): void => { + result.set(item.id, at(levelX[depth.get(item.id) ?? 0] ?? 0, top, item.bounds)); + let childTop = top; + for (const child of children.get(item.id) ?? []) { + place(child, childTop); + childTop = step(childTop + subtreeHeight(child)); + } + }; + let top = 0; + for (const root of roots) { + place(root, top); + top = step(top + subtreeHeight(root)); + } + return result; +} + +/** A near-square grid; each column is as wide as its widest window and each row as tall as its tallest. */ +function arrangeGrid(ordered: readonly CanvasAutoSortItem[], left: number, step: Step): Map { + const columns = Math.max(1, Math.ceil(Math.sqrt(ordered.length))); + const rows = Math.ceil(ordered.length / columns); + const columnWidths = Array.from({ length: columns }, () => 0); + const rowHeights = Array.from({ length: rows }, () => 0); + ordered.forEach((item, index) => { + const row = Math.floor(index / columns); + const column = index % columns; + columnWidths[column] = Math.max(columnWidths[column]!, item.bounds.size.width); + rowHeights[row] = Math.max(rowHeights[row]!, item.bounds.size.height); + }); + const columnX = [left]; + for (let index = 1; index < columns; index += 1) columnX[index] = step(columnX[index - 1]! + columnWidths[index - 1]!); + const rowY = [0]; + for (let index = 1; index < rows; index += 1) rowY[index] = step(rowY[index - 1]! + rowHeights[index - 1]!); + return new Map(ordered.map((item, index) => [ + item.id, + at(columnX[index % columns]!, rowY[Math.floor(index / columns)]!, item.bounds) + ])); +} + +/** Columns left to right: what needs the person first, then what is idle, then what ended. */ +function statusGroup(status: SessionStatus | undefined): string { + switch (status) { + case "needs_approval": return "0"; + case "working": return "1"; + case "idle": return "2"; + case "done": return "3"; + case "failed": return "4"; + case "unavailable": return "5"; + default: return "6"; + } +} + +/** One column per working folder in path order; windows without a folder come last. */ +function projectGroup(project: string | undefined): string { + const normalized = project?.trim().replace(/(?<=.)[\\/]+$/u, "") ?? ""; + return normalized ? `0${normalized}` : "1"; +} + +function readingOrder(left: CanvasAutoSortItem, right: CanvasAutoSortItem): number { + return left.bounds.position.y - right.bounds.position.y + || left.bounds.position.x - right.bounds.position.x + || (left.startedAt ?? 0) - (right.startedAt ?? 0) + || compareText(left.id, right.id); +} + +function compareText(left: string, right: string): number { + return left < right ? -1 : left > right ? 1 : 0; +} + +function at(x: number, y: number, bounds: SessionBounds): SessionBounds { + return { position: { x, y }, size: { ...bounds.size } }; +} + +function boundingBox(bounds: readonly SessionBounds[]): SessionBounds { + const left = Math.min(...bounds.map((item) => item.position.x)); + const top = Math.min(...bounds.map((item) => item.position.y)); + const right = Math.max(...bounds.map((item) => item.position.x + item.size.width)); + const bottom = Math.max(...bounds.map((item) => item.position.y + item.size.height)); + return { position: { x: left, y: top }, size: { width: right - left, height: bottom - top } }; +} + +function overlaps(left: SessionBounds, right: SessionBounds): boolean { + return left.position.x < right.position.x + right.size.width + && left.position.x + left.size.width > right.position.x + && left.position.y < right.position.y + right.size.height + && left.position.y + left.size.height > right.position.y; +} diff --git a/src/renderer/src/features/workspace/canvasAutoSortScope.ts b/src/renderer/src/features/workspace/canvasAutoSortScope.ts new file mode 100644 index 000000000..6a63ba6a4 --- /dev/null +++ b/src/renderer/src/features/workspace/canvasAutoSortScope.ts @@ -0,0 +1,119 @@ +import type { SessionBounds } from "../../../../shared/contracts"; +import type { CanvasAutoSortSession } from "./canvasAutoSort"; + +/** + * What the empty-canvas Auto-sort may move and what its undo puts back. Kept apart from the + * arrangement itself (canvasAutoSort.ts) so the menu can tell whether a sort is possible without + * loading the layout code. + */ +export type CanvasAutoSortMode = "task" | "status" | "project" | "grid"; + +export const CANVAS_AUTO_SORT_MODES: readonly CanvasAutoSortMode[] = ["task", "status", "project", "grid"]; + +export type CanvasAutoSortKind = "terminal" | "plugin" | "browser" | "note" | "files" | "material"; + +export interface CanvasAutoSortCandidate { + id: string; + kind: CanvasAutoSortKind; + bounds: SessionBounds; + /** A companion-style plugin window (mascot): sorted only when explicitly selected. */ + decorative?: boolean; +} + +export interface CanvasAutoSortUndo { + mode: CanvasAutoSortMode; + before: ReadonlyMap; + after: ReadonlyMap; +} + +/** The last criterion (checked row) and the single undo step; renderer memory only. */ +export interface CanvasAutoSortState { + mode: CanvasAutoSortMode | null; + undo: CanvasAutoSortUndo | null; +} + +/** What the workspace hands the Tools submenu: the layout of this moment and how to change it. */ +export interface CanvasAutoSortHost { + /** Bounds of every rendered layer, by layer id. */ + bounds: ReadonlyMap; + selection: ReadonlySet; + home: SessionBounds; + regions: readonly SessionBounds[]; + sessions: readonly CanvasAutoSortSession[]; + /** Plugin windows and installed plugins, to tell companion mascots apart. */ + pluginCanvas: readonly { id: string; pluginId: string }[]; + plugins: readonly { manifest: { id: string }; sourceUrl: string }[]; + snapToGrid: boolean; + state: CanvasAutoSortState; + setState(state: CanvasAutoSortState): void; + /** Persist new bounds through the same per-kind callbacks a group drag uses. */ + commit(next: ReadonlyMap): void; + /** Outline the windows that just moved (presentation only). */ + mark(ids: ReadonlySet): void; + onError(message: string): void; +} + +/** Notes, materials and file cards are annotations whose place carries meaning: never moved. */ +const MOVABLE_KINDS: ReadonlySet = new Set(["terminal", "plugin", "browser"]); + +/** + * Which windows an auto-sort moves. A marquee selection holding two or more movable windows + * limits the sort to them. Otherwise (no selection, or one holding only notes, materials, Files + * cards or a single window) every terminal, plugin and Browser window that is not completely + * inside a canvas region moves: regions hold the person's own grouping, and moving a window out + * of one silently changes its membership. Mascots stay out of that default scope unless the + * person selected them. + */ +export function canvasAutoSortScope( + candidates: readonly CanvasAutoSortCandidate[], + selection: ReadonlySet, + regions: readonly SessionBounds[] +): string[] { + const movable = candidates.filter((candidate) => MOVABLE_KINDS.has(candidate.kind)); + const selected = movable.filter((candidate) => selection.has(candidate.id)); + if (selected.length >= 2) return selected.map((candidate) => candidate.id); + return movable + .filter((candidate) => !candidate.decorative || selection.has(candidate.id)) + .filter((candidate) => !regions.some((region) => contains(region, candidate.bounds))) + .map((candidate) => candidate.id); +} + +/** + * What an undo puts back: only windows that still sit exactly where the sort placed them. + * A window the person moved or resized afterwards keeps its new place; a closed one is skipped. + */ +export function canvasAutoSortUndoTargets( + undo: CanvasAutoSortUndo, + current: ReadonlyMap +): Map { + const result = new Map(); + for (const [id, before] of undo.before) { + const placed = undo.after.get(id); + const now = current.get(id); + if (placed && now && sameBounds(placed, now) && !sameBounds(before, now)) result.set(id, before); + } + return result; +} + +/** Whether a sort step moves or resizes anything; sorting an already sorted layout does not. */ +export function canvasAutoSortMoves(step: Pick): boolean { + for (const [id, placed] of step.after) { + const before = step.before.get(id); + if (!before || !sameBounds(before, placed)) return true; + } + return false; +} + +function contains(outer: SessionBounds, inner: SessionBounds): boolean { + return inner.position.x >= outer.position.x + && inner.position.y >= outer.position.y + && inner.position.x + inner.size.width <= outer.position.x + outer.size.width + && inner.position.y + inner.size.height <= outer.position.y + outer.size.height; +} + +function sameBounds(left: SessionBounds, right: SessionBounds): boolean { + return left.position.x === right.position.x + && left.position.y === right.position.y + && left.size.width === right.size.width + && left.size.height === right.size.height; +} diff --git a/src/renderer/src/features/workspace/canvasContextRouting.ts b/src/renderer/src/features/workspace/canvasContextRouting.ts index 4c4fcadd5..e0aaf6388 100644 --- a/src/renderer/src/features/workspace/canvasContextRouting.ts +++ b/src/renderer/src/features/workspace/canvasContextRouting.ts @@ -4,11 +4,6 @@ export type CanvasContextHit = "empty" | "region" | "note" | "material" | "nativ export type CanvasContextMenuKind = "empty" | "region" | "note" | "material"; export type CanvasSubmenuSide = "left" | "right"; -interface HorizontalBounds { - left: number; - right: number; -} - export function routeCanvasContextMenu( hit: CanvasContextHit, homeEditing: boolean @@ -28,17 +23,3 @@ export function clampCanvasMenuPosition( y: Math.max(inset, Math.min(pointer.y, Math.max(inset, viewport.height - menu.height - inset))) }; } - -export function chooseCanvasSubmenuSide( - anchor: HorizontalBounds, - viewport: HorizontalBounds, - submenuWidth: number, - gap: number -): CanvasSubmenuSide { - const required = submenuWidth + gap; - const rightSpace = viewport.right - anchor.right; - const leftSpace = anchor.left - viewport.left; - if (rightSpace >= required) return "right"; - if (leftSpace >= required) return "left"; - return rightSpace >= leftSpace ? "right" : "left"; -} diff --git a/src/renderer/src/features/workspace/canvasMenuPlacement.ts b/src/renderer/src/features/workspace/canvasMenuPlacement.ts new file mode 100644 index 000000000..6c17a0de9 --- /dev/null +++ b/src/renderer/src/features/workspace/canvasMenuPlacement.ts @@ -0,0 +1,113 @@ +import type { Size } from "../../../../shared/contracts"; +import type { CanvasSubmenuSide } from "./canvasContextRouting"; + +// Placement of the canvas context menu and its submenus around the corner panels. Only the lazily loaded menu uses it, +// so it stays out of the startup bundle. + +interface HorizontalBounds { + left: number; + right: number; +} + +export function chooseCanvasSubmenuSide( + anchor: HorizontalBounds, + viewport: HorizontalBounds, + submenuWidth: number, + gap: number +): CanvasSubmenuSide { + const required = submenuWidth + gap; + const rightSpace = viewport.right - anchor.right; + const leftSpace = anchor.left - viewport.left; + if (rightSpace >= required) return "right"; + if (leftSpace >= required) return "left"; + return rightSpace >= leftSpace ? "right" : "left"; +} + +interface Box { + left: number; + top: number; + right: number; + bottom: number; +} + +/** + * The top for a context menu so it does not cover a corner panel (minimap, shortcut hints, Home/zoom controls): a menu + * opened near a panel moves up until it clears it, by `gap`, while it stays at or below `minTop`. When that is not + * possible the menu keeps its place (it is drawn above the panels anyway). + */ +export function canvasMenuTopClearOfPanels(menu: Box, panels: readonly Box[], minTop: number, gap: number): number { + const height = menu.bottom - menu.top; + const overlaps = (top: number, panel: Box): boolean => menu.left < panel.right && menu.right > panel.left + && top < panel.bottom + gap && top + height > panel.top - gap; + let top = menu.top; + for (let step = 0; step < panels.length; step++) { + const hit = panels.find((panel) => overlaps(top, panel)); + if (!hit) return top; + top = hit.top - gap - height; + if (top < minTop) return menu.top; + } + return panels.some((panel) => overlaps(top, panel)) ? menu.top : top; +} + +/** + * Where a context menu goes so it covers no corner panel, at any UI scale: first up (canvasMenuTopClearOfPanels); + * when that cannot clear every panel, sideways at its own height, to the side of the panel it hits with room for it + * within `minLeft`..`maxRight`. When neither works it keeps its place (drawn above the panels anyway). + */ +export function canvasMenuPlacementClearOfPanels( + menu: Box, panels: readonly Box[], bounds: { minTop: number; minLeft: number; maxRight: number }, gap: number +): { left: number; top: number } { + const width = menu.right - menu.left; + const height = menu.bottom - menu.top; + const hits = (left: number, top: number): Box | undefined => panels.find((panel) => left < panel.right + && left + width > panel.left && top < panel.bottom + gap && top + height > panel.top - gap); + const top = canvasMenuTopClearOfPanels(menu, panels, bounds.minTop, gap); + if (!hits(menu.left, top)) return { left: menu.left, top }; + // Beside the panel it hits, nearest to where it was opened first, never back to a place already tried. + const tried = new Set([menu.left]); + let left = menu.left; + for (let step = 0; step <= panels.length; step++) { + const hit = hits(left, menu.top); + if (!hit) return { left, top: menu.top }; + const next = [hit.left - gap - width, hit.right + gap] + .filter((candidate) => !tried.has(candidate) && candidate >= bounds.minLeft && candidate + width <= bounds.maxRight) + .sort((a, b) => Math.abs(a - menu.left) - Math.abs(b - menu.left))[0]; + if (next === undefined) break; + tried.add(next); + left = next; + } + return { left: menu.left, top: menu.top }; +} + +/** + * Where a submenu opens beside its row (`anchor`): on the side with room (chooseCanvasSubmenuSide), downward unless it + * runs past the canvas bottom (only when `allowUp`). When that place covers a corner panel (minimap, shortcut hints, + * Home/zoom controls; their corners are set in Settings), the first of the other sides and directions that stays + * inside the canvas and clear of every panel by `metrics.clearance`. When none is clear it keeps the preferred place + * (drawn above the panels anyway). `metrics.gap` and `metrics.offset` are the submenu's CSS offsets from the row. + */ +export function chooseCanvasSubmenuPlacement( + anchor: Box, + size: Size, + area: Box, + panels: readonly Box[], + metrics: { gap: number; offset: number; clearance: number }, + allowUp: boolean +): { side: CanvasSubmenuSide; up: boolean } { + const { width, height } = size; + const { gap, offset, clearance } = metrics; + const box = (side: CanvasSubmenuSide, up: boolean): Box => { + const left = side === "right" ? anchor.right + gap : anchor.left - gap - width; + const top = up ? anchor.bottom + offset - height : anchor.top - offset; + return { left, top, right: left + width, bottom: top + height }; + }; + const fits = (b: Box): boolean => b.left >= area.left && b.right <= area.right && b.top >= area.top + && b.bottom <= area.bottom && !panels.some((panel) => b.left < panel.right + clearance + && b.right > panel.left - clearance && b.top < panel.bottom + clearance && b.bottom > panel.top - clearance); + const side = chooseCanvasSubmenuSide(anchor, area, width, gap); + const up = allowUp && box(side, false).bottom > area.bottom; + const other: CanvasSubmenuSide = side === "right" ? "left" : "right"; + const candidates = [{ side, up }, { side, up: !up }, { side: other, up }, { side: other, up: !up }] + .filter((candidate) => allowUp || !candidate.up); + return candidates.find((candidate) => fits(box(candidate.side, candidate.up))) ?? { side, up }; +} diff --git a/src/renderer/src/features/workspace/canvasOverlaySurfaces.ts b/src/renderer/src/features/workspace/canvasOverlaySurfaces.ts new file mode 100644 index 000000000..5dbfa316e --- /dev/null +++ b/src/renderer/src/features/workspace/canvasOverlaySurfaces.ts @@ -0,0 +1,6 @@ +import { preloadedSurface } from "../../lib/preloadedSurface"; + +// The command palette and the canvas context menu exist only while they are open, so their code is not part of the +// startup bundle: the canvas preloads both once it is idle, and an open before that loads its chunk at once. +export const canvasContextMenuSurface = preloadedSurface(() => import("./CanvasContextMenu").then((module) => module.CanvasContextMenu)); +export const canvasCommandPaletteSurface = preloadedSurface(() => import("./CanvasCommandPalette").then((module) => module.CanvasCommandPalette)); diff --git a/src/renderer/src/lib/i18n.ts b/src/renderer/src/lib/i18n.ts index b1a05d81e..eadb1b1f4 100644 --- a/src/renderer/src/lib/i18n.ts +++ b/src/renderer/src/lib/i18n.ts @@ -125,6 +125,7 @@ const ru = { keyboardNavigation: "Навигация", keyboardContexts: "В меню и полях", keyboardToggleDetail: "Пиксельная тема: полный вид / сводка", + keyboardMomEntered: "Ой мама зашла", keyboardTerminal: "Терминал", keyboardCodex: "Редактор Codex", keyboardCodexDescription: "Клавиши редактора применяются к новым или перезапущенным терминалам с расширенным TUI. Остальные CLI управляют своим редактором самостоятельно.", @@ -189,6 +190,7 @@ const ru = { pluginHooksInstallWarning: "Установка не включает их автоматически. После проверки кода их можно отдельно включить в Настройки → Агенты → Хуки; они выполняются с правами вашей учётной записи.", controls: "Управление", externalIntegrations: "Внешние интеграции", + canvasLink: "CanvasLink", plugins: "Плагины", mascots: "Маскоты", installPlugin: "Установить плагин", @@ -562,6 +564,15 @@ const ru = { canvasMenuCreateRegion: "Создать регион", canvasMenuLaunchAgent: "Запустить агента", canvasMenuOpenBrowser: "Открыть браузер", + canvasMenuTools: "Инструменты", + canvasAutoSort: "Автосортировка", + canvasAutoSortTask: "По задачам", + canvasAutoSortStatus: "По статусу", + canvasAutoSortProject: "По проекту", + canvasAutoSortGrid: "Сеткой", + canvasAutoSortUnavailable: "нужно минимум 2 окна", + canvasAutoSortUndo: "Отменить автосортировку", + canvasAutoSortFailed: "Автосортировка не удалась", canvasMenuRenameRegion: "Переименовать", canvasMenuNoteInRegion: "Заметка в регион", canvasMenuDeleteRegion: "Удалить регион", @@ -1157,6 +1168,7 @@ const en: Record = { keyboardNavigation: "Navigation", keyboardContexts: "In menus and fields", keyboardToggleDetail: "Pixel theme: full view / summary", + keyboardMomEntered: "Mom entered", keyboardTerminal: "Terminal", keyboardCodex: "Codex editor", keyboardCodexDescription: "Editor bindings apply to new or restarted terminals using the extended TUI. Other CLIs manage their own editors.", @@ -1221,6 +1233,7 @@ const en: Record = { pluginHooksInstallWarning: "Installation does not enable them automatically. After reviewing the code, enable them separately in Settings → Agents → Hooks; they run with your user account privileges.", controls: "Controls", externalIntegrations: "External integrations", + canvasLink: "CanvasLink", plugins: "Plugins", mascots: "Mascots", installPlugin: "Install plugin", @@ -1587,6 +1600,15 @@ const en: Record = { canvasMenuCreateRegion: "Create region", canvasMenuLaunchAgent: "Launch agent", canvasMenuOpenBrowser: "Open browser", + canvasMenuTools: "Tools", + canvasAutoSort: "Auto-sort", + canvasAutoSortTask: "By task", + canvasAutoSortStatus: "By status", + canvasAutoSortProject: "By project", + canvasAutoSortGrid: "Grid", + canvasAutoSortUnavailable: "needs at least 2 windows", + canvasAutoSortUndo: "Undo auto-sort", + canvasAutoSortFailed: "Auto-sort failed", canvasMenuRenameRegion: "Rename", canvasMenuNoteInRegion: "Note in region", canvasMenuDeleteRegion: "Delete region", diff --git a/src/renderer/src/lib/preloadedSurface.ts b/src/renderer/src/lib/preloadedSurface.ts new file mode 100644 index 000000000..e0e583353 --- /dev/null +++ b/src/renderer/src/lib/preloadedSurface.ts @@ -0,0 +1,69 @@ +import { useEffect, useState } from "react"; + +/** + * A surface (a menu, a palette) whose code is kept out of the startup bundle but still opens at once. Its chunk is + * loaded once, either ahead of time (`preloadWhenIdle`) or by the first open, and the loaded component is then + * rendered directly. It is not a `React.lazy` behind `Suspense`: React holds a suspended boundary's reveal for up to + * 300 ms, which delayed the first right-click and the first Cmd/Ctrl+K by that much. + */ +export interface PreloadedSurface { + /** Starts loading (once; a failed load is tried again next time) and settles with the loaded value. */ + load(): Promise; + /** The loaded value, or null while it has not loaded. */ + loaded(): T | null; +} + +export function preloadedSurface(load: () => Promise): PreloadedSurface { + let value: T | null = null; + let pending: Promise | null = null; + return { + load() { + pending ??= load().then((loaded) => { + value = loaded; + return loaded; + }, (error: unknown) => { + pending = null; + throw error; + }); + return pending; + }, + loaded: () => value + }; +} + +/** + * The surface's value for a render that shows it (`wanted`): the loaded one at once, or null while a load this open + * started is still running (the caller renders nothing until it arrives, then re-renders). + */ +export function usePreloadedSurface(surface: PreloadedSurface, wanted: boolean): T | null { + const [, setLoaded] = useState(0); + const value = surface.loaded(); + const renderedLoaded = value !== null; + useEffect(() => { + if (!wanted) return; + if (surface.loaded() !== null) { + // A preload that finished after this render read it: nothing else re-renders, so this does. + if (!renderedLoaded) setLoaded((count) => count + 1); + return; + } + let live = true; + surface.load().then(() => { + if (live) setLoaded((count) => count + 1); + }, (error: unknown) => console.error("CanvasTTY could not load a menu", error)); + return () => { live = false; }; + }, [surface, wanted, renderedLoaded]); + return wanted ? value : null; +} + +/** Loads the surfaces once the app is idle after its first paint; returns a cancel for the pending start. */ +export function preloadWhenIdle(surfaces: readonly PreloadedSurface[]): () => void { + const start = (): void => { + for (const surface of surfaces) void surface.load().catch(() => undefined); + }; + if (typeof window.requestIdleCallback === "function") { + const handle = window.requestIdleCallback(start, { timeout: 2_000 }); + return () => window.cancelIdleCallback(handle); + } + const timer = window.setTimeout(start, 200); + return () => window.clearTimeout(timer); +} diff --git a/src/renderer/src/lib/shortcuts.ts b/src/renderer/src/lib/shortcuts.ts index 0b037c770..e0c65ea92 100644 --- a/src/renderer/src/lib/shortcuts.ts +++ b/src/renderer/src/lib/shortcuts.ts @@ -43,14 +43,16 @@ export function shortcutFromPointerEvent(event: Omit & { b return [...activeCanvasNavigationModifiers(event), button].join("+"); } -export function matchesShortcut(event: ShortcutEvent, shortcut: string): boolean { +export function matchesShortcut(event: ShortcutEvent, shortcut?: string | null): boolean { + if (!shortcut) return false; return shortcutFromKeyboardEvent(event)?.toLowerCase() === shortcut.toLowerCase(); } export function matchesPointerShortcut( event: Omit & { button: number }, - shortcut: string + shortcut?: string | null ): boolean { + if (!shortcut) return false; return shortcutFromPointerEvent(event)?.toLowerCase() === shortcut.toLowerCase(); } diff --git a/src/renderer/src/styles/app.css b/src/renderer/src/styles/app.css index f4786c22b..9428f7b53 100644 --- a/src/renderer/src/styles/app.css +++ b/src/renderer/src/styles/app.css @@ -5,6 +5,34 @@ body { background: #aaa7a2; user-select: none; } button, select, input { font: inherit; } button { border: 0; } +/* Soft press from SL UI's everyday defaults source; retain CanvasTTY controls and icons. */ +:is(.settings-panel, .canvas-menu, .canvas-command-palette, .canvas-region-editor, + [role="dialog"], [role="menu"], .launch-dialog, .handoff-dialog, .terminal-link-dialog, + .terminal-card__menu, .terminal-card__actions, .browser-card__navigation, + .browser-card__popover, .browser-card__confirm, .browser-card__dialog, .browser-card__page-state, + .agent-chat-history, .home-editor-toolbar, .canvas-controls) button { + transition: background-color 180ms ease, color 180ms ease, border-color 180ms ease, + box-shadow 220ms ease, opacity 180ms ease, + transform 260ms cubic-bezier(.22, 1, .36, 1), scale 260ms cubic-bezier(.22, 1, .36, 1); +} +@media (prefers-reduced-motion: no-preference) { + :is(.settings-panel, .canvas-menu, .canvas-command-palette, .canvas-region-editor, + [role="dialog"], [role="menu"], .launch-dialog, .handoff-dialog, .terminal-link-dialog, + .terminal-card__menu, .terminal-card__actions, .browser-card__navigation, + .browser-card__popover, .browser-card__confirm, .browser-card__dialog, .browser-card__page-state, + .agent-chat-history, .home-editor-toolbar, .canvas-controls) + button:not(:disabled):not([aria-disabled="true"]):active { + scale: .98; + transition-duration: 100ms; + } + button :is(.ui-icon--reload, .ui-icon--reset) { transition: rotate 280ms cubic-bezier(.22, 1, .36, 1); } + @media (hover: hover) { + button:not(:disabled):not([aria-disabled="true"]):hover > .ui-icon--reload { rotate: 35deg; } + button:not(:disabled):not([aria-disabled="true"]):hover > .ui-icon--reset { rotate: -35deg; } + } +} +.button-with-icon { display: inline-flex; align-items: center; justify-content: center; gap: .55em; } + .app { --titlebar-height: 44px; position: relative; width: 100%; height: 100%; color: var(--text); background: var(--canvas-bg); } .app__content { position: relative; height: calc(100% - var(--titlebar-height)); } .app--macos { --titlebar-height: 32px; } @@ -398,11 +426,20 @@ button { border: 0; } .canvas-menu__kbd { padding: .2em .55em; border-radius: .4em; color: var(--text-dark); background: var(--secondary); font: 400 .78em/1.2 var(--font-mono); white-space: nowrap; } .canvas-menu__sub { color: var(--text-muted-on-dark); font-size: .92em; font-weight: 400; white-space: nowrap; } .canvas-menu__chevron { display: inline-flex; color: var(--text-muted-on-dark); } +/* A submenu that opens to the side: the same chevron, turned toward it. */ +.canvas-menu__chevron--right { transform: rotate(-90deg); } +.canvas-menu__chevron--left { transform: rotate(90deg); } +/* The hint explains the disabled rows above it: aligned with their text (1.55em of the row font) and as quiet as they are. */ +.canvas-menu__hint { margin: 0; padding: .3em .8em .45em calc(1.55em / .92); color: var(--text); opacity: .5; font-size: .92em; line-height: 1.35; } +.canvas-menu__check { display: inline-flex; color: var(--primary); } +.canvas-menu__row:disabled { cursor: default; opacity: .5; } +.canvas-menu__row:disabled:hover { background: transparent; } .canvas-menu__provider { min-width: 0; display: inline-flex; align-items: center; gap: .75em; overflow: hidden; text-overflow: ellipsis; } .canvas-menu__provider .provider-icon--small { --provider-icon-art-size: 1em; width: 1.1em; height: 1.1em; } .canvas-menu__submenu-anchor { position: relative; } .canvas-menu__submenu { top: -.45em; left: calc(100% + .55em); z-index: 1; width: 19em; min-width: 19em; margin: 0; } .canvas-menu__submenu--left { right: calc(100% + .55em); left: auto; } +.canvas-menu__submenu--up { top: auto; bottom: -.45em; } .canvas-menu__palette-row { cursor: default; } .canvas-menu__swatches { display: flex; gap: .45em; padding: .15em .3em; } .canvas-menu__swatch { width: 1.5em; height: 1.5em; flex: 0 0 auto; border: .08em solid color-mix(in srgb, var(--surface) 75%, var(--text)); border-radius: .45em; outline: 0; cursor: pointer; } @@ -676,6 +713,12 @@ button { border: 0; } .plugin-canvas-card--summary .plugin-canvas-card__summary { display: flex; } .browser-card { --card-header-height: calc(54px * var(--ui-scale, 1)); position: absolute; left: 0; top: 0; width: 920px; height: 620px; overflow: hidden; border: 1px solid rgba(255,255,255,.12); border-radius: 17px; color: white; background: #272934; box-shadow: var(--shadow-lg); transition: box-shadow .15s ease; } +/* Auto-sort: sorted windows glide to their new place, without motion when reduced motion is requested. The Browser + card jumps: its native page view is placed from the card's measured rect and would stay at the start of a glide. */ +.workspace--auto-sorting :is(.terminal-card, .plugin-canvas-card) { transition: transform .24s ease, box-shadow .15s ease; } +/* Zoomed out (summary), the outline of the windows a sort moved stays as thick on screen as at 50 % zoom. */ +.workspace--auto-sorting :is(.terminal-card--summary.terminal-card--selected, .plugin-canvas-card--summary.plugin-canvas-card--selected, .browser-card--summary.browser-card--selected) { box-shadow: 0 0 0 calc(.375rem * var(--summary-scale, 1)) var(--primary), var(--shadow-lg); } +@media (prefers-reduced-motion: reduce) { .workspace--auto-sorting :is(.terminal-card, .plugin-canvas-card) { transition: none; } } .browser-card:focus-within, .browser-card--selected { box-shadow: 0 0 0 3px var(--primary), var(--shadow-lg); } .browser-card__header { height: var(--card-header-height); padding: 0 calc(11px * var(--ui-scale, 1)) 0 calc(15px * var(--ui-scale, 1)); display: flex; align-items: center; justify-content: space-between; gap: calc(12px * var(--ui-scale, 1)); color: white; background: var(--surface-soft); cursor: move; font-size: calc(13px * var(--ui-scale, 1)); touch-action: none; } .browser-card__title { min-width: 0; display: flex; align-items: center; gap: .77em; } diff --git a/src/renderer/src/styles/pixelSkinPackCreator.css b/src/renderer/src/styles/pixelSkinPackCreator.css index 6b4b6408b..e810ec135 100644 --- a/src/renderer/src/styles/pixelSkinPackCreator.css +++ b/src/renderer/src/styles/pixelSkinPackCreator.css @@ -1,5 +1,7 @@ .pixel-pack-create { grid-column: 1 / -1; + width: 100%; + box-sizing: border-box; min-height: 44px; display: inline-flex; align-items: center; diff --git a/src/shared/canvasLink.ts b/src/shared/canvasLink.ts new file mode 100644 index 000000000..48da2c505 --- /dev/null +++ b/src/shared/canvasLink.ts @@ -0,0 +1,14 @@ +export interface CanvasLinkState { + enabled: boolean; + phase: string; + pin: string; + expires: number; + devices: Array<{ name: string }>; +} + +export type CanvasLinkCommand = "enable" | "disable" | "new-code" | "revoke"; + +export interface CanvasLinkApi { + state(): Promise; + command(action: CanvasLinkCommand): Promise; +} diff --git a/src/shared/canvasLinkInstall.ts b/src/shared/canvasLinkInstall.ts new file mode 100644 index 000000000..ff5b09d3c --- /dev/null +++ b/src/shared/canvasLinkInstall.ts @@ -0,0 +1,20 @@ +import type { LocaleId } from "./contracts"; + +// Pinned owner-signed artifact. Never add pairing codes or runtime state here. +export const CANVASLINK_ANDROID_RELEASE = { + version: "1.0.0", + versionCode: 100, + applicationId: "io.nexcore.canvaslink", + url: "https://github.com/howdeploy/CanvasTTY/releases/download/canvaslink-v1.0.0/CanvasLink-1.0.0.apk", + sha256: "296eb6f1f848fa50231494f4e5b64f2eec879e864e22fac86593cc4ac4dda690", + signerCertificateSha256: "4bdfa29b57e0d94488cd5a7af6ee6d687f3b9125f7fd094db4b2c6782ddb4357", + guideUrl: "https://github.com/howdeploy/CanvasTTY/blob/main/docs/canvaslink.md#connect", + guideRuUrl: "https://github.com/howdeploy/CanvasTTY/blob/main/docs/canvaslink.ru.md", +} as const; + +export function canvasLinkInstallPrompt(locale: LocaleId): string { + const release = CANVASLINK_ANDROID_RELEASE; + const artifact = `CanvasLink ${release.version}; Android 8+; ${release.applicationId}; versionCode ${release.versionCode}\nAPK: ${release.url}\nSHA-256: ${release.sha256}\nSigning certificate SHA-256: ${release.signerCertificateSha256}`; + if (locale === "ru") return `Установи CanvasLink на мой Android по USB с этого компьютера. Нужна CanvasTTY 1.8.3 или новее с разделом Settings → CanvasLink.\n\n${artifact}\n\nИспользуй ADB. Если его нет, установи Google Android SDK Platform-Tools только из официального источника: https://developer.android.com/tools/releases/platform-tools. Выполни adb devices -l. Если устройств несколько, сначала уточни нужное; во всех командах указывай adb -s SERIAL. Если статус unauthorized, попроси меня подтвердить RSA-запрос на Android; не обходи его. USB-отладку включает владелец устройства.\n\nСкачай именно указанный APK и до установки проверь SHA-256. При несовпадении остановись. Если доступен apksigner, также проверь подпись и отпечаток сертификата. Установи через adb -s SERIAL install -r PATH_TO_APK, сохраняя данные. При несовместимой подписи или запрете понижения версии остановись и объясни причину. Не удаляй старое приложение, не очищай данные и не используй обходные флаги.\n\nПосле успешной установки открой adb -s SERIAL shell am start -n io.nexcore.canvaslink/.MainActivity. Проверь, что приложение запустилось. Для работы нужны одна локальная сеть и включённый доступ в Settings → CanvasLink на компьютере; затем компьютер выбирается в Android и вводится одноразовый код. USB нужен только для установки. Не меняй firewall и не открывай порты в Интернет, не перезапускай CanvasTTY с активными терминалами. Не отправляй код подключения или содержимое терминалов в чат. Сообщи результат установки и оставшиеся ручные шаги.\n\nИнструкция: ${release.guideRuUrl}`; + return `Install CanvasLink on my Android over USB from this computer. Use CanvasTTY 1.8.3 or newer with Settings → CanvasLink.\n\n${artifact}\n\nUse ADB. If missing, install Google Android SDK Platform-Tools only from the official source: https://developer.android.com/tools/releases/platform-tools. Run adb devices -l. If multiple devices are present, ask which one to use; specify adb -s SERIAL in every device command. For unauthorized status, ask me to approve the RSA dialog on Android; do not bypass it. The device owner enables USB debugging.\n\nDownload the exact APK above and verify its SHA-256 before installing. Stop on mismatch. If apksigner is available, also verify the signature and certificate fingerprint. Install with adb -s SERIAL install -r PATH_TO_APK, preserving data. Stop and explain incompatible signatures or version downgrade errors. Do not uninstall the existing app, clear data, or use bypass flags.\n\nAfter successful installation, launch adb -s SERIAL shell am start -n io.nexcore.canvaslink/.MainActivity and verify that the app started. Use the same local network and enable access under Settings → CanvasLink on the computer; select the computer in Android and enter the one-time code. USB is only needed for installation. Do not change the firewall, expose ports to the Internet, or restart CanvasTTY with active terminals. Do not send pairing codes or terminal contents to chat. Report installation results and remaining manual steps.\n\nGuide: ${release.guideUrl}`; +} diff --git a/src/shared/contracts.ts b/src/shared/contracts.ts index e406aa1c2..d74dd0653 100644 --- a/src/shared/contracts.ts +++ b/src/shared/contracts.ts @@ -192,6 +192,7 @@ export interface ShortcutBindings { codexSubmitSuper: string; codexNewline: string; codexSelectAll: string; + momEntered: string; } export const DEFAULT_SHORTCUTS: ShortcutBindings = { @@ -215,7 +216,8 @@ export const DEFAULT_SHORTCUTS: ShortcutBindings = { codexSubmitAlternate: "Ctrl+Enter", codexSubmitSuper: "Meta+Enter", codexNewline: "Shift+Enter", - codexSelectAll: "Ctrl+A" + codexSelectAll: "Ctrl+A", + momEntered: "F8" }; export function keyboardPresetShortcuts(preset: Exclude): ShortcutBindings { @@ -232,7 +234,7 @@ export function keyboardPresetShortcuts(preset: Exclude action.startsWith("terminal") || action.startsWith("codex"); - return left === "toggleDetail" || right === "toggleDetail" || terminal(left) === terminal(right); + return left === "toggleDetail" || right === "toggleDetail" || left === "momEntered" || right === "momEntered" || terminal(left) === terminal(right); } export const INITIAL_TERMINAL_COLS = 80; @@ -498,6 +500,25 @@ export interface AppSettings { canvasBackground: CanvasBackgroundId; pattern: CanvasPatternId; terminalBorderSkin: TerminalBorderSkinId; + terminalBackground: import("./terminalBackground.ts").TerminalBackgroundId; + terminalBackgroundBrightness: number; + terminalBackgroundContrast: number; + terminalBackgroundSaturation: number; + terminalBackgroundPaused: boolean; + terminalBackgroundSpeed: number; + terminalBackgroundScale: number; + terminalBackgroundOffsetX: number; + terminalBackgroundOffsetY: number; + terminalBackgroundReadability: boolean; + terminalBackgroundReadabilityStrength: number; + terminalBackgroundPauseOnOutput: boolean; + terminalBackgroundFocusDimming: boolean; + terminalBackgroundSynchronized: boolean; + terminalBackgroundPauseInactive: boolean; + terminalBackgroundOverrides: Record; + terminalBackgroundPresets: import("./terminalBackgroundAppearance.ts").TerminalBackgroundPreset[]; + terminalBackgroundFavorites: Exclude[]; + terminalBackgroundDeleted: string[]; terminalSkinDetail: PixelSkinPreferredDetail; terminalSkinAnimationEnabled: boolean; appSkin: AppSkinId; @@ -1877,6 +1898,7 @@ export interface CanvasTTYApi { onStatus(listener: (status: UpdateStatus) => void): () => void; }; evenG2: import('./evenG2.ts').EvenG2Api; + canvasLink: import('./canvasLink.ts').CanvasLinkApi; appVersion(): Promise; clipboard: { readText(): Promise; diff --git a/src/shared/terminalBackground.ts b/src/shared/terminalBackground.ts new file mode 100644 index 000000000..cbb3e395e --- /dev/null +++ b/src/shared/terminalBackground.ts @@ -0,0 +1,28 @@ +import { TERMINAL_BACKGROUND_CATALOG } from "./terminalBackgroundCatalog.ts"; +export { TERMINAL_BACKGROUND_CATALOG }; +export const TERMINAL_BACKGROUNDS = TERMINAL_BACKGROUND_CATALOG.map((piece) => piece.id); +const backgroundIds = new Set(TERMINAL_BACKGROUNDS); + +export type TerminalBackgroundId = "none" | typeof TERMINAL_BACKGROUND_CATALOG[number]["id"]; + +export function isTerminalBackgroundId(value: unknown): value is TerminalBackgroundId { + return value === "none" || (typeof value === "string" && backgroundIds.has(value)); +} + +export interface AsciiPiece { + meta: { + cols?: number; + rows?: number; + fps?: number; + still?: number; + palette?: string[]; + ground?: string; + cell?: number; + fineDots?: boolean; + fullBleed?: boolean; + cover?: boolean; + contain?: boolean; + options?: Record; + }; + default(options: Record): (time: number, env: { paper: boolean; color?: Uint8Array }) => string; +} diff --git a/src/shared/terminalBackgroundAppearance.ts b/src/shared/terminalBackgroundAppearance.ts new file mode 100644 index 000000000..24c063af9 --- /dev/null +++ b/src/shared/terminalBackgroundAppearance.ts @@ -0,0 +1,73 @@ +import { isTerminalBackgroundId, type TerminalBackgroundId } from "./terminalBackground.ts"; + +export interface TerminalBackgroundAppearance { + terminalBackground: TerminalBackgroundId; + terminalBackgroundBrightness: number; + terminalBackgroundContrast: number; + terminalBackgroundSaturation: number; + terminalBackgroundPaused: boolean; + terminalBackgroundSpeed: number; + terminalBackgroundScale: number; + terminalBackgroundOffsetX: number; + terminalBackgroundOffsetY: number; + terminalBackgroundReadability: boolean; + terminalBackgroundReadabilityStrength: number; + terminalBackgroundPauseOnOutput: boolean; + terminalBackgroundFocusDimming: boolean; +} + +export interface TerminalBackgroundPreset { + id: string; + name: string; + appearance: TerminalBackgroundAppearance; +} + +export const BACKGROUND_APPEARANCE_DEFAULTS: TerminalBackgroundAppearance = { + terminalBackground: "none", terminalBackgroundBrightness: 45, + terminalBackgroundContrast: 100, terminalBackgroundSaturation: 100, + terminalBackgroundPaused: false, terminalBackgroundSpeed: 1, + terminalBackgroundScale: 100, terminalBackgroundOffsetX: 0, terminalBackgroundOffsetY: 0, + terminalBackgroundReadability: true, terminalBackgroundReadabilityStrength: 86, + terminalBackgroundPauseOnOutput: false, + terminalBackgroundFocusDimming: false, +}; + +export function normalizeBackgroundAppearance(value: unknown): TerminalBackgroundAppearance { + const source = value && typeof value === "object" ? value as Record : {}; + const number = (key: keyof TerminalBackgroundAppearance, min: number, max: number): number => + typeof source[key] === "number" && Number.isFinite(source[key]) + ? Math.min(max, Math.max(min, source[key] as number)) : BACKGROUND_APPEARANCE_DEFAULTS[key] as number; + return { + terminalBackground: isTerminalBackgroundId(source.terminalBackground) ? source.terminalBackground : "none", + terminalBackgroundBrightness: number("terminalBackgroundBrightness", 10, 100), + terminalBackgroundContrast: number("terminalBackgroundContrast", 0, 200), + terminalBackgroundSaturation: number("terminalBackgroundSaturation", 0, 200), + terminalBackgroundSpeed: number("terminalBackgroundSpeed", .25, 2), + terminalBackgroundScale: number("terminalBackgroundScale", 25, 300), + terminalBackgroundOffsetX: number("terminalBackgroundOffsetX", -50, 50), + terminalBackgroundOffsetY: number("terminalBackgroundOffsetY", -50, 50), + terminalBackgroundReadabilityStrength: number("terminalBackgroundReadabilityStrength", 0, 100), + terminalBackgroundPaused: typeof source.terminalBackgroundPaused === "boolean" ? source.terminalBackgroundPaused : false, + terminalBackgroundReadability: typeof source.terminalBackgroundReadability === "boolean" ? source.terminalBackgroundReadability : true, + terminalBackgroundPauseOnOutput: typeof source.terminalBackgroundPauseOnOutput === "boolean" ? source.terminalBackgroundPauseOnOutput : false, + terminalBackgroundFocusDimming: typeof source.terminalBackgroundFocusDimming === "boolean" ? source.terminalBackgroundFocusDimming : false, + }; +} + +export function normalizeBackgroundOverrides(value: unknown): Record { + if (!value || typeof value !== "object" || Array.isArray(value)) return {}; + return Object.fromEntries(Object.entries(value).filter(([id, appearance]) => + id.length > 0 && id.length <= 200 && appearance && typeof appearance === "object" && !Array.isArray(appearance)) + .map(([id, appearance]) => [id, normalizeBackgroundAppearance(appearance)])); +} + +export function normalizeBackgroundPresets(value: unknown): TerminalBackgroundPreset[] { + if (!Array.isArray(value)) return []; + const ids = new Set(); + return value.filter(item => { + if (!item || typeof item.id !== "string" || !item.id || item.id.length > 200 || ids.has(item.id) + || typeof item.name !== "string" || !item.name.trim() || !item.appearance || typeof item.appearance !== "object" || Array.isArray(item.appearance)) return false; + ids.add(item.id); + return true; + }).map(item => ({ id: item.id, name: item.name.trim().slice(0, 80), appearance: normalizeBackgroundAppearance(item.appearance) })); +} diff --git a/src/shared/terminalBackgroundCatalog.ts b/src/shared/terminalBackgroundCatalog.ts new file mode 100644 index 000000000..b39dadeed --- /dev/null +++ b/src/shared/terminalBackgroundCatalog.ts @@ -0,0 +1,866 @@ +export const TERMINAL_BACKGROUND_CATALOG = [ + { + "id": "alpine-dawn", + "name": "alpine dawn", + "category": "scenes", + "note": "snow peaks catching first light above a still, misty mountain lake" + }, + { + "id": "analog-clock", + "name": "analog clock", + "category": "objects", + "note": "a round clock face showing the local time, seconds ticking" + }, + { + "id": "aquarium", + "name": "aquarium", + "category": "creatures", + "note": "fish swimming and turning in a tank, bubbles and weed" + }, + { + "id": "aurora", + "name": "aurora", + "category": "nature", + "note": "an aurora curtain rippling over a treeline under stars" + }, + { + "id": "aurora-fjord", + "name": "aurora fjord", + "category": "scenes", + "note": "aurora curtains rippling over a still fjord, a cabin lit on the shore" + }, + { + "id": "bar-chart", + "name": "bar chart", + "category": "data", + "note": "bars easing between three datasets, values riding on top" + }, + { + "id": "big-text", + "name": "big text", + "category": "type", + "note": "a block-letter banner with a line shadow and a passing glint" + }, + { + "id": "black-hole", + "name": "black hole", + "category": "space", + "note": "a black hole, its lensed disk turning, one side brighter" + }, + { + "id": "bonsai", + "name": "bonsai", + "category": "nature", + "note": "a potted tree that sheds its leaves and leafs out again" + }, + { + "id": "boot-log", + "name": "boot log", + "category": "ui", + "note": "a self test counts memory, then the boot log scrolls by" + }, + { + "id": "bouncing-balls", + "name": "bouncing balls", + "category": "physics", + "note": "rubber balls bouncing lower each time, squashing on landing" + }, + { + "id": "box-frames", + "name": "box frames", + "category": "ui", + "note": "twelve frame styles to copy, each one labelled" + }, + { + "id": "butterfly", + "name": "butterfly", + "category": "creatures", + "note": "a butterfly flapping and gliding along a wandering path" + }, + { + "id": "calendar", + "name": "calendar", + "category": "ui", + "note": "this month like cal, today bracketed, time ticking below" + }, + { + "id": "campfire", + "name": "campfire", + "category": "nature", + "note": "a log fire flickering, sparks rising; denser ink is hotter" + }, + { + "id": "candle", + "name": "candle", + "category": "objects", + "note": "a flickering candle burning down in its holder, then renewed" + }, + { + "id": "candlesticks", + "name": "candlesticks", + "category": "data", + "note": "hollow up candles, solid down ones, the last one forming live" + }, + { + "id": "cat", + "name": "cat", + "category": "creatures", + "note": "a tabby asleep on its paws, breathing slowly" + }, + { + "id": "cherry-blossom", + "name": "cherry blossom", + "category": "nature", + "note": "a flowering cherry branch shedding petals on the wind" + }, + { + "id": "chladni", + "name": "chladni plate", + "category": "physics", + "note": "sand travelling to the nodal lines of a ringing plate" + }, + { + "id": "coffee", + "name": "coffee", + "category": "objects", + "note": "steam curling up from a cup of coffee on its saucer" + }, + { + "id": "contour-map", + "name": "contour map", + "category": "nature", + "note": "an island in contour lines, its hills slowly reshaping" + }, + { + "id": "cpu-meters", + "name": "cpu meters", + "category": "data", + "note": "htop-style core, memory and swap meters over a process list" + }, + { + "id": "cube", + "name": "cube", + "category": "shapes", + "note": "a stippled hidden-line cube turning a quarter at a time" + }, + { + "id": "deep-reef", + "name": "deep reef", + "category": "scenes", + "note": "light shafts, swaying kelp and a turning school of fish over a reef" + }, + { + "id": "desert-night", + "name": "desert night", + "category": "scenes", + "note": "the milky way over a lone acacia on moonless dunes, meteors falling" + }, + { + "id": "digital-clock", + "name": "digital clock", + "category": "ui", + "note": "the local time in seven-segment digits, colon blinking" + }, + { + "id": "dissolve", + "name": "dissolve", + "category": "type", + "note": "words that crumble to dust and condense into the next" + }, + { + "id": "dividers", + "name": "dividers", + "category": "ui", + "note": "rules and ornaments for between sections, plain to fancy" + }, + { + "id": "dna-helix", + "name": "dna helix", + "category": "shapes", + "note": "a double helix with base pairs, turning about its axis" + }, + { + "id": "donut", + "name": "donut", + "category": "shapes", + "note": "a lit torus turning on two axes, after donut.c" + }, + { + "id": "doom-fire", + "name": "doom fire", + "category": "effects", + "note": "the psx fire spread, tongues climbing off a white-hot bed" + }, + { + "id": "double-pendulum", + "name": "double pendulum", + "category": "physics", + "note": "a chaotic double pendulum; its tip leaves a fading trail" + }, + { + "id": "earth", + "name": "earth", + "category": "space", + "note": "a lit globe turning, continents from a five-degree map" + }, + { + "id": "earthrise", + "name": "earthrise", + "category": "scenes", + "note": "the earth rising over a cratered lunar horizon in long low sunlight" + }, + { + "id": "eclipse", + "name": "eclipse", + "category": "space", + "note": "the moon slides over the sun; diamond ring, then corona" + }, + { + "id": "epicycles", + "name": "epicycles", + "category": "generative", + "note": "a chain of turning circles whose tip traces a star" + }, + { + "id": "equalizer", + "name": "equalizer", + "category": "data", + "note": "spectrum bars bouncing to a beat, peak caps falling slowly" + }, + { + "id": "falling-sand", + "name": "falling sand", + "category": "physics", + "note": "sand pouring into a heap, then draining through the shelf" + }, + { + "id": "fern", + "name": "fern", + "category": "nature", + "note": "barnsley's fern filling in point by point, then swaying" + }, + { + "id": "ferris-wheel", + "name": "ferris wheel", + "category": "objects", + "note": "a wheel turning on its a-frame, cabins hanging upright" + }, + { + "id": "file-tree", + "name": "file tree", + "category": "ui", + "note": "a tree listing opened folder by folder, then closed again" + }, + { + "id": "fireflies", + "name": "fireflies", + "category": "nature", + "note": "fireflies over night grass, a flash spreading through them" + }, + { + "id": "fireworks", + "name": "fireworks", + "category": "effects", + "note": "shells climb and burst as peonies, rings, willows and palms" + }, + { + "id": "flag", + "name": "flag", + "category": "physics", + "note": "a striped flag rippling on a pole, folds catching the light" + }, + { + "id": "flow-field", + "name": "flow field", + "category": "generative", + "note": "particle trails gathering into winding, merging streams" + }, + { + "id": "form-controls", + "name": "form controls", + "category": "ui", + "note": "a terminal style form being filled in, field by field" + }, + { + "id": "fountain", + "name": "fountain", + "category": "physics", + "note": "arcs of water from a nozzle falling as drops into a basin" + }, + { + "id": "fox", + "name": "fox", + "category": "creatures", + "note": "a fox asleep on its tail, breathing, peeking now and then" + }, + { + "id": "fractal-tree", + "name": "fractal tree", + "category": "nature", + "note": "a recursive branching tree swaying in the wind" + }, + { + "id": "galaxy", + "name": "galaxy", + "category": "space", + "note": "a tilted spiral galaxy turning, its inner stars the fastest" + }, + { + "id": "gauge", + "name": "gauge", + "category": "data", + "note": "semicircle dial, the needle overshoots each new reading" + }, + { + "id": "glider-gun", + "name": "glider gun", + "category": "generative", + "note": "gosper's gun firing a glider every 30 generations" + }, + { + "id": "glitch", + "name": "glitch", + "category": "type", + "note": "a pixel word that tears into sliding slices in short bursts" + }, + { + "id": "glxgears", + "name": "glxgears", + "category": "shapes", + "note": "three meshing gears turning, after glxgears" + }, + { + "id": "gyroscope", + "name": "gyroscope", + "category": "shapes", + "note": "gimbals swing round a spinning rotor whose axle holds still" + }, + { + "id": "harmonograph", + "name": "harmonograph", + "category": "physics", + "note": "a pen on two decaying pendulums winding a figure inward" + }, + { + "id": "hawa-mahal", + "name": "hawa mahal", + "category": "objects", + "note": "jaipur's palace of winds at night, lamps behind the jali" + }, + { + "id": "heart", + "name": "heart", + "category": "shapes", + "note": "a glossy 3d heart swaying and beating lub-dub" + }, + { + "id": "heartbeat", + "name": "heartbeat", + "category": "data", + "note": "an ecg trace sweeping over its last pass, with a bpm readout" + }, + { + "id": "heatmap", + "name": "heatmap", + "category": "data", + "note": "contribution calendar, a day at a time, weeks scrolling left" + }, + { + "id": "hilbert-curve", + "name": "hilbert curve", + "category": "generative", + "note": "a closed loop of two hilbert curves, with two lit runners" + }, + { + "id": "hourglass", + "name": "hourglass", + "category": "objects", + "note": "sand running through the neck, then turned over" + }, + { + "id": "icosahedron", + "name": "icosahedron", + "category": "shapes", + "note": "a wireframe icosahedron turning, its hidden edges dotted" + }, + { + "id": "jellyfish", + "name": "jellyfish", + "category": "creatures", + "note": "a jellyfish pulsing upward, its tentacles trailing late" + }, + { + "id": "julia-set", + "name": "julia set", + "category": "generative", + "note": "a julia set changing shape as its constant c goes round" + }, + { + "id": "kite", + "name": "kite", + "category": "objects", + "note": "a diamond kite riding the wind, its string sagging in lulls" + }, + { + "id": "kyoto-dusk", + "name": "kyoto dusk", + "category": "scenes", + "note": "a pagoda at dusk behind a cherry tree lit by a stone lantern" + }, + { + "id": "landscape", + "name": "landscape", + "category": "nature", + "note": "a range and a lake under the sun or moon of the local hour" + }, + { + "id": "langtons-ant", + "name": "langton's ant", + "category": "generative", + "note": "order, then chaos, then the ant builds its highway" + }, + { + "id": "lantern-lake", + "name": "lantern lake", + "category": "scenes", + "note": "sky lanterns rising over a still mountain lake, one let go from a boat" + }, + { + "id": "lava-lamp", + "name": "lava lamp", + "category": "objects", + "note": "wax blobs rising, merging and sinking in a lit glass" + }, + { + "id": "lighthouse", + "name": "lighthouse", + "category": "objects", + "note": "a beam sweeping over night sea from a lighthouse on rocks" + }, + { + "id": "lightning", + "name": "lightning", + "category": "nature", + "note": "a forked bolt striking out of a storm cloud in the rain" + }, + { + "id": "lorenz", + "name": "lorenz attractor", + "category": "physics", + "note": "one lorenz trajectory looping both wings, older loops fading" + }, + { + "id": "mandelbrot", + "name": "mandelbrot", + "category": "generative", + "note": "a slow zoom into seahorse valley and back out again" + }, + { + "id": "marine-drive", + "name": "marine drive", + "category": "scenes", + "note": "mumbai's queen's necklace at night, lamps curving round the bay" + }, + { + "id": "marquee", + "name": "marquee", + "category": "type", + "note": "a scrolling led sign framed by chasing bulbs" + }, + { + "id": "matrix-rain", + "name": "matrix rain", + "category": "effects", + "note": "streams of glyphs falling, heavy at the head, thin behind" + }, + { + "id": "maze", + "name": "maze", + "category": "generative", + "note": "a maze carved by backtracking, then solved, then remade" + }, + { + "id": "misty-forest", + "name": "misty forest", + "category": "scenes", + "note": "pine ridges fading into morning fog, sunbeams slanting through" + }, + { + "id": "mobius-strip", + "name": "mobius strip", + "category": "shapes", + "note": "a mobius strip turning and nodding, lit from above" + }, + { + "id": "moon-phases", + "name": "moon phases", + "category": "space", + "note": "the moon waxing and waning, its maria darker in the light" + }, + { + "id": "morse", + "name": "morse", + "category": "type", + "note": "a lamp keying morse, the marks on a tape, letters decoded" + }, + { + "id": "newtons-cradle", + "name": "newton's cradle", + "category": "physics", + "note": "five steel balls on strings passing one knock back and forth" + }, + { + "id": "night-coast", + "name": "night coast", + "category": "scenes", + "note": "a lighthouse turning its beam under moonlit clouds over a dark sea" + }, + { + "id": "not-found", + "name": "not found", + "category": "ui", + "note": "a block-letter 404 with a small ghost drifting beneath it" + }, + { + "id": "ocean-sunset", + "name": "ocean sunset", + "category": "scenes", + "note": "the sun sets past a pine headland, lit cloud, glitter on rolling sea" + }, + { + "id": "owl", + "name": "owl", + "category": "creatures", + "note": "an owl on a branch, blinking slowly and turning its head" + }, + { + "id": "pendulum-wave", + "name": "pendulum wave", + "category": "physics", + "note": "a row of pendulums drifting in and out of step, from above" + }, + { + "id": "planet", + "name": "planet", + "category": "space", + "note": "a banded ringed planet turning, with shadows both ways" + }, + { + "id": "plasma", + "name": "plasma", + "category": "generative", + "note": "soft interference blobs drifting and pulsing" + }, + { + "id": "plucked-string", + "name": "plucked string", + "category": "physics", + "note": "a plucked string, slowed down, its harmonics fading" + }, + { + "id": "pond-ripples", + "name": "pond ripples", + "category": "physics", + "note": "raindrops opening rings that cross and fade on a pond" + }, + { + "id": "progress-bar", + "name": "progress bar", + "category": "ui", + "note": "five bar styles filling at uneven paces, percent at right" + }, + { + "id": "radar", + "name": "radar", + "category": "data", + "note": "rotating sweep with afterglow, contacts fade between passes" + }, + { + "id": "rain", + "name": "rain", + "category": "nature", + "note": "slanting rain in three depths, crowns and puddle rings" + }, + { + "id": "reaction-diffusion", + "name": "reaction diffusion", + "category": "generative", + "note": "gray-scott spots and stripes dying back and growing out" + }, + { + "id": "rocket", + "name": "rocket", + "category": "space", + "note": "a rocket lifting off its pad at night, then landing back" + }, + { + "id": "rotozoomer", + "name": "rotozoomer", + "category": "effects", + "note": "a brick wall texture turning and zooming about the centre" + }, + { + "id": "rule-30", + "name": "rule 30", + "category": "generative", + "note": "one cell grows a row at a time under rules 30, 90 and 110" + }, + { + "id": "ruled-mountains", + "name": "ruled mountains", + "category": "nature", + "note": "ruled lines with a range rising from them, drifting forward" + }, + { + "id": "saptarishi", + "name": "saptarishi", + "category": "space", + "note": "the big dipper at true positions in a twinkling sky" + }, + { + "id": "scramble", + "name": "scramble", + "category": "type", + "note": "phrases decoding left to right out of flickering glyphs" + }, + { + "id": "sea-swell", + "name": "sea swell", + "category": "nature", + "note": "a long swell lifting a small sailboat as it rolls under" + }, + { + "id": "sierpinski", + "name": "sierpinski", + "category": "generative", + "note": "the chaos game filling a sierpinski triangle point by point" + }, + { + "id": "skeleton", + "name": "skeleton", + "category": "ui", + "note": "a loading card in shade blocks with a shimmer passing over" + }, + { + "id": "skyline", + "name": "skyline", + "category": "objects", + "note": "a city at night, windows going on and off, a blinking mast" + }, + { + "id": "smoke", + "name": "smoke", + "category": "physics", + "note": "incense smoke rising straight, then curling and thinning" + }, + { + "id": "snake", + "name": "snake", + "category": "creatures", + "note": "a snake slithering along an s-curve, flicking its tongue" + }, + { + "id": "snowfall", + "name": "snowfall", + "category": "nature", + "note": "snow swaying down, building drifts on the ground and a fence" + }, + { + "id": "solar-system", + "name": "solar system", + "category": "space", + "note": "four planets on dotted orbits, periods by kepler's third law" + }, + { + "id": "sparkline", + "name": "sparkline", + "category": "data", + "note": "three live block sparklines scrolling left, values at right" + }, + { + "id": "sparks", + "name": "sparks", + "category": "effects", + "note": "an angle grinder throwing sparks that arc, skip and cool" + }, + { + "id": "spider", + "name": "spider", + "category": "creatures", + "note": "a spider lowering itself on silk, then climbing back up" + }, + { + "id": "spinners", + "name": "spinners", + "category": "ui", + "note": "twelve loading spinners side by side, each with its name" + }, + { + "id": "split-flap", + "name": "split-flap", + "category": "type", + "note": "a departures board whose flaps riffle to each new line" + }, + { + "id": "spring", + "name": "spring", + "category": "shapes", + "note": "a weight bouncing on a coil spring, its coils bunching" + }, + { + "id": "starfield", + "name": "starfield", + "category": "space", + "note": "stars rush out from the centre and streak at the edges" + }, + { + "id": "starlings", + "name": "starlings", + "category": "creatures", + "note": "a murmuration wheeling about, split by a passing falcon" + }, + { + "id": "storm-plains", + "name": "storm plains", + "category": "scenes", + "note": "an anvil thunderhead at dusk flickering over a wheat field" + }, + { + "id": "sundial", + "name": "sundial", + "category": "objects", + "note": "jaipur's samrat yantra, its shadow set by your local time" + }, + { + "id": "sunrise", + "name": "sunrise", + "category": "nature", + "note": "the sun lifting off a sea horizon, its path glittering" + }, + { + "id": "synthwave", + "name": "synthwave", + "category": "effects", + "note": "a grid rolling toward a striped sun behind a mountain ridge" + }, + { + "id": "taj-dawn", + "name": "taj dawn", + "category": "scenes", + "note": "the taj mahal at sunrise, mirrored in its pool through morning haze" + }, + { + "id": "terminal", + "name": "terminal", + "category": "ui", + "note": "a shell session: typed commands, output, scrolling, clear" + }, + { + "id": "tesseract", + "name": "tesseract", + "category": "shapes", + "note": "a 4d hypercube turning, its cubes passing through each other" + }, + { + "id": "three-body", + "name": "three-body", + "category": "space", + "note": "three equal masses sharing one figure-eight orbit" + }, + { + "id": "tokyo-rain", + "name": "tokyo rain", + "category": "scenes", + "note": "a tokyo side street in the rain, neon signs shining on the wet road" + }, + { + "id": "torus-knot", + "name": "torus knot", + "category": "shapes", + "note": "a trefoil knot drawn as a lit tube, turning slowly" + }, + { + "id": "train", + "name": "train", + "category": "objects", + "note": "a steam train crossing on its track, smoke puffing behind" + }, + { + "id": "tunnel", + "name": "tunnel", + "category": "effects", + "note": "flying down a tiled tube that bends and twists" + }, + { + "id": "tv-static", + "name": "tv static", + "category": "effects", + "note": "snow on an old set that tunes into a test card and loses it" + }, + { + "id": "twisted-ring", + "name": "twisted ring", + "category": "shapes", + "note": "a square ring twisted three quarters, so it has one face" + }, + { + "id": "typewriter", + "name": "typewriter", + "category": "type", + "note": "phrases typed and deleted in turn behind a blinking cursor" + }, + { + "id": "uptime-bar", + "name": "uptime bar", + "category": "data", + "note": "60-day uptime bars for three services, a new day slides in" + }, + { + "id": "varanasi-ghats", + "name": "varanasi ghats", + "category": "scenes", + "note": "dusk aarti on the ganga, lamps on the steps and diyas on the water" + }, + { + "id": "vinyl", + "name": "vinyl", + "category": "objects", + "note": "a record turning under the tonearm, seen from above" + }, + { + "id": "voronoi", + "name": "voronoi", + "category": "generative", + "note": "cells of ground nearest each drifting seed" + }, + { + "id": "wave-interference", + "name": "wave interference", + "category": "physics", + "note": "two sources in a ripple tank, still lines fanning out" + }, + { + "id": "wave-text", + "name": "wave text", + "category": "type", + "note": "a swell running through a line of text, left to right" + }, + { + "id": "whale", + "name": "whale", + "category": "creatures", + "note": "a whale blows, arches its back and dives, fluke last" + }, + { + "id": "wind", + "name": "wind", + "category": "nature", + "note": "gusts streaming along bending lines, curling at the head" + }, + { + "id": "windmill", + "name": "windmill", + "category": "objects", + "note": "four lattice sails turning on a dutch mill, clouds passing" + } +] as const; diff --git a/tests/browser-canvas-controllers.test.mjs b/tests/browser-canvas-controllers.test.mjs index e6d291d9c..166159a9c 100644 --- a/tests/browser-canvas-controllers.test.mjs +++ b/tests/browser-canvas-controllers.test.mjs @@ -92,6 +92,17 @@ function gestureHarness() { }; } +test("renderer wheel origins use native coordinates when the app is zoomed", () => { + const harness = gestureHarness(); + // CSS (88, 88) is outside the native slot; at 125% it is DIP (110, 110), inside it. + harness.controller.beginRendererSequence({ clientX: 88, clientY: 88 }, 1.25); + assert.equal(harness.controller.surfaceDecision("tab-1", { + x: 100, y: 100, width: 800, height: 600 + }, { width: 1200, height: 900 }).kind, "frozen"); + assert.equal(harness.controller.isFreezeActive, true); + harness.controller.endSequence(false); +}); + test("BrowserCanvasGestureController latches focus-aware page ownership until reset", () => { const harness = gestureHarness(); harness.setFocused(true); diff --git a/tests/browser-viewport.test.mjs b/tests/browser-viewport.test.mjs index 0a27ed9bd..af1955020 100644 --- a/tests/browser-viewport.test.mjs +++ b/tests/browser-viewport.test.mjs @@ -1,11 +1,51 @@ import assert from "node:assert/strict"; import test from "node:test"; import { + browserCanvasEventToCss, clipBrowserViewportBounds, normalizeBrowserViewportBounds, sameBrowserViewport } from "../src/main/services/browser/BrowserViewport.ts"; +test("renderer zoom converts CSS viewport and clip edges to native DIPs before rounding", () => { + const viewport = normalizeBrowserViewportBounds({ + x: 10.4, y: 20.6, width: 100.2, height: 50.1, surface: "native", canvasScale: 0.5, + clipBounds: { x: 0.2, y: 12.2, width: 300.6, height: 200.4 } + }, 0.8); + assert.deepEqual(viewport, { + x: 8, y: 16, width: 81, height: 41, surface: "native", canvasScale: 0.4, + clipBounds: { x: 1, y: 10, width: 239, height: 160 }, showAgentPresence: false + }); + assert.deepEqual(clipBrowserViewportBounds(viewport, { width: 100, height: 40 }), { + x: 8, y: 16, width: 81, height: 24 + }); +}); + +test("saved negative renderer zoom and enlarged renderer zoom scale the native page along with its slot", () => { + for (const zoom of [1.2 ** -0.5, 1.25]) { + const viewport = normalizeBrowserViewportBounds({ + x: 200, y: 160, width: 420, height: 260, surface: "native", canvasScale: 1.4 + }, zoom); + assert.equal(viewport.x, Math.floor(200 * zoom)); + assert.equal(viewport.width, Math.ceil(620 * zoom) - Math.floor(200 * zoom)); + assert.equal(viewport.canvasScale, 1.4 * zoom); + assert.equal(normalizeBrowserViewportBounds({ + x: 0, y: 0, width: 420, height: 260, surface: "native" + }, zoom).canvasScale, zoom); + } + for (const zoom of [0, -1, Infinity, NaN]) { + assert.equal(normalizeBrowserViewportBounds({ x: 0, y: 0, width: 1, height: 1, surface: "native" }, zoom), null); + } +}); + +test("native pointer and wheel anchors return in renderer CSS pixels without changing wheel deltas", () => { + const event = { tabId: "tab", clientX: 250, clientY: 200, deltaX: 2, deltaY: 18, type: "down" }; + assert.deepEqual(browserCanvasEventToCss(event, 1.25), { + ...event, clientX: 200, clientY: 160 + }); + assert.equal(event.clientX, 250); +}); + test("browser viewport expands fractional edges instead of exposing compositor gaps", () => { assert.deepEqual(normalizeBrowserViewportBounds({ x: 10.4, diff --git a/tests/canvas-auto-sort.test.mjs b/tests/canvas-auto-sort.test.mjs new file mode 100644 index 000000000..cc9a99f47 --- /dev/null +++ b/tests/canvas-auto-sort.test.mjs @@ -0,0 +1,614 @@ +import assert from "node:assert/strict"; +import { readFile } from "node:fs/promises"; +import test from "node:test"; +import { findAll, importWithFakeReact, tick } from "./helpers/fake-react.mjs"; +import { arrangeCanvasAutoSort, planCanvasAutoSort } from "../src/renderer/src/features/workspace/canvasAutoSort.ts"; +import { + CANVAS_AUTO_SORT_MODES, + canvasAutoSortScope, + canvasAutoSortUndoTargets +} from "../src/renderer/src/features/workspace/canvasAutoSortScope.ts"; + +const source = async (path) => (await readFile(new URL(`../${path}`, import.meta.url), "utf8")).replace(/\r\n/g, "\n"); +const card = (x = 0, y = 0, width = 700, height = 430) => ({ position: { x, y }, size: { width, height } }); +const overlaps = (a, b) => a.position.x < b.position.x + b.size.width + && a.position.x + a.size.width > b.position.x + && a.position.y < b.position.y + b.size.height + && a.position.y + a.size.height > b.position.y; +const assertDisjoint = (placements) => { + const entries = [...placements.entries()]; + for (let i = 0; i < entries.length; i += 1) { + for (let j = i + 1; j < entries.length; j += 1) { + assert.equal(overlaps(entries[i][1], entries[j][1]), false, `${entries[i][0]} overlaps ${entries[j][0]}`); + } + } +}; +const HOME = card(0, 0, 1_600, 1_200); +const GAP = 20; +const grown = (bounds, by) => card(bounds.position.x - by, bounds.position.y - by, bounds.size.width + 2 * by, bounds.size.height + 2 * by); +const blockOf = (placements) => { + const all = [...placements.values()]; + const left = Math.min(...all.map((b) => b.position.x)); + const top = Math.min(...all.map((b) => b.position.y)); + return card(left, top, Math.max(...all.map((b) => b.position.x + b.size.width)) - left, + Math.max(...all.map((b) => b.position.y + b.size.height)) - top); +}; +const STATUSES = ["working", "needs_approval", "done", "failed", "idle", "unavailable"]; + +function mixedItems(count) { + return Array.from({ length: count }, (_, index) => ({ + id: `terminal:s${index}`, + bounds: card(2_000 + (index % 7) * 97, 300 + (index % 5) * 61, 420 + (index % 4) * 90, 260 + (index % 5) * 70), + ...(index > 0 && index % 3 !== 0 ? { parentId: `terminal:s${Math.floor((index - 1) / 3)}` } : {}), + status: STATUSES[index % STATUSES.length], + project: ["/work/one", "/work/two/", "/work/three"][index % 3], + startedAt: 1_000 + index + })); +} + +test("every criterion keeps mixed-size windows disjoint, sizes unchanged, and clear of HOME", () => { + for (let count = 1; count <= 40; count += 1) { + const items = mixedItems(count); + for (const mode of CANVAS_AUTO_SORT_MODES) { + const result = arrangeCanvasAutoSort(items, mode, { avoid: [HOME] }); + assert.deepEqual([...result.keys()].sort(), items.map((item) => item.id).sort()); + assertDisjoint(result); + for (const item of items) { + assert.deepEqual(result.get(item.id).size, item.bounds.size, `${mode} keeps ${item.id} size`); + assert.equal(overlaps(result.get(item.id), HOME), false, `${mode} keeps ${item.id} off HOME`); + } + } + } +}); + +test("auto-sort is deterministic: input order does not matter and sorting twice changes nothing", () => { + const items = mixedItems(13); + const reversed = [...items].reverse(); + for (const mode of CANVAS_AUTO_SORT_MODES) { + const first = arrangeCanvasAutoSort(items, mode, { avoid: [HOME], snapToGrid: true }); + assert.deepEqual(arrangeCanvasAutoSort(reversed, mode, { avoid: [HOME], snapToGrid: true }), first, `${mode} ignores list order`); + const again = arrangeCanvasAutoSort( + items.map((item) => ({ ...item, bounds: first.get(item.id) })), + mode, + { avoid: [HOME], snapToGrid: true } + ); + assert.deepEqual(again, first, `${mode} is idempotent`); + } +}); + +test("the block stays at the windows' top-left corner and moves off HOME by the shorter way only when it would cover it", () => { + const away = [ + { id: "a", bounds: card(3_013, 517) }, + { id: "b", bounds: card(5_000, 900) } + ]; + const kept = arrangeCanvasAutoSort(away, "grid", { avoid: [HOME] }); + assert.deepEqual(kept.get("a").position, { x: 3_013, y: 517 }); + const snapped = arrangeCanvasAutoSort(away, "grid", { avoid: [HOME], snapToGrid: true }); + assert.deepEqual(snapped.get("a").position, { x: 3_010, y: 520 }); + + // Two 700-wide cards side by side: leaving HOME downwards (1,120) is shorter than to the right (1,520). + const onHome = [ + { id: "a", bounds: card(100, 100) }, + { id: "b", bounds: card(200, 900) } + ]; + const moved = arrangeCanvasAutoSort(onHome, "grid", { avoid: [HOME] }); + assert.deepEqual(blockOf(moved).position, { x: 100, y: HOME.size.height + GAP }); + // A tall column next to HOME's right edge leaves to the right. + const tall = arrangeCanvasAutoSort([ + { id: "a", bounds: card(1_500, 0, 300, 2_000) }, + { id: "b", bounds: card(1_500, 2_100, 300, 2_000) } + ], "grid", { avoid: [HOME] }); + assert.deepEqual(blockOf(tall).position, { x: HOME.size.width + GAP, y: 0 }); +}); + +test("with snapping the block is snapped first, then checked against HOME, then moved off it onto the grid", () => { + for (const [x, y] of [[1_585, 40], [1_596, 40], [1_612, 333], [1_619, 0], [40, 1_185], [333, 1_214]]) { + const items = [{ id: "a", bounds: card(x, y, 455, 333) }, { id: "b", bounds: card(x + 7, y + 900, 455, 333) }]; + for (const mode of CANVAS_AUTO_SORT_MODES) { + const result = arrangeCanvasAutoSort(items, mode, { avoid: [HOME], snapToGrid: true }); + for (const bounds of result.values()) { + assert.equal(bounds.position.x % 10, 0, `${mode} from ${x},${y}: x on the grid`); + assert.equal(bounds.position.y % 10, 0, `${mode} from ${x},${y}: y on the grid`); + assert.equal(overlaps(grown(bounds, GAP), HOME), false, `${mode} from ${x},${y}: the gap to HOME is kept`); + } + } + } + // Already clear of HOME by the gap once snapped: the block stays where it is. + const clear = arrangeCanvasAutoSort([ + { id: "a", bounds: card(1_624, 0) }, { id: "b", bounds: card(1_624, 600) } + ], "grid", { avoid: [HOME], snapToGrid: true }); + assert.deepEqual(blockOf(clear).position, { x: 1_620, y: 0 }); +}); + +test("with snapping every window lands on the 10px grid with at least the 20px snapping gap", () => { + for (let count = 2; count <= 40; count += 1) { + const items = mixedItems(count).map((item, index) => ({ + ...item, + bounds: card(item.bounds.position.x + 3, item.bounds.position.y + 7, item.bounds.size.width + (index % 9), item.bounds.size.height + (index % 7)) + })); + for (const mode of CANVAS_AUTO_SORT_MODES) { + const result = arrangeCanvasAutoSort(items, mode, { avoid: [HOME], snapToGrid: true }); + const entries = [...result.entries()]; + for (const [id, bounds] of entries) { + assert.equal(bounds.position.x % 10, 0, `${mode} ${id} x`); + assert.equal(bounds.position.y % 10, 0, `${mode} ${id} y`); + } + for (let i = 0; i < entries.length; i += 1) { + for (let j = i + 1; j < entries.length; j += 1) { + assert.equal(overlaps(grown(entries[i][1], GAP), entries[j][1]), false, `${mode}: ${entries[i][0]} and ${entries[j][0]} keep the gap`); + } + } + } + } + const neighbours = arrangeCanvasAutoSort([{ id: "a", bounds: card(3_000, 0) }, { id: "b", bounds: card(3_000, 600) }], "grid", {}); + assert.equal(neighbours.get("b").position.x - (neighbours.get("a").position.x + 700), GAP, "neighbours sit exactly one gap apart"); +}); + +test("the block keeps clear of regions, notes and windows it does not move, and never lands inside a region", () => { + const layerIdOf = (id) => `terminal:${id}`; + const region = card(3_000, 0, 1_000, 3_000); + const bounds = new Map([ + ["terminal:a", card(2_400, 100, 500, 300)], + ["terminal:b", card(2_400, 600, 500, 300)], + ["note:n", card(2_000, 1_300, 300, 200)], + ["terminal:kept", card(2_000, 1_700, 500, 300)] + ]); + const plan = planCanvasAutoSort(["terminal:a", "terminal:b"], bounds, [], layerIdOf, "grid", { avoid: [HOME, region], snapToGrid: true }); + const block = blockOf(plan.after); + for (const obstacle of [HOME, region, bounds.get("note:n"), bounds.get("terminal:kept")]) { + assert.equal(overlaps(grown(block, GAP), obstacle), false, `clear of ${JSON.stringify(obstacle)}`); + } + assert.ok(block.position.x >= region.position.x + region.size.width || block.position.y >= region.position.y + region.size.height, + "the block is past the region, not inside it"); + + // A region all around the windows' corner: the block still ends outside it. + const around = card(1_900, -100, 3_000, 3_000); + const inside = planCanvasAutoSort(["terminal:a", "terminal:b"], bounds, [], layerIdOf, "grid", { avoid: [around] }); + assert.equal(overlaps(blockOf(inside.after), around), false); +}); + +test("by task puts each subagent to the right of the session that spawned it and survives parent cycles", () => { + const items = [ + { id: "terminal:lead", bounds: card(0, 2_000) }, + { id: "terminal:sub-b", parentId: "terminal:lead", bounds: card(900, 2_900) }, + { id: "terminal:sub-a", parentId: "terminal:lead", bounds: card(-500, 2_100) }, + { id: "terminal:solo", bounds: card(300, 1_500) }, + { id: "terminal:orphan", parentId: "terminal:closed", bounds: card(4_000, 4_000) } + ]; + const result = arrangeCanvasAutoSort(items, "task", {}); + const lead = result.get("terminal:lead"); + for (const sub of ["terminal:sub-a", "terminal:sub-b"]) { + assert.ok(result.get(sub).position.x >= lead.position.x + lead.size.width, `${sub} is right of its parent`); + } + assert.ok(result.get("terminal:sub-a").position.y < result.get("terminal:sub-b").position.y, "siblings keep reading order"); + assert.equal(result.get("terminal:orphan").position.x, result.get("terminal:solo").position.x, "a missing parent makes a root"); + assert.ok(result.get("terminal:solo").position.y < lead.position.y, "roots keep reading order"); + + const cycle = arrangeCanvasAutoSort([ + { id: "x", parentId: "y", bounds: card(0, 0) }, + { id: "y", parentId: "x", bounds: card(0, 600) } + ], "task", {}); + assert.equal(cycle.size, 2); + assertDisjoint(cycle); +}); + +test("status columns run attention, working, idle, done, failed, unavailable; project columns merge trailing slashes", () => { + const order = ["unavailable", "failed", "done", "idle", "working", "needs_approval"]; + const items = order.map((status, index) => ({ id: status, status, bounds: card(index * 800, 3_000) })); + const result = arrangeCanvasAutoSort(items, "status", {}); + const columns = [...items].sort((a, b) => result.get(a.id).position.x - result.get(b.id).position.x).map((item) => item.id); + assert.deepEqual(columns, ["needs_approval", "working", "idle", "done", "failed", "unavailable"]); + + const projects = arrangeCanvasAutoSort([ + { id: "browser", bounds: card(0, 0) }, + { id: "b1", project: "/work/b/", bounds: card(0, 500) }, + { id: "a1", project: "/work/a", bounds: card(0, 1_000) }, + { id: "b2", project: "/work/b", bounds: card(0, 1_500) } + ], "project", {}); + const right = (id) => projects.get(id).position.x + projects.get(id).size.width; + for (const id of ["b1", "b2"]) { + assert.ok(projects.get(id).position.x > right("a1"), `${id} is in the /work/b group after /work/a`); + assert.ok(right(id) < projects.get("browser").position.x, "windows without a folder come last"); + } + assert.equal(projects.get("b1").position.y, projects.get("b2").position.y, "/work/b and /work/b/ form one group"); +}); + +test("a plan reads session relations and the bounds of that moment, and needs two live windows", () => { + const layerIdOf = (id) => `terminal:${id}`; + const sessions = [ + { id: "lead", status: "working", cwd: "/work", startedAt: 1 }, + { id: "sub", parentSessionId: "lead", status: "needs_approval", cwd: "/work", startedAt: 2 } + ]; + const bounds = new Map([["terminal:lead", card(3_000, 0)], ["terminal:sub", card(3_000, 900)], ["browser", card(3_000, 1_800)]]); + const plan = planCanvasAutoSort(["terminal:lead", "terminal:sub", "browser", "terminal:closed"], bounds, sessions, layerIdOf, "task", { avoid: [HOME] }); + assert.deepEqual([...plan.before.keys()], ["terminal:lead", "terminal:sub", "browser"]); + assert.deepEqual(plan.before.get("terminal:sub"), card(3_000, 900)); + assert.notEqual(plan.before.get("terminal:sub"), bounds.get("terminal:sub"), "undo keeps its own copy"); + assert.ok(plan.after.get("terminal:sub").position.x > plan.after.get("terminal:lead").position.x, "subagent right of its lead"); + assert.equal(planCanvasAutoSort(["terminal:lead", "terminal:closed"], bounds, sessions, layerIdOf, "grid", {}), null); +}); + +test("scope: a selection limits the sort; otherwise windows outside regions, never notes, materials or Files cards", () => { + const region = card(5_000, 0, 2_000, 2_000); + const candidates = [ + { id: "terminal:a", kind: "terminal", bounds: card(0, 0) }, + { id: "terminal:in-region", kind: "terminal", bounds: card(5_100, 100) }, + { id: "plugin:app", kind: "plugin", bounds: card(0, 800) }, + { id: "plugin:mascot", kind: "plugin", bounds: card(0, 1_600), decorative: true }, + { id: "browser", kind: "browser", bounds: card(900, 0) }, + { id: "note:n", kind: "note", bounds: card(0, 3_000) }, + { id: "material:m", kind: "material", bounds: card(0, 4_000) }, + { id: "files:f", kind: "files", bounds: card(0, 5_000) } + ]; + assert.deepEqual(canvasAutoSortScope(candidates, new Set(), [region]), ["terminal:a", "plugin:app", "browser"]); + assert.deepEqual(canvasAutoSortScope(candidates, new Set(["terminal:a"]), [region]), ["terminal:a", "plugin:app", "browser"], + "a single selected card is not a group"); + assert.deepEqual( + canvasAutoSortScope(candidates, new Set(["terminal:in-region", "plugin:mascot", "note:n", "files:f"]), [region]), + ["terminal:in-region", "plugin:mascot"], + "an explicit selection includes region contents and mascots but still never notes or Files cards" + ); + const fallback = ["terminal:a", "plugin:app", "browser"]; + assert.deepEqual(canvasAutoSortScope(candidates, new Set(["note:n", "files:f"]), [region]), fallback, + "a selection without two windows (two notes) falls back to the default scope"); + assert.deepEqual(canvasAutoSortScope(candidates, new Set(["terminal:in-region", "note:n", "material:m"]), [region]), fallback, + "one selected window plus annotations falls back too"); + assert.deepEqual(canvasAutoSortScope(candidates, new Set(["plugin:mascot"]), [region]), ["terminal:a", "plugin:app", "plugin:mascot", "browser"], + "a single selected mascot is not a group, but the fallback still takes it along"); + assert.deepEqual(canvasAutoSortScope(candidates, new Set(["plugin:mascot", "note:n", "files:f"]), [region]), + ["terminal:a", "plugin:app", "plugin:mascot", "browser"], "a selected mascot plus annotations falls back and keeps the mascot"); +}); + +test("undo restores only windows still where the sort put them", () => { + const before = new Map([["a", card(0, 0)], ["b", card(0, 600)], ["c", card(0, 1_200)], ["d", card(0, 1_800)]]); + const after = new Map([["a", card(2_000, 0)], ["b", card(2_800, 0)], ["c", card(3_600, 0)], ["d", card(0, 1_800)]]); + const current = new Map([ + ["a", card(2_000, 0)], + ["b", card(2_810, 0)], + ["d", card(0, 1_800)] + ]); + const restore = canvasAutoSortUndoTargets({ mode: "grid", before, after }, current); + assert.deepEqual([...restore.keys()], ["a"], "moved (b), closed (c) and unmoved (d) windows are left alone"); + assert.deepEqual(restore.get("a"), card(0, 0)); +}); + +const menuModule = await importWithFakeReact("src/renderer/src/features/workspace/CanvasContextMenu.tsx", "CanvasContextMenu"); +const toolsModule = await importWithFakeReact("src/renderer/src/features/workspace/CanvasAutoSortMenu.tsx", "CanvasAutoSortMenu"); +const brokenMenuModule = await importWithFakeReact("src/renderer/src/features/workspace/CanvasContextMenu.tsx", "CanvasContextMenu", { + plugins: [{ + name: "broken-tools-chunk", + setup(builder) { + builder.onResolve({ filter: /CanvasAutoSortMenu$/ }, () => ({ path: "broken", namespace: "broken" })); + builder.onLoad({ filter: /.*/, namespace: "broken" }, () => ({ contents: 'throw new Error("chunk failed");', loader: "js" })); + } + }] +}); + +const terminal = (id, x, y, extra = {}) => [`terminal:${id}`, card(x, y), { id, status: "idle", cwd: "/work", startedAt: 1, ...extra }]; + +/** A workspace host: the layers of the moment plus spies for what the Tools submenu asks of it. */ +function host(windows, other = {}) { + const calls = { commit: [], state: [], mark: [], error: [] }; + const value = { + bounds: new Map([...windows.map(([id, bounds]) => [id, bounds]), ...(other.layers ?? [])]), + selection: other.selection ?? new Set(), + home: HOME, + regions: other.regions ?? [], + sessions: windows.map(([, , session]) => session).filter(Boolean), + pluginCanvas: other.pluginCanvas ?? [], + plugins: other.plugins ?? [], + snapToGrid: other.snapToGrid ?? true, + state: other.state ?? { mode: null, undo: null }, + setState: (state) => calls.state.push(state), + commit: (next) => calls.commit.push(next), + mark: (ids) => calls.mark.push([...ids]), + onError: (message) => calls.error.push(message) + }; + return { value, calls }; +} + +function menuHarness(t, props = {}, module = menuModule) { + const oldWindow = globalThis.window; + globalThis.window = { canvasTTY: { window: { isMacOS: false } }, getComputedStyle: () => ({ fontSize: "13px" }) }; + module.__reset(); + const oldError = console.error; + console.error = () => undefined; + t.after(() => { module.__unmount(); globalThis.window = oldWindow; console.error = oldError; }); + let closed = 0; + const noop = () => undefined; + const all = new Proxy({ + kind: "empty", position: { x: 0, y: 0 }, locale: "en", launcherItems: [], currentRegionColor: null, + materialHasLocation: false, onSendMaterial: null, onSendRemarks: null, + autoSort: host([]).value, onClose: () => { closed += 1; }, + ...props + }, { get: (target, key) => (key in target ? target[key] : noop) }); + let tree; + const render = () => { module.__flush(); tree = module.__render(module.CanvasContextMenu, all); return tree; }; + const row = (label) => findAll(tree, (node) => node.props?.role === "menuitem" && node.props.children === label)[0]; + const tools = () => findAll(tree, (node) => node.type?.name === "CanvasAutoSortMenu")[0]; + const openTools = async () => { + row(props.locale === "ru" ? "Инструменты" : "Tools").props.onClick({ currentTarget: { getBoundingClientRect: () => ({ top: 0 }) } }); + for (let i = 0; i < 3; i += 1) { render(); await tick(); } + render(); + }; + render(); + return { render, row, tools, openTools, closed: () => closed }; +} + +function toolsMenu(hostValue, locale = "en") { + let done = 0; + const tree = toolsModule.CanvasAutoSortMenu({ host: hostValue, locale, className: "canvas-menu", onDone: () => { done += 1; } }); + const rows = findAll(tree, (node) => typeof node.props?.role === "string" && node.props.role.startsWith("menuitem")); + return { + tree, + rows, + radios: rows.filter((node) => node.props.role === "menuitemradio"), + row: (label) => rows.find((node) => node.props.children === label), + text: JSON.stringify(tree), + done: () => done + }; +} + +test("the empty-canvas menu has one Tools row that loads its submenu on demand", async (t) => { + const sortHost = host([]).value; + const ui = menuHarness(t, { autoSort: sortHost }); + assert.ok(ui.row("Tools"), "Tools is in the empty-canvas menu"); + assert.equal(ui.row("Tools").props["aria-expanded"], false); + assert.equal(ui.tools(), undefined, "the submenu is not rendered until Tools opens"); + await ui.openTools(); + assert.equal(ui.row("Tools").props["aria-expanded"], true); + const tools = ui.tools(); + assert.ok(tools, "the loaded submenu is shown"); + assert.equal(tools.props.host, sortHost); + assert.match(tools.props.className, /canvas-menu__submenu canvas-menu__submenu--right/); + tools.props.onDone(); + assert.equal(ui.closed(), 1, "running an action closes the menu"); + + for (const kind of ["region", "note", "material"]) { + assert.equal(menuHarness(t, { kind }).row("Tools"), undefined, `no Tools row in the ${kind} menu`); + } +}); + +test("the Tools submenu body loads while the empty-canvas menu is open, so Tools shows it at once", async (t) => { + // A fresh copy of the menu module: nothing loaded yet. + const fresh = await importWithFakeReact("src/renderer/src/features/workspace/CanvasContextMenu.tsx", "CanvasContextMenu"); + const ui = menuHarness(t, {}, fresh); + for (let i = 0; i < 3; i += 1) { ui.render(); await tick(); } + ui.row("Tools").props.onClick({ currentTarget: { getBoundingClientRect: () => ({ top: 0 }) } }); + ui.render(); + assert.ok(ui.tools(), "the first render after Tools already shows the submenu"); +}); + +test("a menu that first rendered before the Tools body loaded still shows the submenu once it has", async (t) => { + // A fresh copy of the menu module with nothing loaded yet: a distinct bundle, since identical ones share one module. + const fresh = await importWithFakeReact("src/renderer/src/features/workspace/CanvasContextMenu.tsx", + "CanvasContextMenu, CanvasContextMenu as LateLoadCopy"); + const oldWindow = globalThis.window; + globalThis.window = { canvasTTY: { window: { isMacOS: false } }, getComputedStyle: () => ({ fontSize: "13px" }) }; + t.after(() => { fresh.__unmount(); globalThis.window = oldWindow; }); + const noop = () => undefined; + const props = (kind) => new Proxy({ + kind, position: { x: 0, y: 0 }, locale: "en", launcherItems: [], currentRegionColor: null, + materialHasLocation: false, onSendMaterial: null, onSendRemarks: null, autoSort: host([]).value + }, { get: (target, key) => (key in target ? target[key] : noop) }); + let tree; + const render = (kind) => { fresh.__flush(); tree = fresh.__render(fresh.CanvasContextMenu, props(kind)); }; + const row = (label) => findAll(tree, (node) => node.props?.role === "menuitem" && node.props.children === label)[0]; + const tools = () => findAll(tree, (node) => node.type?.name === "CanvasAutoSortMenu")[0]; + + // An empty-canvas menu starts the load and is closed before it settles. + fresh.__reset(); + render("empty"); + fresh.__unmount(); + // The next menu is first rendered (as a region menu) while the load is still pending, so it starts with no body. + render("region"); + for (let i = 0; i < 3; i += 1) await tick(); + // It becomes the empty-canvas menu after the body has loaded: nothing re-renders it for the load itself. + render("empty"); + row("Tools").props.onClick({ currentTarget: { getBoundingClientRect: () => ({ top: 0 }) } }); + render("empty"); + assert.equal(row("Tools").props["aria-expanded"], true); + assert.ok(tools(), "Tools shows the already-loaded submenu"); +}); + +test("opening Tools closes the launcher submenu and the other way round", async (t) => { + const ui = menuHarness(t); + await ui.openTools(); + ui.row("Launch agent").props.onClick(); + ui.render(); + assert.equal(ui.row("Tools").props["aria-expanded"], false); + assert.equal(ui.row("Launch agent").props["aria-expanded"], true); + await ui.openTools(); + assert.equal(ui.row("Launch agent").props["aria-expanded"], false); +}); + +test("if the Tools submenu cannot be loaded the person gets a localized toast and the row closes", async (t) => { + const { value, calls } = host([]); + const ui = menuHarness(t, { autoSort: value, locale: "ru" }, brokenMenuModule); + await ui.openTools(); + assert.deepEqual(calls.error, ["Автосортировка не удалась"]); + assert.equal(ui.row("Инструменты").props["aria-expanded"], false); + assert.equal(ui.tools(), undefined); +}); + +test("the Tools submenu lists the criteria as radio rows and sorts, outlines and remembers the criterion", () => { + const { value, calls } = host([terminal("a", 3_000, 0), terminal("b", 3_000, 900), terminal("c", 5_000, 400)]); + const menu = toolsMenu(value); + assert.deepEqual(menu.radios.map((node) => node.props.children), ["By task", "By status", "By project", "Grid"]); + assert.ok(menu.radios.every((node) => node.props["aria-checked"] === false && !node.props.disabled)); + assert.equal(menu.row("Undo auto-sort"), undefined, "no undo before a sort"); + assert.doesNotMatch(menu.text, /needs at least 2 windows/); + + menu.row("Grid").props.onClick(); + assert.equal(menu.done(), 1); + assert.equal(calls.commit.length, 1); + const moved = calls.commit[0]; + assert.deepEqual([...moved.keys()].sort(), ["terminal:a", "terminal:b", "terminal:c"]); + assert.deepEqual(calls.mark.map((ids) => ids.sort()), [["terminal:a", "terminal:b", "terminal:c"]], "the moved windows are outlined"); + assert.equal(calls.state.length, 1); + assert.equal(calls.state[0].mode, "grid"); + assert.deepEqual(calls.state[0].undo.after, moved); + + // The next menu checks the criterion and offers undo while a window is still where the sort put it. + const after = host([...moved].map(([id, bounds]) => [id, bounds]), { state: calls.state[0] }); + const next = toolsMenu(after.value); + assert.deepEqual(next.radios.filter((node) => node.props["aria-checked"] === true).map((node) => node.props.children), ["Grid"]); + next.row("Undo auto-sort").props.onClick(); + // terminal:a already sat at the block's corner, so only the windows that actually moved go back. + assert.deepEqual(moved.get("terminal:a"), card(3_000, 0)); + assert.deepEqual(after.calls.commit[0], new Map([["terminal:b", card(3_000, 900)], ["terminal:c", card(5_000, 400)]])); + assert.deepEqual(after.calls.mark.map((ids) => ids.sort()), [["terminal:b", "terminal:c"]]); + assert.deepEqual(after.calls.state, [{ mode: "grid", undo: null }]); +}); + +test("sorting again by a criterion that moves nothing keeps the earlier undo step", () => { + const { value, calls } = host([terminal("a", 3_000, 0), terminal("b", 3_000, 900), terminal("c", 5_000, 400)]); + toolsMenu(value).row("Grid").props.onClick(); + const first = calls.state[0]; + const moved = calls.commit[0]; + + const again = host([...moved].map(([id, bounds]) => [id, bounds]), { state: first }); + toolsMenu(again.value).row("Grid").props.onClick(); + assert.deepEqual(again.calls.commit, [], "nothing moves, so nothing is committed"); + assert.deepEqual(again.calls.mark, [], "nothing moves, so nothing is outlined"); + assert.deepEqual(again.calls.state, [], "the first sort's criterion and undo step stay as they were"); + const next = toolsMenu(again.value); + assert.ok(next.row("Undo auto-sort"), "Undo auto-sort is still offered"); + next.row("Undo auto-sort").props.onClick(); + assert.deepEqual(again.calls.commit[0], new Map([["terminal:b", card(3_000, 900)], ["terminal:c", card(5_000, 400)]]), + "undo still puts back the first sort's windows"); + + // A criterion the layout already follows becomes the checked one without losing the undo step. + const other = host([...moved].map(([id, bounds]) => [id, bounds]), { state: { mode: "task", undo: first.undo } }); + toolsMenu(other.value).row("Grid").props.onClick(); + assert.deepEqual(other.calls.commit, []); + assert.deepEqual(other.calls.state, [{ mode: "grid", undo: first.undo }]); +}); + +test("a selection without two windows does not narrow the sort, and too few windows disable it with a hint", () => { + const windows = [terminal("a", 3_000, 0), terminal("b", 3_000, 900)]; + const notes = [["note:1", card(9_000, 0)], ["note:2", card(9_000, 900)]]; + const selectedNotes = host(windows, { layers: notes, selection: new Set(["note:1", "note:2"]) }); + const menu = toolsMenu(selectedNotes.value); + assert.ok(menu.radios.every((node) => !node.props.disabled), "two selected notes fall back to the default scope"); + menu.row("By task").props.onClick(); + assert.deepEqual([...selectedNotes.calls.commit[0].keys()].sort(), ["terminal:a", "terminal:b"]); + + const mascot = host([terminal("a", 3_000, 0)], { + layers: [["plugin:buddy", card(9_000, 2_000, 200, 200)]], + selection: new Set(["plugin:buddy"]), + pluginCanvas: [{ id: "buddy", pluginId: "pet" }], + plugins: [{ manifest: { id: "pet" }, sourceUrl: "mascot:pet" }] + }); + const withMascot = toolsMenu(mascot.value); + assert.ok(withMascot.radios.every((node) => !node.props.disabled), "a selected mascot counts with the one regular window"); + withMascot.row("Grid").props.onClick(); + assert.deepEqual([...mascot.calls.commit[0].keys()].sort(), ["plugin:buddy", "terminal:a"]); + + const lonely = host([terminal("a", 3_000, 0)], { layers: notes }); + const ru = toolsMenu(lonely.value, "ru"); + assert.ok(ru.radios.every((node) => node.props.disabled === true)); + assert.match(ru.text, /нужно минимум 2 окна/); + ru.radios[0].props.onClick(); + assert.deepEqual(lonely.calls.state, [], "the criterion is kept only once a sort happened"); + assert.deepEqual(lonely.calls.commit, []); +}); + +test("a sort that throws reports a localized toast instead of leaving the menu half done", () => { + const { value, calls } = host([terminal("a", 3_000, 0), terminal("b", 3_000, 900)]); + value.commit = () => { throw new Error("boom"); }; + const oldError = console.error; + console.error = () => undefined; + try { + const menu = toolsMenu(value); + menu.row("Grid").props.onClick(); + assert.deepEqual(calls.error, ["Auto-sort failed"]); + assert.equal(menu.done(), 1); + assert.deepEqual(calls.state, [], "no criterion or undo step is kept for a failed sort"); + } finally { + console.error = oldError; + } +}); + +// Wiring that only the whole workspace shows; kept to the few facts a behaviour test cannot reach. +test("the workspace keeps auto-sort out of the startup bundle, outlines sorted windows without selecting them, and adds no undo shortcut", async () => { + const [canvas, menu] = await Promise.all([ + source("src/renderer/src/features/workspace/WorkspaceCanvas.tsx"), + source("src/renderer/src/features/workspace/CanvasContextMenu.tsx") + ]); + for (const code of [canvas, menu]) { + assert.doesNotMatch(code, /^import (?!type)[^;]*from "\.\/(canvasAutoSort|CanvasAutoSortMenu|canvasAutoSortScope)";/m); + } + assert.doesNotMatch(canvas, /setMarqueeSelection\(new Set\((plan|restore)/, "a sort never turns its windows into a drag group"); + assert.match(canvas, /groupSelected=\{marqueeSelection\.has\(terminalLayerId\(session\.id\)\) \|\| autoSortMarked\.has/); + assert.match(canvas, /element\?\.addEventListener\("pointerdown", clear, true\)/, "the next press clears the outline"); + assert.doesNotMatch(canvas, /key === "z"|KeyZ|"Mod\+Z"|ctrl\+z/i, "no global undo shortcut"); +}); + +test("auto-sort strings exist in every locale and new code uses tokens, vendored icons and no literal glyphs", async () => { + const [i18n, menu, tools, sorter, scope, styles, icons, readme, wrench, undo] = await Promise.all([ + source("src/renderer/src/lib/i18n.ts"), + source("src/renderer/src/features/workspace/CanvasContextMenu.tsx"), + source("src/renderer/src/features/workspace/CanvasAutoSortMenu.tsx"), + source("src/renderer/src/features/workspace/canvasAutoSort.ts"), + source("src/renderer/src/features/workspace/canvasAutoSortScope.ts"), + source("src/renderer/src/styles/app.css"), + source("src/renderer/src/components/UiIcon.tsx"), + source("src/renderer/src/assets/icons/lucide/README.md"), + source("src/renderer/src/assets/icons/lucide/wrench.svg"), + source("src/renderer/src/assets/icons/lucide/undo-2.svg") + ]); + const enStart = i18n.indexOf("const en: Record = {"); + const ru = i18n.slice(0, enStart); + const en = i18n.slice(enStart); + const keys = ["canvasMenuTools", "canvasAutoSort", "canvasAutoSortTask", "canvasAutoSortStatus", "canvasAutoSortProject", + "canvasAutoSortGrid", "canvasAutoSortUnavailable", "canvasAutoSortUndo", "canvasAutoSortFailed"]; + for (const key of keys) { + const ruValue = ru.match(new RegExp(`\\n ${key}: "([^"]+)"`))?.[1]; + const enValue = en.match(new RegExp(`\\n ${key}: "([^"]+)"`))?.[1]; + assert.ok(ruValue && enValue, `${key} is translated in ru and en`); + assert.match(ruValue, /[а-яё]/i, `${key} has a Russian value`); + assert.doesNotMatch(enValue, /[а-яё]/i, `${key} has an English value`); + } + + const toolsBlock = menu.slice(menu.indexOf('icon="wrench"'), menu.indexOf('{kind === "region" && (')); + for (const code of [toolsBlock, tools, sorter, scope]) { + assert.doesNotMatch(code, /#[\da-f]{3,8}\b|rgba?\(/i); + assert.doesNotMatch(code, /[›▸▶→↶↺×✓✔]/u, "icons come from UiIcon, not literal glyphs"); + } + const sortStyles = styles.split("\n").filter((line) => line.includes("auto-sorting") || line.includes("canvas-menu__check") + || line.includes("canvas-menu__row:disabled")); + assert.ok(sortStyles.length >= 4); + for (const line of sortStyles) assert.doesNotMatch(line, /#[\da-f]{3,8}\b|rgba?\(|\d+px/i, line); + assert.match(styles, /@media \(prefers-reduced-motion: reduce\) \{ \.workspace--auto-sorting [^}]*transition: none; \}/); + assert.doesNotMatch(styles, /\.workspace--auto-sorting :is\(\.terminal-card, \.plugin-canvas-card\) \{[^}]*\.browser-card/, "the native Browser view never glides"); + // Zoomed out, the outline of the moved windows keeps its on-screen thickness: it grows with the summary scale. + assert.match(styles, /\.workspace--auto-sorting :is\(\.terminal-card--summary\.terminal-card--selected, \.plugin-canvas-card--summary\.plugin-canvas-card--selected, \.browser-card--summary\.browser-card--selected\) \{ box-shadow: 0 0 0 calc\([^)]*var\(--summary-scale, 1\)\) var\(--primary\)/); + // "Needs at least 2 windows" is a plain muted hint, not the uppercase group label. + assert.match(tools, /\{!available &&

\{t\(locale, "canvasAutoSortUnavailable"\)\}<\/p>\}/); + const hint = styles.match(/\.canvas-menu__hint \{[^}]*\}/)[0]; + const disabledRow = styles.match(/\.canvas-menu__row:disabled \{[^}]*\}/)[0]; + assert.match(hint, /padding: [^;]*calc\(1\.55em \/ \.92\)/, "the hint's text lines up with the indented rows despite its smaller font"); + assert.match(hint, /color: var\(--text\); opacity: \.5;/, "as quiet as the disabled rows it explains"); + assert.match(disabledRow, /opacity: \.5/); + assert.doesNotMatch(styles.match(/\.canvas-menu__hint \{[^}]*\}/)[0], /uppercase|font-mono/); + + assert.match(icons, /import undoIcon from "\.\.\/assets\/icons\/lucide\/undo-2\.svg";/); + assert.match(icons, /import wrenchIcon from "\.\.\/assets\/icons\/lucide\/wrench\.svg";/); + assert.match(readme, /`wrench`, `undo-2`/); + for (const svg of [wrench, undo]) { + assert.match(svg, /^ { + const [en, ru, zh] = await Promise.all([ + source("docs/UI_CONTRACT.md"), + source("docs/UI_CONTRACT.ru.md"), + source("docs/UI_CONTRACT.zh-CN.md") + ]); + assert.match(en, /a \*\*Tools\*\* submenu[\s\S]*\*\*Auto-sort\*\*[\s\S]*\*\*Undo auto-sort\*\*/); + assert.match(ru, /\*\*Инструменты\*\*[\s\S]*\*\*Автосортировка\*\*[\s\S]*\*\*Отменить автосортировку\*\*/); + assert.match(zh, /\*\*Tools\*\*[\s\S]*\*\*Auto-sort\*\*[\s\S]*\*\*Undo auto-sort\*\*/); +}); diff --git a/tests/canvas-context-menu.test.mjs b/tests/canvas-context-menu.test.mjs index f076506a5..7a5207168 100644 --- a/tests/canvas-context-menu.test.mjs +++ b/tests/canvas-context-menu.test.mjs @@ -2,10 +2,15 @@ import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import test from "node:test"; import { - chooseCanvasSubmenuSide, clampCanvasMenuPosition, routeCanvasContextMenu } from "../src/renderer/src/features/workspace/canvasContextRouting.ts"; +import { + canvasMenuPlacementClearOfPanels, + canvasMenuTopClearOfPanels, + chooseCanvasSubmenuPlacement, + chooseCanvasSubmenuSide +} from "../src/renderer/src/features/workspace/canvasMenuPlacement.ts"; const contextMenuComponentPath = new URL( "../src/renderer/src/features/workspace/CanvasContextMenu.tsx", @@ -29,7 +34,7 @@ test("launcher submenu chooses its side before it is mounted", async () => { assert.equal(chooseCanvasSubmenuSide({ left: 740, right: 990 }, viewport, 247, 8), "left"); assert.equal(chooseCanvasSubmenuSide({ left: 480, right: 650 }, { left: 300, right: 720 }, 247, 8), "left"); - const component = await readFile(contextMenuComponentPath, "utf8"); + const component = (await readFile(contextMenuComponentPath, "utf8")).replace(/\r\n/g, "\n"); const sideChoice = component.indexOf("setSubmenuSide(chooseCanvasSubmenuSide("); const firstRender = component.indexOf("setLauncherOpen(true)", sideChoice); assert.ok(sideChoice >= 0 && firstRender > sideChoice); @@ -46,3 +51,80 @@ test("context menus stay inside the canvas viewport", () => { { x: 12, y: 12 } ); }); + +test("a context menu opened near a corner panel moves up off it, and its submenu chevrons point where they open", async () => { + // Home/zoom controls in the bottom-left corner; the menu was clamped to the bottom of a 856 px canvas. + const controls = { left: 18, top: 838, right: 170, bottom: 882 }; + const hints = { left: 1100, top: 615, right: 1420, bottom: 882 }; + const menu = { left: 60, top: 531, right: 272, bottom: 856 }; + assert.equal(canvasMenuTopClearOfPanels(menu, [controls, hints], 12, 8), 838 - 8 - 325); + assert.equal(canvasMenuTopClearOfPanels({ ...menu, left: 300, right: 512 }, [controls, hints], 12, 8), 531, "clear of every panel: unchanged"); + assert.equal(canvasMenuTopClearOfPanels(menu, [controls], 600, 8), 531, "no room above: it keeps its place (drawn above the panel)"); + // Up off one panel and then above a second one where it fits; when it cannot clear both it keeps its place. + assert.equal(canvasMenuTopClearOfPanels(menu, [controls, { left: 18, top: 480, right: 170, bottom: 520 }], 12, 8), 480 - 8 - 325); + assert.equal(canvasMenuTopClearOfPanels(menu, [controls, { left: 18, top: 300, right: 170, bottom: 520 }], 12, 8), 531); + + const component = (await readFile(contextMenuComponentPath, "utf8")).replace(/\r\n/g, "\n"); + assert.match(component, /useLayoutEffect\(\(\) => \{[\s\S]*canvasMenuPlacementClearOfPanels\([\s\S]*\}, \[kind, position\.x, position\.y\]\);/); + assert.match(component, /useLayoutEffect\(\(\) => \{\n chooseSubmenuSide\(\);\n \}, \[kind, place\.left, place\.top\]\);/); + assert.match(component, /style=\{\{ left: place\.left, top: place\.top \}\}/); + assert.equal(component.match(/right=\{\}/g)?.length, 2, "Launch agent and Tools"); + const styles = await readFile(new URL("../src/renderer/src/styles/app.css", import.meta.url), "utf8"); + assert.match(styles, /\.canvas-menu__chevron--right \{ transform: rotate\(-90deg\); \}/); + assert.match(styles, /\.canvas-menu__chevron--left \{ transform: rotate\(90deg\); \}/); + // Menus (and their submenus, inside them) are drawn above the passive corner panels. + const z = (selector) => Number(styles.match(new RegExp(`\\n${selector.replace(".", "\\.")} \\{[^}]*z-index: (\\d+)`))?.[1]); + assert.ok(z(".canvas-menu") > z(".canvas-overlays")); +}); + +test("at a large UI scale a menu that cannot move up off the corner panels moves sideways instead", () => { + // 1.25: minimap top right, shortcut hints bottom right; the menu is too tall to fit between them. + const minimap = { left: 1232, top: 62, right: 1422, bottom: 184 }; + const hints = { left: 1100, top: 615, right: 1420, bottom: 882 }; + const menu = { left: 1055, top: 365, right: 1365, bottom: 845 }; + const bounds = { minTop: 12, minLeft: 12, maxRight: 1428 }; + assert.equal(canvasMenuTopClearOfPanels(menu, [minimap, hints], 12, 8), 365, "up is not possible"); + assert.deepEqual(canvasMenuPlacementClearOfPanels(menu, [minimap, hints], bounds, 8), { left: 1100 - 8 - 310, top: 365 }); + // Up still comes first when it works, and a menu clear of every panel stays put. + const low = { left: 60, top: 531, right: 272, bottom: 856 }; + const controls = { left: 18, top: 838, right: 170, bottom: 882 }; + assert.deepEqual(canvasMenuPlacementClearOfPanels(low, [controls, hints], bounds, 8), { left: 60, top: 838 - 8 - 325 }); + assert.deepEqual(canvasMenuPlacementClearOfPanels({ ...low, left: 300, right: 512 }, [controls, hints], bounds, 8), { left: 300, top: 531 }); + // No room on either side: it keeps its place. + assert.deepEqual(canvasMenuPlacementClearOfPanels(menu, [minimap, { ...hints, left: 20 }], bounds, 8), { left: 1055, top: 365 }); +}); + +test("a submenu opens where it covers no corner panel, wherever Settings put the panels", async () => { + // UI scale 1.25 (16.25 px font) in a 1440 x 900 window: the menu was opened near the right edge, its Tools row is + // low, and the shortcut hints sit in the bottom-right corner, the minimap top right, Home/zoom bottom left. + const area = { left: 0, top: 44, right: 1440, bottom: 900 }; + const hints = { left: 1035, top: 615, right: 1422, bottom: 882 }; + const minimap = { left: 1232, top: 62, right: 1422, bottom: 184 }; + const controls = { left: 18, top: 840, right: 168, bottom: 882 }; + const tools = { left: 722, top: 722, right: 1014, bottom: 770 }; + const size = { width: 309, height: 263 }; + const metrics = { gap: 9, offset: 7, clearance: 10 }; + // Room on the right, so the side rule alone opened it there, upward, over the hints panel. + assert.equal(chooseCanvasSubmenuSide(tools, area, size.width, metrics.gap), "right"); + assert.deepEqual(chooseCanvasSubmenuPlacement(tools, size, area, [hints, minimap, controls], metrics, true), + { side: "left", up: true }); + // Without panels in the way it keeps the side with room and goes up only when down runs off the canvas. + assert.deepEqual(chooseCanvasSubmenuPlacement(tools, size, area, [], metrics, true), { side: "right", up: true }); + assert.deepEqual(chooseCanvasSubmenuPlacement({ ...tools, top: 300, bottom: 348 }, size, area, [hints], metrics, true), + { side: "right", up: false }); + // Hints moved to the bottom-left corner: the right side is clear again. + const hintsLeft = { left: 18, top: 615, right: 405, bottom: 882 }; + assert.deepEqual(chooseCanvasSubmenuPlacement(tools, size, area, [hintsLeft, minimap], metrics, true), + { side: "right", up: true }); + // Up blocked by the panel but down fits: it opens down on the same side. + const tall = { left: 1035, top: 120, right: 1422, bottom: 420 }; + assert.deepEqual(chooseCanvasSubmenuPlacement({ ...tools, top: 380, bottom: 428 }, size, area, [tall], metrics, true), + { side: "left", up: false }); + // The launcher submenu never opens up; with no clear place at all the preferred place stays. + assert.deepEqual(chooseCanvasSubmenuPlacement(tools, size, area, [hints], metrics, false), { side: "right", up: false }); + const everywhere = { left: 0, top: 44, right: 1440, bottom: 900 }; + assert.deepEqual(chooseCanvasSubmenuPlacement(tools, size, area, [everywhere], metrics, true), { side: "right", up: true }); + + const component = (await readFile(contextMenuComponentPath, "utf8")).replace(/\r\n/g, "\n"); + assert.match(component, /chooseCanvasSubmenuPlacement\(anchor\.getBoundingClientRect\(\),[\s\S]*cornerPanelRects\(workspace\)[\s\S]*toolsOpen\);\n setSubmenuSide\(placement\.side\);\n if \(toolsOpen\) setToolsUp\(placement\.up\);\n \}, \[launcherOpen, toolsOpen, ToolsMenu, place\.left, place\.top\]\);/); +}); diff --git a/tests/canvas-preloaded-surfaces.test.mjs b/tests/canvas-preloaded-surfaces.test.mjs new file mode 100644 index 000000000..618ea6207 --- /dev/null +++ b/tests/canvas-preloaded-surfaces.test.mjs @@ -0,0 +1,143 @@ +import assert from "node:assert/strict"; +import { mkdtemp, readFile, rm } from "node:fs/promises"; +import { createRequire } from "node:module"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test from "node:test"; +import { fileURLToPath } from "node:url"; +import { build } from "esbuild"; +import { importWithFakeReact } from "./helpers/fake-react.mjs"; + +// The context menu and the command palette are kept out of the startup bundle (renderer-bundle-splitting), but a +// lazy() behind made their first open wait about 300 ms (React holds a suspended boundary's reveal that +// long). They are preloaded once the canvas is idle and rendered directly: once loaded, the very first render of an +// open menu or palette already contains it. Rendered here with the real React and react-dom/server, through the real +// dynamic imports of canvasOverlaySurfaces. +const root = fileURLToPath(new URL("..", import.meta.url)); + +async function loadBundle(t) { + const outdir = await mkdtemp(join(tmpdir(), "canvastty-surfaces-")); + t.after(() => rm(outdir, { recursive: true, force: true })); + await build({ + stdin: { + contents: [ + 'export { canvasContextMenuSurface, canvasCommandPaletteSurface } from "./src/renderer/src/features/workspace/canvasOverlaySurfaces";', + 'export { preloadedSurface, preloadWhenIdle, usePreloadedSurface } from "./src/renderer/src/lib/preloadedSurface";', + 'export { createElement } from "react";', + 'export { renderToStaticMarkup } from "react-dom/server";' + ].join("\n"), + resolveDir: root, + loader: "ts" + }, + jsx: "automatic", + bundle: true, + platform: "node", + format: "cjs", + outfile: join(outdir, "surfaces.cjs"), + logLevel: "silent", + define: { "process.env.NODE_ENV": '"production"' }, + loader: { ".svg": "text", ".css": "empty", ".png": "empty", ".ttf": "empty", ".woff2": "empty", ".ico": "empty" } + }); + return createRequire(import.meta.url)(join(outdir, "surfaces.cjs")); +} + +const noop = () => undefined; +const menuProps = { + kind: "empty", position: { x: 40, y: 40 }, locale: "en", launcherItems: [], currentRegionColor: null, + materialHasLocation: false, onSendMaterial: null, onSendRemarks: null, + ...Object.fromEntries([ + "onCreateRegion", "onCreateNote", "onLaunch", "onOpenBrowser", "onOpenFiles", "onOpenSettings", "onRenameRegion", + "onChangeRegionColor", "onDeleteRegion", "onEditNote", "onBringNoteToFront", "onDeleteNote", "onAddFiles", + "onPasteFiles", "onPinMaterial", "onRevealMaterial", "onCopyMaterialPath", "onBringMaterialToFront", + "onRemoveMaterial", "onClose" + ].map((name) => [name, noop])) +}; +const paletteProps = { + locale: "en", sessions: [], launcherItems: [], + ...Object.fromEntries([ + "onFocusSession", "onLaunch", "onCreateRegion", "onCreateNote", "onAddFiles", "onPasteFiles", "onFitCanvas", + "onOpenBrowser", "onOpenFiles", "onOpenSettings", "onClose" + ].map((name) => [name, noop])) +}; + +test("a preloaded context menu and command palette render on their first open, with no Suspense wait", async (t) => { + const previousWindow = globalThis.window; + globalThis.window = { canvasTTY: { window: { isMacOS: false } } }; + t.after(() => { globalThis.window = previousWindow; }); + const bundle = await loadBundle(t); + const { createElement, renderToStaticMarkup, usePreloadedSurface, canvasContextMenuSurface, canvasCommandPaletteSurface } = bundle; + // The canvas's own pattern: `{open && Surface && }`. + const Host = ({ surface, open, props }) => { + const Surface = usePreloadedSurface(surface, open); + return open && Surface ? createElement(Surface, props) : null; + }; + const render = (surface, open, props) => renderToStaticMarkup(createElement(Host, { surface, open, props })); + + assert.equal(canvasContextMenuSurface.loaded(), null, "nothing is loaded at startup"); + assert.equal(render(canvasContextMenuSurface, true, menuProps), "", "an open before the preload renders nothing until its chunk arrives"); + + const [menuLoad, paletteLoad] = [canvasContextMenuSurface.load(), canvasCommandPaletteSurface.load()]; + assert.equal(canvasContextMenuSurface.load(), menuLoad, "one load per surface"); + await Promise.all([menuLoad, paletteLoad]); + + const menu = render(canvasContextMenuSurface, true, menuProps); + assert.match(menu, /^]*class="canvas-menu"/u, "the first render of the open menu contains the menu"); + assert.match(menu, /role="menuitem"/u); + const palette = render(canvasCommandPaletteSurface, true, paletteProps); + assert.match(palette, /canvas-command-palette/u, "the first render of the open palette contains the palette"); + assert.equal(render(canvasContextMenuSurface, false, menuProps), "", "a closed menu renders nothing"); +}); + +test("preloadWhenIdle loads after the first paint when the app is idle; a failed load is tried again", async (t) => { + const bundle = await loadBundle(t); + const { preloadedSurface, preloadWhenIdle } = bundle; + let calls = 0, fail = true; + const surface = preloadedSurface(async () => { + calls++; + if (fail) throw new Error("chunk failed"); + return "loaded"; + }); + const idle = []; + const previousWindow = globalThis.window; + globalThis.window = { requestIdleCallback: (callback, options) => { idle.push({ callback, options }); return idle.length; }, cancelIdleCallback: noop }; + t.after(() => { globalThis.window = previousWindow; }); + preloadWhenIdle([surface]); + assert.equal(calls, 0, "nothing loads before the browser is idle"); + assert.equal(idle.length, 1); + assert.ok(idle[0].options.timeout > 0, "bounded: it still loads on a busy start"); + idle[0].callback(); + await new Promise((resolve) => setImmediate(resolve)); + assert.equal(calls, 1); + assert.equal(surface.loaded(), null, "a failed preload leaves nothing loaded"); + fail = false; + assert.equal(await surface.load(), "loaded", "the next open loads it again"); + assert.equal(calls, 2); + assert.equal(surface.loaded(), "loaded"); +}); + +test("the canvas renders the menu and the palette through the preloaded surfaces, not lazy() and Suspense", async () => { + const source = (await readFile(join(root, "src/renderer/src/features/workspace/WorkspaceCanvas.tsx"), "utf8")).replace(/\r\n/g, "\n"); + assert.doesNotMatch(source, /lazy\(\(\) => import\("\.\/Canvas(ContextMenu|CommandPalette)"\)/u); + assert.doesNotMatch(source, /import \{ Canvas(ContextMenu|CommandPalette) \} from/u, "no static import either"); + assert.match(source, /usePreloadedSurface\(canvasContextMenuSurface, contextMenu !== null\)/u); + assert.match(source, /usePreloadedSurface\(canvasCommandPaletteSurface, commandPaletteOpen\)/u); + assert.match(source, /preloadWhenIdle\(\[canvasContextMenuSurface, canvasCommandPaletteSurface\]\)/u); + assert.match(source, /\{contextMenu && CanvasContextMenu && \(\n\s* { + const hook = await importWithFakeReact("src/renderer/src/lib/preloadedSurface.ts", "usePreloadedSurface"); + hook.__reset(); + t.after(() => hook.__unmount()); + // The idle preload settles after the render read the surface and before the effect ran. + let reads = 0, loads = 0; + const surface = { loaded: () => (reads++ === 0 ? null : "menu"), load: async () => { loads++; return "menu"; } }; + const Host = () => hook.usePreloadedSurface(surface, true); + assert.equal(hook.__render(Host), null, "the first render had nothing to show"); + assert.equal(hook.__pending(), 1, "the effect asks for another render"); + assert.equal(loads, 0, "and does not load it again"); + hook.__flush(); + assert.equal(hook.__render(Host), "menu", "which shows the surface"); + assert.equal(hook.__pending(), 0, "once shown, nothing more is asked for"); +}); diff --git a/tests/canvaslink-install.test.mjs b/tests/canvaslink-install.test.mjs new file mode 100644 index 000000000..39f25312d --- /dev/null +++ b/tests/canvaslink-install.test.mjs @@ -0,0 +1,58 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { importWithFakeReact, findAll, tick } from "./helpers/fake-react.mjs"; +import { CANVASLINK_ANDROID_RELEASE, canvasLinkInstallPrompt } from "../src/shared/canvasLinkInstall.ts"; + +const ui = await importWithFakeReact("src/renderer/src/features/settings/CanvasLinkSettings.tsx", "CanvasLinkSettings"); +const content = (tree) => findAll(tree, (node) => typeof node.props?.children === "string") + .map((node) => node.props.children).join("\n"); +const button = (tree, label) => findAll(tree, (node) => node.type === "button" && node.props.children === label)[0]; + +for (const locale of ["ru", "en"]) test(`Android onboarding copies a pinned prompt without session data or host changes (${locale})`, async () => { + const previousWindow = globalThis.window; + const copied = [], opened = [], commands = []; + const pairingCode = "987654"; + globalThis.window = { canvasTTY: { + canvasLink: { + state: async () => ({ enabled: true, phase: "streaming", pin: pairingCode, + expires: Date.now() + 120000, devices: [{ name: "Private test tablet" }] }), + command: async (action) => { commands.push(action); throw Error("unexpected host mutation"); }, + }, + clipboard: { writeText: (value) => copied.push(value) }, + external: { openUrl: async (value) => { opened.push(value); } }, + } }; + ui.__reset(); + const render = () => ui.__render(ui.CanvasLinkSettings, { locale, open: true }); + try { + render(); await tick(); ui.__flush(); + let tree = render(); + button(tree, locale === "ru" ? "Скопировать промпт агенту" : "Copy prompt for agent").props.onClick(); + ui.__flush(); tree = render(); + assert.deepEqual(copied, [canvasLinkInstallPrompt(locale)]); + assert.match(content(tree), locale === "ru" ? /Промпт скопирован/ : /Prompt copied/); + assert.ok(copied[0].includes(CANVASLINK_ANDROID_RELEASE.url)); + assert.ok(copied[0].includes(CANVASLINK_ANDROID_RELEASE.sha256)); + assert.ok(!copied[0].includes(pairingCode)); + assert.ok(!copied[0].includes("Private test tablet")); + assert.match(copied[0], /adb -s SERIAL install -r PATH_TO_APK/); + assert.match(copied[0], /unauthorized/); + button(tree, locale === "ru" ? "Инструкция и APK" : "Guide and APK").props.onClick(); + assert.deepEqual(opened, [locale === "ru" ? CANVASLINK_ANDROID_RELEASE.guideRuUrl : CANVASLINK_ANDROID_RELEASE.guideUrl]); + assert.deepEqual(commands, []); + } finally { ui.__unmount(); globalThis.window = previousWindow; } +}); + +test("clipboard failure offers the guide without changing Android access", () => { + const previousWindow = globalThis.window; + globalThis.window = { canvasTTY: { clipboard: { writeText() { throw Error("clipboard unavailable"); } } } }; + ui.__reset(); + try { + const props = { locale: "en", open: false }; + const tree = ui.__render(ui.CanvasLinkSettings, props); + button(tree, "Copy prompt for agent").props.onClick(); + ui.__flush(); + const next = ui.__render(ui.CanvasLinkSettings, props); + assert.match(content(next), /Could not copy the prompt. Open the installation guide/); + assert.doesNotMatch(content(next), /Prompt copied/); + } finally { ui.__unmount(); globalThis.window = previousWindow; } +}); diff --git a/tests/helpers/fake-react.mjs b/tests/helpers/fake-react.mjs index 77b20e4af..1dcf395c4 100644 --- a/tests/helpers/fake-react.mjs +++ b/tests/helpers/fake-react.mjs @@ -65,8 +65,11 @@ export default { useState, useRef, useEffect, useLayoutEffect, useCallback, useM const root = fileURLToPath(new URL("../..", import.meta.url)); -/** Imports `exports` (comma-separated names) from a repository-relative module, plus the fake React controls. */ -export async function importWithFakeReact(modulePath, exports, { checkSnapshots = false } = {}) { +/** + * Imports `exports` (comma-separated names) from a repository-relative module, plus the fake React controls. + * Extra esbuild `plugins` can stand in for modules, e.g. to make a lazily imported one fail to load. + */ +export async function importWithFakeReact(modulePath, exports, { checkSnapshots = false, plugins = [] } = {}) { const { outputFiles } = await build({ stdin: { contents: `export { ${exports} } from "./${modulePath}"; export { __render, __flush, __pending, __unmount, __reset } from "react";`, @@ -86,7 +89,7 @@ export async function importWithFakeReact(modulePath, exports, { checkSnapshots builder.onResolve({ filter: /^react(\/jsx-runtime)?$/ }, () => ({ path: "react", namespace: "fake-react" })); builder.onLoad({ filter: /.*/, namespace: "fake-react" }, () => ({ contents: FAKE_REACT, loader: "js" })); } - }] + }, ...plugins] }); return import(`data:text/javascript;base64,${Buffer.from(outputFiles[0].contents).toString("base64")}`); } diff --git a/tests/hermes-hooks-recovery.test.mjs b/tests/hermes-hooks-recovery.test.mjs new file mode 100644 index 000000000..68709cb95 --- /dev/null +++ b/tests/hermes-hooks-recovery.test.mjs @@ -0,0 +1,41 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { mkdtemp, rm, writeFile, readFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { HermesRuntimeHooks } from "../src/main/services/agent-runtime/ProviderRuntimeLaunch.ts"; + +test("HermesRuntimeHooks.recover does not throw when config.yaml has no hooks map", async t => { + const dir = await mkdtemp(join(tmpdir(), "hermes-hooks-recover-")); + t.after(() => rm(dir, { recursive: true, force: true })); + + const configPath = join(dir, "config.yaml"); + const initialYaml = "database:\n journal_mode: wal\nmodel:\n default: antigravity/gemini\n"; + await writeFile(configPath, initialYaml, "utf8"); + + // Create a journal that records an aborted hook registration + const journalPath = join(dir, ".canvastty-runtime-hermes.json"); + const journal = { + version: 1, + provider: "hermes", + originalHash: "dummy-hash", + mutatedHash: "unmatched-hash", + backupPath: join(dir, "backup.bak"), + extra: { + commands: { + on_session_start: ["set ELECTRON_RUN_AS_NODE=1 && CanvasTTY.exe hook-helper.mjs"], + pre_llm_call: ["set ELECTRON_RUN_AS_NODE=1 && CanvasTTY.exe hook-helper.mjs"], + }, + }, + }; + await writeFile(journalPath, JSON.stringify(journal), "utf8"); + + // Must not throw "Expected YAML collection at hooks. Remaining path: on_session_start" + assert.doesNotThrow(() => { + HermesRuntimeHooks.recover(dir); + }); + + // Verify journal was cleaned up and config remains valid + const updatedYaml = await readFile(configPath, "utf8"); + assert.match(updatedYaml, /database:\s*journal_mode: wal/); +}); diff --git a/tests/renderer-bundle-splitting.test.mjs b/tests/renderer-bundle-splitting.test.mjs index 91a34d321..256f2e66b 100644 --- a/tests/renderer-bundle-splitting.test.mjs +++ b/tests/renderer-bundle-splitting.test.mjs @@ -28,7 +28,7 @@ async function bundleApp() { write: true, metafile: true, logLevel: "silent", - loader: { ".svg": "text", ".css": "empty", ".png": "empty", ".ttf": "empty", ".woff2": "empty", ".ico": "empty" } + loader: { ".svg": "text", ".css": "empty", ".png": "empty", ".webp": "empty", ".ttf": "empty", ".woff2": "empty", ".ico": "empty" } }); return metafile; } finally { @@ -67,11 +67,17 @@ test("Settings and the launch/link dialogs are split out of the app's static imp assert.ok(settingsPath, "SettingsPanel must be emitted as its own on-demand chunk"); assert.ok(launchDialogPath, "AgentLaunchDialog must be emitted as its own on-demand chunk"); assert.ok(linkDialogPath, "TerminalLinkDialog must be emitted as its own on-demand chunk"); + const paletteChunk = Object.keys(metafile.outputs).find((p) => /CanvasCommandPalette-/.test(p)); + assert.ok(paletteChunk, "the canvas command palette must be emitted as its own on-demand chunk"); + const contextMenuChunk = Object.keys(metafile.outputs).find((p) => /CanvasContextMenu-/.test(p)); + assert.ok(contextMenuChunk, "the canvas context menu must be emitted as its own on-demand chunk"); const { paths: eagerPaths, bytes: eagerBytes } = staticallyReachableBytes(metafile, appPath); assert.ok(!eagerPaths.has(settingsPath), "SettingsPanel must only be reachable through a dynamic import()"); assert.ok(!eagerPaths.has(launchDialogPath), "AgentLaunchDialog must only be reachable through a dynamic import()"); assert.ok(!eagerPaths.has(linkDialogPath), "TerminalLinkDialog must only be reachable through a dynamic import()"); + assert.ok(!eagerPaths.has(paletteChunk), "the command palette must only be reachable through a dynamic import()"); + assert.ok(!eagerPaths.has(contextMenuChunk), "the canvas context menu must only be reachable through a dynamic import()"); const settingsBytes = metafile.outputs[settingsPath].bytes; assert.ok(settingsBytes > 100_000, `SettingsPanel chunk should carry real weight, got ${settingsBytes} bytes`); @@ -81,6 +87,5 @@ test("Settings and the launch/link dialogs are split out of the app's static imp assert.ok( eagerBytes < 1_700_000, `code statically reachable from App.js should stay bounded now that heavy UI loads on demand, got ${eagerBytes} bytes` - ); }); diff --git a/tests/settings-normalizer.test.mjs b/tests/settings-normalizer.test.mjs index 69dc4b49a..8ba9f75bc 100644 --- a/tests/settings-normalizer.test.mjs +++ b/tests/settings-normalizer.test.mjs @@ -37,6 +37,23 @@ const fallback = { canvasBackground: "none", pattern: "dots", terminalBorderSkin: "classic", + terminalBackground: "none", + terminalBackgroundBrightness: 45, + terminalBackgroundContrast: 100, + terminalBackgroundSaturation: 100, + terminalBackgroundPaused: false, + terminalBackgroundSpeed: 1, + terminalBackgroundScale: 100, + terminalBackgroundOffsetX: 0, + terminalBackgroundOffsetY: 0, + terminalBackgroundReadability: true, + terminalBackgroundReadabilityStrength: 86, + terminalBackgroundPauseOnOutput: false, + terminalBackgroundSynchronized: false, + terminalBackgroundPauseInactive: false, + terminalBackgroundOverrides: {}, + terminalBackgroundPresets: [], + terminalBackgroundFavorites: [], terminalSkinDetail: "detailed", terminalSkinAnimationEnabled: true, appSkin: "classic", @@ -78,6 +95,108 @@ const fallback = { browserPauseHiddenTabs: true }; +test("persists ASCII scene settings and rejects invalid scene paths and brightness", () => { + const valid = normalizeSettings({ terminalBackground: "lantern-lake", terminalBackgroundBrightness: 70, terminalBackgroundPaused: true }, fallback); + assert.equal(valid.terminalBackground, "lantern-lake"); + assert.equal(valid.terminalBackgroundBrightness, 70); + assert.equal(valid.terminalBackgroundPaused, true); + for (const value of ["../../remote", "https://example.com/art.js", null, 42]) { + assert.equal(normalizeSettings({ terminalBackground: value }, fallback).terminalBackground, "none"); + } + assert.equal(normalizeSettings({ terminalBackgroundBrightness: Infinity }, fallback).terminalBackgroundBrightness, 45); + assert.equal(normalizeSettings({ terminalBackgroundBrightness: -20 }, fallback).terminalBackgroundBrightness, 10); + assert.equal(normalizeSettings({ terminalBackgroundBrightness: 150 }, fallback).terminalBackgroundBrightness, 100); + const legacy = normalizeSettings({}, fallback); + assert.equal(legacy.terminalBackground, "none"); + assert.equal(legacy.terminalBackgroundPaused, false); + assert.equal(legacy.terminalBackgroundReadability, true); + assert.equal(normalizeSettings({ terminalBackgroundReadability: false }, fallback).terminalBackgroundReadability, false); + assert.equal(normalizeSettings({ terminalBackgroundReadability: true }, fallback).terminalBackgroundReadability, true); + assert.equal(normalizeSettings({ terminalBackgroundReadability: "off" }, fallback).terminalBackgroundReadability, true); +}); + +test("background scale and position validate old settings and persist independently of speed", async () => { + const defaults = normalizeSettings({}, fallback); + assert.equal(defaults.terminalBackgroundScale, 100); + assert.equal(defaults.terminalBackgroundOffsetX, 0); + assert.equal(defaults.terminalBackgroundOffsetY, 0); + const invalid = normalizeSettings({ terminalBackgroundScale: NaN, terminalBackgroundOffsetX: Infinity, terminalBackgroundOffsetY: "down" }, fallback); + assert.equal(invalid.terminalBackgroundScale, 100); + assert.equal(invalid.terminalBackgroundOffsetX, 0); + assert.equal(invalid.terminalBackgroundOffsetY, 0); + const clamped = normalizeSettings({ terminalBackgroundScale: 999, terminalBackgroundOffsetX: -99, terminalBackgroundOffsetY: 99 }, fallback); + assert.equal(clamped.terminalBackgroundScale, 300); + assert.equal(clamped.terminalBackgroundOffsetX, -50); + assert.equal(clamped.terminalBackgroundOffsetY, 50); + assert.equal(normalizeSettings({ terminalBackgroundScale: -1 }, fallback).terminalBackgroundScale, 25); + const dir = await mkdtemp(join(tmpdir(), "canvastty-background-transform-")); + try { + const store = new SettingsStore(dir, "en"); + await store.update({ terminalBackgroundScale: 175, terminalBackgroundOffsetX: -20, terminalBackgroundOffsetY: 15, terminalBackgroundSpeed: 0.5 }); + const restored = await new SettingsStore(dir, "en").load(); + assert.equal(restored.terminalBackgroundScale, 175); + assert.equal(restored.terminalBackgroundOffsetX, -20); + assert.equal(restored.terminalBackgroundOffsetY, 15); + await store.update({ terminalBackgroundScale: 100, terminalBackgroundOffsetX: 0, terminalBackgroundOffsetY: 0 }); + assert.equal((await new SettingsStore(dir, "en").load()).terminalBackgroundSpeed, 0.5); + } finally { + await rm(dir, { recursive: true, force: true }); + } +}); + +test("background speed defaults to 1x, clamps invalid input and survives restart while paused", async () => { + assert.equal(normalizeSettings({}, fallback).terminalBackgroundSpeed, 1); + for (const value of [NaN, Infinity, "fast", null]) { + assert.equal(normalizeSettings({ terminalBackgroundSpeed: value }, fallback).terminalBackgroundSpeed, 1); + } + assert.equal(normalizeSettings({ terminalBackgroundSpeed: -1 }, fallback).terminalBackgroundSpeed, 0.25); + assert.equal(normalizeSettings({ terminalBackgroundSpeed: 10 }, fallback).terminalBackgroundSpeed, 2); + const dir = await mkdtemp(join(tmpdir(), "canvastty-background-speed-")); + try { + const store = new SettingsStore(dir, "en"); + await store.update({ terminalBackgroundSpeed: 0.5, terminalBackgroundPaused: true }); + const restored = await new SettingsStore(dir, "en").load(); + assert.equal(restored.terminalBackgroundSpeed, 0.5); + assert.equal(restored.terminalBackgroundPaused, true); + } finally { + await rm(dir, { recursive: true, force: true }); + } +}); + +test("favorite backgrounds are validated, deduplicated and persist without changing the active scene", async () => { + assert.deepEqual(normalizeSettings({ terminalBackgroundFavorites: ["aurora-fjord", "none", "../../bad", "aurora-fjord", 1, "donut"] }, fallback).terminalBackgroundFavorites, + ["aurora-fjord", "donut"]); + assert.deepEqual(normalizeSettings({}, fallback).terminalBackgroundFavorites, []); + assert.deepEqual(normalizeSettings({ terminalBackgroundFavorites: "donut" }, fallback).terminalBackgroundFavorites, []); + const dir = await mkdtemp(join(tmpdir(), "canvastty-background-favorites-")); + try { + const store = new SettingsStore(dir, "en"); + await store.update({ terminalBackground: "aurora-fjord", terminalBackgroundFavorites: ["donut"] }); + const restored = await new SettingsStore(dir, "en").load(); + assert.deepEqual(restored.terminalBackgroundFavorites, ["donut"]); + assert.equal(restored.terminalBackground, "aurora-fjord"); + await store.update({ terminalBackgroundFavorites: [] }); + assert.deepEqual((await new SettingsStore(dir, "en").load()).terminalBackgroundFavorites, []); + } finally { + await rm(dir, { recursive: true, force: true }); + } +}); + +test("background readability survives reload and background changes", async () => { + const dir = await mkdtemp(join(tmpdir(), "canvastty-background-readability-")); + try { + const store = new SettingsStore(dir, "en"); + await store.update({ terminalBackground: "aurora-fjord", terminalBackgroundReadability: false }); + assert.equal((await new SettingsStore(dir, "en").load()).terminalBackgroundReadability, false); + await store.update({ terminalBackground: "none" }); + assert.equal((await new SettingsStore(dir, "en").load()).terminalBackgroundReadability, false); + await store.update({ terminalBackgroundReadability: true }); + assert.equal((await new SettingsStore(dir, "en").load()).terminalBackgroundReadability, true); + } finally { + await rm(dir, { recursive: true, force: true }); + } +}); + test("keeps valid wheel, edge pan, zoom, and focus values", () => { const normalized = normalizeSettings( { @@ -1023,6 +1142,13 @@ test("both overrides accept zoom modifiers without swallowing ordinary shortcuts assert.equal(mouseBindings.canvasNavigationOverride, "Shift+Mouse5"); }); +test("inactive background pause accepts booleans and preserves the saved fallback", () => { + assert.equal(normalizeSettings({ terminalBackgroundPauseInactive: true }, fallback).terminalBackgroundPauseInactive, true); + assert.equal(normalizeSettings({ terminalBackgroundPauseInactive: false }, { ...fallback, terminalBackgroundPauseInactive: true }).terminalBackgroundPauseInactive, false); + assert.equal(normalizeSettings({ terminalBackgroundPauseInactive: "true" }, fallback).terminalBackgroundPauseInactive, false); + assert.equal(normalizeSettings({}, { ...fallback, terminalBackgroundPauseInactive: true }).terminalBackgroundPauseInactive, true); +}); + test("a saved edge pan preference survives normalization", () => { const normalized = normalizeSettings({ edgePan: true, edgePanSpeed: "fast" }, fallback); assert.equal(normalized.edgePan, true); diff --git a/tests/settings-ui.test.mjs b/tests/settings-ui.test.mjs index c33d31b22..b2f718149 100644 --- a/tests/settings-ui.test.mjs +++ b/tests/settings-ui.test.mjs @@ -293,6 +293,8 @@ test("the redesigned menus use shared tokens, em geometry, and the configured UI const menuStyleEnd = styles.indexOf(".sticky-note-card", menuStyleStart); const menuStyles = styles.slice(menuStyleStart, menuStyleEnd); assert.doesNotMatch(menuStyles, /#[\da-f]{3,8}|rgba?\(/i); + assert.match(menuStyles, /\.canvas-menu__check \{[^}]*color: var\(--primary\)/); + assert.match(menuStyles, /\.canvas-menu__row:disabled \{/); assert.match(styles, /\.launcher-dock \{[^}]*font-size: calc\(13px \* var\(--ui-scale, 1\)\)/); assert.match(styles, /--card-header-height: calc\(54px \* var\(--ui-scale, 1\)\)/); assert.match(styles, /\.terminal-card__header \{[^}]*font-size: calc\(13px \* var\(--ui-scale, 1\)\)/); diff --git a/tests/terminal-ascii-background.test.mjs b/tests/terminal-ascii-background.test.mjs new file mode 100644 index 000000000..3425fc086 --- /dev/null +++ b/tests/terminal-ascii-background.test.mjs @@ -0,0 +1,109 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { readdir } from "node:fs/promises"; +import { TERMINAL_BACKGROUNDS, TERMINAL_BACKGROUND_CATALOG, isTerminalBackgroundId } from "../src/shared/terminalBackground.ts"; +import { asciiFrameSvg } from "../src/renderer/src/features/terminal/asciiFrameSvg.ts"; +import { drawAsciiFrame } from "../src/renderer/src/features/terminal/asciiFrameCanvas.ts"; +import { asciiFrameBounds } from "../src/renderer/src/features/terminal/asciiFrameBounds.ts"; + +test("manual zoom and offsets apply equally to full scenes and objects while defaults stay unchanged", () => { + for (const meta of [{ cols: 200, rows: 100, cell: 1 }, { cols: 46, rows: 28 }]) { + const base = asciiFrameBounds(800, 600, meta, 2); + assert.deepEqual(asciiFrameBounds(800, 600, meta, 2, { scale: 100, offsetX: 0, offsetY: 0 }), base); + const zoom = asciiFrameBounds(800, 600, meta, 2, { scale: 200, offsetX: 25, offsetY: -20 }); + assert.equal(zoom.width, base.width * 2); + assert.equal(zoom.height, base.height * 2); + assert.equal(zoom.x + zoom.width / 2, 600); + assert.equal(zoom.y + zoom.height / 2, 180); + const small = asciiFrameBounds(800, 600, meta, 2, { scale: 25, offsetX: 0, offsetY: 0 }); + assert.equal(small.width, base.width / 4); + assert.equal(small.height, base.height / 4); + } +}); + +test("Apple and other character art keep equal horizontal and vertical scale in wide and tall terminals", () => { + const meta = { cols: 46, rows: 28 }; + for (const [width, height] of [[760, 440], [300, 900], [1400, 801]]) { + const bounds = asciiFrameBounds(width, height, meta); + assert(Math.abs(bounds.width / (46 * 6) - bounds.height / (28 * 12)) < 1e-10); + assert(bounds.width <= width && bounds.height <= height); + assert.equal(bounds.x * 2 + bounds.width, width); + assert.equal(bounds.y * 2 + bounds.height, height); + } + assert.deepEqual(asciiFrameBounds(760, 440, { cols: 200, rows: 100, cell: 1 }), { x: 0, y: 0, width: 760, height: 440 }); +}); + +test("3D backgrounds keep fine character size as terminals grow, including HiDPI displays", () => { + for (const meta of [{ cols: 40, rows: 22 }, { cols: 64, rows: 20 }]) { + const regular = asciiFrameBounds(1060, 710, meta); + const enlarged = asciiFrameBounds(1800, 1100, meta); + const hidpi = asciiFrameBounds(2120, 1420, meta, 2); + assert.equal(regular.width, enlarged.width, 'larger terminals must not enlarge the ASCII characters'); + assert.equal(regular.height, enlarged.height); + assert.equal(regular.width / meta.cols, 7.5); + assert.equal(hidpi.width / 2, regular.width, 'CSS size stays the same on HiDPI'); + assert.equal(hidpi.height / 2, regular.height); + const compact = asciiFrameBounds(120, 90, meta); + assert(compact.width <= 96 && compact.height <= 72); + } +}); + +test("the 2D fallback clears each frame without retaining clipping regions or old pixels", () => { + const calls = []; + const ctx = new Proxy({ canvas: { width: 800, height: 600 } }, { + get(target, key) { return key in target ? target[key] : (...args) => calls.push([key, ...args]); }, + }); + const meta = { cols: 2, rows: 2, cell: 1, palette: ['#ff0000', '#00ff00'] }; + drawAsciiFrame(ctx, 'AA\nBB', meta, new Uint8Array([0, 0, 1, 1])); + drawAsciiFrame(ctx, '', meta); + assert.equal(calls.filter(([key]) => key === 'fillRect').length, 2); + assert.equal(calls.filter(([key]) => key === 'fillText').length, 2); + assert.equal(calls.filter(([key]) => key === 'save').length, calls.filter(([key]) => key === 'restore').length); + assert(calls.some((call) => JSON.stringify(call) === JSON.stringify(['fillText', 'BB', 0, 9, 12]))); +}); + +test("vector frames keep cell alignment, escape text and fill arbitrary terminal dimensions", () => { + const svg = asciiFrameSvg('<&\nAB', { cols: 2, rows: 2, cell: 1, palette: ['#ff0000', '#00ff00'] }, new Uint8Array([0, 1, 1, 1])); + assert(svg.includes('viewBox="0 0 12 12" preserveAspectRatio="none"')); + assert(svg.includes('textLength="12" lengthAdjust="spacingAndGlyphs"')); + assert(svg.includes('fill="#00ff00">AB')); + assert(svg.includes('<')); + assert(svg.includes('&')); + assert(!svg.includes(' { + const files = await readdir(new URL("../node_modules/ascii.rest/dist/pieces/", import.meta.url)); + assert.equal(TERMINAL_BACKGROUNDS.length, 144); + assert.equal(new Set(TERMINAL_BACKGROUNDS).size, 144); + assert.equal(new Set(TERMINAL_BACKGROUND_CATALOG.map((piece) => piece.category)).size, 12); + for (const id of TERMINAL_BACKGROUNDS) { + assert(files.includes(`${id}.js`), id); + assert(isTerminalBackgroundId(id), id); + } + assert(!isTerminalBackgroundId("../remote")); +}); + +test("every bundled animation renders and coloured scenes produce valid changing frames", async () => { + for (const scene of TERMINAL_BACKGROUNDS) { + const { meta, default: make } = await import(`ascii.rest/pieces/${scene}`); + const frame = make({}); + const color = new Uint8Array((meta.cols ?? 80) * (meta.rows ?? 24)); + const env = { paper: false, color }; + const first = frame(0, env); + const firstColor = color.slice(); + const second = frame(1, env); + const svg = asciiFrameSvg(second, meta, color); + assert(svg.startsWith(' 0 || second.length > 0, scene); + if (meta.palette) assert(color.every((index) => index < meta.palette.length), scene); + if (meta.category === "scenes") { + assert.equal(first.split("\n").length, meta.rows, scene); + assert(first.split("\n").every((row) => [...row].length === meta.cols), scene); + assert(first !== second || !firstColor.every((value, index) => value === color[index]), `${scene} must animate`); + } + } +}); diff --git a/tests/terminal-ascii-worker.test.mjs b/tests/terminal-ascii-worker.test.mjs new file mode 100644 index 000000000..e88be811e --- /dev/null +++ b/tests/terminal-ascii-worker.test.mjs @@ -0,0 +1,105 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import vm from "node:vm"; +import { fileURLToPath } from "node:url"; +import { build } from "esbuild"; + +test("speed changes the scene clock continuously without more redraws or waking paused backgrounds", async () => { + const bundle = await build({ + entryPoints: [fileURLToPath(new URL('../src/renderer/src/features/terminal/asciiBackground.worker.ts', import.meta.url))], + bundle: true, write: false, format: 'iife', platform: 'browser', + plugins: [{ name: 'scene-clock-probe', setup(build) { + build.onLoad({ filter: /pieces[\\/]donut\.js$/ }, () => ({ + contents: 'export const meta={cols:1,rows:1,cell:1,fps:12};export default ()=>time=>{recordFrameTime(time);return "X";}', + loader: 'js', + })); + } }], + }); + let clock = 0, next = 0; + const times = [], transforms = [], timers = new Map(), self = {}; + const canvas = { width: 80, height: 40 }; + const ctx = new Proxy({ canvas, setTransform: (...args) => transforms.push(args) }, { get: (target, key) => key in target ? target[key] : () => {} }); + canvas.getContext = kind => kind === '2d' ? ctx : null; + vm.runInNewContext(bundle.outputFiles[0].text, { + self, console, performance: { now: () => clock }, recordFrameTime: value => times.push(value), + setTimeout: (callback, delay) => { timers.set(++next, { callback, delay }); return next; }, + clearTimeout: id => timers.delete(id), + }); + const send = data => self.onmessage({ data }); + const tickAt = now => { + clock = now; + const [id, timer] = [...timers][0]; + timers.delete(id); + timer.callback(); + assert.equal(timers.size, 1); + assert(Math.abs([...timers.values()][0].delay - 1000 / 12) < 1e-8); + }; + await send({ type: 'init', canvas, scene: 'donut' }); + await send({ type: 'playing', value: true }); + tickAt(100); + assert(Math.abs(times.at(-1) - 0.1) < 1e-8); + clock = 150; + await send({ type: 'speed', value: 2 }); + assert.equal(times.length, 2, 'changing speed does not redraw or restart the scene'); + tickAt(200); + assert(Math.abs(times.at(-1) - 0.25) < 1e-8); + clock = 250; + await send({ type: 'speed', value: 0.25 }); + tickAt(300); + assert(Math.abs(times.at(-1) - 0.3625) < 1e-8); + await send({ type: 'playing', value: false }); + const framesBeforeTransform = times.length; + await send({ type: 'transform', value: { scale: 200, offsetX: 25, offsetY: -20 } }); + assert.equal(times.length, framesBeforeTransform, 'moving a paused background reuses its current frame'); + assert.equal(timers.size, 0, 'moving does not unpause the scene'); + assert.deepEqual(transforms.at(-1), [160 / 6, 0, 0, 80 / 6, -20, -28]); + const paintsBeforeSameTransform = transforms.length; + await send({ type: 'transform', value: { scale: 200, offsetX: 25, offsetY: -20 } }); + assert.equal(transforms.length, paintsBeforeSameTransform, 'unchanged settings do not trigger a redraw'); + clock = 10000; + await send({ type: 'speed', value: 2 }); + assert.equal(timers.size, 0, 'speed changes keep paused scenes paused'); + await send({ type: 'playing', value: true }); + tickAt(10100); + assert(Math.abs(times.at(-1) - 0.5625) < 1e-8, 'paused time never advances the animation'); +}); + +test("background worker draws independently, budgets expensive frames and stops without queued work", async () => { + const bundle = await build({ + entryPoints: [fileURLToPath(new URL('../src/renderer/src/features/terminal/asciiBackground.worker.ts', import.meta.url))], + bundle: true, write: false, format: 'iife', platform: 'browser', + }); + let clock = 0, paints = 0, next = 0; + const timers = new Map(); + const canvas = { width: 1200, height: 600 }; + const ctx = new Proxy({ canvas }, { + get(target, key) { + return key in target ? target[key] : () => { clock += 0.02; if (key === 'fillText') paints++; }; + }, + }); + canvas.getContext = (kind) => kind === '2d' ? ctx : null; + const self = {}; + vm.runInNewContext(bundle.outputFiles[0].text, { + self, console, performance: { now: () => clock }, + setTimeout: (callback, delay) => { timers.set(++next, { callback, delay }); return next; }, + clearTimeout: (id) => timers.delete(id), + }); + await self.onmessage({ data: { type: 'init', canvas, scene: 'aurora-fjord' } }); + assert(paints > 0); + assert.equal(timers.size, 0, 'starts with a still frame'); + await self.onmessage({ data: { type: 'playing', value: true } }); + assert.equal(timers.size, 1); + const [id, { callback }] = [...timers][0]; + timers.delete(id); + const start = clock; + callback(); + const cost = clock - start; + assert.equal(timers.size, 1, 'only one next frame is scheduled'); + assert([...timers.values()][0].delay >= cost * 3 - 0.001, 'expensive frames leave at least 75% idle time'); + await self.onmessage({ data: { type: 'playing', value: false } }); + assert.equal(timers.size, 0); + await self.onmessage({ data: { type: 'resize', width: 800, height: 400 } }); + assert.equal(canvas.width, 800); + assert.equal(canvas.height, 400); + assert.equal(timers.size, 0, 'resize preserves pause'); +}); diff --git a/tests/terminal-background-appearance.test.mjs b/tests/terminal-background-appearance.test.mjs new file mode 100644 index 000000000..5f0a2f26f --- /dev/null +++ b/tests/terminal-background-appearance.test.mjs @@ -0,0 +1,132 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { normalizeBackgroundAppearance, normalizeBackgroundOverrides, normalizeBackgroundPresets } from "../src/shared/terminalBackgroundAppearance.ts"; +import { SettingsStore } from "../src/main/services/SettingsStore.ts"; +import { createBackgroundOutputPause } from "../src/renderer/src/features/terminal/backgroundOutputPause.ts"; + +test("background overrides and presets validate fields without leaking unrelated settings", () => { + const appearance = normalizeBackgroundAppearance({ + terminalBackground: "donut", terminalBackgroundScale: 999, + terminalBackgroundSpeed: NaN, terminalBackgroundReadabilityStrength: -20, + terminalBackgroundPauseOnOutput: true, terminalBackgroundFocusDimming: true, terminalBackgroundContrast: -5, terminalBackgroundSaturation: 999, palette: "bad", + }); + assert.equal(appearance.terminalBackgroundScale, 300); + assert.equal(appearance.terminalBackgroundSpeed, 1); + assert.equal(appearance.terminalBackgroundReadabilityStrength, 0); + assert.equal(appearance.terminalBackgroundPauseOnOutput, true); + assert.equal(appearance.terminalBackgroundFocusDimming, true); + assert.equal(appearance.terminalBackgroundContrast, 0); + assert.equal(appearance.terminalBackgroundSaturation, 200); + const oldPreset = normalizeBackgroundAppearance({ terminalBackground: "donut" }); + assert.equal(oldPreset.terminalBackgroundContrast, 100); + assert.equal(oldPreset.terminalBackgroundSaturation, 100); + const invalid = normalizeBackgroundAppearance({ terminalBackgroundContrast: NaN, terminalBackgroundSaturation: "200" }); + assert.equal(invalid.terminalBackgroundContrast, 100); + assert.equal(invalid.terminalBackgroundSaturation, 100); + assert(!("palette" in appearance)); + assert.deepEqual(normalizeBackgroundOverrides([]), {}); + assert.deepEqual(Object.keys(normalizeBackgroundOverrides({ a: appearance, b: null, c: [] })), ["a"]); + const presets = normalizeBackgroundPresets([ + { id: "one", name: " Saved ", appearance }, + { id: "one", name: "Duplicate", appearance }, { id: "two", name: " " }, null, + ]); + assert.equal(presets.length, 1); + assert.equal(presets[0].name, "Saved"); +}); + +test("per-terminal backgrounds, presets and optional controls survive settings reload", async t => { + const dir = await mkdtemp(join(tmpdir(), "background-appearance-")); + t.after(() => rm(dir, { recursive: true, force: true })); + const store = new SettingsStore(dir, "en"); + await store.load(); + assert.equal(store.get().terminalBackgroundPauseInactive, false); + const appearance = normalizeBackgroundAppearance({ terminalBackground: "donut", terminalBackgroundPauseOnOutput: true, terminalBackgroundReadabilityStrength: 35, terminalBackgroundContrast: 150, terminalBackgroundSaturation: 0 }); + await store.update({ + terminalBackground: "none", terminalBackgroundPauseOnOutput: true, + terminalBackgroundFocusDimming: true, + terminalBackgroundSynchronized: true, + terminalBackgroundPauseInactive: true, + terminalBackgroundReadabilityStrength: 48, + terminalBackgroundContrast: 120, terminalBackgroundSaturation: 180, + terminalBackgroundOverrides: { first: appearance, second: { ...appearance, terminalBackground: "none" } }, + terminalBackgroundPresets: [{ id: "saved", name: "My preset", appearance }], + }); + const restored = await new SettingsStore(dir, "en").load(); + assert.equal(restored.terminalBackground, "none"); + assert.equal(restored.terminalBackgroundPauseOnOutput, true); + assert.equal(restored.terminalBackgroundFocusDimming, true); + assert.equal(restored.terminalBackgroundSynchronized, true); + assert.equal(restored.terminalBackgroundPauseInactive, true); + assert.equal(restored.terminalBackgroundReadabilityStrength, 48); + assert.equal(restored.terminalBackgroundContrast, 120); + assert.equal(restored.terminalBackgroundSaturation, 180); + assert.equal(restored.terminalBackgroundOverrides.first.terminalBackgroundContrast, 150); + assert.equal(restored.terminalBackgroundOverrides.first.terminalBackgroundSaturation, 0); + assert.equal(restored.terminalBackgroundOverrides.first.terminalBackground, "donut"); + assert.equal(restored.terminalBackgroundOverrides.second.terminalBackground, "none"); + assert.deepEqual(restored.terminalBackgroundPresets[0].appearance, appearance); +}); + +test("output pauses only when enabled, extends a single timeout and resumes after silence", t => { + t.mock.timers.enable({ apis: ["setTimeout"] }); + let changes = 0; + const pause = createBackgroundOutputPause(() => changes++); + pause.activity(); + assert.equal(pause.isPaused(), false); + pause.setEnabled(true); + pause.activity(); + assert.equal(pause.isPaused(), true); + assert.equal(changes, 1); + t.mock.timers.tick(900); + pause.activity(); + assert.equal(changes, 1); + t.mock.timers.tick(900); + assert.equal(pause.isPaused(), true); + t.mock.timers.tick(100); + assert.equal(pause.isPaused(), false); + assert.equal(changes, 2); + pause.activity(); + pause.setEnabled(false); + assert.equal(pause.isPaused(), false); + const afterDisable = changes; + t.mock.timers.tick(2000); + assert.equal(changes, afterDisable); + pause.setEnabled(true); + pause.activity(); + pause.dispose(); + const afterDispose = changes; + t.mock.timers.tick(2000); + pause.activity(); + assert.equal(changes, afterDispose); +}); + +test("disabling all terminal backgrounds clears root background and all terminal overrides", async t => { + const dir = await mkdtemp(join(tmpdir(), "background-disable-all-")); + t.after(() => rm(dir, { recursive: true, force: true })); + const store = new SettingsStore(dir, "ru"); + await store.load(); + const appearance1 = normalizeBackgroundAppearance({ terminalBackground: "matrix-rain" }); + const appearance2 = normalizeBackgroundAppearance({ terminalBackground: "night-coast" }); + await store.update({ + terminalBackground: "earth", + terminalBackgroundOverrides: { + "session-1": appearance1, + "session-2": appearance2, + }, + }); + let state = store.get(); + assert.equal(state.terminalBackground, "earth"); + assert.equal(Object.keys(state.terminalBackgroundOverrides).length, 2); + + // Apply disable all action + await store.update({ + terminalBackground: "none", + terminalBackgroundOverrides: {}, + }); + state = store.get(); + assert.equal(state.terminalBackground, "none"); + assert.deepEqual(state.terminalBackgroundOverrides, {}); +}); diff --git a/tests/terminal-background-choices.test.mjs b/tests/terminal-background-choices.test.mjs new file mode 100644 index 000000000..4e00332d6 --- /dev/null +++ b/tests/terminal-background-choices.test.mjs @@ -0,0 +1,81 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { fileURLToPath } from "node:url"; +import { readFile } from "node:fs/promises"; +import { build } from "esbuild"; +import { TERMINAL_BACKGROUND_CATALOG } from "../src/shared/terminalBackground.ts"; + +const bundle = await build({ + stdin: { + contents: 'export { TerminalBackgroundChoices, BackgroundPreviewFrame } from "./src/renderer/src/features/settings/TerminalBackgroundChoices.tsx"; export { createElement } from "react"; export { renderToStaticMarkup } from "react-dom/server";', + resolveDir: fileURLToPath(new URL("../", import.meta.url)), loader: "ts" + }, + bundle: true, platform: "node", format: "esm", jsx: "automatic", + loader: { ".css": "empty" }, + plugins: [{ name: "poster-urls", setup(build) { + build.onLoad({ filter: /TerminalBackgroundChoices\.tsx$/ }, async ({ path }) => { + const urls = Object.fromEntries(TERMINAL_BACKGROUND_CATALOG.map(({ id }) => [`../../assets/ascii-posters/${id}.webp`, `/posters/${id}.webp`])); + const animations = Object.fromEntries(TERMINAL_BACKGROUND_CATALOG.map(({ id }) => [`../../assets/ascii-animations/${id}.webp`, `/animations/${id}.webp`])); + const source = await readFile(path, "utf8"); + return { contents: source.replace(/import\.meta\.glob\([^;]+\)/g, (expression) => JSON.stringify(expression.includes("ascii-animations") ? animations : urls)), loader: "tsx" }; + }); + } }], + banner: { js: `import { createRequire } from "node:module"; const require = createRequire(${JSON.stringify(import.meta.url)});` }, + logLevel: "silent", write: false +}); +const { TerminalBackgroundChoices, BackgroundPreviewFrame, createElement, renderToStaticMarkup } = await import( + `data:text/javascript;base64,${Buffer.from(bundle.outputFiles[0].contents).toString("base64")}` +); + +test("hover replaces the poster with the animation instead of layering them", () => { + const still = renderToStaticMarkup(createElement(BackgroundPreviewFrame, { scene: "not-found", playing: false })); + const live = renderToStaticMarkup(createElement(BackgroundPreviewFrame, { scene: "not-found", playing: true })); + assert.match(still, / { + for (const category of new Set(TERMINAL_BACKGROUND_CATALOG.map((piece) => piece.category))) { + const pieces = TERMINAL_BACKGROUND_CATALOG.filter((piece) => piece.category === category); + const html = renderToStaticMarkup(createElement(TerminalBackgroundChoices, { + locale: "en", value: pieces[0].id, paused: false, favorites: [], onFavoritesChange: () => {}, onChange: () => {} + })); + assert.equal((html.match(/class="border-skin-choice"/g) ?? []).length, pieces.length, category); + assert.equal((html.match(/ { + const html = renderToStaticMarkup(createElement(TerminalBackgroundChoices, { + locale: "en", value: "aurora-fjord", paused: false, + favorites: ["aurora-fjord", "donut"], onFavoritesChange: () => {}, onChange: () => {}, + })); + assert.match(html, /