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

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

Issue 1679393002: Multiple DPI Tracking for ScreenWin (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase to b4da629 and a few equivalent comparison tweaks in rect_util Created 4 years, 10 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..0ff294d5b3e5b24b7fde0af5754e1d41533c0df2
--- /dev/null
+++ b/ui/gfx/win/rect_util.cc
@@ -0,0 +1,240 @@
+// 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 <algorithm>
+
+#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"
+#include "ui/gfx/range/range.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 CoordinateReflectRectangleYAxis(const gfx::Rect& rect) {
+ return gfx::Rect(-rect.x() - rect.width(), rect.y(),
+ rect.width(), rect.height());
+}
+
+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);
+ }
+ return rect;
+}
+
+gfx::Rect CanonicalizeTouchingEdge(const gfx::Rect& rect,
+ gfx::win::RectEdge edge) {
+ switch (edge) {
+ case gfx::win::RectEdge::LEFT:
+ return CoordinateRotateRectangle90(rect);
sky 2016/02/17 17:28:48 Can you elaborate on why we need the rotation?
robliao 2016/02/18 00:34:38 The rotation allows us to take advantage of the ro
+ case gfx::win::RectEdge::TOP:
+ // Helps prefer left and top alignment.
+ return CoordinateReflectRectangleYAxis(
+ CoordinateRotateRectangle180(rect));
+ case gfx::win::RectEdge::RIGHT:
+ // Helps prefer left and top alignment.
+ return CoordinateReflectRectangleYAxis(
+ CoordinateRotateRectangle270(rect));
+ }
+ return rect;
+}
+
+// Inverse of CanonicalizeTouchingEdge.
+gfx::Rect RevertToOriginalEdge(const gfx::Rect& rect, gfx::win::RectEdge edge) {
+ switch (edge) {
+ case gfx::win::RectEdge::LEFT:
+ return CoordinateRotateRectangle270(rect);
+ case gfx::win::RectEdge::TOP:
+ return CoordinateRotateRectangle180(
+ CoordinateReflectRectangleYAxis(rect));
+ case gfx::win::RectEdge::RIGHT:
+ return CoordinateRotateRectangle90(
+ CoordinateReflectRectangleYAxis(rect));
+ }
+ return rect;
+}
+
+bool InRange(int target, int lower_bound, int upper_bound) {
+ return lower_bound <= target && target <= upper_bound;
+}
+
+// Scales |val| within |old_min| and |old_max| to |new_min| and |new_max|.
+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_EQ(new_min, new_max);
+ return new_min;
+ }
+ float percent =
+ static_cast<float>(val - old_min) / static_cast<float>(old_delta);
+ return new_min + gfx::ToFlooredInt(percent * (float)(new_max - new_min));
+}
+
+RelativePosition RectRelativePosition(const gfx::Rect& ref,
+ const gfx::Rect& test) {
+ if (ref.bottom() <= test.y())
+ return RelativePosition::BOTTOM;
+ else if (test.right() <= ref.x())
+ return RelativePosition::LEFT;
+ else if (test.bottom() <= ref.y())
+ return RelativePosition::TOP;
+
+ return RelativePosition::RIGHT;
+}
+
+} // namespace
+
+namespace gfx {
+namespace win {
+
+RectEdge FindTouchingRectEdge(const gfx::Rect& ref, const gfx::Rect& test) {
sky 2016/02/17 04:40:28 Add description, in particular who the return valu
robliao 2016/02/18 00:34:39 Done for RectRelativePosition. The header contains
+ int max_x = std::max(ref.x(), test.x());
sky 2016/02/17 17:28:48 This functions seems more complex than it needs to
robliao 2016/02/18 00:34:39 We need to do the intersection test because a rect
+ int max_y = std::max(ref.y(), test.y());
+ int min_right = std::min(ref.right(), test.right());
+ int min_bottom = std::min(ref.bottom(), test.bottom());
+ if (max_x == min_right && max_y == min_bottom) {
+ // Corner touching.
+ if (ref.bottom() == max_y)
+ return RectEdge::BOTTOM;
+ else if (ref.x() == max_x)
+ return RectEdge::LEFT;
+
+ return RectEdge::TOP;
+ } else if (max_x == min_right) {
sky 2016/02/17 04:40:28 nit: no else after return.
robliao 2016/02/18 00:34:39 Done.
+ // Vertical edge touching.
+ return ref.x() == max_x ? RectEdge::LEFT : RectEdge::RIGHT;
+ } else if (max_y == min_bottom) {
+ // Horizontal edge touching.
+ return ref.y() == max_y ? RectEdge::TOP : RectEdge::BOTTOM;
+ }
+ 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 = FindTouchingRectEdge(ref_original_rect, target_rect);
+
+ // Scale size only since the scaled origin location will be determined below.
+ gfx::Rect scaled_rect = gfx::ScaleToEnclosingRect(target_rect,
+ 1.0f / target_scale_factor);
+ scaled_rect.set_origin(gfx::Point(0, 0));
+
+ if (orig_edge == RectEdge::NONE) {
+ // Currently, we expect all rectangles to touch each other.
+ NOTREACHED();
sky 2016/02/17 17:28:48 If that's the case, then 164 should be a DCHECK.
robliao 2016/02/18 00:34:39 Done.
+ 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(
+ FindTouchingRectEdge(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