diff --git a/src/layout/index.vue b/src/layout/index.vue index 1c2190e0..2b8580a9 100644 --- a/src/layout/index.vue +++ b/src/layout/index.vue @@ -287,9 +287,28 @@ onMounted(async () => { }) } - // 恢复大小 + // 恢复大小(验证尺寸有效性) if (globalStore.homeWindowState.width && globalStore.homeWindowState.height) { - await homeWindow.setSize(new LogicalSize(globalStore.homeWindowState.width, globalStore.homeWindowState.height)) + const MIN_WIDTH = 330 + const MIN_HEIGHT = 480 + const DEFAULT_WIDTH = 960 + const DEFAULT_HEIGHT = 720 + + let targetWidth = globalStore.homeWindowState.width + let targetHeight = globalStore.homeWindowState.height + + // 如果窗口大小异常,使用默认值 + if (targetWidth < MIN_WIDTH || targetHeight < MIN_HEIGHT) { + info( + `[layout] 检测到窗口大小异常 (${targetWidth}x${targetHeight}),使用默认值 (${DEFAULT_WIDTH}x${DEFAULT_HEIGHT})` + ) + targetWidth = DEFAULT_WIDTH + targetHeight = DEFAULT_HEIGHT + // 同步更新到 store + globalStore.setHomeWindowState({ width: targetWidth, height: targetHeight }) + } + + await homeWindow.setSize(new LogicalSize(targetWidth, targetHeight)) } // 居中 await homeWindow.center() diff --git a/src/stores/global.ts b/src/stores/global.ts index ee93b496..10d94ee2 100644 --- a/src/stores/global.ts +++ b/src/stores/global.ts @@ -82,7 +82,23 @@ export const useGlobalStore = defineStore( // 设置home窗口位置、宽高信息 const setHomeWindowState = (newState: { width: number; height: number }) => { - homeWindowState.value = { ...homeWindowState.value, ...newState } + // 验证窗口大小,如果小于最小限制,则使用默认值 + const MIN_WIDTH = 330 + const MIN_HEIGHT = 480 + const DEFAULT_WIDTH = 960 + const DEFAULT_HEIGHT = 720 + + if (newState.width < MIN_WIDTH || newState.height < MIN_HEIGHT) { + info( + `[global] 窗口大小异常 (${newState.width}x${newState.height}),强制设置为默认值 (${DEFAULT_WIDTH}x${DEFAULT_HEIGHT})` + ) + homeWindowState.value = { + width: DEFAULT_WIDTH, + height: DEFAULT_HEIGHT + } + } else { + homeWindowState.value = { ...homeWindowState.value, ...newState } + } } // 更新全局未读消息计数