feat: 配置修改

master
JINGYJ 4 weeks ago
parent 3ae318e276
commit afcf2de491

@ -0,0 +1,78 @@
import { shell, BrowserWindow } from 'electron'
import { join } from 'path'
import icon from '../../../resources/icon.png?asset'
import { is } from '@electron-toolkit/utils'
export default class MainFrame {
#frame: any = null
#width = 1920
#height = 1080
create() {
this.#frame = new BrowserWindow({
width: this.#width,
height: this.#height,
show: false,
// titleBarStyle:'hidden',
frame: true, //无边框窗口
resizable: false,
autoHideMenuBar: true,
...(process.platform === 'linux' ? { icon } : {}),
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: false
}
})
this.#frame.on('ready-to-show', () => {
this.#frame.show()
})
this.#frame.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: 'deny' }
})
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
this.#frame.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
this.#frame.loadFile(join(__dirname, '../renderer/index.html'))
}
this.#frame.webContents.openDevTools() // 打开调试工具
}
//隐藏窗口
hide() {
this.#frame.hide()
}
//设置窗口大小
setSize(w, h) {
this.#frame.setSize(w, h) //设置窗口大小
}
//设置窗口最小值
setMinimumSize(w, h) {
this.#frame.setMinimumSize(w, h) //设置
}
//窗口居中
center() {
this.#frame.center() //窗口居中
}
//窗口是否可以改变
setResizable(bool) {
this.#frame.setResizable(bool) //窗口可调节大小
}
//接收窗口拖拽事件:
setPosition(x, y) {
this.#frame.setPosition(x, y)
}
//窗口关闭
close() {
this.#frame.close()
}
//最小化
minimize() {
this.#frame.minimize()
}
//判断是否最大化
isFullScreen() {
return this.#frame.isFullScreen()
}
//放大 & 缩小
setFullScreen(bool) {
this.#frame.setFullScreen(bool)
}
}

@ -1,39 +1,10 @@
import { app, shell, BrowserWindow, ipcMain } from 'electron' import { app, ipcMain } from 'electron'
import { join } from 'path' import { electronApp, optimizer } from '@electron-toolkit/utils'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
function createWindow(): void { import MainFrame from './frame/MainFrame'
// Create the browser window. import routers from './router/router.template'
const mainWindow = new BrowserWindow({ import EventRouter from './router/EventRouter'
width: 1920, // import { da } from 'element-plus/es/locale'
height: 1080,
show: false,
autoHideMenuBar: true,
...(process.platform === 'linux' ? { icon } : {}),
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: false
}
})
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'))
}
}
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
@ -49,15 +20,23 @@ app.whenReady().then(() => {
optimizer.watchWindowShortcuts(window) optimizer.watchWindowShortcuts(window)
}) })
// IPC test let eventRouter = new EventRouter()
ipcMain.on('ping', () => console.log('pong')) let mainWindow = new MainFrame()
mainWindow.create()
createWindow() eventRouter.addApi('mainWindow', mainWindow)
eventRouter.addApi('app', app)
eventRouter.addRouters(routers)
//渲染进程向主进程通信
ipcMain.handle('renderer-to-main', (_e, data) => {
eventRouter.router(data)
})
app.on('activate', function () { app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the // 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. // dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow() // if (BrowserWindow.getAllWindows().length === 0) createWindow()
}) })
}) })
@ -72,3 +51,6 @@ app.on('window-all-closed', () => {
// In this file you can include the rest of your app"s specific main process // In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here. // code. You can also put them in separate files and require them here.
app.commandLine.appendSwitch('high-dpi-support', '1')
app.commandLine.appendSwitch('force-device-scale-factor', '1')

@ -0,0 +1,9 @@
export default class EventRoute {
public name: string;
public callback: (api:any, data:any) => void;
constructor(name: string, callback: (api:any, data:any) => void) {
this.name = name;
this.callback = callback;
}
}

@ -0,0 +1,24 @@
export default class EventRouter {
#api={};
routers;
constructor(){
this.routers = new Array();
}
//添加api
addApi(key,api){
this.#api[key] = api;
}
//添加多个路由
addRouters(routers){
this.routers = this.routers.concat(routers);
}
//触发指定名称的路由回调
router(data){
for(let i=0;i<this.routers.length;i++){
let r = this.routers[i];
if(r.name == data.name && r.callback){
r.callback(this.#api,data);
}
}
}
}

@ -0,0 +1,50 @@
import EventRoute from "./EventRoute";
const routers = new Array();
//关闭软件
routers.push(new EventRoute("close-login", (api,data) => {
console.log(data);
api.mainWindow.close();
}));
//拖拽窗口
routers.push(new EventRoute("custom-adsorption", (api,{data}) => {
console.log(data);
let x = data.appX;
let y = data.appY;
api.mainWindow.setPosition(x,y);
}))
//最小化
routers.push(new EventRoute("min-win", (api,data) => {
console.log(data);
api.mainWindow.minimize();
}))
//最大化
routers.push(new EventRoute("max-win", (api,data) => {
console.log(data);
if(api.mainWindow.isFullScreen()){
api.mainWindow.setFullScreen(false);
}else{
api.mainWindow.setFullScreen(true);
}
}))
//退出软件
routers.push(new EventRoute("win-close", (api,data) => {
console.log(data);
api.app.exit();
}))
//退出登陆
routers.push(new EventRoute("out-login", (api,data) => {
console.log(data);
api.mainWindow.setSize(900,670);
api.mainWindow.center();
api.mainWindow.setResizable(false);
}))
//进入后台管理系统首页
routers.push(new EventRoute("resize-window", (api,data) => {
console.log(data);
api.mainWindow.setSize(1200,720);
api.mainWindow.setMinimumSize(1000,500)
api.mainWindow.center();
api.mainWindow.setResizable(true);
}))
export default routers;
Loading…
Cancel
Save