From 38ba1c36171e8a6187784cea72aac7ac6010c471 Mon Sep 17 00:00:00 2001 From: Bas van den Wollenberg Date: Wed, 16 Oct 2024 03:49:50 +0200 Subject: [PATCH] fix: zoom now works properly frfr --- src/main/index.ts | 31 ++++++++----------------------- src/main/lib/window/menu.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 23 deletions(-) create mode 100644 src/main/lib/window/menu.ts diff --git a/src/main/index.ts b/src/main/index.ts index 8fc6f2ca..58a682fd 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,5 +1,6 @@ import icon from "../../resources/icon.png?asset"; import { Router } from "./lib/route-pass/Router"; +import createMenu from "./lib/window/menu"; import trackBounds, { getBounds, wasMaximized } from "./lib/window/resizer"; import { main } from "./main"; import { electronApp, is, optimizer } from "@electron-toolkit/utils"; @@ -39,6 +40,9 @@ async function createWindow() { */ }); + const menu = createMenu(); + window.setMenu(menu); + app.on("second-instance", () => { if (window.isMinimized()) window.restore(); window.focus(); @@ -58,21 +62,6 @@ async function createWindow() { trackBounds(window); - window.webContents.on("before-input-event", (event, input) => { - const modKeyHeld = process.platform === "darwin" ? input.meta : input.control; - if (modKeyHeld && ["+", "=", "-", "0"].includes(input.key)) { - if (input.key === "+" || input.key === "=") { - zoom(window, 0.1); - } else if (input.key === "-") { - zoom(window, -0.1); - } else if (input.key === "0") { - zoom(window); - } - - event.preventDefault(); - } - }); - window.on("ready-to-show", async () => { window.show(); await Router.dispatch(window, "changeScene", "main"); @@ -108,7 +97,10 @@ app.whenReady().then(async () => { // and ignore CommandOrControl + R in production. // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils app.on("browser-window-created", (_, window) => { - optimizer.watchWindowShortcuts(window); + optimizer.watchWindowShortcuts(window, { + zoom: true, + escToCloseWindow: false, + }); }); await createWindow(); @@ -127,10 +119,3 @@ app.on("window-all-closed", () => { app.quit(); } }); - -function zoom(window: BrowserWindow, factor?: number) { - const currentZoom = window.webContents.getZoomFactor(); - const newZoom = factor ? currentZoom + factor : 1; - const clampedZoom = Math.max(Math.min(newZoom, 2), 0.5); - window.webContents.setZoomFactor(clampedZoom); -} diff --git a/src/main/lib/window/menu.ts b/src/main/lib/window/menu.ts new file mode 100644 index 00000000..0a377632 --- /dev/null +++ b/src/main/lib/window/menu.ts @@ -0,0 +1,31 @@ +import { Menu, type MenuItemConstructorOptions } from "electron"; + +export default function createMenu() { + const menuTemplate: MenuItemConstructorOptions[] = [ + { role: "fileMenu" }, + { role: "editMenu" }, + { + role: "viewMenu", + submenu: [ + { role: "reload" }, + { role: "forceReload" }, + { role: "toggleDevTools" }, + { type: "separator" }, + { role: "resetZoom" }, + { role: "zoomIn" }, + { role: "zoomIn", accelerator: "CommandOrControl+=", visible: false }, + { role: "zoomOut" }, + { type: "separator" }, + { role: "togglefullscreen" }, + ], + }, + { role: "windowMenu" }, + { role: "help" }, + ]; + + if (process.platform === "darwin") { + menuTemplate.unshift({ role: "appMenu" }); + } + + return Menu.buildFromTemplate(menuTemplate); +}