overscroll_refresh.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifndef UI_ANDROID_OVERSCROLL_REFRESH_H_
  5. #define UI_ANDROID_OVERSCROLL_REFRESH_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "ui/android/ui_android_export.h"
  8. #include "ui/gfx/geometry/size_f.h"
  9. #include "ui/gfx/geometry/vector2d_f.h"
  10. // A Java counterpart will be generated for this enum.
  11. // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.ui
  12. enum OverscrollAction { NONE = 0, PULL_TO_REFRESH = 1, HISTORY_NAVIGATION = 2 };
  13. namespace cc {
  14. struct OverscrollBehavior;
  15. }
  16. namespace gfx {
  17. class PointF;
  18. }
  19. namespace ui {
  20. class OverscrollRefreshHandler;
  21. // Simple pull-to-refresh styled effect. Listens to scroll events, conditionally
  22. // activating when:
  23. // 1) The scroll begins when the page's root layer 1) has no vertical scroll
  24. // offset and 2) lacks the overflow-y:hidden property.
  25. // 2) The page doesn't consume the initial scroll events.
  26. // 3) The initial scroll direction is upward.
  27. // The actuall pull response, animation and action are delegated to the
  28. // provided refresh handler.
  29. class UI_ANDROID_EXPORT OverscrollRefresh {
  30. public:
  31. // The default distance in dp from a side of the device to start a navigation
  32. // from.
  33. static constexpr int kDefaultNavigationEdgeWidth = 24;
  34. OverscrollRefresh(OverscrollRefreshHandler* handler, float edge_width);
  35. OverscrollRefresh(const OverscrollRefresh&) = delete;
  36. OverscrollRefresh& operator=(const OverscrollRefresh&) = delete;
  37. virtual ~OverscrollRefresh();
  38. // Scroll event stream listening methods.
  39. void OnScrollBegin(const gfx::PointF& pos);
  40. // Returns whether the refresh was activated.
  41. void OnScrollEnd(const gfx::Vector2dF& velocity);
  42. // Scroll ack listener. The effect will only be activated if |can_navigate|
  43. // is true which happens when the scroll update is not consumed and the
  44. // overscroll_behavior on y axis is 'auto'.
  45. // This method is made virtual for mocking.
  46. virtual void OnOverscrolled(
  47. const cc::OverscrollBehavior& overscroll_behavior);
  48. // Returns true if the effect has consumed the |scroll_delta|.
  49. bool WillHandleScrollUpdate(const gfx::Vector2dF& scroll_delta);
  50. // Release the effect (if active), preventing any associated refresh action.
  51. void ReleaseWithoutActivation();
  52. // Notify the effect of the latest scroll offset and overflow properties.
  53. // The effect will be disabled when the offset is non-zero or overflow is
  54. // hidden. Note: All dimensions are in device pixels.
  55. void OnFrameUpdated(const gfx::SizeF& viewport_size,
  56. const gfx::PointF& content_scroll_offset,
  57. bool root_overflow_y_hidden);
  58. // Reset the effect to its inactive state, immediately detaching and
  59. // disabling any active effects.
  60. // This method is made virtual for mocking.
  61. virtual void Reset();
  62. // Returns true if the refresh effect is either being manipulated or animated.
  63. // This method is made virtual for mocking.
  64. virtual bool IsActive() const;
  65. // Returns true if the effect is waiting for an unconsumed scroll to start.
  66. // This method is made virtual for mocking.
  67. virtual bool IsAwaitingScrollUpdateAck() const;
  68. protected:
  69. // This constructor is for mocking only.
  70. OverscrollRefresh();
  71. private:
  72. void Release(bool allow_refresh);
  73. bool scrolled_to_top_;
  74. // True if the content y offset was zero before scroll began. Overscroll
  75. // should not be triggered for the scroll that started from non-zero offset.
  76. bool top_at_scroll_start_;
  77. bool overflow_y_hidden_;
  78. enum ScrollConsumptionState {
  79. DISABLED,
  80. AWAITING_SCROLL_UPDATE_ACK,
  81. ENABLED,
  82. } scroll_consumption_state_;
  83. float viewport_width_;
  84. float scroll_begin_x_;
  85. float scroll_begin_y_;
  86. const float edge_width_; // in px
  87. gfx::Vector2dF cumulative_scroll_;
  88. const raw_ptr<OverscrollRefreshHandler> handler_;
  89. };
  90. } // namespace ui
  91. #endif // UI_ANDROID_OVERSCROLL_REFRESH_H_