Chromium Code Reviews| 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 : window_(nullptr), | |
|
mfomitchev
2015/03/26 15:36:00
no need to init scoped ptr to null
Nina
2015/03/27 17:52:36
Done.
| |
| 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(float delta_x) { | |
| 54 DCHECK(direction_ != SLIDE_NONE); | |
| 55 const float bounds_width = GetVisibleBounds().width(); | |
| 56 if (direction_ == SLIDE_FRONT) | |
| 57 return std::max(-bounds_width, delta_x); | |
| 58 else | |
| 59 return std::min(bounds_width, delta_x); | |
| 60 } | |
| 61 | |
| 62 gfx::Rect OverscrollWindowAnimation::GetVisibleBounds() const { | |
| 63 return delegate_->GetTargetWindow()->bounds(); | |
| 64 } | |
| 65 | |
| 66 bool OverscrollWindowAnimation::OnOverscrollUpdate(float delta_x, | |
| 67 float delta_y) { | |
| 68 if (direction_ == SLIDE_NONE) | |
| 69 return false; | |
| 70 gfx::Transform transform; | |
| 71 transform.Translate(GetTranslationForOverscroll(delta_x), 0); | |
| 72 GetFrontLayer()->SetTransform(transform); | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 void OverscrollWindowAnimation::OnImplicitAnimationsCompleted() { | |
| 77 if (overscroll_cancelled_) { | |
| 78 window_.reset(); | |
| 79 delegate_->OnOverscrollAborted(); | |
| 80 } else { | |
| 81 delegate_->OnOverscrollCompleted(window_.Pass()); | |
| 82 } | |
| 83 direction_ = SLIDE_NONE; | |
| 84 } | |
| 85 | |
| 86 void OverscrollWindowAnimation::OnOverscrollModeChange( | |
| 87 OverscrollMode old_mode, | |
| 88 OverscrollMode new_mode) { | |
| 89 Direction new_direction = GetDirectionForMode(new_mode); | |
| 90 if (new_direction == SLIDE_NONE) { | |
| 91 // The user cancelled the in progress overscroll gesture. | |
| 92 if (is_active()) | |
| 93 CancelOverscroll(); | |
| 94 return; | |
| 95 } | |
| 96 if (window_) | |
| 97 GetFrontLayer()->GetAnimator()->StopAnimating(); | |
| 98 window_ = new_direction == SLIDE_FRONT ? delegate_->CreateFrontWindow() | |
| 99 : delegate_->CreateBackWindow(); | |
| 100 if (!window_) { | |
| 101 // Cannot navigate, do not start an overscroll gesture. | |
| 102 direction_ = SLIDE_NONE; | |
| 103 return; | |
| 104 } | |
| 105 overscroll_cancelled_ = false; | |
| 106 gfx::Rect bounds = gfx::Rect(GetVisibleBounds().size()); | |
| 107 // Make sure the live window is in its default position. | |
| 108 delegate_->GetTargetWindow()->SetBounds(bounds); | |
| 109 delegate_->GetTargetWindow()->SetTransform(gfx::Transform()); | |
| 110 delegate_->GetTargetWindow()->layer()->GetAnimator()->AbortAllAnimations(); | |
|
mfomitchev
2015/03/26 15:36:00
Is this a left over from the time when we had two
Nina
2015/03/27 17:52:35
We still need to make sure the bounds are OK, but
| |
| 111 if (new_direction == OverscrollWindowAnimation::SLIDE_FRONT) | |
| 112 bounds.Offset(base::i18n::IsRTL() ? -bounds.width() : bounds.width(), 0); | |
| 113 window_->SetBounds(bounds); | |
| 114 direction_ = new_direction; | |
| 115 shadow_.reset(new ShadowLayerDelegate(GetFrontLayer())); | |
| 116 } | |
| 117 | |
| 118 void OverscrollWindowAnimation::OnOverscrollComplete( | |
| 119 OverscrollMode overscroll_mode) { | |
| 120 if (!is_active()) | |
| 121 return; | |
| 122 delegate_->OnOverscrollCompleting(); | |
| 123 int content_width = GetVisibleBounds().width(); | |
| 124 float translate_x; | |
| 125 if ((base::i18n::IsRTL() && direction_ == SLIDE_FRONT) || | |
| 126 (!base::i18n::IsRTL() && direction_ == SLIDE_BACK)) { | |
| 127 translate_x = content_width; | |
| 128 } else { | |
| 129 translate_x = -content_width; | |
| 130 } | |
| 131 ui::Layer* layer = GetFrontLayer(); | |
| 132 gfx::Transform transform; | |
| 133 transform.Translate(translate_x, 0); | |
| 134 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); | |
| 135 settings.SetPreemptionStrategy( | |
| 136 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 137 settings.SetTweenType(gfx::Tween::EASE_OUT); | |
| 138 settings.AddObserver(this); | |
| 139 layer->SetTransform(transform); | |
| 140 } | |
| 141 | |
| 142 ui::Layer* OverscrollWindowAnimation::GetFrontLayer() const { | |
| 143 DCHECK(direction_ != SLIDE_NONE); | |
| 144 if (direction_ == SLIDE_FRONT) { | |
| 145 DCHECK(window_); | |
| 146 return window_->layer(); | |
| 147 } | |
| 148 return delegate_->GetTargetWindow()->layer(); | |
| 149 } | |
| 150 | |
| 151 } // namespace content | |
| OLD | NEW |