All files / packages/tools/src/eventDispatchers/mouseEventHandlers mouseDown.ts

83.67% Statements 41/49
62.16% Branches 23/37
75% Functions 3/4
81.39% Lines 35/43

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197                                              1x                                                     87x 4x     83x       83x 9x   9x 9x         74x 74x         74x     74x         74x 74x     74x         74x         74x               74x       74x 6x       6x 6x   6x         68x                 68x 14x       14x 14x   14x       54x                                         20x                                         20x 20x               20x 20x      
import { state } from '../../store';
import { ToolModes } from '../../enums';
import { EventTypes } from '../../types';
import {
  ToolAnnotationPair,
  ToolsWithMoveableHandles,
} from '../../types/InternalToolTypes';
 
import {
  setAnnotationSelected,
  isAnnotationSelected,
} from '../../stateManagement/annotation/annotationSelection';
 
import { isAnnotationLocked } from '../../stateManagement/annotation/annotationLocking';
import { isAnnotationVisible } from '../../stateManagement/annotation/annotationVisibility';
 
// Util
import filterToolsWithMoveableHandles from '../../store/filterToolsWithMoveableHandles';
import filterToolsWithAnnotationsForElement from '../../store/filterToolsWithAnnotationsForElement';
import filterMoveableAnnotationTools from '../../store/filterMoveableAnnotationTools';
import getActiveToolForMouseEvent from '../shared/getActiveToolForMouseEvent';
import getToolsWithModesForMouseEvent from '../shared/getToolsWithModesForMouseEvent';
 
const { Active, Passive } = ToolModes;
 
/**
 * When the mouse is depressed we check which entities can process these events in the following manner:
 *
 * - First we get the `activeTool` for the mouse button pressed.
 * - If the `activeTool` has a `preMouseDownCallback`, this is called. If the callback returns `true`,
 *   the event does not propagate further.
 * - Next we get all tools which are active or passive (`activeAndPassiveTools`), as annotation. for these tools could
 *   possibly catch and handle these events. We then filter the `activeAndPassiveTools` using `filterToolsWithAnnotationsForElement`, which filters tools with annotations
 *   for this frame of reference. Optionally a tool can employ a further filtering (via a
 *   `filterInteractableAnnotationsForElement` callback) for tools interactable within the current camera view
 *   (e.g. tools that only render when viewed from a certain direction).
 * - Next we check if any handles are interactable for each tool (`filterToolsWithMoveableHandles`). If interactable
 *   handles are found, the first tool/handle found consumes the event and the event does not propagate further.
 * - Next we check any tools are interactable (e.g. moving an entire length annotation rather than one of its handles:
 *   `filterMoveableAnnotationTools`). If interactable tools are found, the first tool found consumes the event and the
 *   event does not propagate further.
 * - Finally, if the `activeTool` has `postMouseDownCallback`, this is called.  If the callback returns `true`,
 *   the event does not propagate further.
 *
 * If the event is not consumed the event will bubble to the `mouseDownActivate` handler.
 *
 * @param evt - The normalized mouseDown event.
 */
