Chromium Code Reviews| Index: ui/touch_selection/touch_handle.cc |
| diff --git a/ui/touch_selection/touch_handle.cc b/ui/touch_selection/touch_handle.cc |
| index 0f28c3fedf8d69068acba4969db54e13cbd60bda..7951597c91ed890ff67d11ed666ede6f5cfc8d22 100644 |
| --- a/ui/touch_selection/touch_handle.cc |
| +++ b/ui/touch_selection/touch_handle.cc |
| @@ -73,12 +73,13 @@ TouchHandle::TouchHandle(TouchHandleClient* client, |
| enabled_(true), |
| is_visible_(false), |
| is_dragging_(false), |
| - is_drag_within_tap_region_(false) { |
| + is_drag_within_tap_region_(false), |
| + mirror_vertical_(false), |
| + mirror_horizontal_(false) { |
| DCHECK_NE(orientation, TouchHandleOrientation::UNDEFINED); |
| drawable_->SetEnabled(enabled_); |
| - drawable_->SetOrientation(orientation_); |
| + drawable_->SetLayout(focus_bottom_, orientation_, false, false); |
| drawable_->SetAlpha(alpha_); |
| - drawable_->SetFocus(position_); |
| } |
| TouchHandle::~TouchHandle() { |
| @@ -103,8 +104,7 @@ void TouchHandle::SetVisible(bool visible, AnimationStyle animation_style) { |
| is_visible_ = visible; |
| // Handle repositioning may have been deferred while previously invisible. |
| - if (visible) |
| - drawable_->SetFocus(position_); |
| + UpdateLayout(); |
| bool animate = animation_style != ANIMATION_NONE; |
| if (is_dragging_) { |
| @@ -118,16 +118,21 @@ void TouchHandle::SetVisible(bool visible, AnimationStyle animation_style) { |
| EndFade(); |
| } |
| -void TouchHandle::SetPosition(const gfx::PointF& position) { |
| - DCHECK(enabled_); |
| - if (position_ == position) |
| +void TouchHandle::SetFocus(const gfx::PointF& top, const gfx::PointF& bottom) { |
| + if (focus_top_ == top && focus_bottom_ == bottom) |
| return; |
| - position_ = position; |
| - // Suppress repositioning a handle while invisible or fading out to prevent it |
| - // from "ghosting" outside the visible bounds. The position will be pushed to |
| - // the drawable when the handle regains visibility (see |SetVisible()|). |
| - if (is_visible_) |
| - drawable_->SetFocus(position_); |
| + |
| + focus_top_ = top; |
| + focus_bottom_ = bottom; |
| + UpdateLayout(); |
|
jdduke (slow)
2015/05/11 15:40:15
Nice, I think this is a bit easier to follow.
AviD
2015/05/19 16:26:27
Acknowledged.
|
| +} |
| + |
| +void TouchHandle::SetViewportRect(const gfx::RectF viewport_rect) { |
| + if (viewport_rect_ == viewport_rect) |
| + return; |
| + |
| + viewport_rect_ = viewport_rect; |
| + UpdateLayout(); |
| } |
| void TouchHandle::SetOrientation(TouchHandleOrientation orientation) { |
| @@ -138,11 +143,12 @@ void TouchHandle::SetOrientation(TouchHandleOrientation orientation) { |
| return; |
| } |
| DCHECK_EQ(deferred_orientation_, TouchHandleOrientation::UNDEFINED); |
| + |
| if (orientation_ == orientation) |
| return; |
| orientation_ = orientation; |
| - drawable_->SetOrientation(orientation); |
| + UpdateLayout(); |
| } |
| bool TouchHandle::WillHandleTouchEvent(const MotionEvent& event) { |
| @@ -167,7 +173,7 @@ bool TouchHandle::WillHandleTouchEvent(const MotionEvent& event) { |
| return false; |
| } |
| touch_down_position_ = touch_point; |
| - touch_to_focus_offset_ = position_ - touch_down_position_; |
| + touch_to_focus_offset_ = focus_bottom_ - touch_down_position_; |
| touch_down_time_ = event.GetEventTime(); |
| BeginDrag(); |
| } break; |
| @@ -215,8 +221,8 @@ bool TouchHandle::Animate(base::TimeTicks frame_time) { |
| float time_u = |
| 1.f - (fade_end_time_ - frame_time).InMillisecondsF() / kFadeDurationMs; |
| - float position_u = |
| - (position_ - fade_start_position_).LengthSquared() / kFadeDistanceSquared; |
| + float position_u = (focus_bottom_ - fade_start_position_).LengthSquared() / |
| + kFadeDistanceSquared; |
| float u = std::max(time_u, position_u); |
| SetAlpha(is_visible_ ? u : 1.f - u); |
| @@ -228,6 +234,42 @@ bool TouchHandle::Animate(base::TimeTicks frame_time) { |
| return true; |
| } |
| +void TouchHandle::UpdateLayout() { |
| + // Suppress repositioning a handle while invisible or fading out to prevent it |
| + // from "ghosting" outside the visible bounds. The position will be pushed to |
| + // the drawable when the handle regains visibility (see |SetVisible()|). |
| + if (!is_visible_) |
| + return; |
| + |
| + gfx::RectF handle_bounds = drawable_->GetVisibleBounds(); |
|
mfomitchev
2015/05/13 22:19:14
move this inside the if
AviD
2015/05/19 16:26:27
Done.
|
| + |
| + // Update mirror values only when dragging has stopped to prevent unwanted |
| + // inversion while dragging of handles |
| + if (!is_dragging_) { |
| + mirror_horizontal_ = false; |
| + mirror_vertical_ = false; |
| + |
| + if (focus_bottom_.y() + handle_bounds.height() > viewport_rect_.bottom()) { |
| + mirror_vertical_ = true; |
| + } |
| + |
| + if (orientation_ == TouchHandleOrientation::LEFT && |
| + focus_bottom_.x() - handle_bounds.width() < viewport_rect_.x()) { |
|
mfomitchev
2015/05/13 22:19:14
This assumes that handle is drawn without any offs
jdduke (slow)
2015/05/14 15:20:10
I believe so, yes. The |handle_bounds| should give
jdduke (slow)
2015/05/14 15:20:10
Good catch, |handle_bounds| should already be in v
mfomitchev
2015/05/14 15:52:39
Yeah, and the coordinates from the previous frame
AviD
2015/05/19 16:26:27
The focal_offset_from_origin is always 1/4th the w
jdduke (slow)
2015/05/19 18:59:26
We've made this assumption until now and it's work
AviD
2015/05/21 09:23:14
I have changed it to (handle_bounds.width() * 0.75
|
| + mirror_horizontal_ = true; |
| + } |
| + |
| + if (orientation_ == TouchHandleOrientation::RIGHT && |
|
mfomitchev
2015/05/13 22:19:14
else if
AviD
2015/05/19 16:26:28
Done.
|
| + focus_bottom_.x() + handle_bounds.width() > viewport_rect_.right()) { |
| + mirror_horizontal_ = true; |
| + } |
| + } |
| + |
| + gfx::PointF position = mirror_vertical_ ? focus_top_ : focus_bottom_; |
| + |
| + drawable_->SetLayout(position, orientation_, mirror_vertical_, |
| + mirror_horizontal_); |
| +} |
| + |
| void TouchHandle::BeginDrag() { |
| DCHECK(enabled_); |
| if (is_dragging_) |
| @@ -251,6 +293,7 @@ void TouchHandle::EndDrag() { |
| TouchHandleOrientation deferred_orientation = deferred_orientation_; |
| deferred_orientation_ = TouchHandleOrientation::UNDEFINED; |
| SetOrientation(deferred_orientation); |
| + UpdateLayout(); |
|
mfomitchev
2015/05/13 22:19:14
// Handle layout may is deferred while the handle
AviD
2015/05/19 16:26:27
Done.
|
| } |
| if (animate_deferred_fade_) { |
| @@ -275,7 +318,7 @@ void TouchHandle::BeginFade() { |
| fade_end_time_ = base::TimeTicks::Now() + |
| base::TimeDelta::FromMillisecondsD( |
| kFadeDurationMs * std::abs(target_alpha - alpha_)); |
| - fade_start_position_ = position_; |
| + fade_start_position_ = focus_bottom_; |
| client_->SetNeedsAnimate(); |
| } |