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 bounds = gfx::Rect(GetVisibleBounds().size()); | |
mfomitchev
2015/04/08 17:33:16
maybe call this slide_window_bounds?
Nina
2015/04/08 17:46:49
Done.
| |
99 if (new_direction == SLIDE_FRONT) | |
100 bounds.Offset(base::i18n::IsRTL() ? -bounds.width() : bounds.width(), 0); | |
101 slide_window_ = | |
102 new_direction == SLIDE_FRONT ? delegate_->CreateFrontWindow(bounds) | |
103 : delegate_->CreateBackWindow(bounds); | |
104 if (!slide_window_) { | |
105 // Cannot navigate, do not start an overscroll gesture. | |
106 direction_ = SLIDE_NONE; | |
107 return; | |
108 } | |
109 overscroll_cancelled_ = false; | |
110 direction_ = new_direction; | |
111 shadow_.reset(new ShadowLayerDelegate(GetFrontLayer())); | |
112 } | |
113 | |
114 void OverscrollWindowAnimation::OnOverscrollComplete( | |
115 OverscrollMode overscroll_mode) { | |
116 if (!is_active()) | |
117 return; | |
118 delegate_->OnOverscrollCompleting(); | |
119 int content_width = GetVisibleBounds().width(); | |
120 float translate_x; | |
121 if ((base::i18n::IsRTL() && direction_ == SLIDE_FRONT) || | |
122 (!base::i18n::IsRTL() && direction_ == SLIDE_BACK)) { | |
123 translate_x = content_width; | |
124 } else { | |
125 translate_x = -content_width; | |
126 } | |
127 ui::Layer* layer = GetFrontLayer(); | |
128 gfx::Transform transform; | |
129 transform.Translate(translate_x, 0); | |
130 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); | |
131 settings.SetPreemptionStrategy( | |
132 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
133 settings.SetTweenType(gfx::Tween::EASE_OUT); | |
134 settings.AddObserver(this); | |
135 layer->SetTransform(transform); | |
136 } | |
137 | |
138 ui::Layer* OverscrollWindowAnimation::GetFrontLayer() const { | |
139 DCHECK(direction_ != SLIDE_NONE); | |
140 if (direction_ == SLIDE_FRONT) { | |
141 DCHECK(slide_window_); | |
142 return slide_window_->layer(); | |
143 } | |
144 return delegate_->GetMainWindow()->layer(); | |
145 } | |
146 | |
147 } // namespace content | |
OLD | NEW |