windowCtl.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { BrowserWindow } from 'electron';
  2. import type { WebContents } from 'electron';
  3. import { logger } from 'ee-core/log';
  4. import {
  5. closeSmartWindow,
  6. focusMainWindow,
  7. isSmartWindow,
  8. isSmartWindowOpen,
  9. openSmartWindow,
  10. } from '../service/window/smartWindow';
  11. import {
  12. closePermWindow,
  13. isPermWindow,
  14. isPermWindowOpen,
  15. openPermWindow,
  16. } from '../service/window/permWindow';
  17. /** 主进程 → 渲染进程的跨窗口状态事件通道 */
  18. export const SYNC_EVENT_CHANNEL = 'app:state';
  19. interface IpcEventLike {
  20. sender?: WebContents;
  21. }
  22. interface OpenSmartWindowParams {
  23. fullPath?: string;
  24. reloadIfOpen?: boolean;
  25. navigateIfOpen?: boolean;
  26. }
  27. type OpenPermWindowParams = OpenSmartWindowParams;
  28. interface SyncStateParams {
  29. event?: {
  30. type?: string;
  31. payload?: Record<string, unknown>;
  32. };
  33. }
  34. function toMessage(error: unknown): string {
  35. return error instanceof Error ? error.message : String(error);
  36. }
  37. /**
  38. * 窗口管理 IPC:通道名 controller/windowCtl/<方法>(ee-core 按文件名自动注册)。
  39. *
  40. * 统一返回 { success, data|message },失败不抛到 Electron 层。
  41. */
  42. class WindowCtl {
  43. /** 打开/聚焦智能工作台窗口;params { fullPath?, reloadIfOpen? } */
  44. async openSmartWindow(params: OpenSmartWindowParams = {}) {
  45. try {
  46. openSmartWindow({
  47. fullPath: params?.fullPath,
  48. reloadIfOpen: params?.reloadIfOpen === true,
  49. navigateIfOpen: params?.navigateIfOpen === true,
  50. });
  51. return { success: true, data: { open: true } };
  52. } catch (e) {
  53. const message = toMessage(e);
  54. logger.error('[windowCtl] openSmartWindow failed:', message);
  55. return { success: false, message };
  56. }
  57. }
  58. /** 关闭智能工作台窗口(已关闭时是 no-op) */
  59. async closeSmartWindow() {
  60. try {
  61. closeSmartWindow();
  62. return { success: true };
  63. } catch (e) {
  64. const message = toMessage(e);
  65. logger.error('[windowCtl] closeSmartWindow failed:', message);
  66. return { success: false, message };
  67. }
  68. }
  69. async isSmartWindowOpen() {
  70. return { success: true, data: { open: isSmartWindowOpen() } };
  71. }
  72. /** 打开/聚焦权限管理窗口;params { fullPath?, reloadIfOpen?, navigateIfOpen? } */
  73. async openPermWindow(params: OpenPermWindowParams = {}) {
  74. try {
  75. openPermWindow({
  76. fullPath: params?.fullPath,
  77. reloadIfOpen: params?.reloadIfOpen === true,
  78. navigateIfOpen: params?.navigateIfOpen === true,
  79. });
  80. return { success: true, data: { open: true } };
  81. } catch (e) {
  82. const message = toMessage(e);
  83. logger.error('[windowCtl] openPermWindow failed:', message);
  84. return { success: false, message };
  85. }
  86. }
  87. /** 关闭权限管理窗口(已关闭时是 no-op) */
  88. async closePermWindow() {
  89. try {
  90. closePermWindow();
  91. return { success: true };
  92. } catch (e) {
  93. const message = toMessage(e);
  94. logger.error('[windowCtl] closePermWindow failed:', message);
  95. return { success: false, message };
  96. }
  97. }
  98. async isPermWindowOpen() {
  99. return { success: true, data: { open: isPermWindowOpen() } };
  100. }
  101. /** 二级窗口遇到登录失效时,把主窗口拉到前台负责提示 */
  102. async focusMainWindow() {
  103. try {
  104. focusMainWindow();
  105. return { success: true };
  106. } catch (e) {
  107. const message = toMessage(e);
  108. logger.error('[windowCtl] focusMainWindow failed:', message);
  109. return { success: false, message };
  110. }
  111. }
  112. /**
  113. * 渲染进程启动时问一次自己的角色。
  114. *
  115. * <p>ee-core 的 IPC 注册是 fn.call(controller, params, event),
  116. * event.sender 就是发起调用的 webContents。
  117. */
  118. async getWindowRole(_params: unknown, event?: IpcEventLike) {
  119. const win = event?.sender ? BrowserWindow.fromWebContents(event.sender) : null;
  120. const role = isSmartWindow(win) ? 'smart' : isPermWindow(win) ? 'perm' : 'main';
  121. return { success: true, data: { role } };
  122. }
  123. /**
  124. * 跨窗口状态同步:把语义事件发给除发送者外的所有窗口。
  125. *
  126. * <p>只做转发,不解释业务;接收方负责按事件类型重读事实源或关闭自己。
  127. */
  128. async syncState(params: SyncStateParams, event?: IpcEventLike) {
  129. const payload = params?.event;
  130. if (!payload?.type) {
  131. return { success: false, message: '同步事件缺少 type' };
  132. }
  133. const senderId = event?.sender?.id;
  134. for (const win of BrowserWindow.getAllWindows()) {
  135. if (win.isDestroyed() || win.webContents.isDestroyed()) continue;
  136. if (senderId && win.webContents.id === senderId) continue;
  137. try {
  138. win.webContents.send(SYNC_EVENT_CHANNEL, payload);
  139. } catch (e) {
  140. logger.warn('[windowCtl] syncState send failed:', toMessage(e));
  141. }
  142. }
  143. return { success: true };
  144. }
  145. }
  146. WindowCtl.toString = () => 'WindowCtl';
  147. export default WindowCtl;