Chromium Code Reviews| 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/bind.h" | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/message_loop.h" | |
| 13 #include "base/time.h" | |
| 14 #include "cc/layer_tree_impl.h" | |
| 15 #include "cc/top_controls_animation.h" | |
| 16 #include "ui/gfx/transform.h" | |
| 17 #include "ui/gfx/vector2d_f.h" | |
| 18 | |
| 19 namespace cc { | |
| 20 namespace { | |
| 21 static const float kShowHideThreshold = 0.75f; | |
| 22 static const int64 kAutoHideDelayMs = 1500; | |
|
jamesr
2012/12/19 22:56:29
I think the floating point constants will generate
Ted C
2012/12/20 00:54:50
thakis informed me that they do not, but he did po
| |
| 23 static const double kShowHideMaxDurationMs = 500; | |
| 24 } | |
| 25 | |
| 26 // static | |
| 27 scoped_ptr<TopControlsManager> TopControlsManager::Create( | |
| 28 TopControlsDelegate* delegate, float top_controls_height) { | |
| 29 return make_scoped_ptr(new TopControlsManager(delegate, top_controls_height)); | |
| 30 } | |
| 31 | |
| 32 TopControlsManager::TopControlsManager(TopControlsDelegate* delegate, | |
| 33 float top_controls_height) | |
| 34 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | |
| 35 delegate_(delegate), | |
| 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 top_controls_auto_hide_trigger_time_(-1), | |
| 41 previous_root_scroll_offset_(0.f), | |
| 42 scroll_readjustment_enabled_(false) { | |
| 43 CHECK(delegate_); | |
| 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 // The two layers that need to be transformed are the clip layer and root | |
| 66 // scrollbar layers, which are the only two children of the root layer. | |
| 67 LayerImpl* root_layer = delegate_->activeTree()->RootLayer(); | |
| 68 for (size_t i = 0; i < root_layer->children().size(); ++i) { | |
| 69 LayerImpl* child_layer = root_layer->children()[i]; | |
| 70 gfx::Transform transform; | |
| 71 transform.Translate(0, offset_top); | |
| 72 child_layer->setImplTransform(transform); | |
| 73 } | |
| 74 | |
| 75 // TODO(tedchoc): Adjust fixed position layers as well. | |
| 76 } | |
| 77 | |
| 78 void TopControlsManager::ScrollBegin() { | |
| 79 ResetAnimations(); | |
| 80 scroll_readjustment_enabled_ = false; | |
| 81 } | |
| 82 | |
| 83 gfx::Vector2dF TopControlsManager::ScrollBy( | |
| 84 const gfx::Vector2dF pending_delta) { | |
| 85 ResetAnimations(); | |
| 86 return ScrollInternal(pending_delta); | |
| 87 } | |
| 88 | |
| 89 gfx::Vector2dF TopControlsManager::ScrollInternal( | |
| 90 const gfx::Vector2dF pending_delta) { | |
| 91 float scroll_total_y = RootScrollLayerTotalScrollY(); | |
| 92 float scroll_delta_y = pending_delta.y(); | |
| 93 | |
| 94 float previous_controls_offset = controls_top_offset_; | |
| 95 float previous_content_offset = content_top_offset_; | |
| 96 bool previous_was_overlay = is_overlay_mode_; | |
| 97 | |
| 98 controls_top_offset_ -= scroll_delta_y; | |
| 99 controls_top_offset_ = std::min( | |
| 100 std::max(controls_top_offset_, -top_controls_height_), 0.f); | |
| 101 | |
| 102 if (scroll_total_y > 0 || (scroll_total_y == 0 | |
| 103 && content_top_offset_ < scroll_delta_y)) { | |
| 104 is_overlay_mode_ = true; | |
| 105 content_top_offset_ = 0; | |
| 106 } else if (scroll_total_y <= 0 && (scroll_delta_y < 0 | |
| 107 || (scroll_delta_y > 0 && content_top_offset_ > 0))) { | |
| 108 is_overlay_mode_ = false; | |
| 109 content_top_offset_ -= scroll_delta_y; | |
| 110 } | |
| 111 content_top_offset_ = std::max( | |
| 112 std::min(content_top_offset_, | |
| 113 controls_top_offset_ + top_controls_height_), 0.f); | |
| 114 | |
| 115 gfx::Vector2dF applied_delta; | |
| 116 if (!previous_was_overlay) | |
| 117 applied_delta.set_y(previous_content_offset - content_top_offset_); | |
| 118 | |
| 119 if (is_overlay_mode_ != previous_was_overlay | |
| 120 || previous_controls_offset != controls_top_offset_ | |
| 121 || previous_content_offset != content_top_offset_) { | |
| 122 delegate_->setNeedsRedraw(); | |
| 123 delegate_->setNeedsUpdateDrawProperties(); | |
| 124 } | |
| 125 | |
| 126 return pending_delta - applied_delta; | |
| 127 } | |
| 128 | |
| 129 void TopControlsManager::ScrollEnd() { | |
| 130 StartAnimationIfNecessary(); | |
| 131 previous_root_scroll_offset_ = RootScrollLayerTotalScrollY(); | |
| 132 scroll_readjustment_enabled_ = true; | |
| 133 } | |
| 134 | |
| 135 void TopControlsManager::Animate(base::TimeTicks monotonic_time) { | |
| 136 if (!top_controls_animation_ || !RootScrollLayer()) | |
| 137 return; | |
| 138 | |
| 139 double time = (monotonic_time - base::TimeTicks()).InSecondsF(); | |
| 140 float new_offset = | |
| 141 top_controls_animation_->ScrollOffsetAtTime(monotonic_time); | |
| 142 gfx::Vector2dF scroll_vector(0.f, -(new_offset - controls_top_offset_)); | |
| 143 ScrollInternal(scroll_vector); | |
| 144 delegate_->setNeedsRedraw(); | |
| 145 | |
| 146 if (top_controls_animation_->IsAnimationCompleteAtTime(monotonic_time)) { | |
| 147 top_controls_animation_.reset(); | |
| 148 StartAnimationIfNecessary(); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 void TopControlsManager::ResetAnimations() { | |
| 153 if (top_controls_animation_) | |
| 154 top_controls_animation_.reset(); | |
| 155 top_controls_auto_hide_trigger_time_ = -1; | |
| 156 } | |
| 157 | |
| 158 LayerImpl* TopControlsManager::RootScrollLayer() { | |
| 159 return delegate_->activeTree()->root_scroll_layer(); | |
| 160 } | |
| 161 | |
| 162 float TopControlsManager::RootScrollLayerTotalScrollY() { | |
| 163 LayerImpl* layer = RootScrollLayer(); | |
| 164 if (!layer) | |
| 165 return 0; | |
| 166 gfx::Vector2dF scroll_total = layer->scrollOffset() + layer->scrollDelta(); | |
| 167 return scroll_total.y(); | |
| 168 } | |
| 169 | |
| 170 void TopControlsManager::StartAnimationIfNecessary() { | |
| 171 if (!is_overlay_mode_) | |
| 172 return; | |
| 173 | |
| 174 float scroll_total_y = RootScrollLayerTotalScrollY(); | |
| 175 | |
| 176 if (controls_top_offset_ != 0 | |
| 177 && controls_top_offset_ != -top_controls_height_) { | |
| 178 top_controls_animation_ = TopControlsAnimation::Create( | |
| 179 controls_top_offset_, | |
| 180 top_controls_height_, | |
| 181 base::TimeTicks::Now(), | |
| 182 base::TimeDelta::FromMilliseconds(kShowHideMaxDurationMs)); | |
| 183 top_controls_animation_->SetDirection( | |
| 184 controls_top_offset_ >= -(top_controls_height_ * kShowHideThreshold)); | |
| 185 delegate_->setNeedsRedraw(); | |
| 186 } else if (controls_top_offset_ == 0 && scroll_total_y != 0) { | |
| 187 MessageLoop* loop = MessageLoop::current(); | |
| 188 DCHECK(loop); | |
| 189 if (loop) { | |
| 190 int64 trigger_time = top_controls_auto_hide_trigger_time_ = | |
| 191 (base::TimeTicks::Now() - base::TimeTicks()).InMilliseconds(); | |
| 192 loop->PostDelayedTask( | |
| 193 FROM_HERE, | |
| 194 base::Bind(&TopControlsManager::StartAutoHideAnimation, | |
| 195 weak_ptr_factory_.GetWeakPtr(), trigger_time), | |
|
jamesr
2012/12/19 22:56:29
a WeakPtrFactory seems like overkill for this - ch
Ted C
2012/12/20 00:42:39
Awesome...didn't know about this. Done
| |
| 196 base::TimeDelta::FromMilliseconds(kAutoHideDelayMs)); | |
| 197 } | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 void TopControlsManager::StartAutoHideAnimation(int64 triggered_time) { | |
| 202 if (triggered_time != top_controls_auto_hide_trigger_time_) | |
| 203 return; | |
| 204 | |
| 205 top_controls_animation_ = TopControlsAnimation::Create( | |
| 206 controls_top_offset_, | |
| 207 top_controls_height_, | |
| 208 base::TimeTicks::Now(), | |
| 209 base::TimeDelta::FromMilliseconds(kShowHideMaxDurationMs)); | |
| 210 top_controls_animation_->SetDirection(false); | |
| 211 delegate_->setNeedsRedraw(); | |
| 212 } | |
| 213 | |
| 214 } // namespace cc | |
| OLD | NEW |