Skip to content

Commit 0d99f57

Browse files
committed
fix(desktop): preserve preview viewport when app is zoomed
1 parent 357b8d5 commit 0d99f57

2 files changed

Lines changed: 24 additions & 21 deletions

File tree

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ type TestDisplayMediaHandler = (
217217

218218
interface TestHostWebContents {
219219
readonly id: number;
220+
readonly getZoomFactor: () => number;
220221
readonly mainFrame: { readonly frameTreeNodeId: number };
221222
readonly executeJavaScript: ReturnType<typeof vi.fn>;
222223
readonly isDestroyed: () => boolean;
@@ -234,6 +235,7 @@ const makeTestHostWebContents = (): TestHostWebContents => {
234235
let handler: TestDisplayMediaHandler | undefined;
235236
return {
236237
id: 7,
238+
getZoomFactor: () => 1,
237239
mainFrame: { frameTreeNodeId: 7 },
238240
executeJavaScript: vi.fn(async () => true),
239241
isDestroyed: () => false,
@@ -481,7 +483,7 @@ describe("PreviewManager", () => {
481483
Effect.gen(function* () {
482484
const preview = makeFaviconWebContents();
483485
const sendInputEvent = vi.fn();
484-
const hostWebContents = { sendInputEvent };
486+
const hostWebContents = { sendInputEvent, getZoomFactor: () => 1 };
485487
Object.assign(preview.webContents, { hostWebContents });
486488
fromId.mockReturnValue(preview.webContents);
487489
yield* manager.setMainWindow({
@@ -1261,13 +1263,15 @@ describe("PreviewManager", () => {
12611263
effectIt.effect("keeps the tab's own zoom instead of the guest's reported zoom", () =>
12621264
withManager((manager) =>
12631265
Effect.gen(function* () {
1266+
const hostWebContents = { getZoomFactor: () => 1.44 };
12641267
let effectiveZoom = 0.9;
12651268
let zoomReadable = true;
12661269
let url = "https://example.com";
12671270
const listeners = new Map<string, (...args: unknown[]) => void>();
12681271
const setZoomFactor = vi.fn();
12691272
fromId.mockReturnValue({
12701273
id: 42,
1274+
hostWebContents,
12711275
isDestroyed: () => false,
12721276
getType: () => "webview",
12731277
getURL: () => url,
@@ -1308,7 +1312,7 @@ describe("PreviewManager", () => {
13081312
yield* manager.registerWebview("tab_zoom", 42);
13091313

13101314
expect(states.at(-1)?.zoomFactor).toBe(1);
1311-
expect(setZoomFactor).toHaveBeenCalledWith(1);
1315+
expect(setZoomFactor).toHaveBeenCalledWith(1.44);
13121316

13131317
// An app zoom leaves the guest reporting the inherited level. Navigating
13141318
// must not adopt it as the preview's zoom.
@@ -1326,7 +1330,7 @@ describe("PreviewManager", () => {
13261330

13271331
// Only the preview's own zoom controls move it.
13281332
yield* manager.zoomIn("tab_zoom");
1329-
expect(setZoomFactor).toHaveBeenCalledWith(1.1);
1333+
expect(setZoomFactor).toHaveBeenCalledWith(1.1 * 1.44);
13301334
expect(states.at(-1)?.zoomFactor).toBe(1.1);
13311335

13321336
zoomReadable = false;
@@ -1338,6 +1342,7 @@ describe("PreviewManager", () => {
13381342
const replacementSetZoomFactor = vi.fn();
13391343
fromId.mockReturnValue({
13401344
id: 43,
1345+
hostWebContents,
13411346
isDestroyed: () => false,
13421347
getType: () => "webview",
13431348
getURL: () => url,
@@ -1365,7 +1370,7 @@ describe("PreviewManager", () => {
13651370

13661371
yield* manager.registerWebview("tab_zoom", 43);
13671372

1368-
expect(replacementSetZoomFactor).toHaveBeenCalledWith(1.1);
1373+
expect(replacementSetZoomFactor).toHaveBeenCalledWith(1.1 * 1.44);
13691374
expect(states.at(-1)?.zoomFactor).toBe(1.1);
13701375
}),
13711376
),
@@ -1376,9 +1381,11 @@ describe("PreviewManager", () => {
13761381
effectIt.effect("re-applies each tab's own zoom when the app window zooms", () =>
13771382
withManager((manager) =>
13781383
Effect.gen(function* () {
1384+
let hostZoom = 1;
13791385
const setZoomFactor = vi.fn();
13801386
fromId.mockReturnValue({
13811387
id: 42,
1388+
hostWebContents: { getZoomFactor: () => hostZoom },
13821389
isDestroyed: () => false,
13831390
getType: () => "webview",
13841391
getURL: () => "https://example.com",
@@ -1409,10 +1416,11 @@ describe("PreviewManager", () => {
14091416
yield* manager.zoomIn("tab_reapply");
14101417
setZoomFactor.mockClear();
14111418

1412-
yield* manager.reapplyZoom();
1413-
1414-
expect(setZoomFactor).toHaveBeenCalledTimes(1);
1415-
expect(setZoomFactor).toHaveBeenCalledWith(1.1);
1419+
for (const zoom of [1.44, 0.8, 1]) {
1420+
hostZoom = zoom;
1421+
yield* manager.reapplyZoom();
1422+
expect(setZoomFactor).toHaveBeenLastCalledWith(1.1 * hostZoom);
1423+
}
14161424
}),
14171425
),
14181426
);

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
923923
const wc = webContents.fromId(tab.webContentsId);
924924
if (!wc || wc.isDestroyed()) return;
925925
yield* attempt({ operation: "assertTabZoom", tabId, webContentsId: wc.id }, () =>
926-
wc.setZoomFactor(tab.zoomFactor),
926+
wc.setZoomFactor(tab.zoomFactor * (wc.hostWebContents?.getZoomFactor() ?? 1)),
927927
).pipe(Effect.ignore);
928928
});
929929

@@ -2125,12 +2125,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
21252125
) {
21262126
return yield* new PreviewTabNotFoundError({ tabId });
21272127
}
2128-
// Always assert the tab's own zoom rather than reading the guest's: a guest
2129-
// attaching while the app UI is zoomed starts at the embedder's inherited
2130-
// zoom level, which is not the preview's zoom. Done before the guest is
2131-
// published so it never paints a frame at the inherited zoom.
2128+
// The webview's box is in host CSS pixels. Include host zoom so the guest's
2129+
// CSS viewport matches the requested size, even when the app UI is zoomed.
21322130
yield* attempt({ operation: "registerWebview.restoreZoomFactor", tabId, webContentsId }, () =>
2133-
wc.setZoomFactor(currentTab.zoomFactor),
2131+
wc.setZoomFactor(currentTab.zoomFactor * (wc.hostWebContents?.getZoomFactor() ?? 1)),
21342132
);
21352133
// A replacement guest attaches unmuted, so reassert the tab's mute before it
21362134
// is published rather than letting it emit audio the user already silenced.
@@ -2550,10 +2548,8 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
25502548
});
25512549

25522550
/**
2553-
* Chromium hands every guest `<webview>` the embedder's zoom level, so zooming
2554-
* the app UI drags the previewed page along with it. The preview browser owns
2555-
* its own zoom factor, so re-assert it on each attached guest whenever the main
2556-
* window's zoom changes (see DesktopWindow.zoomMain).
2551+
* Re-assert each tab's zoom relative to the host after app zoom changes
2552+
* (see DesktopWindow.zoomMain), keeping its CSS viewport stable.
25572553
*/
25582554
const reapplyZoom = Effect.fn("PreviewManager.reapplyZoom")(function* () {
25592555
const tabIds = Array.from((yield* SynchronizedRef.get(tabsRef)).keys());
@@ -2572,7 +2568,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
25722568
const wc = webContents.fromId(tab.webContentsId);
25732569
if (wc && !wc.isDestroyed()) {
25742570
yield* attempt({ operation: "applyZoom", tabId, webContentsId: wc.id }, () =>
2575-
wc.setZoomFactor(next),
2571+
wc.setZoomFactor(next * (wc.hostWebContents?.getZoomFactor() ?? 1)),
25762572
);
25772573
}
25782574
}
@@ -4507,8 +4503,7 @@ export class PreviewManager extends Context.Service<
45074503
readonly zoomIn: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
45084504
readonly zoomOut: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
45094505
readonly resetZoom: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
4510-
// Re-applies every attached guest's own zoom factor, undoing the zoom level
4511-
// Chromium inherits from the embedder when the app UI zooms.
4506+
// Re-applies each tab's zoom relative to its host after app UI zoom changes.
45124507
readonly reapplyZoom: () => Effect.Effect<void>;
45134508
readonly hardReload: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
45144509
readonly setColorScheme: (

0 commit comments

Comments
 (0)