export default function mouseDown(evt: EventTypes.MouseDownEventType) {
  // If a tool has locked the current state it is dealing with an interaction within its own eventLoop.
  if (state.isInteractingWithTool) {
    return;
  }
 
  const activeTool = getActiveToolForMouseEvent(evt);
 
  // Check for preMouseDownCallbacks,
  // If the tool claims it consumed the event, prevent further checks.
  if (activeTool && typeof activeTool.preMouseDownCallback === 'function') {
    const consumedEvent = activeTool.preMouseDownCallback(evt);
 
    Eif (consumedEvent) {
      return;
    }
  }
 
  // Find all tools that might respond to this mouse down
  const isPrimaryClick = evt.detail.event.buttons === 1;
  const activeToolsWithEventBinding = getToolsWithModesForMouseEvent(
    evt,
    [Active],
    evt.detail.event.buttons
  );
  const passiveToolsIfEventWasPrimaryMouseButton = isPrimaryClick
    ? getToolsWithModesForMouseEvent(evt, [Passive])
    : undefined;
  const applicableTools = [
    ...(activeToolsWithEventBinding || []),
    ...(passiveToolsIfEventWasPrimaryMouseButton || []),
  ];
 
  const eventDetail = evt.detail;
  const { element } = eventDetail;
 
  // Filter tools with annotations for this element
  const annotationToolsWithAnnotations = filterToolsWithAnnotationsForElement(
    element,
    applicableTools
  );
 
  const canvasCoords = eventDetail.currentPoints.canvas;
 
  // For the canvas coordinates, find all tools that might respond to this mouse down
  // on their handles. This filter will call getHandleNearImagePoint for each tool
  // instance (each annotation)
  const annotationToolsWithMoveableHandles = filterToolsWithMoveableHandles(
    element,
    annotationToolsWithAnnotations,
    canvasCoords,
    'mouse'
  );
 
  // Preserve existing selections when shift key is pressed
  const isMultiSelect = !!evt.detail.event.shiftKey;
 
  // If there are annotation tools whose handle is near the mouse, select the first one
  // that isn't locked. If there's only one annotation tool, select it.
  if (annotationToolsWithMoveableHandles.length > 0) {
    const { tool, annotation, handle } = getAnnotationForSelection(
      annotationToolsWithMoveableHandles
    ) as ToolsWithMoveableHandles;
 
    toggleAnnotationSelection(annotation.annotationUID, isMultiSelect);
    tool.handleSelectedCallback(evt, annotation, handle, 'Mouse');
 
    return;
  }
 
  // If there were no annotation tools whose handle was near the mouse, try to check
  // if any of the annotation tools are interactable (e.g. moving an entire length annotation)
  const moveableAnnotationTools = filterMoveableAnnotationTools(
    element,
    annotationToolsWithAnnotations,
    canvasCoords,
    'mouse'
  );
 
  // If there are annotation tools that are interactable, select the first one
  // that isn't locked. If there's only one annotation tool, select it.
  if (moveableAnnotationTools.length > 0) {
    const { tool, annotation } = getAnnotationForSelection(
      moveableAnnotationTools
    );
 
    toggleAnnotationSelection(annotation.annotationUID, isMultiSelect);
    tool.toolSelectedCallback(evt, annotation, 'Mouse');
 
    return;
  }
 
  // Run the postMouseDownCallback for the active tool if it exists
  Iif (activeTool && typeof activeTool.postMouseDownCallback === 'function') {
    const consumedEvent = activeTool.postMouseDownCallback(evt);
 
    if (consumedEvent) {
      // If the tool claims it consumed the event, prevent further checks.
      return;
    }
  }
 
  // Don't stop propagation so that mouseDownActivate can handle the event
}
 
/**
 * If there are multiple annotation tools, return the first one that isn't locked neither hidden.
 * If there's only one annotation tool, return it
 * @param annotationTools - An array of tools and annotation.
 * @returns The candidate for selection
 */
function getAnnotationForSelection(
  toolsWithMovableHandles: ToolAnnotationPair[]
): ToolAnnotationPair {
  return (
    (toolsWithMovableHandles.length > 1 &&
      toolsWithMovableHandles.find(
        (item) =>
          !isAnnotationLocked(item.annotation) &&
          isAnnotationVisible(item.annotation.annotationUID)
      )) ||
    toolsWithMovableHandles[0]
  );
}
 
/**
 * If the annotation is selected, deselect it. If it's not selected, select it
 * @param annotationUID - The AnnotationUID that we
 * want to toggle the selection of.
 * @param isMultiSelect - If true, the annotation. will be deselected if it is
 * already selected, or deselected if it is selected.
 */
function toggleAnnotationSelection(
  annotationUID: string,
  isMultiSelect = false
): void {
  Iif (isMultiSelect) {
    if (isAnnotationSelected(annotationUID)) {
      setAnnotationSelected(annotationUID, false);
    } else {
      const preserveSelected = true;
      setAnnotationSelected(annotationUID, true, preserveSelected);
    }
  } else {
    const preserveSelected = false;
    setAnnotationSelected(annotationUID, true, preserveSelected);
  }
}