| 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/input/top_controls_manager.h" | 5 #include "cc/input/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 22 matching lines...) Expand all Loading... |
| 33 top_controls_show_threshold, | 33 top_controls_show_threshold, |
| 34 top_controls_hide_threshold)); | 34 top_controls_hide_threshold)); |
| 35 } | 35 } |
| 36 | 36 |
| 37 TopControlsManager::TopControlsManager(TopControlsManagerClient* client, | 37 TopControlsManager::TopControlsManager(TopControlsManagerClient* client, |
| 38 float top_controls_height, | 38 float top_controls_height, |
| 39 float top_controls_show_threshold, | 39 float top_controls_show_threshold, |
| 40 float top_controls_hide_threshold) | 40 float top_controls_hide_threshold) |
| 41 : client_(client), | 41 : client_(client), |
| 42 animation_direction_(NO_ANIMATION), | 42 animation_direction_(NO_ANIMATION), |
| 43 visibility_restriction_(NONE), | 43 permitted_state_(BOTH), |
| 44 controls_top_offset_(0.f), | 44 controls_top_offset_(0.f), |
| 45 top_controls_height_(top_controls_height), | 45 top_controls_height_(top_controls_height), |
| 46 current_scroll_delta_(0.f), | 46 current_scroll_delta_(0.f), |
| 47 controls_scroll_begin_offset_(0.f), | 47 controls_scroll_begin_offset_(0.f), |
| 48 top_controls_show_height_( | 48 top_controls_show_height_( |
| 49 top_controls_height * top_controls_hide_threshold), | 49 top_controls_height * top_controls_hide_threshold), |
| 50 top_controls_hide_height_( | 50 top_controls_hide_height_( |
| 51 top_controls_height * (1.f - top_controls_show_threshold)) { | 51 top_controls_height * (1.f - top_controls_show_threshold)) { |
| 52 CHECK(client_); | 52 CHECK(client_); |
| 53 } | 53 } |
| 54 | 54 |
| 55 TopControlsManager::~TopControlsManager() { | 55 TopControlsManager::~TopControlsManager() { |
| 56 } | 56 } |
| 57 | 57 |
| 58 void TopControlsManager::UpdateTopControlsState(bool enable_hiding, | 58 void TopControlsManager::UpdateTopControlsState(TopControlsState constraints, |
| 59 bool enable_showing, | 59 TopControlsState current, |
| 60 bool animate) { | 60 bool animate) { |
| 61 DCHECK(!(constraints == SHOWN && current == HIDDEN)); |
| 62 DCHECK(!(constraints == HIDDEN && current == SHOWN)); |
| 63 |
| 64 permitted_state_ = constraints; |
| 65 |
| 66 // Don't do anything if it doesn't matter which state the controls are in. |
| 67 if (constraints == BOTH && current == BOTH) |
| 68 return; |
| 69 |
| 70 // Don't do anything if there is no change in offset. |
| 61 float final_controls_position = 0.f; | 71 float final_controls_position = 0.f; |
| 62 | 72 if (constraints == HIDDEN || current == HIDDEN) { |
| 63 if (enable_hiding && enable_showing) { | |
| 64 visibility_restriction_ = NONE; | |
| 65 } else if (enable_showing || !enable_hiding) { | |
| 66 visibility_restriction_ = ALWAYS_SHOWN; | |
| 67 } else { | |
| 68 visibility_restriction_ = ALWAYS_HIDDEN; | |
| 69 final_controls_position = -top_controls_height_; | 73 final_controls_position = -top_controls_height_; |
| 70 } | 74 } |
| 75 if (final_controls_position == controls_top_offset_) { |
| 76 return; |
| 77 } |
| 71 | 78 |
| 72 if (visibility_restriction_ != NONE && | 79 AnimationDirection animation_direction = SHOWING_CONTROLS; |
| 73 final_controls_position != controls_top_offset_) { | 80 if (constraints == HIDDEN || current == HIDDEN) |
| 74 ResetAnimations(); | 81 animation_direction = HIDING_CONTROLS; |
| 75 if (animate) { | 82 ResetAnimations(); |
| 76 SetupAnimation(visibility_restriction_ == ALWAYS_SHOWN ? | 83 if (animate) { |
| 77 SHOWING_CONTROLS : HIDING_CONTROLS); | 84 SetupAnimation(animation_direction); |
| 78 } else { | 85 } else { |
| 79 controls_top_offset_ = final_controls_position; | 86 controls_top_offset_ = final_controls_position; |
| 80 } | |
| 81 client_->DidChangeTopControlsPosition(); | |
| 82 } | 87 } |
| 88 client_->DidChangeTopControlsPosition(); |
| 83 } | 89 } |
| 84 | 90 |
| 85 void TopControlsManager::ScrollBegin() { | 91 void TopControlsManager::ScrollBegin() { |
| 86 ResetAnimations(); | 92 ResetAnimations(); |
| 87 current_scroll_delta_ = 0.f; | 93 current_scroll_delta_ = 0.f; |
| 88 controls_scroll_begin_offset_ = controls_top_offset_; | 94 controls_scroll_begin_offset_ = controls_top_offset_; |
| 89 } | 95 } |
| 90 | 96 |
| 91 gfx::Vector2dF TopControlsManager::ScrollBy( | 97 gfx::Vector2dF TopControlsManager::ScrollBy( |
| 92 const gfx::Vector2dF pending_delta) { | 98 const gfx::Vector2dF pending_delta) { |
| 93 if (visibility_restriction_ == ALWAYS_SHOWN && pending_delta.y() > 0) | 99 if (permitted_state_ == SHOWN && pending_delta.y() > 0) |
| 94 return pending_delta; | 100 return pending_delta; |
| 95 else if (visibility_restriction_ == ALWAYS_HIDDEN && pending_delta.y() < 0) | 101 else if (permitted_state_ == HIDDEN && pending_delta.y() < 0) |
| 96 return pending_delta; | 102 return pending_delta; |
| 97 | 103 |
| 98 current_scroll_delta_ += pending_delta.y(); | 104 current_scroll_delta_ += pending_delta.y(); |
| 99 | 105 |
| 100 float old_offset = controls_top_offset_; | 106 float old_offset = controls_top_offset_; |
| 101 SetControlsTopOffset(controls_scroll_begin_offset_ - current_scroll_delta_); | 107 SetControlsTopOffset(controls_scroll_begin_offset_ - current_scroll_delta_); |
| 102 | 108 |
| 103 // If the controls are fully visible, treat the current position as the | 109 // If the controls are fully visible, treat the current position as the |
| 104 // new baseline even if the gesture didn't end. | 110 // new baseline even if the gesture didn't end. |
| 105 if (controls_top_offset_ == 0.f) { | 111 if (controls_top_offset_ == 0.f) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 } | 152 } |
| 147 | 153 |
| 148 void TopControlsManager::ResetAnimations() { | 154 void TopControlsManager::ResetAnimations() { |
| 149 if (top_controls_animation_) | 155 if (top_controls_animation_) |
| 150 top_controls_animation_.reset(); | 156 top_controls_animation_.reset(); |
| 151 | 157 |
| 152 animation_direction_ = NO_ANIMATION; | 158 animation_direction_ = NO_ANIMATION; |
| 153 } | 159 } |
| 154 | 160 |
| 155 void TopControlsManager::SetupAnimation(AnimationDirection direction) { | 161 void TopControlsManager::SetupAnimation(AnimationDirection direction) { |
| 162 DCHECK(direction != NO_ANIMATION); |
| 163 |
| 164 if (direction == SHOWING_CONTROLS && controls_top_offset_ == 0) |
| 165 return; |
| 166 |
| 167 if (direction == HIDING_CONTROLS && |
| 168 controls_top_offset_ == -top_controls_height_) { |
| 169 return; |
| 170 } |
| 171 |
| 172 if (top_controls_animation_ && animation_direction_ == direction) |
| 173 return; |
| 174 |
| 156 top_controls_animation_ = KeyframedFloatAnimationCurve::Create(); | 175 top_controls_animation_ = KeyframedFloatAnimationCurve::Create(); |
| 157 double start_time = | 176 double start_time = |
| 158 (base::TimeTicks::Now() - base::TimeTicks()).InMillisecondsF(); | 177 (base::TimeTicks::Now() - base::TimeTicks()).InMillisecondsF(); |
| 159 top_controls_animation_->AddKeyframe( | 178 top_controls_animation_->AddKeyframe( |
| 160 FloatKeyframe::Create(start_time, controls_top_offset_, | 179 FloatKeyframe::Create(start_time, controls_top_offset_, |
| 161 scoped_ptr<TimingFunction>())); | 180 scoped_ptr<TimingFunction>())); |
| 162 float max_ending_offset = | 181 float max_ending_offset = |
| 163 (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_; | 182 (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_; |
| 164 top_controls_animation_->AddKeyframe( | 183 top_controls_animation_->AddKeyframe( |
| 165 FloatKeyframe::Create(start_time + kShowHideMaxDurationMs, | 184 FloatKeyframe::Create(start_time + kShowHideMaxDurationMs, |
| 166 controls_top_offset_ + max_ending_offset, | 185 controls_top_offset_ + max_ending_offset, |
| 167 EaseTimingFunction::Create())); | 186 EaseTimingFunction::Create())); |
| 168 animation_direction_ = direction; | 187 animation_direction_ = direction; |
| 188 client_->DidChangeTopControlsPosition(); |
| 169 } | 189 } |
| 170 | 190 |
| 171 void TopControlsManager::StartAnimationIfNecessary() { | 191 void TopControlsManager::StartAnimationIfNecessary() { |
| 172 if (controls_top_offset_ != 0 | 192 if (controls_top_offset_ != 0 |
| 173 && controls_top_offset_ != -top_controls_height_) { | 193 && controls_top_offset_ != -top_controls_height_) { |
| 174 AnimationDirection show_controls = NO_ANIMATION; | 194 AnimationDirection show_controls = NO_ANIMATION; |
| 175 | 195 |
| 176 if (controls_top_offset_ >= -top_controls_show_height_) { | 196 if (controls_top_offset_ >= -top_controls_show_height_) { |
| 177 // If we're showing so much that the hide threshold won't trigger, show. | 197 // If we're showing so much that the hide threshold won't trigger, show. |
| 178 show_controls = SHOWING_CONTROLS; | 198 show_controls = SHOWING_CONTROLS; |
| 179 } else if (controls_top_offset_ <= -top_controls_hide_height_) { | 199 } else if (controls_top_offset_ <= -top_controls_hide_height_) { |
| 180 // If we're showing so little that the show threshold won't trigger, hide. | 200 // If we're showing so little that the show threshold won't trigger, hide. |
| 181 show_controls = HIDING_CONTROLS; | 201 show_controls = HIDING_CONTROLS; |
| 182 } else { | 202 } else { |
| 183 // If we could be either showing or hiding, we determine which one to | 203 // If we could be either showing or hiding, we determine which one to |
| 184 // do based on whether or not the total scroll delta was moving up or | 204 // do based on whether or not the total scroll delta was moving up or |
| 185 // down. | 205 // down. |
| 186 show_controls = current_scroll_delta_ <= 0.f ? | 206 show_controls = current_scroll_delta_ <= 0.f ? |
| 187 SHOWING_CONTROLS : HIDING_CONTROLS; | 207 SHOWING_CONTROLS : HIDING_CONTROLS; |
| 188 } | 208 } |
| 189 | 209 |
| 190 if (show_controls != NO_ANIMATION && | 210 if (show_controls != NO_ANIMATION) |
| 191 (!top_controls_animation_ || animation_direction_ != show_controls)) { | |
| 192 SetupAnimation(show_controls); | 211 SetupAnimation(show_controls); |
| 193 client_->DidChangeTopControlsPosition(); | |
| 194 } | |
| 195 } | 212 } |
| 196 } | 213 } |
| 197 | 214 |
| 198 bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time) { | 215 bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time) { |
| 199 if (!top_controls_animation_) | 216 if (!top_controls_animation_) |
| 200 return true; | 217 return true; |
| 201 | 218 |
| 202 double time_ms = (time - base::TimeTicks()).InMillisecondsF(); | 219 double time_ms = (time - base::TimeTicks()).InMillisecondsF(); |
| 203 float new_offset = top_controls_animation_->GetValue(time_ms); | 220 float new_offset = top_controls_animation_->GetValue(time_ms); |
| 204 | 221 |
| 205 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) || | 222 if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) || |
| 206 (animation_direction_ == HIDING_CONTROLS | 223 (animation_direction_ == HIDING_CONTROLS |
| 207 && new_offset <= -top_controls_height_)) { | 224 && new_offset <= -top_controls_height_)) { |
| 208 return true; | 225 return true; |
| 209 } | 226 } |
| 210 return false; | 227 return false; |
| 211 } | 228 } |
| 212 | 229 |
| 213 } // namespace cc | 230 } // namespace cc |
| OLD | NEW |