From b394ec0899cbf9f7f7d09e26eb5d05e8ff31e536 Mon Sep 17 00:00:00 2001 From: njzy Date: Wed, 9 Dec 2020 19:50:50 +0800 Subject: [PATCH] refactor: hide window when close app (#78) fix: window can't be close by shortcutkey fix: tray does not show in Windows --- build/icons/menu@88.png | Bin 0 -> 1084 bytes src/background.js | 29 +++++++++++++++++++++++++---- src/electron/ipcMain.js | 5 +++-- src/electron/menu.js | 1 + src/electron/tray.js | 16 +++++++++++++++- 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 build/icons/menu@88.png diff --git a/build/icons/menu@88.png b/build/icons/menu@88.png new file mode 100644 index 0000000000000000000000000000000000000000..cc14a82d1a3de3faff2aaea28d180e0885c8780a GIT binary patch literal 1084 zcmV-C1jGA@P)ZVtY z+*8a+6OLu#leUB5(nv z;i>dQ7Z0wW{DC}Jq@5^(E8l(c0Fi1UwTJ>r8&Qd%b1gzqfrCIQ@R${|4aGji+#J6k z4z?sGJ%)_21m>ZdefwvMjVUN`m$X2*h=#m;Tg2JFeo3P9kPT<_DI&+W!2j*flgKBi zt;4BA7!tuh8rUBDB`S%hT=Clo*7c~)pL8Qgk^ zvDXH%I4AlHW??Bh3ADXpP|(r_(3(M8}ZC=IvL6I}$}Kxw#?p6KGi+$rEKWm4WJ zO+rbrH~5w^<^#P>1r3A?mtCjC8O)e0<{H5qd2&PIK5==2Q%}zP=MGSB6`LE}OnnYV zoV&`rvWE`u%opYYU+b#Nmbaa15oUV+3VCEQU*pPLvolT}+{Wx%!{x@J?`H)Hx0(*j zUE7IX)ig6TpG|*d{+JYOgRgQ@qV zgMk?Rwzjsv+D15nm(7b=D0{UlilQirq9}@@Ncjziaia>bFNlf&0000 { - win = null; + win.on("close", (e) => { + if (willQuitApp) { + /* the user tried to quit the app */ + win = null; + } else { + /* the user only tried to close the window */ + e.preventDefault(); + win.hide(); + } }); + // win.on("closed", () => { + // win = null; + // }); return win; } @@ -71,7 +85,7 @@ app.on("window-all-closed", () => { // On macOS it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform !== "darwin") { - app.quit(); + // app.quit(); } }); @@ -80,9 +94,16 @@ app.on("activate", () => { // dock icon is clicked and there are no other windows open. if (win === null) { createWindow(); + } else { + win.show(); } }); +/** + * 'before-quit' is emitted when Electron receives the signal to exit and wants to start closing windows + */ +app.on("before-quit", () => (willQuitApp = true)); + // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. diff --git a/src/electron/ipcMain.js b/src/electron/ipcMain.js index a631697..83a3ae8 100644 --- a/src/electron/ipcMain.js +++ b/src/electron/ipcMain.js @@ -33,8 +33,9 @@ export function initIpcMain(win) { }); ipcMain.on("close", () => { - win.close(); - app.quit(); + win.hide(); + // win.close(); + // app.quit(); }); ipcMain.on("minimize", () => { diff --git a/src/electron/menu.js b/src/electron/menu.js index 972fc9b..7965400 100644 --- a/src/electron/menu.js +++ b/src/electron/menu.js @@ -128,6 +128,7 @@ export function createMenu(win) { { label: "Window", submenu: [ + { role: "close" }, { role: "minimize" }, { role: "zoom" }, { role: "reload" }, diff --git a/src/electron/tray.js b/src/electron/tray.js index e624c19..04ab51f 100644 --- a/src/electron/tray.js +++ b/src/electron/tray.js @@ -1,5 +1,6 @@ +/* global __static */ import path from "path"; -import { nativeImage, Tray } from "electron"; +import { app, nativeImage, Tray, Menu } from "electron"; export function createTray(win) { let icon = nativeImage @@ -17,5 +18,18 @@ export function createTray(win) { win.show(); } }); + + tray.on("right-click", () => { + const contextMenu = Menu.buildFromTemplate([ + { + label: "Quit", + click: () => { + app.exit(); + }, + }, + ]); + tray.popUpContextMenu(contextMenu); + }); + return tray; }