fix(desktop): app zoom no longer zooms the preview browser (#6649)
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
1 parent
e9ae134c59
commit
d8a6dfd315
4 files changed
+219
-38
No files matched your search
@@ -979,7 +979,10 @@ describe("PreviewManager", () => {
|
||||
),
|
||||
);
|
||||
|
||||
effectIt.effect("mirrors Electron's effective zoom across registration and navigation", () =>
|
||||
// The guest reports whatever zoom level Chromium handed it from the app
|
||||
// window, so the tab's own zoom is the source of truth in both directions:
|
||||
// asserted onto every guest, never read back off one.
|
||||
effectIt.effect("keeps the tab's own zoom instead of the guest's reported zoom", () =>
|
||||
withManager((manager) =>
|
||||
Effect.gen(function* () {
|
||||
let effectiveZoom = 0.9;
|
||||
@@ -1025,18 +1028,13 @@ describe("PreviewManager", () => {
|
||||
yield* manager.createTab("tab_zoom");
|
||||
yield* manager.registerWebview("tab_zoom", 42);
|
||||
|
||||
expect(states.at(-1)?.zoomFactor).toBe(0.9);
|
||||
expect(setZoomFactor).not.toHaveBeenCalled();
|
||||
expect(states.at(-1)?.zoomFactor).toBe(1);
|
||||
expect(setZoomFactor).toHaveBeenCalledWith(1);
|
||||
|
||||
effectiveZoom = 1.25;
|
||||
listeners.get("did-navigate")?.();
|
||||
yield* Effect.yieldNow;
|
||||
|
||||
expect(states.at(-1)?.zoomFactor).toBe(1.25);
|
||||
expect(setZoomFactor).not.toHaveBeenCalled();
|
||||
|
||||
zoomReadable = false;
|
||||
url = "https://example.com/after-zoom-read-failed";
|
||||
// An app zoom leaves the guest reporting the inherited level. Navigating
|
||||
// must not adopt it as the preview's zoom.
|
||||
effectiveZoom = 0.8;
|
||||
url = "https://example.com/after-app-zoom";
|
||||
listeners.get("did-navigate")?.();
|
||||
yield* Effect.yieldNow;
|
||||
|
||||
@@ -1045,7 +1043,18 @@ describe("PreviewManager", () => {
|
||||
url,
|
||||
title: "Example",
|
||||
});
|
||||
expect(states.at(-1)?.zoomFactor).toBe(1.25);
|
||||
expect(states.at(-1)?.zoomFactor).toBe(1);
|
||||
|
||||
// Only the preview's own zoom controls move it.
|
||||
yield* manager.zoomIn("tab_zoom");
|
||||
expect(setZoomFactor).toHaveBeenCalledWith(1.1);
|
||||
expect(states.at(-1)?.zoomFactor).toBe(1.1);
|
||||
|
||||
zoomReadable = false;
|
||||
listeners.get("did-navigate")?.();
|
||||
yield* Effect.yieldNow;
|
||||
|
||||
expect(states.at(-1)?.zoomFactor).toBe(1.1);
|
||||
|
||||
const replacementSetZoomFactor = vi.fn();
|
||||
fromId.mockReturnValue({
|
||||
@@ -1074,8 +1083,103 @@ describe("PreviewManager", () => {
|
||||
|
||||
yield* manager.registerWebview("tab_zoom", 43);
|
||||
|
||||
expect(replacementSetZoomFactor).toHaveBeenCalledWith(1.25);
|
||||
expect(states.at(-1)?.zoomFactor).toBe(1.25);
|
||||
expect(replacementSetZoomFactor).toHaveBeenCalledWith(1.1);
|
||||
expect(states.at(-1)?.zoomFactor).toBe(1.1);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
// Zooming the app UI pushes the window's zoom level onto every guest, so the
|
||||
// preview has to be put back at the zoom the user gave it.
|
||||
effectIt.effect("re-applies each tab's own zoom when the app window zooms", () =>
|
||||
withManager((manager) =>
|
||||
Effect.gen(function* () {
|
||||
const setZoomFactor = vi.fn();
|
||||
fromId.mockReturnValue({
|
||||
id: 42,
|
||||
isDestroyed: () => false,
|
||||
getType: () => "webview",
|
||||
getURL: () => "https://example.com",
|
||||
getTitle: () => "Example",
|
||||
isLoading: () => false,
|
||||
getZoomFactor: () => 1,
|
||||
setZoomFactor,
|
||||
on: vi.fn(),
|
||||
off: vi.fn(),
|
||||
ipc: { on: vi.fn(), off: vi.fn() },
|
||||
send: webviewSend,
|
||||
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
|
||||
setWindowOpenHandler: vi.fn(),
|
||||
debugger: {
|
||||
isAttached: () => false,
|
||||
attach: vi.fn(),
|
||||
sendCommand: vi.fn(async () => undefined),
|
||||
on: vi.fn(),
|
||||
off: vi.fn(),
|
||||
},
|
||||
} as never);
|
||||
|
||||
yield* manager.createTab("tab_reapply");
|
||||
yield* manager.registerWebview("tab_reapply", 42);
|
||||
yield* manager.zoomIn("tab_reapply");
|
||||
setZoomFactor.mockClear();
|
||||
|
||||
yield* manager.reapplyZoom();
|
||||
|
||||
expect(setZoomFactor).toHaveBeenCalledTimes(1);
|
||||
expect(setZoomFactor).toHaveBeenCalledWith(1.1);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
// did-attach and dom-ready both re-register the guest that is already
|
||||
// attached, and a guest that just inherited the app window's zoom needs its
|
||||
// own back — without that round trip republishing tab state.
|
||||
effectIt.effect("re-asserts the tab's zoom when the active guest registers again", () =>
|
||||
withManager((manager) =>
|
||||
Effect.gen(function* () {
|
||||
const setZoomFactor = vi.fn();
|
||||
fromId.mockReturnValue({
|
||||
id: 42,
|
||||
isDestroyed: () => false,
|
||||
getType: () => "webview",
|
||||
getURL: () => "https://example.com",
|
||||
getTitle: () => "Example",
|
||||
isLoading: () => false,
|
||||
getZoomFactor: () => 1,
|
||||
setZoomFactor,
|
||||
on: vi.fn(),
|
||||
off: vi.fn(),
|
||||
ipc: { on: vi.fn(), off: vi.fn() },
|
||||
send: webviewSend,
|
||||
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
|
||||
setWindowOpenHandler: vi.fn(),
|
||||
debugger: {
|
||||
isAttached: () => false,
|
||||
attach: vi.fn(),
|
||||
sendCommand: vi.fn(async () => undefined),
|
||||
on: vi.fn(),
|
||||
off: vi.fn(),
|
||||
},
|
||||
} as never);
|
||||
const states: PreviewManager.PreviewTabState[] = [];
|
||||
yield* manager.subscribeStateChanges((_tabId, state) =>
|
||||
Effect.sync(() => {
|
||||
states.push(state);
|
||||
}),
|
||||
);
|
||||
|
||||
yield* manager.createTab("tab_reregister_zoom");
|
||||
yield* manager.registerWebview("tab_reregister_zoom", 42);
|
||||
yield* manager.zoomIn("tab_reregister_zoom");
|
||||
setZoomFactor.mockClear();
|
||||
const publishedBefore = states.length;
|
||||
|
||||
yield* manager.registerWebview("tab_reregister_zoom", 42);
|
||||
|
||||
expect(setZoomFactor).toHaveBeenCalledWith(1.1);
|
||||
expect(states.length).toBe(publishedBefore);
|
||||
expect(states.at(-1)?.zoomFactor).toBe(1.1);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -647,6 +647,22 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
|
||||
if (Option.isSome(next)) yield* emit(tabId, next.value);
|
||||
});
|
||||
|
||||
/**
|
||||
* Pushes a tab's zoom factor onto whichever guest it currently owns, reading
|
||||
* both at call time. Anything that applies zoom after an await goes through
|
||||
* here: a snapshot taken before the await can be older than a zoom action that
|
||||
* landed in between, and re-applying it would roll that action back.
|
||||
*/
|
||||
const assertTabZoom = Effect.fn("PreviewManager.assertTabZoom")(function* (tabId: string) {
|
||||
const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId);
|
||||
if (!tab || tab.webContentsId == null) return;
|
||||
const wc = webContents.fromId(tab.webContentsId);
|
||||
if (!wc || wc.isDestroyed()) return;
|
||||
yield* attempt({ operation: "assertTabZoom", tabId, webContentsId: wc.id }, () =>
|
||||
wc.setZoomFactor(tab.zoomFactor),
|
||||
).pipe(Effect.ignore);
|
||||
});
|
||||
|
||||
const requireWebContents = Effect.fn("PreviewManager.requireWebContents")(function* (
|
||||
tabId: string,
|
||||
) {
|
||||
@@ -1305,10 +1321,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
|
||||
confirmedNavigation = false,
|
||||
) {
|
||||
if (wc.isDestroyed()) return;
|
||||
const zoomFactor = yield* attempt(
|
||||
{ operation: "syncWebContentsState.getZoomFactor", tabId, webContentsId: wc.id },
|
||||
() => wc.getZoomFactor(),
|
||||
).pipe(Effect.option);
|
||||
const computedNavStatus = computeNavStatus(wc);
|
||||
const canGoBack = wc.navigationHistory.canGoBack();
|
||||
const canGoForward = wc.navigationHistory.canGoForward();
|
||||
@@ -1338,7 +1350,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
|
||||
navStatus,
|
||||
canGoBack,
|
||||
canGoForward,
|
||||
...(Option.isSome(zoomFactor) ? { zoomFactor: zoomFactor.value } : {}),
|
||||
// zoomFactor is deliberately not read back from the guest: Chromium
|
||||
// reports the level it inherited from the app window, so mirroring it
|
||||
// would turn an app zoom into the preview's own zoom.
|
||||
updatedAt,
|
||||
};
|
||||
return [
|
||||
@@ -1716,11 +1730,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
|
||||
const annotationTheme = yield* Ref.get(annotationThemeRef);
|
||||
const currentAttachment = attached.get(webContentsId);
|
||||
if (tab.webContentsId === webContentsId && currentAttachment?.webContents === wc) {
|
||||
const zoomFactor = yield* attempt(
|
||||
{ operation: "registerWebview.getZoomFactor", tabId, webContentsId },
|
||||
() => wc.getZoomFactor(),
|
||||
);
|
||||
yield* update(tabId, { zoomFactor });
|
||||
// The guest we already own re-announced itself, so nothing about the tab
|
||||
// changed. Only push its zoom back down — Chromium may have just handed
|
||||
// this guest the app window's zoom level.
|
||||
yield* assertTabZoom(tabId);
|
||||
yield* attempt({ operation: "registerWebview.sendTheme", tabId, webContentsId }, () =>
|
||||
wc.send(ANNOTATION_THEME_CHANNEL, annotationTheme),
|
||||
);
|
||||
@@ -1749,18 +1762,13 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
|
||||
) {
|
||||
return yield* new PreviewTabNotFoundError({ tabId });
|
||||
}
|
||||
const zoomFactor =
|
||||
replacedWebContentsId !== null
|
||||
? yield* attempt(
|
||||
{ operation: "registerWebview.restoreZoomFactor", tabId, webContentsId },
|
||||
() => {
|
||||
wc.setZoomFactor(currentTab.zoomFactor);
|
||||
return currentTab.zoomFactor;
|
||||
},
|
||||
)
|
||||
: yield* attempt({ operation: "registerWebview.getZoomFactor", tabId, webContentsId }, () =>
|
||||
wc.getZoomFactor(),
|
||||
);
|
||||
// Always assert the tab's own zoom rather than reading the guest's: a guest
|
||||
// attaching while the app UI is zoomed starts at the embedder's inherited
|
||||
// zoom level, which is not the preview's zoom. Done before the guest is
|
||||
// published so it never paints a frame at the inherited zoom.
|
||||
yield* attempt({ operation: "registerWebview.restoreZoomFactor", tabId, webContentsId }, () =>
|
||||
wc.setZoomFactor(currentTab.zoomFactor),
|
||||
);
|
||||
yield* attachListeners(tabId, wc);
|
||||
const registeredAt = yield* currentIso;
|
||||
const registration = yield* SynchronizedRef.modifyEffect(tabsRef, (tabs) =>
|
||||
@@ -1784,7 +1792,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
|
||||
navStatus: pendingUrl === null ? computeNavStatus(wc) : current.navStatus,
|
||||
canGoBack: wc.navigationHistory.canGoBack(),
|
||||
canGoForward: wc.navigationHistory.canGoForward(),
|
||||
zoomFactor,
|
||||
updatedAt: registeredAt,
|
||||
};
|
||||
return [
|
||||
@@ -1806,6 +1813,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
|
||||
return yield* new PreviewTabNotFoundError({ tabId });
|
||||
}
|
||||
const { state: registered, pendingUrl } = registration.value;
|
||||
// A zoom action that landed while this attach was in flight addressed the
|
||||
// guest this one replaced, so settle the new guest on the committed factor.
|
||||
yield* assertTabZoom(tabId);
|
||||
runFork(restoreControlSession(tabId, wc));
|
||||
yield* emit(tabId, registered);
|
||||
yield* attempt({ operation: "registerWebview.sendTheme", tabId, webContentsId }, () =>
|
||||
@@ -2099,6 +2109,17 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
|
||||
);
|
||||
});
|
||||
|
||||
/**
|
||||
* Chromium hands every guest `<webview>` the embedder's zoom level, so zooming
|
||||
* the app UI drags the previewed page along with it. The preview browser owns
|
||||
* its own zoom factor, so re-assert it on each attached guest whenever the main
|
||||
* window's zoom changes (see DesktopWindow.zoomMain).
|
||||
*/
|
||||
const reapplyZoom = Effect.fn("PreviewManager.reapplyZoom")(function* () {
|
||||
const tabIds = Array.from((yield* SynchronizedRef.get(tabsRef)).keys());
|
||||
yield* Effect.forEach(tabIds, assertTabZoom, { discard: true });
|
||||
});
|
||||
|
||||
const applyZoom = Effect.fn("PreviewManager.applyZoom")(function* (
|
||||
tabId: string,
|
||||
transform: (current: number) => number,
|
||||
@@ -3476,6 +3497,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
|
||||
openPictureInPicture,
|
||||
openDevTools,
|
||||
pickElement,
|
||||
reapplyZoom,
|
||||
refresh,
|
||||
registerWebview,
|
||||
resetZoom: (tabId: string) => applyZoom(tabId, () => DEFAULT_ZOOM_FACTOR),
|
||||
@@ -3774,6 +3796,9 @@ export class PreviewManager extends Context.Service<
|
||||
readonly zoomIn: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
|
||||
readonly zoomOut: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
|
||||
readonly resetZoom: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
|
||||
// Re-applies every attached guest's own zoom factor, undoing the zoom level
|
||||
// Chromium inherits from the embedder when the app UI zooms.
|
||||
readonly reapplyZoom: () => Effect.Effect<void>;
|
||||
readonly hardReload: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
|
||||
readonly setColorScheme: (
|
||||
tabId: string,
|
||||
@@ -3874,6 +3899,7 @@ export const make = Effect.gen(function* PreviewManagerMake() {
|
||||
zoomIn: operations.zoomIn,
|
||||
zoomOut: operations.zoomOut,
|
||||
resetZoom: operations.resetZoom,
|
||||
reapplyZoom: operations.reapplyZoom,
|
||||
hardReload: operations.hardReload,
|
||||
setColorScheme: operations.setColorScheme,
|
||||
openDevTools: operations.openDevTools,
|
||||
|
||||
@@ -61,9 +61,14 @@ const environmentInput = {
|
||||
function makeFakeBrowserWindow() {
|
||||
const windowListeners = new Map<string, (...args: readonly unknown[]) => void>();
|
||||
const webContentsListeners = new Map<string, (...args: readonly unknown[]) => void>();
|
||||
let zoomLevel = 0;
|
||||
const webContents = {
|
||||
copyImageAt: vi.fn(),
|
||||
getURL: vi.fn(() => "t3code-dev://app/"),
|
||||
getZoomLevel: vi.fn(() => zoomLevel),
|
||||
setZoomLevel: vi.fn((level: number) => {
|
||||
zoomLevel = level;
|
||||
}),
|
||||
isLoadingMainFrame: vi.fn(() => false),
|
||||
on: vi.fn((eventName: string, listener: (...args: readonly unknown[]) => void) => {
|
||||
webContentsListeners.set(eventName, listener);
|
||||
@@ -116,6 +121,7 @@ function makeFakeBrowserWindow() {
|
||||
openDevTools: webContents.openDevTools,
|
||||
reload: webContents.reload,
|
||||
send: webContents.send,
|
||||
setZoomLevel: webContents.setZoomLevel,
|
||||
setAutoHideCursor: window.setAutoHideCursor,
|
||||
webContentsListeners,
|
||||
windowListeners,
|
||||
@@ -186,6 +192,7 @@ function makeTestLayer(input: {
|
||||
bounds: DesktopAppSettings.DesktopWindowBounds,
|
||||
) => Effect.Effect<void>;
|
||||
readonly openedExternalUrls?: unknown[];
|
||||
readonly previewZoomReapplies?: number[];
|
||||
}) {
|
||||
let desktopSettings = input.desktopSettings ?? DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS;
|
||||
const desktopAppSettingsLayer = Layer.succeed(DesktopAppSettings.DesktopAppSettings, {
|
||||
@@ -264,6 +271,10 @@ function makeTestLayer(input: {
|
||||
setMainWindow: () => Effect.void,
|
||||
isBrowserPartition: (partition) => partition.startsWith("persist:t3code-preview-"),
|
||||
getBrowserPartition: () => Effect.succeed("persist:t3code-preview-test"),
|
||||
reapplyZoom: () =>
|
||||
Effect.sync(() => {
|
||||
input.previewZoomReapplies?.push(input.window.webContents.getZoomLevel());
|
||||
}),
|
||||
}),
|
||||
),
|
||||
),
|
||||
@@ -483,6 +494,42 @@ describe("DesktopWindow", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
// Chromium hands the main window's zoom level down to embedded preview
|
||||
// guests, so every app zoom has to put the preview browser back at its own
|
||||
// zoom or zooming the UI drags the previewed page with it.
|
||||
it.effect("restores the preview browser's own zoom after zooming the app", () =>
|
||||
Effect.gen(function* () {
|
||||
const fakeWindow = makeFakeBrowserWindow();
|
||||
const createCount = yield* Ref.make(0);
|
||||
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
|
||||
const previewZoomReapplies: number[] = [];
|
||||
const layer = makeTestLayer({
|
||||
window: fakeWindow.window,
|
||||
createCount,
|
||||
mainWindow,
|
||||
previewZoomReapplies,
|
||||
});
|
||||
|
||||
yield* Effect.gen(function* () {
|
||||
const desktopWindow = yield* DesktopWindow.DesktopWindow;
|
||||
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
|
||||
|
||||
yield* desktopWindow.zoomMain("out");
|
||||
yield* desktopWindow.zoomMain("out");
|
||||
yield* desktopWindow.zoomMain("in");
|
||||
yield* desktopWindow.zoomMain("reset");
|
||||
|
||||
assert.deepEqual(
|
||||
fakeWindow.setZoomLevel.mock.calls.map(([level]) => level),
|
||||
[-0.5, -1, -0.5, 0],
|
||||
);
|
||||
// Recorded after the window level moved, so the preview is put back at
|
||||
// its own zoom on every step rather than left on the inherited one.
|
||||
assert.deepEqual(previewZoomReapplies, [-0.5, -1, -0.5, 0]);
|
||||
}).pipe(Effect.provide(layer));
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("uses the persisted main window bounds when opening the window", () =>
|
||||
Effect.gen(function* () {
|
||||
const fakeWindow = makeFakeBrowserWindow();
|
||||
|
||||
@@ -855,6 +855,10 @@ export const make = Effect.gen(function* () {
|
||||
webContents.setZoomLevel(
|
||||
direction === "reset" ? 0 : webContents.getZoomLevel() + (direction === "in" ? 0.5 : -0.5),
|
||||
);
|
||||
// Chromium pushes the new level down to embedded guests, which would zoom
|
||||
// the previewed page along with the app UI. The preview browser keeps its
|
||||
// own zoom, so put each guest back where the preview left it.
|
||||
yield* previewManager.reapplyZoom();
|
||||
}),
|
||||
syncAppearance: Effect.gen(function* () {
|
||||
const shouldUseDarkColors = yield* electronTheme.shouldUseDarkColors;
|
||||
|
||||
Reference in new issue
Block a user