Skip to content

Commit 242c6f2

Browse files
committed
🎈 perf: 优化相关引用
1 parent 413b74b commit 242c6f2

40 files changed

Lines changed: 251 additions & 154 deletions

components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ declare module 'vue' {
101101
NQrCode: typeof import('naive-ui')['NQrCode']
102102
NRadio: typeof import('naive-ui')['NRadio']
103103
NRadioGroup: typeof import('naive-ui')['NRadioGroup']
104+
NResult: typeof import('naive-ui')['NResult']
104105
NScrollbar: typeof import('naive-ui')['NScrollbar']
105106
NSelect: typeof import('naive-ui')['NSelect']
106107
NSkeleton: typeof import('naive-ui')['NSkeleton']

electron/main/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import initAppServer from "../server";
99
import { initSingleLock } from "./utils/single-lock";
1010
import loadWindow from "./windows/load-window";
1111
import mainWindow from "./windows/main-window";
12-
import lyricWindow from "./windows/lyric-window";
1312
import initIpc from "./ipc";
1413

1514
// 屏蔽报错
@@ -19,7 +18,6 @@ process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true";
1918
class MainProcess {
2019
// 窗口
2120
mainWindow: BrowserWindow | null = null;
22-
lyricWindow: BrowserWindow | null = null;
2321
loadWindow: BrowserWindow | null = null;
2422
// 托盘
2523
mainTray: MainTray | null = null;
@@ -44,9 +42,8 @@ class MainProcess {
4442
// 启动窗口
4543
this.loadWindow = loadWindow.create();
4644
this.mainWindow = mainWindow.create();
47-
this.lyricWindow = lyricWindow.create();
4845
// 注册其他服务
49-
this.mainTray = initTray(this.mainWindow!, this.lyricWindow!);
46+
this.mainTray = initTray(this.mainWindow!);
5047
// 注册 IPC 通信
5148
initIpc();
5249
});

electron/main/ipc/ipc-file.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { app, BrowserWindow, dialog, ipcMain, shell } from "electron";
2-
import { basename, join, resolve } from "path";
2+
import { basename, isAbsolute, join, relative, resolve } from "path";
33
import { access, readFile, stat, unlink, writeFile } from "fs/promises";
44
import { parseFile } from "music-metadata";
55
import { getFileID, getFileMD5, metaDataLyricsArrayToLrc } from "../utils/helper";
@@ -371,6 +371,16 @@ const initFileIpc = (): void => {
371371
}
372372
},
373373
);
374+
375+
// 检查是否是子文件夹
376+
ipcMain.handle("check-if-subfolder", (_, localFilesPath: string[], selectedDir: string) => {
377+
const resolvedSelectedDir = resolve(selectedDir);
378+
const allPaths = localFilesPath.map((p) => resolve(p));
379+
return allPaths.some((existingPath) => {
380+
const relativePath = relative(existingPath, resolvedSelectedDir);
381+
return relativePath && !relativePath.startsWith("..") && !isAbsolute(relativePath);
382+
});
383+
});
374384
};
375385

376386
export default initFileIpc;

