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

5.55% Statements 1/18
0% Branches 0/12
0% Functions 0/2
6.25% Lines 1/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          1x                                                                                                                      
import { ToolGroupManager } from '../../store';
import { MouseBindings, ToolModes } from '../../enums';
import { 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 TouchEvent's `buttons`. It's possible there will be no match
 * (no active tool for that touch button combination).
 *
 * @param evt - The event dispatcher touch event.
 *
 * @returns tool
 */
export default function getActiveToolForTouchEvent(
  evt: EventTypes.NormalizedTouchEventType
) {
  // Todo: we should refactor this to use getToolsWithModesForTouchEvent instead
  const { renderingEngineId, viewportId } = evt.detail;
  const touchEvent = evt.detail.event;
 
  const toolGroup = ToolGroupManager.getToolGroupForViewport(
    viewportId,
    renderingEngineId
  );
 
  if (!toolGroup) {
    return null;
  }
 
  const toolGroupToolNames = Object.keys(toolGroup.toolOptions);
 
  const numTouchPoints = Object.keys(touchEvent.touches).length;
 
  // If any keyboard modifier key is also pressed
  const modifierKey = getMouseModifier(touchEvent);
 
  for (let j = 0; j < toolGroupToolNames.length; j++) {
    const toolName = toolGroupToolNames[j];
    const toolOptions = toolGroup.toolOptions[toolName];
 
    const correctBinding =
      toolOptions.bindings.length &&
      /**
       * TODO: setActiveTool treats MouseBindings.Primary in a special way
       * which is analgous to numTouchPoints === 1 as the primary interaction
       * for touch based applications. The ToolGroup set active and get active
       * logic should be updated to account for numTouchPoints === 1
       */
      toolOptions.bindings.some(
        (binding) =>
          (binding.numTouchPoints === numTouchPoints ||
            (numTouchPoints === 1 &&
              binding.mouseButton === MouseBindings.Primary)) &&
          binding.modifierKey === modifierKey
      );
 
    if (toolOptions.mode === Active && correctBinding) {
      return toolGroup.getToolInstance(toolName);
    }
  }
}