Skip to content

Commit 9b3019d

Browse files
committed
fix: 修复mini窗口恢复时导致的应用窗口变小问题
1 parent 1e21338 commit 9b3019d

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/main/modules/window-size.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export interface WindowState {
3131
isMaximized: boolean;
3232
}
3333

34+
35+
3436
/**
3537
* 窗口大小管理器
3638
* 负责保存、恢复和维护窗口大小状态
@@ -337,19 +339,23 @@ class WindowSizeManager {
337339
isMaximized: false
338340
};
339341
}
340-
342+
343+
// 检查是否是mini模式窗口(根据窗口大小判断)
344+
const [currentWidth, currentHeight] = win.getSize();
345+
const isMiniMode = currentWidth === DEFAULT_MINI_WIDTH && currentHeight === DEFAULT_MINI_HEIGHT;
346+
341347
const isMaximized = win.isMaximized();
342348
let state: WindowState;
343-
349+
344350
if (isMaximized) {
345351
// 如果窗口处于最大化状态,保存最大化标志
346352
// 由于 Electron 的限制,最大化状态下 getBounds() 可能不准确
347353
// 所以我们尽量保留之前保存的非最大化时的大小
348354
const currentBounds = win.getBounds();
349-
const previousSize = this.savedState && !this.savedState.isMaximized
355+
const previousSize = this.savedState && !this.savedState.isMaximized
350356
? { width: this.savedState.width, height: this.savedState.height }
351357
: { width: currentBounds.width, height: currentBounds.height };
352-
358+
353359
state = {
354360
width: previousSize.width,
355361
height: previousSize.height,
@@ -359,7 +365,7 @@ class WindowSizeManager {
359365
};
360366
console.log('state IsMaximized',state)
361367

362-
}
368+
}
363369
else if (win.isMinimized()) {
364370
// 最小化状态下不保存窗口大小,因为可能不准确
365371
console.log('state IsMinimized',this.savedState)
@@ -368,12 +374,12 @@ class WindowSizeManager {
368374
height: DEFAULT_MAIN_HEIGHT,
369375
isMaximized: false
370376
};
371-
}
377+
}
372378
else {
373379
// 正常状态下保存当前大小和位置
374380
const [width, height] = win.getSize();
375381
const [x, y] = win.getPosition();
376-
382+
377383
state = {
378384
width,
379385
height,
@@ -383,15 +389,21 @@ class WindowSizeManager {
383389
};
384390
console.log('state IsNormal',state)
385391
}
386-
392+
393+
// 如果是mini模式,不保存到持久化存储,只返回状态用于内存中的恢复
394+
if (isMiniMode) {
395+
console.log('检测到mini模式窗口,不保存到持久化存储');
396+
return state;
397+
}
398+
387399
// 保存状态到存储
388400
this.store.set(WINDOW_STATE_KEY, state);
389401
console.log(`已保存窗口状态: ${JSON.stringify(state)}`);
390-
402+
391403
// 更新内部状态
392404
this.savedState = state;
393405
console.log('state',state)
394-
406+
395407
return state;
396408
}
397409

@@ -400,12 +412,12 @@ class WindowSizeManager {
400412
*/
401413
getWindowState(): WindowState | null {
402414
const state = this.store.get(WINDOW_STATE_KEY) as WindowState | undefined;
403-
415+
404416
if (!state) {
405417
console.log('未找到保存的窗口状态,将使用默认值');
406418
return null;
407419
}
408-
420+
409421
// 验证尺寸,确保不小于最小值
410422
const validatedState: WindowState = {
411423
width: Math.max(MIN_WIDTH, state.width || DEFAULT_MAIN_WIDTH),
@@ -414,11 +426,13 @@ class WindowSizeManager {
414426
y: state.y,
415427
isMaximized: !!state.isMaximized
416428
};
417-
429+
418430
console.log(`读取保存的窗口状态: ${JSON.stringify(validatedState)}`);
419-
431+
420432
return validatedState;
421433
}
434+
435+
422436

423437
/**
424438
* 检查位置是否在可见屏幕范围内
@@ -697,4 +711,5 @@ export const initWindowSizeHandlers = (mainWindow: BrowserWindow | null): void =
697711

698712
export const calculateMinimumWindowSize = (): { minWidth: number; minHeight: number } => {
699713
return { minWidth: MIN_WIDTH, minHeight: MIN_HEIGHT };
700-
};
714+
};
715+

0 commit comments

Comments
 (0)