forked from leanote/desktop-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
317 lines (274 loc) · 7.78 KB
/
main.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
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
// var app = require('electron').app; // Module to control application life.
const {app, BrowserWindow, crashReporter, Tray, Menu, ipcMain: ipc} = require('electron');
var pdfMain = require('./src/pdf_main');
var appIcon;
// Report crashes to our server.
crashReporter.start({
productName: 'Leanote',
companyName: 'Leanote',
submitURL: 'https://leanote.com/leanote-desktop/crash-reporter',
autoSubmit: true
});
require('@electron/remote/main').initialize()
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the javascript object is GCed.
var mainWindow = null;
if (!app.makeSingleInstance) {
app.allowRendererProcessReuse = true
// single instance
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
console.log("gotTheLock is false, another instance is running")
app.quit()
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
if (mainWindow) {
mainWindow.show();
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
})
}
}
else {
// single instance
const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
mainWindow.show();
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
})
if (shouldQuit) {
app.quit()
}
}
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// if (process.platform != 'darwin')
app.quit();
});
// 仅MAC
// 避免可以启动多个app
app.on('open-file', function(e) {
// console.log('reopen');
if(mainWindow) {
mainWindow.show();
mainWindow.focus();
} else {
openIt();
}
});
// 仅MAC
// var appIsReady = false;
app.on('activate', function() {
console.log('activate');
if(mainWindow) {
mainWindow.show();
}
else {
// 有时, 重启电脑会出现这种情况
// Cannot create BrowserWindow before app is ready
// 很可能app.on('ready')还没有出现, 但是ready后肯定有mainWindow了
// 所以, 这一段注释掉
// openIt();
}
});
// DB
var DB = {
init: function () {
var me = this;
var db = require('./src/db_main');
// 前端发来消息
// m = {token: token, method: 'insert, findOne', dbname: 'notes', params: {username: "life"}};
ipc.on('db-exec', function(event, m) {
// me._token2Sender[m.token] = event.sender;
db.exec(m, function (ret) {
// console.log('main called ret:');
// console.log(ret);
if (ret && ret.ret) {
ret.ret = JSON.stringify(ret.ret);
}
event.sender.send('db-exec-ret', ret);
});
});
/**
* 前端发消息过来说可以初始化了
* @param {<Event>} event
* @param {Object} params {
curUser: <User> 是当前用户
dbPath: string 是用户的dbPath
dataBasePath: string 所有数据的基地址
* }
*/
ipc.on('db-init', function (event, params) {
db.init(params.curUser, params.dbPath, params.dataBasePath);
});
}
};
// This method will be called when Electron has done everything
// initialization and ready for creating browser windows.
app.on('ready', openIt);
function removeEvents (win) {
win.removeAllListeners('closed');
win.removeAllListeners('focus');
win.removeAllListeners('blur');
win.removeAllListeners('close');
}
function close (e, force) {
console.log('close:', force);
if (mainWindow) {
mainWindow.hide();
e && e.preventDefault();
mainWindow.webContents.send('closeWindow');
} else {
app.quit();
}
}
function bindEvents (win) {
mainWindow = win;
// Emitted when the window is closed.
win.on('closed', function() {
console.log('closed');
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
});
win.on('focus', function() {
console.log('focus');
// ipc.send('focusWindow'); mainProcess没有该方法
if(win && win.webContents)
win.webContents.send('focusWindow');
});
win.on('blur', function() {
console.log('blur');
if(win && win.webContents)
win.webContents.send('blurWindow');
});
// 以前的关闭是真关闭, 现是是假关闭了
// 关闭,先保存数据
win.on('close', function(e) {
// windows支持tray, 点close就是隐藏
if (process.platform.toLowerCase().indexOf('win') === 0) { // win32
win.hide();
e.preventDefault();
return;
}
// mac 在docker下quit;
// linux直接点x linux不支持Tray
close(e, false);
});
}
function openIt() {
// 数据库
DB.init();
// 协议
var leanoteProtocol = require('./src/leanote_protocol');
leanoteProtocol.init();
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1050,
height: 595,
frame: process.platform != 'darwin',
transparent: false,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false, // https://github.com/electron/electron/issues/27961
enableRemoteModule: true
}
}
);
console.log('load: file://' + __dirname + '/note.html');
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/note.html');
bindEvents(mainWindow);
// 前端发来可以关闭了
ipc.on('quit-app', function(event, arg) {
console.log('get quit-app request');
if (mainWindow) {
mainWindow.destroy();
mainWindow = null;
} else {
app.quit();
}
});
// open login.html and note.html
ipc.on('openUrl', function(event, arg) {
console.log('openUrl', arg);
// if (appIcon) {
// appIcon.destroy()
// }
arg.webPreferences = arg.webPreferences === undefined ? {} : arg.webPreferences;
arg.webPreferences.nodeIntegration = true;
arg.webPreferences.contextIsolation = false;
arg.webPreferences.enableRemoteModule = true;
var html = arg.html;
var everWindow = mainWindow;
if (arg.icon) {
arg.icon = new Tray(__dirname + arg.icon)
}
var win2 = new BrowserWindow(arg);
win2.loadURL('file://' + __dirname + '/' + html);
mainWindow = win2;
// remove all events then close it
removeEvents(everWindow);
everWindow.close();
if (html.indexOf('note.html') >= 0) {
bindEvents(mainWindow)
}
});
pdfMain.init();
function show () {
if (mainWindow) {
mainWindow.show();
mainWindow.restore();
mainWindow.focus();
mainWindow.webContents.send('focusWindow');
} else {
app.quit();
}
}
// tray只要实例化一次
// tray在windows下可能会有两个, 原因不明, 当注销后再启动
var trayShowed = false;
ipc.on('show-tray', function(event, arg) {
if (trayShowed) {
return;
}
trayShowed = true;
if (process.platform == 'linux') {
return;
}
console.log('show tray')
appIcon = new Tray(__dirname + '/public/images/tray/' + ( process.platform == 'darwin' ? 'trayTemplate.png' : 'tray.png'))
var contextMenu = Menu.buildFromTemplate([
{
label: arg.Open, click: function () {
show();
}
},
{
label: arg.Close, click: function () {
close(null, true);
}
},
]);
appIcon.setToolTip('Leanote');
// appIcon.setTitle('Leanote');
// appIcon.setContextMenu(contextMenu);
appIcon.on('click', function (e) {
show();
// e.preventDefault();
});
appIcon.on('right-click', function () {
appIcon.popUpContextMenu(contextMenu);
});
});
}