Skip to content
This repository was archived by the owner on Jul 17, 2026. It is now read-only.

Commit 1ea302f

Browse files
committed
🐞 fix: RPC types
1 parent 0061ceb commit 1ea302f

4 files changed

Lines changed: 64 additions & 62 deletions

File tree

electron/main/ipc/ipc-smtc.ts

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@ type DiscordRpcModule = typeof import("@discord-rpc");
1010
let nativeSmtc: NativeSmtcModule | null = null;
1111
let discordRpcNative: DiscordRpcModule | null = null;
1212

13+
/**
14+
* 注册 IPC 处理函数
15+
* @param module 模块
16+
* @param moduleName 模块名称
17+
* @param channel 信道
18+
* @param handler 处理函数
19+
* @param errorContext 错误上下文
20+
*/
21+
const registerHandler = <M, K extends keyof IpcChannelMap>(
22+
module: M | null,
23+
moduleName: string,
24+
channel: K,
25+
handler: (module: M, payload: IpcChannelMap[K]) => void,
26+
errorContext: string,
27+
) => {
28+
ipcMain.on(channel, (_, payload: IpcChannelMap[K]) => {
29+
if (module) {
30+
try {
31+
handler(module, payload);
32+
} catch (e) {
33+
processLog.error(`[${moduleName}] ${errorContext} 失败`, e);
34+
}
35+
}
36+
});
37+
};
38+
1339
export default function initSmtcIpc() {
1440
// 加载 SMTC 原生模块(仅 Windows)
1541
nativeSmtc = loadNativeModule("smtc-for-splayer.node", "smtc-for-splayer");
@@ -53,91 +79,71 @@ export default function initSmtcIpc() {
5379
}
5480
}
5581

