Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(desktop): picture-in-picture stays sharp when the preview is scaled down #31568

Closedwimjan123(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/31568/headopened View on GitHub

Problem

Picture-in-picture on desktop is blurry when its source preview is shown scaled down, in a narrow panel or in the mini preview. The frames keep the full viewport size (for example 1280×800), but small text and 1px lines are smeared, so enlarging the picture-in-picture window only shows a stretched low-detail image. With the panel hidden, the same tab gives sharp frames.

Reproduce on the desktop app:

  1. Open a page with 12 to 16px text in the preview and set a device viewport such as 1280×800, so the panel shows it scaled to fit.
  2. Choose Float preview over chat, then Pop into separate window.
  3. Enlarge the picture-in-picture window. Text stays unreadable.

Change

Cause: Chromium rasters an embedded frame at the scale it is drawn on screen (RemoteFrameView::UpdateCompositingScaleFactor), and picture-in-picture frames are capturePage() copies of that surface (capturePreviewFrame in apps/desktop/src/preview/Manager.ts). JPEG quality is not the cause: a lossless PNG of a guest scaled to 0.6 showed the same loss.

While picture-in-picture is open, a scaled guest gets a matrix3d instead of scale(). The x and y columns keep the on-screen scale and get an extra z term so each column has length 1. Chromium counts those z terms when it computes the raster scale (TryComputeTransform2dScaleComponents in ui/gfx/geometry/transform_util.cc; its comment notes that x/y-to-z terms have no visible effect). The screen drops z, so the guest draws at the same size and position. A small preserve-3d wrapper around the <webview> keeps the z terms until Blink computes the scale. The wrapper now holds the guest's position, so it only contains the guest and the toolbar, resize handles and size label paint as before.

  • browserViewportLayout.ts: resolveBrowserViewportTransform(scale, fullDetail) returns no transform at scale 1, scale() normally, and the matrix3d for full detail.
  • HostedBrowserWebview.tsx: uses it with fullDetail = pictureInPicture and wraps the <webview> in the positioned wrapper.

No capture code changes. Closing picture-in-picture returns the guest to scale().

Cost: while picture-in-picture is open, the guest rasters at full resolution, as it already does when the panel is hidden. The transform is set once when picture-in-picture opens and once when it closes; the capture loop does not toggle anything per frame.

Trade-off: at strong downscales the panel shows a full-resolution raster scaled down by the compositor, which can look more aliased than the low-resolution raster. At 0.6 the panel looks the same (mean pixel difference 0.41/255 over the whole window). At 0.33 small text in the panel has harder, more aliased edges (crops below). This applies only while picture-in-picture is open.

Scope and approval

Bug: #9872 (Refs, not Fixes). The maintainer triage on #21129 closed picture-in-picture blur as a duplicate of #9872 and pointed at capturePage() on the source tab as the frame source. This PR fixes that picture-in-picture case: the same frame size, with full detail when the source is scaled.

Relation to #9916: #9916 fixes saved screenshots, annotation crops and desktop CDP captures. It freezes the displayed preview, removes the host transform, captures, and restores the preview. That fits a single capture, but picture-in-picture captures about 12 times per second, so it would freeze the panel or make it flicker continuously. This PR uses a different mechanism for the picture-in-picture path only and does not touch the capture code that #9916 changes. The two do not conflict.

Not in scope: agent preview_snapshot, the Screenshot button and recordings keep their current behavior when picture-in-picture is closed. The same transform could serve them later, but that would raise raster cost whenever a preview is scaled, which needs its own decision.

Verification

Environment: Linux (Omarchy, Hyprland), display scale 1. Desktop dev build of this branch (Electron 44.10.1, Chromium 152.0.7977.169), with an isolated T3 home, user data dir and ports. The final runs used Xvfb with X11, so the app had a visible, unthrottled window. Fixture: a local page with 12/14/16px text and a 1px black-and-white stripe band, served from 127.0.0.1. No real websites.

Steps per build (main, then this branch): set the preview to 1280×800 in the right panel, open the mini preview, choose Pop into separate window, wait, then read the newest frame that Manager.ts sent to the picture-in-picture page. Then open the preview in the panel again and read the frame. During the measured windows the desktop log showed 0 capture failures.

Metrics on the delivered JPEG frame: "stripe contrast" is the mean difference between neighbouring pixels across the 1px stripes (255 means fully resolved). "Text edge" is the same measure over the text rows.

Source stateGuest scaleFrame sizeStripe contrast, main → this PRText edge, main → this PR
Mini preview0.251280×800 → 1280×8001.1 → 2552.31 → 20.37
Narrow panel0.331280×800 → 1280×80068.8 → 2553.52 → 20.37
Panel, 700×450 viewport0.60700×450 → 700×45053.1 → 2558.82 → 20.38

Picture-in-picture window enlarged to 1280×800, mini preview source (top-left crop, 100%):

mainthis PR
main, mini preview sourcethis PR, mini preview source

Same, narrow panel source:

mainthis PR
main, narrow panel sourcethis PR, narrow panel source

Panel display while picture-in-picture is open (crop scaled up 3×):

Scalemainthis PR
0.60main, panel at 0.60this PR, panel at 0.60
0.33main, panel at 0.33this PR, panel at 0.33

Other checks:

  • Closing picture-in-picture: the guest transform returns to scale(0.25), and the 3D wrapper style is removed.
  • Standalone Electron 44.10.1 page with a <webview>: scale(0.6) gave an 800×600 capture with stripe contrast 51. The matrix3d in a preserve-3d wrapper gave 255 at the same size. A CDP mouse click at host point (320, 270) reached the guest at (500, 400) in both cases. elementFromPoint returned the <webview>. A later sibling that overlaps the guest still painted on top.
  • vp test run src/browser/browserViewportLayout.test.ts (apps/web): 3 passed. The new case checks the matrix layout, so a revert to scale() or a wrong column order fails.
  • vp run check:changed: format, lint, knip, typecheck (2 packages), tests and react compiler pass.

Not checked:

  • Clicks and scrolls in the panel inside the dev app. Synthetic CDP clicks did not reach the guest there on main either, so that run proves nothing. Input mapping was checked only in the standalone Electron page.
  • macOS, Windows, display scales other than 1, and a Wayland run with the final code. An earlier Wayland run reproduced the blur and showed sharp frames from the narrow panel, but frames went stale when the dev window was on a hidden workspace.
  • Long sessions, GPU memory use at very small scales, and pages with their own 3D transforms.

Reviewed with sol-loop: 2 rounds with GPT 6.1 Sol on high. Code: approved. PR text: requested changes applied.

Written by Claude Opus 5.5 in Claude Code.

🤖 Generated with Claude Code

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    2. Approvability

      Verdict: Approved at 713e130

      Macroscope's review found this PR approvable — The change preserves sharp picture-in-picture captures when the desktop preview is scaled down. Its effects are confined to preview rendering, with existing webview configuration and product defaults unchanged.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    3. Explain Change →

      No actionable comments were generated in the recent review.

      Walkthrough

      The change adds a helper that selects a viewport transform based on scale and full-detail mode. The hosted browser webview uses the resolved transform and positions the webview inside a viewport wrapper.

      Changes: Viewport scaling

      Layer / File(s)Summary
      Transform calculation and tests
      apps/web/src/browser/browserViewportLayout.ts, apps/web/src/browser/browserViewportLayout.test.ts
      resolveBrowserViewportTransform returns no transform at scale 1 or above. Below 1, it returns scale() or, in full-detail mode, a matrix3d() transform. Tests check these outputs.
      Hosted webview rendering
      apps/web/src/browser/HostedBrowserWebview.tsx
      The webview resolves its transform from viewport scale and picture-in-picture state. A positioned wrapper holds the webview and uses preserve-3d when picture-in-picture is active and a transform exists.

      Estimated code review effort: 2 (Simple) | ~12 minutes

      Change: Bug fix

      Possibly related issues

      • pingdotgg/t3code#9872: The issue reports detail loss in narrow and mini previews. This change applies a full-detail transform when picture-in-picture is active.

      Possibly related PRs

      Suggested labels: bugfix, size:M

      Suggested reviewers: maria-rcks, macroscopeapp, juliusmarminge

      Merge Risk: Minimal · up to 713e1

      No merge-blocking issue was established. Guest input behavior should receive normal platform testing.

      Pre-merge checks: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe title names the main change: picture-in-picture frames stay sharp when the source preview is scaled down. The changeset adds resolveBrowserViewportTransform, which uses a matrix3d transform with a…
      Description checkPassedThe description follows the template sections (Problem, Change, Scope and approval, Verification). The Problem section gives the symptom, expected behavior and reproduction steps. The Change section e…
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Full details: Title check

      Explanation

      The title names the main change: picture-in-picture frames stay sharp when the source preview is scaled down. The changeset adds resolveBrowserViewportTransform, which uses a matrix3d transform with a preserve-3d wrapper while picture-in-picture is open. The title is specific, and the author's description and objectives state the same goal. The title is a single conventional-commit style sentence and it stays on the main change.

      Full details: Description check

      Explanation

      The description follows the template sections (Problem, Change, Scope and approval, Verification). The Problem section gives the symptom, expected behavior and reproduction steps. The Change section explains the cause and the fix, and it names the modified files. The Scope section links bug #9872 and the maintainer triage on #21129, and it explains how the change relates to #9916. The Verification section lists the environment, the measured metrics, before and after images, the tests run, and what was not checked. The description is thorough. The template's comment block at the top of the description was not kept, which does not affect the check.

      Finishing Touches

      Generate unit tests (beta)

      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      Tip

      🎯 Customize your risk assessments

      Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

      ✅ Automatic approvals, with your rules

      Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

      Recent review info

      Run configuration

      Commits

      Reviewing files that changed from the base of the PR and between 337c5858c33bc7f8f6dfd391a2c279fa0f7e4d17 and 713e1304d3ca3c10335db92c62106e8e6c5b459f.

      Files selected for processing: 3

      • apps/web/src/browser/HostedBrowserWebview.tsx
      • apps/web/src/browser/browserViewportLayout.test.ts
      • apps/web/src/browser/browserViewportLayout.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

    4. Wladefant(GitHub profile, opens in a new tab) approved these changes

      APPROVED — exact-head source review

      Reviewer: GPT-6 (ChatGPT GitHub integration). Source author identifies Claude Opus 5.5 / Claude Code. This is a separate reviewer and not author self-review.

      Reviewed exact candidate 713e1304d3ca3c10335db92c62106e8e6c5b459f against main@337c5858c33bc7f8f6dfd391a2c279fa0f7e4d17, all three changed files:

      • apps/web/src/browser/HostedBrowserWebview.tsx
      • apps/web/src/browser/browserViewportLayout.ts
      • apps/web/src/browser/browserViewportLayout.test.ts

      The source change is narrowly gated by pictureInPicture. The layout helper guarantees the input scale is positive and at most 1; the full-detail matrix keeps the projected x/y screen scale and top-left placement while making each transformed basis vector unit length for Chromium raster-scale calculation. The wrapper preserves the prior viewport origin and visible bounds; webview identity, partition, preload, popup permission, ref, and data attributes remain on the same guest. Exiting picture-in-picture returns to the ordinary scale() transform; scale 1 remains untransformed. The helper's regression checks both ordinary and full-detail transforms, including unit-length basis vectors.

      I found no scoped P1/P2 source-correctness defect in this exact diff. The PR contains before/after captures and measured frame detail, plus a standalone Electron hit-test check. Exact-head normal Actions #42066380601 succeeded: browserViewportLayout.test.ts 3/3; Web suite 5,486/5,486 across 515 files; Test job 246/246 across 35 files; Lint, Build/Typecheck and Check succeeded. Test Server shards, Rust, Release Smoke and Mobile Native Static Analysis were skipped.

      Acceptance boundary: packaged macOS/Windows, final-code Wayland, non-1× display scales, in-app click/scroll behavior, and long-session/GPU soak remain NOT RUN. These are integration/platform follow-ups, not a blanket source-merge veto; this review does not claim live host or physical acceptance.

      Integration note: OPEN PRs #27879 and #28669 also touch HostedBrowserWebview.tsx on their own author branches. Their inspected hunks are adjacent/disjoint, but neither delta is in this candidate's main-based tree. This approval covers only #31568's isolated diff; the landing owner must preserve/reconcile those independent changes when composing current main.

      No source/branch writes, CI dispatch, merge, install, device action, or landing custody were performed by this reviewer.

    5. tanstack-source added labelsbug

    6. A data point for this one: the same matrix3d placement also fixes blurred agent captures. Turned on while the desktop host paints a guest for preview_snapshot or a recording, it made agent snapshots of the default panel and the mini player sharp on macOS, with the T3 window in front, covered or minimized (stripe score 0.19 and 0.00 on main, 1.00 with it). Setup and numbers: https://github.com/pingdotgg/t3code/issues/9872#issuecomment-6813114747

      I have that change on a branch that uses your commit unchanged.

    7. Thanks for this. Main has the change since #36944.

      Main now preserves full raster detail while picture-in-picture is open, including narrow-panel and mini-preview sources, and restores ordinary scaling afterward. I compared all three changed files: the transform helper and its test are byte-identical on main, and HostedBrowserWebview includes the same positioned preserve-3d wrapper and transform activation for picture-in-picture.

      Closing as superseded.

    8. juliusmarminge closed this

Sign in to join this conversation.