Update keyboard/mouse modifiers to return an array rather than a set (#1015)

This commit is contained in:
toasted-nutbread 2020-11-08 22:19:54 -05:00 committed by GitHub
parent 681065e554
commit 8edb478d0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 12 deletions

View File

@ -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.

View File

@ -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()) {

View File

@ -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;
}
}

View File

@ -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

View File

@ -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) {