Skip to content

Commit eb51389

Browse files
0xKaMnigos
andauthored
fix(desktop): keypad shortcuts and Ctrl+wheel zoom the app (pingdotgg#21200)
Co-authored-by: Mnigos <[email protected]>
1 parent cef6f7a commit eb51389

4 files changed

Lines changed: 108 additions & 9 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,37 @@ describe("DesktopApplicationMenu", () => {
280280
}),
281281
);
282282

283+
it.effect("zooms from the numeric keypad without listing the commands twice", () =>
284+
Effect.gen(function* () {
285+
for (const [accelerator, expected] of [
286+
["CmdOrCtrl+numadd", "zoom-in"],
287+
["CmdOrCtrl+numsub", "zoom-out"],
288+
["CmdOrCtrl+num0", "zoom-reset"],
289+
] as const) {
290+
const selectedAction = yield* Deferred.make<string>();
291+
const applicationMenuTemplate =
292+
yield* Deferred.make<readonly Electron.MenuItemConstructorOptions[]>();
293+
294+
yield* configureMenu(selectedAction, applicationMenuTemplate);
295+
296+
const template = yield* Deferred.await(applicationMenuTemplate);
297+
const viewMenu = template.find((item) => item.label === "View");
298+
if (!viewMenu || !Array.isArray(viewMenu.submenu)) {
299+
throw new Error("Expected View menu submenu to be an array.");
300+
}
301+
const item = viewMenu.submenu.find((entry) => entry.accelerator === accelerator);
302+
assert.isDefined(item);
303+
assert.isFalse(item.visible);
304+
if (typeof item.click !== "function") {
305+
throw new Error(`Expected ${accelerator} menu item to have a click handler.`);
306+
}
307+
308+
item.click({} as Electron.MenuItem, {} as Electron.BrowserWindow, {} as KeyboardEvent);
309+
assert.equal(yield* Deferred.await(selectedAction), expected);
310+
}
311+
}),
312+
);
313+
283314
it.effect("reloads the main window even while a browser page has focus", () =>
284315
Effect.gen(function* () {
285316
const selectedAction = yield* Deferred.make<string>();

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,26 @@ export const make = Effect.gen(function* () {
264264
click: zoomClick("in"),
265265
},
266266
{ label: "Zoom Out", accelerator: "CmdOrCtrl+-", click: zoomClick("out") },
267+
// The numeric keypad sends its own key codes, so the number-row
268+
// accelerators above never match it.
269+
{
270+
label: "Actual Size",
271+
accelerator: "CmdOrCtrl+num0",
272+
visible: false,
273+
click: zoomClick("reset"),
274+
},
275+
{
276+
label: "Zoom In",
277+
accelerator: "CmdOrCtrl+numadd",
278+
visible: false,
279+
click: zoomClick("in"),
280+
},
281+
{
282+
label: "Zoom Out",
283+
accelerator: "CmdOrCtrl+numsub",
284+
visible: false,
285+
click: zoomClick("out"),
286+
},
267287
{ type: "separator" },
268288
{ role: "togglefullscreen" },
269289
],

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,40 @@ describe("DesktopWindow", () => {
798798
}),
799799
);
800800

801+
// Ctrl+wheel bypasses the menu accelerators, so it has to take the same
802+
// zoom step, preview restore included, from the window's zoom-changed event.
803+
it.effect("zooms the app from Ctrl+wheel like the menu does", () =>
804+
Effect.gen(function* () {
805+
const fakeWindow = makeFakeBrowserWindow();
806+
const createCount = yield* Ref.make(0);
807+
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
808+
const previewZoomReapplies: number[] = [];
809+
const layer = layerTest({
810+
window: fakeWindow.window,
811+
createCount,
812+
mainWindow,
813+
previewZoomReapplies,
814+
});
815+
816+
yield* Effect.gen(function* () {
817+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
818+
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
819+
820+
const zoomChanged = fakeWindow.webContentsListeners.get("zoom-changed");
821+
if (!zoomChanged) return yield* Effect.die("zoom-changed listener was not registered");
822+
zoomChanged({}, "in");
823+
zoomChanged({}, "out");
824+
zoomChanged({}, "out");
825+
826+
assert.deepEqual(
827+
fakeWindow.setZoomLevel.mock.calls.map(([level]) => level),
828+
[0.5, 0, -0.5],
829+
);
830+
assert.deepEqual(previewZoomReapplies, [0.5, 0, -0.5]);
831+
}).pipe(Effect.provide(layer));
832+
}),
833+
);
834+
801835
it.effect("reloads and inspects the main window even when a popup has focus", () =>
802836
Effect.gen(function* () {
803837
const fakeWindow = makeFakeBrowserWindow();

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,21 @@ export const make = Effect.gen(function* () {
329329
const currentMainWindow = electronWindow.currentMainOrFirst.pipe(Effect.flatMap(withoutSplash));
330330
const focusedMainWindow = electronWindow.focusedMainOrFirst.pipe(Effect.flatMap(withoutSplash));
331331

332+
const zoomWindow = Effect.fn("desktop.window.zoomWindow")(function* (
333+
window: Electron.BrowserWindow,
334+
direction: MainWindowZoomDirection,
335+
) {
336+
const webContents = window.webContents;
337+
webContents.setZoomLevel(
338+
direction === "reset" ? 0 : webContents.getZoomLevel() + (direction === "in" ? 0.5 : -0.5),
339+
);
340+
if (environment.platform === "darwin") syncMacosWindowButtons(window);
341+
// Chromium pushes the new level down to embedded guests, which would zoom
342+
// the previewed page along with the app UI. The preview browser keeps its
343+
// own zoom, so put each guest back where the preview left it.
344+
yield* previewManager.reapplyZoom();
345+
});
346+
332347
const createWindow = Effect.fn("desktop.window.createWindow")(function* (): Effect.fn.Return<
333348
Electron.BrowserWindow,
334349
DesktopWindowError
@@ -645,6 +660,13 @@ export const make = Effect.gen(function* () {
645660
window.webContents.on("input-event", (_event, input) => {
646661
if (input.type === "gestureScrollEnd") window.webContents.send(TRACKPAD_SCROLL_END_CHANNEL);
647662
});
663+
// Ctrl+wheel never reaches the menu accelerators: Chromium reports it here
664+
// and leaves applying the zoom to the app. A preview guest under the
665+
// pointer emits this on its own webContents and keeps its own zoom.
666+
window.webContents.on("zoom-changed", (_event, direction) => {
667+
if (window.isDestroyed()) return;
668+
runFork(zoomWindow(window, direction));
669+
});
648670

649671
window.on("page-title-updated", (event) => {
650672
event.preventDefault();
@@ -976,15 +998,7 @@ export const make = Effect.gen(function* () {
976998
if (Option.isNone(window) || window.value.isDestroyed()) {
977999
return;
9781000
}
979-
const webContents = window.value.webContents;
980-
webContents.setZoomLevel(
981-
direction === "reset" ? 0 : webContents.getZoomLevel() + (direction === "in" ? 0.5 : -0.5),
982-
);
983-
if (environment.platform === "darwin") syncMacosWindowButtons(window.value);
984-
// Chromium pushes the new level down to embedded guests, which would zoom
985-
// the previewed page along with the app UI. The preview browser keeps its
986-
// own zoom, so put each guest back where the preview left it.
987-
yield* previewManager.reapplyZoom();
1001+
yield* zoomWindow(window.value, direction);
9881002
}),
9891003
runMainContentsCommand: Effect.fn("desktop.window.runMainContentsCommand")(function* (command) {
9901004
yield* Effect.annotateCurrentSpan({ command });

0 commit comments

Comments
 (0)