Skip to content

Commit 3916a4d

Browse files
fix(desktop): app zoom no longer zooms the preview browser
Zooming the app UI (View → Zoom In/Out) also zoomed whatever the preview browser was showing, so there was no way to shrink T3's chrome without shrinking the previewed page with it. Chromium hands every guest `<webview>` the embedder's zoom level, and the preview tab then read that level back off the guest, which turned an app zoom into the preview's own zoom and left the zoom indicator reporting it. The tab's zoom factor is now the only source of truth: it is asserted onto the guest whenever a webview attaches, re-asserted after every app zoom, and never read back off the guest. Model: Claude Opus 5 (1M context). Harness: Claude Code inside T3 Code. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent e25021a commit 3916a4d

4 files changed

Lines changed: 161 additions & 36 deletions

File tree

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

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,10 @@ describe("PreviewManager", () => {
979979
),
980980
);
981981

982-
effectIt.effect("mirrors Electron's effective zoom across registration and navigation", () =>
982+
// The guest reports whatever zoom level Chromium handed it from the app
983+
// window, so the tab's own zoom is the source of truth in both directions:
984+
// asserted onto every guest, never read back off one.
985+
effectIt.effect("keeps the tab's own zoom instead of the guest's reported zoom", () =>
983986
withManager((manager) =>
984987
Effect.gen(function* () {
985988
let effectiveZoom = 0.9;
@@ -1025,18 +1028,13 @@ describe("PreviewManager", () => {
10251028
yield* manager.createTab("tab_zoom");
10261029
yield* manager.registerWebview("tab_zoom", 42);
10271030

1028-
expect(states.at(-1)?.zoomFactor).toBe(0.9);
1029-
expect(setZoomFactor).not.toHaveBeenCalled();
1031+
expect(states.at(-1)?.zoomFactor).toBe(1);
1032+
expect(setZoomFactor).toHaveBeenCalledWith(1);
10301033

1031-
effectiveZoom = 1.25;
1032-
listeners.get("did-navigate")?.();
1033-
yield* Effect.yieldNow;
1034-
1035-
expect(states.at(-1)?.zoomFactor).toBe(1.25);
1036-
expect(setZoomFactor).not.toHaveBeenCalled();
1037-
1038-
zoomReadable = false;
1039-
url = "https://example.com/after-zoom-read-failed";
1034+
// An app zoom leaves the guest reporting the inherited level. Navigating
1035+
// must not adopt it as the preview's zoom.
1036+
effectiveZoom = 0.8;
1037+
url = "https://example.com/after-app-zoom";
10401038
listeners.get("did-navigate")?.();
10411039
yield* Effect.yieldNow;
10421040

@@ -1045,7 +1043,18 @@ describe("PreviewManager", () => {
10451043
url,
10461044
title: "Example",
10471045
});
1048-
expect(states.at(-1)?.zoomFactor).toBe(1.25);
1046+
expect(states.at(-1)?.zoomFactor).toBe(1);
1047+
1048+
// Only the preview's own zoom controls move it.
1049+
yield* manager.zoomIn("tab_zoom");
1050+
expect(setZoomFactor).toHaveBeenCalledWith(1.1);
1051+
expect(states.at(-1)?.zoomFactor).toBe(1.1);
1052+
1053+
zoomReadable = false;
1054+
listeners.get("did-navigate")?.();
1055+
yield* Effect.yieldNow;
1056+
1057+
expect(states.at(-1)?.zoomFactor).toBe(1.1);
10491058

10501059
const replacementSetZoomFactor = vi.fn();
10511060
fromId.mockReturnValue({
@@ -1074,8 +1083,51 @@ describe("PreviewManager", () => {
10741083

10751084
yield* manager.registerWebview("tab_zoom", 43);
10761085

1077-
expect(replacementSetZoomFactor).toHaveBeenCalledWith(1.25);
1078-
expect(states.at(-1)?.zoomFactor).toBe(1.25);
1086+
expect(replacementSetZoomFactor).toHaveBeenCalledWith(1.1);
1087+
expect(states.at(-1)?.zoomFactor).toBe(1.1);
1088+
}),
1089+
),
1090+
);
1091+
1092+
// Zooming the app UI pushes the window's zoom level onto every guest, so the
1093+
// preview has to be put back at the zoom the user gave it.
1094+
effectIt.effect("re-applies each tab's own zoom when the app window zooms", () =>
1095+
withManager((manager) =>
1096+
Effect.gen(function* () {
1097+
const setZoomFactor = vi.fn();
1098+
fromId.mockReturnValue({
1099+
id: 42,
1100+
isDestroyed: () => false,
1101+
getType: () => "webview",
1102+
getURL: () => "https://example.com",
1103+
getTitle: () => "Example",
1104+
isLoading: () => false,
1105+
getZoomFactor: () => 1,
1106+
setZoomFactor,
1107+
on: vi.fn(),
1108+
off: vi.fn(),
1109+
ipc: { on: vi.fn(), off: vi.fn() },
1110+
send: webviewSend,
1111+
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
1112+
setWindowOpenHandler: vi.fn(),
1113+
debugger: {
1114+
isAttached: () => false,
1115+
attach: vi.fn(),
1116+
sendCommand: vi.fn(async () => undefined),
1117+
on: vi.fn(),
1118+
off: vi.fn(),
1119+
},
1120+
} as never);
1121+
1122+
yield* manager.createTab("tab_reapply");
1123+
yield* manager.registerWebview("tab_reapply", 42);
1124+
yield* manager.zoomIn("tab_reapply");
1125+
setZoomFactor.mockClear();
1126+
1127+
yield* manager.reapplyZoom();
1128+
1129+
expect(setZoomFactor).toHaveBeenCalledTimes(1);
1130+
expect(setZoomFactor).toHaveBeenCalledWith(1.1);
10791131
}),
10801132
),
10811133
);

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

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,10 +1305,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
13051305
confirmedNavigation = false,
13061306
) {
13071307
if (wc.isDestroyed()) return;
1308-
const zoomFactor = yield* attempt(
1309-
{ operation: "syncWebContentsState.getZoomFactor", tabId, webContentsId: wc.id },
1310-
() => wc.getZoomFactor(),
1311-
).pipe(Effect.option);
13121308
const computedNavStatus = computeNavStatus(wc);
13131309
const canGoBack = wc.navigationHistory.canGoBack();
13141310
const canGoForward = wc.navigationHistory.canGoForward();
@@ -1338,7 +1334,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
13381334
navStatus,
13391335
canGoBack,
13401336
canGoForward,
1341-
...(Option.isSome(zoomFactor) ? { zoomFactor: zoomFactor.value } : {}),
1337+
// zoomFactor is deliberately not read back from the guest: Chromium
1338+
// reports the level it inherited from the app window, so mirroring it
1339+
// would turn an app zoom into the preview's own zoom.
13421340
updatedAt,
13431341
};
13441342
return [
@@ -1716,11 +1714,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
17161714
const annotationTheme = yield* Ref.get(annotationThemeRef);
17171715
const currentAttachment = attached.get(webContentsId);
17181716
if (tab.webContentsId === webContentsId && currentAttachment?.webContents === wc) {
1719-
const zoomFactor = yield* attempt(
1720-
{ operation: "registerWebview.getZoomFactor", tabId, webContentsId },
1721-
() => wc.getZoomFactor(),
1717+
yield* attempt({ operation: "registerWebview.restoreZoomFactor", tabId, webContentsId }, () =>
1718+
wc.setZoomFactor(tab.zoomFactor),
17221719
);
1723-
yield* update(tabId, { zoomFactor });
1720+
yield* update(tabId, { zoomFactor: tab.zoomFactor });
17241721
yield* attempt({ operation: "registerWebview.sendTheme", tabId, webContentsId }, () =>
17251722
wc.send(ANNOTATION_THEME_CHANNEL, annotationTheme),
17261723
);
@@ -1749,18 +1746,16 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
17491746
) {
17501747
return yield* new PreviewTabNotFoundError({ tabId });
17511748
}
1752-
const zoomFactor =
1753-
replacedWebContentsId !== null
1754-
? yield* attempt(
1755-
{ operation: "registerWebview.restoreZoomFactor", tabId, webContentsId },
1756-
() => {
1757-
wc.setZoomFactor(currentTab.zoomFactor);
1758-
return currentTab.zoomFactor;
1759-
},
1760-
)
1761-
: yield* attempt({ operation: "registerWebview.getZoomFactor", tabId, webContentsId }, () =>
1762-
wc.getZoomFactor(),
1763-
);
1749+
// Always assert the tab's own zoom rather than reading the guest's: a guest
1750+
// attaching while the app UI is zoomed starts at the embedder's inherited
1751+
// zoom level, which is not the preview's zoom.
1752+
const zoomFactor = yield* attempt(
1753+
{ operation: "registerWebview.restoreZoomFactor", tabId, webContentsId },
1754+
() => {
1755+
wc.setZoomFactor(currentTab.zoomFactor);
1756+
return currentTab.zoomFactor;
1757+
},
1758+
);
17641759
yield* attachListeners(tabId, wc);
17651760
const registeredAt = yield* currentIso;
17661761
const registration = yield* SynchronizedRef.modifyEffect(tabsRef, (tabs) =>
@@ -2099,6 +2094,28 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
20992094
);
21002095
});
21012096

2097+
/**
2098+
* Chromium hands every guest `<webview>` the embedder's zoom level, so zooming
2099+
* the app UI drags the previewed page along with it. The preview browser owns
2100+
* its own zoom factor, so re-assert it on each attached guest whenever the main
2101+
* window's zoom changes (see DesktopWindow.zoomMain).
2102+
*/
2103+
const reapplyZoom = Effect.fn("PreviewManager.reapplyZoom")(function* () {
2104+
const tabs = yield* SynchronizedRef.get(tabsRef);
2105+
yield* Effect.forEach(
2106+
tabs.values(),
2107+
(tab) => {
2108+
if (tab.webContentsId === null) return Effect.void;
2109+
const wc = webContents.fromId(tab.webContentsId);
2110+
if (!wc || wc.isDestroyed()) return Effect.void;
2111+
return attempt({ operation: "reapplyZoom", tabId: tab.tabId, webContentsId: wc.id }, () =>
2112+
wc.setZoomFactor(tab.zoomFactor),
2113+
).pipe(Effect.ignore);
2114+
},
2115+
{ discard: true },
2116+
);
2117+
});
2118+
21022119
const applyZoom = Effect.fn("PreviewManager.applyZoom")(function* (
21032120
tabId: string,
21042121
transform: (current: number) => number,
@@ -3476,6 +3493,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
34763493
openPictureInPicture,
34773494
openDevTools,
34783495
pickElement,
3496+
reapplyZoom,
34793497
refresh,
34803498
registerWebview,
34813499
resetZoom: (tabId: string) => applyZoom(tabId, () => DEFAULT_ZOOM_FACTOR),
@@ -3774,6 +3792,9 @@ export class PreviewManager extends Context.Service<
37743792
readonly zoomIn: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
37753793
readonly zoomOut: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
37763794
readonly resetZoom: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
3795+
// Re-applies every attached guest's own zoom factor, undoing the zoom level
3796+
// Chromium inherits from the embedder when the app UI zooms.
3797+
readonly reapplyZoom: () => Effect.Effect<void>;
37773798
readonly hardReload: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
37783799
readonly setColorScheme: (
37793800
tabId: string,
@@ -3874,6 +3895,7 @@ export const make = Effect.gen(function* PreviewManagerMake() {
38743895
zoomIn: operations.zoomIn,
38753896
zoomOut: operations.zoomOut,
38763897
resetZoom: operations.resetZoom,
3898+
reapplyZoom: operations.reapplyZoom,
38773899
hardReload: operations.hardReload,
38783900
setColorScheme: operations.setColorScheme,
38793901
openDevTools: operations.openDevTools,

‎apps/desktop/src/window/DesktopWindow.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ const environmentInput = {
6161
function makeFakeBrowserWindow() {
6262
const windowListeners = new Map<string, (...args: readonly unknown[]) => void>();
6363
const webContentsListeners = new Map<string, (...args: readonly unknown[]) => void>();
64+
let zoomLevel = 0;
6465
const webContents = {
6566
copyImageAt: vi.fn(),
6667
getURL: vi.fn(() => "t3code-dev://app/"),
68+
getZoomLevel: vi.fn(() => zoomLevel),
69+
setZoomLevel: vi.fn((level: number) => {
70+
zoomLevel = level;
71+
}),
6772
isLoadingMainFrame: vi.fn(() => false),
6873
on: vi.fn((eventName: string, listener: (...args: readonly unknown[]) => void) => {
6974
webContentsListeners.set(eventName, listener);
@@ -116,6 +121,7 @@ function makeFakeBrowserWindow() {
116121
openDevTools: webContents.openDevTools,
117122
reload: webContents.reload,
118123
send: webContents.send,
124+
setZoomLevel: webContents.setZoomLevel,
119125
setAutoHideCursor: window.setAutoHideCursor,
120126
webContentsListeners,
121127
windowListeners,
@@ -186,6 +192,7 @@ function makeTestLayer(input: {
186192
bounds: DesktopAppSettings.DesktopWindowBounds,
187193
) => Effect.Effect<void>;
188194
readonly openedExternalUrls?: unknown[];
195+
readonly previewZoomReapplies?: number[];
189196
}) {
190197
let desktopSettings = input.desktopSettings ?? DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS;
191198
const desktopAppSettingsLayer = Layer.succeed(DesktopAppSettings.DesktopAppSettings, {
@@ -264,6 +271,10 @@ function makeTestLayer(input: {
264271
setMainWindow: () => Effect.void,
265272
isBrowserPartition: (partition) => partition.startsWith("persist:t3code-preview-"),
266273
getBrowserPartition: () => Effect.succeed("persist:t3code-preview-test"),
274+
reapplyZoom: () =>
275+
Effect.sync(() => {
276+
input.previewZoomReapplies?.push(input.window.webContents.getZoomLevel());
277+
}),
267278
}),
268279
),
269280
),
@@ -483,6 +494,42 @@ describe("DesktopWindow", () => {
483494
}),
484495
);
485496

497+
// Chromium hands the main window's zoom level down to embedded preview
498+
// guests, so every app zoom has to put the preview browser back at its own
499+
// zoom or zooming the UI drags the previewed page with it.
500+
it.effect("restores the preview browser's own zoom after zooming the app", () =>
501+
Effect.gen(function* () {
502+
const fakeWindow = makeFakeBrowserWindow();
503+
const createCount = yield* Ref.make(0);
504+
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
505+
const previewZoomReapplies: number[] = [];
506+
const layer = makeTestLayer({
507+
window: fakeWindow.window,
508+
createCount,
509+
mainWindow,
510+
previewZoomReapplies,
511+
});
512+
513+
yield* Effect.gen(function* () {
514+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
515+
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
516+
517+
yield* desktopWindow.zoomMain("out");
518+
yield* desktopWindow.zoomMain("out");
519+
yield* desktopWindow.zoomMain("in");
520+
yield* desktopWindow.zoomMain("reset");
521+
522+
assert.deepEqual(
523+
fakeWindow.setZoomLevel.mock.calls.map(([level]) => level),
524+
[-0.5, -1, -0.5, 0],
525+
);
526+
// Recorded after the window level moved, so the preview is put back at
527+
// its own zoom on every step rather than left on the inherited one.
528+
assert.deepEqual(previewZoomReapplies, [-0.5, -1, -0.5, 0]);
529+
}).pipe(Effect.provide(layer));
530+
}),
531+
);
532+
486533
it.effect("uses the persisted main window bounds when opening the window", () =>
487534
Effect.gen(function* () {
488535
const fakeWindow = makeFakeBrowserWindow();

‎apps/desktop/src/window/DesktopWindow.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,10 @@ export const make = Effect.gen(function* () {
855855
webContents.setZoomLevel(
856856
direction === "reset" ? 0 : webContents.getZoomLevel() + (direction === "in" ? 0.5 : -0.5),
857857
);
858+
// Chromium pushes the new level down to embedded guests, which would zoom
859+
// the previewed page along with the app UI. The preview browser keeps its
860+
// own zoom, so put each guest back where the preview left it.
861+
yield* previewManager.reapplyZoom();
858862
}),
859863
syncAppearance: Effect.gen(function* () {
860864
const shouldUseDarkColors = yield* electronTheme.shouldUseDarkColors;

0 commit comments

Comments
 (0)