@@ -21,6 +21,7 @@ const store = new Store();
21
21
// 保存主窗口引用,以便在 activate 事件中使用
22
22
let mainWindowInstance : BrowserWindow | null = null ;
23
23
let isPlaying = false ;
24
+ let isAppQuitting = false ;
24
25
// 保存迷你模式前的窗口状态
25
26
let preMiniModeState : WindowState = {
26
27
width : DEFAULT_MAIN_WIDTH ,
@@ -30,6 +31,13 @@ let preMiniModeState: WindowState = {
30
31
isMaximized : false
31
32
} ;
32
33
34
+ /**
35
+ * 设置应用退出状态
36
+ */
37
+ export function setAppQuitting ( quitting : boolean ) {
38
+ isAppQuitting = quitting ;
39
+ }
40
+
33
41
/**
34
42
* 初始化代理设置
35
43
*/
@@ -117,8 +125,13 @@ export function initializeWindowManager() {
117
125
ipcMain . on ( 'close-window' , ( event ) => {
118
126
const win = BrowserWindow . fromWebContents ( event . sender ) ;
119
127
if ( win ) {
120
- win . destroy ( ) ;
121
- app . quit ( ) ;
128
+ // 在 macOS 上,关闭窗口不应该退出应用,而是隐藏窗口
129
+ if ( process . platform === 'darwin' ) {
130
+ win . hide ( ) ;
131
+ } else {
132
+ win . destroy ( ) ;
133
+ app . quit ( ) ;
134
+ }
122
135
}
123
136
} ) ;
124
137
@@ -294,6 +307,20 @@ export function createMainWindow(icon: Electron.NativeImage): BrowserWindow {
294
307
setThumbarButtons ( mainWindow ) ;
295
308
} ) ;
296
309
310
+ // 处理窗口关闭事件
311
+ mainWindow . on ( 'close' , ( event ) => {
312
+ // 在 macOS 上,阻止默认的关闭行为,改为隐藏窗口
313
+ if ( process . platform === 'darwin' ) {
314
+ // 检查是否是应用正在退出
315
+ if ( ! isAppQuitting ) {
316
+ event . preventDefault ( ) ;
317
+ mainWindow . hide ( ) ;
318
+ return ;
319
+ }
320
+ }
321
+ // 在其他平台上,或者应用正在退出时,允许正常关闭
322
+ } ) ;
323
+
297
324
mainWindow . on ( 'ready-to-show' , ( ) => {
298
325
const [ width , height ] = mainWindow . getSize ( ) ;
299
326
console . log ( `窗口显示前的大小: ${ width } x${ height } ` ) ;
0 commit comments