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;
* Runs user commands.
*/
class Commands {
// ============= Static Methods ===============
static initialize() {
Commands.instance = new Commands();
}
// ============= Private Methods ===============
/** @private */
constructor() {
/**
......@@ -32,6 +24,10 @@ class Commands {
this.runCommand_.bind(this));
}
static initialize() {
Commands.instance = new Commands();
}
/**
* Run the function binding for the specified command.
* @param {!SwitchAccessCommand} command
......
......@@ -25,7 +25,7 @@ const EventHelper = {
* Sends a synthetic mouse event.
* @param {number} x
* @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.
*/
simulateMouseClick: (x, y, delayMs) => {
......@@ -37,7 +37,7 @@ const EventHelper = {
chrome.accessibilityPrivate.sendSyntheticMouseEvent({type, x, y});
};
if (delayMs) {
if (delayMs !== null) {
setTimeout(callback, delayMs);
} else {
callback();
......
......@@ -6,14 +6,6 @@
* Class to manage user preferences.
*/
class SwitchAccessPreferences {
// =============== Static Methods ==============
static initialize() {
SwitchAccessPreferences.instance = new SwitchAccessPreferences();
}
// =============== Private Methods ==============
/** @private */
constructor() {
/**
......@@ -26,6 +18,14 @@ class SwitchAccessPreferences {
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
* boolean or does not exist.
......@@ -67,6 +67,32 @@ class SwitchAccessPreferences {
(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.
* @param {!Array<chrome.settingsPrivate.PrefObject>} preferences
......@@ -76,7 +102,7 @@ class SwitchAccessPreferences {
updateFromSettings_(preferences, isFirstLoad = false) {
for (const pref of preferences) {
// Ignore preferences that are not used by Switch Access.
if (!Object.values(SAConstants.Preference).includes(pref.key)) {
if (!this.usesPreference_(pref)) {
continue;
}
......@@ -106,45 +132,17 @@ class SwitchAccessPreferences {
}
}
if (isFirstLoad) {
this.onInitialLoadComplete_();
}
}
/**
* Called when the preferences are finished loading for the first time.
* @private
*/
onInitialLoadComplete_() {
if (!this.settingsAreConfigured_()) {
if (isFirstLoad && !this.settingsAreConfigured_()) {
chrome.accessibilityPrivate.openSettingsSubpage(
'manageAccessibility/switchAccess');
}
}
/**
* Whether the current settings configuration is reasonably usable;
* specifically, whether there is a way to select and a way to navigate.
* @param {!chrome.settingsPrivate.PrefObject} pref
* @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;
usesPreference_(pref) {
return Object.values(SAConstants.Preference).includes(pref.key);
}
}
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