Skip to content

Commit 0a46468

Browse files
fix(desktop): apply preview zoom from live tab state
Review follow-up. Both the re-registration fast path and reapplyZoom applied a tab snapshot taken before an await, so a zoom action landing in that window could be rolled back on the guest, and the fast path wrote its stale factor back over tab state. Zoom now goes through one helper that reads the tab and its guest at the moment it applies, the fast path no longer republishes unchanged state, and a swapped-in guest settles on the committed factor. Model: Claude Opus 5 (1M context). Harness: Claude Code inside T3 Code. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 3916a4d commit 0a46468

2 files changed

Lines changed: 81 additions & 25 deletions

File tree

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,58 @@ describe("PreviewManager", () => {
11321132
),
11331133
);
11341134

1135+
// did-attach and dom-ready both re-register the guest that is already
1136+
// attached, and a guest that just inherited the app window's zoom needs its
1137+
// own back — without that round trip republishing tab state.
1138+
effectIt.effect("re-asserts the tab's zoom when the active guest registers again", () =>
1139+
withManager((manager) =>
1140+
Effect.gen(function* () {
1141+
const setZoomFactor = vi.fn();
1142+
fromId.mockReturnValue({
1143+
id: 42,
1144+
isDestroyed: () => false,
1145+
getType: () => "webview",
1146+
getURL: () => "https://example.com",
1147+
getTitle: () => "Example",
1148+
isLoading: () => false,
1149+
getZoomFactor: () => 1,
1150+
setZoomFactor,
1151+
on: vi.fn(),
1152+
off: vi.fn(),
1153+
ipc: { on: vi.fn(), off: vi.fn() },
1154+
send: webviewSend,
1155+
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
1156+
setWindowOpenHandler: vi.fn(),
1157+
debugger: {
1158+
isAttached: () => false,
1159+
attach: vi.fn(),
1160+
sendCommand: vi.fn(async () => undefined),
1161+
on: vi.fn(),
1162+
off: vi.fn(),
1163+
},
1164+
} as never);
1165+
const states: PreviewManager.PreviewTabState[] = [];
1166+
yield* manager.subscribeStateChanges((_tabId, state) =>
1167+
Effect.sync(() => {
1168+
states.push(state);
1169+
}),
1170+
);
1171+
1172+
yield* manager.createTab("tab_reregister_zoom");
1173+
yield* manager.registerWebview("tab_reregister_zoom", 42);
1174+
yield* manager.zoomIn("tab_reregister_zoom");
1175+
setZoomFactor.mockClear();
1176+
const publishedBefore = states.length;
1177+
1178+
yield* manager.registerWebview("tab_reregister_zoom", 42);
1179+
1180+
expect(setZoomFactor).toHaveBeenCalledWith(1.1);
1181+
expect(states.length).toBe(publishedBefore);
1182+
expect(states.at(-1)?.zoomFactor).toBe(1.1);
1183+
}),
1184+
),
1185+
);
1186+
11351187
effectIt.effect("emulates prefers-color-scheme and re-applies it across webview swaps", () =>
11361188
withManager((manager) =>
11371189
Effect.gen(function* () {

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

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,22 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
647647
if (Option.isSome(next)) yield* emit(tabId, next.value);
648648
});
649649

650+
/**
651+
* Pushes a tab's zoom factor onto whichever guest it currently owns, reading
652+
* both at call time. Anything that applies zoom after an await goes through
653+
* here: a snapshot taken before the await can be older than a zoom action that
654+
* landed in between, and re-applying it would roll that action back.
655+
*/
656+
const assertTabZoom = Effect.fn("PreviewManager.assertTabZoom")(function* (tabId: string) {
657+
const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId);
658+
if (!tab || tab.webContentsId == null) return;
659+
const wc = webContents.fromId(tab.webContentsId);
660+
if (!wc || wc.isDestroyed()) return;
661+
yield* attempt({ operation: "assertTabZoom", tabId, webContentsId: wc.id }, () =>
662+
wc.setZoomFactor(tab.zoomFactor),
663+
).pipe(Effect.ignore);
664+
});
665+
650666
const requireWebContents = Effect.fn("PreviewManager.requireWebContents")(function* (
651667
tabId: string,
652668
) {
@@ -1714,10 +1730,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
17141730
const annotationTheme = yield* Ref.get(annotationThemeRef);
17151731
const currentAttachment = attached.get(webContentsId);
17161732
if (tab.webContentsId === webContentsId && currentAttachment?.webContents === wc) {
1717-
yield* attempt({ operation: "registerWebview.restoreZoomFactor", tabId, webContentsId }, () =>
1718-
wc.setZoomFactor(tab.zoomFactor),
1719-
);
1720-
yield* update(tabId, { zoomFactor: tab.zoomFactor });
1733+
// The guest we already own re-announced itself, so nothing about the tab
1734+
// changed. Only push its zoom back down — Chromium may have just handed
1735+
// this guest the app window's zoom level.
1736+
yield* assertTabZoom(tabId);
17211737
yield* attempt({ operation: "registerWebview.sendTheme", tabId, webContentsId }, () =>
17221738
wc.send(ANNOTATION_THEME_CHANNEL, annotationTheme),
17231739
);
@@ -1748,13 +1764,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
17481764
}
17491765
// Always assert the tab's own zoom rather than reading the guest's: a guest
17501766
// 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-
},
1767+
// zoom level, which is not the preview's zoom. Done before the guest is
1768+
// published so it never paints a frame at the inherited zoom.
1769+
yield* attempt({ operation: "registerWebview.restoreZoomFactor", tabId, webContentsId }, () =>
1770+
wc.setZoomFactor(currentTab.zoomFactor),
17581771
);
17591772
yield* attachListeners(tabId, wc);
17601773
const registeredAt = yield* currentIso;
@@ -1779,7 +1792,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
17791792
navStatus: pendingUrl === null ? computeNavStatus(wc) : current.navStatus,
17801793
canGoBack: wc.navigationHistory.canGoBack(),
17811794
canGoForward: wc.navigationHistory.canGoForward(),
1782-
zoomFactor,
17831795
updatedAt: registeredAt,
17841796
};
17851797
return [
@@ -1801,6 +1813,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
18011813
return yield* new PreviewTabNotFoundError({ tabId });
18021814
}
18031815
const { state: registered, pendingUrl } = registration.value;
1816+
// A zoom action that landed while this attach was in flight addressed the
1817+
// guest this one replaced, so settle the new guest on the committed factor.
1818+
yield* assertTabZoom(tabId);
18041819
runFork(restoreControlSession(tabId, wc));
18051820
yield* emit(tabId, registered);
18061821
yield* attempt({ operation: "registerWebview.sendTheme", tabId, webContentsId }, () =>
@@ -2101,19 +2116,8 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
21012116
* window's zoom changes (see DesktopWindow.zoomMain).
21022117
*/
21032118
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-
);
2119+
const tabIds = Array.from((yield* SynchronizedRef.get(tabsRef)).keys());
2120+
yield* Effect.forEach(tabIds, assertTabZoom, { discard: true });
21172121
});
21182122

21192123
const applyZoom = Effect.fn("PreviewManager.applyZoom")(function* (

0 commit comments

Comments
 (0)