OLD | NEW |
(Empty) | |
| 1 // Copyright 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 : delegate_(delegate), |
| 32 direction_(SLIDE_NONE), |
| 33 overscroll_cancelled_(false) { |
| 34 DCHECK(delegate_); |
| 35 } |
| 36 |
| 37 OverscrollWindowAnimation::~OverscrollWindowAnimation() { |
| 38 } |
| 39 |
| 40 void OverscrollWindowAnimation::CancelSlide() { |
| 41 ui::Layer* layer = GetFrontLayer(); |
| 42 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); |
| 43 settings.SetPreemptionStrategy( |
| 44 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 45 settings.SetTweenType(gfx::Tween::EASE_OUT); |
| 46 settings.AddObserver(this); |
| 47 layer->SetTransform(gfx::Transform()); |
| 48 overscroll_cancelled_ = true; |
| 49 } |
| 50 |
| 51 float OverscrollWindowAnimation::GetTranslationForOverscroll(float delta_x) { |
| 52 DCHECK(direction_ != SLIDE_NONE); |
| 53 const float bounds_width = GetVisibleBounds().width(); |
| 54 if (direction_ == SLIDE_FRONT) |
| 55 return std::max(-bounds_width, delta_x); |
| 56 else |
| 57 return std::min(bounds_width, delta_x); |
| 58 } |
| 59 |
| 60 gfx::Rect OverscrollWindowAnimation::GetVisibleBounds() const { |
| 61 return delegate_->GetMainWindow()->bounds(); |
| 62 } |
| 63 |
| 64 bool OverscrollWindowAnimation::OnOverscrollUpdate(float delta_x, |
| 65 float delta_y) { |
| 66 if (direction_ == SLIDE_NONE) |
| 67 return false; |
| 68 gfx::Transform transform; |
| 69 transform.Translate(GetTranslationForOverscroll(delta_x), 0); |
| 70 GetFrontLayer()->SetTransform(transform); |
| 71 return true; |
| 72 } |
| 73 |
| 74 void OverscrollWindowAnimation::OnImplicitAnimationsCompleted() { |
| 75 if (overscroll_cancelled_) { |
| 76 slide_window_.reset(); |
| 77 delegate_->OnOverscrollCancelled(); |
| 78 overscroll_cancelled_ = false; |
| 79 } else { |
| 80 delegate_->OnOverscrollCompleted(slide_window_.Pass()); |
| 81 } |
| 82 direction_ = SLIDE_NONE; |
| 83 } |
| 84 |
| 85 void OverscrollWindowAnimation::OnOverscrollModeChange( |
| 86 OverscrollMode old_mode, |
| 87 OverscrollMode new_mode) { |
| 88 Direction new_direction = GetDirectionForMode(new_mode); |
| 89 if (new_direction == SLIDE_NONE) { |
| 90 // The user cancelled the in progress animation. |
| 91 if (is_active()) |
| 92 CancelSlide(); |
| 93 return; |
| 94 } |
| 95 if (is_active()) |
| 96 GetFrontLayer()->GetAnimator()->StopAnimating(); |
| 97 |
| 98 gfx::Rect slide_window_bounds = gfx::Rect(GetVisibleBounds().size()); |
| 99 if (new_direction == SLIDE_FRONT) { |
| 100 slide_window_bounds.Offset(base::i18n::IsRTL() |
| 101 ? -slide_window_bounds.width() |
| 102 : slide_window_bounds.width(), |
| 103 0); |
| 104 } |
| 105 slide_window_ = new_direction == SLIDE_FRONT |
| 106 ? delegate_->CreateFrontWindow(slide_window_bounds) |
| 107 : delegate_->CreateBackWindow(slide_window_bounds); |
| 108 if (!slide_window_) { |
| 109 // Cannot navigate, do not start an overscroll gesture. |
| 110 direction_ = SLIDE_NONE; |
| 111 return; |
| 112 } |
| 113 overscroll_cancelled_ = false; |
| 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(slide_window_); |
| 146 return slide_window_->layer(); |
| 147 } |
| 148 return delegate_->GetMainWindow()->layer(); |
| 149 } |
| 150 |
| 151 } // namespace content |
OLD | NEW |