From 02e41549a832a116e18ceb768e6a7d24227ccb9d Mon Sep 17 00:00:00 2001 From: vapormusic Date: Wed, 26 Jan 2022 20:59:54 +0700 Subject: [PATCH] add minimize to tray --- src/main/plugins/minimizeToTray.ts | 149 ++++++++++++++++++++++++++ src/renderer/audio/audio.js | 2 +- src/renderer/index.js | 17 +++ src/renderer/views/main.ejs | 4 +- src/renderer/views/pages/settings.ejs | 24 ++--- 5 files changed, 181 insertions(+), 15 deletions(-) create mode 100644 src/main/plugins/minimizeToTray.ts diff --git a/src/main/plugins/minimizeToTray.ts b/src/main/plugins/minimizeToTray.ts new file mode 100644 index 00000000..0bb13492 --- /dev/null +++ b/src/main/plugins/minimizeToTray.ts @@ -0,0 +1,149 @@ +import * as electron from 'electron'; +import * as path from 'path'; + + +export default class MinimizeToTray { + /** + * Private variables for interaction in plugins + */ + private _win: any; + private _app: any; + private _store: any; + private _tray: any; + + /** + * Base Plugin Details (Eventually implemented into a GUI in settings) + */ + public name: string = 'Minimize to tray'; + public description: string = 'Allow Cider to minimize to tray'; + public version: string = '1.0.0'; + public author: string = 'vapormusic'; + + constructor(app: any, store: any) { + this._app = app; + this._store = store; + } + + private SetContextMenu(visibility : any) { + let self = this + if (visibility) { + this._tray.setContextMenu(electron.Menu.buildFromTemplate([ + // { + // label: 'Check for Updates', + // click: function () { + // app.ame.utils.checkForUpdates(true) + // } + // }, + { + label: 'Minimize to Tray', + click: function () { + if (typeof self._win.hide === 'function') { + self._win.hide(); + self.SetContextMenu(false); + } + } + }, + { + label: 'Quit', + click: function () { + self._app.quit(); + } + } + ])); + } else { + this._tray.setContextMenu(electron.Menu.buildFromTemplate([ + // { + // label: 'Check for Updates', + // click: function () { + // this._app.ame.utils.checkForUpdates(true) + // } + // }, + { + label: `Show ${electron.app.getName()}`, + click: function () { + if (typeof self._win.show === 'function') { + self._win.show(); + self.SetContextMenu(true); + } + } + }, + { + label: 'Quit', + click: function () { + self._app.quit(); + } + } + ])); + } + return true + + } + + /** + * Runs on app ready + */ + onReady(win: any): void { + this._win = win; + const winTray = electron.nativeImage.createFromPath(path.join(__dirname, `../../resources/icons/icon.ico`)).resize({ + width: 32, + height: 32 + }) + const macTray = electron.nativeImage.createFromPath(path.join(__dirname, `../../resources/icons/icon.png`)).resize({ + width: 20, + height: 20 + }) + const linuxTray = electron.nativeImage.createFromPath(path.join(__dirname, `../../resources/icons/icon.png`)).resize({ + width: 32, + height: 32 + }) + let trayIcon : any ; + if (process.platform === "win32") { + trayIcon = winTray + } else if (process.platform === "linux") { + trayIcon = linuxTray + } else if (process.platform === "darwin") { + trayIcon = macTray + } + + this._tray = new electron.Tray(trayIcon) + this._tray.setToolTip(this._app.getName()); + this.SetContextMenu(true); + + this._tray.on('double-click', () => { + if (typeof this._win.show === 'function') { + if (this._win.isVisible()) { + this._win.focus() + } else { + this._win.show() + } + } + }) + electron.ipcMain.on("minimizeTray", (event, value) => { + // listen for close event + this._win.hide(); + this.SetContextMenu(false); + }); + } + + /** + * Runs on app stop + */ + onBeforeQuit(): void { + + } + + /** + * Runs on playback State Change + * @param attributes Music Attributes (attributes.state = current state) + */ + onPlaybackStateDidChange(attributes: object): void { + } + + /** + * Runs on song change + * @param attributes Music Attributes + */ + onNowPlayingItemDidChange(attributes: object): void { + } + +} diff --git a/src/renderer/audio/audio.js b/src/renderer/audio/audio.js index 034a7cb8..dbc619d5 100644 --- a/src/renderer/audio/audio.js +++ b/src/renderer/audio/audio.js @@ -124,7 +124,7 @@ var CiderAudio = { for (i = 1; i < BANDS.length; i ++) { CiderAudio.audioNodes.audioBands[i-1].connect(CiderAudio.audioNodes.audioBands[i]); } - CiderAudio.audioNodes.audioBands[ BANDS.length-1].connect(CiderAudio.context.destination); + CiderAudio.audioNodes.audioBands[BANDS.length-1].connect(CiderAudio.context.destination); } } diff --git a/src/renderer/index.js b/src/renderer/index.js index 13168409..eb66e9f2 100644 --- a/src/renderer/index.js +++ b/src/renderer/index.js @@ -3419,6 +3419,23 @@ const app = new Vue({ document.getElementsByClassName('song-name')[0].classList.add('marquee'); document.getElementsByClassName('song-name')[1].classList.add('marquee-after'); } + }, + closeWindow(){ + switch (app.cfg.general.close_behavior) { + case 0: + case '0': + ipcRenderer.send('close'); + break; + case 1: + case '1': + ipcRenderer.send('minimize'); + break; + case 2: + case '2': + ipcRenderer.send('minimizeTray'); + break; + + } } } diff --git a/src/renderer/views/main.ejs b/src/renderer/views/main.ejs index 5b2f56dd..0cfeab5b 100644 --- a/src/renderer/views/main.ejs +++ b/src/renderer/views/main.ejs @@ -41,7 +41,7 @@
-
+
@@ -174,7 +174,7 @@ @click="ipcRenderer.send('maximize')">
-
+
diff --git a/src/renderer/views/pages/settings.ejs b/src/renderer/views/pages/settings.ejs index 2bd44f66..ed3bdc2b 100644 --- a/src/renderer/views/pages/settings.ejs +++ b/src/renderer/views/pages/settings.ejs @@ -529,6 +529,18 @@ +
+
+ Close Button Behavior +
+
+ +
+
@@ -581,18 +593,6 @@
-
-
- Close Button Behavior -
-
- -
-
Open Cider on Startup