From 8506cbc1ad61cce6c8810db4a1d06001f7b45c12 Mon Sep 17 00:00:00 2001 From: Ramiro Saenz Date: Wed, 24 Aug 2016 21:00:31 -0300 Subject: [PATCH] Fixes #229 --- app.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ electron/main.js | 20 +++++++++++++++---- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index e779ccc0..d9db1688 100644 --- a/app.js +++ b/app.js @@ -82,3 +82,53 @@ ipc.on('autoUpdater:update-downloaded', function(e, releaseNotes, releaseName, r ] }); }); + +// Set Badge in taskbar for Windows +ipc.on('setBadge', (event, messageCount) => { + messageCount = messageCount.toString(); + var canvas = document.createElement("canvas"); + canvas.height = 140; + canvas.width = 140; + var ctx = canvas.getContext("2d"); + ctx.fillStyle = "red"; + ctx.beginPath(); + ctx.ellipse(70, 70, 70, 70, 0, 0, 2 * Math.PI); + ctx.fill(); + ctx.textAlign = "center"; + ctx.fillStyle = "white"; + + var ranges = [ + { divider: 1e18 , suffix: 'P' }, + { divider: 1e15 , suffix: 'E' }, + { divider: 1e12 , suffix: 'T' }, + { divider: 1e9 , suffix: 'G' }, + { divider: 1e6 , suffix: 'M' }, + { divider: 1e3 , suffix: 'k' } + ]; + + function formatNumber(n) { + n = parseInt(n); + for (var i = 0; i < ranges.length; i++) { + if (n >= ranges[i].divider) { + return Math.round(n / ranges[i].divider).toString() + ranges[i].suffix; + } + } + return n.toString(); + } + + if (messageCount.length === 3) { + ctx.font = "75px sans-serif"; + ctx.fillText("" + messageCount, 70, 98); + } else if (messageCount.length === 2) { + ctx.font = "100px sans-serif"; + ctx.fillText("" + messageCount, 70, 105); + } else if (messageCount.length === 1) { + ctx.font = "125px sans-serif"; + ctx.fillText("" + messageCount, 70, 112); + } else { + ctx.font = "75px sans-serif"; + ctx.fillText("" + formatNumber(messageCount), 70, 98); + } + + ipc.send('setBadge', messageCount, canvas.toDataURL()); +}); diff --git a/electron/main.js b/electron/main.js index 8eae92c6..49ab6239 100644 --- a/electron/main.js +++ b/electron/main.js @@ -1,6 +1,6 @@ 'use strict'; -const {app, protocol, BrowserWindow, dialog, shell, Menu, ipcMain} = require('electron'); +const {app, protocol, BrowserWindow, dialog, shell, Menu, ipcMain, nativeImage} = require('electron'); // Menu const appMenu = require('./menu'); // Tray @@ -183,13 +183,25 @@ function createWindow () { function updateBadge(title) { var messageCount = title.match(/\d+/g) ? parseInt(title.match(/\d+/g).join("")) : 0; - if (process.platform === 'win32') { + if (process.platform === 'win32') { // Windows tray.setBadge(messageCount); - } - app.setBadgeCount(messageCount); + if (messageCount === 0) { + mainWindow.setOverlayIcon(null, ""); + return; + } + + mainWindow.webContents.send('setBadge', messageCount); + } else { // Linux and macOS + app.setBadgeCount(messageCount); + } } +ipcMain.on('setBadge', function(event, messageCount, value) { + var img = nativeImage.createFromDataURL(value); + mainWindow.setOverlayIcon(img, messageCount.toString()); +}); + const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => { // Someone tried to run a second instance, we should focus our window. if (mainWindow) {