Skip to content

Commit

Permalink
refactor: hide window when close app (qier222#78)
Browse files Browse the repository at this point in the history
fix: window can't be close by shortcutkey

fix: tray does not show in Windows
  • Loading branch information
njzydark authored Dec 9, 2020
1 parent f68ae5c commit b394ec0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
Binary file added build/icons/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 25 additions & 4 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const isDevelopment = process.env.NODE_ENV !== "production";
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;
// eslint-disable-next-line no-unused-vars
let tray;

let willQuitApp = false;

// ipcMain
initIpcMain(win);
Expand All @@ -39,7 +43,7 @@ function createWindow() {
win.setMenuBarVisibility(false);

if (process.platform !== "darwin") {
createTray(win);
tray = createTray(win);
}

if (process.env.WEBPACK_DEV_SERVER_URL) {
Expand All @@ -60,9 +64,19 @@ function createWindow() {
e.preventDefault();
shell.openExternal(url);
});
win.on("closed", () => {
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;
}

Expand All @@ -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();
}
});

Expand All @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions src/electron/ipcMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ export function initIpcMain(win) {
});

ipcMain.on("close", () => {
win.close();
app.quit();
win.hide();
// win.close();
// app.quit();
});

ipcMain.on("minimize", () => {
Expand Down
1 change: 1 addition & 0 deletions src/electron/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export function createMenu(win) {
{
label: "Window",
submenu: [
{ role: "close" },
{ role: "minimize" },
{ role: "zoom" },
{ role: "reload" },
Expand Down
16 changes: 15 additions & 1 deletion src/electron/tray.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}

0 comments on commit b394ec0

Please sign in to comment.