Skip to content

Commit

Permalink
Merge pull request #79 from Team-BTMC/fix/zoom-again
Browse files Browse the repository at this point in the history
fix: zoom (again)
  • Loading branch information
hrfarmer authored Oct 16, 2024
2 parents fa74d1a + 38ba1c3 commit 121a6b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
31 changes: 8 additions & 23 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -39,6 +40,9 @@ async function createWindow() {
*/
});

const menu = createMenu();
window.setMenu(menu);

app.on("second-instance", () => {
if (window.isMinimized()) window.restore();
window.focus();
Expand All @@ -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");
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
31 changes: 31 additions & 0 deletions src/main/lib/window/menu.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 121a6b4

Please sign in to comment.