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

Unified Diff: ui/gfx/win/scaling_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/scaling_util.h ('k') | ui/gfx/win/scaling_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/win/scaling_util.cc
diff --git a/ui/gfx/win/scaling_util.cc b/ui/gfx/win/scaling_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..590a71c3f7dce210ffe80830540fc17673b643ee
--- /dev/null
+++ b/ui/gfx/win/scaling_util.cc
@@ -0,0 +1,257 @@
+// 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/scaling_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,
+};
+
+// 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 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;
+}
+
+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));
+}
+
+// Scaled |unscaled_offset| to the same relative position on |unscaled_length|
+// based off of |unscaled_length|'s |scale_factor|.
+int ScaleOffset(int unscaled_length, float scale_factor, int unscaled_offset) {
+ float scaled_length = static_cast<float>(unscaled_length) / scale_factor;
+ float percent =
+ static_cast<float>(unscaled_offset) / static_cast<float>(unscaled_length);
+ return gfx::ToFlooredInt(scaled_length * percent);
+}
+
+} // namespace
+
+namespace gfx {
+namespace win {
+
+bool DisplayInfosTouch(const gfx::win::DisplayInfo& a,
+ const gfx::win::DisplayInfo& b) {
+ const gfx::Rect& a_rect = a.screen_rect();
+ const gfx::Rect& b_rect = b.screen_rect();
+ int max_left = std::max(a_rect.x(), b_rect.x());
+ int max_top = std::max(a_rect.y(), b_rect.y());
+ int min_right = std::min(a_rect.right(), b_rect.right());
+ int min_bottom = std::min(a_rect.bottom(), b_rect.bottom());
+ return max_left == min_right || max_top == min_bottom;
+}
+
+gfx::DisplayPlacement::Position CalculateDisplayPosition(
+ const gfx::win::DisplayInfo& parent,
+ const gfx::win::DisplayInfo& current) {
+ const gfx::Rect& parent_rect = parent.screen_rect();
+ const gfx::Rect& current_rect = current.screen_rect();
+ int max_left = std::max(parent_rect.x(), current_rect.x());
+ int max_top = std::max(parent_rect.y(), current_rect.y());
+ int min_right = std::min(parent_rect.right(), current_rect.right());
+ int min_bottom = std::min(parent_rect.bottom(), current_rect.bottom());
+ if (max_left == min_right && max_top == min_bottom) {
+ // Corner touching.
+ if (parent_rect.bottom() == max_top)
+ return gfx::DisplayPlacement::Position::BOTTOM;
+ if (parent_rect.x() == max_left)
+ return gfx::DisplayPlacement::Position::LEFT;
+
+ return gfx::DisplayPlacement::Position::TOP;
+ }
+ if (max_left == min_right) {
+ // Vertical edge touching.
+ return parent_rect.x() == max_left
+ ? gfx::DisplayPlacement::Position::LEFT
+ : gfx::DisplayPlacement::Position::RIGHT;
+ }
+ if (max_top == min_bottom) {
+ // Horizontal edge touching.
+ return parent_rect.y() == max_top
+ ? gfx::DisplayPlacement::Position::TOP
+ : gfx::DisplayPlacement::Position::BOTTOM;
+ }
+ NOTREACHED();
+ return gfx::DisplayPlacement::Position::RIGHT;
+}
+
+gfx::DisplayPlacement CalculateDisplayPlacement(
+ const gfx::win::DisplayInfo& parent,
+ const gfx::win::DisplayInfo& current) {
+ DCHECK(DisplayInfosTouch(parent, current)) << "DisplayInfos must touch.";
+
+ gfx::DisplayPlacement placement;
+ placement.parent_display_id = parent.id();
+ placement.display_id = current.id();
+ placement.position = CalculateDisplayPosition(parent, current);
+
+ int parent_begin = 0;
+ int parent_end = 0;
+ int current_begin = 0;
+ int current_end = 0;
+
+ switch (placement.position) {
+ case gfx::DisplayPlacement::Position::TOP:
+ case gfx::DisplayPlacement::Position::BOTTOM:
+ parent_begin = parent.screen_rect().x();
+ parent_end = parent.screen_rect().right();
+ current_begin = current.screen_rect().x();
+ current_end = current.screen_rect().right();
+ break;
+ case gfx::DisplayPlacement::Position::LEFT:
+ case gfx::DisplayPlacement::Position::RIGHT:
+ parent_begin = parent.screen_rect().y();
+ parent_end = parent.screen_rect().bottom();
+ current_begin = current.screen_rect().y();
+ current_end = current.screen_rect().bottom();
+ break;
+ }
+
+ // Since we're talking offsets, make everything relative to parent_begin.
+ parent_end -= parent_begin;
+ current_begin -= parent_begin;
+ current_end -= parent_begin;
+ parent_begin = 0;
+
+ // There are a few ways lines can intersect:
+ // End Aligned
+ // CURRENT's offset is relative to the end (in our world, BOTTOM_RIGHT).
+ // +-PARENT----------------+
+ // +-CURRENT------------+
+ //
+ // Positioning based off of |current_begin|.
+ // CURRENT's offset is simply a percentage of its position on PARENT.
+ // +-PARENT----------------+
+ // +-CURRENT------------+
+ //
+ // Positioning based off of |current_end|.
+ // CURRENT's offset is dependent on the percentage of its end position on
+ // PARENT.
+ // +-PARENT----------------+
+ // +-CURRENT------------+
+ //
+ // Positioning based off of |parent_begin| on current.
+ // CURRENT's offset is dependent on the percentage of its end position on
+ // PARENT.
+ // +-PARENT----------------+
+ // +-CURRENT--------------------------+
+ if (parent_end == current_end) {
+ // End aligned.
+ placement.offset_reference =
+ gfx::DisplayPlacement::OffsetReference::BOTTOM_RIGHT;
+ placement.offset = 0;
+ } else if (InRange(current_begin, parent_begin, parent_end)) {
+ placement.offset = ScaleOffset(parent_end,
+ parent.device_scale_factor(),
+ current_begin);
+ } else if (InRange(current_end, parent_begin, parent_end)) {
+ placement.offset_reference =
+ gfx::DisplayPlacement::OffsetReference::BOTTOM_RIGHT;
+ placement.offset = ScaleOffset(parent_end,
+ parent.device_scale_factor(),
+ parent_end - current_end);
+ } else {
+ DCHECK(InRange(parent_begin, current_begin, current_end));
+ placement.offset = ScaleOffset(current_end - current_begin,
+ current.device_scale_factor(),
+ current_begin);
+ }
+
+ return placement;
+}
+
+// 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/scaling_util.h ('k') | ui/gfx/win/scaling_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698