Skip to content

Commit

Permalink
Simplify event handling logic used by both event strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
greena13 committed Jan 27, 2019
1 parent 88dc94a commit c7deffe
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 246 deletions.
18 changes: 18 additions & 0 deletions src/const/EventResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @typedef {Number} EventResponseType
*/

/**
* Enum for different ways to respond to a key event
* @readonly
* @enum {EventResponseType}
*/
const EventResponse = {
unseen: 0,
ignored: 1,
seen: 2,
recorded: 3,
handled: 4
};

export default EventResponse;
9 changes: 6 additions & 3 deletions src/lib/KeyEventManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import FocusOnlyKeyEventStrategy from './strategies/FocusOnlyKeyEventStrategy';
import GlobalKeyEventStrategy from './strategies/GlobalKeyEventStrategy';
import isFromFocusOnlyComponent from '../helpers/resolving-handlers/isFromFocusOnlyComponent';
import Configuration from './Configuration';
import EventResponse from '../const/EventResponse';

/**
* Provides a registry for keyboard sequences and events, and the handlers that should
Expand Down Expand Up @@ -377,12 +378,14 @@ class KeyEventManager {

if (currentEvent.key === key && currentEvent.type === type) {
if (currentEvent.handled) {
return 'handled';
return EventResponse.handled;
} else if (currentEvent.ignored) {
return EventResponse.ignored;
} else {
return 'seen';
return EventResponse.seen;
}
} else {
return 'unseen';
return EventResponse.unseen;
}
}

Expand Down
160 changes: 31 additions & 129 deletions src/lib/strategies/FocusOnlyKeyEventStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import isUndefined from '../../utils/isUndefined';
import normalizeKeyName from '../../helpers/resolving-handlers/normalizeKeyName';
import isCmdKey from '../../helpers/parsing-key-maps/isCmdKey';
import describeKeyEvent from '../../helpers/logging/describeKeyEvent';
import EventResponse from '../../const/EventResponse';

/**
* Defines behaviour for dealing with key maps defined in focus-only HotKey components
Expand Down Expand Up @@ -61,7 +62,9 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
*/
type: null,

handled: false
handled: false,

ignored: false,
};
}

Expand Down Expand Up @@ -287,14 +290,15 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
return true;
}

