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 "base/i18n/rtl.h" | |
8 #include "content/browser/web_contents/aura/overscroll_layer_wrapper.h" | |
9 #include "content/browser/web_contents/aura/shadow_layer_delegate.h" | |
10 #include "ui/aura/window.h" | |
11 #include "ui/compositor/layer_animation_observer.h" | |
12 #include "ui/compositor/scoped_layer_animation_settings.h" | |
13 | |
14 namespace content { | |
15 | |
16 namespace { | |
17 | |
18 OverscrollWindowAnimation::Direction GetDirectionForMode(OverscrollMode mode) { | |
19 LOG(ERROR) << "Getting navigation direction"; | |
20 if (mode == (base::i18n::IsRTL() ? OVERSCROLL_EAST : OVERSCROLL_WEST)) | |
21 return OverscrollWindowAnimation::FORWARD; | |
22 if (mode == (base::i18n::IsRTL() ? OVERSCROLL_WEST : OVERSCROLL_EAST)) | |
23 return OverscrollWindowAnimation::BACKWARD; | |
24 return OverscrollWindowAnimation::NONE; | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 OverscrollWindowAnimation::OverscrollWindowAnimation() | |
30 : layer_wrapper_(nullptr), | |
31 shadow_(nullptr), | |
32 live_window_(nullptr), | |
mfomitchev
2015/03/05 23:37:06
Don't allow this or add null checks elsewhere (or
Nina
2015/03/09 15:54:52
Done.
| |
33 delegate_(nullptr), | |
mfomitchev
2015/03/05 23:37:06
Same thing here
Nina
2015/03/09 15:54:52
Done.
| |
34 direction_(NONE) { | |
35 } | |
36 | |
37 OverscrollWindowAnimation::~OverscrollWindowAnimation() { | |
38 LOG(ERROR) << "OWA destructor"; | |
39 } | |
40 | |
41 void OverscrollWindowAnimation::CancelOverscroll() { | |
42 LOG(ERROR) << "OWA: Cancelling overscroll"; | |
43 ui::Layer* layer = GetAnimatedLayer(); | |
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 direction_ = NONE; | |
51 } | |
52 | |
53 float OverscrollWindowAnimation::GetTranslationForOverscroll( | |
54 float delta_x) { | |
55 LOG(ERROR) << "Translation for delta_x: " << delta_x; | |
56 DCHECK(direction_ != NONE); | |
57 const float bounds_width = layer_wrapper_->GetBounds().width(); | |
58 if (direction_ == FORWARD) | |
59 return std::max(-bounds_width, delta_x); | |
60 else | |
61 return std::min(bounds_width, delta_x); | |
62 } | |
63 | |
64 gfx::Rect OverscrollWindowAnimation::GetVisibleBounds() const { | |
65 return live_window_->bounds(); | |
66 } | |
67 | |
68 bool OverscrollWindowAnimation::OnOverscrollUpdate(float delta_x, | |
69 float delta_y) { | |
70 if (direction_ == NONE) | |
71 return false; | |
72 LOG(ERROR) << "OWA: OnOverscrollUpdate"; | |
73 gfx::Transform transform; | |
74 transform.Translate(GetTranslationForOverscroll(delta_x), 0); | |
75 GetAnimatedLayer()->SetTransform(transform); | |
76 return true; | |
77 } | |
78 | |
79 void OverscrollWindowAnimation::OnImplicitAnimationsCompleted() { | |
mfomitchev
2015/03/05 23:37:06
Failure vs Success
Nina
2015/03/09 15:54:52
Done.
| |
80 LOG(ERROR) << "OnImplicitAnimationsCompleted"; | |
81 LOG(ERROR) << "LAYER_WRAPPER_: " << layer_wrapper_; | |
82 if (layer_wrapper_) | |
83 delegate_->OnOverscrollCompleted(layer_wrapper_.Pass()); | |
84 } | |
85 | |
86 void OverscrollWindowAnimation::OnOverscrollModeChange( | |
87 OverscrollMode old_mode, | |
88 OverscrollMode new_mode) { | |
89 LOG(ERROR) << "OWA: OnOverscrollModeChange for mode " << new_mode; | |
90 Direction new_direction = GetDirectionForMode(new_mode); | |
91 | |
92 if (new_direction == NONE) { | |
93 // The user cancelled the in progress overscroll gesture. | |
94 // TODO(nsatragno): in this case, show a feedback animation. | |
95 LOG(ERROR) << "Direction: NONE"; | |
96 if (is_active()) | |
97 CancelOverscroll(); | |
98 return; | |
99 } | |
100 LOG(ERROR) << "Preparing layer wrapper"; | |
101 if (layer_wrapper_) { | |
102 LOG(ERROR) << "ALERT: MODE CHANGE WITH LAYER WRAPPER ACTIVE! " | |
103 << "Aborting animations."; | |
104 } | |
105 layer_wrapper_ = new_direction == FORWARD ? delegate_->CreateFrontLayer() | |
106 : delegate_->CreateBackLayer(); | |
107 if (!layer_wrapper_) { | |
108 LOG(ERROR) << "Cannot navigate, setting direction to NONE"; | |
109 direction_ = NONE; | |
110 return; | |
111 } | |
112 gfx::Rect bounds = gfx::Rect(live_window_->bounds().size()); | |
113 if (new_direction == OverscrollWindowAnimation::FORWARD) | |
114 bounds.Offset(base::i18n::IsRTL() ? -bounds.width() : bounds.width(), 0); | |
115 // TODO maybe we want to set the window bounds here, too. | |
116 layer_wrapper_->layer()->SetBounds(bounds); | |
117 LOG(ERROR) << "Setting shadow delegate."; | |
118 direction_ = new_direction; | |
119 shadow_.reset(new ShadowLayerDelegate(GetAnimatedLayer())); | |
120 } | |
121 | |
122 void OverscrollWindowAnimation::OnOverscrollComplete( | |
123 OverscrollMode overscroll_mode) { | |
124 LOG(ERROR) << "OWA: OnOverscrollComplete"; | |
125 // TODO we might want to also check for direction_ == NONE. | |
126 // I think there is a case in which is_active == false && direction_ == NONE, | |
127 // but I can't find it. | |
128 if (!is_active()) | |
129 return; | |
130 delegate_->OnOverscrollCompleting(); | |
131 int content_width = layer_wrapper_->GetBounds().width(); | |
132 float translate_x; | |
133 if ((base::i18n::IsRTL() && direction_ == FORWARD) || | |
134 (!base::i18n::IsRTL() && direction_ == BACKWARD)) { | |
135 translate_x = content_width; | |
136 } else { | |
137 translate_x = -content_width; | |
138 } | |
139 // TODO refactor into one function. | |
140 ui::Layer* layer = GetAnimatedLayer(); | |
141 gfx::Transform transform; | |
142 transform.Translate(translate_x, 0); | |
143 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); | |
144 settings.SetPreemptionStrategy( | |
145 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
146 settings.SetTweenType(gfx::Tween::EASE_OUT); | |
147 settings.AddObserver(this); | |
148 layer->SetTransform(transform); | |
149 direction_ = NONE; | |
150 } | |
151 | |
152 ui::Layer* OverscrollWindowAnimation::GetAnimatedLayer() { | |
mfomitchev
2015/03/06 01:36:43
Perhaps call this GetFrontLayer() - this way we wo
Nina
2015/03/09 15:54:52
Done.
| |
153 if (direction_ == FORWARD) | |
154 return layer_wrapper_->layer(); | |
155 return live_window_->layer(); | |
156 } | |
157 | |
158 } // namespace content | |
OLD | NEW |