Skip to content

Commit 0dd9bd1

Browse files
committed
完成electron版本
1 parent 44197e7 commit 0dd9bd1

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

electron-main.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const { BrowserWindow, app } = require('electron')
2+
const isDev = require('electron-is-dev')
3+
const createWindow = () => {
4+
const mainWindow = new BrowserWindow({
5+
width: 1280,
6+
height: 800,
7+
webPreferences: {
8+
nodeIntegration: true, //渲染进程中使用nodejs
9+
contextIsolation: false,
10+
enableRemoteModule: true //渲染线程中使用remote模块
11+
}
12+
})
13+
//dev development
14+
const waitOn = require('wait-on')
15+
if (isDev) {
16+
mainWindow.loadFile('loading.html')
17+
// wait for http://localhost:5006 to load
18+
// detail to using look https://github.com/jeffbski/wait-on
19+
const opts = {
20+
resources: ['http://127.0.0.1:5008/index.html'],
21+
delay: 1000, // initial delay in ms, default 0
22+
timeout: 6000 // timeout in ms, default Infinity
23+
}
24+
waitOn(opts, (err) => {
25+
if (err) {
26+
console.error(err)
27+
return
28+
}
29+
mainWindow.loadURL('http://localhost:5008')
30+
//open devTools
31+
mainWindow.webContents.openDevTools({ mode: 'bottom' })
32+
})
33+
} else {
34+
mainWindow.loadFile('dist/index.html')
35+
}
36+
//开启remote
37+
require('@electron/remote/main').initialize()
38+
require('@electron/remote/main').enable(mainWindow.webContents)
39+
//引入相应的主线程
40+
require('./electron/main/MainRendererComm')
41+
//import menu
42+
//require('./electron/main/menu')
43+
//import tray
44+
require('./electron/main/tray')
45+
//import tray
46+
require('./electron/main/globalShortcut')
47+
}
48+
app.on('ready', createWindow)
49+
//监听窗口关闭的事件,关闭的时候退出应用,macOs 需要排除(mac相关)
50+
app.on('window-all-closed', () => {
51+
if (process.platform !== 'darwin') {
52+
app.quit()
53+
}
54+
})
55+
//Macos 中点击 dock 中的应用图标的时候重新创建窗口
56+
app.on('activate', () => {
57+
if (BrowserWindow.getAllWindows().length === 0) {
58+
createWindow()
59+
}
60+
})

0 commit comments

Comments
 (0)