diff --git a/packages/capture-kit/src/screenshot-overlay.ts b/packages/capture-kit/src/screenshot-overlay.ts index 93a385aaad..306c43a037 100644 --- a/packages/capture-kit/src/screenshot-overlay.ts +++ b/packages/capture-kit/src/screenshot-overlay.ts @@ -9,6 +9,7 @@ import { type SnapshotState } from '@agent-device/contracts/snapshot-model'; import { decodePngAsync, encodePngAsync } from './png-worker-client.ts'; import { projectSnapshotRectToScreenshot, + resolveScreenshotFrame, resolveSnapshotBounds, } from './snapshot-rect-projection.ts'; import { analyzeReactNativeOverlay } from './react-native-overlay.ts'; @@ -270,10 +271,21 @@ function projectRectToScreenshot( screenshotHeight: number, ): Rect { const space = snapshot.backend === 'android' ? 'device-pixels' : 'viewport-points'; + const frame = resolveScreenshotFrame( + { width: screenshotWidth, height: screenshotHeight }, + snapshot, + ); return clampRect( - projectSnapshotRectToScreenshot(space, bounds, rect, screenshotWidth, screenshotHeight), - screenshotWidth, - screenshotHeight, + projectSnapshotRectToScreenshot( + space, + bounds, + rect, + screenshotWidth, + screenshotHeight, + snapshot.captureScale, + ), + frame.width, + frame.height, ); } diff --git a/packages/capture-kit/src/snapshot-state.ts b/packages/capture-kit/src/snapshot-state.ts index b6300858b7..044b2b3b2c 100644 --- a/packages/capture-kit/src/snapshot-state.ts +++ b/packages/capture-kit/src/snapshot-state.ts @@ -19,6 +19,7 @@ import { type PresentedSurface, type RawSnapshotNode, type SnapshotKeyboardBandFact, + type SnapshotCaptureScale, type SnapshotViewportSize, type SnapshotQualityNote, } from '@agent-device/kernel/snapshot'; @@ -75,6 +76,8 @@ export function buildSnapshotState( keyboard?: SnapshotKeyboardBandFact; /** The box the producer measured the rects in (#3182), carried to the response that reads this state. */ viewport?: SnapshotViewportSize; + /** Screenshot pixels per rect unit, carried beside `viewport` to every projection of this state. */ + captureScale?: SnapshotCaptureScale; /** * iOS: the producer's reading of a modal presentation over the app (#3565), decided on the * acquired tree so every projection of one screen carries the same value. @@ -129,7 +132,7 @@ export function buildSnapshotState( ...(data.presentedSurface ? { presentedSurface: data.presentedSurface } : {}), ...(data.targetActivation ? { targetActivation: data.targetActivation } : {}), ...(data.keyboard ? { keyboard: data.keyboard } : {}), - ...(data.viewport ? { viewport: data.viewport } : {}), + ...snapshotStateGeometry(data), presentationKey: buildSnapshotPresentationKey(presentation), presentation, // Only broad Android snapshots become freshness baselines. If the user asked for a scoped @@ -139,6 +142,15 @@ export function buildSnapshotState( }; } +function snapshotStateGeometry( + data: Readonly<{ viewport?: SnapshotViewportSize; captureScale?: SnapshotCaptureScale }>, +): Pick { + return { + ...(data.viewport ? { viewport: data.viewport } : {}), + ...(data.captureScale ? { captureScale: data.captureScale } : {}), + }; +} + /** * The hint a node carries when the rect it is published with has no area. That is not evidence * that the node is off screen, but nothing confirms that it is on screen either: a scroll view diff --git a/packages/contracts/src/snapshot-model.ts b/packages/contracts/src/snapshot-model.ts index 3928522e59..eb763d513c 100644 --- a/packages/contracts/src/snapshot-model.ts +++ b/packages/contracts/src/snapshot-model.ts @@ -7,6 +7,7 @@ import type { SnapshotQualityNote, SnapshotQualityVerdict, SnapshotRefMinting, + SnapshotCaptureScale, SnapshotViewportSize, } from '@agent-device/kernel/snapshot'; import { @@ -136,6 +137,12 @@ export type SnapshotState = { * producer measured instead of inferring one from the largest rect still on screen. */ viewport?: SnapshotViewportSize; + /** + * Screenshot pixels per rect unit, when the producer published one (a TV app page's CSS px). The + * state carries it beside `viewport` so every projection of a stored tree onto a capture uses the + * producer's scale. See {@link SnapshotCaptureScale}. + */ + captureScale?: SnapshotCaptureScale; /** * iOS: this capture's own command found the session app out of foreground and the runner * activated it before answering, so an earlier observation in the session described whatever held diff --git a/packages/daemon/src/execution/screenshot-runtime-execution.test.ts b/packages/daemon/src/execution/screenshot-runtime-execution.test.ts index bb7a566d25..2d35257410 100644 --- a/packages/daemon/src/execution/screenshot-runtime-execution.test.ts +++ b/packages/daemon/src/execution/screenshot-runtime-execution.test.ts @@ -656,13 +656,18 @@ const TV_PAGE_TREE: RawSnapshotNode[] = [ }, ]; -async function cropProviderTv(page: { +type ProviderTvPage = { producer?: 'cdp-dom' | 'android-uiautomator'; captureScale: number; viewport: { width: number; height: number }; - cropOn?: string; -}) { - const { producer = 'cdp-dom', cropOn = 'label="Save"' } = page; +}; + +async function cropProviderTv(page: ProviderTvPage & { cropOn?: string }) { + return await screenshotProviderTv(page, { screenshotCropOn: page.cropOn ?? 'label="Save"' }); +} + +async function screenshotProviderTv(page: ProviderTvPage, flags: DaemonRequest['flags']) { + const { producer = 'cdp-dom' } = page; const captureScale = snapshotCaptureScaleFrom(page.captureScale); const viewport = snapshotViewportSizeFrom({ x: 0, y: 0, ...page.viewport }); const fixture = screenshotRuntimeFixture({ @@ -676,11 +681,8 @@ async function cropProviderTv(page: { }), }); const session = makeSession('tv', { device: PROVIDER_TV }); - const outPath = path.join(mkdtempForTestSync('provider-tv-crop'), 'tv.png'); - const req = screenshotRequest({ - positionals: [outPath], - flags: { screenshotCropOn: cropOn }, - }); + const outPath = path.join(mkdtempForTestSync('provider-tv-screenshot'), 'tv.png'); + const req = screenshotRequest({ positionals: [outPath], flags }); const resolved = await resolveScreenshotGenericExecution({ hostCwd: process.cwd(), createCommandSurface: testCreateCommandSurface, @@ -720,6 +722,35 @@ test('a provider TV crop clips a node at the page edge to the page, not to the l ]); }); +test("provider TV overlay refs are drawn at the page's capture scale", async () => { + const result = await screenshotProviderTv( + { captureScale: 1.5, viewport: PAGE_720 }, + { overlayRefs: true }, + ); + + expect(result.overlayRefs).toContainEqual( + expect.objectContaining({ + label: 'Save', + rect: { x: 10, y: 10, width: 40, height: 20 }, + overlayRect: { x: 15, y: 15, width: 60, height: 30 }, + }), + ); +}); + +test('provider TV overlay refs stop at the page edge, not at the larger capture', async () => { + const result = await screenshotProviderTv( + { captureScale: 1, viewport: PAGE_720 }, + { overlayRefs: true }, + ); + + expect(result.overlayRefs).toContainEqual( + expect.objectContaining({ + label: 'Tile', + overlayRect: { x: 1200, y: 100, width: 80, height: 200 }, + }), + ); +}); + test('a provider TV crop is refused on a tree the TV plugins did not capture', async () => { await expect( cropProviderTv({ producer: 'android-uiautomator', captureScale: 1, viewport: PAGE_720 }), diff --git a/packages/provider-tizen/README.md b/packages/provider-tizen/README.md index b6efd1a93a..37f241de50 100644 --- a/packages/provider-tizen/README.md +++ b/packages/provider-tizen/README.md @@ -119,8 +119,9 @@ Not yet: system buttons (`home`, menu, volume and channel), `network dump`, `per - **Snapshots are DOM-derived.** Labels come from `aria-label`, `alt`, `title` or own text; rects are CSS px. The screenshot magnifies them by the page's `visualViewport.scale`: a `width=1280` meta viewport on a 1920x1080 TV gives 1.5. `screenshot --crop-on` applies that scale, and refuses - a page whose browser has no `visualViewport` or whose visual viewport is panned. The walk does not - enter iframes or shadow roots. `is text` on an input compares its label, not its value; the value + a page whose browser has no `visualViewport` or whose visual viewport is panned. + `screenshot --overlay-refs` draws ref boxes at the same scale; without one it draws them 1:1. The + walk does not enter iframes or shadow roots. `is text` on an input compares its label, not its value; the value is in `snapshot --raw`. - **System overlays are outside the page.** The Samsung menu, on-screen keyboard and other system layers never appear in `screenshot` or `snapshot`. diff --git a/packages/provider-webos/README.md b/packages/provider-webos/README.md index c088ac60c3..cbdd901261 100644 --- a/packages/provider-webos/README.md +++ b/packages/provider-webos/README.md @@ -116,8 +116,9 @@ in `open`, `power`. webOS has separate Play and Pause key codes and no play-paus 1920x1080 TV, an app at `"resolution": "1280x720"` reports 1.5 yet fills only the top-left 1280x720 of the capture. `screenshot --crop-on` scales rects by the page's `visualViewport.scale`, which is 1 because webOS ignores the meta viewport, and clips the crop to the page. It refuses a - page whose browser has no `visualViewport` or whose visual viewport is panned. The walk does not - enter iframes or shadow roots, so overlays rendered there (for example a dev-server error overlay) + page whose browser has no `visualViewport` or whose visual viewport is panned. + `screenshot --overlay-refs` draws ref boxes at the same scale and stops them at the page edge. The + walk does not enter iframes or shadow roots, so overlays rendered there (for example a dev-server error overlay) appear in screenshots but not in snapshots. - **`click`, `press`, `fill` and `focus` refuse a control marked `[covered]`.** A tap at its centre would land on the element on top, such as a card's own pressable area. `snapshot -i` diff --git a/website/docs/docs/tizen.md b/website/docs/docs/tizen.md index 96b2f4b497..7b30c83b46 100644 --- a/website/docs/docs/tizen.md +++ b/website/docs/docs/tizen.md @@ -108,5 +108,6 @@ Not supported yet: system buttons (`home`, menu, volume, and channel), `network - Snapshots are read from the DOM. Labels come from `aria-label`, `alt`, `title`, or own text. Rects are CSS pixels. The screenshot magnifies them by the page's `visualViewport.scale`: a `width=1280` meta viewport on a 1920x1080 TV gives 1.5. `screenshot --crop-on` applies that scale, and refuses - a page whose browser has no `visualViewport` or whose visual viewport is panned. The walk does not - enter iframes or shadow roots. + a page whose browser has no `visualViewport` or whose visual viewport is panned. + `screenshot --overlay-refs` draws ref boxes at the same scale; without one it draws them 1:1. The + walk does not enter iframes or shadow roots. diff --git a/website/docs/docs/webos.md b/website/docs/docs/webos.md index 5284433ff8..deedff6d7b 100644 --- a/website/docs/docs/webos.md +++ b/website/docs/docs/webos.md @@ -109,8 +109,9 @@ play-pause toggle, so `media-play-pause` is refused; use `media-play` or `media- on a 1920x1080 TV, an app at `"resolution": "1280x720"` reports 1.5 yet fills only the top-left 1280x720 of the capture. `screenshot --crop-on` scales rects by the page's `visualViewport.scale`, which is 1 because webOS ignores the meta viewport, and clips the crop to the page. It refuses a - page whose browser has no `visualViewport` or whose visual viewport is panned. The walk does not - enter iframes or shadow roots. + page whose browser has no `visualViewport` or whose visual viewport is panned. + `screenshot --overlay-refs` draws ref boxes at the same scale and stops them at the page edge. The + walk does not enter iframes or shadow roots. - `click`, `press`, `fill`, and `focus` refuse a control marked `[covered]`, because a tap at its centre would land on the element on top, such as a card's own pressable area. `snapshot -i` still lists it, and `tv-remote` reaches it through focus.