OLD | NEW |
(Empty) | |
| 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 |
| 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 content_layer_id_(0), |
| 36 is_overlay_mode_(false), |
| 37 top_controls_height_(top_controls_height), |
| 38 controls_top_offset_(0), |
| 39 content_top_offset_(top_controls_height), |
| 40 previous_root_scroll_offset_(0.f), |
| 41 scroll_readjustment_enabled_(false), |
| 42 is_showing_animation_(false) { |
| 43 CHECK(client_); |
| 44 } |
| 45 |
| 46 TopControlsManager::~TopControlsManager() { |
| 47 } |
| 48 |
| 49 void TopControlsManager::UpdateDrawPositions() { |
| 50 if (!RootScrollLayer()) |
| 51 return; |
| 52 |
| 53 // If the scroll position has changed underneath us (i.e. a javascript |
| 54 // scroll), then simulate a scroll that covers the delta. |
| 55 float scroll_total_y = RootScrollLayerTotalScrollY(); |
| 56 if (scroll_readjustment_enabled_ |
| 57 && scroll_total_y != previous_root_scroll_offset_) { |
| 58 ScrollBy(gfx::Vector2dF(0, scroll_total_y - previous_root_scroll_offset_)); |
| 59 StartAnimationIfNecessary(); |
| 60 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); |
| 61 } |
| 62 |
| 63 float offset_top = is_overlay_mode_ ? 0 : content_top_offset_; |
| 64 |
| 65 LayerImpl* content_layer = |
| 66 client_->activeTree()->FindActiveTreeLayerById(content_layer_id_); |
| 67 DCHECK(content_layer); |
| 68 if (content_layer) { |
| 69 gfx::Transform transform; |
| 70 transform.Translate(0, offset_top); |
| 71 content_layer->setTransform(transform); |
| 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 |