|
@@ -0,0 +1,56 @@
|
|
|
|
|
+import { eventTarget } from '@cornerstonejs/core';
|
|
|
|
|
+import { Enums } from '@cornerstonejs/tools';
|
|
|
|
|
+import store from '@/states/store';
|
|
|
|
|
+import { setToolActive, setToolInactive } from '@/states/view/functionAreaSlice';
|
|
|
|
|
+
|
|
|
|
|
+// 工具名称到action的映射
|
|
|
|
|
+const TOOL_TO_ACTION_MAP: Record<string, string> = {
|
|
|
|
|
+ 'WindowLevel': 'Adjust Brightness and Contrast',
|
|
|
|
|
+ 'Magnify': 'Magnifier',
|
|
|
|
|
+ // 可以在这里添加其他工具映射
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+export class ToolStateListener {
|
|
|
|
|
+ private static instance: ToolStateListener;
|
|
|
|
|
+ private initialized = false;
|
|
|
|
|
+
|
|
|
|
|
+ static getInstance(): ToolStateListener {
|
|
|
|
|
+ if (!ToolStateListener.instance) {
|
|
|
|
|
+ ToolStateListener.instance = new ToolStateListener();
|
|
|
|
|
+ }
|
|
|
|
|
+ return ToolStateListener.instance;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ init() {
|
|
|
|
|
+ if (this.initialized) return;
|
|
|
|
|
+
|
|
|
|
|
+ eventTarget.addEventListener(Enums.Events.TOOL_MODE_CHANGED, this.handleToolModeChanged);
|
|
|
|
|
+ this.initialized = true;
|
|
|
|
|
+ console.log('[ToolStateListener] Initialized and listening for tool mode changes');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private handleToolModeChanged = (event: any) => {
|
|
|
|
|
+ const { toolName, mode } = event.detail;
|
|
|
|
|
+
|
|
|
|
|
+ const action = TOOL_TO_ACTION_MAP[toolName];
|
|
|
|
|
+ if (!action) return;
|
|
|
|
|
+
|
|
|
|
|
+ const isActive = mode === Enums.ToolModes.Active;
|
|
|
|
|
+
|
|
|
|
|
+ console.log(`[ToolStateListener] Tool ${toolName} mode changed: ${isActive ? 'active' : 'inactive'}`);
|
|
|
|
|
+
|
|
|
|
|
+ if (isActive) {
|
|
|
|
|
+ store.dispatch(setToolActive(action));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ store.dispatch(setToolInactive(action));
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ destroy() {
|
|
|
|
|
+ if (this.initialized) {
|
|
|
|
|
+ eventTarget.removeEventListener(Enums.Events.TOOL_MODE_CHANGED, this.handleToolModeChanged);
|
|
|
|
|
+ this.initialized = false;
|
|
|
|
|
+ console.log('[ToolStateListener] Destroyed');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|