mirror of
https://github.com/yuruotong1/autoMate.git
synced 2026-03-22 04:57:18 +08:00
✨(index.ts):重构UI窗口创建功能为独立文件
📝(window.ts):添加窗口创建和配置方法 📝(ipc.ts):重构IPC消息注册处理函数
This commit is contained in:
9
ui/autoMate/src/main/code/index.ts
Normal file
9
ui/autoMate/src/main/code/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { app } from 'electron'
|
||||
import { createWindow } from './window'
|
||||
|
||||
|
||||
app.whenReady().then(() => {
|
||||
createWindow()
|
||||
})
|
||||
|
||||
|
||||
43
ui/autoMate/src/main/code/window.ts
Normal file
43
ui/autoMate/src/main/code/window.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
import { BrowserWindow, shell } from 'electron'
|
||||
import { is } from '@electron-toolkit/utils'
|
||||
import icon from '../../../resources/icon.png?asset'
|
||||
import * as ipc from './ipc'
|
||||
import { join } from 'path'
|
||||
|
||||
export function createWindow(): void { // Create the browser window.
|
||||
const mainWindow = new BrowserWindow({
|
||||
width: 600,
|
||||
height: 350,
|
||||
show: false,
|
||||
// transparent: true,
|
||||
// frame: false,
|
||||
// alwaysOnTop: true,
|
||||
autoHideMenuBar: true,
|
||||
...(process.platform === 'linux' ? { icon } : {}),
|
||||
webPreferences: {
|
||||
preload: join(__dirname, '../preload/index.js'),
|
||||
sandbox: false
|
||||
}
|
||||
})
|
||||
ipc.registerIpc(mainWindow)
|
||||
|
||||
mainWindow.webContents.openDevTools()
|
||||
mainWindow.on('ready-to-show', () => {
|
||||
mainWindow.show()
|
||||
})
|
||||
|
||||
mainWindow.webContents.setWindowOpenHandler((details) => {
|
||||
shell.openExternal(details.url)
|
||||
return { action: 'deny' }
|
||||
})
|
||||
|
||||
// HMR for renderer base on electron-vite cli.
|
||||
// Load the remote URL for development or the local html file for production.
|
||||
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
||||
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
|
||||
} else {
|
||||
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,5 @@
|
||||
import { app, shell, BrowserWindow, ipcMain } from 'electron'
|
||||
import { join } from 'path'
|
||||
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
|
||||
import icon from '../../resources/icon.png?asset'
|
||||
import './ipc'
|
||||
|
||||
function createWindow(): void { // Create the browser window.
|
||||
const mainWindow = new BrowserWindow({
|
||||
width: 600,
|
||||
height: 350,
|
||||
show: false,
|
||||
// transparent: true,
|
||||
// frame: false,
|
||||
// alwaysOnTop: true,
|
||||
autoHideMenuBar: true,
|
||||
...(process.platform === 'linux' ? { icon } : {}),
|
||||
webPreferences: {
|
||||
preload: join(__dirname, '../preload/index.js'),
|
||||
sandbox: false
|
||||
}
|
||||
})
|
||||
|
||||
mainWindow.webContents.openDevTools()
|
||||
mainWindow.on('ready-to-show', () => {
|
||||
mainWindow.show()
|
||||
})
|
||||
|
||||
mainWindow.webContents.setWindowOpenHandler((details) => {
|
||||
shell.openExternal(details.url)
|
||||
return { action: 'deny' }
|
||||
})
|
||||
|
||||
// HMR for renderer base on electron-vite cli.
|
||||
// Load the remote URL for development or the local html file for production.
|
||||
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
||||
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
|
||||
} else {
|
||||
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
|
||||
}
|
||||
}
|
||||
import { app, BrowserWindow, ipcMain } from 'electron'
|
||||
import { electronApp, optimizer } from '@electron-toolkit/utils'
|
||||
|
||||
// This method will be called when Electron has finished
|
||||
// initialization and is ready to create browser windows.
|
||||
@@ -55,15 +17,14 @@ app.whenReady().then(() => {
|
||||
|
||||
// IPC test
|
||||
ipcMain.on('ping', () => console.log('pong'))
|
||||
|
||||
createWindow()
|
||||
|
||||
|
||||
app.on('activate', function () {
|
||||
// On macOS 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 (BrowserWindow.getAllWindows().length === 0) createWindow()
|
||||
if (BrowserWindow.getAllWindows().length === 0) code.createWindow()
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
// Quit when all windows are closed, except on macOS. There, it's common
|
||||
// for applications and their menu bar to stay active until the user quits
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {ipcMain, BrowserWindow, IpcMainEvent } from "electron";
|
||||
|
||||
ipcMain.on('hideWindow', (event: IpcMainEvent) => {
|
||||
const win = BrowserWindow.fromWebContents(event.sender)
|
||||
if(win) win.hide()
|
||||
import {ipcMain, BrowserWindow} from "electron";
|
||||
export const registerIpc = (win: BrowserWindow)=>{
|
||||
ipcMain.on('hideWindow', () => {
|
||||
win.hide()
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user