Index: ui/display/win/scaling_util.cc |
diff --git a/ui/display/win/scaling_util.cc b/ui/display/win/scaling_util.cc |
index 54fd7e5fa60b29dad08c439d50bbaa6ac642a5c0..6e0e41c85c2a2d85a8cff0cd400752538a17858a 100644 |
--- a/ui/display/win/scaling_util.cc |
+++ b/ui/display/win/scaling_util.cc |
@@ -14,6 +14,60 @@ |
namespace { |
+enum class RelativePosition { |
+ BOTTOM = 0, |
+ LEFT = 1, |
+ TOP = 2, |
+ RIGHT = 3, |
+}; |
+ |
+// Returns the relative position of |test| with respect to |ref|. |
+// If |test| is below |ref|, then the return value is RelativePosition::BOTTOM. |
+// The precedence of relative position in order of highest to lowest is |
+// BOTTOM, LEFT, TOP, and finally RIGHT. |
+// In other words, if |test| is both below and to the left of |ref|, then the |
+// RelativePosition is BOTTOM. |
+RelativePosition RectRelativePosition(const gfx::Rect& ref, |
+ const gfx::Rect& test) { |
+ if (ref.bottom() <= test.y()) |
+ return RelativePosition::BOTTOM; |
+ if (test.right() <= ref.x()) |
+ return RelativePosition::LEFT; |
+ if (test.bottom() <= ref.y()) |
+ return RelativePosition::TOP; |
+ |
+ return RelativePosition::RIGHT; |
+} |
+ |
+gfx::Rect CoordinateRotateRectangle90(const gfx::Rect& rect) { |
+ return gfx::Rect(rect.y(), -rect.x() - rect.width(), |
+ rect.height(), rect.width()); |
+} |
+ |
+gfx::Rect CoordinateRotateRectangle180(const gfx::Rect& rect) { |
+ return gfx::Rect(-rect.x() - rect.width(), -rect.y() -rect.height(), |
+ rect.width(), rect.height()); |
+} |
+ |
+gfx::Rect CoordinateRotateRectangle270(const gfx::Rect& rect) { |
+ return gfx::Rect(-rect.y() - rect.height(), rect.x(), |
+ rect.height(), rect.width()); |
+} |
+ |
+gfx::Rect CanonicalizeRelativePosition(const gfx::Rect& rect, |
+ RelativePosition relative_position) { |
+ switch (relative_position) { |
+ case RelativePosition::LEFT: |
+ return CoordinateRotateRectangle90(rect); |
+ case RelativePosition::TOP: |
+ return CoordinateRotateRectangle180(rect); |
+ case RelativePosition::RIGHT: |
+ return CoordinateRotateRectangle270(rect); |
+ default: |
+ return rect; |
+ } |
+} |
+ |
bool InRange(int target, int lower_bound, int upper_bound) { |
return lower_bound <= target && target <= upper_bound; |
} |
@@ -169,5 +223,27 @@ DisplayPlacement CalculateDisplayPlacement(const DisplayInfo& parent, |
return placement; |
} |
+// This function rotates the rectangles so that ref is always on top of rect, |
+// allowing the function to concentrate on comparing |ref|'s bottom corners and |
+// |rect|'s top corners when the rects don't overlap vertically. |
+int64_t SquaredDistanceBetweenRects(const gfx::Rect& ref, |
+ const gfx::Rect& rect) { |
+ if (ref.Intersects(rect)) |
+ return 0; |
+ |
+ RelativePosition relative_position = RectRelativePosition(ref, rect); |
+ gfx::Rect ref_work(CanonicalizeRelativePosition(ref, relative_position)); |
+ gfx::Rect rect_work(CanonicalizeRelativePosition(rect, relative_position)); |
oshima
2016/05/26 21:52:57
Just naming suggestion. how about:
Rotation degre
robliao
2016/05/26 22:28:47
I want to avoid using display::Rotation as that se
oshima
2016/05/26 22:54:47
I didn't mean to suggest display::Rotation. Sorry
|
+ // Now that the ref is on top, we can concentrate ref bottom |
+ // and rect top calculations. |
+ if (rect_work.right() < ref_work.x()) |
+ return (rect_work.top_right() - ref_work.bottom_left()).LengthSquared(); |
+ else if (ref_work.right() < rect_work.x()) |
+ return (rect_work.origin() - ref_work.bottom_right()).LengthSquared(); |
+ |
+ int distance = rect_work.y() - ref_work.bottom(); |
+ return distance * distance; |
+} |
+ |
} // namespace win |
} // namespace display |