-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
337 lines (296 loc) · 9.09 KB
/
Copy pathmain.js
File metadata and controls
337 lines (296 loc) · 9.09 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const {app, BrowserWindow, Menu, Tray, ipcMain} = require("electron");
const path = require("path");
const url = require("url");
const config = require("./config");
const util = require("./src/js/util");
const Store = require("electron-store");
/*
* Start in development mode
*/
// process.env.NODE_ENV = "development";
if (process.env.NODE_ENV !== "development") {
console.log = function() {}
}
/*
* Window names enumerations
*/
const k_winNames = Object.freeze({
main: "mainWindow",
tray: "trayWindow",
login: "loginWindow"
});
/*
* Paths to html files
*/
const k_viewPaths = Object.freeze({
index: "src/html/index.html",
tray: "src/html/tray.html"
});
/*
* Global variables
*/
let wins = {}; // keep global references to windows
let forceQuit = false;
const store = new Store();
function setMessageListener() {
ipcMain.on("login", (event, arg) => {
let indexSender = event.sender;
console.log("main.js received login message, arg=", arg);
let authWindow = new BrowserWindow({
width: 800,
height: 600,
show: false
});
wins[k_winNames.login] = authWindow;
function closeAuthWindow() {
new Promise(function(res, rej) {
authWindow.webContents.session.clearStorageData(res);
})
.then(function() {
console.log("cleared login window cache");
authWindow.webContents.clearHistory();
authWindow.destroy();
wins[k_winNames.login] = null;
authWindow = null;
});
}
authWindow.on("close", (event) => {
event.preventDefault();
closeAuthWindow();
});
authWindow.webContents.on("did-get-redirect-request", function(event, oldUrl, newUrl) {
console.log("get redirect request, event=", event, "oldUrl=", oldUrl, "newUrl=", newUrl);
var errorMatch = RegExp('[?&]error=([^&]*)').exec(newUrl);
var tokenMatch = RegExp('[?&#]access_token=([^&]*)').exec(newUrl);
var expiresMatch = RegExp('[?&]expires_in=([^&]*)').exec(newUrl);
if (errorMatch) {
indexSender.send("login-error", errorMatch[1]);
closeAuthWindow();
} else if (tokenMatch && expiresMatch) {
var expired_at = new Date((new Date()).getTime() + parseInt(expiresMatch[1]) * 1000);
console.log(tokenMatch[1]);
console.log(expired_at);
util.user.setToken(tokenMatch[1], expired_at);
indexSender.send("login-success");
closeAuthWindow();
}
});
let authUrl = config.shanbay.api_root + config.shanbay.auth_url + "?" +
"client_id=" + config.shanbay.client_id +
"&response_type=" + config.shanbay.response_type;
console.log("authUrl=", authUrl);
authWindow.loadURL(authUrl);
authWindow.show();
});
}
/*
* Create the main window
*/
function createMainWindow() {
wins[k_winNames.main] = new BrowserWindow({
width: 800,
height: 620,
title: "",
icon: path.join(__dirname, "icons/app/icon16.png"),
});
let win = wins[k_winNames.main];
win.setFullScreenable(false);
// don't close the window, unless it's forced to quit
// e.g. cmd+q / ctrl+q
win.on("close", (e) => {
console.log("main window received close");
if (!forceQuit) {
e.preventDefault();
win.hide();
}
})
win.on("closed", () => {
console.log("main window received closed");
wins[k_winNames.main] = null;
win = null;
});
// create the menu
const menuTemplate = [
{
label: '编辑',
submenu: [
{ label: "撤销", role: 'undo' },
{ label: "重做", role: 'redo' },
{ type: 'separator' },
{ label: "剪切", role: 'cut' },
{ label: "复制", role: 'copy' },
{ label: "粘贴", role: 'paste' },
{ label: "删除", role: 'delete' },
{ label: "全选", role: 'selectall' }
]
},
{
label: "窗口",
submenu: [
{ label: "最大化", role: "zoom" },
{ label: "最小化", role: "minimize" },
{ label: "窗口最前", role: "front" },
{ label: "关闭", role: "close" }
]
},
{
label: "视图",
submenu: [
{ label: "重载", role: "reload" },
{ label: "强制重载", role: "forcereload" },
{ label: "还原窗口大小", role: "resetzoom" },
{ label: "放大", role: "zoomin" },
{ label: "缩小", role: "zoomout" },
{ type: 'separator' },
{ label: "全屏/取消全屏", role: 'togglefullscreen' }
]
}
];
if (process.env.NODE_ENV === "development") {
menuTemplate.push({
label: "Dev",
submenu: [
{ label: "DevTool", role: "toggledevtools" }
]
})
}
if (process.platform
=== "darwin") {
menuTemplate.unshift({
label: app.getName(),
submenu: [
// {role: 'about'},
// {type: 'separator'},
// {
// role: 'services', submenu: []
// },
{
label: "隐藏",
role: 'hide'
},
{
label: "隐藏其它",
role: 'hideothers'
},
{
label: "取消隐藏",
role: 'unhide'
},
{type: 'separator'},
{
label: "退出",
role: 'quit'
}
]
});
}
const menu = Menu.buildFromTemplate(menuTemplate)
Menu.setApplicationMenu(menu)
menu.on("click", function() {
console.log("Menu clicked");
});
win.loadURL(url.format({
pathname: path.join(__dirname, k_viewPaths.index),
protocol: "file:",
slashes: true
}
));
}
/*
* Create the tray popup window
*
*/
function createTrayWindow(bounds) {
console.log(bounds);
let option = {
width: 280,
height: 70,
title: "",
type: "textured",
resizable: false,
movable: false,
minimizable: false,
maximizable: false,
skipTaskbar: true,
frame: false,
x: bounds.x,
y: bounds.y + bounds.height
};
if (process.env.NODE_ENV === "development") {
option.resizable = true;
option.movable = true;
// option.width = option.width * 3;
// option.height = option.height * 3;
option.x = 0;
}
wins[k_winNames.tray] = new BrowserWindow(option);
let win = wins[k_winNames.tray];
win.loadURL(url.format({
pathname: path.join(__dirname, k_viewPaths.tray),
protocol: "file:",
slashes: true
}));
win.on("closed", () => {
console.log("tray window received closed");
wins[k_winNames.tray] = null;
win = null;
});
// close popup window when it loses focus
win.on("blur", () => {
console.log("tray window received blur");
win.hide();
});
win.on("ready-to-show", () => {
console.log("tray window received ready-to-show");
win.focus();
win.setMenu(null);
});
win.hide();
}
let tray = null; // keep global reference
function createTray() {
console.log("creating tray");
tray = new Tray(path.join(__dirname, "icons/app/icon-bw16.png"));
createTrayWindow(tray.getBounds());
tray.setToolTip("扇贝桌面版");
tray.on("click", function(e, bounds) {
console.log(bounds);
if (wins[k_winNames.tray].isVisible()) {
wins[k_winNames.tray].hide();
} else {
let winBounds = wins[k_winNames.tray].getBounds();
winBounds.x = bounds.x;
winBounds.y = bounds.y + bounds.height;
wins[k_winNames.tray].setBounds(winBounds, false);
wins[k_winNames.tray].show();
}
});
}
app.on("ready", function() {
console.log("app received ready");
setMessageListener();
createMainWindow();
createTray();
});
/*
* Quit when all windows are closed.
* But on macOS, let menu bar stay activ.
*/
app.on("window-all-closed", () => {
console.log("app received window-all-closed");
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
console.log("app received activate");
if (wins[k_winNames.main] === null) {
createMainWindow();
} else {
wins[k_winNames.main].show();
}
});
app.on('before-quit', () => {
console.log("app received before-quit");
forceQuit = true;
});