@@ -20,7 +20,7 @@ const store = new Store();
20
20
21
21
// 保存主窗口引用,以便在 activate 事件中使用
22
22
let mainWindowInstance : BrowserWindow | null = null ;
23
-
23
+ let isPlaying = false ;
24
24
// 保存迷你模式前的窗口状态
25
25
let preMiniModeState : WindowState = {
26
26
width : DEFAULT_MAIN_WIDTH ,
@@ -56,6 +56,38 @@ function initializeProxy() {
56
56
}
57
57
}
58
58
59
+ function setThumbarButtons ( window : BrowserWindow ) {
60
+ window . setThumbarButtons ( [
61
+ {
62
+ tooltip : 'prev' ,
63
+ icon : nativeImage
64
+ . createFromPath ( join ( app . getAppPath ( ) , 'resources/icons' , 'prev.png' ) ) ,
65
+ click ( ) {
66
+ window . webContents . send ( 'global-shortcut' , 'prevPlay' ) ;
67
+ } ,
68
+ } ,
69
+
70
+ {
71
+ tooltip : isPlaying ? 'pause' : 'play' ,
72
+ icon : nativeImage
73
+ . createFromPath ( join ( app . getAppPath ( ) , 'resources/icons' , isPlaying ? 'pause.png' : 'play.png' ) ) ,
74
+ click ( ) {
75
+ window . webContents . send ( 'global-shortcut' , 'togglePlay' ) ;
76
+ } ,
77
+ } ,
78
+
79
+ {
80
+ tooltip : 'next' ,
81
+ icon : nativeImage
82
+ . createFromPath ( join ( app . getAppPath ( ) , 'resources/icons' , 'next.png' ) ) ,
83
+ click ( ) {
84
+ window . webContents . send ( 'global-shortcut' , 'nextPlay' ) ;
85
+ } ,
86
+ }
87
+ ] ) ;
88
+ }
89
+
90
+
59
91
/**
60
92
* 初始化窗口管理相关的IPC监听
61
93
*/
@@ -107,7 +139,7 @@ export function initializeWindowManager() {
107
139
// 获取屏幕工作区尺寸
108
140
const display = screen . getDisplayMatching ( win . getBounds ( ) ) ;
109
141
const { width : screenWidth , x : screenX } = display . workArea ;
110
-
142
+
111
143
// 设置迷你窗口的大小和位置
112
144
win . unmaximize ( ) ;
113
145
win . setMinimumSize ( DEFAULT_MINI_WIDTH , DEFAULT_MINI_HEIGHT ) ;
@@ -124,7 +156,7 @@ export function initializeWindowManager() {
124
156
125
157
// 发送事件到渲染进程,通知切换到迷你模式
126
158
win . webContents . send ( 'mini-mode' , true ) ;
127
-
159
+
128
160
// 迷你窗口使用默认的缩放比
129
161
win . webContents . setZoomFactor ( 1 ) ;
130
162
}
@@ -137,22 +169,22 @@ export function initializeWindowManager() {
137
169
// 恢复窗口的大小调整功能
138
170
win . setResizable ( true ) ;
139
171
win . setMaximumSize ( 0 , 0 ) ; // 取消最大尺寸限制
140
-
172
+
141
173
console . log ( '从迷你模式恢复,使用保存的状态:' , JSON . stringify ( preMiniModeState ) ) ;
142
-
174
+
143
175
// 设置适当的最小尺寸
144
176
win . setMinimumSize ( Math . max ( DEFAULT_MAIN_WIDTH * 0.5 , 600 ) , Math . max ( DEFAULT_MAIN_HEIGHT * 0.5 , 400 ) ) ;
145
-
177
+
146
178
// 恢复窗口状态
147
179
win . setAlwaysOnTop ( false ) ;
148
180
win . setSkipTaskbar ( false ) ;
149
-
181
+
150
182
// 导航回主页面
151
183
win . webContents . send ( 'navigate' , '/' ) ;
152
-
184
+
153
185
// 发送事件到渲染进程,通知退出迷你模式
154
186
win . webContents . send ( 'mini-mode' , false ) ;
155
-
187
+
156
188
// 应用保存的状态
157
189
setTimeout ( ( ) => {
158
190
// 如果有保存的位置,则应用
@@ -161,25 +193,25 @@ export function initializeWindowManager() {
161
193
} else {
162
194
win . center ( ) ;
163
195
}
164
-
196
+
165
197
// 使用存储的迷你模式前的状态
166
198
if ( preMiniModeState . isMaximized ) {
167
199
win . maximize ( ) ;
168
200
} else {
169
201
// 设置正确的窗口大小
170
202
win . setSize ( preMiniModeState . width , preMiniModeState . height , false ) ;
171
203
}
172
-
204
+
173
205
// 应用页面缩放
174
206
applyContentZoom ( win ) ;
175
-
207
+
176
208
// 确保窗口大小被正确应用
177
209
setTimeout ( ( ) => {
178
210
if ( ! win . isDestroyed ( ) && ! win . isMaximized ( ) && ! win . isMinimized ( ) ) {
179
211
// 再次验证窗口大小
180
212
const [ width , height ] = win . getSize ( ) ;
181
- if ( Math . abs ( width - preMiniModeState . width ) > 2 ||
182
- Math . abs ( height - preMiniModeState . height ) > 2 ) {
213
+ if ( Math . abs ( width - preMiniModeState . width ) > 2 ||
214
+ Math . abs ( height - preMiniModeState . height ) > 2 ) {
183
215
console . log ( `恢复后窗口大小不一致,再次调整: 当前=${ width } x${ height } , 目标=${ preMiniModeState . width } x${ preMiniModeState . height } ` ) ;
184
216
win . setSize ( preMiniModeState . width , preMiniModeState . height , false ) ;
185
217
}
@@ -191,19 +223,9 @@ export function initializeWindowManager() {
191
223
192
224
193
225
ipcMain . on ( 'update-play-state' , ( _ , playing : boolean ) => {
194
- let isPlaying = playing ;
226
+ isPlaying = playing ;
195
227
if ( mainWindowInstance ) {
196
- let mainWindow = mainWindowInstance ;
197
- mainWindow . setThumbarButtons ( [
198
- {
199
- tooltip : isPlaying ? 'pause' : 'play' ,
200
- icon : nativeImage
201
- . createFromPath ( join ( app . getAppPath ( ) , 'resources/icons' , isPlaying ? 'pause.png' : 'play.png' ) ) ,
202
- click ( ) {
203
- mainWindow . webContents . send ( 'global-shortcut' , 'togglePlay' ) ;
204
- } ,
205
- }
206
- ] ) ;
228
+ setThumbarButtons ( mainWindowInstance ) ;
207
229
}
208
230
} ) ;
209
231
@@ -231,10 +253,10 @@ export function initializeWindowManager() {
231
253
*/
232
254
export function createMainWindow ( icon : Electron . NativeImage ) : BrowserWindow {
233
255
console . log ( '开始创建主窗口...' ) ;
234
-
256
+
235
257
// 获取窗口创建选项
236
258
const options = getWindowOptions ( ) ;
237
-
259
+
238
260
// 添加图标和预加载脚本
239
261
options . icon = icon ;
240
262
options . webPreferences = {
@@ -243,7 +265,7 @@ export function createMainWindow(icon: Electron.NativeImage): BrowserWindow {
243
265
contextIsolation : true ,
244
266
webSecurity : false
245
267
} ;
246
-
268
+
247
269
console . log ( `创建窗口,使用选项: ${ JSON . stringify ( {
248
270
width : options . width ,
249
271
height : options . height ,
@@ -252,44 +274,47 @@ export function createMainWindow(icon: Electron.NativeImage): BrowserWindow {
252
274
minWidth : options . minWidth ,
253
275
minHeight : options . minHeight
254
276
} ) } `) ;
255
-
277
+
256
278
// 创建窗口
257
279
const mainWindow = new BrowserWindow ( options ) ;
258
-
280
+
259
281
// 移除菜单
260
282
mainWindow . removeMenu ( ) ;
261
-
283
+
262
284
// 应用初始状态 (例如最大化状态)
263
285
applyInitialState ( mainWindow ) ;
264
-
286
+
265
287
// 更新 preMiniModeState,以便迷你模式可以正确恢复
266
288
const savedState = getWindowState ( ) ;
267
289
if ( savedState ) {
268
290
preMiniModeState = { ...savedState } ;
269
291
}
270
292
293
+ mainWindow . on ( 'show' , ( ) => {
294
+ setThumbarButtons ( mainWindow ) ;
295
+ } ) ;
296
+
271
297
mainWindow . on ( 'ready-to-show' , ( ) => {
272
298
const [ width , height ] = mainWindow . getSize ( ) ;
273
299
console . log ( `窗口显示前的大小: ${ width } x${ height } ` ) ;
274
-
300
+
275
301
// 强制确保窗口使用正确的大小
276
302
if ( savedState && ! savedState . isMaximized ) {
277
303
mainWindow . setSize ( savedState . width , savedState . height , false ) ;
278
304
}
279
-
305
+
280
306
// 显示窗口
281
307
mainWindow . show ( ) ;
282
-
283
308
// 应用页面内容缩放
284
309
applyContentZoom ( mainWindow ) ;
285
-
310
+
286
311
// 再次检查窗口大小是否正确应用
287
312
setTimeout ( ( ) => {
288
313
if ( ! mainWindow . isDestroyed ( ) && ! mainWindow . isMaximized ( ) ) {
289
314
const [ currentWidth , currentHeight ] = mainWindow . getSize ( ) ;
290
315
if ( savedState && ! savedState . isMaximized ) {
291
- if ( Math . abs ( currentWidth - savedState . width ) > 2 ||
292
- Math . abs ( currentHeight - savedState . height ) > 2 ) {
316
+ if ( Math . abs ( currentWidth - savedState . width ) > 2 ||
317
+ Math . abs ( currentHeight - savedState . height ) > 2 ) {
293
318
console . log ( `窗口大小不匹配,再次调整: 当前=${ currentWidth } x${ currentHeight } , 目标=${ savedState . width } x${ savedState . height } ` ) ;
294
319
mainWindow . setSize ( savedState . width , savedState . height , false ) ;
295
320
}
@@ -322,6 +347,6 @@ export function createMainWindow(icon: Electron.NativeImage): BrowserWindow {
322
347
323
348
// 保存主窗口引用
324
349
mainWindowInstance = mainWindow ;
325
-
350
+
326
351
return mainWindow ;
327
352
}
0 commit comments