|
@@ -1,14 +1,21 @@
|
|
|
-const { app, BrowserWindow, Menu ,ipcMain} = require('electron');
|
|
|
-const path = require('path');
|
|
|
-const {writeLog} = require('./src/log/log-writer.js')
|
|
|
+// main.js (ESM 版本)
|
|
|
+import { app, BrowserWindow, Menu, ipcMain } from 'electron';
|
|
|
+import { fileURLToPath } from 'url';
|
|
|
+import { dirname, join } from 'path';
|
|
|
+import { writeLog } from './src/log/log-writer.js';
|
|
|
|
|
|
+// -------------- 构造 ESM 版 __dirname --------------
|
|
|
+const __filename = fileURLToPath(import.meta.url);
|
|
|
+const __dirname = dirname(__filename);
|
|
|
+
|
|
|
+// -------------- 创建窗口 --------------
|
|
|
function createWindow() {
|
|
|
const isMac = process.platform === 'darwin';
|
|
|
|
|
|
const win = new BrowserWindow({
|
|
|
- show: false, // 先隐藏,避免白屏
|
|
|
- frame: !isMac, // 非 macOS 无边框
|
|
|
- titleBarStyle: isMac ? 'hiddenInset' : 'default', // macOS 隐藏标题栏但保留控制按钮
|
|
|
+ show: false,
|
|
|
+ frame: !isMac,
|
|
|
+ titleBarStyle: isMac ? 'hiddenInset' : 'default',
|
|
|
webPreferences: {
|
|
|
nodeIntegration: true,
|
|
|
contextIsolation: false,
|
|
@@ -17,20 +24,17 @@ function createWindow() {
|
|
|
|
|
|
// 去掉应用菜单栏
|
|
|
Menu.setApplicationMenu(null);
|
|
|
- if (!isMac) win.removeMenu(); // Windows / Linux 额外保险
|
|
|
+ if (!isMac) win.removeMenu();
|
|
|
|
|
|
- // 启动即最大化
|
|
|
win.maximize();
|
|
|
|
|
|
- // 加载 Taro 编译后的 H5 页面
|
|
|
- // win.loadFile(path.join(__dirname, './dist/h5/index.html'));
|
|
|
// 加载外置 H5 页面
|
|
|
- win.loadFile(path.join(process.cwd(), 'h5/index.html'));
|
|
|
+ win.loadFile(join(process.cwd(), 'h5/index.html'));
|
|
|
|
|
|
- // 页面准备好再显示,防止视觉闪烁
|
|
|
win.once('ready-to-show', () => win.show());
|
|
|
}
|
|
|
|
|
|
+// -------------- 应用生命周期 --------------
|
|
|
app.whenReady().then(createWindow);
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
|
@@ -41,6 +45,7 @@ app.on('activate', () => {
|
|
|
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
|
|
});
|
|
|
|
|
|
-ipcMain.handle('write-log', (_, level, msg) => {
|
|
|
+// -------------- IPC --------------
|
|
|
+ipcMain.handle('write-log', async (_, level, msg) => {
|
|
|
writeLog(level, msg);
|
|
|
});
|