Skip to content

Commit 58ab990

Browse files
committed
启动默认显示缩略图控制按钮。
(cherry picked from commit 1f438e3)
1 parent 9bec67e commit 58ab990

File tree

1 file changed

+65
-40
lines changed

1 file changed

+65
-40
lines changed

src/main/modules/window.ts

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const store = new Store();
2020

2121
// 保存主窗口引用,以便在 activate 事件中使用
2222
let mainWindowInstance: BrowserWindow | null = null;
23-
23+
let isPlaying = false;
2424
// 保存迷你模式前的窗口状态
2525
let preMiniModeState: WindowState = {
2626
width: DEFAULT_MAIN_WIDTH,
@@ -56,6 +56,38 @@ function initializeProxy() {
5656
}
5757
}
5858

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+
5991
/**
6092
* 初始化窗口管理相关的IPC监听
6193
*/
@@ -107,7 +139,7 @@ export function initializeWindowManager() {
107139
// 获取屏幕工作区尺寸
108140
const display = screen.getDisplayMatching(win.getBounds());
109141
const { width: screenWidth, x: screenX } = display.workArea;
110-
142+
111143
// 设置迷你窗口的大小和位置
112144
win.unmaximize();
113145
win.setMinimumSize(DEFAULT_MINI_WIDTH, DEFAULT_MINI_HEIGHT);
@@ -124,7 +156,7 @@ export function initializeWindowManager() {
124156

125157
// 发送事件到渲染进程,通知切换到迷你模式
126158
win.webContents.send('mini-mode', true);
127-
159+
128160
// 迷你窗口使用默认的缩放比
129161
win.webContents.setZoomFactor(1);
130162
}
@@ -137,22 +169,22 @@ export function initializeWindowManager() {
137169
// 恢复窗口的大小调整功能
138170
win.setResizable(true);
139171
win.setMaximumSize(0, 0); // 取消最大尺寸限制
140-
172+
141173
console.log('从迷你模式恢复,使用保存的状态:', JSON.stringify(preMiniModeState));
142-
174+
143175
// 设置适当的最小尺寸
144176
win.setMinimumSize(Math.max(DEFAULT_MAIN_WIDTH * 0.5, 600), Math.max(DEFAULT_MAIN_HEIGHT * 0.5, 400));
145-
177+
146178
// 恢复窗口状态
147179
win.setAlwaysOnTop(false);
148180
win.setSkipTaskbar(false);
149-
181+
150182
// 导航回主页面
151183
win.webContents.send('navigate', '/');
152-
184+
153185
// 发送事件到渲染进程,通知退出迷你模式
154186
win.webContents.send('mini-mode', false);
155-
187+
156188
// 应用保存的状态
157189
setTimeout(() => {
158190
// 如果有保存的位置,则应用
@@ -161,25 +193,25 @@ export function initializeWindowManager() {
161193
} else {
162194
win.center();
163195
}
164-
196+
165197
// 使用存储的迷你模式前的状态
166198
if (preMiniModeState.isMaximized) {
167199
win.maximize();
168200
} else {
169201
// 设置正确的窗口大小
170202
win.setSize(preMiniModeState.width, preMiniModeState.height, false);
171203
}
172-
204+
173205
// 应用页面缩放
174206
applyContentZoom(win);
175-
207+
176208
// 确保窗口大小被正确应用
177209
setTimeout(() => {
178210
if (!win.isDestroyed() && !win.isMaximized() && !win.isMinimized()) {
179211
// 再次验证窗口大小
180212
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) {
183215
console.log(`恢复后窗口大小不一致,再次调整: 当前=${width}x${height}, 目标=${preMiniModeState.width}x${preMiniModeState.height}`);
184216
win.setSize(preMiniModeState.width, preMiniModeState.height, false);
185217
}
@@ -191,19 +223,9 @@ export function initializeWindowManager() {
191223

192224

193225
ipcMain.on('update-play-state', (_, playing: boolean) => {
194-
let isPlaying = playing;
226+
isPlaying = playing;
195227
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);
207229
}
208230
});
209231

@@ -231,10 +253,10 @@ export function initializeWindowManager() {
231253
*/
232254
export function createMainWindow(icon: Electron.NativeImage): BrowserWindow {
233255
console.log('开始创建主窗口...');
234-
256+
235257
// 获取窗口创建选项
236258
const options = getWindowOptions();
237-
259+
238260
// 添加图标和预加载脚本
239261
options.icon = icon;
240262
options.webPreferences = {
@@ -243,7 +265,7 @@ export function createMainWindow(icon: Electron.NativeImage): BrowserWindow {
243265
contextIsolation: true,
244266
webSecurity: false
245267
};
246-
268+
247269
console.log(`创建窗口,使用选项: ${JSON.stringify({
248270
width: options.width,
249271
height: options.height,
@@ -252,44 +274,47 @@ export function createMainWindow(icon: Electron.NativeImage): BrowserWindow {
252274
minWidth: options.minWidth,
253275
minHeight: options.minHeight
254276
})}`);
255-
277+
256278
// 创建窗口
257279
const mainWindow = new BrowserWindow(options);
258-
280+
259281
// 移除菜单
260282
mainWindow.removeMenu();
261-
283+
262284
// 应用初始状态 (例如最大化状态)
263285
applyInitialState(mainWindow);
264-
286+
265287
// 更新 preMiniModeState,以便迷你模式可以正确恢复
266288
const savedState = getWindowState();
267289
if (savedState) {
268290
preMiniModeState = { ...savedState };
269291
}
270292

293+
mainWindow.on('show', () => {
294+
setThumbarButtons(mainWindow);
295+
});
296+
271297
mainWindow.on('ready-to-show', () => {
272298
const [width, height] = mainWindow.getSize();
273299
console.log(`窗口显示前的大小: ${width}x${height}`);
274-
300+
275301
// 强制确保窗口使用正确的大小
276302
if (savedState && !savedState.isMaximized) {
277303
mainWindow.setSize(savedState.width, savedState.height, false);
278304
}
279-
305+
280306
// 显示窗口
281307
mainWindow.show();
282-
283308
// 应用页面内容缩放
284309
applyContentZoom(mainWindow);
285-
310+
286311
// 再次检查窗口大小是否正确应用
287312
setTimeout(() => {
288313
if (!mainWindow.isDestroyed() && !mainWindow.isMaximized()) {
289314
const [currentWidth, currentHeight] = mainWindow.getSize();
290315
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) {
293318
console.log(`窗口大小不匹配,再次调整: 当前=${currentWidth}x${currentHeight}, 目标=${savedState.width}x${savedState.height}`);
294319
mainWindow.setSize(savedState.width, savedState.height, false);
295320
}
@@ -322,6 +347,6 @@ export function createMainWindow(icon: Electron.NativeImage): BrowserWindow {
322347

323348
// 保存主窗口引用
324349
mainWindowInstance = mainWindow;
325-
350+
326351
return mainWindow;
327352
}

0 commit comments

Comments
 (0)