From 212f29c5d7ce15cf205adcd17063258b0be306ff Mon Sep 17 00:00:00 2001 From: Mahadevan Sreenivasan Date: Fri, 10 Apr 2020 12:41:22 +0530 Subject: Add notification & audio toggle action in tray context menu (#542) * feat: Add new tray menu item to enable / disable Notifications and Audio - Use ipcMain to listen to iniital App Settings which determines the current isAppMuted state and display menu item text accordingly - Use ipcMain to listen to updates to App Settings -> isAppMuted from the App Menu / Side Bar and display menu item text accordingly - When the user clicks on the Enable / Disable Notifications & Audio menu item in the Tray, pass a message to the app via window.webContents.send with channel 'muteApp' - In stores/AppStore.js, use an ipcRenderer to toggleMuteApp() - To get the initial app state to the main process, pass a channel 'initialAppSettings' from SettingsStore which the tray class can listen to. * feat - System Tray menu item for Muting App - Fix lint errors * feat: Provide a menu item in the tray bar icon to Mute/Unmute the application - Revert package-lock.json to the previous commit. --- src/lib/Tray.js | 66 +++++++++++++++++++++++++++++++-------------- src/stores/AppStore.js | 4 +++ src/stores/SettingsStore.js | 1 + 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/lib/Tray.js b/src/lib/Tray.js index 84ee8e3bb..3700cca27 100644 --- a/src/lib/Tray.js +++ b/src/lib/Tray.js @@ -1,5 +1,5 @@ import { - app, Menu, nativeImage, nativeTheme, systemPreferences, Tray, + app, Menu, nativeImage, nativeTheme, systemPreferences, Tray, ipcMain, } from 'electron'; import path from 'path'; @@ -14,29 +14,55 @@ export default class TrayIcon { themeChangeSubscriberId = null; + trayMenu = null; + + trayMenuTemplate = [ + { + label: 'Show Ferdi', + click() { + if (app.mainWindow.isMinimized()) { + app.mainWindow.restore(); + } + app.mainWindow.show(); + app.mainWindow.focus(); + }, + }, + { + label: 'Disable Notifications & Audio', + click() { + app.mainWindow.webContents.send('muteApp'); + }, + }, + { + label: 'Quit Ferdi', + click() { + app.quit(); + }, + }, + ]; + + _updateTrayMenu(appSettings) { + if (appSettings.type === 'app') { + const { isAppMuted } = appSettings.data; + this.trayMenuTemplate[1].label = isAppMuted ? 'Enable Notifications && Audio' : 'Disable Notifications && Audio'; + this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate); + } + } + show() { if (this.trayIcon) return; this.trayIcon = new Tray(this._getAsset('tray', INDICATOR_TRAY_PLAIN)); - const trayMenuTemplate = [ - { - label: 'Show Ferdi', - click() { - if (app.mainWindow.isMinimized()) { - app.mainWindow.restore(); - } - app.mainWindow.show(); - app.mainWindow.focus(); - }, - }, { - label: 'Quit Ferdi', - click() { - app.quit(); - }, - }, - ]; - const trayMenu = Menu.buildFromTemplate(trayMenuTemplate); + this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate); + + ipcMain.on('initialAppSettings', (event, appSettings) => { + this._updateTrayMenu(appSettings); + }); + + ipcMain.on('updateAppSettings', (event, appSettings) => { + this._updateTrayMenu(appSettings); + }); this.trayIcon.on('click', () => { if (app.mainWindow.isMinimized()) { @@ -50,7 +76,7 @@ export default class TrayIcon { }); this.trayIcon.on('right-click', () => { - this.trayIcon.popUpContextMenu(trayMenu); + this.trayIcon.popUpContextMenu(this.trayMenu); }); if (process.platform === 'darwin') { diff --git a/src/stores/AppStore.js b/src/stores/AppStore.js index 593bf341e..c8adb9c3c 100644 --- a/src/stores/AppStore.js +++ b/src/stores/AppStore.js @@ -179,6 +179,10 @@ export default class AppStore extends Store { this.stores.router.push(url); }); + ipcRenderer.on('muteApp', () => { + this._toggleMuteApp(); + }); + this.locale = this._getDefaultLocale(); setTimeout(() => { diff --git a/src/stores/SettingsStore.js b/src/stores/SettingsStore.js index 799ba3f5a..227eb2145 100644 --- a/src/stores/SettingsStore.js +++ b/src/stores/SettingsStore.js @@ -110,6 +110,7 @@ export default class SettingsStore extends Store { } debug('Get appSettings resolves', resp.type, resp.data); Object.assign(this._fileSystemSettingsCache[resp.type], resp.data); + ipcRenderer.send('initialAppSettings', resp); }); this.fileSystemSettingsTypes.forEach((type) => { -- cgit v1.2.3-54-g00ecf