electron/main/ipc/ipc-lyric.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { ipcMain, screen } from "electron";
2-
import { isAbsolute, relative, resolve } from "path";
1+
import { BrowserWindow, ipcMain, screen } from "electron";
32
import { useStore } from "../store";
43
import lyricWindow from "../windows/lyric-window";
54
import mainWindow from "../windows/main-window";
@@ -10,30 +9,54 @@ import mainWindow from "../windows/main-window";
109
const initLyricIpc = (): void => {
1110
const store = useStore();
1211
const mainWin = mainWindow.getWin();
13-
const lyricWin = lyricWindow.getWin();
12+
13+
// 歌词窗口
14+
let lyricWin: BrowserWindow | null = null;
1415

1516
// 切换桌面歌词
16-
ipcMain.on("change-desktop-lyric", (_event, val: boolean) => {
17+
ipcMain.on("toggle-desktop-lyric", (_event, val: boolean) => {
1718
if (val) {
18-
lyricWin?.show();
19+
lyricWin = lyricWindow.create();
1920
lyricWin?.setAlwaysOnTop(true, "screen-saver");
20-
} else lyricWin?.hide();
21+
} else {
22+
lyricWin?.destroy();
23+
lyricWin = null;
24+
}
25+
});
26+
27+
// 更新歌词窗口数据
28+
ipcMain.on("update-desktop-lyric-data", (_, lyricData) => {
29+
if (!lyricData || !lyricWin) return;
30+
lyricWin?.webContents.send("update-desktop-lyric-data", lyricData);
31+
});
32+
33+
// 更新歌词窗口配置
34+
ipcMain.on("update-desktop-lyric-option", (_, option) => {
35+
if (!option || !lyricWin) return;
36+
lyricWin?.webContents.send("desktop-lyric-option-change", option);
37+
});
38+
39+
// 播放状态更改
40+
ipcMain.on("play-status-change", (_, status) => {
41+
if (!status || !lyricWin) return;
42+
lyricWin?.webContents.send("update-desktop-lyric-data", { playStatus: status });
2143
});
2244

2345
// 音乐名称更改
2446
ipcMain.on("play-song-change", (_, title) => {
25-
if (!title) return;
26-
lyricWin?.webContents.send("play-song-change", title);
47+
if (!title || !lyricWin) return;
48+
lyricWin?.webContents.send("update-desktop-lyric-data", { playName: title });
2749
});
2850

2951
// 音乐歌词更改
3052
ipcMain.on("play-lyric-change", (_, lyricData) => {
31-
if (!lyricData) return;
32-
lyricWin?.webContents.send("play-lyric-change", lyricData);
53+
if (!lyricData || !lyricWin) return;
54+
lyricWin?.webContents.send("update-desktop-lyric-data", lyricData);
3355
});
3456

3557
// 获取窗口位置
3658
ipcMain.handle("get-window-bounds", () => {
59+
if (!lyricWin) return {};
3760
return lyricWin?.getBounds();
3861
});
3962

@@ -45,7 +68,8 @@ const initLyricIpc = (): void => {
4568

4669
// 移动窗口
4770
ipcMain.on("move-window", (_, x, y, width, height) => {
48-
lyricWin?.setBounds({ x, y, width, height });
71+
if (!lyricWin) return;
72+
lyricWin.setBounds({ x, y, width, height });
4973
// 保存配置
5074
store.set("lyric", { ...store.get("lyric"), x, y, width, height });
5175
// 保持置顶
@@ -82,7 +106,8 @@ const initLyricIpc = (): void => {
82106

83107
// 关闭桌面歌词
84108
ipcMain.on("closeDesktopLyric", () => {
85-
lyricWin?.hide();
109+
if (!lyricWin) return;
110+
lyricWin.hide();
86111
mainWin?.webContents.send("closeDesktopLyric");
87112
});
88113

@@ -96,16 +121,6 @@ const initLyricIpc = (): void => {
96121
lyricWin.setIgnoreMouseEvents(false);
97122
}
98123
});
99-
100-
// 检查是否是子文件夹
101-
ipcMain.handle("check-if-subfolder", (_, localFilesPath: string[], selectedDir: string) => {
102-
const resolvedSelectedDir = resolve(selectedDir);
103-
const allPaths = localFilesPath.map((p) => resolve(p));
104-
return allPaths.some((existingPath) => {
105-
const relativePath = relative(existingPath, resolvedSelectedDir);
106-
return relativePath && !relativePath.startsWith("..") && !isAbsolute(relativePath);
107-
});
108-
});
109124
};
110125

111126
export default initLyricIpc;

electron/main/ipc/ipc-tray.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const initTrayIpc = (): void => {
3434
});
3535

3636
// 桌面歌词开关
37-
ipcMain.on("change-desktop-lyric", (_, val: boolean) => {
37+
ipcMain.on("toggle-desktop-lyric", (_, val: boolean) => {
3838
tray?.setDesktopLyricShow(val);
3939
});
4040

electron/main/tray/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { isWin, appName } from "../utils/config";
1111
import { join } from "path";
1212
import { trayLog } from "../logger";
13+
import lyricWindow from "../windows/lyric-window";
1314

1415
// 播放模式
1516
type PlayMode = "repeat" | "repeat-once" | "shuffle";
@@ -49,7 +50,7 @@ const trayIcon = (filename: string) => {
4950
// 托盘菜单
5051
const createTrayMenu = (
5152
win: BrowserWindow,
52-
lyricWin: BrowserWindow,
53+
lyricWin: BrowserWindow | null,
5354
): MenuItemConstructorOptions[] => {
5455
// 区分明暗图标
5556
const showIcon = (iconName: string) => {
@@ -145,8 +146,12 @@ const createTrayMenu = (
145146
id: "toogleDesktopLyricLock",
146147
label: `${desktopLyricLock ? "解锁" : "锁定"}桌面歌词`,
147148
icon: showIcon(desktopLyricLock ? "lock" : "unlock"),
148-
visible: desktopLyricShow,
149-
click: () => lyricWin.webContents.send("toogleDesktopLyricLock", !desktopLyricLock),
149+
visible: desktopLyricShow && lyricWin !== null,
150+
click: () => {
151+
if (lyricWin) {
152+
lyricWin.webContents.send("toogleDesktopLyricLock", !desktopLyricLock);
153+
}
154+
},
150155
},
151156
{
152157
type: "separator",
@@ -182,22 +187,22 @@ const createTrayMenu = (
182187
class CreateTray implements MainTray {
183188
// 窗口
184189
private _win: BrowserWindow;
185-
private _lyricWin: BrowserWindow;
190+
private _lyricWin: BrowserWindow | null;
186191
// 托盘
187192
private _tray: Tray;
188193
// 菜单
189194
private _menu: MenuItemConstructorOptions[];
190195
private _contextMenu: Menu;
191196

192-
constructor(win: BrowserWindow, lyricWin: BrowserWindow) {
197+
constructor(win: BrowserWindow) {
193198
// 托盘图标
194199
const icon = trayIcon(isWin ? "tray.ico" : "tray@32.png").resize({
195200
height: 32,
196201
width: 32,
197202
});
198203
// 初始化数据
199204
this._win = win;
200-
this._lyricWin = lyricWin;
205+
this._lyricWin = lyricWindow.getWin();
201206
this._tray = new Tray(icon);
202207
this._menu = createTrayMenu(this._win, this._lyricWin);
203208
this._contextMenu = Menu.buildFromTemplate(this._menu);
@@ -301,10 +306,10 @@ class CreateTray implements MainTray {
301306
* @param lyricWin 歌词窗口
302307
* @returns 托盘实例
303308
*/
304-
export const initTray = (win: BrowserWindow, lyricWin: BrowserWindow) => {
309+
export const initTray = (win: BrowserWindow) => {
305310
try {
306311
trayLog.info("🚀 Tray Process Startup");
307-
const tray = new CreateTray(win, lyricWin);
312+
const tray = new CreateTray(win);
308313
// 保存单例实例
309314
mainTrayInstance = tray;
310315
return tray;

electron/main/utils/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ export const mainWinUrl =
4141
? process.env["ELECTRON_RENDERER_URL"]
4242
: `http://localhost:${port}`;
4343

44+
/**
45+
* 歌词窗口加载地址
46+
* @returns string
47+
*/
48+
export const lyricWinUrl =
49+
isDev && process.env["ELECTRON_RENDERER_URL"]
50+
? `${process.env["ELECTRON_RENDERER_URL"]}/#/desktop-lyric`
51+
: `http://localhost:${port}/#/desktop-lyric`;
52+
4453
/**
4554
* 加载窗口地址
4655
* @returns string

electron/main/windows/lyric-window.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BrowserWindow } from "electron";
22
import { createWindow } from "./index";
33
import { useStore } from "../store";
4-
import { join } from "path";
4+
import { lyricWinUrl } from "../utils/config";
55

66
class LyricWindow {
77
private win: BrowserWindow | null = null;
@@ -12,6 +12,10 @@ class LyricWindow {
1212
*/
1313
private event(): void {
1414
if (!this.win) return;
15+
// 准备好显示
16+
this.win.on("ready-to-show", () => {
17+
this.win?.show();
18+
});
1519
// 歌词窗口缩放
1620
this.win?.on("resized", () => {
1721
const store = useStore();
@@ -56,7 +60,7 @@ class LyricWindow {
5660
});
5761
if (!this.win) return null;
5862
// 加载地址
59-
this.win.loadFile(join(__dirname, "../main/web/lyric.html"));
63+
this.win.loadURL(lyricWinUrl);
6064
// 窗口事件
6165
this.event();
6266
return this.win;

src/api/song.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isElectron } from "@/utils/helper";
1+
import { isElectron } from "@/utils/env";
22
import { songLevelData } from "@/utils/meta";
33
import request from "@/utils/request";
44

src/components/Card/SongCard.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,14 @@
153153
<script setup lang="ts">
154154
import type { SongType } from "@/types/main";
155155
import { useStatusStore, useMusicStore, useDataStore } from "@/stores";
156-
import { formatNumber, isElectron } from "@/utils/helper";
156+
import { formatNumber } from "@/utils/helper";
157157
import { openJumpArtist } from "@/utils/modal";
158158
import { toLikeSong } from "@/utils/auth";
159159
import { isObject } from "lodash-es";
160160
import { formatTimestamp, msToTime } from "@/utils/time";
161161
import player from "@/utils/player";
162162
import blob from "@/utils/blob";
163+
import { isElectron } from "@/utils/env";
163164
164165
const props = defineProps<{
165166
// 歌曲

0 commit comments

Comments
 (0)