From 407d050211e419b5b59b3276141876ff4905978c Mon Sep 17 00:00:00 2001 From: Jose Ouellet Date: Mon, 9 Jul 2018 01:18:41 -0400 Subject: [PATCH] Notification on app icon --- src/background.custom.js | 8 ++++++-- src/scripts/start.js | 37 ++++++++++++++++++++++++++++++++++++- src/scripts/tray.js | 1 - 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/background.custom.js b/src/background.custom.js index 602d65807a..1c27ae0db5 100644 --- a/src/background.custom.js +++ b/src/background.custom.js @@ -3,13 +3,12 @@ // It doesn't have any windows which you can see on screen, but we can open // window from here. -import { app, ipcMain, BrowserWindow } from 'electron'; +import { app, BrowserWindow, ipcMain, nativeImage, } from 'electron'; import windowStateKeeper from './background/windowState'; import certificate from './background/certificate'; import idle from '@paulcbetts/system-idle-time'; import { checkForUpdates } from './background/autoUpdate'; - process.env.GOOGLE_API_KEY = 'AIzaSyADqUh_c1Qhji3Cp1NE43YrcpuPkmhXD-c'; let screenshareEvent; @@ -124,6 +123,11 @@ export function afterMainWindow (mainWindow) { event.returnValue = idle.getIdleTime(); }); + ipcMain.on('update-taskbar-icon', (event, data, text) => { + const img = nativeImage.createFromDataURL(data); + mainWindow.setOverlayIcon(img, text); + }); + certificate.initWindow(mainWindow); checkForUpdates(); diff --git a/src/scripts/start.js b/src/scripts/start.js index c8bc1f6e17..fb67c2fe7b 100644 --- a/src/scripts/start.js +++ b/src/scripts/start.js @@ -1,6 +1,6 @@ /* globals $ */ -import { remote, ipcRenderer } from 'electron'; +import { ipcRenderer, remote } from 'electron'; import i18n from '../i18n/index.js'; import servers from './servers'; import sidebar from './sidebar'; @@ -14,6 +14,17 @@ sidebar.on('badge-setted', function () { if (process.platform === 'darwin') { remote.app.dock.setBadge(badge.title); } + if (process.platform === 'win32') { + const mainWindow = remote.getCurrentWindow(); + if (badge.showAlert) { + if (!mainWindow.isFocused()) { + mainWindow.flashFrame(true); + } + mainWindow.webContents.send('render-taskbar-icon', badge.count); + } else { + mainWindow.setOverlayIcon(null, ''); + } + } tray.showTrayAlert(badge.showAlert, badge.title); }); @@ -123,6 +134,30 @@ export const start = function () { validateHost().then(function () {}, function () {}); }); + ipcRenderer.on('render-taskbar-icon', (event, messageCount) => { + // Create a canvas from unread messages + function createOverlayIcon (messageCount) { + const canvas = document.createElement('canvas'); + canvas.height = 128; + canvas.width = 128; + + const ctx = canvas.getContext('2d'); + ctx.beginPath(); + + ctx.fillStyle = 'red'; + ctx.arc(64, 64, 64, 0, 2 * Math.PI); + ctx.fill(); + ctx.fillStyle = '#ffffff'; + ctx.textAlign = 'center'; + canvas.style.letterSpacing = '-4px'; + ctx.font = 'bold 92px sans-serif'; + ctx.fillText(String(Math.min(99, messageCount)), 64, 98); + + return canvas; + } + ipcRenderer.send('update-taskbar-icon', createOverlayIcon(messageCount).toDataURL(), String(messageCount)); + }); + const submit = function () { validateHost().then(function () { const input = form.querySelector('[name="host"]'); diff --git a/src/scripts/tray.js b/src/scripts/tray.js index 26a5fef219..0a5385e2f2 100644 --- a/src/scripts/tray.js +++ b/src/scripts/tray.js @@ -126,7 +126,6 @@ function showTrayAlert (showAlert, title, status = 'online') { return; } - mainWindow.flashFrame(showAlert, title); const trayImagePath = getTrayIcon(process.platform, showAlert, getImageTitle(title), status); mainWindow.tray.setImage(trayImagePath);