mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-16 13:17:46 +00:00
feat(electron): add tray for Windows
This commit is contained in:
parent
0665f53f0d
commit
15ac2b5815
2 changed files with 50 additions and 0 deletions
|
|
@ -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);
|
||||||
|
|
|
||||||
37
src/electron/tray.js
Normal file
37
src/electron/tray.js
Normal file
|
|
@ -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…
Add table
Add a link
Reference in a new issue