| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/browser_controls_offset_manager.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "cc/input/top_controls_manager_client.h" | 13 #include "cc/input/browser_controls_offset_manager_client.h" |
| 14 #include "cc/output/begin_frame_args.h" | 14 #include "cc/output/begin_frame_args.h" |
| 15 #include "cc/trees/layer_tree_impl.h" | 15 #include "cc/trees/layer_tree_impl.h" |
| 16 #include "ui/gfx/animation/tween.h" | 16 #include "ui/gfx/animation/tween.h" |
| 17 #include "ui/gfx/geometry/vector2d_f.h" | 17 #include "ui/gfx/geometry/vector2d_f.h" |
| 18 #include "ui/gfx/transform.h" | 18 #include "ui/gfx/transform.h" |
| 19 | 19 |
| 20 namespace cc { | 20 namespace cc { |
| 21 namespace { | 21 namespace { |
| 22 // These constants were chosen empirically for their visually pleasant behavior. | 22 // These constants were chosen empirically for their visually pleasant behavior. |
| 23 // Contact tedchoc@chromium.org for questions about changing these values. | 23 // Contact tedchoc@chromium.org for questions about changing these values. |
| 24 const int64_t kShowHideMaxDurationMs = 200; | 24 const int64_t kShowHideMaxDurationMs = 200; |
| 25 } | 25 } |
| 26 | 26 |
| 27 // static | 27 // static |
| 28 std::unique_ptr<TopControlsManager> TopControlsManager::Create( | 28 std::unique_ptr<BrowserControlsOffsetManager> |
| 29 TopControlsManagerClient* client, | 29 BrowserControlsOffsetManager::Create(BrowserControlsOffsetManagerClient* client, |
| 30 float top_controls_show_threshold, | 30 float controls_show_threshold, |
| 31 float top_controls_hide_threshold) { | 31 float controls_hide_threshold) { |
| 32 return base::WrapUnique(new TopControlsManager( | 32 return base::WrapUnique(new BrowserControlsOffsetManager( |
| 33 client, top_controls_show_threshold, top_controls_hide_threshold)); | 33 client, controls_show_threshold, controls_hide_threshold)); |
| 34 } | 34 } |
| 35 | 35 |
| 36 TopControlsManager::TopControlsManager(TopControlsManagerClient* client, | 36 BrowserControlsOffsetManager::BrowserControlsOffsetManager( |
| 37 float top_controls_show_threshold, | 37 BrowserControlsOffsetManagerClient* client, |
| 38 float top_controls_hide_threshold) | 38 float controls_show_threshold, |
| 39 float controls_hide_threshold) |
| 39 : client_(client), | 40 : client_(client), |
| 40 animation_start_value_(0.f), | 41 animation_start_value_(0.f), |
| 41 animation_stop_value_(0.f), | 42 animation_stop_value_(0.f), |
| 42 animation_direction_(NO_ANIMATION), | 43 animation_direction_(NO_ANIMATION), |
| 43 permitted_state_(BOTH), | 44 permitted_state_(BOTH), |
| 44 accumulated_scroll_delta_(0.f), | 45 accumulated_scroll_delta_(0.f), |
| 45 baseline_content_offset_(0.f), | 46 baseline_content_offset_(0.f), |
| 46 top_controls_show_threshold_(top_controls_hide_threshold), | 47 controls_show_threshold_(controls_hide_threshold), |
| 47 top_controls_hide_threshold_(top_controls_show_threshold), | 48 controls_hide_threshold_(controls_show_threshold), |
| 48 pinch_gesture_active_(false) { | 49 pinch_gesture_active_(false) { |
| 49 CHECK(client_); | 50 CHECK(client_); |
| 50 } | 51 } |
| 51 | 52 |
| 52 TopControlsManager::~TopControlsManager() { | 53 BrowserControlsOffsetManager::~BrowserControlsOffsetManager() {} |
| 53 } | |
| 54 | 54 |
| 55 float TopControlsManager::ControlsTopOffset() const { | 55 float BrowserControlsOffsetManager::ControlsTopOffset() const { |
| 56 return ContentTopOffset() - TopControlsHeight(); | 56 return ContentTopOffset() - TopControlsHeight(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 float TopControlsManager::ContentTopOffset() const { | 59 float BrowserControlsOffsetManager::ContentTopOffset() const { |
| 60 return TopControlsShownRatio() * TopControlsHeight(); | 60 return TopControlsShownRatio() * TopControlsHeight(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 float TopControlsManager::ContentOffsetInternal() const { | 63 float BrowserControlsOffsetManager::ContentOffsetInternal() const { |
| 64 if (!TopControlsHeight()) | 64 if (!TopControlsHeight()) |
| 65 return BottomControlsShownRatio() * BottomControlsHeight(); | 65 return BottomControlsShownRatio() * BottomControlsHeight(); |
| 66 return ContentTopOffset(); | 66 return ContentTopOffset(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 float TopControlsManager::TopControlsShownRatio() const { | 69 float BrowserControlsOffsetManager::TopControlsShownRatio() const { |
| 70 return client_->CurrentTopControlsShownRatio(); | 70 return client_->CurrentTopControlsShownRatio(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 float TopControlsManager::TopControlsHeight() const { | 73 float BrowserControlsOffsetManager::TopControlsHeight() const { |
| 74 return client_->TopControlsHeight(); | 74 return client_->TopControlsHeight(); |
| 75 } | 75 } |
| 76 | 76 |
| 77 float TopControlsManager::BottomControlsHeight() const { | 77 float BrowserControlsOffsetManager::BottomControlsHeight() const { |
| 78 return client_->BottomControlsHeight(); | 78 return client_->BottomControlsHeight(); |
| 79 } | 79 } |
| 80 | 80 |
| 81 float TopControlsManager::ContentBottomOffset() const { | 81 float BrowserControlsOffsetManager::ContentBottomOffset() const { |
| 82 return BottomControlsShownRatio() * BottomControlsHeight(); | 82 return BottomControlsShownRatio() * BottomControlsHeight(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 float TopControlsManager::BottomControlsShownRatio() const { | 85 float BrowserControlsOffsetManager::BottomControlsShownRatio() const { |
| 86 return TopControlsShownRatio(); | 86 return TopControlsShownRatio(); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void TopControlsManager::UpdateTopControlsState(TopControlsState constraints, | 89 void BrowserControlsOffsetManager::UpdateTopControlsState( |
| 90 TopControlsState current, | 90 BrowserControlsState constraints, |
| 91 bool animate) { | 91 BrowserControlsState current, |
| 92 bool animate) { |
| 92 DCHECK(!(constraints == SHOWN && current == HIDDEN)); | 93 DCHECK(!(constraints == SHOWN && current == HIDDEN)); |
| 93 DCHECK(!(constraints == HIDDEN && current == SHOWN)); | 94 DCHECK(!(constraints == HIDDEN && current == SHOWN)); |
| 94 | 95 |
| 95 permitted_state_ = constraints; | 96 permitted_state_ = constraints; |
| 96 | 97 |
| 97 // Don't do anything if it doesn't matter which state the controls are in. | 98 // Don't do anything if it doesn't matter which state the controls are in. |
| 98 if (constraints == BOTH && current == BOTH) | 99 if (constraints == BOTH && current == BOTH) |
| 99 return; | 100 return; |
| 100 | 101 |
| 101 // Don't do anything if there is no change in offset. | 102 // Don't do anything if there is no change in offset. |
| 102 float final_shown_ratio = 1.f; | 103 float final_shown_ratio = 1.f; |
| 103 if (constraints == HIDDEN || current == HIDDEN) | 104 if (constraints == HIDDEN || current == HIDDEN) |
| 104 final_shown_ratio = 0.f; | 105 final_shown_ratio = 0.f; |
| 105 if (final_shown_ratio == TopControlsShownRatio()) { | 106 if (final_shown_ratio == TopControlsShownRatio()) { |
| 106 ResetAnimations(); | 107 ResetAnimations(); |
| 107 return; | 108 return; |
| 108 } | 109 } |
| 109 | 110 |
| 110 if (animate) { | 111 if (animate) { |
| 111 SetupAnimation(final_shown_ratio ? SHOWING_CONTROLS : HIDING_CONTROLS); | 112 SetupAnimation(final_shown_ratio ? SHOWING_CONTROLS : HIDING_CONTROLS); |
| 112 } else { | 113 } else { |
| 113 ResetAnimations(); | 114 ResetAnimations(); |
| 114 client_->SetCurrentTopControlsShownRatio(final_shown_ratio); | 115 client_->SetCurrentTopControlsShownRatio(final_shown_ratio); |
| 115 } | 116 } |
| 116 } | 117 } |
| 117 | 118 |
| 118 void TopControlsManager::ScrollBegin() { | 119 void BrowserControlsOffsetManager::ScrollBegin() { |
| 119 if (pinch_gesture_active_) | 120 if (pinch_gesture_active_) |
| 120 return; | 121 return; |
| 121 | 122 |
| 122 ResetAnimations(); | 123 ResetAnimations(); |
| 123 ResetBaseline(); | 124 ResetBaseline(); |
| 124 } | 125 } |
| 125 | 126 |
| 126 gfx::Vector2dF TopControlsManager::ScrollBy( | 127 gfx::Vector2dF BrowserControlsOffsetManager::ScrollBy( |
| 127 const gfx::Vector2dF& pending_delta) { | 128 const gfx::Vector2dF& pending_delta) { |
| 128 // If one or both of the top/bottom controls are showing, the shown ratio | 129 // If one or both of the top/bottom controls are showing, the shown ratio |
| 129 // needs to be computed. | 130 // needs to be computed. |
| 130 float controls_height = | 131 float controls_height = |
| 131 TopControlsHeight() ? TopControlsHeight() : BottomControlsHeight(); | 132 TopControlsHeight() ? TopControlsHeight() : BottomControlsHeight(); |
| 132 | 133 |
| 133 if (!controls_height) | 134 if (!controls_height) |
| 134 return pending_delta; | 135 return pending_delta; |
| 135 | 136 |
| 136 if (pinch_gesture_active_) | 137 if (pinch_gesture_active_) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 151 // new baseline even if the gesture didn't end. | 152 // new baseline even if the gesture didn't end. |
| 152 if (TopControlsShownRatio() == 1.f) | 153 if (TopControlsShownRatio() == 1.f) |
| 153 ResetBaseline(); | 154 ResetBaseline(); |
| 154 | 155 |
| 155 ResetAnimations(); | 156 ResetAnimations(); |
| 156 | 157 |
| 157 gfx::Vector2dF applied_delta(0.f, old_offset - ContentOffsetInternal()); | 158 gfx::Vector2dF applied_delta(0.f, old_offset - ContentOffsetInternal()); |
| 158 return pending_delta - applied_delta; | 159 return pending_delta - applied_delta; |
| 159 } | 160 } |
| 160 | 161 |
| 161 void TopControlsManager::ScrollEnd() { | 162 void BrowserControlsOffsetManager::ScrollEnd() { |
| 162 if (pinch_gesture_active_) | 163 if (pinch_gesture_active_) |
| 163 return; | 164 return; |
| 164 | 165 |
| 165 StartAnimationIfNecessary(); | 166 StartAnimationIfNecessary(); |
| 166 } | 167 } |
| 167 | 168 |
| 168 void TopControlsManager::PinchBegin() { | 169 void BrowserControlsOffsetManager::PinchBegin() { |
| 169 DCHECK(!pinch_gesture_active_); | 170 DCHECK(!pinch_gesture_active_); |
| 170 pinch_gesture_active_ = true; | 171 pinch_gesture_active_ = true; |
| 171 StartAnimationIfNecessary(); | 172 StartAnimationIfNecessary(); |
| 172 } | 173 } |
| 173 | 174 |
| 174 void TopControlsManager::PinchEnd() { | 175 void BrowserControlsOffsetManager::PinchEnd() { |
| 175 DCHECK(pinch_gesture_active_); | 176 DCHECK(pinch_gesture_active_); |
| 176 // Pinch{Begin,End} will always occur within the scope of Scroll{Begin,End}, | 177 // Pinch{Begin,End} will always occur within the scope of Scroll{Begin,End}, |
| 177 // so return to a state expected by the remaining scroll sequence. | 178 // so return to a state expected by the remaining scroll sequence. |
| 178 pinch_gesture_active_ = false; | 179 pinch_gesture_active_ = false; |
| 179 ScrollBegin(); | 180 ScrollBegin(); |
| 180 } | 181 } |
| 181 | 182 |
| 182 void TopControlsManager::MainThreadHasStoppedFlinging() { | 183 void BrowserControlsOffsetManager::MainThreadHasStoppedFlinging() { |
| 183 StartAnimationIfNecessary(); | 184 StartAnimationIfNecessary(); |
| 184 } | 185 } |
| 185 | 186 |
| 186 gfx::Vector2dF TopControlsManager::Animate(base::TimeTicks monotonic_time) { | 187 gfx::Vector2dF BrowserControlsOffsetManager::Animate( |
| 188 base::TimeTicks monotonic_time) { |
| 187 if (!has_animation() || !client_->HaveRootScrollLayer()) | 189 if (!has_animation() || !client_->HaveRootScrollLayer()) |
| 188 return gfx::Vector2dF(); | 190 return gfx::Vector2dF(); |
| 189 | 191 |
| 190 float old_offset = ContentOffsetInternal(); | 192 float old_offset = ContentOffsetInternal(); |
| 191 float new_ratio = gfx::Tween::ClampedFloatValueBetween( | 193 float new_ratio = gfx::Tween::ClampedFloatValueBetween( |
| 192 monotonic_time, animation_start_time_, animation_start_value_, | 194 monotonic_time, animation_start_time_, animation_start_value_, |
| 193 animation_stop_time_, animation_stop_value_); | 195 animation_stop_time_, animation_stop_value_); |
| 194 client_->SetCurrentTopControlsShownRatio(new_ratio); | 196 client_->SetCurrentTopControlsShownRatio(new_ratio); |
| 195 | 197 |
| 196 if (IsAnimationComplete(new_ratio)) | 198 if (IsAnimationComplete(new_ratio)) |
| 197 ResetAnimations(); | 199 ResetAnimations(); |
| 198 | 200 |
| 199 gfx::Vector2dF scroll_delta(0.f, ContentOffsetInternal() - old_offset); | 201 gfx::Vector2dF scroll_delta(0.f, ContentOffsetInternal() - old_offset); |
| 200 return scroll_delta; | 202 return scroll_delta; |
| 201 } | 203 } |
| 202 | 204 |
| 203 void TopControlsManager::ResetAnimations() { | 205 void BrowserControlsOffsetManager::ResetAnimations() { |
| 204 animation_start_time_ = base::TimeTicks(); | 206 animation_start_time_ = base::TimeTicks(); |
| 205 animation_start_value_ = 0.f; | 207 animation_start_value_ = 0.f; |
| 206 animation_stop_time_ = base::TimeTicks(); | 208 animation_stop_time_ = base::TimeTicks(); |
| 207 animation_stop_value_ = 0.f; | 209 animation_stop_value_ = 0.f; |
| 208 | 210 |
| 209 animation_direction_ = NO_ANIMATION; | 211 animation_direction_ = NO_ANIMATION; |
| 210 } | 212 } |
| 211 | 213 |
| 212 void TopControlsManager::SetupAnimation(AnimationDirection direction) { | 214 void BrowserControlsOffsetManager::SetupAnimation( |
| 215 AnimationDirection direction) { |
| 213 DCHECK_NE(NO_ANIMATION, direction); | 216 DCHECK_NE(NO_ANIMATION, direction); |
| 214 DCHECK(direction != HIDING_CONTROLS || TopControlsShownRatio() > 0.f); | 217 DCHECK(direction != HIDING_CONTROLS || TopControlsShownRatio() > 0.f); |
| 215 DCHECK(direction != SHOWING_CONTROLS || TopControlsShownRatio() < 1.f); | 218 DCHECK(direction != SHOWING_CONTROLS || TopControlsShownRatio() < 1.f); |
| 216 | 219 |
| 217 if (has_animation() && animation_direction_ == direction) | 220 if (has_animation() && animation_direction_ == direction) |
| 218 return; | 221 return; |
| 219 | 222 |
| 220 if (!TopControlsHeight() && !BottomControlsHeight()) { | 223 if (!TopControlsHeight() && !BottomControlsHeight()) { |
| 221 client_->SetCurrentTopControlsShownRatio( | 224 client_->SetCurrentTopControlsShownRatio( |
| 222 direction == HIDING_CONTROLS ? 0.f : 1.f); | 225 direction == HIDING_CONTROLS ? 0.f : 1.f); |
| 223 return; | 226 return; |
| 224 } | 227 } |
| 225 | 228 |
| 226 animation_start_time_ = base::TimeTicks::Now(); | 229 animation_start_time_ = base::TimeTicks::Now(); |
| 227 animation_start_value_ = TopControlsShownRatio(); | 230 animation_start_value_ = TopControlsShownRatio(); |
| 228 | 231 |
| 229 const float max_ending_ratio = (direction == SHOWING_CONTROLS ? 1 : -1); | 232 const float max_ending_ratio = (direction == SHOWING_CONTROLS ? 1 : -1); |
| 230 animation_stop_time_ = | 233 animation_stop_time_ = |
| 231 animation_start_time_ + | 234 animation_start_time_ + |
| 232 base::TimeDelta::FromMilliseconds(kShowHideMaxDurationMs); | 235 base::TimeDelta::FromMilliseconds(kShowHideMaxDurationMs); |
| 233 animation_stop_value_ = animation_start_value_ + max_ending_ratio; | 236 animation_stop_value_ = animation_start_value_ + max_ending_ratio; |
| 234 | 237 |
| 235 animation_direction_ = direction; | 238 animation_direction_ = direction; |
| 236 client_->DidChangeTopControlsPosition(); | 239 client_->DidChangeTopControlsPosition(); |
| 237 } | 240 } |
| 238 | 241 |
| 239 void TopControlsManager::StartAnimationIfNecessary() { | 242 void BrowserControlsOffsetManager::StartAnimationIfNecessary() { |
| 240 if (TopControlsShownRatio() == 0.f || TopControlsShownRatio() == 1.f) | 243 if (TopControlsShownRatio() == 0.f || TopControlsShownRatio() == 1.f) |
| 241 return; | 244 return; |
| 242 | 245 |
| 243 if (TopControlsShownRatio() >= 1.f - top_controls_hide_threshold_) { | 246 if (TopControlsShownRatio() >= 1.f - controls_hide_threshold_) { |
| 244 // If we're showing so much that the hide threshold won't trigger, show. | 247 // If we're showing so much that the hide threshold won't trigger, show. |
| 245 SetupAnimation(SHOWING_CONTROLS); | 248 SetupAnimation(SHOWING_CONTROLS); |
| 246 } else if (TopControlsShownRatio() <= top_controls_show_threshold_) { | 249 } else if (TopControlsShownRatio() <= controls_show_threshold_) { |
| 247 // If we're showing so little that the show threshold won't trigger, hide. | 250 // If we're showing so little that the show threshold won't trigger, hide. |
| 248 SetupAnimation(HIDING_CONTROLS); | 251 SetupAnimation(HIDING_CONTROLS); |
| 249 } else { | 252 } else { |
| 250 // If we could be either showing or hiding, we determine which one to | 253 // If we could be either showing or hiding, we determine which one to |
| 251 // do based on whether or not the total scroll delta was moving up or | 254 // do based on whether or not the total scroll delta was moving up or |
| 252 // down. | 255 // down. |
| 253 SetupAnimation(accumulated_scroll_delta_ <= 0.f ? SHOWING_CONTROLS | 256 SetupAnimation(accumulated_scroll_delta_ <= 0.f ? SHOWING_CONTROLS |
| 254 : HIDING_CONTROLS); | 257 : HIDING_CONTROLS); |
| 255 } | 258 } |
| 256 } | 259 } |
| 257 | 260 |
| 258 bool TopControlsManager::IsAnimationComplete(float new_ratio) { | 261 bool BrowserControlsOffsetManager::IsAnimationComplete(float new_ratio) { |
| 259 return (animation_direction_ == SHOWING_CONTROLS && new_ratio >= 1.f) || | 262 return (animation_direction_ == SHOWING_CONTROLS && new_ratio >= 1.f) || |
| 260 (animation_direction_ == HIDING_CONTROLS && new_ratio <= 0.f); | 263 (animation_direction_ == HIDING_CONTROLS && new_ratio <= 0.f); |
| 261 } | 264 } |
| 262 | 265 |
| 263 void TopControlsManager::ResetBaseline() { | 266 void BrowserControlsOffsetManager::ResetBaseline() { |
| 264 accumulated_scroll_delta_ = 0.f; | 267 accumulated_scroll_delta_ = 0.f; |
| 265 baseline_content_offset_ = ContentOffsetInternal(); | 268 baseline_content_offset_ = ContentOffsetInternal(); |
| 266 } | 269 } |
| 267 | 270 |
| 268 } // namespace cc | 271 } // namespace cc |
| OLD | NEW |