scroll_elasticity_helper.cc 3.5 KB

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