removeCollapsedSelectionWhenShiftIsReleased top-level property

ReadOnlyDocumentKeyboardAction removeCollapsedSelectionWhenShiftIsReleased
final

Shortcut to remove a document selection when the shift key is released and the selection is collapsed.

Read-only documents should only display expanded selections (selections that contain at least one character or block of content). The user might expand or contract a selection while holding the shift key. As long as the user is pressing shift, we want to allow any selection. When the user releases the shift key (and triggers this shortcut), we want to remove the document selection if it's collapsed.

Implementation

final removeCollapsedSelectionWhenShiftIsReleased = createShortcut(
  ({
    required SuperReaderContext documentContext,
    required KeyEvent keyEvent,
  }) {
    final selection = documentContext.selection.value;
    if (selection == null || !selection.isCollapsed) {
      return ExecutionInstruction.continueExecution;
    }

    // The selection is collapsed, and the shift key was released. We don't
    // want to retain the selection any longer. Remove it.
    documentContext.selection.value = null;
    return ExecutionInstruction.haltExecution;
  },
  keyPressedOrReleased: LogicalKeyboardKey.shift,
  isShiftPressed: false,
  onKeyUp: true,
  onKeyDown: false,
);