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

Side by Side Diff: ui/touch_selection/touch_handle.cc

Issue 481683003: Support for Adaptive Handle Orientation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nits Created 5 years, 4 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/touch_selection/touch_handle.h" 5 #include "ui/touch_selection/touch_handle.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 namespace ui { 9 namespace ui {
10 10
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 return os << "CENTER"; 56 return os << "CENTER";
57 case TouchHandleOrientation::UNDEFINED: 57 case TouchHandleOrientation::UNDEFINED:
58 return os << "UNDEFINED"; 58 return os << "UNDEFINED";
59 default: 59 default:
60 return os << "INVALID: " << static_cast<int>(orientation); 60 return os << "INVALID: " << static_cast<int>(orientation);
61 } 61 }
62 } 62 }
63 63
64 // Responsible for rendering a selection or insertion handle for text editing. 64 // Responsible for rendering a selection or insertion handle for text editing.
65 TouchHandle::TouchHandle(TouchHandleClient* client, 65 TouchHandle::TouchHandle(TouchHandleClient* client,
66 TouchHandleOrientation orientation) 66 TouchHandleOrientation orientation,
67 const gfx::RectF viewport_rect)
67 : drawable_(client->CreateDrawable()), 68 : drawable_(client->CreateDrawable()),
68 client_(client), 69 client_(client),
70 viewport_rect_(viewport_rect),
69 orientation_(orientation), 71 orientation_(orientation),
70 deferred_orientation_(TouchHandleOrientation::UNDEFINED), 72 deferred_orientation_(TouchHandleOrientation::UNDEFINED),
71 alpha_(0.f), 73 alpha_(0.f),
72 animate_deferred_fade_(false), 74 animate_deferred_fade_(false),
73 enabled_(true), 75 enabled_(true),
74 is_visible_(false), 76 is_visible_(false),
75 is_dragging_(false), 77 is_dragging_(false),
76 is_drag_within_tap_region_(false) { 78 is_drag_within_tap_region_(false),
79 is_handle_layout_update_required_(false),
80 mirror_vertical_(false),
81 mirror_horizontal_(false) {
77 DCHECK_NE(orientation, TouchHandleOrientation::UNDEFINED); 82 DCHECK_NE(orientation, TouchHandleOrientation::UNDEFINED);
78 drawable_->SetEnabled(enabled_); 83 drawable_->SetEnabled(enabled_);
79 drawable_->SetOrientation(orientation_); 84 drawable_->SetOrientation(orientation_, false, false);
85 drawable_->SetOrigin(focus_bottom_);
80 drawable_->SetAlpha(alpha_); 86 drawable_->SetAlpha(alpha_);
81 drawable_->SetFocus(position_); 87 handle_horizontal_padding_ = drawable_->GetDrawableHorizontalPadding();
82 } 88 }
83 89
84 TouchHandle::~TouchHandle() { 90 TouchHandle::~TouchHandle() {
85 } 91 }
86 92
87 void TouchHandle::SetEnabled(bool enabled) { 93 void TouchHandle::SetEnabled(bool enabled) {
88 if (enabled_ == enabled) 94 if (enabled_ == enabled)
89 return; 95 return;
90 if (!enabled) { 96 if (!enabled) {
91 EndDrag(); 97 EndDrag();
92 EndFade(); 98 EndFade();
93 } 99 }
94 enabled_ = enabled; 100 enabled_ = enabled;
95 drawable_->SetEnabled(enabled); 101 drawable_->SetEnabled(enabled);
96 } 102 }
97 103
98 void TouchHandle::SetVisible(bool visible, AnimationStyle animation_style) { 104 void TouchHandle::SetVisible(bool visible, AnimationStyle animation_style) {
99 DCHECK(enabled_); 105 DCHECK(enabled_);
100 if (is_visible_ == visible) 106 if (is_visible_ == visible)
101 return; 107 return;
102 108
103 is_visible_ = visible; 109 is_visible_ = visible;
104 110
105 // Handle repositioning may have been deferred while previously invisible. 111 // Handle repositioning may have been deferred while previously invisible.
106 if (visible) 112 if (visible)
107 drawable_->SetFocus(position_); 113 SetUpdateLayoutRequired();
108 114
109 bool animate = animation_style != ANIMATION_NONE; 115 bool animate = animation_style != ANIMATION_NONE;
110 if (is_dragging_) { 116 if (is_dragging_) {
111 animate_deferred_fade_ = animate; 117 animate_deferred_fade_ = animate;
112 return; 118 return;
113 } 119 }
114 120
115 if (animate) 121 if (animate)
116 BeginFade(); 122 BeginFade();
117 else 123 else
118 EndFade(); 124 EndFade();
119 } 125 }
120 126
121 void TouchHandle::SetPosition(const gfx::PointF& position) { 127 void TouchHandle::SetFocus(const gfx::PointF& top, const gfx::PointF& bottom) {
122 DCHECK(enabled_); 128 if (focus_top_ == top && focus_bottom_ == bottom)
123 if (position_ == position)
124 return; 129 return;
125 position_ = position; 130
126 // Suppress repositioning a handle while invisible or fading out to prevent it 131 focus_top_ = top;
127 // from "ghosting" outside the visible bounds. The position will be pushed to 132 focus_bottom_ = bottom;
128 // the drawable when the handle regains visibility (see |SetVisible()|). 133 SetUpdateLayoutRequired();
129 if (is_visible_) 134 }
130 drawable_->SetFocus(position_); 135
136 void TouchHandle::SetViewportRect(const gfx::RectF viewport_rect) {
jdduke (slow) 2015/09/08 15:42:55 Nit: Pass by const ref.
AviD 2015/09/09 15:17:01 Done.
137 if (viewport_rect_ == viewport_rect)
138 return;
139
140 viewport_rect_ = viewport_rect;
141 SetUpdateLayoutRequired();
131 } 142 }
132 143
133 void TouchHandle::SetOrientation(TouchHandleOrientation orientation) { 144 void TouchHandle::SetOrientation(TouchHandleOrientation orientation) {
134 DCHECK(enabled_); 145 DCHECK(enabled_);
135 DCHECK_NE(orientation, TouchHandleOrientation::UNDEFINED); 146 DCHECK_NE(orientation, TouchHandleOrientation::UNDEFINED);
136 if (is_dragging_) { 147 if (is_dragging_) {
137 deferred_orientation_ = orientation; 148 deferred_orientation_ = orientation;
138 return; 149 return;
139 } 150 }
140 DCHECK_EQ(deferred_orientation_, TouchHandleOrientation::UNDEFINED); 151 DCHECK_EQ(deferred_orientation_, TouchHandleOrientation::UNDEFINED);
141 if (orientation_ == orientation) 152 if (orientation_ == orientation)
142 return; 153 return;
143 154
144 orientation_ = orientation; 155 orientation_ = orientation;
145 drawable_->SetOrientation(orientation); 156 SetUpdateLayoutRequired();
146 } 157 }
147 158
148 bool TouchHandle::WillHandleTouchEvent(const MotionEvent& event) { 159 bool TouchHandle::WillHandleTouchEvent(const MotionEvent& event) {
149 if (!enabled_) 160 if (!enabled_)
150 return false; 161 return false;
151 162
152 if (!is_dragging_ && event.GetAction() != MotionEvent::ACTION_DOWN) 163 if (!is_dragging_ && event.GetAction() != MotionEvent::ACTION_DOWN)
153 return false; 164 return false;
154 165
155 switch (event.GetAction()) { 166 switch (event.GetAction()) {
156 case MotionEvent::ACTION_DOWN: { 167 case MotionEvent::ACTION_DOWN: {
157 if (!is_visible_) 168 if (!is_visible_)
158 return false; 169 return false;
159 const gfx::PointF touch_point(event.GetX(), event.GetY()); 170 const gfx::PointF touch_point(event.GetX(), event.GetY());
160 const float touch_radius = std::max( 171 const float touch_radius = std::max(
161 kMinTouchMajorForHitTesting, 172 kMinTouchMajorForHitTesting,
162 std::min(kMaxTouchMajorForHitTesting, event.GetTouchMajor())) * 0.5f; 173 std::min(kMaxTouchMajorForHitTesting, event.GetTouchMajor())) * 0.5f;
163 if (!RectIntersectsCircle(drawable_->GetVisibleBounds(), 174 if (!RectIntersectsCircle(drawable_->GetVisibleBounds(),
164 touch_point, 175 touch_point,
165 touch_radius)) { 176 touch_radius)) {
166 EndDrag(); 177 EndDrag();
167 return false; 178 return false;
168 } 179 }
169 touch_down_position_ = touch_point; 180 touch_down_position_ = touch_point;
170 touch_drag_offset_ = position_ - touch_down_position_; 181 touch_drag_offset_ = focus_bottom_ - touch_down_position_;
171 touch_down_time_ = event.GetEventTime(); 182 touch_down_time_ = event.GetEventTime();
172 BeginDrag(); 183 BeginDrag();
173 } break; 184 } break;
174 185
175 case MotionEvent::ACTION_MOVE: { 186 case MotionEvent::ACTION_MOVE: {
176 gfx::PointF touch_move_position(event.GetX(), event.GetY()); 187 gfx::PointF touch_move_position(event.GetX(), event.GetY());
177 is_drag_within_tap_region_ &= 188 is_drag_within_tap_region_ &=
178 client_->IsWithinTapSlop(touch_down_position_ - touch_move_position); 189 client_->IsWithinTapSlop(touch_down_position_ - touch_move_position);
179 190
180 // Note that we signal drag update even if we're inside the tap region, 191 // Note that we signal drag update even if we're inside the tap region,
(...skipping 26 matching lines...) Expand all
207 } 218 }
208 219
209 bool TouchHandle::Animate(base::TimeTicks frame_time) { 220 bool TouchHandle::Animate(base::TimeTicks frame_time) {
210 if (fade_end_time_ == base::TimeTicks()) 221 if (fade_end_time_ == base::TimeTicks())
211 return false; 222 return false;
212 223
213 DCHECK(enabled_); 224 DCHECK(enabled_);
214 225
215 float time_u = 226 float time_u =
216 1.f - (fade_end_time_ - frame_time).InMillisecondsF() / kFadeDurationMs; 227 1.f - (fade_end_time_ - frame_time).InMillisecondsF() / kFadeDurationMs;
217 float position_u = 228 float position_u = (focus_bottom_ - fade_start_position_).LengthSquared() /
218 (position_ - fade_start_position_).LengthSquared() / kFadeDistanceSquared; 229 kFadeDistanceSquared;
219 float u = std::max(time_u, position_u); 230 float u = std::max(time_u, position_u);
220 SetAlpha(is_visible_ ? u : 1.f - u); 231 SetAlpha(is_visible_ ? u : 1.f - u);
221 232
222 if (u >= 1.f) { 233 if (u >= 1.f) {
223 EndFade(); 234 EndFade();
224 return false; 235 return false;
225 } 236 }
226 237
227 return true; 238 return true;
228 } 239 }
229 240
230 gfx::RectF TouchHandle::GetVisibleBounds() const { 241 gfx::RectF TouchHandle::GetVisibleBounds() const {
231 if (!is_visible_ || !enabled_) 242 if (!is_visible_ || !enabled_)
232 return gfx::RectF(); 243 return gfx::RectF();
233 244
234 return drawable_->GetVisibleBounds(); 245 return drawable_->GetVisibleBounds();
235 } 246 }
236 247
248 void TouchHandle::UpdateHandleLayout() {
249 // Suppress repositioning a handle while invisible or fading out to prevent it
250 // from "ghosting" outside the visible bounds. The position will be pushed to
251 // the drawable when the handle regains visibility (see |SetVisible()|).
252 if (!is_visible_ || !is_handle_layout_update_required_)
253 return;
254
255 is_handle_layout_update_required_ = false;
256
257 // Update mirror values only when dragging has stopped to prevent unwanted
258 // inversion while dragging of handles.
259 if (client_->IsAdaptiveHandleOrientationEnabled() && !is_dragging_) {
260 gfx::RectF handle_bounds = drawable_->GetVisibleBounds();
261 bool mirror_horizontal = false;
262 bool mirror_vertical = false;
263
264 const float handle_width =
265 handle_bounds.width() * (1.0 - handle_horizontal_padding_);
266 const float handle_height = handle_bounds.height();
267
268 const float bottom_y_unmirrored =
269 focus_bottom_.y() + handle_height + viewport_rect_.y();
270 const float top_y_mirrored =
271 focus_top_.y() - handle_height + viewport_rect_.y();
272
273 // In case the viewport height is small, like webview, avoid inversion.
274 if (bottom_y_unmirrored > viewport_rect_.bottom() &&
275 top_y_mirrored > viewport_rect_.y()) {
276 mirror_vertical = true;
277 }
278
279 if (orientation_ == TouchHandleOrientation::LEFT &&
280 focus_bottom_.x() - handle_width < viewport_rect_.x()) {
281 mirror_horizontal = true;
282 } else if (orientation_ == TouchHandleOrientation::RIGHT &&
283 focus_bottom_.x() + handle_width > viewport_rect_.right()) {
284 mirror_horizontal = true;
285 }
286
287 mirror_horizontal_ = mirror_horizontal;
288 mirror_vertical_ = mirror_vertical;
289 }
290
291 drawable_->SetOrientation(orientation_, mirror_vertical_, mirror_horizontal_);
292 drawable_->SetOrigin(ComputeHandleOrigin());
293 }
294
295 gfx::PointF TouchHandle::ComputeHandleOrigin() const {
296 gfx::PointF focus = mirror_vertical_ ? focus_top_ : focus_bottom_;
297 gfx::RectF drawable_bounds = drawable_->GetVisibleBounds();
298 float drawable_width = drawable_->GetVisibleBounds().width();
299
300 // Calculate the focal offsets from origin for the handle drawable
301 // based on the orientation.
302 int focal_offset_x = 0;
303 int focal_offset_y = mirror_vertical_ ? drawable_bounds.height() : 0;
304 switch (orientation_) {
305 case ui::TouchHandleOrientation::LEFT:
306 focal_offset_x =
307 mirror_horizontal_
308 ? drawable_width * handle_horizontal_padding_
309 : drawable_width * (1.0f - handle_horizontal_padding_);
310 break;
311 case ui::TouchHandleOrientation::RIGHT:
312 focal_offset_x =
313 mirror_horizontal_
314 ? drawable_width * (1.0f - handle_horizontal_padding_)
315 : drawable_width * handle_horizontal_padding_;
316 break;
317 case ui::TouchHandleOrientation::CENTER:
318 focal_offset_x = drawable_width * 0.5f;
319 break;
320 case ui::TouchHandleOrientation::UNDEFINED:
321 NOTREACHED() << "Invalid touch handle orientation.";
322 break;
323 };
324
325 return focus - gfx::Vector2dF(focal_offset_x, focal_offset_y);
326 }
327
237 void TouchHandle::BeginDrag() { 328 void TouchHandle::BeginDrag() {
238 DCHECK(enabled_); 329 DCHECK(enabled_);
239 if (is_dragging_) 330 if (is_dragging_)
240 return; 331 return;
241 EndFade(); 332 EndFade();
242 is_dragging_ = true; 333 is_dragging_ = true;
243 is_drag_within_tap_region_ = true; 334 is_drag_within_tap_region_ = true;
244 client_->OnDragBegin(*this, position()); 335 client_->OnDragBegin(*this, focus_bottom());
245 } 336 }
246 337
247 void TouchHandle::EndDrag() { 338 void TouchHandle::EndDrag() {
248 DCHECK(enabled_); 339 DCHECK(enabled_);
249 if (!is_dragging_) 340 if (!is_dragging_)
250 return; 341 return;
251 342
252 is_dragging_ = false; 343 is_dragging_ = false;
253 is_drag_within_tap_region_ = false; 344 is_drag_within_tap_region_ = false;
254 client_->OnDragEnd(*this); 345 client_->OnDragEnd(*this);
255 346
256 if (deferred_orientation_ != TouchHandleOrientation::UNDEFINED) { 347 if (deferred_orientation_ != TouchHandleOrientation::UNDEFINED) {
257 TouchHandleOrientation deferred_orientation = deferred_orientation_; 348 TouchHandleOrientation deferred_orientation = deferred_orientation_;
258 deferred_orientation_ = TouchHandleOrientation::UNDEFINED; 349 deferred_orientation_ = TouchHandleOrientation::UNDEFINED;
259 SetOrientation(deferred_orientation); 350 SetOrientation(deferred_orientation);
351 // Handle layout may is deferred while the handle is dragged.
352 SetUpdateLayoutRequired();
353 UpdateHandleLayout();
260 } 354 }
261 355
262 if (animate_deferred_fade_) { 356 if (animate_deferred_fade_) {
263 BeginFade(); 357 BeginFade();
264 } else { 358 } else {
265 // As drawable visibility assignment is deferred while dragging, push the 359 // As drawable visibility assignment is deferred while dragging, push the
266 // change by forcing fade completion. 360 // change by forcing fade completion.
267 EndFade(); 361 EndFade();
268 } 362 }
269 } 363 }
270 364
271 void TouchHandle::BeginFade() { 365 void TouchHandle::BeginFade() {
272 DCHECK(enabled_); 366 DCHECK(enabled_);
273 DCHECK(!is_dragging_); 367 DCHECK(!is_dragging_);
274 animate_deferred_fade_ = false; 368 animate_deferred_fade_ = false;
275 const float target_alpha = is_visible_ ? 1.f : 0.f; 369 const float target_alpha = is_visible_ ? 1.f : 0.f;
276 if (target_alpha == alpha_) { 370 if (target_alpha == alpha_) {
277 EndFade(); 371 EndFade();
278 return; 372 return;
279 } 373 }
280 374
281 fade_end_time_ = base::TimeTicks::Now() + 375 fade_end_time_ = base::TimeTicks::Now() +
282 base::TimeDelta::FromMillisecondsD( 376 base::TimeDelta::FromMillisecondsD(
283 kFadeDurationMs * std::abs(target_alpha - alpha_)); 377 kFadeDurationMs * std::abs(target_alpha - alpha_));
284 fade_start_position_ = position_; 378 fade_start_position_ = focus_bottom_;
285 client_->SetNeedsAnimate(); 379 client_->SetNeedsAnimate();
286 } 380 }
287 381
288 void TouchHandle::EndFade() { 382 void TouchHandle::EndFade() {
289 DCHECK(enabled_); 383 DCHECK(enabled_);
290 animate_deferred_fade_ = false; 384 animate_deferred_fade_ = false;
291 fade_end_time_ = base::TimeTicks(); 385 fade_end_time_ = base::TimeTicks();
292 SetAlpha(is_visible_ ? 1.f : 0.f); 386 SetAlpha(is_visible_ ? 1.f : 0.f);
293 } 387 }
294 388
295 void TouchHandle::SetAlpha(float alpha) { 389 void TouchHandle::SetAlpha(float alpha) {
296 alpha = std::max(0.f, std::min(1.f, alpha)); 390 alpha = std::max(0.f, std::min(1.f, alpha));
297 if (alpha_ == alpha) 391 if (alpha_ == alpha)
298 return; 392 return;
299 alpha_ = alpha; 393 alpha_ = alpha;
300 drawable_->SetAlpha(alpha); 394 drawable_->SetAlpha(alpha);
301 } 395 }
302 396
303 } // namespace ui 397 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698