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

94.11% Statements 16/17
86.66% Branches 13/15
100% Functions 2/2
93.75% Lines 15/16

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 66            428x                                       1116x           1116x   1116x   1116x       1116x 1116x   1116x   1116x 4195x 4195x         4195x   5230x           4195x 1116x        
import { ToolModes } from '../../enums';
import { keyEventListener } from '../../eventListeners';
import { getToolGroupForViewport } from '../../store/ToolGroupManager';
import type { EventTypes } from '../../types';
import getMouseModifier from './getMouseModifier';
 
const { Active } = ToolModes;
 
/**
 * Iterate tool group tools until we find a tool that has a "ToolBinding"
 * that matches our MouseEvent's `buttons`. It's possible there will be no match
 * (no active tool for that mouse button combination), in which case undefined
 * is returned.
 *
 * The buttons used for matching are first from the `evt.buttons`, then the `evt.detail.event.buttons`
 * and finally defaulting to the primary mouse button if none are defined.  This
 * allows over-riding the buttons, as one can't update the event buttons.
 *
 * @param evt - The event dispatcher mouse event.
 *
 * @returns tool
 */
export default function getActiveToolForMouseEvent(
  evt: EventTypes.NormalizedMouseEventType
) {
  // Todo: we should refactor this to use getToolsWithModesForMouseEvent instead
  const { renderingEngineId, viewportId, event: mouseEvent } = evt.detail;
 
  // If any keyboard modifier key is also pressed - get the mouse version
  // first since it handles combinations, while the key event handles non-modifier
  // keys.
  const modifierKey =
    getMouseModifier(mouseEvent) || keyEventListener.getModifierKey();
 
  const toolGroup = getToolGroupForViewport(viewportId, renderingEngineId);
 
  Iif (!toolGroup) {
    return null;
  }
 
  const toolGroupToolNames = Object.keys(toolGroup.toolOptions);
  const defaultMousePrimary = toolGroup.getDefaultMousePrimary();
  const mouseButton =
    evt.detail.buttons ?? mouseEvent?.buttons ?? defaultMousePrimary;
 
  for (let j = 0; j < toolGroupToolNames.length; j++) {
    const toolName = toolGroupToolNames[j];
    const toolOptions = toolGroup.toolOptions[toolName];
 
    // tool has binding that matches the mouse button, if mouseEvent is undefined
    // it uses the primary button
    const correctBinding =
      toolOptions.bindings.length &&
      toolOptions.bindings.some((binding) => {
        return (
          binding.mouseButton === mouseButton &&
          binding.modifierKey === modifierKey
        );
      });
 
    if (toolOptions.mode === Active && correctBinding) {
      return toolGroup.getToolInstance(toolName);
    }
  }
}