Browse Source

Fixes #229

pull/291/merge
Ramiro Saenz 9 years ago
parent
commit
8506cbc1ad
  1. 50
      app.js
  2. 16
      electron/main.js

50
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());
});

16
electron/main.js

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const {app, protocol, BrowserWindow, dialog, shell, Menu, ipcMain} = require('electron'); const {app, protocol, BrowserWindow, dialog, shell, Menu, ipcMain, nativeImage} = require('electron');
// Menu // Menu
const appMenu = require('./menu'); const appMenu = require('./menu');
// Tray // Tray
@ -183,12 +183,24 @@ function createWindow () {
function updateBadge(title) { function updateBadge(title) {
var messageCount = title.match(/\d+/g) ? parseInt(title.match(/\d+/g).join("")) : 0; 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); tray.setBadge(messageCount);
if (messageCount === 0) {
mainWindow.setOverlayIcon(null, "");
return;
} }
mainWindow.webContents.send('setBadge', messageCount);
} else { // Linux and macOS
app.setBadgeCount(messageCount); 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) => { const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window. // Someone tried to run a second instance, we should focus our window.

Loading…
Cancel
Save