56-
// 注册原生 SMTC 事件处理器
57-
const registerNativeSmtcHandler = <K extends keyof IpcChannelMap>(
58-
channel: K,
59-
handler: (module: NativeSmtcModule, payload: IpcChannelMap[K]) => void,
60-
errorContext: string,
61-
) => {
62-
ipcMain.on(channel, (_, payload: IpcChannelMap[K]) => {
63-
if (nativeSmtc) {
64-
try {
65-
handler(nativeSmtc, payload);
66-
} catch (e) {
67-
processLog.error(`[SMTC] ${errorContext} 失败`, e);
68-
}
69-
}
70-
});
71-
};
72-
73-
// 注册 Discord RPC 事件处理器
74-
const registerDiscordRpcHandler = <K extends keyof IpcChannelMap>(
75-
channel: K,
76-
handler: (module: DiscordRpcModule, payload: IpcChannelMap[K]) => void,
77-
errorContext: string,
78-
) => {
79-
ipcMain.on(channel, (_, payload: IpcChannelMap[K]) => {
80-
if (discordRpcNative) {
81-
try {
82-
handler(discordRpcNative, payload);
83-
} catch (e) {
84-
processLog.error(`[Discord RPC] ${errorContext} 失败`, e);
85-
}
86-
}
87-
});
88-
};
89-
9082
// 元数据 - Discord
91-
registerDiscordRpcHandler(
83+
registerHandler(
84+
discordRpcNative,
85+
"Discord RPC",
9286
"discord-update-metadata",
9387
(mod, payload) => mod.updateMetadata(payload),
9488
"updateMetadata",
9589
);
9690

9791
// 元数据 - Native SMTC
98-
registerNativeSmtcHandler(
92+
registerHandler(
93+
nativeSmtc,
94+
"SMTC",
9995
"smtc-update-metadata",
10096
(mod, payload) => mod.updateMetadata(payload),
10197
"updateMetadata",
10298
);
10399

104100
// 播放状态 - Discord
105-
registerDiscordRpcHandler(
101+
registerHandler(
102+
discordRpcNative,
103+
"Discord RPC",
106104
"discord-update-play-state",
107105
(mod, payload) => mod.updatePlayState(payload),
108106
"updatePlayState",
109107
);
110108

111109
// 播放状态 - Native SMTC
112-
registerNativeSmtcHandler(
110+
registerHandler(
111+
nativeSmtc,
112+
"SMTC",
113113
"smtc-update-play-state",
114114
(mod, payload) => mod.updatePlayState(payload),
115115
"updatePlayState",
116116
);
117117

118118
// 进度信息 - Discord
119-
registerDiscordRpcHandler(
119+
registerHandler(
120+
discordRpcNative,
121+
"Discord RPC",
120122
"discord-update-timeline",
121123
(mod, payload) => mod.updateTimeline(payload),
122124
"updateTimeline",
123125
);
124126

125127
// 进度信息 - Native SMTC
126-
registerNativeSmtcHandler(
128+
registerHandler(
129+
nativeSmtc,
130+
"SMTC",
127131
"smtc-update-timeline",
128132
(mod, payload) => mod.updateTimeline(payload),
129133
"updateTimeline",
130134
);
131135

132136
// 播放模式 - Native SMTC
133-
registerNativeSmtcHandler(
137+
registerHandler(
138+
nativeSmtc,
139+
"SMTC",
134140
"smtc-update-play-mode",
135141
(mod, payload) => mod.updatePlayMode(payload),
136142
"updatePlayMode",
137143
);
138144

139145
// Discord - 开启
140-
ipcMain.on("smtc-enable-discord", () => {
146+
ipcMain.on("discord-enable", () => {
141147
if (discordRpcNative) {
142148
try {
143149
discordRpcNative.enable();
@@ -148,7 +154,7 @@ export default function initSmtcIpc() {
148154
});
149155

150156
// Discord - 关闭
151-
ipcMain.on("smtc-disable-discord", () => {
157+
ipcMain.on("discord-disable", () => {
152158
if (discordRpcNative) {
153159
try {
154160
discordRpcNative.disable();
@@ -159,8 +165,10 @@ export default function initSmtcIpc() {
159165
});
160166

161167
// Discord - 更新配置
162-
registerDiscordRpcHandler(
163-
"smtc-update-discord-config",
168+
registerHandler(
169+
discordRpcNative,
170+
"Discord RPC",
171+
"discord-update-config",
164172
(mod, payload) => mod.updateConfig(payload),
165173
"updateConfig",
166174
);

src/components/Setting/ThirdSetting.vue

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,13 @@
1010
</div>
1111
<n-switch v-model:value="settingStore.smtcOpen" class="set" :round="false" />
1212
</n-card>
13-
<n-collapse-transition :show="settingStore.smtcOpen && isWin">
13+
<n-collapse-transition :show="settingStore.smtcOpen && isElectron && isWin">
1414
<n-card class="set-item">
1515
<div class="label">
1616
<n-text class="name">原生 SMTC 支持</n-text>
17-
<n-text class="tip" :depth="3">
18-
使用原生插件与系统交互,支持高清封面显示
19-
</n-text>
17+
<n-text class="tip" :depth="3"> 使用原生插件与系统交互,支持高清封面显示 </n-text>
2018
</div>
21-
<n-switch
22-
v-model:value="settingStore.enableNativeSmtc"
23-
class="set"
24-
:round="false"
25-
/>
19+
<n-switch v-model:value="settingStore.enableNativeSmtc" class="set" :round="false" />
2620
</n-card>
2721
</n-collapse-transition>
2822
</div>

src/core/player/PlayerIpc.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ export const sendSmtcPlayMode = (isShuffling: boolean, repeatMode: RepeatMode) =
181181
*/
182182
export const enableDiscordRpc = () => {
183183
if (isElectron) {
184-
window.electron.ipcRenderer.send("smtc-enable-discord");
184+
window.electron.ipcRenderer.send("discord-enable");
185185
// 立即发送当前配置,确保 Rust 模块使用正确的设置
186186
const settingStore = useSettingStore();
187187
// 转换字符串 displayMode 为数字枚举
188188
const displayModeMap = { name: 0, state: 1, details: 2 } as const;
189-
window.electron.ipcRenderer.send("smtc-update-discord-config", {
189+
window.electron.ipcRenderer.send("discord-update-config", {
190190
showWhenPaused: settingStore.discordRpc.showWhenPaused,
191191
displayMode: displayModeMap[settingStore.discordRpc.displayMode],
192192
});
@@ -197,7 +197,7 @@ export const enableDiscordRpc = () => {
197197
* @description 禁用 Discord RPC
198198
*/
199199
export const disableDiscordRpc = () => {
200-
if (isElectron) window.electron.ipcRenderer.send("smtc-disable-discord");
200+
if (isElectron) window.electron.ipcRenderer.send("discord-disable");
201201
};
202202

203203
/**
@@ -211,7 +211,7 @@ export const updateDiscordConfig = (payload: {
211211
if (isElectron) {
212212
// 转换字符串 displayMode 为数字枚举
213213
const displayModeMap = { name: 0, state: 1, details: 2 } as const;
214-
window.electron.ipcRenderer.send("smtc-update-discord-config", {
214+
window.electron.ipcRenderer.send("discord-update-config", {
215215
showWhenPaused: payload.showWhenPaused,
216216
displayMode: displayModeMap[payload.displayMode],
217217
});

src/types/global.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface DiscordMetadataParam {
2222
}
2323

2424
export interface DiscordPlayStateParam {
25-
status: 0 | 1; // 0: Playing, 1: Paused
25+
status: "Playing" | "Paused";
2626
}
2727

2828
export interface DiscordTimelineParam {
@@ -35,9 +35,9 @@ export interface IpcChannelMap {
3535
"smtc-update-play-state": PlayStatePayload;
3636
"smtc-update-timeline": TimelinePayload;
3737
"smtc-update-play-mode": PlayModePayload;
38-
"smtc-enable-discord": void;
39-
"smtc-disable-discord": void;
40-
"smtc-update-discord-config": DiscordConfigPayload;
38+
"discord-enable": void;
39+
"discord-disable": void;
40+
"discord-update-config": DiscordConfigPayload;
4141
"discord-update-metadata": DiscordMetadataParam;
4242
"discord-update-play-state": DiscordPlayStateParam;
4343
"discord-update-timeline": DiscordTimelineParam;

0 commit comments

Comments
 (0)