Commit 94d3ced1 authored by Anastasia Helfinstein's avatar Anastasia Helfinstein Committed by Commit Bot

[Switch Access] Refactor Commands, Preferences, and EventHelper classes

Refactors three Switch Access classes for clarity, consistency, and
readability.

Bug: None
Change-Id: Ib81a279cef24348f997f870f208413245a8c2357
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2064790Reviewed-by: default avatarAkihiro Ota <akihiroota@chromium.org>
Commit-Queue: Anastasia Helfinstein <anastasi@google.com>
Cr-Commit-Position: refs/heads/master@{#744045}
parent 39717d27
...@@ -8,14 +8,6 @@ const SwitchAccessCommand = chrome.accessibilityPrivate.SwitchAccessCommand; ...@@ -8,14 +8,6 @@ const SwitchAccessCommand = chrome.accessibilityPrivate.SwitchAccessCommand;
* Runs user commands. * Runs user commands.
*/ */
class Commands { class Commands {
// ============= Static Methods ===============
static initialize() {
Commands.instance = new Commands();
}
// ============= Private Methods ===============
/** @private */ /** @private */
constructor() { constructor() {
/** /**
...@@ -32,6 +24,10 @@ class Commands { ...@@ -32,6 +24,10 @@ class Commands {
this.runCommand_.bind(this)); this.runCommand_.bind(this));
} }
static initialize() {
Commands.instance = new Commands();
}
/** /**
* Run the function binding for the specified command. * Run the function binding for the specified command.
* @param {!SwitchAccessCommand} command * @param {!SwitchAccessCommand} command
......
...@@ -25,7 +25,7 @@ const EventHelper = { ...@@ -25,7 +25,7 @@ const EventHelper = {
* Sends a synthetic mouse event. * Sends a synthetic mouse event.
* @param {number} x * @param {number} x
* @param {number} y * @param {number} y
* @param {number=} delayMs The delay between mouse press and mouse release, * @param {?number} delayMs The delay between mouse press and mouse release,
* in milliseconds. * in milliseconds.
*/ */
simulateMouseClick: (x, y, delayMs) => { simulateMouseClick: (x, y, delayMs) => {
...@@ -37,7 +37,7 @@ const EventHelper = { ...@@ -37,7 +37,7 @@ const EventHelper = {
chrome.accessibilityPrivate.sendSyntheticMouseEvent({type, x, y}); chrome.accessibilityPrivate.sendSyntheticMouseEvent({type, x, y});
}; };
if (delayMs) { if (delayMs !== null) {
setTimeout(callback, delayMs); setTimeout(callback, delayMs);
} else { } else {
callback(); callback();
......
...@@ -6,14 +6,6 @@ ...@@ -6,14 +6,6 @@
* Class to manage user preferences. * Class to manage user preferences.
*/ */
class SwitchAccessPreferences { class SwitchAccessPreferences {
// =============== Static Methods ==============
static initialize() {
SwitchAccessPreferences.instance = new SwitchAccessPreferences();
}
// =============== Private Methods ==============
/** @private */ /** @private */
constructor() { constructor() {
/** /**
...@@ -26,6 +18,14 @@ class SwitchAccessPreferences { ...@@ -26,6 +18,14 @@ class SwitchAccessPreferences {
this.init_(); this.init_();
} }
// =============== Static Methods ==============
static initialize() {
SwitchAccessPreferences.instance = new SwitchAccessPreferences();
}
// =============== Private Methods ==============
/** /**
* Get the boolean value for the given name, or |null| if the value is not a * Get the boolean value for the given name, or |null| if the value is not a
* boolean or does not exist. * boolean or does not exist.
...@@ -67,6 +67,32 @@ class SwitchAccessPreferences { ...@@ -67,6 +67,32 @@ class SwitchAccessPreferences {
(prefs) => this.updateFromSettings_(prefs, true /* isFirstLoad */)); (prefs) => this.updateFromSettings_(prefs, true /* isFirstLoad */));
} }
/**
* Whether the current settings configuration is reasonably usable;
* specifically, whether there is a way to select and a way to navigate.
* @return {boolean}
* @private
*/
settingsAreConfigured_() {
const selectSetting =
this.getNumber_(SAConstants.Preference.SELECT_SETTING);
const nextSetting = this.getNumber_(SAConstants.Preference.NEXT_SETTING);
const previousSetting =
this.getNumber_(SAConstants.Preference.PREVIOUS_SETTING);
const autoScanEnabled =
!!this.getBoolean_(SAConstants.Preference.AUTO_SCAN_ENABLED);
if (!selectSetting) {
return false;
}
if (nextSetting || previousSetting) {
return true;
}
return autoScanEnabled;
}
/** /**
* Updates the cached preferences. * Updates the cached preferences.
* @param {!Array<chrome.settingsPrivate.PrefObject>} preferences * @param {!Array<chrome.settingsPrivate.PrefObject>} preferences
...@@ -76,7 +102,7 @@ class SwitchAccessPreferences { ...@@ -76,7 +102,7 @@ class SwitchAccessPreferences {
updateFromSettings_(preferences, isFirstLoad = false) { updateFromSettings_(preferences, isFirstLoad = false) {
for (const pref of preferences) { for (const pref of preferences) {
// Ignore preferences that are not used by Switch Access. // Ignore preferences that are not used by Switch Access.
if (!Object.values(SAConstants.Preference).includes(pref.key)) { if (!this.usesPreference_(pref)) {
continue; continue;
} }
...@@ -106,45 +132,17 @@ class SwitchAccessPreferences { ...@@ -106,45 +132,17 @@ class SwitchAccessPreferences {
} }
} }
if (isFirstLoad) { if (isFirstLoad && !this.settingsAreConfigured_()) {
this.onInitialLoadComplete_();
}
}
/**
* Called when the preferences are finished loading for the first time.
* @private
*/
onInitialLoadComplete_() {
if (!this.settingsAreConfigured_()) {
chrome.accessibilityPrivate.openSettingsSubpage( chrome.accessibilityPrivate.openSettingsSubpage(
'manageAccessibility/switchAccess'); 'manageAccessibility/switchAccess');
} }
} }
/** /**
* Whether the current settings configuration is reasonably usable; * @param {!chrome.settingsPrivate.PrefObject} pref
* specifically, whether there is a way to select and a way to navigate.
* @return {boolean} * @return {boolean}
* @private
*/ */
settingsAreConfigured_() { usesPreference_(pref) {
const selectSetting = return Object.values(SAConstants.Preference).includes(pref.key);
this.getNumber_(SAConstants.Preference.SELECT_SETTING);
const nextSetting = this.getNumber_(SAConstants.Preference.NEXT_SETTING);
const previousSetting =
this.getNumber_(SAConstants.Preference.PREVIOUS_SETTING);
const autoScanEnabled =
!!this.getBoolean_(SAConstants.Preference.AUTO_SCAN_ENABLED);
if (!selectSetting) {
return false;
}
if (nextSetting || previousSetting) {
return true;
}
return autoScanEnabled;
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment