Add an 'other' option for when there is no matching main scanning input (#978)

This commit is contained in:
toasted-nutbread 2020-10-31 17:03:22 -04:00 committed by GitHub
parent f78e243853
commit 11c5dbac64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,6 +26,8 @@ class ScanInputsSimpleController {
this._settingsController = settingsController; this._settingsController = settingsController;
this._middleMouseButtonScan = null; this._middleMouseButtonScan = null;
this._mainScanModifierKeyInput = null; this._mainScanModifierKeyInput = null;
this._mainScanModifierKeyInputHasOther = false;
this._os = null;
} }
async prepare() { async prepare() {
@ -33,7 +35,10 @@ class ScanInputsSimpleController {
this._mainScanModifierKeyInput = document.querySelector('#main-scan-modifier-key'); this._mainScanModifierKeyInput = document.querySelector('#main-scan-modifier-key');
const {platform: {os}} = await api.getEnvironmentInfo(); const {platform: {os}} = await api.getEnvironmentInfo();
this._populateSelect(this._mainScanModifierKeyInput, os); this._os = os;
this._mainScanModifierKeyInputHasOther = false;
this._populateSelect(this._mainScanModifierKeyInput, this._mainScanModifierKeyInputHasOther);
const options = await this._settingsController.getOptions(); const options = await this._settingsController.getOptions();
@ -61,6 +66,7 @@ class ScanInputsSimpleController {
const {scanning: {inputs}} = options; const {scanning: {inputs}} = options;
const middleMouseSupportedIndex = this._getIndexOfMiddleMouseButtonScanInput(inputs); const middleMouseSupportedIndex = this._getIndexOfMiddleMouseButtonScanInput(inputs);
const mainScanInputIndex = this._getIndexOfMainScanInput(inputs); const mainScanInputIndex = this._getIndexOfMainScanInput(inputs);
const hasMainScanInput = (mainScanInputIndex >= 0);
let middleMouseSupported = false; let middleMouseSupported = false;
if (middleMouseSupportedIndex >= 0) { if (middleMouseSupportedIndex >= 0) {
@ -71,13 +77,17 @@ class ScanInputsSimpleController {
} }
let mainScanInput = 'none'; let mainScanInput = 'none';
if (mainScanInputIndex >= 0) { if (hasMainScanInput) {
const includeValues = this._splitValue(inputs[mainScanInputIndex].include); const includeValues = this._splitValue(inputs[mainScanInputIndex].include);
if (includeValues.length > 0) { if (includeValues.length > 0) {
mainScanInput = includeValues[0]; mainScanInput = includeValues[0];
} }
} else {
mainScanInput = 'other';
} }
this._setHasMainScanInput(hasMainScanInput);
this._middleMouseButtonScan.checked = middleMouseSupported; this._middleMouseButtonScan.checked = middleMouseSupported;
this._mainScanModifierKeyInput.value = mainScanInput; this._mainScanModifierKeyInput.value = mainScanInput;
} }
@ -89,16 +99,21 @@ class ScanInputsSimpleController {
_onMainScanModifierKeyInputChange(e) { _onMainScanModifierKeyInputChange(e) {
const mainScanKey = e.currentTarget.value; const mainScanKey = e.currentTarget.value;
if (mainScanKey === 'other') { return; }
const mainScanInputs = (mainScanKey === 'none' ? [] : [mainScanKey]); const mainScanInputs = (mainScanKey === 'none' ? [] : [mainScanKey]);
this._setMainScanInputs(mainScanInputs); this._setMainScanInputs(mainScanInputs);
} }
_populateSelect(select, os) { _populateSelect(select, hasOther) {
const modifierKeys = [ const modifierKeys = [
{value: 'none', name: 'None'}, {value: 'none', name: 'None'},
...DocumentUtil.getModifierKeys(os).map(([value, name]) => ({value, name})) ...DocumentUtil.getModifierKeys(this._os).map(([value, name]) => ({value, name}))
]; ];
if (hasOther) {
modifierKeys.push({value: 'other', name: 'Other'});
}
const fragment = document.createDocumentFragment(); const fragment = document.createDocumentFragment();
for (const {value, name} of modifierKeys) { for (const {value, name} of modifierKeys) {
const option = document.createElement('option'); const option = document.createElement('option');
@ -154,6 +169,8 @@ class ScanInputsSimpleController {
const {scanning: {inputs}} = options; const {scanning: {inputs}} = options;
const index = this._getIndexOfMainScanInput(inputs); const index = this._getIndexOfMainScanInput(inputs);
this._setHasMainScanInput(true);
if (index < 0) { if (index < 0) {
// Add new // Add new
const input = ScanInputsController.createDefaultMouseInput(value, 'mouse0'); const input = ScanInputsController.createDefaultMouseInput(value, 'mouse0');
@ -218,4 +235,10 @@ class ScanInputsSimpleController {
_isMouseInput(input) { _isMouseInput(input) {
return /^mouse\d+$/.test(input); return /^mouse\d+$/.test(input);
} }
_setHasMainScanInput(hasMainScanInput) {
if (this._mainScanModifierKeyInputHasOther !== hasMainScanInput) { return; }
this._mainScanModifierKeyInputHasOther = !hasMainScanInput;
this._populateSelect(this._mainScanModifierKeyInput, this._mainScanModifierKeyInputHasOther);
}
} }