Skip to content

Commit c96a44b

Browse files
authored
Merge pull request #607 from MoYingJi/prot
feat(protocol): 在设置中选择是否注册协议
2 parents 895843a + 03b2cae commit c96a44b

9 files changed

Lines changed: 62 additions & 15 deletions

File tree

electron/main/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import initAppServer from "../server";
1111
import loadWindow from "./windows/load-window";
1212
import mainWindow from "./windows/main-window";
1313
import initIpc from "./ipc";
14-
import { trySendCustomProtocol, registerCustomProtocol } from "./utils/protocol";
14+
import { trySendCustomProtocol } from "./utils/protocol";
1515

1616
// 屏蔽报错
1717
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true";
@@ -48,8 +48,6 @@ class MainProcess {
4848
processLog.info("🚀 Application Process Startup");
4949
// 设置应用程序名称
5050
electronApp.setAppUserModelId("com.imsyy.splayer");
51-
// 注册自定义协议
52-
registerCustomProtocol();
5351
// 启动主服务进程
5452
await initAppServer();
5553
// 启动窗口

electron/main/ipc/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import initThumbarIpc from "./ipc-thumbar";
77
import initTrayIpc from "./ipc-tray";
88
import initUpdateIpc from "./ipc-update";
99
import initWindowsIpc from "./ipc-window";
10+
import initProtocolIpc from "./ipc-protocol";
1011

1112
/**
1213
* 初始化全部 IPC 通信
@@ -22,6 +23,7 @@ const initIpc = (): void => {
2223
initStoreIpc();
2324
initThumbarIpc();
2425
initShortcutIpc();
26+
initProtocolIpc();
2527
};
2628

2729
export default initIpc;

electron/main/ipc/ipc-protocol.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { app, ipcMain } from "electron";
2+
import { processLog } from "../logger";
3+
4+
const initProtocolIpc = (): void => {
5+
ipcMain.on("register-protocol", (_, protocol: string) => {
6+
app.setAsDefaultProtocolClient(protocol)
7+
processLog.info("🔗 Registered custom protocol", protocol);
8+
})
9+
10+
ipcMain.on("unregister-protocol", (_, protocol: string) => {
11+
app.removeAsDefaultProtocolClient(protocol)
12+
processLog.info("🔗 Unregistered custom protocol", protocol);
13+
})
14+
}
15+
16+
export default initProtocolIpc;

electron/main/utils/protocol.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
import { app } from "electron";
2-
import { processLog } from "../logger";
31
import mainWindow from "../windows/main-window";
4-
5-
/**
6-
* 注册自定义协议
7-
* 用于在外部打开应用时传递 URL 参数
8-
*/
9-
export const registerCustomProtocol = () => {
10-
app.setAsDefaultProtocolClient("orpheus");
11-
processLog.info("🔗 Registered custom protocol");
12-
};
2+
import { processLog } from "../logger";
133

144
/**
155
* 尝试发送自定义协议 URL 到主窗口

src/components/Setting/GeneralSetting.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,15 @@
315315
</div>
316316
<n-switch v-model:value="settingStore.preventSleep" class="set" :round="false" />
317317
</n-card>
318+
<n-card class="set-item">
319+
<div class="label">
320+
<n-text class="name">通过 Orpheus 协议唤起本应用</n-text>
321+
<n-text class="tip" :depth="3">
322+
该协议通常用于官方网页端唤起官方客户端, 启用后可能导致官方客户端无法被唤起
323+
</n-text>
324+
</div>
325+
<n-switch v-model:value="settingStore.registryProtocol.orpheus" class="set" :round="false" @update:value="orpheusChange" />
326+
</n-card>
318327
<n-card class="set-item">
319328
<div class="label">
320329
<n-text class="name">自动检查更新</n-text>
@@ -334,6 +343,7 @@ import songManager from "@/utils/songManager";
334343
import { isEmpty } from "lodash-es";
335344
import themeColor from "@/assets/data/themeColor.json";
336345
import { openSidebarHideManager, openHomePageSectionManager } from "@/utils/modal";
346+
import { sendRegisterProtocol } from "@/utils/protocol";
337347
338348
const dataStore = useDataStore();
339349
const musicStore = useMusicStore();
@@ -441,6 +451,11 @@ const themeGlobalColorChange = (val: boolean) => {
441451
if (val) songManager.getCoverColor(musicStore.songCover);
442452
};
443453
454+
// 注册或取消注册协议
455+
const orpheusChange = async (isRegistry: boolean) => {
456+
sendRegisterProtocol("orpheus", isRegistry)
457+
};
458+
444459
onMounted(() => {
445460
if (isElectron) {
446461
getAllSystemFonts();

src/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import initIpc from "@/utils/initIpc";
1313
import "@/style/main.scss";
1414
import "@/style/animate.scss";
1515
import "github-markdown-css/github-markdown.css";
16+
import { useSettingStore } from "@/stores";
17+
import { sendRegisterProtocol } from "@/utils/protocol";
1618

1719
// 初始化 ipc
1820
initIpc();
@@ -31,3 +33,7 @@ app.directive("throttle", throttleDirective);
3133
app.directive("visible", visibleDirective);
3234
// app
3335
app.mount("#app");
36+
37+
// 根据设置判断是否要注册协议
38+
const settings = useSettingStore();
39+
sendRegisterProtocol("orpheus", settings.registryProtocol.orpheus);

src/stores/setting.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ export interface SettingState {
221221
visible: boolean;
222222
order: number;
223223
}>;
224+
/** 自定义协议注册 **/
225+
registryProtocol: {
226+
orpheus: boolean;
227+
};
224228
}
225229

226230
export const useSettingStore = defineStore("setting", {
@@ -333,6 +337,9 @@ export const useSettingStore = defineStore("setting", {
333337
{ key: "radio", name: "推荐播客", visible: true, order: 4 },
334338
{ key: "album", name: "新碟上架", visible: true, order: 5 },
335339
],
340+
registryProtocol: {
341+
orpheus: false,
342+
},
336343
}),
337344
getters: {
338345
/**

src/utils/initIpc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { usePlayer } from "./player";
66
import { cloneDeep } from "lodash-es";
77
import songManager from "./songManager";
88
import { SettingType } from "@/types/main";
9-
import { handleProtocolUrl } from "@/utils/protocal";
9+
import { handleProtocolUrl } from "@/utils/protocol";
1010

1111
// 关闭更新状态
1212
const closeUpdateStatus = () => {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ class OrpheusData {
1414
cmd: string;
1515
}
1616

17+
/**
18+
* 发送 注册/取消注册 协议的 Ipc
19+
* @param protocol 协议名
20+
* @param register true 则注册,false 则取消注册
21+
*/
22+
export const sendRegisterProtocol = (protocol: string, register: boolean = true) => {
23+
if (register) {
24+
window.electron.ipcRenderer.send("register-protocol", protocol);
25+
} else {
26+
window.electron.ipcRenderer.send("unregister-protocol", protocol);
27+
}
28+
};
29+
1730
export const handleProtocolUrl = (url: string) => {
1831
switch (true) {
1932
case url.startsWith("orpheus://"):

0 commit comments

Comments
 (0)