-
Notifications
You must be signed in to change notification settings - Fork 18
/
tray.js
138 lines (127 loc) · 3.19 KB
/
tray.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var electron = require('electron'),
electronApp = electron.app,
path = require('path'),
url = require('url'),
appIcon = null,
BrowserWindow = electron.BrowserWindow,
mainWindow,
open = require('open'),
mainWindow,
BrowserWindow = electron.BrowserWindow,
argv,
serverPID = false,
trayOn = true,
darkOn = false,
httpPort = 9173;
if (process.argv[2]) {
argv = JSON.parse(process.argv[2]);
serverPID = argv.pid;
trayOn = argv.tray;
darkOn = argv.dark;
httpPort = argv.port;
}
var start = function () {
buildWindow();
if (trayOn) showTray();
};
var showTray = function() {
if (!appIcon) {
var icon = 'icon16.png';
if (darkOn) icon = 'icon16w.png';
appIcon = new electron.Tray(path.normalize(__dirname + '/../chrome/img/'+icon));
appIcon.setToolTip('Yez! is running');
appIcon.setContextMenu(menu);
appIcon.on('click', function () {
mainWindow.show()
});
}
};
var hideTray = function(i) {
if (appIcon) {
appIcon.destroy();
appIcon = null;
}
};
var buildWindow = function() {
if (!mainWindow) {
mainWindow = new BrowserWindow({width: 416, height: 507, show: false});
var yezPath = path.normalize(__dirname + '/..').replace(/\\/g, '/');
mainWindow.loadURL('file://'+yezPath+'/chrome/index.html');
mainWindow.on('minimize', mainWindow.hide);
mainWindow.on('closed', function() {
if (serverPID) process.kill(serverPID);
});
}
};
var setLight = function(i) {
darkOn = false;
if (appIcon) appIcon.setImage(path.normalize(__dirname + '/../chrome/img/icon16.png'));
};
var setDark = function (i) {
darkOn = true;
if (appIcon) appIcon.setImage(path.normalize(__dirname + '/../chrome/img/icon16w.png'));
};
var menu = electron.Menu.buildFromTemplate([{
label: 'Open Yez!',
click: function () { mainWindow.show(); }
}, {
label: 'Yez! in browser',
click: function () { open('http://localhost:'+httpPort) }
}, {
type: 'separator'
}, {
label: 'Light Theme',
type: 'radio',
checked: !darkOn,
click: function () {
setLight();
if (mainWindow) mainWindow.webContents.send('theme', 'light');
}
}, {
label: 'Dark Theme',
type: 'radio',
checked: darkOn,
click: function () {
setDark();
if (mainWindow) mainWindow.webContents.send('theme', 'dark');
}
}, {
type: 'separator'
}, {
label: 'View in Store',
click: function() {
open('https://chrome.google.com/webstore/detail/yez/acbhddemkmodoahhmnphpcfmcfgpjmap');
}
}, {
label: 'About Yez!',
click: function() {
open('https://github.com/krasimir/yez');
}
}, {
type: 'separator'
}, {
label: 'Hide',
click: function () {
hideTray();
if (mainWindow) mainWindow.webContents.send('tray', false);
}
}, {
label: 'Close',
click: function() {
if (serverPID) process.kill(serverPID);
electronApp.quit();
process.exit(0);
}
}]);
electron.ipcMain.on('data', function(event, data) {
if (data.action == 'tray') {
if (Boolean(data.show)) showTray();
else hideTray('icon');
}
if (data.action == 'theme') {
if (data.theme == 'light') setLight();
else setDark();
}
});
electronApp.on('ready', start);
console.log('Yez! electron task is running.');