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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/gfx/win/rect_util.h"
6
7 #include "ui/gfx/geometry/rect.h"
8 #include "ui/gfx/geometry/safe_integer_conversions.h"
9 #include "ui/gfx/geometry/size.h"
10 #include "ui/gfx/geometry/vector2d.h"
11
12 namespace {
13
14 enum class RelativePosition {
15 BOTTOM = 0,
16 LEFT = 1,
17 TOP = 2,
18 RIGHT = 3,
19 };
20
21 gfx::Rect CoordinateRotateRectangle90(const gfx::Rect& rect) {
22 return gfx::Rect(rect.y(), -rect.x() - rect.width(),
23 rect.height(), rect.width());
24 }
25
26 gfx::Rect CoordinateRotateRectangle180(const gfx::Rect& rect) {
27 return gfx::Rect(-rect.x() - rect.width(), -rect.y() -rect.height(),
28 rect.width(), rect.height());
29 }
30
31 gfx::Rect CoordinateRotateRectangle270(const gfx::Rect& rect) {
32 return gfx::Rect(-rect.y() -rect.height(), rect.x(),
33 rect.height(), rect.width());
34 }
35
36 gfx::Rect CoordinateReflectRectagleYAxis(const gfx::Rect& rect) {
37 return gfx::Rect(-rect.x() - rect.width(), rect.y(),
38 rect.width(), rect.height());
39 }
40
41 gfx::Rect CanonicalizeRelativePosition(gfx::Rect rect,
42 RelativePosition relative_position) {
43 switch (relative_position) {
44 case RelativePosition::LEFT:
45 rect = CoordinateRotateRectangle90(rect);
46 break;
47 case RelativePosition::TOP:
48 rect = CoordinateRotateRectangle180(rect);
49 break;
50 case RelativePosition::RIGHT:
51 rect = CoordinateRotateRectangle270(rect);
52 break;
53 }
54 return rect;
55 }
56
57 gfx::Rect CanonicalizeTouchingEdge(gfx::Rect rect,
58 gfx::win::RectEdge edge) {
59 switch (edge) {
60 case gfx::win::RectEdge::LEFT:
61 rect = CoordinateRotateRectangle90(rect);
62 break;
63 case gfx::win::RectEdge::TOP:
64 rect = CoordinateRotateRectangle180(rect);
65 // Helps prefer left and top alignment.
66 rect = CoordinateReflectRectagleYAxis(rect);
67 break;
68 case gfx::win::RectEdge::RIGHT:
69 rect = CoordinateRotateRectangle270(rect);
70 // Helps prefer left and top alignment.
71 rect = CoordinateReflectRectagleYAxis(rect);
72 break;
73 }
74 return rect;
75 }
76
77 gfx::Rect RevertToOriginalEdge(gfx::Rect rect, gfx::win::RectEdge edge) {
78 switch (edge) {
79 case gfx::win::RectEdge::LEFT:
80 rect = CoordinateRotateRectangle270(rect);
81 break;
82 case gfx::win::RectEdge::TOP:
83 rect = CoordinateReflectRectagleYAxis(rect);
84 rect = CoordinateRotateRectangle180(rect);
85 break;
86 case gfx::win::RectEdge::RIGHT:
87 rect = CoordinateReflectRectagleYAxis(rect);
88 rect = CoordinateRotateRectangle90(rect);
89 break;
90 }
91 return rect;
92 }
93
94 bool InRange(int target, int lower_bound, int upper_bound) {
95 return lower_bound <= target && target <= upper_bound;
96 }
97
98 int ScaleValue(int new_min, int new_max, int old_min, int old_max, int val) {
99 int old_delta = old_max - old_min;
100 if (old_delta == 0) {
101 DCHECK(new_min == new_max);
102 return new_min;
103 }
104 float percent = (float)(val - old_min) / (float)old_delta;
105 return new_min + gfx::ToFlooredInt(percent * (float)(new_max - new_min));
106 }
107
108 RelativePosition RectRelativePosition(gfx::Rect ref, gfx::Rect test) {
109 int i = 0;
110 for (; i <= 3; ++i) {
111 if (ref.bottom() <= test.y()) {
112 break;
113 } else {
114 ref = CoordinateRotateRectangle90(ref);
115 test = CoordinateRotateRectangle90(test);
116 }
117 }
118 switch (i) {
119 case 0:
120 return RelativePosition::BOTTOM;
121 case 1:
122 return RelativePosition::LEFT;
123 case 2:
124 return RelativePosition::TOP;
125 }
126 DCHECK(i == 3);
127 return RelativePosition::RIGHT;
128 }
129
130 } // namespace
131
132 namespace gfx {
133 namespace win {
134
135 RectEdge RectTouch(gfx::Rect ref, gfx::Rect test) {
136 for (int i = 0; i <= 3; ++i) {
137 if (ref.bottom() == test.y() &&
138 (InRange(test.x(), ref.x(), ref.right()) ||
139 InRange(test.right(), ref.x(), ref.right()) ||
140 InRange(ref.x(), test.x(), test.right()) ||
141 InRange(ref.right(), test.x(), test.right()))) {
142 switch (i) {
143 case 0:
144 return RectEdge::BOTTOM;
145 case 1:
146 return RectEdge::LEFT;
147 case 2:
148 return RectEdge::TOP;
149 case 3:
150 return RectEdge::RIGHT;
151 }
152 } else {
153 ref = CoordinateRotateRectangle90(ref);
154 test = CoordinateRotateRectangle90(test);
155 }
156 }
157 return RectEdge::NONE;
158 }
159
160 // Writing code that specifically deals with scaling each rect is tedious and
161 // error prone. Instead, this function transforms the positions of the
162 // two rects so that |ref_scaled_rect| is always on top of |ref_original_rect|,
163 // calculates the scaled and positioned target rect, and then reverses the
164 // transforms. As a result, the position logic can be written in terms of the
165 // bottom of the |ref_original_rect| instead of the bottom, left, top, and
166 // right.
167 gfx::Rect ScaleAndPositionRect(gfx::Rect ref_scaled_rect,
168 gfx::Rect ref_original_rect,
169 gfx::Rect target_rect,
170 float target_scale_factor) {
171 RectEdge orig_edge = RectTouch(ref_original_rect, target_rect);
172 gfx::Rect scaled_rect(gfx::ScaleToFlooredSize(target_rect.size(),
173 1.0f / target_scale_factor));
174 if (orig_edge == RectEdge::NONE) {
175 // Currently, we expect all rectangles to touch each other.
176 NOTREACHED();
177 scaled_rect.set_origin(target_rect.origin());
178 }
179
180 // Transform rectangles so we can simply deal with bottom edge sharing.
181 ref_scaled_rect = CanonicalizeTouchingEdge(ref_scaled_rect, orig_edge);
182 ref_original_rect = CanonicalizeTouchingEdge(ref_original_rect, orig_edge);
183 target_rect = CanonicalizeTouchingEdge(target_rect, orig_edge);
184 scaled_rect = CanonicalizeTouchingEdge(scaled_rect, orig_edge);
185
186 // Position the rect.
187 scaled_rect.set_y(ref_scaled_rect.bottom());
188 int x;
189 if (target_rect.right() == ref_original_rect.right()) {
190 // Maintain right alignment. If the rectangle was left-aligned, the next
191 // case will catch that.
192 x = ref_scaled_rect.right() - scaled_rect.width();
193 } else if (InRange(target_rect.x(),
194 ref_original_rect.x(),
195 ref_original_rect.right())) {
196 // Position using the left point relative to the reference rect.
197 x = ScaleValue(ref_scaled_rect.x(), ref_scaled_rect.right(),
198 ref_original_rect.x(), ref_original_rect.right(),
199 target_rect.x());
200 } else if (InRange(target_rect.right(),
201 ref_original_rect.x(),
202 ref_original_rect.right())) {
203 // Position using the right point relative to the reference rect.
204 x = ScaleValue(ref_scaled_rect.x(), ref_scaled_rect.right(),
205 ref_original_rect.x(), ref_original_rect.right(),
206 target_rect.right()) - scaled_rect.width();
207 } else if (InRange(ref_scaled_rect.x(),
208 target_rect.x(),
209 target_rect.right())) {
210 // Position relative to the left point of the reference rect.
211 int offset = ScaleValue(0, scaled_rect.width(),
212 0, target_rect.width(),
213 ref_original_rect.x() - target_rect.x());
214 x = ref_scaled_rect.x() - offset;
215 } else {
216 // Position relative to the right point of the reference rect.
217 int offset = ScaleValue(0, scaled_rect.width(),
218 0, target_rect.width(),
219 ref_original_rect.right() - target_rect.x());
220 x = ref_scaled_rect.x() - offset - scaled_rect.width();
221 }
222 scaled_rect.set_x(x);
223 DCHECK(RectTouch(ref_scaled_rect, scaled_rect) == RectEdge::BOTTOM);
224
225 // Reverse the transformation.
226 return RevertToOriginalEdge(scaled_rect, orig_edge);
227 }
228
229 // This function takes the same approach as ScaleAndPositionRect, transforming
230 // the two input rects so that the relative positions are always the same.
231 int64_t SquaredDistanceBetweenRects(gfx::Rect ref, gfx::Rect rect) {
232 if (ref.Intersects(rect))
233 return 0;
234
235 RelativePosition relative_position = RectRelativePosition(ref, rect);
236 ref = CanonicalizeRelativePosition(ref, relative_position);
237 rect = CanonicalizeRelativePosition(rect, relative_position);
238 // Now that the ref is on top, we can concentrate ref bottom
239 // and rect top calculations.
240 if (rect.right() < ref.x())
241 return (rect.top_right() - ref.bottom_left()).LengthSquared();
242 else if (ref.right() < rect.x())
243 return (rect.origin() - ref.bottom_right()).LengthSquared();
244
245 int distance = rect.y() - ref.bottom();
246 return distance * distance;
247 }
248
249 } // namespace win
250 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698