ink_drop.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2015 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_H_
  5. #define UI_VIEWS_ANIMATION_INK_DROP_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/observer_list.h"
  9. #include "base/time/time.h"
  10. #include "ui/base/metadata/metadata_header_macros.h"
  11. #include "ui/compositor/layer_tree_owner.h"
  12. #include "ui/events/event_handler.h"
  13. #include "ui/gfx/geometry/rect.h"
  14. #include "ui/gfx/geometry/size.h"
  15. #include "ui/views/animation/ink_drop_state.h"
  16. #include "ui/views/view.h"
  17. #include "ui/views/views_export.h"
  18. namespace views {
  19. class InkDropHost;
  20. class InkDropObserver;
  21. class View;
  22. // Base class that manages the lifetime and state of an ink drop ripple as
  23. // well as visual hover state feedback.
  24. class VIEWS_EXPORT InkDrop {
  25. public:
  26. InkDrop(const InkDrop&) = delete;
  27. InkDrop& operator=(const InkDrop&) = delete;
  28. virtual ~InkDrop();
  29. // TODO(pbos): Make sure what's installed here implements InkDrop so that can
  30. // be used as type instead of InkDropHost.
  31. static void Install(View* host, std::unique_ptr<InkDropHost> ink_drop);
  32. // Removes the InkDrop from `host`.
  33. static void Remove(View* host);
  34. // TODO(pbos): Make sure what's installed here implements InkDrop so that can
  35. // be used as type instead of InkDropHost.
  36. static const InkDropHost* Get(const View* host);
  37. static InkDropHost* Get(View* host) {
  38. return const_cast<InkDropHost*>(Get(const_cast<const View*>(host)));
  39. }
  40. // Create an InkDrop appropriate for the "square" InkDropRipple effect. This
  41. // InkDrop hides when the ripple effect is active instead of layering
  42. // underneath it.
  43. static std::unique_ptr<InkDrop> CreateInkDropForSquareRipple(
  44. InkDropHost* host,
  45. bool highlight_on_hover = true,
  46. bool highlight_on_focus = false);
  47. // Configure `host` to use CreateInkDropForSquareRipple().
  48. static void UseInkDropForSquareRipple(InkDropHost* host,
  49. bool highlight_on_hover = true,
  50. bool highlight_on_focus = false);
  51. // Create an InkDrop appropriate for the "flood-fill" InkDropRipple effect.
  52. // This InkDrop shows as a response to the ripple effect.
  53. static std::unique_ptr<InkDrop> CreateInkDropForFloodFillRipple(
  54. InkDropHost* host,
  55. bool highlight_on_hover = true,
  56. bool highlight_on_focus = false);
  57. // Configure `host` to use CreateInkDropForFloodFillRipple().
  58. static void UseInkDropForFloodFillRipple(InkDropHost* host,
  59. bool highlight_on_hover = true,
  60. bool highlight_on_focus = false);
  61. // Create an InkDrop whose highlight does not react to its ripple.
  62. static std::unique_ptr<InkDrop> CreateInkDropWithoutAutoHighlight(
  63. InkDropHost* host,
  64. bool highlight_on_hover = true,
  65. bool highlight_on_focus = false);
  66. // Configure `host` to use CreateInkDropWithoutAutoHighlight().
  67. static void UseInkDropWithoutAutoHighlight(InkDropHost* host,
  68. bool highlight_on_hover = true,
  69. bool highlight_on_focus = false);
  70. // Called by ink drop hosts when their size is changed.
  71. virtual void HostSizeChanged(const gfx::Size& new_size) = 0;
  72. // Called by ink drop hosts when their transform is changed.
  73. virtual void HostTransformChanged(const gfx::Transform& new_transform) = 0;
  74. // Gets the target state of the ink drop.
  75. virtual InkDropState GetTargetInkDropState() const = 0;
  76. // Animates from the current InkDropState to |ink_drop_state|.
  77. virtual void AnimateToState(InkDropState ink_drop_state) = 0;
  78. // Sets hover highlight fade animations to last for |duration|.
  79. virtual void SetHoverHighlightFadeDuration(base::TimeDelta duration) = 0;
  80. // Clears any set hover highlight fade durations and uses the default
  81. // durations instead.
  82. virtual void UseDefaultHoverHighlightFadeDuration() = 0;
  83. // Immediately snaps the InkDropState to ACTIVATED and HIDDEN specifically.
  84. // These are more specific implementations of the non-existent
  85. // SnapToState(InkDropState) function are the only ones available because
  86. // they were the only InkDropState that clients needed to skip animations
  87. // for.
  88. virtual void SnapToActivated() = 0;
  89. virtual void SnapToHidden() = 0;
  90. // Enables or disables the hover state.
  91. virtual void SetHovered(bool is_hovered) = 0;
  92. // Enables or disables the focus state.
  93. virtual void SetFocused(bool is_focused) = 0;
  94. // Returns true if the highlight animation is in the process of fading in or
  95. // is visible.
  96. virtual bool IsHighlightFadingInOrVisible() const = 0;
  97. // Enables or disables the highlight when the target is hovered.
  98. virtual void SetShowHighlightOnHover(bool show_highlight_on_hover) = 0;
  99. // Enables or disables the highlight when the target is focused.
  100. virtual void SetShowHighlightOnFocus(bool show_highlight_on_focus) = 0;
  101. // Methods to add/remove observers for this object.
  102. void AddObserver(InkDropObserver* observer);
  103. void RemoveObserver(InkDropObserver* observer);
  104. protected:
  105. InkDrop();
  106. // Notifes all of the observers that the animation has started.
  107. void NotifyInkDropAnimationStarted();
  108. // Notifies all of the observers that an animation to a state has ended.
  109. void NotifyInkDropRippleAnimationEnded(InkDropState state);
  110. private:
  111. base::ObserverList<InkDropObserver>::Unchecked observers_;
  112. };
  113. // A View which can be used to parent ink drop layers. Typically this is used
  114. // as a non-ancestor view to labels so that the labels can paint on an opaque
  115. // canvas. This is used to avoid ugly text renderings when labels with subpixel
  116. // rendering enabled are painted onto a non-opaque canvas.
  117. // TODO(pbos): Replace with a function that returns unique_ptr<View>, this only
  118. // calls SetProcessEventsWithinSubtree(false) right now.
  119. class VIEWS_EXPORT InkDropContainerView : public View {
  120. public:
  121. METADATA_HEADER(InkDropContainerView);
  122. InkDropContainerView();
  123. InkDropContainerView(const InkDropContainerView&) = delete;
  124. InkDropContainerView& operator=(const InkDropContainerView&) = delete;
  125. };
  126. } // namespace views
  127. #endif // UI_VIEWS_ANIMATION_INK_DROP_H_