Skip to content

Commit

Permalink
Merge pull request #1339 from silx-kit/selection-change
Browse files Browse the repository at this point in the history
Fix selection change callback not called on first pointer move
  • Loading branch information
axelboc authored Jan 6, 2023
2 parents fa2d21d + 90420ac commit 12b0fb9
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions packages/lib/src/interactions/SelectionTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,24 @@ function SelectionTool(props: Props) {
useEffect(() => {
const { selection, isCancelled } = selectionState;

// Selection state is now defined => selection has started
if (!prevSelectionState?.selection && selection) {
onSelectionStartRef.current?.(selection);
return;
}

/* Selection state is now undefined => selection has ended.
* Invoke callback but only if the selection has ended successfully - i.e. if:
* - the selection was not cancelled with Escape,
* - and the modifier key was not released before the pointer. */
if (prevSelectionState?.selection && !selection && !isCancelled) {
onSelectionEndRef.current?.(prevSelectionState.selection);
return;
}
if (selection) {
// Previous selection was undefined and current selection is now defined => selection has started
if (!prevSelectionState?.selection) {
onSelectionStartRef.current?.(selection);
}

// Selection remains defined => selection has changed
if (prevSelectionState?.selection && selection) {
// Either way, current selection is defined, so invoke change callback
onSelectionChangeRef.current?.(
isModifierKeyPressed ? selection : undefined // don't pass selection object if user is not pressing modifier key
);

return;
}

/* Previous selection was defined and current selection is now undefined => selection has ended.
* Invoke callback but only if selection wasn't cancelled. */
if (prevSelectionState?.selection && !isCancelled) {
onSelectionEndRef.current?.(prevSelectionState.selection);
}
}, [
prevSelectionState,
Expand Down

0 comments on commit 12b0fb9

Please sign in to comment.