From 14d8f7353256740feb5cdb260dd96e04256b63fd Mon Sep 17 00:00:00 2001 From: Magnus Hauge Bakke Date: Fri, 1 Apr 2016 21:50:34 +0200 Subject: [PATCH] Electron: Set app badge in os x (darwin) --- electron-globals.js | 9 +++++++++ electron-main.js | 8 +++++--- electron-start.html | 2 +- js/glowingbear.js | 1 + js/notifications.js | 13 +++++++++++++ 5 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 electron-globals.js diff --git a/electron-globals.js b/electron-globals.js new file mode 100644 index 0000000..9259511 --- /dev/null +++ b/electron-globals.js @@ -0,0 +1,9 @@ +var ipc = require('electron').ipcRenderer; + +var setElectronBadge = function(value) { + if (ipc && typeof ipc.send === 'function') { + ipc.send('badge', value); + } +}; + +global.setElectronBadge = setElectronBadge; diff --git a/electron-main.js b/electron-main.js index 4baf51a..43420a2 100644 --- a/electron-main.js +++ b/electron-main.js @@ -187,10 +187,12 @@ mainWindow = new BrowserWindow({width: 1280, height: 800, 'min-width': 1024, 'min-height': 600, 'autoHideMenuBar': true, 'web-security': true, 'java': false, 'icon':'file://'+__dirname + 'assets/img/favicon.png'}); mainWindow.loadUrl('file://' + __dirname + '/electron-start.html'); - /* + ipcMain.on('badge', function(event, arg) { - app.dock.setBadge(String(arg)); - });*/ + if (process.platform === "darwin") { + app.dock.setBadge(String(arg)); + } + }); mainWindow.on('devtools-opened', function() { mainWindow.webContents.executeJavaScript("document.getElementById('glowingbear').openDevTools();"); }); diff --git a/electron-start.html b/electron-start.html index 6e7e28a..c6f0710 100644 --- a/electron-start.html +++ b/electron-start.html @@ -26,6 +26,6 @@ onload = function() { - + diff --git a/js/glowingbear.js b/js/glowingbear.js index 271916a..54e1f04 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -378,6 +378,7 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', notifications.updateFavico(); } else { $rootScope.favico.reset(); + notifications.updateBadge(''); } }); diff --git a/js/notifications.js b/js/notifications.js index e99ecc5..61fa024 100644 --- a/js/notifications.js +++ b/js/notifications.js @@ -141,19 +141,31 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu bgColor: '#d00', textColor: '#fff' }); + updateBadge(notifications); } else { var unread = unreadCount('unread'); if (unread === 0) { $rootScope.favico.reset(); + updateBadge(''); } else { $rootScope.favico.badge(unread, { bgColor: '#5CB85C', textColor: '#ff0' }); + updateBadge("."); } } }; + var updateBadge = function(value) { + + // Get ipc + if (typeof setElectronBadge === 'function') { + setElectronBadge(value); + } + + }; + /* Function gets called from bufferLineAdded code if user should be notified */ var createHighlight = function(buffer, message) { var title = ''; @@ -204,6 +216,7 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu requestNotificationPermission: requestNotificationPermission, updateTitle: updateTitle, updateFavico: updateFavico, + updateBadge: updateBadge, createHighlight: createHighlight, cancelAll: cancelAll, unreadCount: unreadCount