Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -5024,6 +5028,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 = {
lscoder marked this conversation as resolved.
Show resolved Hide resolved
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