Skip to content

Commit

Permalink
feat(toolEvent): added an event that is triggered when a tool is acti…
Browse files Browse the repository at this point in the history
…vated (#718)

* feat(toolEvent): added an event that is triggered when a tool is activated

* feat(toolEvent): api doc

* code review
  • Loading branch information
lscoder authored Aug 2, 2023
1 parent 64d4409 commit c67b61e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
14 changes: 14 additions & 0 deletions common/reviews/api/tools.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -1825,6 +1827,8 @@ declare namespace EventTypes_2 {
NormalizedInteractionEventDetail,
NormalizedMouseEventType,
NormalizedTouchEventType,
ToolActivatedEventDetail,
ToolActivatedEventType,
AnnotationAddedEventDetail,
AnnotationAddedEventType,
AnnotationCompletedEventDetail,
Expand Down Expand Up @@ -5025,6 +5029,16 @@ function throttle(func: Function, wait?: number, options?: {
trailing?: boolean;
}): Function;

// @public (undocumented)
type ToolActivatedEventDetail = {
toolGroupId: string;
toolName: string;
toolBindingsOptions: SetToolBindingsType;
};

// @public (undocumented)
type ToolActivatedEventType = Types_2.CustomEventType<ToolActivatedEventDetail>;

// @public (undocumented)
interface ToolData {
// (undocumented)
Expand Down
13 changes: 13 additions & 0 deletions packages/tools/src/enums/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
///////////////////////////////////////
Expand Down
14 changes: 13 additions & 1 deletion packages/tools/src/store/ToolGroupManager/ToolGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { MouseBindings, ToolModes } from '../../enums';
import cloneDeep from 'lodash.clonedeep';
import get from 'lodash.get';
import {
triggerEvent,
eventTarget,
getRenderingEngine,
getRenderingEngines,
getEnabledElementByIds,
Settings,
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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -372,6 +376,14 @@ export default class ToolGroup implements IToolGroup {
toolInstance.onSetToolActive();
}
this._renderViewports();

const eventDetail: ToolActivatedEventDetail = {
toolGroupId: this.id,
toolName,
toolBindingsOptions,
};

triggerEvent(eventTarget, Events.TOOL_ACTIVATED, eventDetail);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/tools/src/types/EventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -70,6 +71,18 @@ type InteractionStartEventDetail = InteractionEventDetail;

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 */
toolBindingsOptions: SetToolBindingsType;
};

/**
* The data that is passed to the event handler when a new annotation is added
* to the annotations.
Expand Down Expand Up @@ -411,6 +424,11 @@ type NormalizedMouseEventType = Types.CustomEventType<MouseCustomEventDetail>;
*/
type NormalizedTouchEventType = Types.CustomEventType<TouchCustomEventDetail>;

/**
* The ToolActivated event type
*/
type ToolActivatedEventType = Types.CustomEventType<ToolActivatedEventDetail>;

/**
* The AnnotationAdded event type
*/
Expand Down Expand Up @@ -611,6 +629,8 @@ export {
NormalizedInteractionEventDetail,
NormalizedMouseEventType,
NormalizedTouchEventType,
ToolActivatedEventDetail,
ToolActivatedEventType,
AnnotationAddedEventDetail,
AnnotationAddedEventType,
AnnotationCompletedEventDetail,
Expand Down

0 comments on commit c67b61e

Please sign in to comment.