OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/top_controls_manager.h" | 5 #include "cc/top_controls_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 scoped_ptr<TopControlsManager> TopControlsManager::Create( | 27 scoped_ptr<TopControlsManager> TopControlsManager::Create( |
28 TopControlsManagerClient* client, float top_controls_height) { | 28 TopControlsManagerClient* client, float top_controls_height) { |
29 return make_scoped_ptr(new TopControlsManager(client, top_controls_height)); | 29 return make_scoped_ptr(new TopControlsManager(client, top_controls_height)); |
30 } | 30 } |
31 | 31 |
32 TopControlsManager::TopControlsManager(TopControlsManagerClient* client, | 32 TopControlsManager::TopControlsManager(TopControlsManagerClient* client, |
33 float top_controls_height) | 33 float top_controls_height) |
34 : client_(client), | 34 : client_(client), |
35 animation_direction_(NO_ANIMATION), | 35 animation_direction_(NO_ANIMATION), |
36 is_overlay_mode_(false), | 36 is_overlay_mode_(false), |
37 scroll_readjustment_enabled_(false), | 37 in_scroll_gesture_(false), |
38 top_controls_height_(top_controls_height), | 38 top_controls_height_(top_controls_height), |
39 controls_top_offset_(0), | 39 controls_top_offset_(0), |
40 content_top_offset_(top_controls_height), | 40 content_top_offset_(top_controls_height), |
41 previous_root_scroll_offset_(0.f) { | 41 previous_root_scroll_offset_(0.f), |
| 42 scroll_start_offset_(0.f) { |
42 CHECK(client_); | 43 CHECK(client_); |
43 } | 44 } |
44 | 45 |
45 TopControlsManager::~TopControlsManager() { | 46 TopControlsManager::~TopControlsManager() { |
46 } | 47 } |
47 | 48 |
48 void TopControlsManager::UpdateDrawPositions() { | 49 void TopControlsManager::UpdateDrawPositions() { |
49 if (!client_->haveRootScrollLayer()) | 50 if (!client_->haveRootScrollLayer()) |
50 return; | 51 return; |
51 | 52 |
52 // If the scroll position has changed underneath us (i.e. a javascript | 53 // If the scroll position has changed underneath us (i.e. a javascript |
53 // scroll), then simulate a scroll that covers the delta. | 54 // scroll), then simulate a scroll that covers the delta. |
54 float scroll_total_y = RootScrollLayerTotalScrollY(); | 55 float scroll_total_y = RootScrollLayerTotalScrollY(); |
55 if (scroll_readjustment_enabled_ | 56 if (!in_scroll_gesture_ |
56 && scroll_total_y != previous_root_scroll_offset_) { | 57 && scroll_total_y != previous_root_scroll_offset_) { |
57 ScrollBy(gfx::Vector2dF(0, scroll_total_y - previous_root_scroll_offset_)); | 58 ScrollBy(gfx::Vector2dF(0, scroll_total_y - previous_root_scroll_offset_)); |
58 StartAnimationIfNecessary(); | 59 StartAnimationIfNecessary(); |
59 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); | 60 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); |
60 } | 61 } |
61 } | 62 } |
62 | 63 |
63 void TopControlsManager::ScrollBegin() { | 64 void TopControlsManager::ScrollBegin() { |
64 ResetAnimations(); | 65 ResetAnimations(); |
65 scroll_readjustment_enabled_ = false; | 66 in_scroll_gesture_ = true; |
| 67 scroll_start_offset_ = RootScrollLayerTotalScrollY() + controls_top_offset_; |
66 } | 68 } |
67 | 69 |
68 gfx::Vector2dF TopControlsManager::ScrollBy( | 70 gfx::Vector2dF TopControlsManager::ScrollBy( |
69 const gfx::Vector2dF pending_delta) { | 71 const gfx::Vector2dF pending_delta) { |
| 72 if (pending_delta.y() == 0) |
| 73 return pending_delta; |
| 74 |
| 75 float scroll_total_y = RootScrollLayerTotalScrollY(); |
| 76 if (in_scroll_gesture_ && |
| 77 ((pending_delta.y() > 0 && scroll_total_y < scroll_start_offset_) || |
| 78 (pending_delta.y() < 0 && |
| 79 scroll_total_y > scroll_start_offset_ + top_controls_height_))) { |
| 80 return pending_delta; |
| 81 } |
| 82 |
70 ResetAnimations(); | 83 ResetAnimations(); |
71 return ScrollInternal(pending_delta); | 84 return ScrollInternal(pending_delta); |
72 } | 85 } |
73 | 86 |
74 gfx::Vector2dF TopControlsManager::ScrollInternal( | 87 gfx::Vector2dF TopControlsManager::ScrollInternal( |
75 const gfx::Vector2dF pending_delta) { | 88 const gfx::Vector2dF pending_delta) { |
76 float scroll_total_y = RootScrollLayerTotalScrollY(); | 89 float scroll_total_y = RootScrollLayerTotalScrollY(); |
77 float scroll_delta_y = pending_delta.y(); | 90 float scroll_delta_y = pending_delta.y(); |
78 | 91 |
79 float previous_controls_offset = controls_top_offset_; | 92 float previous_controls_offset = controls_top_offset_; |
80 float previous_content_offset = content_top_offset_; | 93 float previous_content_offset = content_top_offset_; |
81 bool previous_was_overlay = is_overlay_mode_; | 94 bool previous_was_overlay = is_overlay_mode_; |
82 | 95 |
83 controls_top_offset_ -= scroll_delta_y; | 96 controls_top_offset_ -= scroll_delta_y; |
84 controls_top_offset_ = std::min( | 97 controls_top_offset_ = std::min( |
85 std::max(controls_top_offset_, -top_controls_height_), 0.f); | 98 std::max(controls_top_offset_, -top_controls_height_), 0.f); |
86 | 99 |
87 if (scroll_total_y > 0 || (scroll_total_y == 0 | 100 if (scroll_total_y > 0 || (scroll_total_y == 0 |
88 && content_top_offset_ < scroll_delta_y)) { | 101 && content_top_offset_ < scroll_delta_y)) { |
89 is_overlay_mode_ = true; | 102 is_overlay_mode_ = true; |
90 content_top_offset_ = 0; | 103 |
| 104 // The first case is where the page applies a scroll (javascript) and is |
| 105 // being re-adjusted in a call to UpdateDrawPositions. Instead of slamming |
| 106 // the controls to the top, we adjust by the scroll delta until we reach |
| 107 // zero as we expect. |
| 108 if (scroll_total_y > 0 && content_top_offset_ != 0) |
| 109 content_top_offset_ -= scroll_delta_y; |
| 110 else |
| 111 content_top_offset_ = 0; |
91 } else if (scroll_total_y <= 0 && (scroll_delta_y < 0 | 112 } else if (scroll_total_y <= 0 && (scroll_delta_y < 0 |
92 || (scroll_delta_y > 0 && content_top_offset_ > 0))) { | 113 || (scroll_delta_y > 0 && content_top_offset_ > 0))) { |
93 is_overlay_mode_ = false; | 114 is_overlay_mode_ = false; |
94 content_top_offset_ -= scroll_delta_y; | 115 content_top_offset_ -= scroll_delta_y; |
95 } | 116 } |
96 content_top_offset_ = std::max( | 117 content_top_offset_ = std::max( |
97 std::min(content_top_offset_, | 118 std::min(content_top_offset_, |
98 controls_top_offset_ + top_controls_height_), 0.f); | 119 controls_top_offset_ + top_controls_height_), 0.f); |
99 | 120 |
100 gfx::Vector2dF applied_delta; | 121 gfx::Vector2dF applied_delta; |
101 if (!previous_was_overlay) | 122 if (!previous_was_overlay) |
102 applied_delta.set_y(previous_content_offset - content_top_offset_); | 123 applied_delta.set_y(previous_content_offset - content_top_offset_); |
103 | 124 |
104 if (is_overlay_mode_ != previous_was_overlay | 125 if (is_overlay_mode_ != previous_was_overlay |
105 || previous_controls_offset != controls_top_offset_ | 126 || previous_controls_offset != controls_top_offset_ |
106 || previous_content_offset != content_top_offset_) { | 127 || previous_content_offset != content_top_offset_) { |
107 client_->setNeedsRedraw(); | 128 client_->setNeedsRedraw(); |
108 client_->setNeedsUpdateDrawProperties(); | 129 client_->setNeedsUpdateDrawProperties(); |
109 } | 130 } |
110 | 131 |
111 return pending_delta - applied_delta; | 132 return pending_delta - applied_delta; |
112 } | 133 } |
113 | 134 |
114 void TopControlsManager::ScrollEnd() { | 135 void TopControlsManager::ScrollEnd() { |
115 StartAnimationIfNecessary(); | 136 StartAnimationIfNecessary(); |
116 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); | 137 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); |
117 scroll_readjustment_enabled_ = true; | 138 in_scroll_gesture_ = false; |
118 } | 139 } |
119 | 140 |
120 void TopControlsManager::Animate(base::TimeTicks monotonic_time) { | 141 void TopControlsManager::Animate(base::TimeTicks monotonic_time) { |
121 if (!top_controls_animation_ || !client_->haveRootScrollLayer()) | 142 if (!top_controls_animation_ || !client_->haveRootScrollLayer()) |
122 return; | 143 return; |
123 | 144 |
124 double time = (monotonic_time - base::TimeTicks()).InMillisecondsF(); | 145 double time = (monotonic_time - base::TimeTicks()).InMillisecondsF(); |
125 float new_offset = top_controls_animation_->getValue(time); | 146 float new_offset = top_controls_animation_->getValue(time); |
126 gfx::Vector2dF scroll_vector(0.f, -(new_offset - controls_top_offset_)); | 147 gfx::Vector2dF scroll_vector(0.f, -(new_offset - controls_top_offset_)); |
127 ScrollInternal(scroll_vector); | 148 ScrollInternal(scroll_vector); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 203 |
183 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) || | 204 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) || |
184 (animation_direction_ == HIDING_CONTROLS | 205 (animation_direction_ == HIDING_CONTROLS |
185 && new_offset <= -top_controls_height_)) { | 206 && new_offset <= -top_controls_height_)) { |
186 return true; | 207 return true; |
187 } | 208 } |
188 return false; | 209 return false; |
189 } | 210 } |
190 | 211 |
191 } // namespace cc | 212 } // namespace cc |
OLD | NEW |