feat(electron): add tray for Windows

master
qier222 4 years ago
parent 0665f53f0d
commit 15ac2b5815

@ -5,6 +5,7 @@ import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer";
import { startNeteaseMusicApi } from "./electron/services"; import { startNeteaseMusicApi } from "./electron/services";
import { initIpcMain } from "./electron/ipcMain.js"; import { initIpcMain } from "./electron/ipcMain.js";
import { createMenu } from "./electron/menu"; import { createMenu } from "./electron/menu";
import { createTray } from "@/electron/tray";
import { createTouchBar } from "./electron/touchBar"; import { createTouchBar } from "./electron/touchBar";
import { createDockMenu } from "./electron/dockMenu"; import { createDockMenu } from "./electron/dockMenu";
import { autoUpdater } from "electron-updater"; import { autoUpdater } from "electron-updater";
@ -15,6 +16,7 @@ import Store from "electron-store";
class Background { class Background {
constructor() { constructor() {
this.window = null; this.window = null;
this.tray = null;
this.store = new Store({ this.store = new Store({
windowWidth: { windowWidth: {
width: { type: "number", default: 1440 }, width: { type: "number", default: 1440 },
@ -101,6 +103,11 @@ class Background {
// hide menu bar on Microsoft Windows and Linux // hide menu bar on Microsoft Windows and Linux
this.window.setMenuBarVisibility(false); this.window.setMenuBarVisibility(false);
// create tray only for Microsoft windows
if (process.platform === "win32") {
this.tray = createTray(this.window);
}
if (process.env.WEBPACK_DEV_SERVER_URL) { if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode // Load the url of the dev server if in development mode
this.window.loadURL(process.env.WEBPACK_DEV_SERVER_URL); this.window.loadURL(process.env.WEBPACK_DEV_SERVER_URL);
@ -164,6 +171,12 @@ class Background {
this.store.set("window", { height, width }); this.store.set("window", { height, width });
}); });
this.window.on("minimize", () => {
if (process.platform === "win32") {
this.window.hide();
}
});
this.window.webContents.on("new-window", function (e, url) { this.window.webContents.on("new-window", function (e, url) {
e.preventDefault(); e.preventDefault();
shell.openExternal(url); shell.openExternal(url);

@ -0,0 +1,37 @@
/* global __static */
import path from "path";
import { app, nativeImage, Tray, Menu } from "electron";
export function createTray(win) {
let icon = nativeImage
.createFromPath(path.join(__static, "img/icons/menu@88.png"))
.resize({
height: 20,
width: 20,
});
let tray = new Tray(icon);
tray.setToolTip("YesPlayMusic");
tray.on("click", () => {
if (win && win.isVisible()) {
win.hide();
} else {
win.show();
}
});
tray.on("right-click", () => {
const contextMenu = Menu.buildFromTemplate([
{
label: "Quit",
click: () => {
app.exit();
},
},
]);
tray.popUpContextMenu(contextMenu);
});
return tray;
}
Loading…
Cancel
Save