| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import { BrowserWindow, app } from 'electron';
- import path from 'path';
- import { fileURLToPath } from 'url';
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = path.dirname(__filename);
- // 创建主窗口,根据环境加载不同内容源
- export function createWindow() {
- const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged;
-
- const mainWindow = new BrowserWindow({
- width: 1200,
- height: 800,
- webPreferences: {
- preload: path.join(__dirname, '..', 'preload.cjs'),
- nodeIntegration: false,
- contextIsolation: true
- }
- });
- // 设置窗口事件处理(禁用关闭确认对话框等)
- setupWindow(mainWindow);
- if (isDev) {
- mainWindow.loadURL('http://localhost:5173');
- mainWindow.webContents.openDevTools();
- } else {
- mainWindow.loadFile(path.join(__dirname, '..', 'dist/index.html'));
- }
-
- return mainWindow;
- }
- // 设置窗口事件处理(禁用关闭确认对话框等)
- function setupWindow(mainWindow) {
- // 禁用窗口关闭确认对话框,直接关闭
- mainWindow.on('close', (event) => {
- // 不阻止关闭事件,直接关闭窗口
- // 如果需要清理资源,可以在这里添加
- });
-
- // 禁用 webContents 的 beforeunload 确认对话框
- mainWindow.webContents.on('will-prevent-unload', (event) => {
- // 不阻止卸载,直接关闭
- // 注意:这里不需要调用 event.preventDefault(),因为我们要允许关闭
- });
-
- // 禁用 beforeunload 事件(前端页面可能触发的确认对话框)
- mainWindow.webContents.setWindowOpenHandler(() => {
- return { action: 'allow' };
- });
-
- // 禁用所有可能阻止关闭的事件
- mainWindow.webContents.on('beforeunload', (event) => {
- // 不阻止卸载
- });
- }
|