Skip to content

Commit a9b5282

Browse files
authored
Merge pull request element-hq#3909 from vector-im/t3chguy/favicon_improvements
Electron Tray Improvements
2 parents d7f1e01 + 9352e5d commit a9b5282

6 files changed

Lines changed: 105 additions & 75 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
/key.pem
66
/lib
77
/node_modules
8-
/electron/node_modules
8+
/electron_app/node_modules
9+
/electron_app/dist
910
/packages/
1011
/webapp
1112
/.npmrc
1213
.DS_Store
1314
npm-debug.log
14-
electron/dist
15-
electron/pub
1615
/config.json
1716
/src/component-index.js

‎electron_app/src/electron-main.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,31 @@ function startAutoUpdate(update_base_url) {
161161
// no other way to catch this error).
162162
// Assuming we generally run from the console when developing,
163163
// this is far preferable.
164-
process.on('uncaughtException', function (error) {
164+
process.on('uncaughtException', function(error) {
165165
console.log("Unhandled exception", error);
166166
});
167167

168168
electron.ipcMain.on('install_update', installUpdate);
169169

170+
let focusHandlerAttached = false;
171+
electron.ipcMain.on('setBadgeCount', function(ev, count) {
172+
electron.app.setBadgeCount(count);
173+
if (process.platform === 'win32' && mainWindow && !mainWindow.isFocused()) {
174+
if (count > 0) {
175+
if (!focusHandlerAttached) {
176+
mainWindow.once('focus', () => {
177+
mainWindow.flashFrame(false);
178+
focusHandlerAttached = false;
179+
});
180+
focusHandlerAttached = true;
181+
}
182+
mainWindow.flashFrame(true);
183+
} else {
184+
mainWindow.flashFrame(false);
185+
}
186+
}
187+
});
188+
170189
electron.app.commandLine.appendSwitch('--enable-usermedia-screen-capturing');
171190

172191
const shouldQuit = electron.app.makeSingleInstance((commandLine, workingDirectory) => {

‎electron_app/src/tray.js‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,21 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
1717

18-
const path = require('path');
19-
const electron = require('electron');
20-
21-
const app = electron.app;
22-
const Tray = electron.Tray;
23-
const MenuItem = electron.MenuItem;
18+
const {app, Tray, Menu, nativeImage} = require('electron');
2419

2520
let trayIcon = null;
2621

2722
exports.hasTray = function hasTray() {
2823
return (trayIcon !== null);
29-
}
24+
};
3025

31-
exports.create = function (win, config) {
26+
exports.create = function(win, config) {
3227
// no trays on darwin
3328
if (process.platform === 'darwin' || trayIcon) {
3429
return;
3530
}
3631

37-
const toggleWin = function () {
32+
const toggleWin = function() {
3833
if (win.isVisible() && !win.isMinimized()) {
3934
win.hide();
4035
} else {
@@ -44,24 +39,48 @@ exports.create = function (win, config) {
4439
}
4540
};
4641

47-
const contextMenu = electron.Menu.buildFromTemplate([
42+
const contextMenu = Menu.buildFromTemplate([
4843
{
4944
label: 'Show/Hide ' + config.brand,
50-
click: toggleWin
45+
click: toggleWin,
5146
},
5247
{
53-
type: 'separator'
48+
type: 'separator',
5449
},
5550
{
5651
label: 'Quit',
57-
click: function () {
52+
click: function() {
5853
app.quit();
59-
}
60-
}
54+
},
55+
},
6156
]);
6257

6358
trayIcon = new Tray(config.icon_path);
6459
trayIcon.setToolTip(config.brand);
6560
trayIcon.setContextMenu(contextMenu);
6661
trayIcon.on('click', toggleWin);
62+
63+
let lastFavicon = null;
64+
win.webContents.on('page-favicon-updated', function(ev, favicons) {
65+
let newFavicon = config.icon_path;
66+
if (favicons && favicons.length > 0 && favicons[0].startsWith('data:')) {
67+
newFavicon = favicons[0];
68+
}
69+
70+
// No need to change, shortcut
71+
if (newFavicon === lastFavicon) return;
72+
lastFavicon = newFavicon;
73+
74+
// if its not default we have to construct into nativeImage
75+
if (newFavicon !== config.icon_path) {
76+
newFavicon = nativeImage.createFromDataURL(favicons[0]);
77+
}
78+
79+
trayIcon.setImage(newFavicon);
80+
win.setIcon(newFavicon);
81+
});
82+
83+
win.webContents.on('page-title-updated', function(ev, title) {
84+
trayIcon.setToolTip(title);
85+
});
6786
};

‎src/vector/platform/ElectronPlatform.js‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020
import VectorBasePlatform from './VectorBasePlatform';
2121
import dis from 'matrix-react-sdk/lib/dispatcher';
2222
import q from 'q';
23-
import electron, {remote} from 'electron';
23+
import electron, {remote, ipcRenderer} from 'electron';
2424

2525
remote.autoUpdater.on('update-downloaded', onUpdateDownloaded);
2626

@@ -58,16 +58,8 @@ export default class ElectronPlatform extends VectorBasePlatform {
5858
setNotificationCount(count: number) {
5959
if (this.notificationCount === count) return;
6060
super.setNotificationCount(count);
61-
// this sometimes throws because electron is made of fail:
62-
// https://github.com/electron/electron/issues/7351
63-
// For now, let's catch the error, but I suspect it may
64-
// continue to fail and we might just have to accept that
65-
// electron's remote RPC is a non-starter for now and use IPC
66-
try {
67-
remote.app.setBadgeCount(count);
68-
} catch (e) {
69-
console.error('Failed to set notification count', e);
70-
}
61+
62+
ipcRenderer.send('setBadgeCount', count);
7163
}
7264

7365
supportsNotifications(): boolean {

‎src/vector/platform/VectorBasePlatform.js‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,57 @@ See the License for the specific language governing permissions and
1717
limitations under the License.
1818
*/
1919

20-
import BasePlatform from 'matrix-react-sdk/lib/BasePlatform'
20+
import BasePlatform from 'matrix-react-sdk/lib/BasePlatform';
21+
import Favico from 'favico.js';
2122

2223
/**
2324
* Vector-specific extensions to the BasePlatform template
2425
*/
2526
export default class VectorBasePlatform extends BasePlatform {
27+
constructor() {
28+
super();
29+
30+
// The 'animations' are really low framerate and look terrible.
31+
// Also it re-starts the animationb every time you set the badge,
32+
// and we set the state each time, even if the value hasn't changed,
33+
// so we'd need to fix that if enabling the animation.
34+
this.favicon = new Favico({animation: 'none'});
35+
this._updateFavicon();
36+
}
37+
38+
_updateFavicon() {
39+
try {
40+
// This needs to be in in a try block as it will throw
41+
// if there are more than 100 badge count changes in
42+
// its internal queue
43+
let bgColor = "#d00",
44+
notif = this.notificationCount;
45+
46+
if (this.errorDidOccur) {
47+
notif = notif || "×";
48+
bgColor = "#f00";
49+
}
50+
51+
this.favicon.badge(notif, {
52+
bgColor: bgColor,
53+
});
54+
} catch (e) {
55+
console.warn(`Failed to set badge count: ${e.message}`);
56+
}
57+
}
58+
59+
setNotificationCount(count: number) {
60+
if (this.notificationCount === count) return;
61+
super.setNotificationCount(count);
62+
this._updateFavicon();
63+
}
64+
65+
setErrorStatus(errorDidOccur: boolean) {
66+
if (this.errorDidOccur === errorDidOccur) return;
67+
super.setErrorStatus(errorDidOccur);
68+
this._updateFavicon();
69+
}
70+
2671
/**
2772
* Check for the availability of an update to the version of the
2873
* app that's currently running.

‎src/vector/platform/WebPlatform.js‎

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ limitations under the License.
1818
*/
1919

2020
import VectorBasePlatform from './VectorBasePlatform';
21-
import Favico from 'favico.js';
2221
import request from 'browser-request';
2322
import dis from 'matrix-react-sdk/lib/dispatcher.js';
2423
import q from 'q';
@@ -27,49 +26,6 @@ import url from 'url';
2726
import UAParser from 'ua-parser-js';
2827

2928
export default class WebPlatform extends VectorBasePlatform {
30-
constructor() {
31-
super();
32-
this.runningVersion = null;
33-
// The 'animations' are really low framerate and look terrible.
34-
// Also it re-starts the animationb every time you set the badge,
35-
// and we set the state each time, even if the value hasn't changed,
36-
// so we'd need to fix that if enabling the animation.
37-
this.favicon = new Favico({animation: 'none'});
38-
this._updateFavicon();
39-
}
40-
41-
_updateFavicon() {
42-
try {
43-
// This needs to be in in a try block as it will throw
44-
// if there are more than 100 badge count changes in
45-
// its internal queue
46-
let bgColor = "#d00",
47-
notif = this.notificationCount;
48-
49-
if (this.errorDidOccur) {
50-
notif = notif || "×";
51-
bgColor = "#f00";
52-
}
53-
54-
this.favicon.badge(notif, {
55-
bgColor: bgColor,
56-
});
57-
} catch (e) {
58-
console.warn(`Failed to set badge count: ${e.message}`);
59-
}
60-
}
61-
62-
setNotificationCount(count: number) {
63-
if (this.notificationCount === count) return;
64-
super.setNotificationCount(count);
65-
this._updateFavicon();
66-
}
67-
68-
setErrorStatus(errorDidOccur: boolean) {
69-
if (this.errorDidOccur === errorDidOccur) return;
70-
super.setErrorStatus(errorDidOccur);
71-
this._updateFavicon();
72-
}
7329

7430
/**
7531
* Returns true if the platform supports displaying

0 commit comments

Comments
 (0)