All files / packages/tools/src/eventDispatchers/shared getActiveToolForKeyboardEvent.ts

89.47% Statements 17/19
75% Branches 9/12
100% Functions 2/2
88.88% Lines 16/18

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            1x                           24x     24x       24x   24x         24x       24x 24x   24x 24x 24x   24x           24x     24x       24x 12x        
import { ToolGroupManager } from '../../store';
import { ToolModes } from '../../enums';
import { keyEventListener } from '../../eventListeners';
import { EventTypes } from '../../types';
import { getMouseButton } from '../../eventListeners/mouse/mouseDownListener';
 
const { Active } = ToolModes;
 
/**
 * Iterate tool group tools until we find a tool that has a "ToolBinding"
 * that matches our Keyboard pressed keys. It's possible there will be no match
 * (no active tool for that mouse button combination).
 *
 * @param evt - The normalized keyboard event.
 *
 * @returns tool
 */
export default function getActiveToolForKeyboardEvent(
  evt: EventTypes.KeyDownEventType
) {
  const { renderingEngineId, viewportId } = evt.detail;
 
  // Get the current mouse button clicked
  const mouseButton = getMouseButton();
 
  // If any keyboard modifier key is also pressed
  // TODO - get the real modifier key
  const modifierKey = keyEventListener.getModifierKey();
 
  const toolGroup = ToolGroupManager.getToolGroupForViewport(
    viewportId,
    renderingEngineId
  );
 
  Iif (!toolGroup) {
    return null;
  }
 
  const toolGroupToolNames = Object.keys(toolGroup.toolOptions);
  const defaultMousePrimary = toolGroup.getDefaultMousePrimary();
 
  for (let j = 0; j < toolGroupToolNames.length; j++) {
    const toolName = toolGroupToolNames[j];
    const toolOptions = toolGroup.toolOptions[toolName];
 
    Iif (toolOptions.mode !== Active) {
      continue;
    }
    // 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) =>
          binding.mouseButton === (mouseButton ?? defaultMousePrimary) &&
          binding.modifierKey === modifierKey
      );
 
    if (correctBinding) {
      return toolGroup.getToolInstance(toolName);
    }
  }
}