@@ -15,26 +15,21 @@ See the License for the specific language governing permissions and
1515limitations 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
2520let trayIcon = null ;
2621
2722exports . 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} ;
0 commit comments