cc 6 дней назад
Родитель
Сommit
9e59459d77

+ 31 - 2
ai-electron/electron/config/windowOptions.ts

@@ -1,5 +1,6 @@
 import path from 'path';
 import { getBaseDir } from 'ee-core/ps';
+import { getMainWindow } from 'ee-core/electron';
 import type { BrowserWindowConstructorOptions } from 'electron';
 
 /**
@@ -40,7 +41,7 @@ export function getSmartWindowOptions(): BrowserWindowConstructorOptions {
   };
 }
 
-/** 权限管理窗口的默认尺寸与下限(与智能工作台同规格,管理表格需要足够宽度) */
+/** 权限管理窗口的默认尺寸与下限(主窗口取不到时兜底;实际打开时优先跟随主窗口当前大小) */
 export const permWindowSize = {
   width: 1280,
   height: 860,
@@ -48,15 +49,43 @@ export const permWindowSize = {
   minHeight: 860,
 };
 
+/**
+ * 主窗口当前尺寸。
+ *
+ * <p>最小化时用 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;
+}
+
 /**
  * 权限管理窗口选项。
  *
  * <p>与智能工作台一致:独立顶层窗口,不设 parent(Windows 下带 parent 会始终悬浮在主窗口之上)。
+ * 打开时尺寸跟随主窗口当前大小,避免主窗口放大/最大化后权限窗口还是固定小窗。
  */
 export function getPermWindowOptions(): BrowserWindowConstructorOptions {
+  const mainSize = resolveMainWindowSize();
+  const width = mainSize?.width ?? permWindowSize.width;
+  const height = mainSize?.height ?? permWindowSize.height;
   return {
     title: '清鉴 · 权限管理',
-    ...permWindowSize,
+    width,
+    height,
+    // 下限不能大于本次尺寸,否则主窗口较小时会被 Electron 钳成下限尺寸
+    minWidth: Math.min(permWindowSize.minWidth, width),
+    minHeight: Math.min(permWindowSize.minHeight, height),
     webPreferences: { ...sharedWebPreferences },
     frame: false,
     show: false,

+ 2 - 0
ai-electron/electron/service/window/permWindow.ts

@@ -15,6 +15,8 @@ const permWindowController = createSubWindowController({
   createWindowOptions: getPermWindowOptions,
   // 五个页面是不同路径段(/perm-window/user 等),点击入口时按目标地址导航
   navigateOnlyWhenQuery: false,
+  // 尺寸跟随主窗口当前大小(见 getPermWindowOptions),这里只沿用上次的位置
+  restoreBoundsMode: 'position',
 });
 
 export function getPermWindow(): BrowserWindow | null {

+ 43 - 1
ai-electron/electron/service/window/subWindow.ts

@@ -33,6 +33,16 @@ export interface SubWindowDefinition {
    * 只有 ?newChat=1 / ?view=agent 这类明确意图才跳转。
    */
   navigateOnlyWhenQuery?: boolean;
+  /**
+   * 已保存 bounds 的使用方式。
+   *
+   * <ul>
+   *   <li>all(默认):位置 + 尺寸</li>
+   *   <li>position:只用上次的位置,尺寸由 createWindowOptions 决定(权限窗口跟随主窗口大小)</li>
+   *   <li>none:不恢复</li>
+   * </ul>
+   */
+  restoreBoundsMode?: 'all' | 'position' | 'none';
 }
 
 export interface SubWindowController {
@@ -111,6 +121,7 @@ export function createSubWindowController(definition: SubWindowDefinition): SubW
       ...restoreBounds(),
     });
     win = created;
+    clampToVisibleArea(created);
 
     created.once('ready-to-show', () => {
       if (!created.isDestroyed()) {
@@ -200,13 +211,44 @@ export function createSubWindowController(definition: SubWindowDefinition): SubW
         );
       });
 
-      return visible ? { x, y, width, height } : {};
+      return visible ? buildRestoredBounds({ x, y, width, height }) : {};
     } catch (e) {
       logger.warn(`${logPrefix} 恢复窗口位置失败:`, e instanceof Error ? e.message : String(e));
       return {};
     }
   }
 
+  /** 按配置决定恢复位置和/或尺寸 */
+  function buildRestoredBounds(saved: Partial<SavedBounds>): Partial<SavedBounds> {
+    switch (definition.restoreBoundsMode) {
+      case 'none':
+        return {};
+      case 'position':
+        // 尺寸交给 createWindowOptions(例如权限窗口跟随主窗口大小),这里只保留位置
+        return { x: saved.x, y: saved.y };
+      default:
+        return saved;
+    }
+  }
+
+  /** 位置可能配不上新的窗口尺寸(例如跟随最大化的主窗口):保证窗口完整落在最近显示器的工作区内 */
+  function clampToVisibleArea(target: BrowserWindow): void {
+    try {
+      const bounds = target.getBounds();
+      const display = screen.getDisplayMatching(bounds);
+      const area = display.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), Math.max(area.x, area.x + area.width - width));
+      const y = Math.min(Math.max(bounds.y, area.y), Math.max(area.y, area.y + area.height - height));
+      if (x !== bounds.x || y !== bounds.y || width !== bounds.width || height !== bounds.height) {
+        target.setBounds({ x, y, width, height });
+      }
+    } catch (e) {
+      logger.warn(`${logPrefix} 校正窗口位置失败:`, e instanceof Error ? e.message : String(e));
+    }
+  }
+
   function notifyState(open: boolean): void {
     const channel = definition.stateChannel;
     if (!channel) return;