소스 검색

feat(electron): make window borderless, launch maximized and compatible with macOS, Windows and Linux via main.js

sw 1 개월 전
부모
커밋
f2d928bf02
1개의 변경된 파일18개의 추가작업 그리고 11개의 파일을 삭제
  1. 18 11
      main.js

+ 18 - 11
main.js

@@ -1,32 +1,39 @@
-const { app, BrowserWindow } = require('electron');
+const { app, BrowserWindow, Menu } = require('electron');
 const path = require('path');
 
 function createWindow() {
+  const isMac = process.platform === 'darwin';
+
   const win = new BrowserWindow({
-    width: 800,
-    height: 600,
+    show: false,                       // 先隐藏,避免白屏
+    frame: !isMac,                     // 非 macOS 无边框
+    titleBarStyle: isMac ? 'hiddenInset' : 'default', // macOS 隐藏标题栏但保留控制按钮
     webPreferences: {
       nodeIntegration: true,
       contextIsolation: false,
     },
   });
-  // 关键:把应用菜单设为空
+
+  // 去掉应用菜单栏
   Menu.setApplicationMenu(null);
+  if (!isMac) win.removeMenu();        // Windows / Linux 额外保险
+
+  // 启动即最大化
+  win.maximize();
+
   // 加载 Taro 编译后的 H5 页面
-  // win.loadFile('./dist/index.html');
   win.loadFile(path.join(__dirname, './dist/h5/index.html'));
+
+  // 页面准备好再显示,防止视觉闪烁
+  win.once('ready-to-show', () => win.show());
 }
 
 app.whenReady().then(createWindow);
 
 app.on('window-all-closed', () => {
-  if (process.platform !== 'darwin') {
-    app.quit();
-  }
+  if (process.platform !== 'darwin') app.quit();
 });
 
 app.on('activate', () => {
-  if (BrowserWindow.getAllWindows().length === 0) {
-    createWindow();
-  }
+  if (BrowserWindow.getAllWindows().length === 0) createWindow();
 });