-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
325 lines (284 loc) · 8.77 KB
/
main.js
File metadata and controls
325 lines (284 loc) · 8.77 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
const electron = require('electron')
const {app, BrowserWindow, Menu} = require('electron')
let win = null
const gotTheLock = app.requestSingleInstanceLock()
const ipcMain = require('electron').ipcMain;
const path = require("path")
function createWindow() {
// 创建浏览器窗口
win = new BrowserWindow({
width: 800,
height: 620,
show: false,
frame: false,
transparent: true,
maximizable: false,
resizable: false,
backgroundColor: '#00000000',
useContentSize: true,
icon: path.join(__dirname, 'favicon.png'),
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
},
})
win.once('ready-to-show', () => {
console.log("ready to show")
win.show()
// 检查是否存在需要直接打开的文件,有的话就直接打开
if (preFilePath) {
win.webContents.send('dock-file', [preFilePath])
}
})
// 并且为你的应用加载 index.html
win.loadFile('arena.html')
// 打开开发者工具
// win.webContents.openDevTools()
// 取消默认菜单
Menu.setApplicationMenu(null)
ipcMain.on('asynchronous-message', function(event, arg) {
console.log(arg);
win.focus()
if (arg == 'copy') {
win.webContents.copy()
console.log("win.webContents.copy()", win.webContents.copy())
} else if (arg == 'paste') {
win.webContents.paste()
} else if (arg == 'cut') {
win.webContents.cut()
}
event.sender.send('asynchronous-reply', '操作完毕');
});
let bool = false
let channel = 'main-process-messages'
win.on('close', function(event) {
//在应用程序开始关闭它的窗口的时候被触发
console.log('close start')
if (bool == false) {
event.preventDefault()
}
win.webContents.send(channel, 'beforeCopy');
ipcMain.on('main-process-messages', function(event, arg) {
console.log(arg);
if (arg == 'close') {
channel = 'others'
bool = true
app.quit()
}
});
})
// id 为 1
// console.log("id", win.webContents.id)
ipcMain.on('min', function() {
win.minimize();
})
//登录窗口最大化
ipcMain.on('max', function() {
if (win.isMaximized()) {
//重新设置窗口客户端的宽高值(例如网页界面),这里win.setSize(x,y)并不生效。
win.setContentSize(800, 620);
// 窗口居中
win.center();
} else {
win.maximize();
}
})
ipcMain.on('close', function() {
win.close();
})
}
let myTurtle = null
let myTurtlePage = function(code) {
myTurtle = new BrowserWindow ({
width: 630,
height: 520,
show: false,
maximizable: false,
resizable: false,
frame: false,
icon: path.join(__dirname, 'favicon.png'),
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false
},
})
// Menu.setApplicationMenu(null)
myTurtle.loadFile("turtle.html");
myTurtle.webContents.on('did-finish-load', function(){
myTurtle.show()
console.log("id", myTurtle.webContents.id)
console.log("send")
myTurtle.webContents.send('myTurtle-reply', code);
ipcMain.on('myTurtle-reply', function(event, arg) {
console.log(arg);
if (arg != 'get-myTurtle-reply') {
console.log('send twice')
myTurtle.webContents.send('myTurtle-reply', code);
}
});
});
// myTurtle.webContents.openDevTools()
}
let myTurtleMessage = function() {
ipcMain.on('myTurtle', function(event, arg) {
console.log(arg)
myTurtlePage(arg)
});
ipcMain.on('turtle-min', function() {
myTurtle.minimize();
})
ipcMain.on('turtle-max', function() {
if (myTurtle.isMaximized()) {
myTurtle.setContentSize(630, 520);
myTurtle.center();
} else {
myTurtle.maximize();
}
})
ipcMain.on('turtle-close', function(event, arg) {
console.log("close begin")
if (myTurtle != null) {
console.log("close")
console.log("id", myTurtle.webContents.id)
myTurtle.close()
}
})
}
let about = null
let aboutPage = function() {
about = new BrowserWindow ({
width: 400,
height: 220,
show: false,
maximizable: false,
resizable: false,
frame: false,
icon: path.join(__dirname, 'favicon.png'),
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false
},
})
// Menu.setApplicationMenu(null)
about.loadFile("about.html");
about.webContents.on('did-finish-load', function(){
about.show()
console.log("about id", about.webContents.id)
});
// about.webContents.openDevTools()
}
let aboutMessage = function() {
ipcMain.on('about', function(event, arg) {
console.log("about page open")
aboutPage()
});
ipcMain.on('about-min', function() {
about.minimize();
})
ipcMain.on('about-close', function(event, arg) {
console.log("about close begin")
if (about != null) {
console.log("about close")
console.log("about id", about.webContents.id)
about.close()
}
})
}
let preFilePath = ''
let initApp = function() {
// Electron会在初始化完成并且准备好创建浏览器窗口时调用这个方法
// 部分 API 在 ready 事件触发后才能使用。
app.whenReady().then(function() {
createWindow()
// 打开文件事件(MacOS有效)
app.on("open-file", (e, filePath) => {
console.log("open-file: ", filePath);
const fw = BrowserWindow.getFocusedWindow();
if (fw) {
fw.webContents.send("dock-file", [filePath]);
} else {
preFilePath = filePath
}
});
if (process.platform ==='win32' && process.argv.length >= 2) {
console.log('process argv:', process.argv)
// windows系统当没有路径参数时这个位置默认有个.,需要加以判断
preFilePath = process.argv[1] === '.' ? '' : process.argv[1]
}
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
// myTurtle page 事件处理
myTurtleMessage()
// about page 事件处理
aboutMessage()
}
let __main = function() {
// app.on('open-file', (event, path) => {
// win.webContents.send('dock-file', "-----hello");
// });
if (!gotTheLock) {
app.quit()
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
// 当运行第二个实例时,将会聚焦到 win 这个窗口
if (win) {
if (win.isMinimized()) win.restore()
win.focus()
}
})
// 创建 win, 加载应用的其余部分, etc...
initApp()
}
}
__main()
// 您可以把应用程序其他的流程写在在此文件中
// 代码 也可以拆分成几个文件,然后用 require 导入
/*
{
"name": "arena",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "electron .",
"packager": "electron-packager ./ myapp --out ./OutApp --electron-version 11.0.0-beta.9 --overwrite --icon=./favicon.ico",
"pack": " electron-builder --dir ",
"dist": " electron-builder --win --x64",
"postinstall": "electron-builder install-app-deps"
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^11.2.3"
},
"keywords": [],
"description": "Arena 一款轻量级的 JS 编辑器",
"build": {
"productName":"Arena",
"appId": "com.juantu.vip",
"copyright":"Juantu",
"directories": {
"output": "build"
},
"win": {
"icon": "./favicon.ico"
}
}
}
*/