diff --git a/package.json b/package.json index 7c2c848a6c..2e65e9e717 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,6 @@ "electron-builder": "26.6.0", "electron-builder-squirrel-windows": "26.6.0", "electron-devtools-installer": "^4.0.0", - "electron-playwright-helpers": "^2.1.0", "eslint": "^8.26.0", "eslint-config-google": "^0.14.0", "eslint-config-prettier": "^10.0.0", diff --git a/playwright/element-desktop-test.ts b/playwright/element-desktop-test.ts index bed0946448..743be8da6f 100644 --- a/playwright/element-desktop-test.ts +++ b/playwright/element-desktop-test.ts @@ -12,7 +12,6 @@ import path, { dirname } from "node:path"; import os from "node:os"; import { fileURLToPath } from "node:url"; import { PassThrough } from "node:stream"; -import { stubDialog } from "electron-playwright-helpers"; /** * A PassThrough stream that captures all data written to it. @@ -112,8 +111,6 @@ export const test = base.extend({ page: async ({ app }, use) => { const window = await app.firstWindow(); await use(window); - // EW may be configured to ask for confirmation before the app exits. - await stubDialog(app, "showMessageBoxSync", 1); await app.close().catch((e) => { console.error(e); }); diff --git a/src/electron-main.ts b/src/electron-main.ts index 66b1545b3f..9b4b9f9089 100644 --- a/src/electron-main.ts +++ b/src/electron-main.ts @@ -507,42 +507,15 @@ app.on("ready", async () => { const exitShortcutPressed = input.type === "keyDown" && exitShortcuts.some((shortcutFn) => shortcutFn(input, process.platform)); - // Early return if: - // 1. Keys that were pressed are not shortcuts for exiting the app - // 2. Electron mainWindow is null for some reason - // 3. The application is already in the process of quitting - if (!exitShortcutPressed || !global.mainWindow || global.appQuitting) return; + // We only care about the exit shortcuts here + if (!exitShortcutPressed || !global.mainWindow) return; // Prevent the default behaviour event.preventDefault(); - // Check if the user expects us to minimize to tray instead of quitting the app - // Two cases: - // 1. On Linux/Windows, user has enabled "Show tray icon and minimise window to it on close" in settings. - // 2. On Mac, the canonical behaviour is to minimize to tray; this is not configurable. - const shouldMinimize = store.get("minimizeToTray") || process.platform === "darwin"; - if (shouldMinimize) { - if (global.mainWindow?.isFullScreen()) { - global.mainWindow.once("leave-full-screen", () => global.mainWindow?.hide()); - global.mainWindow.setFullScreen(false); - } else { - global.mainWindow?.hide(); - } - return; - } - - // Quit the app; rest of the quit logic is on the close event handler below. - app.quit(); - }); - - global.mainWindow.on("closed", () => { - global.mainWindow = null; - }); - - global.mainWindow.on("close", async (e) => { - // Check if the user expects us to ask for confirmation before quitting the app + // Let's ask the user if they really want to exit the app const shouldWarnBeforeExit = store.get("warnBeforeExit", true); - if (shouldWarnBeforeExit && global.mainWindow) { + if (shouldWarnBeforeExit) { const shouldCancelCloseRequest = dialog.showMessageBoxSync(global.mainWindow, { type: "question", @@ -556,11 +529,33 @@ app.on("ready", async () => { defaultId: 1, cancelId: 0, }) === 0; - if (shouldCancelCloseRequest) { - e.preventDefault(); - global.appQuitting = false; - return false; + if (shouldCancelCloseRequest) return; + } + + // Exit the app + app.exit(); + }); + + global.mainWindow.on("closed", () => { + global.mainWindow = null; + }); + global.mainWindow.on("close", async (e) => { + // If we are not quitting and have a tray icon then minimize to tray + if (!global.appQuitting && (tray.hasTray() || process.platform === "darwin")) { + // On Mac, closing the window just hides it + // (this is generally how single-window Mac apps + // behave, eg. Mail.app) + e.preventDefault(); + + if (global.mainWindow?.isFullScreen()) { + global.mainWindow.once("leave-full-screen", () => global.mainWindow?.hide()); + + global.mainWindow.setFullScreen(false); + } else { + global.mainWindow?.hide(); } + + return false; } }); diff --git a/yarn.lock b/yarn.lock index c8e06a006a..fe59715257 100644 --- a/yarn.lock +++ b/yarn.lock @@ -894,7 +894,7 @@ ajv "^6.12.0" ajv-keywords "^3.4.1" -"@electron/asar@3.4.1", "@electron/asar@^3.2.4", "@electron/asar@^3.3.1": +"@electron/asar@3.4.1", "@electron/asar@^3.3.1": version "3.4.1" resolved "https://registry.yarnpkg.com/@electron/asar/-/asar-3.4.1.tgz#4e9196a4b54fba18c56cd8d5cac67c5bdc588065" integrity sha512-i4/rNPRS84t0vSRa2HorerGRXWyF4vThfHesw0dmcWHp+cspK743UanA0suA5Q5y8kzY2y6YKrvbIUn69BCAiA== @@ -3379,13 +3379,6 @@ electron-devtools-installer@^4.0.0: dependencies: unzip-crx-3 "^0.2.0" -electron-playwright-helpers@^2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/electron-playwright-helpers/-/electron-playwright-helpers-2.1.0.tgz#bafda622b236a53cdcfb7e8d62f3d62550fdbfce" - integrity sha512-aQOefS1irz/Ou6IYuTE34ZLmIKVHKoTGUwkVuwu2P5iguuMTLtsg6CFImsWu0cabRPVZ1NgEpcGPumFZNDdrqA== - dependencies: - "@electron/asar" "^3.2.4" - electron-publish@26.6.0: version "26.6.0" resolved "https://registry.yarnpkg.com/electron-publish/-/electron-publish-26.6.0.tgz#94e8f00c163376ebb6b296544c11b61d3afc027d"