Skip to content

Commit 3b3f321

Browse files
committed
🐞 fix: 修复窗口 IPC 报错 & 歌单去重
1 parent 58e3c6e commit 3b3f321

File tree

12 files changed

+91
-40
lines changed

12 files changed

+91
-40
lines changed

components.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ declare module 'vue' {
9797
NP: typeof import('naive-ui')['NP']
9898
NPopconfirm: typeof import('naive-ui')['NPopconfirm']
9999
NPopover: typeof import('naive-ui')['NPopover']
100-
NProgress: typeof import('naive-ui')['NProgress']
101100
NQrCode: typeof import('naive-ui')['NQrCode']
102101
NRadio: typeof import('naive-ui')['NRadio']
103102
NRadioGroup: typeof import('naive-ui')['NRadioGroup']
@@ -107,7 +106,6 @@ declare module 'vue' {
107106
NSlider: typeof import('naive-ui')['NSlider']
108107
NSpin: typeof import('naive-ui')['NSpin']
109108
NSwitch: typeof import('naive-ui')['NSwitch']
110-
NTab: typeof import('naive-ui')['NTab']
111109
NTabPane: typeof import('naive-ui')['NTabPane']
112110
NTabs: typeof import('naive-ui')['NTabs']
113111
NTag: typeof import('naive-ui')['NTag']

electron/main/ipc/ipc-lyric.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import mainWindow from "../windows/main-window";
88
*/
99
const initLyricIpc = (): void => {
1010
const store = useStore();
11-
const mainWin = mainWindow.getWin();
1211

1312
// 歌词窗口
1413
let lyricWin: BrowserWindow | null = null;
@@ -58,6 +57,7 @@ const initLyricIpc = (): void => {
5857

5958
// 更新歌词窗口配置
6059
ipcMain.on("update-desktop-lyric-option", (_, option, callback: boolean = false) => {
60+
const mainWin = mainWindow.getWin();
6161
if (!option || !isWinAlive(lyricWin)) return;
6262
// 增量更新
6363
const prevOption = store.get("lyric.config");
@@ -69,7 +69,9 @@ const initLyricIpc = (): void => {
6969
if (callback && isWinAlive(lyricWin)) {
7070
lyricWin.webContents.send("update-desktop-lyric-option", option);
7171
}
72-
mainWin?.webContents.send("update-desktop-lyric-option", option);
72+
if (isWinAlive(mainWin)) {
73+
mainWin?.webContents.send("update-desktop-lyric-option", option);
74+
}
7375
});
7476

7577
// 播放状态更改
@@ -155,7 +157,8 @@ const initLyricIpc = (): void => {
155157

156158
// 请求歌词数据
157159
ipcMain.on("request-desktop-lyric-data", () => {
158-
if (!isWinAlive(lyricWin)) return;
160+
const mainWin = mainWindow.getWin();
161+
if (!isWinAlive(lyricWin) || !isWinAlive(mainWin)) return;
159162
// 触发窗口更新
160163
mainWin?.webContents.send("request-desktop-lyric-data");
161164
});
@@ -171,14 +174,16 @@ const initLyricIpc = (): void => {
171174

172175
// 关闭桌面歌词
173176
ipcMain.on("closeDesktopLyric", () => {
174-
if (!isWinAlive(lyricWin)) return;
177+
const mainWin = mainWindow.getWin();
178+
if (!isWinAlive(lyricWin) || !isWinAlive(mainWin)) return;
175179
lyricWin.hide();
176180
mainWin?.webContents.send("closeDesktopLyric");
177181
});
178182

179183
// 锁定/解锁桌面歌词
180184
ipcMain.on("toogleDesktopLyricLock", (_, isLock: boolean, isTemp: boolean = false) => {
181-
if (!isWinAlive(lyricWin)) return;
185+
const mainWin = mainWindow.getWin();
186+
if (!isWinAlive(lyricWin) || !isWinAlive(mainWin)) return;
182187
// 是否穿透
183188
if (isLock) {
184189
lyricWin.setIgnoreMouseEvents(true, { forward: true });

electron/main/ipc/ipc-shortcut.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import mainWindow from "../windows/main-window";
77
* @returns void
88
*/
99
const initShortcutIpc = (): void => {
10-
const mainWin = mainWindow.getWin();
11-
1210
// 快捷键是否被注册
1311
ipcMain.handle("is-shortcut-registered", (_, shortcut: string) => isShortcutRegistered(shortcut));
1412

1513
// 注册快捷键
1614
ipcMain.handle("register-all-shortcut", (_, allShortcuts: any): string[] | false => {
15+
const mainWin = mainWindow.getWin();
1716
if (!mainWin || !allShortcuts) return false;
1817
// 卸载所有快捷键
1918
unregisterShortcuts();

electron/main/ipc/ipc-system.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ipcMain, net, powerSaveBlocker, session } from "electron";
1+
import { app, ipcMain, net, powerSaveBlocker, session } from "electron";
22
import { ipcLog } from "../logger";
33
import { getFonts } from "font-list";
44
import { useStore } from "../store";
@@ -10,7 +10,6 @@ import mainWindow from "../windows/main-window";
1010
*/
1111
const initSystemIpc = (): void => {
1212
const store = useStore();
13-
const mainWin = mainWindow.getWin();
1413

1514
/** 阻止系统息屏 ID */
1615
let preventId: number | null = null;
@@ -28,6 +27,11 @@ const initSystemIpc = (): void => {
2827
}
2928
});
3029

30+
// 退出应用
31+
ipcMain.on("quit-app", () => {
32+
app.exit();
33+
});
34+
3135
// 获取系统全部字体
3236
ipcMain.handle("get-all-fonts", async () => {
3337
try {
@@ -41,13 +45,18 @@ const initSystemIpc = (): void => {
4145

4246
// 取消代理
4347
ipcMain.on("remove-proxy", () => {
48+
const mainWin = mainWindow.getWin();
4449
store.set("proxy", "");
45-
mainWin?.webContents.session.setProxy({ proxyRules: "" });
50+
if (mainWin) {
51+
mainWin?.webContents.session.setProxy({ proxyRules: "" });
52+
}
4653
ipcLog.info("✅ Remove proxy successfully");
4754
});
4855

4956
// 配置网络代理
5057
ipcMain.on("set-proxy", (_, config) => {
58+
const mainWin = mainWindow.getWin();
59+
if (!mainWin) return;
5160
const proxyRules = `${config.protocol}://${config.server}:${config.port}`;
5261
store.set("proxy", proxyRules);
5362
mainWin?.webContents.session.setProxy({ proxyRules });

electron/main/ipc/ipc-tray.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import lyricWindow from "../windows/lyric-window";
77
*/
88
const initTrayIpc = (): void => {
99
const tray = getMainTray();
10-
const lyricWin = lyricWindow.getWin();
1110

1211
// 音乐播放状态更改
1312
ipcMain.on("play-status-change", (_, playStatus: boolean) => {
13+
const lyricWin = lyricWindow.getWin();
1414
tray?.setPlayState(playStatus ? "play" : "pause");
15-
lyricWin?.webContents.send("play-status-change", playStatus);
15+
if (!lyricWin) return;
16+
lyricWin.webContents.send("play-status-change", playStatus);
1617
});
1718

1819
// 音乐名称更改

electron/main/ipc/ipc-update.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { checkUpdate, startDownloadUpdate } from "../update";
33
import mainWindow from "../windows/main-window";
44

55
const initUpdateIpc = () => {
6-
const mainWin = mainWindow.getWin();
7-
86
// 检查更新
9-
ipcMain.on("check-update", (_event, showTip) => checkUpdate(mainWin!, showTip));
7+
ipcMain.on("check-update", (_event, showTip) => {
8+
const mainWin = mainWindow.getWin();
9+
if (!mainWin) return;
10+
checkUpdate(mainWin, showTip);
11+
});
1012

1113
// 开始下载更新
1214
ipcMain.on("start-download-update", () => startDownloadUpdate());

electron/main/ipc/ipc-window.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@ import loginWindow from "../windows/login-window";
1111
* @returns void
1212
*/
1313
const initWindowsIpc = (): void => {
14-
// 相关窗口
15-
const mainWin = mainWindow.getWin();
16-
const loadWin = loadWindow.getWin();
1714
// store
1815
const store = useStore();
1916

2017
// 当前窗口状态
2118
ipcMain.on("win-state", (event) => {
19+
const mainWin = mainWindow.getWin();
20+
if (!mainWin) return;
2221
event.returnValue = mainWin?.isMaximized();
2322
});
2423

2524
// 加载完成
2625
ipcMain.on("win-loaded", () => {
26+
const loadWin = loadWindow.getWin();
27+
const mainWin = mainWindow.getWin();
2728
if (loadWin && !loadWin.isDestroyed()) loadWin.destroy();
2829
const isMaximized = store.get("window")?.maximized;
2930
if (isMaximized) mainWin?.maximize();
31+
if (!mainWin) return;
3032
mainWin?.show();
3133
mainWin?.focus();
3234
// 解决窗口不立即显示
@@ -45,34 +47,37 @@ const initWindowsIpc = (): void => {
4547

4648
// 最小化
4749
ipcMain.on("win-min", (event) => {
50+
const mainWin = mainWindow.getWin();
51+
if (!mainWin) return;
4852
event.preventDefault();
4953
mainWin?.minimize();
5054
});
5155

5256
// 最大化
5357
ipcMain.on("win-max", () => {
58+
const mainWin = mainWindow.getWin();
59+
if (!mainWin) return;
5460
mainWin?.maximize();
5561
});
5662

5763
// 还原
5864
ipcMain.on("win-restore", () => {
65+
const mainWin = mainWindow.getWin();
66+
if (!mainWin) return;
5967
mainWin?.restore();
6068
});
6169

62-
// 关闭
63-
ipcMain.on("win-close", (event) => {
64-
event.preventDefault();
65-
mainWin?.close();
66-
app.quit();
67-
});
68-
6970
// 隐藏
7071
ipcMain.on("win-hide", () => {
72+
const mainWin = mainWindow.getWin();
73+
if (!mainWin) return;
7174
mainWin?.hide();
7275
});
7376

7477
// 显示
7578
ipcMain.on("win-show", () => {
79+
const mainWin = mainWindow.getWin();
80+
if (!mainWin) return;
7681
mainWin?.show();
7782
mainWin?.focus();
7883
});
@@ -85,11 +90,15 @@ const initWindowsIpc = (): void => {
8590

8691
// 向主窗口发送事件
8792
ipcMain.on("send-to-mainWin", (_, eventName, ...args) => {
88-
mainWin?.webContents.send(eventName, ...args);
93+
const mainWin = mainWindow.getWin();
94+
if (!mainWin || mainWin.isDestroyed() || mainWin.webContents.isDestroyed()) return;
95+
mainWin.webContents.send(eventName, ...args);
8996
});
9097

9198
// 显示进度
9299
ipcMain.on("set-bar", (_event, val: number | "none" | "indeterminate" | "error" | "paused") => {
100+
const mainWin = mainWindow.getWin();
101+
if (!mainWin) return;
93102
switch (val) {
94103
case "none":
95104
mainWin?.setProgressBar(-1);
@@ -115,17 +124,25 @@ const initWindowsIpc = (): void => {
115124

116125
// 开启控制台
117126
ipcMain.on("open-dev-tools", () => {
127+
const mainWin = mainWindow.getWin();
128+
if (!mainWin) return;
118129
mainWin?.webContents.openDevTools({
119130
title: "SPlayer DevTools",
120131
mode: isDev ? "right" : "detach",
121132
});
122133
});
123134

124135
// 开启登录窗口
125-
ipcMain.on("open-login-web", () => loginWindow.create(mainWin!));
136+
ipcMain.on("open-login-web", () => {
137+
const mainWin = mainWindow.getWin();
138+
if (!mainWin) return;
139+
loginWindow.create(mainWin);
140+
});
126141

127142
// 开启设置
128143
ipcMain.on("open-setting", (_, type) => {
144+
const mainWin = mainWindow.getWin();
145+
if (!mainWin) return;
129146
mainWin?.show();
130147
mainWin?.focus();
131148
mainWin?.webContents.send("openSetting", type);

electron/main/windows/lyric-window.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ class LyricWindow {
2828
});
2929
// 歌词窗口关闭
3030
this.win?.on("close", () => {
31+
this.win = null;
3132
const mainWin = mainWindow?.getWin();
32-
if (!mainWin || mainWin.isDestroyed() || mainWin.webContents.isDestroyed()) return;
33-
mainWin?.webContents.send("closeDesktopLyric");
33+
if (mainWin) {
34+
mainWin?.webContents.send("closeDesktopLyric");
35+
}
3436
});
3537
}
3638
/**
@@ -79,7 +81,8 @@ class LyricWindow {
7981
* @returns BrowserWindow | null
8082
*/
8183
getWin(): BrowserWindow | null {
82-
return this.win;
84+
if (this.win && !this.win?.isDestroyed()) return this.win;
85+
return null;
8386
}
8487
}
8588

electron/main/windows/main-window.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ class MainWindow {
8181
this.saveBounds();
8282
});
8383
}
84+
// 窗口关闭
85+
this.win?.on("close", (event) => {
86+
event.preventDefault();
87+
this.win?.show();
88+
this.win?.focus();
89+
this.win?.webContents.send("win-will-close");
90+
});
8491
}
8592
/**
8693
* 创建窗口
@@ -110,7 +117,10 @@ class MainWindow {
110117
* @returns BrowserWindow | null
111118
*/
112119
getWin(): BrowserWindow | null {
113-
return this.win;
120+
if (this.win && !this.win.isDestroyed()) {
121+
return this.win;
122+
}
123+
return null;
114124
}
115125
/**
116126
* 显示主窗口

src/components/Layout/Nav.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const hideOrClose = (action: "hide" | "exit") => {
124124
settingStore.closeAppMethod = action;
125125
}
126126
showCloseModal.value = false;
127-
window.electron.ipcRenderer.send(action === "hide" ? "win-hide" : "win-close");
127+
window.electron.ipcRenderer.send(action === "hide" ? "win-hide" : "quit-app");
128128
};
129129
130130
// 尝试关闭软件
@@ -203,6 +203,9 @@ onMounted(() => {
203203
window.electron.ipcRenderer.on("win-state-change", (_event, value: boolean) => {
204204
isMax.value = value;
205205
});
206+
window.electron.ipcRenderer.on("win-will-close", () => {
207+
tryClose();
208+
});
206209
}
207210
});
208211
</script>

0 commit comments

Comments
 (0)