-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
145 lines (128 loc) · 4.26 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
const { app, BrowserWindow, Menu, Tray, ipcMain, dialog } = require('electron')
const Store = require('electron-store');
const path = require('path')
const package = require('./package.json')
// 清除启动时electron的安全告警:Electron Security Warning (Insecure Content-Security-Policy)
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
// 禁用当前应用程序的硬件加速
app.disableHardwareAcceleration();
let win;
const store = new Store();
app.whenReady().then(() => {
createTray();
createWindow()
})
app.on('window-all-closed', () => {
if(process.platform !== 'darwin') app.quit();
});
const createWindow = () => {
Menu.setApplicationMenu(null);
// 启动恢复主窗口位置和大小
let isMax = store.get('isMax') ? true : false
, position = store.get('mainPosition')
, config = {};
config.minWidth = 900;
config.minHeight = 700;
config.icon = path.join(__dirname, './src/logo.png');
config.webPreferences = {
preload: path.join(__dirname, './src/preload.js'),
spellcheck: false
}
config.useContentSize = true;
config.show = false;
win = new BrowserWindow(config);
if(isMax) win.maximize();
win.loadFile('./src/index.html');
// 打开开发者窗口
// win.webContents.openDevTools({mode: 'detach'});
// 启动恢复主窗口位置和大小
// let position = store.get('mainPosition')
if(!isMax && !('' == position || undefined == position)) {
win.setContentBounds(position)
}
win.on('ready-to-show', ()=>{
win.show();
});
// 关闭主窗口事件,记录窗口大小和位置
win.on('close', (e) => {
console.info('close main window, we need record postion of mainWindow and it\'s size');
store.set('isMax', win.isMaximized())
store.set('mainPosition', win.getContentBounds())
});
}
const createTray = () => {
tray = new Tray(path.join(__dirname, './src/logo.png'));
const menu = Menu.buildFromTemplate(trayMenuTemplate);
tray.setContextMenu(menu);
}
const trayMenuTemplate = [{
label: 'About',
type: 'normal',
click: function() {
dialog.showMessageBox({
type: 'info',
title: '关于',
message: package.name + ':' + package.version + '\n' + package.description + '\nnode:' + process.versions['node'] + '\nchrome:' + process.versions['chrome'] + '\nelectron:' + process.versions['electron']
});
}
}, {
label: 'Project Repository',
type: 'normal',
click: function() {
let exec = require('child_process').exec
, locale = app.getLocale()
, url = ''
// 使用ip的话要么自己维护一个ip库放在外部(太大,没必要放项目里),要么使用第三方,都需要进行网络交互
// 所以这里使用一个最粗略的方式“语言环境”来判断是否是中国大陆
if(locale.indexOf('zh-CN') == -1) {
url = 'https://github.com/rexlevin/coderbox'
} else {
url = 'https://gitee.com/rexlevin/coderbox'
}
exec('open ' + url)
}
}, {
label: 'Quit',
type: 'normal',
click: function() {
app.quit();
}
}]
ipcMain.on('devTools', () => {
if(win.webContents.isDevToolsOpened()) win.webContents.closeDevTools();
else win.webContents.openDevTools();
});
ipcMain.on('reload', () => {
win.reload();
// win.webContents.reload();
});
ipcMain.on('exit', () => {
app.quit();
});
ipcMain.on('title', (e, arg) => {
e.reply('title-reply', package.name + ' - ' + package['description'] + ' - v' + package.version);
});
let winHttp;
ipcMain.on('windowHttp', (e, title, filePath) => {
if(null != winHttp && undefined != winHttp) {
winHttp.show();
return;
}
winHttp = new BrowserWindow({
width: 1100,
height: 800,
minWidth: 1100,
minHeight: 800,
frame: true,
title: title,
// parent: win
parent: null,
webPreferences: {
preload: path.join(__dirname, './src/preload.js'),
spellcheck: false
}
});
winHttp.loadFile(path.join(__dirname, './src', filePath));
// winHttp.webContents.openDevTools();
winHttp.on('closed',()=>{winHttp = null})
});