const shouldHandleEvent = this._shouldHandleKeyDownEvent(event,
const responseAction = this._howToHandleKeyDownEvent(event,
focusTreeId,
componentId,
_key,
options
options,
KeyEventBitmapIndex.keydown
);

if (shouldHandleEvent) {
if (responseAction === EventResponse.handled) {
const keyInCurrentCombination = !!this._getCurrentKeyState(_key);

if (keyInCurrentCombination || this.keyCombinationIncludesKeyUp) {
Expand All @@ -313,20 +317,20 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
return false;
}

_shouldHandleKeyDownEvent(event, focusTreeId, componentId, key, options){
_howToHandleKeyDownEvent(event, focusTreeId, componentId, key, options, keyEventBitmapIndex){
if (this._shouldIgnoreEvent()) {
this.logger.debug(
this._logPrefix(componentId),
`Ignored ${describeKeyEvent(event, key, KeyEventBitmapIndex.keydown)} event because ignoreEventsFilter rejected it.`
`Ignored ${describeKeyEvent(event, key, keyEventBitmapIndex)} event because ignoreEventsFilter rejected it.`
);

this._ignoreEvent(event, componentId);

return false;
return EventResponse.ignored;
}

if (this._isNewKeyEvent(componentId)) {
this._setNewEventParameters(event, KeyEventBitmapIndex.keydown);
this._setNewEventParameters(event, keyEventBitmapIndex);

/**
* We know that this is a new key event and not the same event bubbling up
Expand All @@ -339,23 +343,23 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
if (this._shouldIgnoreEvent()) {
this.logger.debug(
this._logPrefix(componentId),
`Ignored ${describeKeyEvent(event, key, KeyEventBitmapIndex.keydown)} event because ignoreEventsFilter rejected it.`
`Ignored ${describeKeyEvent(event, key, keyEventBitmapIndex)} event because ignoreEventsFilter rejected it.`
);

this._ignoreEvent(event, componentId);

return false;
return EventResponse.ignored;
}

this.logger.debug(
this._logPrefix(componentId),
`New ${describeKeyEvent(event, key, KeyEventBitmapIndex.keydown)} event.`
`New ${describeKeyEvent(event, key, keyEventBitmapIndex)} event.`
);

this._checkForModifierFlagDiscrepancies(event);
}

return true;
return EventResponse.handled;
}

/**
Expand Down Expand Up @@ -385,8 +389,12 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
* We first decide if the keypress event should be handled (to ensure the correct
* order of logging statements)
*/
const shouldHandleEvent = this._shouldHandleKeyPressEvent(
event, focusTreeId, componentId, _key, options
const responseAction = this._howToHandleKeyDownEvent(event,
focusTreeId,
componentId,
_key,
options,
KeyEventBitmapIndex.keypress
);

if (this._isNewKeyEvent(componentId) && this._getCurrentKeyState(_key)) {
Expand All @@ -402,7 +410,7 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
* We attempt to find a handler of the event, only if it has not already
* been handled and should not be ignored
*/
if (shouldHandleEvent) {
if (responseAction === EventResponse.handled) {
this._callHandlerIfActionNotHandled(
event,
_key,
Expand All @@ -417,61 +425,6 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
return shouldDiscardFocusTreeId;
}

_shouldHandleKeyPressEvent(event, focusTreeId, componentId, key, options) {
if (focusTreeId !== this.focusTreeId) {
this.logger.debug(
this._logPrefix(componentId),
`Ignored ${describeKeyEvent(event, key, KeyEventBitmapIndex.keypress)} event because it had an old focus tree id: ${focusTreeId}.`
);

this._ignoreEvent(event, componentId);

return false;
}

if (this._shouldIgnoreEvent()) {
this.logger.debug(
this._logPrefix(componentId),
`Ignored ${describeKeyEvent(event, key, KeyEventBitmapIndex.keypress)} event because ignoreEventsFilter rejected it.`
);

this._ignoreEvent(event, componentId);

return false;
}

if (this._isNewKeyEvent(componentId)) {
this._setNewEventParameters(event, KeyEventBitmapIndex.keypress);

/**
* We know that this is a new key event and not the same event bubbling up
* the React render tree towards the document root, so perform actions specific
* to the first time an event is seen
*/

this._setIgnoreEventFlag(event, options);

if (this._shouldIgnoreEvent()) {
this.logger.debug(
this._logPrefix(componentId),
`Ignored ${describeKeyEvent(event, key, KeyEventBitmapIndex.keypress)} event because ignoreEventsFilter rejected it.`
);

this._ignoreEvent(event, componentId);

return false;
}

this.logger.debug(
this._logPrefix(componentId),
`New ${describeKeyEvent(event, key, KeyEventBitmapIndex.keypress)} event.`
);

return true;
}
}


/**
* Records a keyup keyboard event and matches it against the list of pre-registered
* event handlers, calling the first matching handler with the highest priority if
Expand Down Expand Up @@ -499,12 +452,12 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
* We first decide if the keyup event should be handled (to ensure the correct
* order of logging statements)
*/
const shouldHandleKeyUp = this._shouldHandleKeyupEvent(
event,
const responseAction = this._howToHandleKeyDownEvent(event,
focusTreeId,
componentId,
_key,
options
options,
KeyEventBitmapIndex.keyup
);

/**
Expand All @@ -526,7 +479,7 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
* We attempt to find a handler of the event, only if it has not already
* been handled and should not be ignored
*/
if (shouldHandleKeyUp) {
if (responseAction === EventResponse.handled) {
this._callHandlerIfActionNotHandled(event, _key, KeyEventBitmapIndex.keyup, componentId, focusTreeId);
}

Expand All @@ -541,60 +494,6 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
return shouldDiscardFocusId;
}

_shouldHandleKeyupEvent(event, focusTreeId, componentId, key, options){
if (focusTreeId !== this.focusTreeId) {
this.logger.debug(
this._logPrefix(componentId),
`Ignored ${describeKeyEvent(event, key, KeyEventBitmapIndex.keyup)} because it had an old focus tree id: ${focusTreeId}.`
);

this._ignoreEvent(event, componentId);

return false;
}

if (this._shouldIgnoreEvent()) {
this.logger.debug(
this._logPrefix(componentId),
`Ignored ${describeKeyEvent(event, key, KeyEventBitmapIndex.keyup)} event because ignoreEventsFilter rejected it.`
);

this._ignoreEvent(event, componentId);

return false;
}

if (this._isNewKeyEvent(componentId)) {
this._setNewEventParameters(event, KeyEventBitmapIndex.keyup);

/**
* We know that this is a new key event and not the same event bubbling up
* the React render tree towards the document root, so perform actions specific
* to the first time an event is seen
*/

this._setIgnoreEventFlag(event, options);

if (this._shouldIgnoreEvent()) {
this.logger.debug(
this._logPrefix(componentId),
`Ignored ${describeKeyEvent(event, key, KeyEventBitmapIndex.keyup)} event because ignoreEventsFilter rejected it.`
);

this._ignoreEvent(event, componentId);

return false;
}

this.logger.debug(
this._logPrefix(componentId),
`New ${describeKeyEvent(event, key, KeyEventBitmapIndex.keyup)} event.`
);

return true;
}
}

_simulateKeyPressesMissingFromBrowser(event, key, focusTreeId, componentId, options){
this._handleEventSimulation(
'keypressEventsToSimulate',
Expand Down Expand Up @@ -632,6 +531,8 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
}

_ignoreEvent(event, componentId) {
this.currentEvent.ignored = true;

if(this._stopEventPropagationAfterIgnoringIfEnabled(event, componentId)) {
this._updateEventPropagationHistory(componentId, { forceReset: true });
} else {
Expand Down Expand Up @@ -706,7 +607,8 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
this.currentEvent = {
key: event.key,
type,
handled: false
handled: false,
ignored: false,
};
}

Expand Down
Loading

0 comments on commit c7deffe

Please sign in to comment.