import path from 'path'; import { getBaseDir } from 'ee-core/ps'; import { getMainWindow } from 'ee-core/electron'; import type { BrowserWindowConstructorOptions } from 'electron'; /** * 主窗口、智能工作台窗口、权限管理窗口共用的 webPreferences。 * * 各窗口必须一致:渲染进程目前依赖 nodeIntegration 直接 require('electron') 调 IPC, * 二级窗口若单独改成 contextIsolation,现有 preload 又没启用,行为会和主窗口不一致。 */ export const sharedWebPreferences = { // webSecurity: false, contextIsolation: false, nodeIntegration: true, }; /** 智能工作台窗口的默认尺寸与下限(bounds 落盘恢复时也按这个下限校验) */ export const smartWindowSize = { width: 1280, height: 860, minWidth: 1280, minHeight: 860, }; /** * 智能工作台窗口选项。 * * 刻意不设 parent:Windows 下带 parent 的子窗口会始终悬浮在主窗口之上, * 用户无法把主窗口切到前面;VS Code 的 Agents window 也是独立顶层窗口。 */ export function getSmartWindowOptions(): BrowserWindowConstructorOptions { return { title: '清鉴 · 智能工作台', ...smartWindowSize, webPreferences: { ...sharedWebPreferences }, frame: false, show: false, backgroundColor: '#ffffff', icon: path.join(getBaseDir(), 'public', 'images', 'logo-32.png'), }; } /** 权限管理窗口的默认尺寸与下限(主窗口取不到时兜底;实际打开时优先跟随主窗口当前大小) */ export const permWindowSize = { width: 1280, height: 860, minWidth: 1280, minHeight: 860, }; /** * 主窗口当前尺寸。 * *
最小化时用 getNormalBounds 取还原后的尺寸,避免把窗口开成 0 大小; * 最大化时 getBounds 就是工作区大小,权限窗口也跟着铺满。 */ function resolveMainWindowSize(): { width: number; height: number } | null { try { const main = getMainWindow(); if (!main || main.isDestroyed()) return null; const bounds = main.isMinimized() ? main.getNormalBounds() : main.getBounds(); if (bounds.width > 0 && bounds.height > 0) { return { width: bounds.width, height: bounds.height }; } } catch { // 主窗口尚未创建或已销毁:用兜底尺寸 } return null; } /** * 权限管理窗口选项。 * *
与智能工作台一致:独立顶层窗口,不设 parent(Windows 下带 parent 会始终悬浮在主窗口之上)。 * 打开时尺寸跟随主窗口当前大小,避免主窗口放大/最大化后权限窗口还是固定小窗。 */ export function getPermWindowOptions(): BrowserWindowConstructorOptions { const mainSize = resolveMainWindowSize(); const width = mainSize?.width ?? permWindowSize.width; const height = mainSize?.height ?? permWindowSize.height; return { title: '清鉴 · 权限管理', width, height, // 下限不能大于本次尺寸,否则主窗口较小时会被 Electron 钳成下限尺寸 minWidth: Math.min(permWindowSize.minWidth, width), minHeight: Math.min(permWindowSize.minHeight, height), webPreferences: { ...sharedWebPreferences }, frame: false, show: false, backgroundColor: '#ffffff', icon: path.join(getBaseDir(), 'public', 'images', 'logo-32.png'), }; }