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

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

Issue 1813493002: COMPLETED PREVIEW Migrate to Display Placement (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gfxmove
Patch Set: Created 4 years, 9 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
« no previous file with comments | « ui/gfx/win/rect_util.h ('k') | ui/gfx/win/rect_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/win/rect_util.cc
diff --git a/ui/gfx/win/rect_util.cc b/ui/gfx/win/rect_util.cc
deleted file mode 100644
index 3d292cb9615e6d2a41e273eb65aab75b222060e1..0000000000000000000000000000000000000000
--- a/ui/gfx/win/rect_util.cc
+++ /dev/null
@@ -1,255 +0,0 @@
-// 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);
- 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));
-}
-
-// 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;
-}
-
-} // namespace
-
-namespace gfx {
-namespace win {
-
-RectEdge FindTouchingRectEdge(const gfx::Rect& ref, const gfx::Rect& test) {
- int max_left = std::max(ref.x(), test.x());
- int max_top = 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_left == min_right && max_top == min_bottom) {
- // Corner touching.
- if (ref.bottom() == max_top)
- return RectEdge::BOTTOM;
- if (ref.x() == max_left)
- return RectEdge::LEFT;
-
- return RectEdge::TOP;
- }
- if (max_left == min_right) {
- // Vertical edge touching.
- return ref.x() == max_left ? RectEdge::LEFT : RectEdge::RIGHT;
- }
- if (max_top == min_bottom) {
- // Horizontal edge touching.
- return ref.y() == max_top ? 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_unscaled_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_unscaled_rect| instead of the bottom, left, top, and
-// right.
-gfx::Rect ScaleAndPositionRect(const gfx::Rect& ref_scaled_rect,
- const gfx::Rect& ref_unscaled_rect,
- const gfx::Rect& unscaled_rect,
- float unscaled_rect_scale_factor) {
- RectEdge orig_edge = FindTouchingRectEdge(ref_unscaled_rect, unscaled_rect);
-
- // Scale size only since the scaled origin location will be determined below.
- gfx::Rect scaled_rect(
- gfx::ScaleToEnclosingRect(unscaled_rect,
- 1.0f / unscaled_rect_scale_factor));
- scaled_rect.set_origin(gfx::Point(0, 0));
-
- if (orig_edge == RectEdge::NONE) {
- // ScaleAndPositionRect depends on unscaled rectangles touching.
- DCHECK(false);
- scaled_rect.set_origin(unscaled_rect.origin());
- }
-
- // Transform rectangles so we can simply deal with bottom edge sharing.
- gfx::Rect ref_scaled_rect_work(
- CanonicalizeTouchingEdge(ref_scaled_rect, orig_edge));
- gfx::Rect ref_unscaled_rect_work(
- CanonicalizeTouchingEdge(ref_unscaled_rect, orig_edge));
- gfx::Rect unscaled_rect_work(
- CanonicalizeTouchingEdge(unscaled_rect,orig_edge));
- scaled_rect = CanonicalizeTouchingEdge(scaled_rect, orig_edge);
-
- // Position the rect.
- scaled_rect.set_y(ref_scaled_rect_work.bottom());
- int x;
- if (unscaled_rect_work.right() == ref_unscaled_rect_work.right()) {
- // Maintain right alignment. If the rectangle was left-aligned, the next
- // case will catch that.
- x = ref_scaled_rect_work.right() - scaled_rect.width();
- } else if (InRange(unscaled_rect_work.x(),
- ref_unscaled_rect_work.x(),
- ref_unscaled_rect_work.right())) {
- // Position using the left point relative to the reference rect.
- x = ScaleValue(ref_scaled_rect_work.x(), ref_scaled_rect_work.right(),
- ref_unscaled_rect_work.x(), ref_unscaled_rect_work.right(),
- unscaled_rect_work.x());
- } else if (InRange(unscaled_rect_work.right(),
- ref_unscaled_rect_work.x(),
- ref_unscaled_rect_work.right())) {
- // Position using the right point relative to the reference rect.
- x = ScaleValue(ref_scaled_rect_work.x(), ref_scaled_rect_work.right(),
- ref_unscaled_rect_work.x(), ref_unscaled_rect_work.right(),
- unscaled_rect_work.right()) - scaled_rect.width();
- } else if (InRange(ref_scaled_rect_work.x(),
- unscaled_rect_work.x(),
- unscaled_rect_work.right())) {
- // Position relative to the left point of the reference rect.
- int offset = ScaleValue(0, scaled_rect.width(),
- 0, unscaled_rect_work.width(),
- ref_unscaled_rect_work.x() -
- unscaled_rect_work.x());
- x = ref_scaled_rect_work.x() - offset;
- } else {
- // Position relative to the right point of the reference rect.
- int offset = ScaleValue(0, scaled_rect.width(),
- 0, unscaled_rect_work.width(),
- ref_unscaled_rect_work.right() -
- unscaled_rect_work.x());
- x = ref_scaled_rect_work.x() - offset - scaled_rect.width();
- }
- scaled_rect.set_x(x);
- DCHECK(FindTouchingRectEdge(ref_scaled_rect_work, 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(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));
- // 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 gfx
« no previous file with comments | « ui/gfx/win/rect_util.h ('k') | ui/gfx/win/rect_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698