-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·193 lines (171 loc) · 4.79 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
const electron = require('electron')
const app = electron.app
const dialog = electron.dialog;
const BrowserWindow = electron.BrowserWindow
const Menu = electron.Menu
const path = require('path')
if (process.mas) app.setName('客服系统')
let mainWindow
function createWindow () {
let windowOptions = {
width: 1330,
height: 780,
title: app.getName()
}
if (process.platform === 'linux') {
windowOptions.icon = path.join(__dirname, '/logo.ico')
}
mainWindow = new BrowserWindow(windowOptions);
mainWindow.loadURL(path.join('file://', __dirname, '/dist/index.html'))
mainWindow.on('closed', function () {
mainWindow = null
})
mainWindow.on('close', function (e) {
const options = {
type: 'warning',
title: '温馨提示!',
message: "您确定关闭应用程序吗?",
buttons: ['我点错了', '窗口最小化', '确定关闭']
}
dialog.showMessageBox(options, function (index) {
if(index == 2) app.exit();
if(index == 1) mainWindow.minimize()
})
e.preventDefault()
})
}
app.on('ready', function() {
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
createWindow()
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', function () {
if (mainWindow === null) createWindow()
})
app.on('browser-window-created', function () {
let reopenMenuItem = findReopenMenuItem()
if (reopenMenuItem) reopenMenuItem.enabled = false
})
app.on('window-all-closed', function () {
let reopenMenuItem = findReopenMenuItem()
if (reopenMenuItem) reopenMenuItem.enabled = true
app.quit()
})
/**
* 注册键盘快捷键
*/
let template = [
{
label: '操作',
submenu: [{
label: '重新加载',
accelerator: 'CmdOrCtrl+R',
click: function (item, focusedWindow) {
if (focusedWindow) {
if (focusedWindow.id === 1) {
BrowserWindow.getAllWindows().forEach(function (win) {
if (win.id > 1) {
win.close()
}
})
}
focusedWindow.loadURL(path.join('file://', __dirname, '/dist/index.html'))
}
}
},
{
label: "选项",
submenu: [
{ label: "复制", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "粘贴", accelerator: "CmdOrCtrl+V", selector: "paste:" },
]
}]
},
{
label: '窗口设置',
role: 'window',
submenu: [{
label: '最小化',
accelerator: 'CmdOrCtrl+M',
role: 'minimize'
},{
label: '切换开发者工具',
accelerator: (function () {
if (process.platform === 'darwin') {
return 'Alt+Command+I'
} else {
return 'Ctrl+Shift+I'
}
})(),
click: function (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.toggleDevTools()
}
}
}]
},
{
label: '关于',
role: 'help',
submenu: [{
label: '客服',
click: function () {
electron.shell.openExternal('http://kf.aissz.com:666')
}
}]
}
]
/**
* 增加更新相关的菜单选项
*/
function addUpdateMenuItems (items, position) {
if (process.mas) return
const version = electron.app.getVersion()
let updateItems = [{
label: `当前版本 ${version}`,
enabled: false
}]
items.splice.apply(items, [position, 0].concat(updateItems))
}
function findReopenMenuItem () {
const menu = Menu.getApplicationMenu()
if (!menu) return
let reopenMenuItem
menu.items.forEach(function (item) {
if (item.submenu) {
item.submenu.items.forEach(function (item) {
if (item.key === 'reopenMenuItem') {
reopenMenuItem = item
}
})
}
})
return reopenMenuItem
}
// 针对Mac端的一些配置
if (process.platform === 'darwin') {
const name = electron.app.getName()
template.unshift({
label: name,
submenu: [{
label: '退出应用',
accelerator: 'Command+Q',
click: function () {
app.exit()
}
}]
})
// Window menu.
template[3].submenu.push({
type: 'separator'
})
addUpdateMenuItems(template[0].submenu, 1)
}
// 针对Windows端的一些配置
if (process.platform === 'win32') {
const helpMenu = template[template.length - 1].submenu
addUpdateMenuItems(helpMenu, 0)
}