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

Side by Side 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: 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 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(),
scottmg 2016/02/09 20:21:36 binary - here, missing space before rect.height()
robliao 2016/02/10 22:50:03 Oops, indeed. Done.
33 rect.height(), rect.width());
34 }
35
36 gfx::Rect CoordinateReflectRectagleYAxis(const gfx::Rect& rect) {
scottmg 2016/02/09 20:21:36 Rectagle -> Rectangle
robliao 2016/02/10 22:50:03 Done.
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 // Inverse of CanonicalizeTouchingEdge.
78 gfx::Rect RevertToOriginalEdge(gfx::Rect rect, gfx::win::RectEdge edge) {
79 switch (edge) {
80 case gfx::win::RectEdge::LEFT:
81 rect = CoordinateRotateRectangle270(rect);
82 break;
83 case gfx::win::RectEdge::TOP:
84 rect = CoordinateReflectRectagleYAxis(rect);
85 rect = CoordinateRotateRectangle180(rect);
86 break;
87 case gfx::win::RectEdge::RIGHT:
88 rect = CoordinateReflectRectagleYAxis(rect);
89 rect = CoordinateRotateRectangle90(rect);
90 break;
91 }
92 return rect;
93 }
94
95 bool InRange(int target, int lower_bound, int upper_bound) {
96 return lower_bound <= target && target <= upper_bound;
97 }
98
99 // Scales |val| within |old_min| and |old_max| to |new_min| and |new_max|.
100 int ScaleValue(int new_min, int new_max, int old_min, int old_max, int val) {
101 int old_delta = old_max - old_min;
102 if (old_delta == 0) {
103 DCHECK(new_min == new_max);
104 return new_min;
105 }
106 float percent = (float)(val - old_min) / (float)old_delta;
107 return new_min + gfx::ToFlooredInt(percent * (float)(new_max - new_min));
108 }
109
110 RelativePosition RectRelativePosition(gfx::Rect ref, gfx::Rect test) {
111 int i = 0;
112 for (; i <= 3; ++i) {
113 if (ref.bottom() <= test.y()) {
114 break;
115 } else {
116 ref = CoordinateRotateRectangle90(ref);
117 test = CoordinateRotateRectangle90(test);
118 }
119 }
120 switch (i) {
121 case 0:
122 return RelativePosition::BOTTOM;
123 case 1:
124 return RelativePosition::LEFT;
125 case 2:
126 return RelativePosition::TOP;
127 default:
128 DCHECK(i == 3);
129 return RelativePosition::RIGHT;
130 }
131 }
132
133 } // namespace
134
135 namespace gfx {
136 namespace win {
137
138 RectEdge RectTouch(gfx::Rect ref, gfx::Rect test) {
139 for (int i = 0; i <= 3; ++i) {
140 if (ref.bottom() == test.y() &&
141 (InRange(test.x(), ref.x(), ref.right()) ||
142 InRange(test.right(), ref.x(), ref.right()) ||
143 InRange(ref.x(), test.x(), test.right()) ||
144 InRange(ref.right(), test.x(), test.right()))) {
145 switch (i) {
146 case 0:
147 return RectEdge::BOTTOM;
148 case 1:
149 return RectEdge::LEFT;
150 case 2:
151 return RectEdge::TOP;
152 case 3:
153 return RectEdge::RIGHT;
154 }
155 } else {
156 ref = CoordinateRotateRectangle90(ref);
157 test = CoordinateRotateRectangle90(test);
158 }
159 }
160 return RectEdge::NONE;
161 }
162
163 // Writing code that specifically deals with scaling each rect is tedious and
164 // error prone. Instead, this function transforms the positions of the
165 // two rects so that |ref_scaled_rect| is always on top of |ref_original_rect|,
166 // calculates the scaled and positioned target rect, and then reverses the
167 // transforms. As a result, the position logic can be written in terms of the
168 // bottom of the |ref_original_rect| instead of the bottom, left, top, and
169 // right.
170 gfx::Rect ScaleAndPositionRect(gfx::Rect ref_scaled_rect,
171 gfx::Rect ref_original_rect,
172 gfx::Rect target_rect,
173 float target_scale_factor) {
174 RectEdge orig_edge = RectTouch(ref_original_rect, target_rect);
175 gfx::Rect scaled_rect(gfx::ScaleToFlooredSize(target_rect.size(),
176 1.0f / target_scale_factor));
177 if (orig_edge == RectEdge::NONE) {
178 // Currently, we expect all rectangles to touch each other.
179 NOTREACHED();
180 scaled_rect.set_origin(target_rect.origin());
181 }
182
183 // Transform rectangles so we can simply deal with bottom edge sharing.
184 ref_scaled_rect = CanonicalizeTouchingEdge(ref_scaled_rect, orig_edge);
scottmg 2016/02/09 21:24:26 (I'm still trying to figure out how this works, bu
robliao 2016/02/10 22:50:03 All rectangles are assumed to have a coordinate or
185 ref_original_rect = CanonicalizeTouchingEdge(ref_original_rect, orig_edge);
186 target_rect = CanonicalizeTouchingEdge(target_rect, orig_edge);
187 scaled_rect = CanonicalizeTouchingEdge(scaled_rect, orig_edge);
188
189 // Position the rect.
190 scaled_rect.set_y(ref_scaled_rect.bottom());
191 int x;
192 if (target_rect.right() == ref_original_rect.right()) {
193 // Maintain right alignment. If the rectangle was left-aligned, the next
194 // case will catch that.
195 x = ref_scaled_rect.right() - scaled_rect.width();
196 } else if (InRange(target_rect.x(),
197 ref_original_rect.x(),
198 ref_original_rect.right())) {
199 // Position using the left point relative to the reference rect.
200 x = ScaleValue(ref_scaled_rect.x(), ref_scaled_rect.right(),
201 ref_original_rect.x(), ref_original_rect.right(),
202 target_rect.x());
203 } else if (InRange(target_rect.right(),
204 ref_original_rect.x(),
205 ref_original_rect.right())) {
206 // Position using the right point relative to the reference rect.
207 x = ScaleValue(ref_scaled_rect.x(), ref_scaled_rect.right(),
208 ref_original_rect.x(), ref_original_rect.right(),
209 target_rect.right()) - scaled_rect.width();
210 } else if (InRange(ref_scaled_rect.x(),
211 target_rect.x(),
212 target_rect.right())) {
213 // Position relative to the left point of the reference rect.
214 int offset = ScaleValue(0, scaled_rect.width(),
215 0, target_rect.width(),
216 ref_original_rect.x() - target_rect.x());
217 x = ref_scaled_rect.x() - offset;
218 } else {
219 // Position relative to the right point of the reference rect.
220 int offset = ScaleValue(0, scaled_rect.width(),
221 0, target_rect.width(),
222 ref_original_rect.right() - target_rect.x());
223 x = ref_scaled_rect.x() - offset - scaled_rect.width();
224 }
225 scaled_rect.set_x(x);
226 DCHECK(RectTouch(ref_scaled_rect, scaled_rect) == RectEdge::BOTTOM);
227
228 // Reverse the transformation.
229 return RevertToOriginalEdge(scaled_rect, orig_edge);
230 }
231
232 // This function takes the same approach as ScaleAndPositionRect, transforming
233 // the two input rects so that the relative positions are always the same.
234 int64_t SquaredDistanceBetweenRects(gfx::Rect ref, gfx::Rect rect) {
235 if (ref.Intersects(rect))
236 return 0;
237
238 RelativePosition relative_position = RectRelativePosition(ref, rect);
239 ref = CanonicalizeRelativePosition(ref, relative_position);
240 rect = CanonicalizeRelativePosition(rect, relative_position);
241 // Now that the ref is on top, we can concentrate ref bottom
242 // and rect top calculations.
243 if (rect.right() < ref.x())
244 return (rect.top_right() - ref.bottom_left()).LengthSquared();
245 else if (ref.right() < rect.x())
246 return (rect.origin() - ref.bottom_right()).LengthSquared();
247
248 int distance = rect.y() - ref.bottom();
249 return distance * distance;
250 }
251
252 } // namespace win
253 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698