diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts index 1a4b8291..c9833370 100644 --- a/apps/desktop/src/main/index.ts +++ b/apps/desktop/src/main/index.ts @@ -2,6 +2,13 @@ import { app, shell, BrowserWindow, ipcMain, nativeTheme, Tray, Menu } from 'ele import { join } from 'path' import { electronApp, optimizer, is } from '@electron-toolkit/utils' import { getConfig, registerConfigIpc } from './services/config' +import { + clampToWorkArea, + flushWindowState, + loadWindowState, + trackWindowState, + windowBoundsOptions +} from './services/window-state' import { getAppIcon, getTrayIcon } from './services/tray-icon' import { registerWorkspaceIpc } from './services/workspace' import { registerSecretsIpc } from './services/secrets' @@ -99,9 +106,15 @@ function overlayOptions( } function createWindow(): void { + // 上次关窗时的尺寸 / 位置(含是否最大化);不可信时返回 null,回落默认 1280×820。 + const saved = loadWindowState() + const bounds = windowBoundsOptions(saved) mainWindow = new BrowserWindow({ - width: 1280, - height: 820, + // x / y 为 undefined 时 Electron 按平台默认居中,等同原来的行为。 + x: bounds.x, + y: bounds.y, + width: bounds.width, + height: bounds.height, minWidth: 940, minHeight: 600, show: false, @@ -141,6 +154,14 @@ function createWindow(): void { } win.once('ready-to-show', () => revealOnce('ready-to-show')) win.webContents.once('did-finish-load', () => revealOnce('did-finish-load')) + // 上次是以最大化关的,还原成最大化。 + if (bounds.maximized) win.maximize() + // 夹回工作区必须无条件做,不能只在最大化分支里: + // loadWindowState 的校验是「与某块屏幕有交集」,只要有 1 像素可见就算通过。 + // 拔掉外接屏后窗口可能整块落在屏幕外、只剩一条边在内,不夹取就表现为「窗口几乎看不见」。 + // 放在 maximize 之后:最大化窗口背后的普通尺寸也要落在工作区内,否则取消最大化会跳出去。 + clampToWorkArea(win) + trackWindowState(win) win.webContents.on('did-fail-load', (_e, code, desc, url) => { console.error(`[window] 页面加载失败 ${code} ${desc} ${url}`) revealOnce('did-fail-load') @@ -424,6 +445,8 @@ let turnsSettled = false let settling = false app.on('before-quit', (e) => { isQuitting = true + // 关窗驻留托盘时 close 里已经存过;这里兜托盘「退出」与系统关机这两条不经过 close 的路由。 + flushWindowState(mainWindow) if (!turnsSettled && hasActiveTurns()) { e.preventDefault() if (settling) return diff --git a/apps/desktop/src/main/services/window-state.ts b/apps/desktop/src/main/services/window-state.ts new file mode 100644 index 00000000..56771f6f --- /dev/null +++ b/apps/desktop/src/main/services/window-state.ts @@ -0,0 +1,184 @@ +import { BrowserWindow, screen } from 'electron' +import { getConfig, setConfig } from './config' + +/** + * 主窗口尺寸与位置记忆。 + * + * 为什么放在主进程而不是渲染层:窗口是主进程的对象,渲染层拿不到 getBounds(); + * 而 `layout` 段(侧栏宽度、输入框高度)已经被 redusable.tsx 占用,窗口状态另起 `windowState` 键。 + * + * 只在「普通状态」下记:最大化 / 全屏 / 最小化时 getBounds() 给的是铺满屏幕的尺寸, + * 记下来会让下次以「占满屏幕的普通窗口」打开,观感不对。故用 getNormalBounds(), + * 并把「当时是否最大化」单独记一个布尔,启动时一并还原。 + */ +export interface WindowState { + x?: number + y?: number + width: number + height: number + maximized?: boolean +} + +/** 与 createWindow 里的 minWidth / minHeight 保持一致:记下的小于此值的尺寸一律不采信。 */ +const MIN_WIDTH = 940 +const MIN_HEIGHT = 600 +/** 拖动窗口时 resize / move 每秒触发几十次,而 setConfig 是全量写盘,必须压到低频。 */ +const SAVE_DELAY = 400 + +function asInt(v: unknown): number | undefined { + return typeof v === 'number' && Number.isFinite(v) ? Math.round(v) : undefined +} + +/** + * 读上次保存的窗口状态;数据不可信时返回 null(调用方回落默认尺寸)。 + * + * 「可信」两条:尺寸不小于最小尺寸;位置必须落在某块屏幕的工作区内。 + * 第二条是关键——外接屏拔掉后,保存的坐标会落在已不存在的显示器上, + * 不校验的话下次启动窗口就在屏幕外,表现为「启动后看不见窗口」。 + */ +export function loadWindowState(): WindowState | null { + const raw = getConfig().windowState + if (!raw || typeof raw !== 'object') return null + const s = raw as Record + const width = asInt(s.width) + const height = asInt(s.height) + if (width === undefined || height === undefined) return null + if (width < MIN_WIDTH || height < MIN_HEIGHT) return null + + const state: WindowState = { width, height, maximized: s.maximized === true } + const x = asInt(s.x) + const y = asInt(s.y) + if (x === undefined || y === undefined) return state + + // 与任一块屏幕的工作区有交集才采用坐标(照 browser/host.ts 的 firstBounds 思路)。 + const visible = screen.getAllDisplays().some((d) => { + const a = d.workArea + return x < a.x + a.width && x + width > a.x && y < a.y + a.height && y + height > a.y + }) + if (visible) { + state.x = x + state.y = y + } + return state +} + +/** 把窗口夹回它所在屏幕的工作区,防止出现「一部分在屏幕外」的窗口(比整块不可见更常见)。 */ +export function clampToWorkArea(win: BrowserWindow): void { + if (win.isDestroyed()) return + const bounds = win.getNormalBounds() + const area = screen.getDisplayMatching(bounds).workArea + const width = Math.min(bounds.width, area.width) + const height = Math.min(bounds.height, area.height) + const x = Math.min(Math.max(bounds.x, area.x), area.x + area.width - width) + const y = Math.min(Math.max(bounds.y, area.y), area.y + area.height - height) + if (x !== bounds.x || y !== bounds.y || width !== bounds.width || height !== bounds.height) { + win.setBounds({ x, y, width, height }) + } +} + +/** + * 挂上自动保存。在 createWindow 里建好窗口后调用一次。 + * + * 落盘时机有三个,缺一不可: + * - resize / move:正常拖动(防抖); + * - close:托盘驻留时窗口只是 hide(),这是「用户以为关掉了」的那一次; + * - before-quit 时由调用方调 flushWindowState:托盘菜单退出、系统关机走这条,拿不到 close。 + * + * 另需注意 win32 默认 closeToTray:close 事件被 preventDefault 后窗口没销毁, + * 所以不能只依赖它,这也是必须同时在 resize / move 上记的原因。 + */ +export function trackWindowState(win: BrowserWindow): void { + let timer: NodeJS.Timeout | null = null + + const save = (): void => { + if (win.isDestroyed()) return + // 最大化 / 全屏期间 getNormalBounds() 仍返回普通状态尺寸,可以照记; + // 最小化时同样记普通尺寸(用户只是收起,不代表想要小窗口)。 + const b = win.getNormalBounds() + const next: WindowState = { + x: b.x, + y: b.y, + width: b.width, + height: b.height, + maximized: win.isMaximized() + } + const prev = getConfig().windowState as WindowState | undefined + // 完全相同就不写盘:拖动只在真正停下后落一次,避免无意义的全量写。 + if (isSameState(prev, next)) return + setConfig({ windowState: next }) + } + + const schedule = (): void => { + if (timer) clearTimeout(timer) + timer = setTimeout(() => { + timer = null + save() + }, SAVE_DELAY) + } + + win.on('resize', schedule) + win.on('move', schedule) + win.on('maximize', schedule) + win.on('unmaximize', schedule) + win.on('close', () => { + // 关窗前立刻落一次,不等防抖(此后窗口可能只是 hide,不再有事件)。 + if (timer) { + clearTimeout(timer) + timer = null + } + save() + }) + win.on('closed', () => { + if (timer) { + clearTimeout(timer) + timer = null + } + }) +} + +/** 退出前补一次(托盘菜单退出、系统关机不经过 close 的路由)。 */ +export function flushWindowState(win: BrowserWindow | null): void { + if (!win || win.isDestroyed()) return + const b = win.getNormalBounds() + const next: WindowState = { + x: b.x, + y: b.y, + width: b.width, + height: b.height, + maximized: win.isMaximized() + } + // 与 isUnchanged 同一判断:退出时若状态没变过,不必再全量重写 config.json + // (setConfig 每次 writeFileAtomic 写整份配置)。 + if (isSameState(getConfig().windowState as WindowState | undefined, next)) return + setConfig({ windowState: next }) +} + +/** 两个窗口状态是否完全一致(用于避免无意义的写盘)。 */ +function isSameState(prev: WindowState | undefined, next: WindowState): boolean { + return ( + !!prev && + prev.x === next.x && + prev.y === next.y && + prev.width === next.width && + prev.height === next.height && + (prev.maximized === true) === next.maximized + ) +} + +/** 把保存的状态展开成 BrowserWindow 构造参数里能用的那几项。 */ +export function windowBoundsOptions(state: WindowState | null): { + x?: number + y?: number + width: number + height: number + maximized: boolean +} { + if (!state) return { width: 1280, height: 820, maximized: false } + return { + x: state.x, + y: state.y, + width: state.width, + height: state.height, + maximized: state.maximized === true + } +}