Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Unified Diff: ui/gfx/win/rect_util.cc

Issue 1426933002: Refactor Windows DPI Point, Rect, and Size for Multiple Monitor DPI Awareness (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed Other Unit Tests - Moved Inner Classes Outside Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/gfx/win/rect_util.cc
diff --git a/ui/gfx/win/rect_util.cc b/ui/gfx/win/rect_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..99b2b95d9ebc84dde7ec1cb5104d59d53e3d897c
--- /dev/null
+++ b/ui/gfx/win/rect_util.cc
@@ -0,0 +1,250 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/gfx/win/rect_util.h"
+
+#include "ui/gfx/geometry/rect.h"
+#include "ui/gfx/geometry/safe_integer_conversions.h"
+#include "ui/gfx/geometry/size.h"
+#include "ui/gfx/geometry/vector2d.h"
+
+namespace {
+
+enum class RelativePosition {
+ BOTTOM = 0,
+ LEFT = 1,
+ TOP = 2,
+ RIGHT = 3,
+};
+
+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 CoordinateReflectRectagleYAxis(const gfx::Rect& rect) {
+ return gfx::Rect(-rect.x() - rect.width(), rect.y(),
+ rect.width(), rect.height());
+}
+
+gfx::Rect CanonicalizeRelativePosition(gfx::Rect rect,
+ RelativePosition relative_position) {
+ switch (relative_position) {
+ case RelativePosition::LEFT:
+ rect = CoordinateRotateRectangle90(rect);
+ break;
+ case RelativePosition::TOP:
+ rect = CoordinateRotateRectangle180(rect);
+ break;
+ case RelativePosition::RIGHT:
+ rect = CoordinateRotateRectangle270(rect);
+ break;
+ }
+ return rect;
+}
+
+gfx::Rect CanonicalizeTouchingEdge(gfx::Rect rect,
+ gfx::win::RectEdge edge) {
+ switch (edge) {
+ case gfx::win::RectEdge::LEFT:
+ rect = CoordinateRotateRectangle90(rect);
+ break;
+ case gfx::win::RectEdge::TOP:
+ rect = CoordinateRotateRectangle180(rect);
+ // Helps prefer left and top alignment.
+ rect = CoordinateReflectRectagleYAxis(rect);
+ break;
+ case gfx::win::RectEdge::RIGHT:
+ rect = CoordinateRotateRectangle270(rect);
+ // Helps prefer left and top alignment.
+ rect = CoordinateReflectRectagleYAxis(rect);
+ break;
+ }
+ return rect;
+}
+
+gfx::Rect RevertToOriginalEdge(gfx::Rect rect, gfx::win::RectEdge edge) {
+ switch (edge) {
+ case gfx::win::RectEdge::LEFT:
+ rect = CoordinateRotateRectangle270(rect);
+ break;
+ case gfx::win::RectEdge::TOP:
+ rect = CoordinateReflectRectagleYAxis(rect);
+ rect = CoordinateRotateRectangle180(rect);
+ break;
+ case gfx::win::RectEdge::RIGHT:
+ rect = CoordinateReflectRectagleYAxis(rect);
+ rect = CoordinateRotateRectangle90(rect);
+ break;
+ }
+ return rect;
+}
+
+bool InRange(int target, int lower_bound, int upper_bound) {
+ return lower_bound <= target && target <= upper_bound;
+}
+
+int ScaleValue(int new_min, int new_max, int old_min, int old_max, int val) {
+ int old_delta = old_max - old_min;
+ if (old_delta == 0) {
+ DCHECK(new_min == new_max);
+ return new_min;
+ }
+ float percent = (float)(val - old_min) / (float)old_delta;
+ return new_min + gfx::ToFlooredInt(percent * (float)(new_max - new_min));
+}
+
+RelativePosition RectRelativePosition(gfx::Rect ref, gfx::Rect test) {
+ int i = 0;
+ for (; i <= 3; ++i) {
+ if (ref.bottom() <= test.y()) {
+ break;
+ } else {
+ ref = CoordinateRotateRectangle90(ref);
+ test = CoordinateRotateRectangle90(test);
+ }
+ }
+ switch (i) {
+ case 0:
+ return RelativePosition::BOTTOM;
+ case 1:
+ return RelativePosition::LEFT;
+ case 2:
+ return RelativePosition::TOP;
+ }
+ DCHECK(i == 3);
+ return RelativePosition::RIGHT;
+}
+
+} // namespace
+
+namespace gfx {
+namespace win {
+
+RectEdge RectTouch(gfx::Rect ref, gfx::Rect test) {
+ for (int i = 0; i <= 3; ++i) {
+ if (ref.bottom() == test.y() &&
+ (InRange(test.x(), ref.x(), ref.right()) ||
+ InRange(test.right(), ref.x(), ref.right()) ||
+ InRange(ref.x(), test.x(), test.right()) ||
+ InRange(ref.right(), test.x(), test.right()))) {
+ switch (i) {
+ case 0:
+ return RectEdge::BOTTOM;
+ case 1:
+ return RectEdge::LEFT;
+ case 2:
+ return RectEdge::TOP;
+ case 3:
+ return RectEdge::RIGHT;
+ }
+ } else {
+ ref = CoordinateRotateRectangle90(ref);
+ test = CoordinateRotateRectangle90(test);
+ }
+ }
+ return RectEdge::NONE;
+}
+
+// Writing code that specifically deals with scaling each rect is tedious and
+// error prone. Instead, this function transforms the positions of the
+// two rects so that |ref_scaled_rect| is always on top of |ref_original_rect|,
+// calculates the scaled and positioned target rect, and then reverses the
+// transforms. As a result, the position logic can be written in terms of the
+// bottom of the |ref_original_rect| instead of the bottom, left, top, and
+// right.
+gfx::Rect ScaleAndPositionRect(gfx::Rect ref_scaled_rect,
+ gfx::Rect ref_original_rect,
+ gfx::Rect target_rect,
+ float target_scale_factor) {
+ RectEdge orig_edge = RectTouch(ref_original_rect, target_rect);
+ gfx::Rect scaled_rect(gfx::ScaleToFlooredSize(target_rect.size(),
+ 1.0f / target_scale_factor));
+ if (orig_edge == RectEdge::NONE) {
+ // Currently, we expect all rectangles to touch each other.
+ NOTREACHED();
+ scaled_rect.set_origin(target_rect.origin());
+ }
+
+ // Transform rectangles so we can simply deal with bottom edge sharing.
+ ref_scaled_rect = CanonicalizeTouchingEdge(ref_scaled_rect, orig_edge);
+ ref_original_rect = CanonicalizeTouchingEdge(ref_original_rect, orig_edge);
+ target_rect = CanonicalizeTouchingEdge(target_rect, orig_edge);
+ scaled_rect = CanonicalizeTouchingEdge(scaled_rect, orig_edge);
+
+ // Position the rect.
+ scaled_rect.set_y(ref_scaled_rect.bottom());
+ int x;
+ if (target_rect.right() == ref_original_rect.right()) {
+ // Maintain right alignment. If the rectangle was left-aligned, the next
+ // case will catch that.
+ x = ref_scaled_rect.right() - scaled_rect.width();
+ } else if (InRange(target_rect.x(),
+ ref_original_rect.x(),
+ ref_original_rect.right())) {
+ // Position using the left point relative to the reference rect.
+ x = ScaleValue(ref_scaled_rect.x(), ref_scaled_rect.right(),
+ ref_original_rect.x(), ref_original_rect.right(),
+ target_rect.x());
+ } else if (InRange(target_rect.right(),
+ ref_original_rect.x(),
+ ref_original_rect.right())) {
+ // Position using the right point relative to the reference rect.
+ x = ScaleValue(ref_scaled_rect.x(), ref_scaled_rect.right(),
+ ref_original_rect.x(), ref_original_rect.right(),
+ target_rect.right()) - scaled_rect.width();
+ } else if (InRange(ref_scaled_rect.x(),
+ target_rect.x(),
+ target_rect.right())) {
+ // Position relative to the left point of the reference rect.
+ int offset = ScaleValue(0, scaled_rect.width(),
+ 0, target_rect.width(),
+ ref_original_rect.x() - target_rect.x());
+ x = ref_scaled_rect.x() - offset;
+ } else {
+ // Position relative to the right point of the reference rect.
+ int offset = ScaleValue(0, scaled_rect.width(),
+ 0, target_rect.width(),
+ ref_original_rect.right() - target_rect.x());
+ x = ref_scaled_rect.x() - offset - scaled_rect.width();
+ }
+ scaled_rect.set_x(x);
+ DCHECK(RectTouch(ref_scaled_rect, scaled_rect) == RectEdge::BOTTOM);
+
+ // Reverse the transformation.
+ return RevertToOriginalEdge(scaled_rect, orig_edge);
+}
+
+// This function takes the same approach as ScaleAndPositionRect, transforming
+// the two input rects so that the relative positions are always the same.
+int64_t SquaredDistanceBetweenRects(gfx::Rect ref, gfx::Rect rect) {
+ if (ref.Intersects(rect))
+ return 0;
+
+ RelativePosition relative_position = RectRelativePosition(ref, rect);
+ ref = CanonicalizeRelativePosition(ref, relative_position);
+ rect = CanonicalizeRelativePosition(rect, relative_position);
+ // Now that the ref is on top, we can concentrate ref bottom
+ // and rect top calculations.
+ if (rect.right() < ref.x())
+ return (rect.top_right() - ref.bottom_left()).LengthSquared();
+ else if (ref.right() < rect.x())
+ return (rect.origin() - ref.bottom_right()).LengthSquared();
+
+ int distance = rect.y() - ref.bottom();
+ return distance * distance;
+}
+
+} // namespace win
+} // namespace gfx

Powered by Google App Engine
This is Rietveld 408576698