OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012 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 "cc/top_controls_manager.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/time.h" | |
11 #include "cc/keyframed_animation_curve.h" | |
12 #include "cc/layer_tree_impl.h" | |
13 #include "cc/timing_function.h" | |
14 #include "cc/top_controls_manager_client.h" | |
15 #include "ui/gfx/transform.h" | |
16 #include "ui/gfx/vector2d_f.h" | |
17 | |
18 namespace cc { | |
19 namespace { | |
20 // These constants were chosen empirically for their visually pleasant behavior. | |
21 // Contact tedchoc@chromium.org for questions about changing these values. | |
22 const float kShowHideThreshold = 0.75f; | |
23 const int64 kShowHideMaxDurationMs = 250; | |
24 } | |
25 | |
26 // static | |
27 scoped_ptr<TopControlsManager> TopControlsManager::Create( | |
28 TopControlsManagerClient* client, float top_controls_height) { | |
29 return make_scoped_ptr(new TopControlsManager(client, top_controls_height)); | |
30 } | |
31 | |
32 TopControlsManager::TopControlsManager(TopControlsManagerClient* client, | |
33 float top_controls_height) | |
34 : client_(client), | |
35 is_overlay_mode_(false), | |
36 top_controls_height_(top_controls_height), | |
37 controls_top_offset_(0), | |
38 content_top_offset_(top_controls_height), | |
39 previous_root_scroll_offset_(0.f), | |
40 scroll_readjustment_enabled_(false), | |
41 is_showing_animation_(false) { | |
42 CHECK(client_); | |
43 } | |
44 | |
45 TopControlsManager::~TopControlsManager() { | |
46 } | |
47 | |
48 void TopControlsManager::UpdateDrawPositions() { | |
49 if (!RootScrollLayer()) | |
50 return; | |
51 | |
52 // If the scroll position has changed underneath us (i.e. a javascript | |
53 // scroll), then simulate a scroll that covers the delta. | |
54 float scroll_total_y = RootScrollLayerTotalScrollY(); | |
55 if (scroll_readjustment_enabled_ | |
56 && scroll_total_y != previous_root_scroll_offset_) { | |
57 ScrollBy(gfx::Vector2dF(0, scroll_total_y - previous_root_scroll_offset_)); | |
58 StartAnimationIfNecessary(); | |
59 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); | |
60 } | |
61 | |
62 float offset_top = is_overlay_mode_ ? 0 : content_top_offset_; | |
63 | |
64 // The two layers that need to be transformed are the clip layer and root | |
65 // scrollbar layers, which are the only two children of the root layer. | |
66 LayerImpl* root_layer = client_->activeTree()->RootLayer(); | |
67 for (size_t i = 0; i < root_layer->children().size(); ++i) { | |
68 LayerImpl* child_layer = root_layer->children()[i]; | |
69 gfx::Transform transform; | |
70 transform.Translate(0, offset_top); | |
71 child_layer->setImplTransform(transform); | |
wjmaclean
2013/01/11 14:45:24
Why are you using the implTransform for this? It's
enne (OOO)
2013/01/11 17:20:29
As I mentioned in https://codereview.chromium.org/
Ted C
2013/01/11 17:50:54
What is the distinction? I was under the impressi
Ted C
2013/01/11 17:50:54
It needs to move the content/clip layers around so
enne (OOO)
2013/01/11 17:57:01
Why does it need to move the content/clip layers a
Ted C
2013/01/11 18:10:23
As you scroll down a page, we move the urlbar/top
| |
72 } | |
73 | |
74 // TODO(tedchoc): Adjust fixed position layers as well. | |
75 } | |
76 | |
77 void TopControlsManager::ScrollBegin() { | |
78 ResetAnimations(); | |
79 scroll_readjustment_enabled_ = false; | |
80 } | |
81 | |
82 gfx::Vector2dF TopControlsManager::ScrollBy( | |
83 const gfx::Vector2dF pending_delta) { | |
84 ResetAnimations(); | |
85 return ScrollInternal(pending_delta); | |
86 } | |
87 | |
88 gfx::Vector2dF TopControlsManager::ScrollInternal( | |
89 const gfx::Vector2dF pending_delta) { | |
90 float scroll_total_y = RootScrollLayerTotalScrollY(); | |
91 float scroll_delta_y = pending_delta.y(); | |
92 | |
93 float previous_controls_offset = controls_top_offset_; | |
94 float previous_content_offset = content_top_offset_; | |
95 bool previous_was_overlay = is_overlay_mode_; | |
96 | |
97 controls_top_offset_ -= scroll_delta_y; | |
98 controls_top_offset_ = std::min( | |
99 std::max(controls_top_offset_, -top_controls_height_), 0.f); | |
100 | |
101 if (scroll_total_y > 0 || (scroll_total_y == 0 | |
102 && content_top_offset_ < scroll_delta_y)) { | |
103 is_overlay_mode_ = true; | |
104 content_top_offset_ = 0; | |
105 } else if (scroll_total_y <= 0 && (scroll_delta_y < 0 | |
106 || (scroll_delta_y > 0 && content_top_offset_ > 0))) { | |
107 is_overlay_mode_ = false; | |
108 content_top_offset_ -= scroll_delta_y; | |
109 } | |
110 content_top_offset_ = std::max( | |
111 std::min(content_top_offset_, | |
112 controls_top_offset_ + top_controls_height_), 0.f); | |
113 | |
114 gfx::Vector2dF applied_delta; | |
115 if (!previous_was_overlay) | |
116 applied_delta.set_y(previous_content_offset - content_top_offset_); | |
117 | |
118 if (is_overlay_mode_ != previous_was_overlay | |
119 || previous_controls_offset != controls_top_offset_ | |
120 || previous_content_offset != content_top_offset_) { | |
121 client_->setNeedsRedraw(); | |
122 client_->setNeedsUpdateDrawProperties(); | |
123 } | |
124 | |
125 return pending_delta - applied_delta; | |
126 } | |
127 | |
128 void TopControlsManager::ScrollEnd() { | |
129 StartAnimationIfNecessary(); | |
130 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); | |
131 scroll_readjustment_enabled_ = true; | |
132 } | |
133 | |
134 void TopControlsManager::Animate(base::TimeTicks monotonic_time) { | |
135 if (!top_controls_animation_ || !RootScrollLayer()) | |
136 return; | |
137 | |
138 double time = (monotonic_time - base::TimeTicks()).InMillisecondsF(); | |
139 float new_offset = top_controls_animation_->getValue(time); | |
140 gfx::Vector2dF scroll_vector(0.f, -(new_offset - controls_top_offset_)); | |
141 ScrollInternal(scroll_vector); | |
142 client_->setNeedsRedraw(); | |
143 | |
144 if ((is_showing_animation_ && new_offset >= 0) || | |
145 (!is_showing_animation_ && new_offset <= -top_controls_height_)) { | |
146 top_controls_animation_.reset(); | |
147 StartAnimationIfNecessary(); | |
148 } | |
149 } | |
150 | |
151 void TopControlsManager::ResetAnimations() { | |
152 if (top_controls_animation_) | |
153 top_controls_animation_.reset(); | |
154 } | |
155 | |
156 LayerImpl* TopControlsManager::RootScrollLayer() { | |
157 return client_->activeTree()->root_scroll_layer(); | |
158 } | |
159 | |
160 float TopControlsManager::RootScrollLayerTotalScrollY() { | |
161 LayerImpl* layer = RootScrollLayer(); | |
162 if (!layer) | |
163 return 0; | |
164 gfx::Vector2dF scroll_total = layer->scrollOffset() + layer->scrollDelta(); | |
165 return scroll_total.y(); | |
166 } | |
167 | |
168 void TopControlsManager::SetupAnimation(bool show_controls) { | |
169 top_controls_animation_ = KeyframedFloatAnimationCurve::create(); | |
170 double start_time = | |
171 (base::TimeTicks::Now() - base::TimeTicks()).InMillisecondsF(); | |
172 top_controls_animation_->addKeyframe( | |
173 FloatKeyframe::create(start_time, controls_top_offset_, | |
174 scoped_ptr<TimingFunction>())); | |
175 float max_ending_offset = (show_controls ? 1 : -1) * top_controls_height_; | |
176 top_controls_animation_->addKeyframe( | |
177 FloatKeyframe::create(start_time + kShowHideMaxDurationMs, | |
178 controls_top_offset_ + max_ending_offset, | |
179 EaseTimingFunction::create())); | |
180 is_showing_animation_ = show_controls; | |
181 } | |
182 | |
183 void TopControlsManager::StartAnimationIfNecessary() { | |
184 float scroll_total_y = RootScrollLayerTotalScrollY(); | |
185 | |
186 if (controls_top_offset_ != 0 | |
187 && controls_top_offset_ != -top_controls_height_) { | |
188 SetupAnimation( | |
189 controls_top_offset_ >= -(top_controls_height_ * kShowHideThreshold)); | |
190 client_->setNeedsRedraw(); | |
191 } | |
192 } | |
193 | |
194 } // namespace cc | |
OLD | NEW |