feat(electron): add minimize to tray option to settings page

This commit is contained in:
qier222 2021-01-29 19:55:02 +08:00
parent 09c54486cc
commit de7d008c0b
8 changed files with 67 additions and 13 deletions

View File

@ -43,7 +43,7 @@ class Background {
this.createExpressApp(); this.createExpressApp();
// init ipcMain // init ipcMain
initIpcMain(this.window); initIpcMain(this.window, this.store);
// Scheme must be registered before the app is ready // Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([ protocol.registerSchemesAsPrivileged([
@ -103,11 +103,6 @@ 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);
@ -172,7 +167,11 @@ class Background {
}); });
this.window.on("minimize", () => { this.window.on("minimize", () => {
if (process.platform === "win32") { if (
["win32", "linux"].includes(process.platform) &&
this.store.get("settings.minimizeToTray")
) {
this.tray = createTray(this.window);
this.window.hide(); this.window.hide();
} }
}); });

View File

@ -1,7 +1,7 @@
import { app, ipcMain } from "electron"; import { app, ipcMain } from "electron";
import match from "@njzy/unblockneteasemusic"; import match from "@njzy/unblockneteasemusic";
export function initIpcMain(win) { export function initIpcMain(win, store) {
ipcMain.on("unblock-music", (event, track) => { ipcMain.on("unblock-music", (event, track) => {
// 兼容 unblockneteasemusic 所使用的 api 字段 // 兼容 unblockneteasemusic 所使用的 api 字段
track.alias = track.alia || []; track.alias = track.alia || [];
@ -34,4 +34,8 @@ export function initIpcMain(win) {
ipcMain.on("minimize", () => { ipcMain.on("minimize", () => {
win.minimize(); win.minimize();
}); });
ipcMain.on("settings", (event, options) => {
store.set("settings", options);
});
} }

View File

@ -14,11 +14,8 @@ export function createTray(win) {
tray.setToolTip("YesPlayMusic"); tray.setToolTip("YesPlayMusic");
tray.on("click", () => { tray.on("click", () => {
if (win && win.isVisible()) { win.show();
win.hide(); tray.destroy();
} else {
win.show();
}
}); });
tray.on("right-click", () => { tray.on("right-click", () => {

View File

@ -7,10 +7,15 @@ import { changeAppearance } from "@/utils/common";
import Player from "@/utils/Player"; import Player from "@/utils/Player";
// vuex 自定义插件 // vuex 自定义插件
import saveToLocalStorage from "./plugins/localStorage"; import saveToLocalStorage from "./plugins/localStorage";
import { getSendSettingsPlugin } from "./plugins/sendSettings";
Vue.use(Vuex); Vue.use(Vuex);
let plugins = [saveToLocalStorage]; let plugins = [saveToLocalStorage];
if (process.env.IS_ELECTRON === true) {
let sendSettings = getSendSettingsPlugin();
plugins.push(sendSettings);
}
const options = { const options = {
state, state,
mutations, mutations,

View File

@ -13,6 +13,7 @@ let localStorage = {
automaticallyCacheSongs: false, automaticallyCacheSongs: false,
nyancatStyle: false, nyancatStyle: false,
showLyricsTranslation: true, showLyricsTranslation: true,
minimizeToTray: false,
}, },
data: { data: {
user: {}, user: {},

View File

@ -0,0 +1,13 @@
export function getSendSettingsPlugin() {
const electron = window.require("electron");
const ipcRenderer = electron.ipcRenderer;
return (store) => {
store.subscribe((mutation, state) => {
console.log(mutation);
if (mutation.type !== "updateSettings") return;
ipcRenderer.send("settings", {
minimizeToTray: state.settings.minimizeToTray,
});
});
};
}

View File

@ -9,6 +9,7 @@ const updateSetting = () => {
automaticallyCacheSongs, automaticallyCacheSongs,
nyancatStyle, nyancatStyle,
showLyricsTranslation, showLyricsTranslation,
minimizeToTray,
} = initLocalStorage.settings; } = initLocalStorage.settings;
const settings = { const settings = {
playlistCategories, playlistCategories,
@ -16,6 +17,7 @@ const updateSetting = () => {
automaticallyCacheSongs, automaticallyCacheSongs,
nyancatStyle, nyancatStyle,
showLyricsTranslation, showLyricsTranslation,
minimizeToTray,
...parsedSettings, ...parsedSettings,
}; };

View File

@ -125,6 +125,22 @@
</div> </div>
</div> </div>
</div> </div>
<div class="item" v-if="isElectron && !isMac">
<div class="left">
<div class="title">最小化到托盘</div>
</div>
<div class="right">
<div class="toggle">
<input
type="checkbox"
name="minimize-to-tray"
id="minimize-to-tray"
v-model="minimizeToTray"
/>
<label for="minimize-to-tray"></label>
</div>
</div>
</div>
<div class="item"> <div class="item">
<div class="left"> <div class="left">
<div class="title"> {{ $t("settings.showGitHubIcon") }} </div> <div class="title"> {{ $t("settings.showGitHubIcon") }} </div>
@ -215,6 +231,12 @@ export default {
}, },
computed: { computed: {
...mapState(["settings", "data"]), ...mapState(["settings", "data"]),
isElectron() {
return process.env.IS_ELECTRON;
},
isMac() {
return /macintosh|mac os x/i.test(navigator.userAgent);
},
lang: { lang: {
get() { get() {
return this.settings.lang; return this.settings.lang;
@ -321,6 +343,17 @@ export default {
}); });
}, },
}, },
minimizeToTray: {
get() {
return this.settings.minimizeToTray;
},
set(value) {
this.$store.commit("updateSettings", {
key: "minimizeToTray",
value,
});
},
},
}, },
methods: { methods: {
logout() { logout() {