@@ -8,56 +8,26 @@ import taskbarLyricManager from "../utils/taskbar-lyric-manager";
88
99let cachedIsPlaying = false ;
1010
11+ /** 读取完整任务栏配置 */
1112const getTaskbarConfig = ( ) : TaskbarConfig => {
12- const store = useStore ( ) ;
13- return {
14- mode : store . get ( "taskbar.mode" , "taskbar" ) ,
15- maxWidth : store . get ( "taskbar.maxWidth" , 300 ) ,
16- position : store . get ( "taskbar.position" , "automatic" ) ,
17- autoShrink : store . get ( "taskbar.autoShrink" , false ) ,
18- margin : store . get ( "taskbar.margin" , 10 ) ,
19- minWidth : store . get ( "taskbar.minWidth" , 10 ) ,
20- enabled : store . get ( "taskbar.enabled" , false ) ,
21- floatingAlign : store . get ( "taskbar.floatingAlign" , "right" ) ,
22- floatingAutoWidth : store . get ( "taskbar.floatingAutoWidth" , true ) ,
23- floatingWidth : store . get ( "taskbar.floatingWidth" , 300 ) ,
24- floatingHeight : store . get ( "taskbar.floatingHeight" , 48 ) ,
25- floatingAlwaysOnTop : store . get ( "taskbar.floatingAlwaysOnTop" , false ) ,
26-
27- showWhenPaused : store . get ( "taskbar.showWhenPaused" , true ) ,
28- showCover : store . get ( "taskbar.showCover" , true ) ,
29- themeMode : store . get ( "taskbar.themeMode" , "auto" ) ,
30- fontFamily : store . get ( "taskbar.fontFamily" , "" ) ,
31- globalFont : store . get ( "taskbar.globalFont" , "" ) ,
32- fontWeight : store . get ( "taskbar.fontWeight" , 0 ) ,
33- animationMode : store . get ( "taskbar.animationMode" , "slide-blur" ) ,
34- singleLineMode : store . get ( "taskbar.singleLineMode" , false ) ,
35- showTranslation : store . get ( "taskbar.showTranslation" , true ) ,
36- showRomaji : store . get ( "taskbar.showRomaji" , true ) ,
37- showWordLyrics : store . get ( "taskbar.showWordLyrics" , true ) ,
38- } ;
13+ return useStore ( ) . get ( "taskbar" ) ;
3914} ;
4015
16+ /** 根据配置更新窗口可见性 */
4117const updateWindowVisibility = ( config : TaskbarConfig ) => {
4218 const tray = getMainTray ( ) ;
43-
44- if ( tray ) {
45- tray . setTaskbarLyricShow ( config . enabled ) ;
19+ if ( tray ) tray . setTaskbarLyricShow ( config . enabled ) ;
20+ if ( ! config . enabled ) {
21+ taskbarLyricManager . close ( false ) ;
22+ return ;
4623 }
47-
48- const shouldBeVisible = config . enabled && ( cachedIsPlaying || config . showWhenPaused ) ;
49-
24+ taskbarLyricManager . create ( config . mode ) ;
25+ const shouldBeVisible = cachedIsPlaying || config . showWhenPaused ;
5026 taskbarLyricManager . setVisibility ( shouldBeVisible ) ;
5127} ;
5228
53- const updateWindowLayout = ( animate : boolean = true ) => {
54- taskbarLyricManager . updateLayout ( animate ) ;
55- } ;
56-
5729const initTaskbarIpc = ( ) => {
58- // 在函数内部获取 store,确保在 app ready 事件之后
5930 const store = useStore ( ) ;
60-
6131 const initialConfig = getTaskbarConfig ( ) ;
6232 if ( initialConfig . enabled ) {
6333 taskbarLyricManager . create ( initialConfig . mode ) ;
@@ -68,67 +38,30 @@ const initTaskbarIpc = () => {
6838 taskbarLyricManager . setContentWidth ( width ) ;
6939 } ) ;
7040
71- ipcMain . on (
72- TASKBAR_IPC_CHANNELS . UPDATE_CONFIG ,
73- ( _event , partialConfig : Partial < TaskbarConfig > ) => {
74- const oldConfig = getTaskbarConfig ( ) ;
41+ ipcMain . handle ( TASKBAR_IPC_CHANNELS . GET_OPTION , ( ) => getTaskbarConfig ( ) ) ;
7542
76- Object . entries ( partialConfig ) . forEach ( ( [ key , value ] ) => {
43+ // 设置配置(增量合并)
44+ ipcMain . on (
45+ TASKBAR_IPC_CHANNELS . SET_OPTION ,
46+ ( _event , option : Partial < TaskbarConfig > , pushToWindow = true ) => {
47+ if ( ! option ) return ;
48+ // 增量更新
49+ const prev = getTaskbarConfig ( ) ;
50+ const next = { ...prev , ...option } ;
51+ Object . entries ( option ) . forEach ( ( [ key , value ] ) => {
7752 store . set ( `taskbar.${ key } ` , value ) ;
7853 } ) ;
79-
80- const newConfig = getTaskbarConfig ( ) ;
81-
82- const modeChanged = newConfig . mode !== oldConfig . mode ;
83-
84- if ( modeChanged ) {
85- taskbarLyricManager . close ( false ) ;
54+ // 推送到歌词窗口
55+ if ( pushToWindow ) {
56+ taskbarLyricManager . send ( TASKBAR_IPC_CHANNELS . SYNC_STATE , {
57+ type : "config-update" ,
58+ data : option ,
59+ } as SyncStatePayload ) ;
8660 }
87-
88- if ( newConfig . enabled && ( ! oldConfig . enabled || modeChanged ) ) {
89- taskbarLyricManager . create ( newConfig . mode ) ;
61+ updateWindowVisibility ( next ) ;
62+ if ( next . enabled ) {
63+ taskbarLyricManager . updateLayout ( false ) ;
9064 }
91-
92- if (
93- newConfig . enabled !== oldConfig . enabled ||
94- newConfig . showWhenPaused !== oldConfig . showWhenPaused ||
95- modeChanged
96- ) {
97- updateWindowVisibility ( newConfig ) ;
98- }
99-
100- if ( newConfig . enabled ) {
101- if ( newConfig . mode === "taskbar" ) {
102- if (
103- newConfig . maxWidth !== oldConfig . maxWidth ||
104- newConfig . position !== oldConfig . position ||
105- newConfig . autoShrink !== oldConfig . autoShrink ||
106- newConfig . margin !== oldConfig . margin ||
107- newConfig . minWidth !== oldConfig . minWidth
108- ) {
109- updateWindowLayout ( true ) ;
110- }
111- } else {
112- const floatingWidthChanged =
113- newConfig . floatingAutoWidth === false && newConfig . floatingWidth !== oldConfig . floatingWidth ;
114- if (
115- newConfig . maxWidth !== oldConfig . maxWidth ||
116- newConfig . floatingAlign !== oldConfig . floatingAlign ||
117- newConfig . floatingAutoWidth !== oldConfig . floatingAutoWidth ||
118- floatingWidthChanged ||
119- newConfig . floatingHeight !== oldConfig . floatingHeight ||
120- newConfig . floatingAlwaysOnTop !== oldConfig . floatingAlwaysOnTop ||
121- modeChanged
122- ) {
123- updateWindowLayout ( false ) ;
124- }
125- }
126- }
127-
128- taskbarLyricManager . send ( TASKBAR_IPC_CHANNELS . SYNC_STATE , {
129- type : "config-update" ,
130- data : partialConfig ,
131- } as SyncStatePayload ) ;
13265 } ,
13366 ) ;
13467
@@ -174,9 +107,8 @@ const initTaskbarIpc = () => {
174107 // 把事件发射到 app 里不太好,但是我觉得也没有必要为了这一个事件创建一个事件总线
175108 // TODO: 如果有了事件总线,通过那个事件总线发射这个事件
176109 ( app as EventEmitter ) . on ( "explorer-restarted" , ( ) => {
177- const currentEnabled = store . get ( "taskbar.enabled" ) ;
178- const currentMode = store . get ( "taskbar.mode" , "taskbar" ) ;
179- if ( currentEnabled && currentMode === "taskbar" ) {
110+ const currentConfig = getTaskbarConfig ( ) ;
111+ if ( currentConfig . enabled && currentConfig . mode === "taskbar" ) {
180112 taskbarLyricManager . close ( false ) ;
181113 setTimeout ( ( ) => {
182114 taskbarLyricManager . create ( "taskbar" ) ;
0 commit comments