windowOptions.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import path from 'path';
  2. import { getBaseDir } from 'ee-core/ps';
  3. import { getMainWindow } from 'ee-core/electron';
  4. import type { BrowserWindowConstructorOptions } from 'electron';
  5. /**
  6. * 主窗口、智能工作台窗口、权限管理窗口共用的 webPreferences。
  7. *
  8. * 各窗口必须一致:渲染进程目前依赖 nodeIntegration 直接 require('electron') 调 IPC,
  9. * 二级窗口若单独改成 contextIsolation,现有 preload 又没启用,行为会和主窗口不一致。
  10. */
  11. export const sharedWebPreferences = {
  12. // webSecurity: false,
  13. contextIsolation: false,
  14. nodeIntegration: true,
  15. };
  16. /** 智能工作台窗口的默认尺寸与下限(bounds 落盘恢复时也按这个下限校验) */
  17. export const smartWindowSize = {
  18. width: 1280,
  19. height: 860,
  20. minWidth: 1280,
  21. minHeight: 860,
  22. };
  23. /**
  24. * 智能工作台窗口选项。
  25. *
  26. * 刻意不设 parent:Windows 下带 parent 的子窗口会始终悬浮在主窗口之上,
  27. * 用户无法把主窗口切到前面;VS Code 的 Agents window 也是独立顶层窗口。
  28. */
  29. export function getSmartWindowOptions(): BrowserWindowConstructorOptions {
  30. return {
  31. title: '清鉴 · 智能工作台',
  32. ...smartWindowSize,
  33. webPreferences: { ...sharedWebPreferences },
  34. frame: false,
  35. show: false,
  36. backgroundColor: '#ffffff',
  37. icon: path.join(getBaseDir(), 'public', 'images', 'logo-32.png'),
  38. };
  39. }
  40. /** 权限管理窗口的默认尺寸与下限(主窗口取不到时兜底;实际打开时优先跟随主窗口当前大小) */
  41. export const permWindowSize = {
  42. width: 1280,
  43. height: 860,
  44. minWidth: 1280,
  45. minHeight: 860,
  46. };
  47. /**
  48. * 主窗口当前尺寸。
  49. *
  50. * <p>最小化时用 getNormalBounds 取还原后的尺寸,避免把窗口开成 0 大小;
  51. * 最大化时 getBounds 就是工作区大小,权限窗口也跟着铺满。
  52. */
  53. function resolveMainWindowSize(): { width: number; height: number } | null {
  54. try {
  55. const main = getMainWindow();
  56. if (!main || main.isDestroyed()) return null;
  57. const bounds = main.isMinimized() ? main.getNormalBounds() : main.getBounds();
  58. if (bounds.width > 0 && bounds.height > 0) {
  59. return { width: bounds.width, height: bounds.height };
  60. }
  61. } catch {
  62. // 主窗口尚未创建或已销毁:用兜底尺寸
  63. }
  64. return null;
  65. }
  66. /**
  67. * 权限管理窗口选项。
  68. *
  69. * <p>与智能工作台一致:独立顶层窗口,不设 parent(Windows 下带 parent 会始终悬浮在主窗口之上)。
  70. * 打开时尺寸跟随主窗口当前大小,避免主窗口放大/最大化后权限窗口还是固定小窗。
  71. */
  72. export function getPermWindowOptions(): BrowserWindowConstructorOptions {
  73. const mainSize = resolveMainWindowSize();
  74. const width = mainSize?.width ?? permWindowSize.width;
  75. const height = mainSize?.height ?? permWindowSize.height;
  76. return {
  77. title: '清鉴 · 权限管理',
  78. width,
  79. height,
  80. // 下限不能大于本次尺寸,否则主窗口较小时会被 Electron 钳成下限尺寸
  81. minWidth: Math.min(permWindowSize.minWidth, width),
  82. minHeight: Math.min(permWindowSize.minHeight, height),
  83. webPreferences: { ...sharedWebPreferences },
  84. frame: false,
  85. show: false,
  86. backgroundColor: '#ffffff',
  87. icon: path.join(getBaseDir(), 'public', 'images', 'logo-32.png'),
  88. };
  89. }