smartWindow.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { BrowserWindow } from 'electron';
  2. import { logger } from 'ee-core/log';
  3. import { getMainWindow } from 'ee-core/electron';
  4. import { getSmartWindowOptions, smartWindowSize } from '../../config/windowOptions';
  5. import { createSubWindowController, focusWindow, type SubWindowOpenOptions } from './subWindow';
  6. /** 智能窗口只承载工作台,非该路径一律改写 */
  7. const WORKBENCH_PATH = '/workbench';
  8. /** 主进程 → 主窗口的窗口开关状态通道 */
  9. export const SMART_WINDOW_STATE_CHANNEL = 'smart-window:state';
  10. export type OpenSmartWindowOptions = SubWindowOpenOptions;
  11. /** 单例:应用内最多一个智能工作台窗口 */
  12. const smartWindowController = createSubWindowController({
  13. pathPrefix: WORKBENCH_PATH,
  14. boundsKey: 'smartWindowBounds',
  15. minSize: smartWindowSize,
  16. createWindowOptions: getSmartWindowOptions,
  17. stateChannel: SMART_WINDOW_STATE_CHANNEL,
  18. // 工作台普通打开只聚焦;?newChat=1 / ?view=agent 这类明确意图才导航
  19. navigateOnlyWhenQuery: true,
  20. });
  21. export function getSmartWindow(): BrowserWindow | null {
  22. return smartWindowController.getWindow();
  23. }
  24. export function isSmartWindowOpen(): boolean {
  25. return smartWindowController.isOpen();
  26. }
  27. /** 判断某个窗口是不是智能工作台窗口(IPC 解析窗口角色用) */
  28. export function isSmartWindow(win: BrowserWindow | null | undefined): boolean {
  29. return smartWindowController.matches(win);
  30. }
  31. /**
  32. * 打开/聚焦智能工作台窗口。
  33. *
  34. * 已存在时只做 restore/show/focus(reloadIfOpen 时再 reload),保证单例。
  35. */
  36. export function openSmartWindow(options: OpenSmartWindowOptions = {}): BrowserWindow {
  37. return smartWindowController.open(options);
  38. }
  39. export function closeSmartWindow(): void {
  40. smartWindowController.close();
  41. }
  42. /** 把已打开的智能窗口拉到前台;没打开返回 false */
  43. export function focusSmartWindow(): boolean {
  44. return smartWindowController.focus();
  45. }
  46. /** 把主窗口拉到前台(智能窗口遇到登录/授权失效时,由主窗口负责提示) */
  47. export function focusMainWindow(): void {
  48. try {
  49. focusWindow(getMainWindow());
  50. } catch (e) {
  51. logger.warn('[smartWindow] focusMainWindow failed:', e instanceof Error ? e.message : String(e));
  52. }
  53. }