1 // Copyright 2014 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/input/scroll_elasticity_helper.h"
6 
7 #include "cc/layers/layer_impl.h"
8 #include "cc/trees/layer_tree_host_impl.h"
9 #include "cc/trees/layer_tree_impl.h"
10 #include "cc/trees/scroll_node.h"
11 
12 namespace cc {
13 
14 class ScrollElasticityHelperImpl : public ScrollElasticityHelper {
15  public:
16   explicit ScrollElasticityHelperImpl(LayerTreeHostImpl* host_impl);
17   ~ScrollElasticityHelperImpl() override;
18 
19   bool IsUserScrollable() const override;
20   gfx::Vector2dF StretchAmount() const override;
21   void SetStretchAmount(const gfx::Vector2dF& stretch_amount) override;
22   gfx::ScrollOffset ScrollOffset() const override;
23   gfx::ScrollOffset MaxScrollOffset() const override;
24   void ScrollBy(const gfx::Vector2dF& delta) override;
25   void RequestOneBeginFrame() override;
26 
27  private:
28   LayerTreeHostImpl* host_impl_;
29 };
30 
ScrollElasticityHelperImpl(LayerTreeHostImpl * layer_tree)31 ScrollElasticityHelperImpl::ScrollElasticityHelperImpl(
32     LayerTreeHostImpl* layer_tree)
33     : host_impl_(layer_tree) {}
34 
35 ScrollElasticityHelperImpl::~ScrollElasticityHelperImpl() = default;
36 
IsUserScrollable() const37 bool ScrollElasticityHelperImpl::IsUserScrollable() const {
38   const auto* scroll_node = host_impl_->OuterViewportScrollNode();
39   if (!scroll_node)
40     return false;
41   return scroll_node->user_scrollable_horizontal ||
42          scroll_node->user_scrollable_vertical;
43 }
44 
StretchAmount() const45 gfx::Vector2dF ScrollElasticityHelperImpl::StretchAmount() const {
46   return host_impl_->active_tree()->elastic_overscroll()->Current(true);
47 }
48 
SetStretchAmount(const gfx::Vector2dF & stretch_amount)49 void ScrollElasticityHelperImpl::SetStretchAmount(
50     const gfx::Vector2dF& stretch_amount) {
51   if (stretch_amount == StretchAmount())
52     return;
53 
54   host_impl_->active_tree()->elastic_overscroll()->SetCurrent(stretch_amount);
55   host_impl_->active_tree()->set_needs_update_draw_properties();
56   host_impl_->SetNeedsCommit();
57   host_impl_->SetNeedsRedraw();
58   host_impl_->SetFullViewportDamage();
59 }
60 
ScrollOffset() const61 gfx::ScrollOffset ScrollElasticityHelperImpl::ScrollOffset() const {
62   return host_impl_->active_tree()->TotalScrollOffset();
63 }
64 
MaxScrollOffset() const65 gfx::ScrollOffset ScrollElasticityHelperImpl::MaxScrollOffset() const {
66   return host_impl_->active_tree()->TotalMaxScrollOffset();
67 }
68 
ScrollBy(const gfx::Vector2dF & delta)69 void ScrollElasticityHelperImpl::ScrollBy(const gfx::Vector2dF& delta) {
70   ScrollNode* root_scroll_node = host_impl_->OuterViewportScrollNode()
71                                      ? host_impl_->OuterViewportScrollNode()
72                                      : host_impl_->InnerViewportScrollNode();
73   if (root_scroll_node) {
74     LayerTreeImpl* tree_impl = host_impl_->active_tree();
75     tree_impl->property_trees()->scroll_tree.ScrollBy(*root_scroll_node, delta,
76                                                       tree_impl);
77   }
78 }
79 
RequestOneBeginFrame()80 void ScrollElasticityHelperImpl::RequestOneBeginFrame() {
81   host_impl_->SetNeedsOneBeginImplFrame();
82 }
83 
84 // static
CreateForLayerTreeHostImpl(LayerTreeHostImpl * host_impl)85 ScrollElasticityHelper* ScrollElasticityHelper::CreateForLayerTreeHostImpl(
86     LayerTreeHostImpl* host_impl) {
87   return new ScrollElasticityHelperImpl(host_impl);
88 }
89 
90 }  // namespace cc
91