| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_VIEWS_ANIMATION_INK_DROP_IMPL_H_ | 5 #ifndef UI_VIEWS_ANIMATION_INK_DROP_IMPL_H_ |
| 6 #define UI_VIEWS_ANIMATION_INK_DROP_IMPL_H_ | 6 #define UI_VIEWS_ANIMATION_INK_DROP_IMPL_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "ui/gfx/geometry/rect.h" | 11 #include "ui/gfx/geometry/rect.h" |
| 12 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
| 13 #include "ui/views/animation/ink_drop.h" | 13 #include "ui/views/animation/ink_drop.h" |
| 14 #include "ui/views/animation/ink_drop_highlight_observer.h" | 14 #include "ui/views/animation/ink_drop_highlight_observer.h" |
| 15 #include "ui/views/animation/ink_drop_ripple_observer.h" | 15 #include "ui/views/animation/ink_drop_ripple_observer.h" |
| 16 #include "ui/views/views_export.h" | 16 #include "ui/views/views_export.h" |
| 17 | 17 |
| 18 namespace base { | |
| 19 class Timer; | |
| 20 } // namespace base | |
| 21 | |
| 22 namespace views { | 18 namespace views { |
| 23 namespace test { | 19 namespace test { |
| 24 class InkDropImplTestApi; | 20 class InkDropImplTestApi; |
| 25 } // namespace test | 21 } // namespace test |
| 26 | 22 |
| 27 class InkDropRipple; | 23 class InkDropRipple; |
| 28 class InkDropHost; | 24 class InkDropHost; |
| 29 class InkDropHighlight; | 25 class InkDropHighlight; |
| 30 class InkDropFactoryTest; | 26 class InkDropFactoryTest; |
| 31 | 27 |
| 32 // A functional implementation of an InkDrop. | 28 // A functional implementation of an InkDrop. |
| 33 class VIEWS_EXPORT InkDropImpl : public InkDrop, | 29 class VIEWS_EXPORT InkDropImpl : public InkDrop, |
| 34 public InkDropRippleObserver, | 30 public InkDropRippleObserver, |
| 35 public InkDropHighlightObserver { | 31 public InkDropHighlightObserver { |
| 36 public: | 32 public: |
| 33 // The different auto highlight behaviors. |
| 34 enum class AutoHighlightMode { |
| 35 // No auto-highlighting is done. The highlight will only be shown/hidden as |
| 36 // per the hover/focus settings. |
| 37 NONE, |
| 38 // The highlight will be hidden when a ripple becomes visible. After the |
| 39 // ripple is hidden the highlight will be made visible again if the |
| 40 // hover/focus settings deem it should be. |
| 41 HIDE_ON_RIPPLE, |
| 42 // The highlight is made visible when the ripple becomes visible. After the |
| 43 // ripple is hidden the highlight will be hidden again if the hover/focus |
| 44 // settings deem it should be. |
| 45 SHOW_ON_RIPPLE, |
| 46 }; |
| 47 |
| 37 // Constructs an ink drop that will attach the ink drop to the given | 48 // Constructs an ink drop that will attach the ink drop to the given |
| 38 // |ink_drop_host|. | 49 // |ink_drop_host|. |
| 50 // |
| 51 // By default the highlight will be made visible while |this| is hovered but |
| 52 // not focused and the NONE AutoHighlightMode will be used. |
| 39 explicit InkDropImpl(InkDropHost* ink_drop_host); | 53 explicit InkDropImpl(InkDropHost* ink_drop_host); |
| 40 ~InkDropImpl() override; | 54 ~InkDropImpl() override; |
| 41 | 55 |
| 56 void SetShowHighlightOnHover(bool show_highlight_on_hover); |
| 57 |
| 58 void SetShowHighlightOnFocus(bool show_highlight_on_focus); |
| 59 |
| 60 // Auto highlighting is a mechanism to show/hide the highlight based on the |
| 61 // visibility of the ripple. See the documentation of the AutoHighlightMode |
| 62 // for more info on the different modes. |
| 63 // |
| 64 // This method is intended as a configuration option to be used after |
| 65 // construction. Behavior is undefined if |this| has already handled any |
| 66 // InkDrop inherited functions. |
| 67 void SetAutoHighlightMode(AutoHighlightMode auto_highlight_mode); |
| 68 |
| 42 // InkDrop: | 69 // InkDrop: |
| 43 InkDropState GetTargetInkDropState() const override; | 70 InkDropState GetTargetInkDropState() const override; |
| 44 void AnimateToState(InkDropState ink_drop_state) override; | 71 void AnimateToState(InkDropState ink_drop_state) override; |
| 45 void SnapToActivated() override; | 72 void SnapToActivated() override; |
| 46 void SetHovered(bool is_hovered) override; | 73 void SetHovered(bool is_hovered) override; |
| 47 void SetFocused(bool is_focused) override; | 74 void SetFocused(bool is_focused) override; |
| 48 | 75 |
| 49 private: | 76 private: |
| 77 friend class InkDropImplTest; |
| 50 friend class test::InkDropImplTestApi; | 78 friend class test::InkDropImplTestApi; |
| 51 | 79 |
| 80 // Forward declaration for use by the HighlightState class definition. |
| 81 class HighlightStateFactory; |
| 82 |
| 83 // Base HighlightState defines functions to handle all state changes that may |
| 84 // affect the highlight state. |
| 85 // |
| 86 // Subclasses are expected to handle state changes and transition the |
| 87 // InkDropImpl::highlight_state_ to new states as desired via the |
| 88 // InkDropImpl::SetHighlightState() method. |
| 89 // |
| 90 // New states should be created via the HighlightStateFactory and not |
| 91 // directly. This makes it possible for highlighting strategies to extend the |
| 92 // behavior of existing states and re-use existing state behavior. |
| 93 // |
| 94 // Subclasses are also expected to trigger the appropriate highlight |
| 95 // animations (e.g. fade in/out) via GetInkDrop()->SetHighlight(). Typically |
| 96 // this is done in the Enter()/Exit() functions. Triggering animations |
| 97 // anywhere else may be a sign that a new state should exist. |
| 98 class HighlightState { |
| 99 public: |
| 100 virtual ~HighlightState() {} |
| 101 |
| 102 // Called when |this| becomes the current state. Allows subclasses to |
| 103 // perform any work that should not be done in the constructor. It is ok for |
| 104 // subclass implementations to trigger state changes from within Enter(). |
| 105 virtual void Enter() {} |
| 106 |
| 107 // Called just before |this| is removed as the current state. Allows |
| 108 // subclasses to perform any work that should not be done in the destructor |
| 109 // but is required before exiting |this| state (e.g. releasing resources). |
| 110 // |
| 111 // Subclass implementations should NOT do any work that may trigger another |
| 112 // state change since a state change is already in progress. |
| 113 virtual void Exit() {} |
| 114 |
| 115 // Input state change handlers. |
| 116 |
| 117 // Called when the value of InkDropImpl::show_highlight_on_hover_ changes. |
| 118 virtual void ShowOnHoverChanged() = 0; |
| 119 |
| 120 // Called when the value of InkDropImpl::is_hovered_ changes. |
| 121 virtual void OnHoverChanged() = 0; |
| 122 |
| 123 // Called when the value of InkDropImpl::show_highlight_on_focus_ changes. |
| 124 virtual void ShowOnFocusChanged() = 0; |
| 125 |
| 126 // Called when the value of InkDropImpl::is_focused_ changes. |
| 127 virtual void OnFocusChanged() = 0; |
| 128 |
| 129 // Called when an ink drop ripple animation is started. |
| 130 virtual void AnimationStarted(InkDropState ink_drop_state) = 0; |
| 131 |
| 132 // Called when an ink drop ripple animation has ended. |
| 133 virtual void AnimationEnded(InkDropState ink_drop_state, |
| 134 InkDropAnimationEndedReason reason) = 0; |
| 135 |
| 136 protected: |
| 137 explicit HighlightState(HighlightStateFactory* state_factory) |
| 138 : state_factory_(state_factory) {} |
| 139 |
| 140 HighlightStateFactory* state_factory() { return state_factory_; } |
| 141 |
| 142 // Returns the ink drop that has |this| as the current state. |
| 143 InkDropImpl* GetInkDrop(); |
| 144 |
| 145 private: |
| 146 // Used by |this| to create the new states to transition to. |
| 147 HighlightStateFactory* state_factory_; |
| 148 |
| 149 DISALLOW_COPY_AND_ASSIGN(HighlightState); |
| 150 }; |
| 151 |
| 152 // Creates the different HighlightStates instances. A factory is used to make |
| 153 // it easier for states to extend and re-use existing state logic. |
| 154 class HighlightStateFactory { |
| 155 public: |
| 156 HighlightStateFactory(AutoHighlightMode highlight_mode, |
| 157 InkDropImpl* ink_drop); |
| 158 |
| 159 // Returns the initial state. |
| 160 std::unique_ptr<HighlightState> CreateStartState(); |
| 161 |
| 162 std::unique_ptr<HighlightState> CreateHiddenState( |
| 163 base::TimeDelta animation_duration, |
| 164 bool explode); |
| 165 |
| 166 std::unique_ptr<HighlightState> CreateVisibleState( |
| 167 base::TimeDelta animation_duration, |
| 168 bool explode); |
| 169 |
| 170 InkDropImpl* ink_drop() { return ink_drop_; } |
| 171 |
| 172 private: |
| 173 // Defines which concrete state types to create. |
| 174 AutoHighlightMode highlight_mode_; |
| 175 |
| 176 // The ink drop to invoke highlight changes on. |
| 177 InkDropImpl* ink_drop_; |
| 178 |
| 179 DISALLOW_COPY_AND_ASSIGN(HighlightStateFactory); |
| 180 }; |
| 181 |
| 182 // AutoHighlightMode::NONE |
| 183 class NoAutoHighlightHiddenState; |
| 184 class NoAutoHighlightVisibleState; |
| 185 |
| 186 // AutoHighlightMode::HIDE_ON_RIPPLE |
| 187 class HideHighlightOnRippleHiddenState; |
| 188 class HideHighlightOnRippleVisibleState; |
| 189 |
| 190 // AutoHighlightMode::SHOW_ON_RIPPLE states |
| 191 class ShowHighlightOnRippleHiddenState; |
| 192 class ShowHighlightOnRippleVisibleState; |
| 193 |
| 52 // Destroys |ink_drop_ripple_| if it's targeted to the HIDDEN state. | 194 // Destroys |ink_drop_ripple_| if it's targeted to the HIDDEN state. |
| 53 void DestroyHiddenTargetedAnimations(); | 195 void DestroyHiddenTargetedAnimations(); |
| 54 | 196 |
| 55 // Creates a new InkDropRipple and sets it to |ink_drop_ripple_|. If | 197 // Creates a new InkDropRipple and sets it to |ink_drop_ripple_|. If |
| 56 // |ink_drop_ripple_| wasn't null then it will be destroyed using | 198 // |ink_drop_ripple_| wasn't null then it will be destroyed using |
| 57 // DestroyInkDropRipple(). | 199 // DestroyInkDropRipple(). |
| 58 void CreateInkDropRipple(); | 200 void CreateInkDropRipple(); |
| 59 | 201 |
| 60 // Destroys the current |ink_drop_ripple_|. | 202 // Destroys the current |ink_drop_ripple_|. |
| 61 void DestroyInkDropRipple(); | 203 void DestroyInkDropRipple(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 92 InkDropAnimationEndedReason reason) override; | 234 InkDropAnimationEndedReason reason) override; |
| 93 | 235 |
| 94 // Enables or disables the highlight state based on |should_highlight| and if | 236 // Enables or disables the highlight state based on |should_highlight| and if |
| 95 // an animation is triggered it will be scheduled to have the given | 237 // an animation is triggered it will be scheduled to have the given |
| 96 // |animation_duration|. If |explode| is true the highlight will expand as it | 238 // |animation_duration|. If |explode| is true the highlight will expand as it |
| 97 // fades out. |explode| is ignored when |should_higlight| is true. | 239 // fades out. |explode| is ignored when |should_higlight| is true. |
| 98 void SetHighlight(bool should_highlight, | 240 void SetHighlight(bool should_highlight, |
| 99 base::TimeDelta animation_duration, | 241 base::TimeDelta animation_duration, |
| 100 bool explode); | 242 bool explode); |
| 101 | 243 |
| 102 // Returns true if this ink drop is hovered or focused. | 244 // Returns true if |this| the highlight should be visible based on the |
| 245 // hover/focus status. |
| 103 bool ShouldHighlight() const; | 246 bool ShouldHighlight() const; |
| 104 | 247 |
| 105 // Starts the |highlight_after_ripple_timer_| timer. This will stop the | 248 // Returns true if |this| the hilight should be visible based on the focus |
| 106 // current | 249 // status. |
| 107 // |highlight_after_ripple_timer_| instance if it exists. | 250 bool ShouldHighlightBasedOnFocus() const; |
| 108 void StartHighlightAfterRippleTimer(); | |
| 109 | 251 |
| 110 // Callback for when the |highlight_after_ripple_timer_| fires. | 252 // Updates the current |highlight_state_|. Calls Exit()/Enter() on the |
| 111 void HighlightAfterRippleTimerFired(); | 253 // previous/new state to notify them of the transition. |
| 254 // |
| 255 // Uses ExitHighlightState() to exit the current state. |
| 256 void SetHighlightState(std::unique_ptr<HighlightState> highlight_state); |
| 112 | 257 |
| 113 // The host of the ink drop. Used to poll for information such as whether the | 258 // Exits the current |highlight_state_| and sets it to null. Ensures state |
| 114 // highlight should be shown or not. | 259 // transitions are not triggered during HighlightStatae::Exit() calls on debug |
| 260 // builds. |
| 261 void ExitHighlightState(); |
| 262 |
| 263 // The host of the ink drop. Used to create the ripples and highlights, and to |
| 264 // add/remove the root layer to/from it. |
| 115 InkDropHost* ink_drop_host_; | 265 InkDropHost* ink_drop_host_; |
| 116 | 266 |
| 117 // The root Layer that parents the InkDropRipple layers and the | 267 // The root Layer that parents the InkDropRipple layers and the |
| 118 // InkDropHighlight layers. The |root_layer_| is the one that is added and | 268 // InkDropHighlight layers. The |root_layer_| is the one that is added and |
| 119 // removed from the InkDropHost. | 269 // removed from the |ink_drop_host_|. |
| 120 std::unique_ptr<ui::Layer> root_layer_; | 270 std::unique_ptr<ui::Layer> root_layer_; |
| 121 | 271 |
| 122 // True when the |root_layer_| has been added to the |ink_drop_host_|. | 272 // True when the |root_layer_| has been added to the |ink_drop_host_|. |
| 123 bool root_layer_added_to_host_; | 273 bool root_layer_added_to_host_; |
| 124 | 274 |
| 125 // The current InkDropHighlight. Lazily created using | 275 // The current InkDropHighlight. Lazily created using |
| 126 // CreateInkDropHighlight(); | 276 // CreateInkDropHighlight(); |
| 127 std::unique_ptr<InkDropHighlight> highlight_; | 277 std::unique_ptr<InkDropHighlight> highlight_; |
| 128 | 278 |
| 279 // True denotes the highlight should be shown when |this| is hovered. |
| 280 bool show_highlight_on_hover_; |
| 281 |
| 282 // True denotes the highlight should be shown when |this| is focused. |
| 283 bool show_highlight_on_focus_; |
| 284 |
| 129 // Tracks the logical hovered state of |this| as manipulated by the public | 285 // Tracks the logical hovered state of |this| as manipulated by the public |
| 130 // SetHovered() function. | 286 // SetHovered() function. |
| 131 bool is_hovered_; | 287 bool is_hovered_; |
| 132 | 288 |
| 133 // Tracks the logical focused state of |this| as manipulated by the public | 289 // Tracks the logical focused state of |this| as manipulated by the public |
| 134 // SetFocused() function. | 290 // SetFocused() function. |
| 135 bool is_focused_; | 291 bool is_focused_; |
| 136 | 292 |
| 137 // The current InkDropRipple. Created on demand using CreateInkDropRipple(). | 293 // The current InkDropRipple. Created on demand using CreateInkDropRipple(). |
| 138 std::unique_ptr<InkDropRipple> ink_drop_ripple_; | 294 std::unique_ptr<InkDropRipple> ink_drop_ripple_; |
| 139 | 295 |
| 140 // The timer used to delay the highlight fade in after an ink drop ripple | 296 // Used by |this| to initialize the starting |highlight_state_| and by the |
| 141 // animation. | 297 // current |highlight_state_| to create the next state. |
| 142 std::unique_ptr<base::Timer> highlight_after_ripple_timer_; | 298 std::unique_ptr<HighlightStateFactory> highlight_state_factory_; |
| 299 |
| 300 // The current state object that handles all inputs that affect the visibility |
| 301 // of the |highlight_|. |
| 302 std::unique_ptr<HighlightState> highlight_state_; |
| 303 |
| 304 // Used to ensure highlight state transitions are not triggered when exiting |
| 305 // the current state. |
| 306 bool exiting_highlight_state_; |
| 143 | 307 |
| 144 DISALLOW_COPY_AND_ASSIGN(InkDropImpl); | 308 DISALLOW_COPY_AND_ASSIGN(InkDropImpl); |
| 145 }; | 309 }; |
| 146 | 310 |
| 147 } // namespace views | 311 } // namespace views |
| 148 | 312 |
| 149 #endif // UI_VIEWS_ANIMATION_INK_DROP_IMPL_H_ | 313 #endif // UI_VIEWS_ANIMATION_INK_DROP_IMPL_H_ |
| OLD | NEW |