From 65a3df3e08f3628133c4e3d2f2522e3217faa65a Mon Sep 17 00:00:00 2001 From: Ramiro Saenz Date: Thu, 1 Sep 2016 17:29:42 -0300 Subject: [PATCH] Notifications and Dont Disturb Improve disabling Notifications using Electron methods. Persistent Dont Disturb. Fixes #178 Fixes #249 --- app/Application.js | 3 +++ app/ux/WebView.js | 21 +++++---------------- app/view/main/Main.js | 3 ++- app/view/main/MainController.js | 8 +++----- electron/main.js | 10 +++++++++- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/app/Application.js b/app/Application.js index 0aebaa06..6c978fc5 100644 --- a/app/Application.js +++ b/app/Application.js @@ -148,6 +148,9 @@ Ext.define('Rambox.Application', { this.checkUpdate(true); } + // Define default value + if ( localStorage.getItem('dontDisturb') === null ) localStorage.setItem('dontDisturb', false); + if ( localStorage.getItem('locked') ) { console.info('Lock Rambox:', 'Enabled'); Ext.cq1('app-main').getController().showLockWindow(); diff --git a/app/ux/WebView.js b/app/ux/WebView.js index 9605885e..9459490d 100644 --- a/app/ux/WebView.js +++ b/app/ux/WebView.js @@ -153,6 +153,9 @@ Ext.define('Rambox.ux.WebView',{ // Google Analytics Event ga_storage._trackEvent('Services', 'load', me.type, 1, true); + // Notifications in Webview + me.setNotifications(localStorage.getItem('locked') || JSON.parse(localStorage.getItem('dontDisturb')) ? false : me.record.get('notifications')); + // Show and hide spinner when is loading webview.addEventListener("did-start-loading", function() { console.info('Start loading...', me.src); @@ -192,15 +195,7 @@ Ext.define('Rambox.ux.WebView',{ webview.addEventListener("dom-ready", function(e) { // Mute Webview - if ( me.muted || localStorage.getItem('locked') ) me.setAudioMuted(true); - - // Notifications in Webview - webview.executeJavaScript('var originalNotification = Notification;'); - if ( me.notifications ) { - me.setNotifications(me.notifications); - } else if ( localStorage.getItem('locked') ) { - me.setNotifications(false); - } + if ( me.record.get('muted') || localStorage.getItem('locked') || JSON.parse(localStorage.getItem('dontDisturb')) ) me.setAudioMuted(true); // Injected code to detect new messages if ( me.record ) { @@ -290,19 +285,13 @@ Ext.define('Rambox.ux.WebView',{ var me = this; var webview = me.down('component').el.dom; - if ( !notification ) { - webview.executeJavaScript('(function() { Notification = function() { } })();'); - } else { - webview.executeJavaScript('(function() { Notification = originalNotification })();'); - } + ipc.send('setServiceNotifications', webview.partition, notification); } ,setOffline: function(btn, e) { var me = this; var webview = me.down('component').el.dom; - console.log(btn, e); - console.info(me.type, 'Going '+ (!btn.offline ? 'offline' : 'online') + '...'); webview.getWebContents().session.setProxy({ proxyRules: !btn.offline ? 'offline' : '' }, Ext.emptyFn); diff --git a/app/view/main/Main.js b/app/view/main/Main.js index c86e1e4d..4ab3a4f0 100644 --- a/app/view/main/Main.js +++ b/app/view/main/Main.js @@ -214,12 +214,13 @@ Ext.define('Rambox.view.main.Main', { ,items: [ { glyph: 'xf1f7@FontAwesome' - ,text: 'Don\'t Disturb: OFF' + ,text: 'Don\'t Disturb: '+(JSON.parse(localStorage.getItem('dontDisturb')) ? 'ON' : 'OFF') ,tooltip: 'Disable notifications and sounds in all services. Perfect to be concentrated and focused.
Shortcut key: F1' ,enableToggle: true ,handler: 'dontDisturb' ,reference: 'disturbBtn' ,id: 'disturbBtn' + ,pressed: JSON.parse(localStorage.getItem('dontDisturb')) } ,{ glyph: 'xf023@FontAwesome' diff --git a/app/view/main/MainController.js b/app/view/main/MainController.js index a6df1d09..3efe7643 100644 --- a/app/view/main/MainController.js +++ b/app/view/main/MainController.js @@ -797,13 +797,11 @@ Ext.define('Rambox.view.main.MainController', { tab.setAudioMuted(btn.pressed); // Prevent Notifications - if ( btn.pressed ) { - tab.down('component').el.dom.getWebContents().executeJavaScript('var originalNotification = Notification; (function() { Notification = function() { } })();'); - } else { - tab.down('component').el.dom.getWebContents().executeJavaScript('(function() { Notification = originalNotification })();'); - } + tab.setNotifications(!btn.pressed); }); + localStorage.setItem('dontDisturb', btn.pressed); + btn.setText('Don\'t Disturb: ' + ( btn.pressed ? 'ON' : 'OFF' )); // If this method is called from Lock method, prevent showing toast diff --git a/electron/main.js b/electron/main.js index c27663d9..73dc0903 100644 --- a/electron/main.js +++ b/electron/main.js @@ -1,6 +1,6 @@ 'use strict'; -const {app, protocol, BrowserWindow, dialog, shell, Menu, ipcMain, nativeImage} = require('electron'); +const {app, protocol, BrowserWindow, dialog, shell, Menu, ipcMain, nativeImage, session} = require('electron'); // Menu const appMenu = require('./menu'); // Tray @@ -237,6 +237,14 @@ ipcMain.on('setConfig', function(event, values) { values.auto_launch ? appLauncher.enable() : appLauncher.disable(); }); +// Handle Service Notifications +ipcMain.on('setServiceNotifications', function(event, partition, op) { + session.fromPartition(partition).setPermissionRequestHandler(function(webContents, permission, callback) { + if (permission === 'notifications') return callback(op); + callback(true) + }); +}); + const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => { // Someone tried to run a second instance, we should focus our window. if (mainWindow) {