From 8edb478d0adbc7c09e827f1606f9e7a6660dec65 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 8 Nov 2020 22:19:54 -0500 Subject: [PATCH] Update keyboard/mouse modifiers to return an array rather than a set (#1015) --- .../js/settings/keyboard-mouse-input-field.js | 2 +- ext/fg/js/frontend.js | 1 + ext/mixed/js/display.js | 12 +++++++++++- ext/mixed/js/document-util.js | 17 ++++++++--------- ext/mixed/js/text-scanner.js | 3 ++- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/ext/bg/js/settings/keyboard-mouse-input-field.js b/ext/bg/js/settings/keyboard-mouse-input-field.js index f9c6dfcd..d5a2a2ea 100644 --- a/ext/bg/js/settings/keyboard-mouse-input-field.js +++ b/ext/bg/js/settings/keyboard-mouse-input-field.js @@ -147,7 +147,7 @@ class KeyboardMouseInputField extends EventDispatcher { } _getModifierKeys(e) { - const modifiers = DocumentUtil.getActiveModifiers(e); + const modifiers = new Set(DocumentUtil.getActiveModifiers(e)); // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/metaKey // https://askubuntu.com/questions/567731/why-is-shift-alt-being-mapped-to-meta // It works with mouse events on some platforms, so try to determine if metaKey is pressed. diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index af1a6527..0ed842f5 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -250,6 +250,7 @@ class Frontend { } async _onActiveModifiersChanged({modifiers}) { + modifiers = new Set(modifiers); if (areSetsEqual(modifiers, this._activeModifiers)) { return; } this._activeModifiers = modifiers; if (this._popup !== null && await this._popup.isVisible()) { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 39bd7732..9824c25e 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -205,7 +205,7 @@ class Display extends EventDispatcher { const eventModifiers = DocumentUtil.getActiveModifiers(e); for (const {modifiers, action} of handlers) { - if (getSetDifference(modifiers, eventModifiers).size !== 0) { continue; } + if (!this._areSame(modifiers, eventModifiers)) { continue; } const actionHandler = this._actions.get(action); if (typeof actionHandler === 'undefined') { continue; } @@ -1476,4 +1476,14 @@ class Display extends EventDispatcher { const {expression, reading} = termDetailsList[Math.max(0, bestIndex)]; return {expression, reading}; } + + _areSame(set, array) { + if (set.size !== array.size) { return false; } + for (const value of array) { + if (!set.has(value)) { + return false; + } + } + return true; + } } diff --git a/ext/mixed/js/document-util.js b/ext/mixed/js/document-util.js index 90add8f9..da27a75d 100644 --- a/ext/mixed/js/document-util.js +++ b/ext/mixed/js/document-util.js @@ -170,11 +170,11 @@ class DocumentUtil { } static getActiveModifiers(event) { - const modifiers = new Set(); - if (event.altKey) { modifiers.add('alt'); } - if (event.ctrlKey) { modifiers.add('ctrl'); } - if (event.metaKey) { modifiers.add('meta'); } - if (event.shiftKey) { modifiers.add('shift'); } + const modifiers = []; + if (event.altKey) { modifiers.push('alt'); } + if (event.ctrlKey) { modifiers.push('ctrl'); } + if (event.metaKey) { modifiers.push('meta'); } + if (event.shiftKey) { modifiers.push('shift'); } return modifiers; } @@ -185,7 +185,7 @@ class DocumentUtil { } static getActiveButtons(event) { - const buttons = new Set(); + const buttons = []; this._getActiveButtons(event, buttons); return buttons; } @@ -301,19 +301,18 @@ class DocumentUtil { return !(browser === 'firefox' || browser === 'firefox-mobile') || os === 'mac'; } - static _getActiveButtons(event, set) { + static _getActiveButtons(event, array) { let {buttons} = event; if (typeof buttons === 'number' && buttons > 0) { for (let i = 0; i < 6; ++i) { const buttonFlag = (1 << i); if ((buttons & buttonFlag) !== 0) { - set.add(`mouse${i}`); + array.push(`mouse${i}`); buttons &= ~buttonFlag; if (buttons === 0) { break; } } } } - return set; } // Private diff --git a/ext/mixed/js/text-scanner.js b/ext/mixed/js/text-scanner.js index f4b8df68..189afd07 100644 --- a/ext/mixed/js/text-scanner.js +++ b/ext/mixed/js/text-scanner.js @@ -817,11 +817,12 @@ class TextScanner extends EventDispatcher { _getMatchingInputGroup(modifiers, type) { let fallback = null; + const modifiersSet = new Set(modifiers); for (let i = 0, ii = this._inputs.length; i < ii; ++i) { const input = this._inputs[i]; const {include, exclude, types} = input; if (!types.has(type)) { continue; } - if (this._setHasAll(modifiers, include) && (exclude.length === 0 || !this._setHasAll(modifiers, exclude))) { + if (this._setHasAll(modifiersSet, include) && (exclude.length === 0 || !this._setHasAll(modifiersSet, exclude))) { if (include.length > 0) { return {index: i, empty: false, input}; } else if (fallback === null) {