From d52dc1205cc7173bbd2ce43b3b44caf4e8623a5e Mon Sep 17 00:00:00 2001 From: HappyTobi Date: Wed, 4 Jul 2018 19:42:20 +0200 Subject: [PATCH] [NEW] Show account status in menu bar (#759) --- src/public/preload.js | 2 +- src/scripts/sidebar.js | 10 ++++----- src/scripts/start.js | 4 ++-- src/scripts/tray.js | 48 +++++++++++++++++++++++++----------------- src/scripts/webview.js | 5 +++++ 5 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/public/preload.js b/src/public/preload.js index e4e27585e475..3773f5e847a5 100644 --- a/src/public/preload.js +++ b/src/public/preload.js @@ -24,7 +24,7 @@ function customWindowOpen (url, frameName, features) { window.open = customWindowOpen; -const events = ['unread-changed', 'get-sourceId']; +const events = ['unread-changed', 'get-sourceId', 'user-status-manually-set']; events.forEach(function (e) { window.addEventListener(e, function (event) { diff --git a/src/scripts/sidebar.js b/src/scripts/sidebar.js index caa650654a35..5cef88b341f2 100644 --- a/src/scripts/sidebar.js +++ b/src/scripts/sidebar.js @@ -259,11 +259,11 @@ class SideBar extends EventEmitter { } } - if (count > 0) { - return String(count); - } else { - return alert; - } + return { + count: count, + showAlert: (!isNaN(parseInt(count)) && count > 0), + title: alert + }; } hide () { diff --git a/src/scripts/start.js b/src/scripts/start.js index 4695d00253e2..c8bc1f6e1741 100644 --- a/src/scripts/start.js +++ b/src/scripts/start.js @@ -12,9 +12,9 @@ sidebar.on('badge-setted', function () { const badge = sidebar.getGlobalBadge(); if (process.platform === 'darwin') { - remote.app.dock.setBadge(badge); + remote.app.dock.setBadge(badge.title); } - tray.showTrayAlert(!isNaN(parseInt(badge)) && badge > 0, badge); + tray.showTrayAlert(badge.showAlert, badge.title); }); export const start = function () { diff --git a/src/scripts/tray.js b/src/scripts/tray.js index 1926d001cde9..26a5fef219c7 100644 --- a/src/scripts/tray.js +++ b/src/scripts/tray.js @@ -19,12 +19,18 @@ const icons = { darwin: { dir: 'osx', - icon: 'icon-trayTemplate.png' + icon: 'icon-trayTemplate.png', + iconAlert: 'icon-tray-alert.png', + title: { + online: '\u001B[32m', + away: '\u001B[33m', + busy: '\u001B[31m', + offline: '\u001B[30m' + } } }; const _iconTray = path.join(__dirname, 'images', icons[process.platform].dir, icons[process.platform].icon || 'icon-tray.png'); -const _iconTrayAlert = path.join(__dirname, 'images', icons[process.platform].dir, icons[process.platform].iconAlert || 'icon-tray-alert.png'); function createAppTray () { const _tray = new Tray(_iconTray); @@ -95,34 +101,38 @@ function createAppTray () { }; } -function setImage (title) { +function getImageTitle (title) { if (title === '•') { - title = "Dot"; + return "Dot"; } else if (!isNaN(parseInt(title)) && title > 9) { - title = "9Plus"; + return "9Plus"; } +} - const _iconPath = path.join(__dirname, 'images', icons[process.platform].dir, `icon-tray${title}.png`); - mainWindow.tray.setImage(_iconPath); +function getTrayIcon (platform, showAlert, title, status) { + if (platform !== 'darwin') { + return path.join(__dirname, 'images', icons[process.platform].dir, `icon-tray${title}-${status}.png`); + } + + if (showAlert) { + return path.join(__dirname, 'images', icons[process.platform].dir, icons[process.platform].iconAlert ||`icon-tray-alert-${status}Template.png`); + } else { + return path.join(__dirname, 'images', icons[process.platform].dir, icons[process.platform].icon ||`icon-tray-${status}Template.png`); + } } -function showTrayAlert (showAlert, title) { +function showTrayAlert (showAlert, title, status = 'online') { if (mainWindow.tray === null || mainWindow.tray === undefined) { return; } - mainWindow.flashFrame(showAlert); - if (process.platform !== 'darwin') { - setImage(title); - } else { - if (showAlert) { - mainWindow.tray.setImage(_iconTrayAlert); - } else { - mainWindow.tray.setImage(_iconTray); - } - mainWindow.tray.setTitle(title); - } + mainWindow.flashFrame(showAlert, title); + const trayImagePath = getTrayIcon(process.platform, showAlert, getImageTitle(title), status); + mainWindow.tray.setImage(trayImagePath); + if (process.platform === 'darwin') { + mainWindow.tray.setTitle(`${icons[process.platform].title[status]}${title}`); + } } function removeAppTray () { diff --git a/src/scripts/webview.js b/src/scripts/webview.js index 6831c0ddb9a6..ea3346b96d0d 100644 --- a/src/scripts/webview.js +++ b/src/scripts/webview.js @@ -1,6 +1,7 @@ import { EventEmitter } from 'events'; import servers from './servers'; import sidebar from './sidebar'; +import tray from './tray'; import { desktopCapturer, ipcRenderer } from 'electron'; class WebView extends EventEmitter { @@ -90,6 +91,10 @@ class WebView extends EventEmitter { case 'focus': servers.setActive(host.url); break; + case 'user-status-manually-set': + const badge = sidebar.getGlobalBadge(); + tray.showTrayAlert(badge.showAlert, badge.title, event.args[0]); + break; case 'get-sourceId': desktopCapturer.getSources({types: ['window', 'screen']}, (error, sources) => { if (error) {