Skip to content
TypeScript

fix(desktop): keypad shortcuts and Ctrl+wheel zoom the app (#21200)

Reda Hilaleb51389e4eac
diff --git a/apps/desktop/src/window/DesktopApplicationMenu.test.ts b/apps/desktop/src/window/DesktopApplicationMenu.test.ts
index 2b8bea6be9a..fa3960a34ee 100644
--- a/apps/desktop/src/window/DesktopApplicationMenu.test.ts
+++ b/apps/desktop/src/window/DesktopApplicationMenu.test.ts
@@ -280,6 +280,37 @@ describe("DesktopApplicationMenu", () => {
}),
);
+ it.effect("zooms from the numeric keypad without listing the commands twice", () =>
+ Effect.gen(function* () {
+ for (const [accelerator, expected] of [
+ ["CmdOrCtrl+numadd", "zoom-in"],
+ ["CmdOrCtrl+numsub", "zoom-out"],
+ ["CmdOrCtrl+num0", "zoom-reset"],
+ ] as const) {
+ const selectedAction = yield* Deferred.make<string>();
+ const applicationMenuTemplate =
+ yield* Deferred.make<readonly Electron.MenuItemConstructorOptions[]>();
+
+ yield* configureMenu(selectedAction, applicationMenuTemplate);
+
+ const template = yield* Deferred.await(applicationMenuTemplate);
+ const viewMenu = template.find((item) => item.label === "View");
+ if (!viewMenu || !Array.isArray(viewMenu.submenu)) {
+ throw new Error("Expected View menu submenu to be an array.");
+ }
+ const item = viewMenu.submenu.find((entry) => entry.accelerator === accelerator);
+ assert.isDefined(item);
+ assert.isFalse(item.visible);
+ if (typeof item.click !== "function") {
+ throw new Error(`Expected ${accelerator} menu item to have a click handler.`);
+ }
+
+ item.click({} as Electron.MenuItem, {} as Electron.BrowserWindow, {} as KeyboardEvent);
+ assert.equal(yield* Deferred.await(selectedAction), expected);
+ }
+ }),
+ );
+
it.effect("reloads the main window even while a browser page has focus", () =>
Effect.gen(function* () {
const selectedAction = yield* Deferred.make<string>();
diff --git a/apps/desktop/src/window/DesktopApplicationMenu.ts b/apps/desktop/src/window/DesktopApplicationMenu.ts
index 09808348a0d..c51e13e5867 100644
--- a/apps/desktop/src/window/DesktopApplicationMenu.ts
+++ b/apps/desktop/src/window/DesktopApplicationMenu.ts
@@ -264,6 +264,26 @@ export const make = Effect.gen(function* () {
click: zoomClick("in"),
},
{ label: "Zoom Out", accelerator: "CmdOrCtrl+-", click: zoomClick("out") },
+ // The numeric keypad sends its own key codes, so the number-row
+ // accelerators above never match it.
+ {
+ label: "Actual Size",
+ accelerator: "CmdOrCtrl+num0",
+ visible: false,
+ click: zoomClick("reset"),
+ },
+ {
+ label: "Zoom In",
+ accelerator: "CmdOrCtrl+numadd",
+ visible: false,
+ click: zoomClick("in"),
+ },
+ {
+ label: "Zoom Out",
+ accelerator: "CmdOrCtrl+numsub",
+ visible: false,
+ click: zoomClick("out"),
+ },
{ type: "separator" },
{ role: "togglefullscreen" },
],
diff --git a/apps/desktop/src/window/DesktopWindow.test.ts b/apps/desktop/src/window/DesktopWindow.test.ts
index 8f8b8d40777..cebdcbb7797 100644
--- a/apps/desktop/src/window/DesktopWindow.test.ts
+++ b/apps/desktop/src/window/DesktopWindow.test.ts
@@ -798,6 +798,40 @@ describe("DesktopWindow", () => {
}),
);
+ // Ctrl+wheel bypasses the menu accelerators, so it has to take the same
+ // zoom step, preview restore included, from the window's zoom-changed event.
+ it.effect("zooms the app from Ctrl+wheel like the menu does", () =>
+ 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 = layerTest({
+ 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"));
+
+ const zoomChanged = fakeWindow.webContentsListeners.get("zoom-changed");
+ if (!zoomChanged) return yield* Effect.die("zoom-changed listener was not registered");
+ zoomChanged({}, "in");
+ zoomChanged({}, "out");
+ zoomChanged({}, "out");
+
+ assert.deepEqual(
+ fakeWindow.setZoomLevel.mock.calls.map(([level]) => level),
+ [0.5, 0, -0.5],
+ );
+ assert.deepEqual(previewZoomReapplies, [0.5, 0, -0.5]);
+ }).pipe(Effect.provide(layer));
+ }),
+ );
+
it.effect("reloads and inspects the main window even when a popup has focus", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
diff --git a/apps/desktop/src/window/DesktopWindow.ts b/apps/desktop/src/window/DesktopWindow.ts
index 2a39c15193e..1d76107425e 100644
--- a/apps/desktop/src/window/DesktopWindow.ts
+++ b/apps/desktop/src/window/DesktopWindow.ts
@@ -329,6 +329,21 @@ export const make = Effect.gen(function* () {
const currentMainWindow = electronWindow.currentMainOrFirst.pipe(Effect.flatMap(withoutSplash));
const focusedMainWindow = electronWindow.focusedMainOrFirst.pipe(Effect.flatMap(withoutSplash));
+ const zoomWindow = Effect.fn("desktop.window.zoomWindow")(function* (
+ window: Electron.BrowserWindow,
+ direction: MainWindowZoomDirection,
+ ) {
+ const webContents = window.webContents;
+ webContents.setZoomLevel(
+ direction === "reset" ? 0 : webContents.getZoomLevel() + (direction === "in" ? 0.5 : -0.5),
+ );
+ if (environment.platform === "darwin") syncMacosWindowButtons(window);
+ // 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();
+ });
+
const createWindow = Effect.fn("desktop.window.createWindow")(function* (): Effect.fn.Return<
Electron.BrowserWindow,
DesktopWindowError
@@ -645,6 +660,13 @@ export const make = Effect.gen(function* () {
window.webContents.on("input-event", (_event, input) => {
if (input.type === "gestureScrollEnd") window.webContents.send(TRACKPAD_SCROLL_END_CHANNEL);
});
+ // Ctrl+wheel never reaches the menu accelerators: Chromium reports it here
+ // and leaves applying the zoom to the app. A preview guest under the
+ // pointer emits this on its own webContents and keeps its own zoom.
+ window.webContents.on("zoom-changed", (_event, direction) => {
+ if (window.isDestroyed()) return;
+ runFork(zoomWindow(window, direction));
+ });
window.on("page-title-updated", (event) => {
event.preventDefault();
@@ -976,15 +998,7 @@ export const make = Effect.gen(function* () {
if (Option.isNone(window) || window.value.isDestroyed()) {
return;
}
- const webContents = window.value.webContents;
- webContents.setZoomLevel(
- direction === "reset" ? 0 : webContents.getZoomLevel() + (direction === "in" ? 0.5 : -0.5),
- );
- if (environment.platform === "darwin") syncMacosWindowButtons(window.value);
- // 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();
+ yield* zoomWindow(window.value, direction);
}),
runMainContentsCommand: Effect.fn("desktop.window.runMainContentsCommand")(function* (command) {
yield* Effect.annotateCurrentSpan({ command });