Commit 44c931f3 authored by Anastasia Helfinstein's avatar Anastasia Helfinstein Committed by Commit Bot

[Switch Access] Reorder functions in rect_helper.js

This is a pure refactor, with no change in logic or behavior. It groups
functions by simplicity, and alphabetizes within those groups.

Bug: None
Change-Id: I5eda83d75e50a30ed8ae5e7c2c3ef1444d20601e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1891015
Commit-Queue: Anastasia Helfinstein <anastasi@google.com>
Reviewed-by: default avatarChris Hall <chrishall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#711815}
parent 280a2c85
......@@ -8,18 +8,27 @@ const RectHelper = {
ZERO_RECT: {top: 0, left: 0, width: 0, height: 0},
/**
* Finds the bottom of a rect.
* @param {!chrome.accessibilityPrivate.ScreenRect} rect
* @return {number}
* Returns true if the two rects are equal.
*
* @param {chrome.accessibilityPrivate.ScreenRect=} rect1
* @param {chrome.accessibilityPrivate.ScreenRect=} rect2
* @return {boolean}
*/
bottom: (rect) => rect.top + rect.height,
areEqual: (rect1, rect2) => {
if (!rect1 && !rect2)
return true;
if (!rect1 || !rect2)
return false;
return rect1.left === rect2.left && rect1.top === rect2.top &&
rect1.width === rect2.width && rect1.height === rect2.height;
},
/**
* Finds the right of a rect.
* Finds the bottom of a rect.
* @param {!chrome.accessibilityPrivate.ScreenRect} rect
* @return {number}
*/
right: (rect) => rect.left + rect.width,
bottom: (rect) => rect.top + rect.height,
/**
* Returns the point at the center of the rectangle.
......@@ -34,83 +43,12 @@ const RectHelper = {
},
/**
* Returns the union of the specified rectangles.
* @param {!chrome.accessibilityPrivate.ScreenRect} rect1
* @param {!chrome.accessibilityPrivate.ScreenRect} rect2
* @return {!chrome.accessibilityPrivate.ScreenRect}
*/
union: (rect1, rect2) => {
const top = rect1.top < rect2.top ? rect1.top : rect2.top;
const left = rect1.left < rect2.left ? rect1.left : rect2.left;
const r1Bottom = RectHelper.bottom(rect1);
const r2Bottom = RectHelper.bottom(rect2);
const bottom = r1Bottom > r2Bottom ? r1Bottom : r2Bottom;
const r1Right = RectHelper.right(rect1);
const r2Right = RectHelper.right(rect2);
const right = r1Right > r2Right ? r1Right : r2Right;
const height = bottom - top;
const width = right - left;
return {top, left, width, height};
},
/**
* Returns the union of all the rectangles specified.
* @param {!Array<!chrome.accessibilityPrivate.ScreenRect>} rects
* @param {!chrome.accessibilityPrivate.ScreenRect} rect
* @return {!chrome.accessibilityPrivate.ScreenRect}
*/
unionAll: (rects) => {
if (rects.length < 1) return RectHelper.ZERO_RECT;
let result = rects[0];
for (let i = 1; i < rects.length; i++) {
result = RectHelper.union(result, rects[i]);
}
return result;
},
/**
* Returns true if the two rects are equal.
*
* @param {chrome.accessibilityPrivate.ScreenRect=} rect1
* @param {chrome.accessibilityPrivate.ScreenRect=} rect2
* @return {boolean}
*/
areEqual: (rect1, rect2) => {
if (!rect1 && !rect2) return true;
if (!rect1 || !rect2) return false;
return rect1.left === rect2.left && rect1.top === rect2.top &&
rect1.width === rect2.width && rect1.height === rect2.height;
},
/**
* Returns a string representing the given rectangle.
* @param {chrome.accessibilityPrivate.ScreenRect|undefined} rect
* @return {string}
*/
toString: (rect) => {
let str = '';
if (rect) {
str = rect.left + ',' + rect.top + ' ';
str += rect.width + 'x' + rect.height;
}
return str;
},
/*
* @param {chrome.accessibilityPrivate.ScreenRect=} rect1
* @param {chrome.accessibilityPrivate.ScreenRect=} rect2
*/
sameRow: (rect1, rect2) => {
if (!rect1 || !rect2) return false;
const halfHeight = Math.round(rect1.height / 2);
const topBound = rect1.top - halfHeight;
const bottomBound = rect1.top + halfHeight;
return topBound <= rect2.top && bottomBound >= rect2.top;
deepCopy: (rect) => {
const copy = (Object.assign({}, rect));
return /** @type {!chrome.accessibilityPrivate.ScreenRect} */ (copy);
},
/**
......@@ -148,12 +86,77 @@ const RectHelper = {
},
/**
* Finds the right of a rect.
* @param {!chrome.accessibilityPrivate.ScreenRect} rect
* @return {number}
*/
right: (rect) => rect.left + rect.width,
/*
* @param {chrome.accessibilityPrivate.ScreenRect=} rect1
* @param {chrome.accessibilityPrivate.ScreenRect=} rect2
*/
sameRow: (rect1, rect2) => {
if (!rect1 || !rect2)
return false;
const halfHeight = Math.round(rect1.height / 2);
const topBound = rect1.top - halfHeight;
const bottomBound = rect1.top + halfHeight;
return topBound <= rect2.top && bottomBound >= rect2.top;
},
/**
* Returns a string representing the given rectangle.
* @param {chrome.accessibilityPrivate.ScreenRect|undefined} rect
* @return {string}
*/
toString: (rect) => {
let str = '';
if (rect) {
str = rect.left + ',' + rect.top + ' ';
str += rect.width + 'x' + rect.height;
}
return str;
},
/**
* Returns the union of the specified rectangles.
* @param {!chrome.accessibilityPrivate.ScreenRect} rect1
* @param {!chrome.accessibilityPrivate.ScreenRect} rect2
* @return {!chrome.accessibilityPrivate.ScreenRect}
*/
deepCopy: (rect) => {
const copy = (Object.assign({}, rect));
return /** @type {!chrome.accessibilityPrivate.ScreenRect} */ (copy);
}
union: (rect1, rect2) => {
const top = rect1.top < rect2.top ? rect1.top : rect2.top;
const left = rect1.left < rect2.left ? rect1.left : rect2.left;
const r1Bottom = RectHelper.bottom(rect1);
const r2Bottom = RectHelper.bottom(rect2);
const bottom = r1Bottom > r2Bottom ? r1Bottom : r2Bottom;
const r1Right = RectHelper.right(rect1);
const r2Right = RectHelper.right(rect2);
const right = r1Right > r2Right ? r1Right : r2Right;
const height = bottom - top;
const width = right - left;
return {top, left, width, height};
},
/**
* Returns the union of all the rectangles specified.
* @param {!Array<!chrome.accessibilityPrivate.ScreenRect>} rects
* @return {!chrome.accessibilityPrivate.ScreenRect}
*/
unionAll: (rects) => {
if (rects.length < 1)
return RectHelper.ZERO_RECT;
let result = rects[0];
for (let i = 1; i < rects.length; i++) {
result = RectHelper.union(result, rects[i]);
}
return result;
}
};
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