From 68362754f722e61051e0b228974106cdf9cc6c9d Mon Sep 17 00:00:00 2001 From: Leonardo Campos Date: Wed, 2 Aug 2023 03:53:52 -0300 Subject: [PATCH 1/3] feat(toolEvent): added an event that is triggered when a tool is activated --- packages/tools/src/enums/Events.ts | 13 +++++++++++++ .../src/store/ToolGroupManager/ToolGroup.ts | 13 ++++++++++++- packages/tools/src/types/EventTypes.ts | 18 ++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/tools/src/enums/Events.ts b/packages/tools/src/enums/Events.ts index 8c7747eee..2f7b354f6 100644 --- a/packages/tools/src/enums/Events.ts +++ b/packages/tools/src/enums/Events.ts @@ -5,6 +5,19 @@ * */ enum Events { + /////////////////////////////////////// + // Tools + /////////////////////////////////////// + + /** + * Triggers on the eventTarget when a new tools is activated. + * + * Make use of {@link EventTypes.ToolActivatedEventType | Tool Activated Event Type } + * for typing your event listeners for this tool activated event, and see what event + * detail is included in {@link EventTypes.ToolActivatedEventDetail | Tool Activated Event Detail}. + */ + TOOL_ACTIVATED = 'CORNERSTONE_TOOLS_TOOL_ACTIVATED', + /////////////////////////////////////// // Annotations /////////////////////////////////////// diff --git a/packages/tools/src/store/ToolGroupManager/ToolGroup.ts b/packages/tools/src/store/ToolGroupManager/ToolGroup.ts index aa93fc613..f45a83435 100644 --- a/packages/tools/src/store/ToolGroupManager/ToolGroup.ts +++ b/packages/tools/src/store/ToolGroupManager/ToolGroup.ts @@ -2,6 +2,8 @@ import { MouseBindings, ToolModes } from '../../enums'; import cloneDeep from 'lodash.clonedeep'; import get from 'lodash.get'; import { + triggerEvent, + eventTarget, getRenderingEngine, getRenderingEngines, getEnabledElementByIds, @@ -9,6 +11,8 @@ import { utilities as csUtils, } from '@cornerstonejs/core'; import type { Types } from '@cornerstonejs/core'; +import { Events } from '../../enums'; +import { ToolActivatedEventDetail } from '../../types/EventTypes'; import { state } from '../index'; import { IToolBinding, @@ -69,7 +73,7 @@ export default class ToolGroup implements IToolGroup { const toolInstance = this._toolInstances[toolInstanceName]; if (!toolInstance) { console.warn( - `'${toolInstanceName}' is not registered with this toolGroup.` + `'${toolInstanceName}' is not registered with this toolGroup (${this.id}).` ); return; } @@ -372,6 +376,13 @@ export default class ToolGroup implements IToolGroup { toolInstance.onSetToolActive(); } this._renderViewports(); + + const eventDetail: ToolActivatedEventDetail = { + toolName, + toolBindingsOptions, + }; + + triggerEvent(eventTarget, Events.TOOL_ACTIVATED, eventDetail); } /** diff --git a/packages/tools/src/types/EventTypes.ts b/packages/tools/src/types/EventTypes.ts index e1414ecd8..adae711ee 100644 --- a/packages/tools/src/types/EventTypes.ts +++ b/packages/tools/src/types/EventTypes.ts @@ -3,6 +3,7 @@ import { Annotation } from './AnnotationTypes'; import IPoints from './IPoints'; import ITouchPoints from './ITouchPoints'; import IDistance from './IDistance'; +import { SetToolBindingsType } from './ISetToolModeOptions'; import { Swipe } from '../enums/Touch'; /** @@ -70,6 +71,16 @@ type InteractionStartEventDetail = InteractionEventDetail; type InteractionEndEventDetail = InteractionEventDetail; +/** + * The data that is passed to the event handler when a tool is activated. + */ +type ToolActivatedEventDetail = { + /** Tool name */ + toolName: string; + /** Tool binding options */ + toolBindingsOptions: SetToolBindingsType; +}; + /** * The data that is passed to the event handler when a new annotation is added * to the annotations. @@ -411,6 +422,11 @@ type NormalizedMouseEventType = Types.CustomEventType; */ type NormalizedTouchEventType = Types.CustomEventType; +/** + * The ToolActivated event type + */ +type ToolActivatedEventType = Types.CustomEventType; + /** * The AnnotationAdded event type */ @@ -611,6 +627,8 @@ export { NormalizedInteractionEventDetail, NormalizedMouseEventType, NormalizedTouchEventType, + ToolActivatedEventDetail, + ToolActivatedEventType, AnnotationAddedEventDetail, AnnotationAddedEventType, AnnotationCompletedEventDetail, From b3699ba99f8d08a974f80c2ebc02569f1cd0ec74 Mon Sep 17 00:00:00 2001 From: Leonardo Campos Date: Wed, 2 Aug 2023 04:08:17 -0300 Subject: [PATCH 2/3] feat(toolEvent): api doc --- common/reviews/api/tools.api.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/common/reviews/api/tools.api.md b/common/reviews/api/tools.api.md index 4b5012ff7..9beb0c874 100644 --- a/common/reviews/api/tools.api.md +++ b/common/reviews/api/tools.api.md @@ -1743,6 +1743,8 @@ enum Events { // (undocumented) SEGMENTATION_REPRESENTATION_REMOVED = "CORNERSTONE_TOOLS_SEGMENTATION_REPRESENTATION_REMOVED", // (undocumented) + TOOL_ACTIVATED = "CORNERSTONE_TOOLS_TOOL_ACTIVATED", + // (undocumented) TOUCH_DRAG = "CORNERSTONE_TOOLS_TOUCH_DRAG", // (undocumented) TOUCH_END = "CORNERSTONE_TOOLS_TOUCH_END", @@ -1825,6 +1827,8 @@ declare namespace EventTypes_2 { NormalizedInteractionEventDetail, NormalizedMouseEventType, NormalizedTouchEventType, + ToolActivatedEventDetail, + ToolActivatedEventType, AnnotationAddedEventDetail, AnnotationAddedEventType, AnnotationCompletedEventDetail, @@ -5024,6 +5028,15 @@ function throttle(func: Function, wait?: number, options?: { trailing?: boolean; }): Function; +// @public (undocumented) +type ToolActivatedEventDetail = { + toolName: string; + toolBindingsOptions: SetToolBindingsType; +}; + +// @public (undocumented) +type ToolActivatedEventType = Types_2.CustomEventType; + // @public (undocumented) interface ToolData { // (undocumented) From 54a0e5daeb818605c7804ec7b088cc843a1af2ac Mon Sep 17 00:00:00 2001 From: Leonardo Campos Date: Wed, 2 Aug 2023 13:40:40 -0300 Subject: [PATCH 3/3] code review --- common/reviews/api/tools.api.md | 1 + packages/tools/src/store/ToolGroupManager/ToolGroup.ts | 1 + packages/tools/src/types/EventTypes.ts | 2 ++ 3 files changed, 4 insertions(+) diff --git a/common/reviews/api/tools.api.md b/common/reviews/api/tools.api.md index 9beb0c874..d23ac2c6a 100644 --- a/common/reviews/api/tools.api.md +++ b/common/reviews/api/tools.api.md @@ -5030,6 +5030,7 @@ function throttle(func: Function, wait?: number, options?: { // @public (undocumented) type ToolActivatedEventDetail = { + toolGroupId: string; toolName: string; toolBindingsOptions: SetToolBindingsType; }; diff --git a/packages/tools/src/store/ToolGroupManager/ToolGroup.ts b/packages/tools/src/store/ToolGroupManager/ToolGroup.ts index f45a83435..3b4cfbcd1 100644 --- a/packages/tools/src/store/ToolGroupManager/ToolGroup.ts +++ b/packages/tools/src/store/ToolGroupManager/ToolGroup.ts @@ -378,6 +378,7 @@ export default class ToolGroup implements IToolGroup { this._renderViewports(); const eventDetail: ToolActivatedEventDetail = { + toolGroupId: this.id, toolName, toolBindingsOptions, }; diff --git a/packages/tools/src/types/EventTypes.ts b/packages/tools/src/types/EventTypes.ts index adae711ee..2a20a786e 100644 --- a/packages/tools/src/types/EventTypes.ts +++ b/packages/tools/src/types/EventTypes.ts @@ -75,6 +75,8 @@ type InteractionEndEventDetail = InteractionEventDetail; * The data that is passed to the event handler when a tool is activated. */ type ToolActivatedEventDetail = { + /** unique id of the toolGroup */ + toolGroupId: string; /** Tool name */ toolName: string; /** Tool binding options */