Skip to content

Commit

Permalink
[NEW] Show account status in menu bar (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyTobi authored and gdelavald committed Jul 4, 2018
1 parent 0d09d00 commit d52dc12
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/public/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/scripts/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
48 changes: 29 additions & 19 deletions src/scripts/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 () {
Expand Down
5 changes: 5 additions & 0 deletions src/scripts/webview.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit d52dc12

Please sign in to comment.