scrollbar_animation_controller.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifndef CC_INPUT_SCROLLBAR_ANIMATION_CONTROLLER_H_
  5. #define CC_INPUT_SCROLLBAR_ANIMATION_CONTROLLER_H_
  6. #include <memory>
  7. #include "base/cancelable_callback.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/time/time.h"
  11. #include "cc/cc_export.h"
  12. #include "cc/input/single_scrollbar_animation_controller_thinning.h"
  13. #include "cc/layers/layer_impl.h"
  14. #include "cc/layers/scrollbar_layer_impl_base.h"
  15. #include "ui/gfx/geometry/vector2d_f.h"
  16. namespace cc {
  17. class CC_EXPORT ScrollbarAnimationControllerClient {
  18. public:
  19. virtual void PostDelayedScrollbarAnimationTask(base::OnceClosure task,
  20. base::TimeDelta delay) = 0;
  21. virtual void SetNeedsRedrawForScrollbarAnimation() = 0;
  22. virtual void SetNeedsAnimateForScrollbarAnimation() = 0;
  23. virtual void DidChangeScrollbarVisibility() = 0;
  24. virtual ScrollbarSet ScrollbarsFor(ElementId scroll_element_id) const = 0;
  25. virtual bool IsFluentScrollbar() const = 0;
  26. protected:
  27. virtual ~ScrollbarAnimationControllerClient() {}
  28. };
  29. // This class show scrollbars when scroll and fade out after an idle delay.
  30. // The fade animations works on both scrollbars and is controlled in this class
  31. // This class also passes the mouse state to each
  32. // SingleScrollbarAnimationControllerThinning. The thinning animations are
  33. // independent between vertical/horizontal and are managed by the
  34. // SingleScrollbarAnimationControllerThinnings.
  35. class CC_EXPORT ScrollbarAnimationController {
  36. public:
  37. // ScrollbarAnimationController for Android. It only has show & fade out
  38. // animation.
  39. static std::unique_ptr<ScrollbarAnimationController>
  40. CreateScrollbarAnimationControllerAndroid(
  41. ElementId scroll_element_id,
  42. ScrollbarAnimationControllerClient* client,
  43. base::TimeDelta fade_delay,
  44. base::TimeDelta fade_duration,
  45. float initial_opacity);
  46. // ScrollbarAnimationController for Desktop Overlay Scrollbar. It has show &
  47. // fade out animation and thinning animation.
  48. static std::unique_ptr<ScrollbarAnimationController>
  49. CreateScrollbarAnimationControllerAuraOverlay(
  50. ElementId scroll_element_id,
  51. ScrollbarAnimationControllerClient* client,
  52. base::TimeDelta fade_delay,
  53. base::TimeDelta fade_duration,
  54. base::TimeDelta thinning_duration,
  55. float initial_opacity);
  56. ~ScrollbarAnimationController();
  57. bool ScrollbarsHidden() const;
  58. bool visibility_changed() const { return visibility_changed_; }
  59. void ClearVisibilityChanged() { visibility_changed_ = false; }
  60. bool Animate(base::TimeTicks now);
  61. // WillUpdateScroll expects to be called even if the scroll position won't
  62. // change as a result of the scroll. Only effect Aura Overlay Scrollbar.
  63. void WillUpdateScroll();
  64. // DidScrollUpdate expects to be called only if the scroll position change.
  65. // Effect both Android and Aura Overlay Scrollbar.
  66. void DidScrollUpdate();
  67. void DidMouseDown();
  68. void DidMouseUp();
  69. void DidMouseLeave();
  70. void DidMouseMove(const gfx::PointF& device_viewport_point);
  71. // Called when we want to show the scrollbars.
  72. void DidRequestShow();
  73. void UpdateTickmarksVisibility(bool show);
  74. // These methods are public for testing.
  75. bool MouseIsOverScrollbarThumb(ScrollbarOrientation orientation) const;
  76. bool MouseIsNearScrollbarThumb(ScrollbarOrientation orientation) const;
  77. bool MouseIsNearScrollbar(ScrollbarOrientation orientation) const;
  78. bool MouseIsNearAnyScrollbar() const;
  79. ScrollbarSet Scrollbars() const;
  80. SingleScrollbarAnimationControllerThinning& GetScrollbarAnimationController(
  81. ScrollbarOrientation) const;
  82. private:
  83. // Describes whether the current animation should FadeIn or FadeOut.
  84. enum class AnimationChange { NONE, FADE_IN, FADE_OUT };
  85. ScrollbarAnimationController(ElementId scroll_element_id,
  86. ScrollbarAnimationControllerClient* client,
  87. base::TimeDelta fade_delay,
  88. base::TimeDelta fade_duration,
  89. float initial_opacity);
  90. ScrollbarAnimationController(ElementId scroll_element_id,
  91. ScrollbarAnimationControllerClient* client,
  92. base::TimeDelta fade_delay,
  93. base::TimeDelta fade_duration,
  94. base::TimeDelta thinning_duration,
  95. float initial_opacity);
  96. // Any scrollbar state update would show scrollbar hen post the delay fade out
  97. // if needed.
  98. void UpdateScrollbarState();
  99. // Returns how far through the animation we are as a progress value from
  100. // 0 to 1.
  101. float AnimationProgressAtTime(base::TimeTicks now);
  102. void RunAnimationFrame(float progress);
  103. void StartAnimation();
  104. void StopAnimation();
  105. void Show();
  106. void PostDelayedAnimation(AnimationChange animation_change);
  107. bool Captured() const;
  108. void ApplyOpacityToScrollbars(float opacity);
  109. raw_ptr<ScrollbarAnimationControllerClient> client_;
  110. base::TimeTicks last_awaken_time_;
  111. base::TimeDelta fade_delay_;
  112. base::TimeDelta fade_duration_;
  113. bool need_trigger_scrollbar_fade_in_;
  114. bool is_animating_;
  115. AnimationChange animation_change_;
  116. const ElementId scroll_element_id_;
  117. base::CancelableOnceClosure delayed_scrollbar_animation_;
  118. float opacity_;
  119. const bool show_scrollbars_on_scroll_gesture_;
  120. const bool need_thinning_animation_;
  121. // Controls whether an overlay scrollbar should fade in/out. Should be True
  122. // for Aura overlay scrollbars and False for Fluent overlay scrollbars.
  123. const bool need_fade_animation_;
  124. bool is_mouse_down_;
  125. bool tickmarks_showing_;
  126. bool visibility_changed_ = false;
  127. std::unique_ptr<SingleScrollbarAnimationControllerThinning>
  128. vertical_controller_;
  129. std::unique_ptr<SingleScrollbarAnimationControllerThinning>
  130. horizontal_controller_;
  131. base::WeakPtrFactory<ScrollbarAnimationController> weak_factory_{this};
  132. };
  133. } // namespace cc
  134. #endif // CC_INPUT_SCROLLBAR_ANIMATION_CONTROLLER_H_