refactor: preload communication
This commit is contained in:
16
src/main/communication.ts
Normal file
16
src/main/communication.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Expose something function to renderer
|
||||
*/
|
||||
import { BrowserWindow, ipcMain } from 'electron'
|
||||
import {
|
||||
LOGIN,
|
||||
LOGOUT,
|
||||
TOGGLE_DEVTOOLS,
|
||||
} from '@/common/constant/event'
|
||||
|
||||
export function register(win: BrowserWindow) {
|
||||
ipcMain.handle(TOGGLE_DEVTOOLS, () => {
|
||||
win.webContents.toggleDevTools()
|
||||
})
|
||||
ipcMain.handle(LOGOUT, () => { })
|
||||
}
|
||||
@@ -1,31 +1,28 @@
|
||||
/**
|
||||
* electron 主文件
|
||||
*/
|
||||
import { join } from 'path'
|
||||
import path from 'path'
|
||||
import { app, BrowserWindow } from 'electron'
|
||||
import dotenv from 'dotenv'
|
||||
import { register } from './communication'
|
||||
|
||||
dotenv.config({ path: join(__dirname, '../../.env') })
|
||||
let win: BrowserWindow | null = null
|
||||
|
||||
let win: BrowserWindow
|
||||
|
||||
function createWin() {
|
||||
// 创建浏览器窗口
|
||||
function bootstrap() {
|
||||
win = new BrowserWindow({
|
||||
width: 1024,
|
||||
height: 768,
|
||||
webPreferences: {
|
||||
nodeIntegration: true,
|
||||
contextIsolation: false,
|
||||
preload: join(__dirname, '../preload/index.js'),
|
||||
preload: path.join(__dirname, '../preload/index.js'),
|
||||
},
|
||||
})
|
||||
|
||||
const URL = app.isPackaged
|
||||
? `file://${join(__dirname, '../render/index.html')}` // vite 构建后的静态文件地址
|
||||
: `http://localhost:${process.env.PORT}` // vite 启动的服务器地址
|
||||
if (app.isPackaged) {
|
||||
win.loadFile(path.join(__dirname, '../render/index.html'))
|
||||
} else {
|
||||
win.maximize()
|
||||
win.webContents.openDevTools()
|
||||
win.loadURL(`http://localhost:${process.env.PORT}`)
|
||||
}
|
||||
|
||||
win?.loadURL(URL)
|
||||
// something init setup
|
||||
register(win)
|
||||
}
|
||||
|
||||
app.whenReady().then(createWin)
|
||||
app.whenReady().then(bootstrap)
|
||||
|
||||
app.on('window-all-closed', () => { win = null })
|
||||
|
||||
Reference in New Issue
Block a user