Commit c52bb558 authored by Anastasia Helfinstein's avatar Anastasia Helfinstein Committed by Commit Bot

[Switch Access] Reorder node methods

I've realized that the ordering of the functions in the node classes,
while it makes sense to me, is fairly arbitrary. Reorder the functions
to group by type (private/setters and getters/static/etc.) and
alphabetize within those groups.

This is a pure refactor. It changes no logic nor behavior, it only
reorders the functions in the file.

Bug: None
Change-Id: I38d75a90df01070a817eb8847b7121d220193183
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1902535
Commit-Queue: Anastasia Helfinstein <anastasi@google.com>
Reviewed-by: default avatarAkihiro Ota <akihiroota@chromium.org>
Cr-Commit-Position: refs/heads/master@{#715014}
parent 19d343dd
...@@ -21,14 +21,16 @@ class BackButtonNode extends SAChildNode { ...@@ -21,14 +21,16 @@ class BackButtonNode extends SAChildNode {
this.node_ = SwitchAccess.get().getBackButtonAutomationNode(); this.node_ = SwitchAccess.get().getBackButtonAutomationNode();
} }
// ================= Getters and setters =================
/** @override */ /** @override */
equals(other) { get actions() {
return other instanceof BackButtonNode; return [SAConstants.MenuAction.SELECT];
} }
/** @override */ /** @override */
get role() { get automationNode() {
return chrome.automation.RoleType.BUTTON; return this.node_;
} }
/** @override */ /** @override */
...@@ -39,25 +41,20 @@ class BackButtonNode extends SAChildNode { ...@@ -39,25 +41,20 @@ class BackButtonNode extends SAChildNode {
} }
/** @override */ /** @override */
get automationNode() { get role() {
return this.node_; return chrome.automation.RoleType.BUTTON;
} }
// ================= General methods =================
/** @override */ /** @override */
get actions() { asRootNode() {
return [SAConstants.MenuAction.SELECT]; return null;
} }
/** @override */ /** @override */
performAction(action) { equals(other) {
if (action !== SAConstants.MenuAction.SELECT) { return other instanceof BackButtonNode;
return false;
}
if (this.node_) {
this.node_.doDefault();
}
return true;
} }
/** @override */ /** @override */
...@@ -70,11 +67,6 @@ class BackButtonNode extends SAChildNode { ...@@ -70,11 +67,6 @@ class BackButtonNode extends SAChildNode {
return false; return false;
} }
/** @override */
asRootNode() {
return null;
}
/** @override */ /** @override */
onFocus() { onFocus() {
chrome.accessibilityPrivate.setSwitchAccessMenuState( chrome.accessibilityPrivate.setSwitchAccessMenuState(
...@@ -87,6 +79,20 @@ class BackButtonNode extends SAChildNode { ...@@ -87,6 +79,20 @@ class BackButtonNode extends SAChildNode {
false, RectHelper.ZERO_RECT, 0 /* num_actions */); false, RectHelper.ZERO_RECT, 0 /* num_actions */);
} }
/** @override */
performAction(action) {
if (action !== SAConstants.MenuAction.SELECT) {
return false;
}
if (this.node_) {
this.node_.doDefault();
}
return true;
}
// ================= Debug methods =================
/** @override */ /** @override */
debugString() { debugString() {
return 'BackButtonNode'; return 'BackButtonNode';
......
...@@ -21,27 +21,16 @@ class GroupNode extends SAChildNode { ...@@ -21,27 +21,16 @@ class GroupNode extends SAChildNode {
this.children_ = children; this.children_ = children;
} }
/** @override */ // ================= Getters and setters =================
equals(other) {
if (!(other instanceof GroupNode)) {
return false;
}
other = /** @type {GroupNode} */ (other); /** @override */
if (other.children_.length !== this.children_.length) { get actions() {
return false; return [];
}
for (let i = 0; i < this.children_.length; i++) {
if (other.children_[i].equals(this.children_[i])) {
return false;
}
}
return true;
} }
/** @override */ /** @override */
get role() { get automationNode() {
return chrome.automation.RoleType.GROUP; return null;
} }
/** @override */ /** @override */
...@@ -51,17 +40,42 @@ class GroupNode extends SAChildNode { ...@@ -51,17 +40,42 @@ class GroupNode extends SAChildNode {
} }
/** @override */ /** @override */
get automationNode() { get role() {
return null; return chrome.automation.RoleType.GROUP;
} }
// ================= General methods =================
/** @override */ /** @override */
get actions() { asRootNode() {
return []; const root = new SARootNode();
let children = [];
for (const child of this.children_) {
children.push(child);
}
children.push(new BackButtonNode(root));
root.children = children;
return root;
} }
/** @override */ /** @override */
performAction(action) { equals(other) {
if (!(other instanceof GroupNode)) {
return false;
}
other = /** @type {GroupNode} */ (other);
if (other.children_.length !== this.children_.length) {
return false;
}
for (let i = 0; i < this.children_.length; i++) {
if (other.children_[i].equals(this.children_[i])) {
return false;
}
}
return true; return true;
} }
...@@ -76,20 +90,12 @@ class GroupNode extends SAChildNode { ...@@ -76,20 +90,12 @@ class GroupNode extends SAChildNode {
} }
/** @override */ /** @override */
asRootNode() { performAction(action) {
const root = new SARootNode(); return true;
let children = [];
for (const child of this.children_) {
children.push(child);
}
children.push(new BackButtonNode(root));
root.children = children;
return root;
} }
// ================= Static methods =================
/** /**
* Assumes nodes are visually in rows. * Assumes nodes are visually in rows.
* @param {!Array<!SAChildNode>} nodes * @param {!Array<!SAChildNode>} nodes
......
...@@ -15,6 +15,8 @@ class KeyboardNode extends NodeWrapper { ...@@ -15,6 +15,8 @@ class KeyboardNode extends NodeWrapper {
super(node, parent); super(node, parent);
} }
// ================= Getters and setters =================
/** @override */ /** @override */
get actions() { get actions() {
if (this.isGroup()) { if (this.isGroup()) {
...@@ -23,6 +25,24 @@ class KeyboardNode extends NodeWrapper { ...@@ -23,6 +25,24 @@ class KeyboardNode extends NodeWrapper {
return [SAConstants.MenuAction.SELECT]; return [SAConstants.MenuAction.SELECT];
} }
// ================= General methods =================
/** @override */
asRootNode() {
if (!this.isGroup()) {
return null;
}
const node = this.automationNode;
if (!node) {
throw new TypeError('Keyboard nodes must have an automation node.');
}
const root = new RootNodeWrapper(node);
KeyboardNode.findAndSetChildren(root);
return root;
}
/** @override */ /** @override */
performAction(action) { performAction(action) {
if (this.isGroup()) { if (this.isGroup()) {
...@@ -45,21 +65,7 @@ class KeyboardNode extends NodeWrapper { ...@@ -45,21 +65,7 @@ class KeyboardNode extends NodeWrapper {
return true; return true;
} }
/** @override */ // ================= Static methods =================
asRootNode() {
if (!this.isGroup()) {
return null;
}
const node = this.automationNode;
if (!node) {
throw new TypeError('Keyboard nodes must have an automation node.');
}
const root = new RootNodeWrapper(node);
KeyboardNode.findAndSetChildren(root);
return root;
}
/** /**
* Helper function to connect tree elements, given the root node. * Helper function to connect tree elements, given the root node.
...@@ -93,11 +99,15 @@ class KeyboardRootNode extends RootNodeWrapper { ...@@ -93,11 +99,15 @@ class KeyboardRootNode extends RootNodeWrapper {
super(keyboard); super(keyboard);
} }
// ================= General methods =================
/** @override */ /** @override */
onExit() { onExit() {
chrome.accessibilityPrivate.setVirtualKeyboardVisible(false); chrome.accessibilityPrivate.setVirtualKeyboardVisible(false);
} }
// ================= Private methods =================
/** /**
* Custom logic when entering the node. * Custom logic when entering the node.
*/ */
...@@ -105,6 +115,8 @@ class KeyboardRootNode extends RootNodeWrapper { ...@@ -105,6 +115,8 @@ class KeyboardRootNode extends RootNodeWrapper {
chrome.accessibilityPrivate.setVirtualKeyboardVisible(true); chrome.accessibilityPrivate.setVirtualKeyboardVisible(true);
} }
// ================= Static methods =================
/** /**
* Creates the tree structure for the system menu. * Creates the tree structure for the system menu.
* @param {!chrome.automation.AutomationNode} desktop * @param {!chrome.automation.AutomationNode} desktop
......
...@@ -22,30 +22,7 @@ class NodeWrapper extends SAChildNode { ...@@ -22,30 +22,7 @@ class NodeWrapper extends SAChildNode {
this.isGroup_ = SwitchAccessPredicate.isGroup(this.baseNode_, parent); this.isGroup_ = SwitchAccessPredicate.isGroup(this.baseNode_, parent);
} }
/** @override */ // ================= Getters and setters =================
equals(other) {
if (!other || !(other instanceof NodeWrapper)) {
return false;
}
other = /** @type {!NodeWrapper} */ (other);
return other.baseNode_ === this.baseNode_;
}
/** @override */
get role() {
return this.baseNode_.role;
}
/** @override */
get location() {
return this.baseNode_.location;
}
/** @override */
get automationNode() {
return this.baseNode_;
}
/** @override */ /** @override */
get actions() { get actions() {
...@@ -79,6 +56,51 @@ class NodeWrapper extends SAChildNode { ...@@ -79,6 +56,51 @@ class NodeWrapper extends SAChildNode {
return actions.concat(standardActions); return actions.concat(standardActions);
} }
/** @override */
get automationNode() {
return this.baseNode_;
}
/** @override */
get location() {
return this.baseNode_.location;
}
/** @override */
get role() {
return this.baseNode_.role;
}
// ================= General methods =================
/** @override */
asRootNode() {
if (!this.isGroup()) {
return null;
}
return RootNodeWrapper.buildTree(this.baseNode_);
}
/** @override */
equals(other) {
if (!other || !(other instanceof NodeWrapper)) {
return false;
}
other = /** @type {!NodeWrapper} */ (other);
return other.baseNode_ === this.baseNode_;
}
/** @override */
isEquivalentTo(node) {
return this.baseNode_ === node;
}
/** @override */
isGroup() {
return this.isGroup_;
}
/** @override */ /** @override */
performAction(action) { performAction(action) {
let ancestor; let ancestor;
...@@ -125,6 +147,8 @@ class NodeWrapper extends SAChildNode { ...@@ -125,6 +147,8 @@ class NodeWrapper extends SAChildNode {
} }
} }
// ================= Private methods =================
/** /**
* @return {AutomationNode} * @return {AutomationNode}
* @protected * @protected
...@@ -135,24 +159,6 @@ class NodeWrapper extends SAChildNode { ...@@ -135,24 +159,6 @@ class NodeWrapper extends SAChildNode {
ancestor = ancestor.parent; ancestor = ancestor.parent;
return ancestor; return ancestor;
} }
/** @override */
isEquivalentTo(node) {
return this.baseNode_ === node;
}
/** @override */
isGroup() {
return this.isGroup_;
}
/** @override */
asRootNode() {
if (!this.isGroup()) {
return null;
}
return RootNodeWrapper.buildTree(this.baseNode_);
}
} }
/** /**
...@@ -170,6 +176,20 @@ class RootNodeWrapper extends SARootNode { ...@@ -170,6 +176,20 @@ class RootNodeWrapper extends SARootNode {
this.baseNode_ = baseNode; this.baseNode_ = baseNode;
} }
// ================= Getters and setters =================
/** @override */
get automationNode() {
return this.baseNode_;
}
/** @override */
get location() {
return this.baseNode_.location || super.location;
}
// ================= General methods =================
/** @override */ /** @override */
equals(other) { equals(other) {
if (!(other instanceof RootNodeWrapper)) { if (!(other instanceof RootNodeWrapper)) {
...@@ -181,8 +201,8 @@ class RootNodeWrapper extends SARootNode { ...@@ -181,8 +201,8 @@ class RootNodeWrapper extends SARootNode {
} }
/** @override */ /** @override */
get location() { isEquivalentTo(automationNode) {
return this.baseNode_.location || super.location; return this.baseNode_ === automationNode;
} }
/** @override */ /** @override */
...@@ -190,14 +210,27 @@ class RootNodeWrapper extends SARootNode { ...@@ -190,14 +210,27 @@ class RootNodeWrapper extends SARootNode {
return !!this.baseNode_.role; return !!this.baseNode_.role;
} }
/** @override */ // ================= Static methods =================
isEquivalentTo(automationNode) {
return this.baseNode_ === automationNode;
}
/** @override */ /**
get automationNode() { * @param {!AutomationNode} desktop
return this.baseNode_; * @return {!RootNodeWrapper}
*/
static buildDesktopTree(desktop) {
const root = new RootNodeWrapper(desktop);
const interestingChildren = RootNodeWrapper.getInterestingChildren(root);
if (interestingChildren.length < 1) {
throw SwitchAccess.error(
SAConstants.ErrorType.MALFORMED_DESKTOP,
'Desktop node must have at least 1 interesting child.');
}
const childConstructor = (autoNode) => new NodeWrapper(autoNode, root);
let children = interestingChildren.map(childConstructor);
root.children = children;
return root;
} }
/** /**
...@@ -232,27 +265,6 @@ class RootNodeWrapper extends SARootNode { ...@@ -232,27 +265,6 @@ class RootNodeWrapper extends SARootNode {
root.children = children; root.children = children;
} }
/**
* @param {!AutomationNode} desktop
* @return {!RootNodeWrapper}
*/
static buildDesktopTree(desktop) {
const root = new RootNodeWrapper(desktop);
const interestingChildren = RootNodeWrapper.getInterestingChildren(root);
if (interestingChildren.length < 1) {
throw SwitchAccess.error(
SAConstants.ErrorType.MALFORMED_DESKTOP,
'Desktop node must have at least 1 interesting child.');
}
const childConstructor = (autoNode) => new NodeWrapper(autoNode, root);
let children = interestingChildren.map(childConstructor);
root.children = children;
return root;
}
/** /**
* @param {!RootNodeWrapper} root * @param {!RootNodeWrapper} root
* @return {!Array<!AutomationNode>} * @return {!Array<!AutomationNode>}
......
...@@ -21,34 +21,14 @@ class SAChildNode { ...@@ -21,34 +21,14 @@ class SAChildNode {
this.next_ = null; this.next_ = null;
} }
/** @param {!SAChildNode} newVal */ // ================= Getters and setters =================
set previous(newVal) {
this.previous_ = newVal;
}
/** @param {!SAChildNode} newVal */
set next(newVal) {
this.next_ = newVal;
}
/** /**
* @param {SAChildNode} other * Returns a list of all the actions available for this node.
* @return {boolean} * @return {!Array<SAConstants.MenuAction>}
* @abstract
*/
equals(other) {}
/**
* @return {chrome.automation.RoleType|undefined}
* @abstract
*/
get role() {}
/**
* @return {chrome.accessibilityPrivate.ScreenRect|undefined}
* @abstract * @abstract
*/ */
get location() {} get actions() {}
/** /**
* Returns the underlying automation node, if one exists. * Returns the underlying automation node, if one exists.
...@@ -58,44 +38,16 @@ class SAChildNode { ...@@ -58,44 +38,16 @@ class SAChildNode {
get automationNode() {} get automationNode() {}
/** /**
* Returns whether this node should be displayed as a group. * @return {chrome.accessibilityPrivate.ScreenRect|undefined}
* @return {boolean}
* @abstract
*/
isGroup() {}
/**
* Returns a list of all the actions available for this node.
* @return {!Array<SAConstants.MenuAction>}
* @abstract * @abstract
*/ */
get actions() {} get location() {}
/** /** @param {!SAChildNode} newVal */
* Given a menu action, returns whether it can be performed on this node. set next(newVal) {
* @param {SAConstants.MenuAction} action this.next_ = newVal;
* @return {boolean}
*/
hasAction(action) {
return this.actions.includes(action);
} }
/**
* Performs the specified action on the node, if it is available.
* @param {SAConstants.MenuAction} action
* @return {boolean} Whether to close the menu. True if the menu should close,
* false otherwise.
* @abstract
*/
performAction(action) {}
/**
* @param {!chrome.automation.AutomationNode} node
* @return {boolean}
* @abstract
*/
isEquivalentTo(node) {}
/** /**
* Returns the next node in pre-order traversal. * Returns the next node in pre-order traversal.
* @return {!SAChildNode} * @return {!SAChildNode}
...@@ -109,6 +61,11 @@ class SAChildNode { ...@@ -109,6 +61,11 @@ class SAChildNode {
return this.next_; return this.next_;
} }
/** @param {!SAChildNode} newVal */
set previous(newVal) {
this.previous_ = newVal;
}
/** /**
* Returns the previous node in pre-order traversal. * Returns the previous node in pre-order traversal.
* @return {!SAChildNode} * @return {!SAChildNode}
...@@ -122,6 +79,14 @@ class SAChildNode { ...@@ -122,6 +79,14 @@ class SAChildNode {
return this.previous_; return this.previous_;
} }
/**
* @return {chrome.automation.RoleType|undefined}
* @abstract
*/
get role() {}
// ================= General methods =================
/** /**
* If this node is a group, returns the analogous SARootNode. * If this node is a group, returns the analogous SARootNode.
* @return {SARootNode} * @return {SARootNode}
...@@ -129,6 +94,36 @@ class SAChildNode { ...@@ -129,6 +94,36 @@ class SAChildNode {
*/ */
asRootNode() {} asRootNode() {}
/**
* @param {SAChildNode} other
* @return {boolean}
* @abstract
*/
equals(other) {}
/**
* Given a menu action, returns whether it can be performed on this node.
* @param {SAConstants.MenuAction} action
* @return {boolean}
*/
hasAction(action) {
return this.actions.includes(action);
}
/**
* @param {!chrome.automation.AutomationNode} node
* @return {boolean}
* @abstract
*/
isEquivalentTo(node) {}
/**
* Returns whether this node should be displayed as a group.
* @return {boolean}
* @abstract
*/
isGroup() {}
/** /**
* Called when this node becomes the primary highlighted node. * Called when this node becomes the primary highlighted node.
*/ */
...@@ -139,6 +134,17 @@ class SAChildNode { ...@@ -139,6 +134,17 @@ class SAChildNode {
*/ */
onUnfocus() {} onUnfocus() {}
/**
* Performs the specified action on the node, if it is available.
* @param {SAConstants.MenuAction} action
* @return {boolean} Whether to close the menu. True if the menu should close,
* false otherwise.
* @abstract
*/
performAction(action) {}
// ================= Debug methods =================
/** /**
* String-ifies the node (for debugging purposes). * String-ifies the node (for debugging purposes).
* @param {boolean} wholeTree Whether to recursively include descendants. * @param {boolean} wholeTree Whether to recursively include descendants.
...@@ -181,36 +187,17 @@ class SARootNode { ...@@ -181,36 +187,17 @@ class SARootNode {
this.children_ = []; this.children_ = [];
} }
// ================= Getters and setters =================
/** @return {chrome.automation.AutomationNode} */
get automationNode() {}
/** @param {!Array<!SAChildNode>} newVal */ /** @param {!Array<!SAChildNode>} newVal */
set children(newVal) { set children(newVal) {
this.children_ = newVal; this.children_ = newVal;
this.connectChildren_(); this.connectChildren_();
} }
/**
* @param {SARootNode} other
* @return {boolean}
*/
equals(other) {
if (!other) {
return false;
}
if (this.children_.length !== other.children_.length) {
return false;
}
let result = true;
for (let i = 0; i < this.children_.length; i++) {
if (!this.children_[i]) {
throw SwitchAccess.error(
SAConstants.ErrorType.NULL_CHILD, 'Child cannot be null.');
}
result = result && this.children_[i].equals(other.children_[i]);
}
return result;
}
/** @return {!Array<!SAChildNode>} */ /** @return {!Array<!SAChildNode>} */
get children() { get children() {
return this.children_; return this.children_;
...@@ -238,11 +225,6 @@ class SARootNode { ...@@ -238,11 +225,6 @@ class SARootNode {
} }
} }
/** @return {boolean} */
isValid() {
return true;
}
/** @return {!chrome.accessibilityPrivate.ScreenRect} */ /** @return {!chrome.accessibilityPrivate.ScreenRect} */
get location() { get location() {
let children = this.children_.filter((c) => !(c instanceof BackButtonNode)); let children = this.children_.filter((c) => !(c instanceof BackButtonNode));
...@@ -250,6 +232,32 @@ class SARootNode { ...@@ -250,6 +232,32 @@ class SARootNode {
return RectHelper.unionAll(childLocations); return RectHelper.unionAll(childLocations);
} }
// ================= General methods =================
/**
* @param {SARootNode} other
* @return {boolean}
*/
equals(other) {
if (!other) {
return false;
}
if (this.children_.length !== other.children_.length) {
return false;
}
let result = true;
for (let i = 0; i < this.children_.length; i++) {
if (!this.children_[i]) {
throw SwitchAccess.error(
SAConstants.ErrorType.NULL_CHILD, 'Child cannot be null.');
}
result = result && this.children_[i].equals(other.children_[i]);
}
return result;
}
/** /**
* @param {chrome.automation.AutomationNode} automationNode * @param {chrome.automation.AutomationNode} automationNode
* @return {boolean} * @return {boolean}
...@@ -258,12 +266,16 @@ class SARootNode { ...@@ -258,12 +266,16 @@ class SARootNode {
return false; return false;
} }
/** @return {chrome.automation.AutomationNode} */ /** @return {boolean} */
get automationNode() {} isValid() {
return true;
}
/** Called when a group is exiting. */ /** Called when a group is exiting. */
onExit() {} onExit() {}
// ================= Debug methods =================
/** /**
* String-ifies the node (for debugging purposes). * String-ifies the node (for debugging purposes).
* @param {boolean=} wholeTree Whether to recursively descend the tree * @param {boolean=} wholeTree Whether to recursively descend the tree
...@@ -295,6 +307,8 @@ class SARootNode { ...@@ -295,6 +307,8 @@ class SARootNode {
return str; return str;
} }
// ================= Private methods =================
/** /**
* Helper function to connect children. * Helper function to connect children.
* @private * @private
......
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