/
main.js
94 lines (78 loc) · 2.4 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
const electron = require('electron');
// 控制应用生命周期的模块
const app = electron.app
// 创建窗口
const BrowserWindow = electron.BrowserWindow;
const path = require('path');
const url = require('url');
// 保持一个对于 window 对象的全局引用,不然,当 JavaScript 被 GC,
// window 会被自动地关闭
let mainWindow
// 当 Electron 完成了初始化并且准备创建浏览器窗口的时候
// 这个方法就被调用
app.on('ready', () => {
// 创建浏览器窗口。
mainWindow = new BrowserWindow({
frame: false,
width: 400,
height: 600,
resizable: false
});
// 加载应用的index.html
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'src/index.html'),
protocol: 'file:',
slashes: true
}));
// 打开 DevTools.
//mainWindow.webContents.openDevTools()
// window关闭事件
mainWindow.on('closed', () => {
/// 取消引用 window 对象,如果你的应用支持多窗口的话,
// 通常会把多个 window 对象存放在一个数组里面,
// 但这次不是。
mainWindow = null
})
})
// 当所有窗口被关闭了,退出。
app.on('window-all-closed', () => {
// 在 OS X 上,通常用户在明确地按下 Cmd + Q 之前
// 应用会保持活动状态
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On OS X 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 (mainWindow === null) {
createWindow()
}
})
var ipcMain = electron.ipcMain;
//遍历获取图片文件
const { getImgList } = require('./assist');
ipcMain.on('file-drop', (event, dropPaths) => {
var imgList = getImgList(dropPaths);
event.sender.send('drop-imgs', imgList);
})
//关闭/最小化窗口
ipcMain.on('window-handle', (event, arg) => {
if (arg == 'close') {
mainWindow.close();
return
}
if (arg == 'min') {
mainWindow.minimize();
return
}
});
//打开目录选择面板
var dialog = electron.dialog;
ipcMain.on('select-directory', (event, arg) => {
dialog.showOpenDialog({ properties: ['openDirectory'] }, (directory) => {
if (directory) {
event.sender.send('directory-selected', path.normalize(directory[0]))
}
});
})