mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-04-16 07:50:50 +08:00
Merge pull request #396 from algerkong/fix/mac-window-close
🐞 fix: 修复mac快捷键关闭窗口报错的问题
This commit is contained in:
@@ -21,6 +21,7 @@ const store = new Store();
|
||||
// 保存主窗口引用,以便在 activate 事件中使用
|
||||
let mainWindowInstance: BrowserWindow | null = null;
|
||||
let isPlaying = false;
|
||||
let isAppQuitting = false;
|
||||
// 保存迷你模式前的窗口状态
|
||||
let preMiniModeState: WindowState = {
|
||||
width: DEFAULT_MAIN_WIDTH,
|
||||
@@ -30,6 +31,13 @@ let preMiniModeState: WindowState = {
|
||||
isMaximized: false
|
||||
};
|
||||
|
||||
/**
|
||||
* 设置应用退出状态
|
||||
*/
|
||||
export function setAppQuitting(quitting: boolean) {
|
||||
isAppQuitting = quitting;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化代理设置
|
||||
*/
|
||||
@@ -117,8 +125,13 @@ export function initializeWindowManager() {
|
||||
ipcMain.on('close-window', (event) => {
|
||||
const win = BrowserWindow.fromWebContents(event.sender);
|
||||
if (win) {
|
||||
win.destroy();
|
||||
app.quit();
|
||||
// 在 macOS 上,关闭窗口不应该退出应用,而是隐藏窗口
|
||||
if (process.platform === 'darwin') {
|
||||
win.hide();
|
||||
} else {
|
||||
win.destroy();
|
||||
app.quit();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -294,6 +307,20 @@ export function createMainWindow(icon: Electron.NativeImage): BrowserWindow {
|
||||
setThumbarButtons(mainWindow);
|
||||
});
|
||||
|
||||
// 处理窗口关闭事件
|
||||
mainWindow.on('close', (event) => {
|
||||
// 在 macOS 上,阻止默认的关闭行为,改为隐藏窗口
|
||||
if (process.platform === 'darwin') {
|
||||
// 检查是否是应用正在退出
|
||||
if (!isAppQuitting) {
|
||||
event.preventDefault();
|
||||
mainWindow.hide();
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 在其他平台上,或者应用正在退出时,允许正常关闭
|
||||
});
|
||||
|
||||
mainWindow.on('ready-to-show', () => {
|
||||
const [width, height] = mainWindow.getSize();
|
||||
console.log(`窗口显示前的大小: ${width}x${height}`);
|
||||
|
||||
Reference in New Issue
Block a user