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

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: Fixed 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) {
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 gfx::PointF TouchHandle::GetHandleOrigin() const {
jdduke (slow) 2015/08/17 17:20:53 This definition should follow the declaration orde
AviD 2015/08/18 13:56:14 Done.
249 gfx::PointF focus = mirror_vertical_ ? focus_top_ : focus_bottom_;
250 gfx::RectF drawable_bounds = drawable_->GetVisibleBounds();
251 float drawable_width = drawable_->GetVisibleBounds().width();
252
253 // Calculate the focal offsets from origin for the handle drawable
254 // based on the orientation
jdduke (slow) 2015/08/17 17:20:53 Nit: period at the end of comment.
AviD 2015/08/18 13:56:14 Done.
255 int focal_offset_x = 0;
256 int focal_offset_y = mirror_vertical_ ? drawable_bounds.height() : 0;
257 switch (orientation_) {
258 case ui::TouchHandleOrientation::LEFT:
259 focal_offset_x =
260 mirror_horizontal_
261 ? drawable_width * handle_horizontal_padding_
262 : drawable_width * (1.0f - handle_horizontal_padding_);
263 break;
264 case ui::TouchHandleOrientation::RIGHT:
265 focal_offset_x =
266 mirror_horizontal_
267 ? drawable_width * (1.0f - handle_horizontal_padding_)
268 : drawable_width * handle_horizontal_padding_;
269 break;
270 case ui::TouchHandleOrientation::CENTER:
271 focal_offset_x = drawable_width * 0.5f;
272 break;
273 case ui::TouchHandleOrientation::UNDEFINED:
274 NOTREACHED() << "Invalid touch handle orientation.";
275 break;
276 };
277
278 return focus - gfx::Vector2dF(focal_offset_x, focal_offset_y);
279 }
280
281 void TouchHandle::UpdateHandleLayout() {
282 // Suppress repositioning a handle while invisible or fading out to prevent it
283 // from "ghosting" outside the visible bounds. The position will be pushed to
284 // the drawable when the handle regains visibility (see |SetVisible()|).
285 if (!is_visible_ || !is_handle_layout_update_required_)
286 return;
287
288 is_handle_layout_update_required_ = false;
289
290 // Update mirror values only when dragging has stopped to prevent unwanted
291 // inversion while dragging of handles
jdduke (slow) 2015/08/17 17:20:53 Nit: period at end of comment.
AviD 2015/08/18 13:56:14 Done.
292 if (client_->IsEnabledAdaptiveHandleOrientation() && !is_dragging_) {
293 gfx::RectF handle_bounds = drawable_->GetVisibleBounds();
294 bool mirror_horizontal = false;
295 bool mirror_vertical = false;
296
297 const float handle_width =
298 handle_bounds.width() * (1.0 - handle_horizontal_padding_);
299 const float handle_height = handle_bounds.height();
300
301 const float bottom_y_unmirrored =
302 focus_bottom_.y() + handle_height + viewport_rect_.y();
303 const float top_y_mirrored =
304 focus_top_.y() - handle_height + viewport_rect_.y();
305
306 // In case the viewport height is small, like webview, avoid inversion.
307 if (bottom_y_unmirrored > viewport_rect_.bottom() &&
308 top_y_mirrored > viewport_rect_.y()) {
309 mirror_vertical = true;
310 }
311
312 if (orientation_ == TouchHandleOrientation::LEFT &&
313 focus_bottom_.x() - handle_width < viewport_rect_.x()) {
314 mirror_horizontal = true;
315 } else if (orientation_ == TouchHandleOrientation::RIGHT &&
316 focus_bottom_.x() + handle_width > viewport_rect_.right()) {
317 mirror_horizontal = true;
318 }
319
320 mirror_horizontal_ = mirror_horizontal;
321 mirror_vertical_ = mirror_vertical;
322 }
323
324 drawable_->SetOrientation(orientation_, mirror_vertical_, mirror_horizontal_);
325
326 gfx::PointF origin = GetHandleOrigin();
327 drawable_->SetOrigin(origin);
jdduke (slow) 2015/08/17 17:20:53 Nit: Let's just inline this, drawable_->SetOrigin(
AviD 2015/08/18 13:56:14 Done.
328 }
329
237 void TouchHandle::BeginDrag() { 330 void TouchHandle::BeginDrag() {
238 DCHECK(enabled_); 331 DCHECK(enabled_);
239 if (is_dragging_) 332 if (is_dragging_)
240 return; 333 return;
241 EndFade(); 334 EndFade();
242 is_dragging_ = true; 335 is_dragging_ = true;
243 is_drag_within_tap_region_ = true; 336 is_drag_within_tap_region_ = true;
244 client_->OnDragBegin(*this, position()); 337 client_->OnDragBegin(*this, focus_bottom());
245 } 338 }
246 339
247 void TouchHandle::EndDrag() { 340 void TouchHandle::EndDrag() {
248 DCHECK(enabled_); 341 DCHECK(enabled_);
249 if (!is_dragging_) 342 if (!is_dragging_)
250 return; 343 return;
251 344
252 is_dragging_ = false; 345 is_dragging_ = false;
253 is_drag_within_tap_region_ = false; 346 is_drag_within_tap_region_ = false;
254 client_->OnDragEnd(*this); 347 client_->OnDragEnd(*this);
255 348
256 if (deferred_orientation_ != TouchHandleOrientation::UNDEFINED) { 349 if (deferred_orientation_ != TouchHandleOrientation::UNDEFINED) {
257 TouchHandleOrientation deferred_orientation = deferred_orientation_; 350 TouchHandleOrientation deferred_orientation = deferred_orientation_;
258 deferred_orientation_ = TouchHandleOrientation::UNDEFINED; 351 deferred_orientation_ = TouchHandleOrientation::UNDEFINED;
259 SetOrientation(deferred_orientation); 352 SetOrientation(deferred_orientation);
353 // Handle layout may is deferred while the handle is dragged.
354 SetUpdateLayoutRequired();
355 UpdateHandleLayout();
260 } 356 }
261 357
262 if (animate_deferred_fade_) { 358 if (animate_deferred_fade_) {
263 BeginFade(); 359 BeginFade();
264 } else { 360 } else {
265 // As drawable visibility assignment is deferred while dragging, push the 361 // As drawable visibility assignment is deferred while dragging, push the
266 // change by forcing fade completion. 362 // change by forcing fade completion.
267 EndFade(); 363 EndFade();
268 } 364 }
269 } 365 }
270 366
271 void TouchHandle::BeginFade() { 367 void TouchHandle::BeginFade() {
272 DCHECK(enabled_); 368 DCHECK(enabled_);
273 DCHECK(!is_dragging_); 369 DCHECK(!is_dragging_);
274 animate_deferred_fade_ = false; 370 animate_deferred_fade_ = false;
275 const float target_alpha = is_visible_ ? 1.f : 0.f; 371 const float target_alpha = is_visible_ ? 1.f : 0.f;
276 if (target_alpha == alpha_) { 372 if (target_alpha == alpha_) {
277 EndFade(); 373 EndFade();
278 return; 374 return;
279 } 375 }
280 376
281 fade_end_time_ = base::TimeTicks::Now() + 377 fade_end_time_ = base::TimeTicks::Now() +
282 base::TimeDelta::FromMillisecondsD( 378 base::TimeDelta::FromMillisecondsD(
283 kFadeDurationMs * std::abs(target_alpha - alpha_)); 379 kFadeDurationMs * std::abs(target_alpha - alpha_));
284 fade_start_position_ = position_; 380 fade_start_position_ = focus_bottom_;
285 client_->SetNeedsAnimate(); 381 client_->SetNeedsAnimate();
286 } 382 }
287 383
288 void TouchHandle::EndFade() { 384 void TouchHandle::EndFade() {
289 DCHECK(enabled_); 385 DCHECK(enabled_);
290 animate_deferred_fade_ = false; 386 animate_deferred_fade_ = false;
291 fade_end_time_ = base::TimeTicks(); 387 fade_end_time_ = base::TimeTicks();
292 SetAlpha(is_visible_ ? 1.f : 0.f); 388 SetAlpha(is_visible_ ? 1.f : 0.f);
293 } 389 }
294 390
295 void TouchHandle::SetAlpha(float alpha) { 391 void TouchHandle::SetAlpha(float alpha) {
296 alpha = std::max(0.f, std::min(1.f, alpha)); 392 alpha = std::max(0.f, std::min(1.f, alpha));
297 if (alpha_ == alpha) 393 if (alpha_ == alpha)
298 return; 394 return;
299 alpha_ = alpha; 395 alpha_ = alpha;
300 drawable_->SetAlpha(alpha); 396 drawable_->SetAlpha(alpha);
301 } 397 }
302 398
303 } // namespace ui 399 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698