forked from leanote/desktop-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
149 lines (129 loc) · 3.93 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
// var app = require('electron').app; // Module to control application life.
const {app, BrowserWindow, crashReporter} = require('electron');
var ipc = require('electron').ipcMain;
var pdfMain = require('pdf_main');
// Report crashes to our server.
crashReporter.start({
productName: 'YourName',
companyName: 'YourCompany',
submitURL: 'https://your-domain.com/url-to-submit',
autoSubmit: true
});
// 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;
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// if (process.platform != 'darwin')
app.quit();
});
// 避免可以启动多个app
app.on('open-file', function(e) {
// console.log('reopen');
if(mainWindow) {
mainWindow.show();
mainWindow.focus();
} else {
openIt();
}
});
// 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('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 openIt() {
// 数据库
DB.init();
// 协议
var leanoteProtocol = require('leanote_protocol');
leanoteProtocol.init();
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1050,
height: 595,
frame: process.platform != 'darwin',
transparent: false
}
);
console.log('load: file://' + __dirname + '/note.html');
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/note.html');
// Emitted when the window is closed.
mainWindow.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.
mainWindow = null;
});
mainWindow.on('focus', function() {
console.log('focus');
// ipc.send('focusWindow'); mainProcess没有该方法
if(mainWindow && mainWindow.webContents)
mainWindow.webContents.send('focusWindow');
});
mainWindow.on('blur', function() {
console.log('blur');
if(mainWindow && mainWindow.webContents)
mainWindow.webContents.send('blurWindow');
});
// 关闭,先保存数据
mainWindow.on('close', function(e) {
console.log('close');
mainWindow.hide();
e.preventDefault();
mainWindow.webContents.send('closeWindow');
});
// 前端发来可以关闭了
ipc.on('quit-app', function(event, arg) {
console.log('get quit-app request');
mainWindow.destroy();
mainWindow = null;
});
pdfMain.init();
}