summaryrefslogtreecommitdiff
path: root/ext/bg/js/settings
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-01-18 13:52:07 -0500
committerGitHub <noreply@github.com>2021-01-18 13:52:07 -0500
commitb0f0ecd7df7694e14f08c6cf376a964abe7ee7f5 (patch)
tree7b7c1d0c5ce3149bb93ea07f6738bf38312d180a /ext/bg/js/settings
parent25568637fe82988522ddd5c4d8642702b898a293 (diff)
Hotkey util (#1270)
* Create HotkeyUtil * Create tests * Use HotkeyUtil * Fix issues
Diffstat (limited to 'ext/bg/js/settings')
-rw-r--r--ext/bg/js/settings/keyboard-mouse-input-field.js75
-rw-r--r--ext/bg/js/settings/scan-inputs-simple-controller.js13
2 files changed, 12 insertions, 76 deletions
diff --git a/ext/bg/js/settings/keyboard-mouse-input-field.js b/ext/bg/js/settings/keyboard-mouse-input-field.js
index d48b130f..94709313 100644
--- a/ext/bg/js/settings/keyboard-mouse-input-field.js
+++ b/ext/bg/js/settings/keyboard-mouse-input-field.js
@@ -17,6 +17,7 @@
/* global
* DocumentUtil
+ * HotkeyUtil
*/
class KeyboardMouseInputField extends EventDispatcher {
@@ -25,15 +26,7 @@ class KeyboardMouseInputField extends EventDispatcher {
this._inputNode = inputNode;
this._mouseButton = mouseButton;
this._isPointerTypeSupported = isPointerTypeSupported;
- this._keySeparator = ' + ';
- this._inputNameMap = new Map(DocumentUtil.getModifierKeys(os));
- this._modifierPriorities = new Map([
- ['meta', -4],
- ['ctrl', -3],
- ['alt', -2],
- ['shift', -1]
- ]);
- this._mouseInputNamePattern = /^mouse(\d+)$/;
+ this._hotkeyUtil = new HotkeyUtil(os);
this._eventListeners = new EventListenerCollection();
this._key = null;
this._modifiers = [];
@@ -93,74 +86,14 @@ class KeyboardMouseInputField extends EventDispatcher {
// Private
_sortModifiers(modifiers) {
- const pattern = this._mouseInputNamePattern;
- const keyPriorities = this._modifierPriorities;
- const modifierInfos = modifiers.map((modifier, index) => {
- const match = pattern.exec(modifier);
- if (match !== null) {
- return [modifier, 1, Number.parseInt(match[1], 10), index];
- } else {
- let priority = keyPriorities.get(modifier);
- if (typeof priority === 'undefined') { priority = 0; }
- return [modifier, 0, priority, index];
- }
- });
- modifierInfos.sort((a, b) => {
- let i = a[1] - b[1];
- if (i !== 0) { return i; }
-
- i = a[2] - b[2];
- if (i !== 0) { return i; }
-
- i = a[0].localeCompare(b[0], 'en-US'); // Ensure an invariant culture
- if (i !== 0) { return i; }
-
- i = a[3] - b[3];
- return i;
- });
- return modifierInfos.map(([modifier]) => modifier);
+ return this._hotkeyUtil.sortModifiers(modifiers);
}
_updateDisplayString() {
- let displayValue = '';
- let first = true;
- for (const modifier of this._modifiers) {
- const {name} = this._getModifierName(modifier);
- if (first) {
- first = false;
- } else {
- displayValue += this._keySeparator;
- }
- displayValue += name;
- }
- if (this._key !== null) {
- if (!first) { displayValue += this._keySeparator; }
- displayValue += this._getDisplayKey(this._key);
- }
+ const displayValue = this._hotkeyUtil.getInputDisplayValue(this._key, this._modifiers);
this._inputNode.value = displayValue;
}
- _getDisplayKey(key) {
- if (typeof key === 'string') {
- if (key.length === 4 && key.startsWith('Key')) {
- key = key.substring(3);
- }
- }
- return key;
- }
-
- _getModifierName(modifier) {
- const pattern = this._mouseInputNamePattern;
- const match = pattern.exec(modifier);
- if (match !== null) {
- return {name: `Mouse ${match[1]}`, type: 'mouse'};
- }
-
- let name = this._inputNameMap.get(modifier);
- if (typeof name === 'undefined') { name = modifier; }
- return {name, type: 'key'};
- }
-
_getModifierKeys(e) {
const modifiers = new Set(DocumentUtil.getActiveModifiers(e));
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/metaKey
diff --git a/ext/bg/js/settings/scan-inputs-simple-controller.js b/ext/bg/js/settings/scan-inputs-simple-controller.js
index 9e7eb5fc..01f044c2 100644
--- a/ext/bg/js/settings/scan-inputs-simple-controller.js
+++ b/ext/bg/js/settings/scan-inputs-simple-controller.js
@@ -16,7 +16,7 @@
*/
/* global
- * DocumentUtil
+ * HotkeyUtil
* ScanInputsController
* api
*/
@@ -27,7 +27,7 @@ class ScanInputsSimpleController {
this._middleMouseButtonScan = null;
this._mainScanModifierKeyInput = null;
this._mainScanModifierKeyInputHasOther = false;
- this._os = null;
+ this._hotkeyUtil = new HotkeyUtil();
}
async prepare() {
@@ -35,7 +35,7 @@ class ScanInputsSimpleController {
this._mainScanModifierKeyInput = document.querySelector('#main-scan-modifier-key');
const {platform: {os}} = await api.getEnvironmentInfo();
- this._os = os;
+ this._hotkeyUtil.os = os;
this._mainScanModifierKeyInputHasOther = false;
this._populateSelect(this._mainScanModifierKeyInput, this._mainScanModifierKeyInputHasOther);
@@ -106,9 +106,12 @@ class ScanInputsSimpleController {
_populateSelect(select, hasOther) {
const modifierKeys = [
- {value: 'none', name: 'No key'},
- ...DocumentUtil.getModifierKeys(this._os).map(([value, name]) => ({value, name}))
+ {value: 'none', name: 'No key'}
];
+ for (const value of ['alt', 'ctrl', 'shift', 'meta']) {
+ const name = this._hotkeyUtil.getModifierDisplayValue(value);
+ modifierKeys.push({value, name});
+ }
if (hasOther) {
modifierKeys.push({value: 'other', name: 'Other'});