ink_drop_ripple.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2016 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_INK_DROP_RIPPLE_H_
  5. #define UI_VIEWS_ANIMATION_INK_DROP_RIPPLE_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "ui/gfx/geometry/point.h"
  9. #include "ui/gfx/geometry/size.h"
  10. #include "ui/views/animation/ink_drop_ripple_observer.h"
  11. #include "ui/views/animation/ink_drop_state.h"
  12. #include "ui/views/views_export.h"
  13. namespace ui {
  14. class CallbackLayerAnimationObserver;
  15. class Layer;
  16. class LayerAnimationObserver;
  17. } // namespace ui
  18. namespace views {
  19. namespace test {
  20. class InkDropRippleTestApi;
  21. } // namespace test
  22. // Simple base class for animations that provide visual feedback for View state.
  23. // Manages the attached InkDropRippleObservers.
  24. //
  25. // TODO(bruthig): Document the ink drop ripple on chromium.org and add a link to
  26. // the doc here.
  27. class VIEWS_EXPORT InkDropRipple {
  28. public:
  29. // The opacity of the ink drop when it is not visible.
  30. static const float kHiddenOpacity;
  31. InkDropRipple();
  32. InkDropRipple(const InkDropRipple&) = delete;
  33. InkDropRipple& operator=(const InkDropRipple&) = delete;
  34. virtual ~InkDropRipple();
  35. // In the event that an animation is in progress for ink drop state 's1' and
  36. // an animation to a new state 's2' is triggered, then
  37. // AnimationEnded(s1, PRE_EMPTED) will be called before
  38. // AnimationStarted(s2).
  39. void set_observer(InkDropRippleObserver* observer) { observer_ = observer; }
  40. // Animates from the current InkDropState to the new |ink_drop_state|.
  41. //
  42. // NOTE: GetTargetInkDropState() should return the new |ink_drop_state| value
  43. // to any observers being notified as a result of the call.
  44. void AnimateToState(InkDropState ink_drop_state);
  45. // Snaps from the current InkDropState to the new |ink_drop_state|.
  46. void SnapToState(InkDropState ink_drop_state);
  47. InkDropState target_ink_drop_state() const { return target_ink_drop_state_; }
  48. // Immediately aborts all in-progress animations and hides the ink drop.
  49. //
  50. // NOTE: This will NOT raise Animation(Started|Ended) events for the state
  51. // transition to HIDDEN!
  52. void SnapToHidden();
  53. // Immediately snaps the ink drop to the ACTIVATED target state. All pending
  54. // animations are aborted. Events will be raised for the pending animations
  55. // as well as the transition to the ACTIVATED state.
  56. virtual void SnapToActivated();
  57. // The root Layer that can be added in to a Layer tree.
  58. virtual ui::Layer* GetRootLayer() = 0;
  59. // Returns true when the ripple is visible. This is different from checking if
  60. // the ink_drop_state() == HIDDEN because the ripple may be visible while it
  61. // animates to the target HIDDEN state.
  62. bool IsVisible();
  63. // Returns a test api to access internals of this. Default implmentations
  64. // should return nullptr and test specific subclasses can override to return
  65. // an instance.
  66. virtual test::InkDropRippleTestApi* GetTestApi();
  67. protected:
  68. // Animates the ripple from the |old_ink_drop_state| to the
  69. // |new_ink_drop_state|. |observer| is added to all LayerAnimationSequence's
  70. // used if not null.
  71. virtual void AnimateStateChange(InkDropState old_ink_drop_state,
  72. InkDropState new_ink_drop_state) = 0;
  73. // Updates the transforms, opacity, and visibility to a HIDDEN state.
  74. virtual void SetStateToHidden() = 0;
  75. virtual void AbortAllAnimations() = 0;
  76. // Get the current observer. CreateAnimationObserver must have already been
  77. // called.
  78. ui::LayerAnimationObserver* GetLayerAnimationObserver();
  79. private:
  80. // The Callback invoked when all of the animation sequences for the specific
  81. // |ink_drop_state| animation have started. |observer| is the
  82. // ui::CallbackLayerAnimationObserver which is notifying the callback.
  83. void AnimationStartedCallback(
  84. InkDropState ink_drop_state,
  85. const ui::CallbackLayerAnimationObserver& observer);
  86. // The Callback invoked when all of the animation sequences for the specific
  87. // |ink_drop_state| animation have finished. |observer| is the
  88. // ui::CallbackLayerAnimationObserver which is notifying the callback.
  89. bool AnimationEndedCallback(
  90. InkDropState ink_drop_state,
  91. const ui::CallbackLayerAnimationObserver& observer);
  92. // Creates a new animation observer bound to AnimationStartedCallback() and
  93. // AnimationEndedCallback().
  94. std::unique_ptr<ui::CallbackLayerAnimationObserver> CreateAnimationObserver(
  95. InkDropState ink_drop_state);
  96. // The target InkDropState.
  97. InkDropState target_ink_drop_state_ = InkDropState::HIDDEN;
  98. raw_ptr<InkDropRippleObserver> observer_ = nullptr;
  99. std::unique_ptr<ui::CallbackLayerAnimationObserver> animation_observer_;
  100. };
  101. } // namespace views
  102. #endif // UI_VIEWS_ANIMATION_INK_DROP_RIPPLE_H_