Skip to content

Commit d52dc12

Browse files
HappyTobigdelavald
authored andcommitted
[NEW] Show account status in menu bar (RocketChat#759)
1 parent 0d09d00 commit d52dc12

File tree

5 files changed

+42
-27
lines changed

5 files changed

+42
-27
lines changed

src/public/preload.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function customWindowOpen (url, frameName, features) {
2424

2525
window.open = customWindowOpen;
2626

27-
const events = ['unread-changed', 'get-sourceId'];
27+
const events = ['unread-changed', 'get-sourceId', 'user-status-manually-set'];
2828

2929
events.forEach(function (e) {
3030
window.addEventListener(e, function (event) {

src/scripts/sidebar.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,11 @@ class SideBar extends EventEmitter {
259259
}
260260
}
261261

262-
if (count > 0) {
263-
return String(count);
264-
} else {
265-
return alert;
266-
}
262+
return {
263+
count: count,
264+
showAlert: (!isNaN(parseInt(count)) && count > 0),
265+
title: alert
266+
};
267267
}
268268

269269
hide () {

src/scripts/start.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ sidebar.on('badge-setted', function () {
1212
const badge = sidebar.getGlobalBadge();
1313

1414
if (process.platform === 'darwin') {
15-
remote.app.dock.setBadge(badge);
15+
remote.app.dock.setBadge(badge.title);
1616
}
17-
tray.showTrayAlert(!isNaN(parseInt(badge)) && badge > 0, badge);
17+
tray.showTrayAlert(badge.showAlert, badge.title);
1818
});
1919

2020
export const start = function () {

src/scripts/tray.js

+29-19
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ const icons = {
1919

2020
darwin: {
2121
dir: 'osx',
22-
icon: 'icon-trayTemplate.png'
22+
icon: 'icon-trayTemplate.png',
23+
iconAlert: 'icon-tray-alert.png',
24+
title: {
25+
online: '\u001B[32m',
26+
away: '\u001B[33m',
27+
busy: '\u001B[31m',
28+
offline: '\u001B[30m'
29+
}
2330
}
2431
};
2532

2633
const _iconTray = path.join(__dirname, 'images', icons[process.platform].dir, icons[process.platform].icon || 'icon-tray.png');
27-
const _iconTrayAlert = path.join(__dirname, 'images', icons[process.platform].dir, icons[process.platform].iconAlert || 'icon-tray-alert.png');
2834

2935
function createAppTray () {
3036
const _tray = new Tray(_iconTray);
@@ -95,34 +101,38 @@ function createAppTray () {
95101
};
96102
}
97103

98-
function setImage (title) {
104+
function getImageTitle (title) {
99105
if (title === '•') {
100-
title = "Dot";
106+
return "Dot";
101107
} else if (!isNaN(parseInt(title)) && title > 9) {
102-
title = "9Plus";
108+
return "9Plus";
103109
}
110+
}
104111

105-
const _iconPath = path.join(__dirname, 'images', icons[process.platform].dir, `icon-tray${title}.png`);
106-
mainWindow.tray.setImage(_iconPath);
112+
function getTrayIcon (platform, showAlert, title, status) {
113+
if (platform !== 'darwin') {
114+
return path.join(__dirname, 'images', icons[process.platform].dir, `icon-tray${title}-${status}.png`);
115+
}
116+
117+
if (showAlert) {
118+
return path.join(__dirname, 'images', icons[process.platform].dir, icons[process.platform].iconAlert ||`icon-tray-alert-${status}Template.png`);
119+
} else {
120+
return path.join(__dirname, 'images', icons[process.platform].dir, icons[process.platform].icon ||`icon-tray-${status}Template.png`);
121+
}
107122
}
108123

109-
function showTrayAlert (showAlert, title) {
124+
function showTrayAlert (showAlert, title, status = 'online') {
110125
if (mainWindow.tray === null || mainWindow.tray === undefined) {
111126
return;
112127
}
113128

114-
mainWindow.flashFrame(showAlert);
115-
if (process.platform !== 'darwin') {
116-
setImage(title);
117-
} else {
118-
if (showAlert) {
119-
mainWindow.tray.setImage(_iconTrayAlert);
120-
} else {
121-
mainWindow.tray.setImage(_iconTray);
122-
}
123-
mainWindow.tray.setTitle(title);
124-
}
129+
mainWindow.flashFrame(showAlert, title);
130+
const trayImagePath = getTrayIcon(process.platform, showAlert, getImageTitle(title), status);
131+
mainWindow.tray.setImage(trayImagePath);
125132

133+
if (process.platform === 'darwin') {
134+
mainWindow.tray.setTitle(`${icons[process.platform].title[status]}${title}`);
135+
}
126136
}
127137

128138
function removeAppTray () {

src/scripts/webview.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { EventEmitter } from 'events';
22
import servers from './servers';
33
import sidebar from './sidebar';
4+
import tray from './tray';
45
import { desktopCapturer, ipcRenderer } from 'electron';
56

67
class WebView extends EventEmitter {
@@ -90,6 +91,10 @@ class WebView extends EventEmitter {
9091
case 'focus':
9192
servers.setActive(host.url);
9293
break;
94+
case 'user-status-manually-set':
95+
const badge = sidebar.getGlobalBadge();
96+
tray.showTrayAlert(badge.showAlert, badge.title, event.args[0]);
97+
break;
9398
case 'get-sourceId':
9499
desktopCapturer.getSources({types: ['window', 'screen']}, (error, sources) => {
95100
if (error) {

0 commit comments

Comments
 (0)