All files / tools/src/eventDispatchers/shared getToolsWithActionsForMouseEvent.ts

91.66% Statements 22/24
83.33% Branches 15/18
100% Functions 3/3
91.3% Lines 21/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65                                          74x 74x 74x   74x       74x 74x 74x 74x   74x   74x 404x 404x 404x 404x   404x 350x     54x     108x     108x         54x         74x    
import type { ToolModes } from '../../enums';
import type { ToolAction, EventTypes } from '../../types';
 
import { keyEventListener } from '../../eventListeners';
import getMouseModifier from './getMouseModifier';
import type { BaseTool } from '../../tools';
import { getToolGroupForViewport } from '../../store/ToolGroupManager';
 
/**
 * Given the mouse event and a list of tool modes, find all tool instances
 * with actions that were added to the tool group associated with the viewport
 * that triggered the event.
 *
 * @param evt - mouseDown event triggered by a cornerstone viewport
 * @param toolModes - List of tool modes used to filter the tools registered
 *                    in the viewport's tool group
 */
export default function getToolsWithActionsForMouseEvent(
  evt: EventTypes.MouseMoveEventType,
  toolModes: ToolModes[]
): Map<BaseTool, ToolAction> {
  const toolsWithActions = new Map();
  const { renderingEngineId, viewportId } = evt.detail;
  const toolGroup = getToolGroupForViewport(viewportId, renderingEngineId);
 
  Iif (!toolGroup) {
    return toolsWithActions;
  }
 
  const toolGroupToolNames = Object.keys(toolGroup.toolOptions);
  const defaultMousePrimary = toolGroup.getDefaultMousePrimary();
  const mouseEvent = evt.detail.event;
  const mouseButton = mouseEvent?.buttons ?? defaultMousePrimary;
  const modifierKey =
    getMouseModifier(mouseEvent) || keyEventListener.getModifierKey();
 
  for (let j = 0; j < toolGroupToolNames.length; j++) {
    const toolName = toolGroupToolNames[j];
    const tool = toolGroup.getToolInstance(toolName);
    const actionsConfig = tool.configuration?.actions ?? {};
    const actions = Object.values(actionsConfig);
 
    if (!actions?.length || !toolModes.includes(tool.mode)) {
      continue;
    }
 
    const action = actions.find(
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      (action: any) =>
        action.bindings?.length &&
        action.bindings.some(
          (binding) =>
            binding.mouseButton === mouseButton &&
            binding.modifierKey === modifierKey
        )
    );
 
    Iif (action) {
      toolsWithActions.set(tool, action);
    }
  }
 
  return toolsWithActions;
}