scrollbar_animator_mac.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2021 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_NATIVE_THEME_SCROLLBAR_ANIMATOR_MAC_H_
  5. #define UI_NATIVE_THEME_SCROLLBAR_ANIMATOR_MAC_H_
  6. #include "base/callback.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/task/single_thread_task_runner.h"
  9. #include "base/time/time.h"
  10. #include "base/timer/timer.h"
  11. #include "ui/gfx/animation/keyframe/timing_function.h"
  12. #include "ui/native_theme/native_theme_export.h"
  13. namespace ui {
  14. // Timer used for animating scrollbar effects.
  15. // TODO(https://crbug.com/961835): Change this to be driven by the client
  16. // (Blink or Views) animation system.
  17. class NATIVE_THEME_EXPORT ScrollbarAnimationTimerMac {
  18. public:
  19. ScrollbarAnimationTimerMac(
  20. base::RepeatingCallback<void(double)> callback,
  21. double duration,
  22. scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  23. ~ScrollbarAnimationTimerMac();
  24. void Start();
  25. void Stop();
  26. void SetDuration(double duration);
  27. private:
  28. void TimerFired();
  29. base::RepeatingTimer timer_;
  30. double start_time_; // In seconds.
  31. double duration_; // In seconds.
  32. base::RepeatingCallback<void(double)> callback_;
  33. std::unique_ptr<gfx::CubicBezierTimingFunction> timing_function_;
  34. };
  35. class NATIVE_THEME_EXPORT OverlayScrollbarAnimatorMac {
  36. public:
  37. class Client {
  38. public:
  39. virtual ~Client() {}
  40. // Return true if the mouse is currently in the tracks' frame. This is used
  41. // during an initial scroll, because MouseDidEnter and MouseDidExit are not
  42. // while the scrollbar is hidden.
  43. virtual bool IsMouseInScrollbarFrameRect() const = 0;
  44. // Set whether all of the scrollbar is hidden.
  45. virtual void SetHidden(bool) = 0;
  46. // Called whenever the value of `GetThumbAlpha` or `GetThumbWidth` may
  47. // have changed.
  48. virtual void SetThumbNeedsDisplay() = 0;
  49. // Called whenever the value of `GetTrackAlpha` may have changed.
  50. virtual void SetTrackNeedsDisplay() = 0;
  51. };
  52. OverlayScrollbarAnimatorMac(
  53. Client* client,
  54. int thumb_width_expanded,
  55. int thumb_width_unexpanded,
  56. scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  57. ~OverlayScrollbarAnimatorMac();
  58. // Called when the mouse moves to be over the scrollbar's area.
  59. void MouseDidEnter();
  60. // Called when the mouse leave the scrollbar's area.
  61. void MouseDidExit();
  62. // Called in response to a scroll in the direction of this scrollbar.
  63. void DidScroll();
  64. // Retrieve the rendering properties of the scrollbar.
  65. float GetThumbAlpha() const { return thumb_alpha_; }
  66. float GetTrackAlpha() const { return track_alpha_; }
  67. int GetThumbWidth() const { return thumb_width_; }
  68. protected:
  69. void ExpandThumbAnimationStart();
  70. void ExpandThumbAnimationTicked(double progress);
  71. void FadeInTrackAnimationStart();
  72. void FadeInTrackAnimationTicked(double progress);
  73. void FadeOutTimerUpdate();
  74. void FadeOutAnimationStart();
  75. void FadeOutAnimationTicked(double progress);
  76. void FadeOutAnimationCancel();
  77. const raw_ptr<Client> client_; // Weak, owns `this`.
  78. const int thumb_width_expanded_;
  79. const int thumb_width_unexpanded_;
  80. int thumb_width_ = 0;
  81. float thumb_alpha_ = 0;
  82. float track_alpha_ = 0;
  83. bool mouse_in_track_ = false;
  84. static const float kAnimationDurationSeconds;
  85. static const base::TimeDelta kFadeOutDelay;
  86. // Animator for expanding `thumb_width_` when the mouse enters the
  87. // scrollbar area.
  88. std::unique_ptr<ScrollbarAnimationTimerMac> expand_thumb_animation_;
  89. // Animator for fading in `track_alpha_` when the mouse enters the scrollbar
  90. // area. Note that `thumb_alpha_` never fades in (it instantaneously appears).
  91. std::unique_ptr<ScrollbarAnimationTimerMac> fade_in_track_animation_;
  92. // Animator for fading out `track_alpha_` and `thumb_alpha_` together after
  93. // inactivity.
  94. std::unique_ptr<ScrollbarAnimationTimerMac> fade_out_animation_;
  95. // Timer to trigger the `fade_out_animation_`.
  96. std::unique_ptr<base::RetainingOneShotTimer> start_scrollbar_fade_out_timer_;
  97. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  98. base::WeakPtrFactory<OverlayScrollbarAnimatorMac> weak_factory_;
  99. };
  100. } // namespace ui
  101. #endif // UI_NATIVE_THEME_OVERLAY_SCROLLBAR_ANIMATOR_MAC_H_