OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 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 "content/browser/web_contents/aura/overscroll_window_animation.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/i18n/rtl.h" |
| 10 #include "content/browser/web_contents/aura/shadow_layer_delegate.h" |
| 11 #include "content/browser/web_contents/web_contents_impl.h" |
| 12 #include "ui/aura/window.h" |
| 13 #include "ui/compositor/layer_animation_observer.h" |
| 14 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 namespace { |
| 19 |
| 20 OverscrollWindowAnimation::Direction GetDirectionForMode(OverscrollMode mode) { |
| 21 if (mode == (base::i18n::IsRTL() ? OVERSCROLL_EAST : OVERSCROLL_WEST)) |
| 22 return OverscrollWindowAnimation::SLIDE_FRONT; |
| 23 if (mode == (base::i18n::IsRTL() ? OVERSCROLL_WEST : OVERSCROLL_EAST)) |
| 24 return OverscrollWindowAnimation::SLIDE_BACK; |
| 25 return OverscrollWindowAnimation::SLIDE_NONE; |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
| 30 OverscrollWindowAnimation::OverscrollWindowAnimation(Delegate* delegate) |
| 31 : wrapper_(nullptr), |
| 32 shadow_(nullptr), |
| 33 delegate_(delegate), |
| 34 direction_(SLIDE_NONE), |
| 35 overscroll_cancelled_(false) { |
| 36 DCHECK(delegate_); |
| 37 } |
| 38 |
| 39 OverscrollWindowAnimation::~OverscrollWindowAnimation() { |
| 40 } |
| 41 |
| 42 void OverscrollWindowAnimation::CancelOverscroll() { |
| 43 ui::Layer* layer = GetFrontLayer(); |
| 44 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); |
| 45 settings.SetPreemptionStrategy( |
| 46 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 47 settings.SetTweenType(gfx::Tween::EASE_OUT); |
| 48 settings.AddObserver(this); |
| 49 layer->SetTransform(gfx::Transform()); |
| 50 overscroll_cancelled_ = true; |
| 51 } |
| 52 |
| 53 float OverscrollWindowAnimation::GetTranslationForOverscroll( |
| 54 float delta_x) { |
| 55 DCHECK(direction_ != SLIDE_NONE); |
| 56 const float bounds_width = GetVisibleBounds().width(); |
| 57 if (direction_ == SLIDE_FRONT) |
| 58 return std::max(-bounds_width, delta_x); |
| 59 else |
| 60 return std::min(bounds_width, delta_x); |
| 61 } |
| 62 |
| 63 gfx::Rect OverscrollWindowAnimation::GetVisibleBounds() const { |
| 64 return delegate_->GetTargetWindow()->bounds(); |
| 65 } |
| 66 |
| 67 bool OverscrollWindowAnimation::OnOverscrollUpdate(float delta_x, |
| 68 float delta_y) { |
| 69 if (direction_ == SLIDE_NONE) |
| 70 return false; |
| 71 gfx::Transform transform; |
| 72 transform.Translate(GetTranslationForOverscroll(delta_x), 0); |
| 73 GetFrontLayer()->SetTransform(transform); |
| 74 return true; |
| 75 } |
| 76 |
| 77 void OverscrollWindowAnimation::OnImplicitAnimationsCompleted() { |
| 78 if (overscroll_cancelled_) { |
| 79 wrapper_.reset(); |
| 80 delegate_->OnOverscrollAborted(); |
| 81 } else { |
| 82 delegate_->OnOverscrollCompleted(wrapper_.Pass()); |
| 83 } |
| 84 direction_ = SLIDE_NONE; |
| 85 } |
| 86 |
| 87 void OverscrollWindowAnimation::OnOverscrollModeChange( |
| 88 OverscrollMode old_mode, |
| 89 OverscrollMode new_mode) { |
| 90 Direction new_direction = GetDirectionForMode(new_mode); |
| 91 if (new_direction == SLIDE_NONE) { |
| 92 // The user cancelled the in progress overscroll gesture. |
| 93 if (direction_ != SLIDE_NONE) |
| 94 CancelOverscroll(); |
| 95 return; |
| 96 } |
| 97 if (wrapper_) |
| 98 GetFrontLayer()->GetAnimator()->StopAnimating(); |
| 99 wrapper_ = |
| 100 new_direction == SLIDE_FRONT ? delegate_->CreateFrontWrapper() |
| 101 : delegate_->CreateBackWrapper(); |
| 102 if (!wrapper_) { |
| 103 // Cannot navigate, do not start an overscroll gesture. |
| 104 direction_ = SLIDE_NONE; |
| 105 return; |
| 106 } |
| 107 overscroll_cancelled_ = false; |
| 108 gfx::Rect bounds = gfx::Rect(GetVisibleBounds().size()); |
| 109 // Make sure the live window is in its default position. |
| 110 delegate_->GetTargetWindow()->SetBounds(bounds); |
| 111 delegate_->GetTargetWindow()->SetTransform(gfx::Transform()); |
| 112 delegate_->GetTargetWindow()->layer()->GetAnimator()->AbortAllAnimations(); |
| 113 if (new_direction == OverscrollWindowAnimation::SLIDE_FRONT) |
| 114 bounds.Offset(base::i18n::IsRTL() ? -bounds.width() : bounds.width(), 0); |
| 115 wrapper_->SetBounds(bounds); |
| 116 direction_ = new_direction; |
| 117 shadow_.reset(new ShadowLayerDelegate(GetFrontLayer())); |
| 118 } |
| 119 |
| 120 void OverscrollWindowAnimation::OnOverscrollComplete( |
| 121 OverscrollMode overscroll_mode) { |
| 122 if (!is_active()) |
| 123 return; |
| 124 delegate_->OnOverscrollCompleting(); |
| 125 int content_width = GetVisibleBounds().width(); |
| 126 float translate_x; |
| 127 if ((base::i18n::IsRTL() && direction_ == SLIDE_FRONT) || |
| 128 (!base::i18n::IsRTL() && direction_ == SLIDE_BACK)) { |
| 129 translate_x = content_width; |
| 130 } else { |
| 131 translate_x = -content_width; |
| 132 } |
| 133 ui::Layer* layer = GetFrontLayer(); |
| 134 gfx::Transform transform; |
| 135 transform.Translate(translate_x, 0); |
| 136 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); |
| 137 settings.SetPreemptionStrategy( |
| 138 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 139 settings.SetTweenType(gfx::Tween::EASE_OUT); |
| 140 settings.AddObserver(this); |
| 141 layer->SetTransform(transform); |
| 142 } |
| 143 |
| 144 ui::Layer* OverscrollWindowAnimation::GetFrontLayer() const { |
| 145 DCHECK(direction_ != SLIDE_NONE); |
| 146 if (direction_ == SLIDE_FRONT) { |
| 147 DCHECK(wrapper_); |
| 148 return wrapper_->GetLayer(); |
| 149 } |
| 150 return delegate_->GetTargetWindow()->layer(); |
| 151 } |
| 152 |
| 153 } // namespace content |
OLD | NEW |