widget_fade_animator.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_VIEWS_ANIMATION_WIDGET_FADE_ANIMATOR_H_
  5. #define UI_VIEWS_ANIMATION_WIDGET_FADE_ANIMATOR_H_
  6. #include "base/callback_forward.h"
  7. #include "base/callback_list.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/scoped_observation.h"
  10. #include "base/time/time.h"
  11. #include "build/build_config.h"
  12. #include "build/chromeos_buildflags.h"
  13. #include "ui/gfx/animation/linear_animation.h"
  14. #include "ui/gfx/animation/tween.h"
  15. #include "ui/views/animation/animation_delegate_views.h"
  16. #include "ui/views/views_export.h"
  17. #include "ui/views/widget/widget.h"
  18. #include "ui/views/widget/widget_observer.h"
  19. namespace views {
  20. class Widget;
  21. // Animates a widget's opacity between fully hidden and fully shown, providing
  22. // a fade-in/fade-out effect.
  23. class VIEWS_EXPORT WidgetFadeAnimator : public AnimationDelegateViews,
  24. public WidgetObserver {
  25. public:
  26. // Describes the current fade animation.
  27. enum class FadeType {
  28. kNone,
  29. kFadeIn,
  30. kFadeOut,
  31. };
  32. // Defines a callback for when a fade completes. Not called on cancel. The
  33. // |animation_type| of the completed animation is specified (it will never be
  34. // kNone).
  35. using FadeCompleteCallbackSignature = void(WidgetFadeAnimator*,
  36. FadeType animation_type);
  37. using FadeCompleteCallback =
  38. base::RepeatingCallback<FadeCompleteCallbackSignature>;
  39. // Creates a new fade animator for the specified widget. If the widget closes
  40. // the animator will no longer be valid and should not be used.
  41. explicit WidgetFadeAnimator(Widget* widget);
  42. WidgetFadeAnimator(const WidgetFadeAnimator&) = delete;
  43. WidgetFadeAnimator& operator=(const WidgetFadeAnimator&) = delete;
  44. ~WidgetFadeAnimator() override;
  45. void set_fade_in_duration(base::TimeDelta fade_in_duration) {
  46. fade_in_duration_ = fade_in_duration;
  47. }
  48. base::TimeDelta fade_in_duration() const { return fade_in_duration_; }
  49. void set_fade_out_duration(base::TimeDelta fade_out_duration) {
  50. fade_out_duration_ = fade_out_duration;
  51. }
  52. base::TimeDelta fade_out_duration() const { return fade_out_duration_; }
  53. void set_tween_type(gfx::Tween::Type tween_type) { tween_type_ = tween_type; }
  54. gfx::Tween::Type tween_type() const { return tween_type_; }
  55. void set_close_on_hide(bool close_on_hide) { close_on_hide_ = close_on_hide; }
  56. bool close_on_hide() const { return close_on_hide_; }
  57. Widget* widget() { return widget_; }
  58. bool IsFadingIn() const { return animation_type_ == FadeType::kFadeIn; }
  59. bool IsFadingOut() const { return animation_type_ == FadeType::kFadeOut; }
  60. // Plays the fade-in animation. If the widget is not currently visible, it
  61. // will be made visible.
  62. void FadeIn();
  63. // Cancels any pending fade-in, leaves the widget at the current opacity to
  64. // avoid abrupt visual changes. CancelFadeIn() should be followed with
  65. // something, either another FadeIn(), or widget closing. It has no effect
  66. // if the widget is not fading in.
  67. void CancelFadeIn();
  68. // Plays the fade-out animation. At the end of the fade, the widget will be
  69. // hidden or closed, as per |close_on_hide|. If the widget is already hidden
  70. // or closed, completes immediately.
  71. void FadeOut();
  72. // Cancels any pending fade-out, returning the widget to 100% opacity. Has no
  73. // effect if the widget is not fading out.
  74. void CancelFadeOut();
  75. // Adds a listener for fade complete events.
  76. base::CallbackListSubscription AddFadeCompleteCallback(
  77. FadeCompleteCallback callback);
  78. private:
  79. // AnimationDelegateViews:
  80. void AnimationProgressed(const gfx::Animation* animation) override;
  81. void AnimationEnded(const gfx::Animation* animation) override;
  82. // WidgetObserver:
  83. void OnWidgetDestroying(Widget* widget) override;
  84. raw_ptr<Widget> widget_;
  85. base::ScopedObservation<Widget, WidgetObserver> widget_observation_{this};
  86. gfx::LinearAnimation fade_animation_{this};
  87. FadeType animation_type_ = FadeType::kNone;
  88. // Duration for fade-in animations. The default should be visually pleasing
  89. // for most applications.
  90. base::TimeDelta fade_in_duration_ = base::Milliseconds(200);
  91. // Duration for fade-out animations. The default should be visually pleasing
  92. // for most applications.
  93. base::TimeDelta fade_out_duration_ = base::Milliseconds(150);
  94. // The tween type to use. The default value should be pleasing for most
  95. // applications.
  96. gfx::Tween::Type tween_type_ = gfx::Tween::FAST_OUT_SLOW_IN;
  97. // Whether the widget should be closed at the end of a fade-out animation.
  98. bool close_on_hide_ = false;
  99. base::RepeatingCallbackList<FadeCompleteCallbackSignature>
  100. fade_complete_callbacks_;
  101. };
  102. } // namespace views
  103. #endif // UI_VIEWS_ANIMATION_WIDGET_FADE_ANIMATOR_H_