1- import { ipcMain , screen } from "electron" ;
2- import { isAbsolute , relative , resolve } from "path" ;
1+ import { BrowserWindow , ipcMain , screen } from "electron" ;
32import { useStore } from "../store" ;
43import lyricWindow from "../windows/lyric-window" ;
54import mainWindow from "../windows/main-window" ;
@@ -10,30 +9,54 @@ import mainWindow from "../windows/main-window";
109const 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
111126export default initLyricIpc ;
0 commit comments