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 #include "ui/views/animation/ink_drop_animation_controller_impl.h" | 5 #include "ui/views/animation/ink_drop_animation_controller_impl.h" |
6 | 6 |
| 7 #include "base/auto_reset.h" |
7 #include "base/timer/timer.h" | 8 #include "base/timer/timer.h" |
8 #include "ui/compositor/layer.h" | 9 #include "ui/compositor/layer.h" |
9 #include "ui/views/animation/ink_drop_animation.h" | 10 #include "ui/views/animation/ink_drop_animation.h" |
10 #include "ui/views/animation/ink_drop_host.h" | 11 #include "ui/views/animation/ink_drop_host.h" |
11 #include "ui/views/animation/ink_drop_hover.h" | 12 #include "ui/views/animation/ink_drop_hover.h" |
12 | 13 |
13 namespace views { | 14 namespace views { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 // The duration, in milliseconds, of the hover state fade in animation when it | 18 // The duration, in milliseconds, of the hover state fade in animation when it |
18 // is triggered by user input. | 19 // is triggered by user input. |
19 const int kHoverFadeInFromUserInputDurationInMs = 250; | 20 const int kHoverFadeInFromUserInputDurationInMs = 250; |
20 | 21 |
21 // The duration, in milliseconds, of the hover state fade out animation when it | 22 // The duration, in milliseconds, of the hover state fade out animation when it |
22 // is triggered by user input. | 23 // is triggered by user input. |
23 const int kHoverFadeOutFromUserInputDurationInMs = 250; | 24 const int kHoverFadeOutFromUserInputDurationInMs = 250; |
24 | 25 |
25 // The duration, in milliseconds, of the hover state fade in animation when it | 26 // The duration, in milliseconds, of the hover state fade in animation when it |
26 // is triggered by an ink drop ripple animation ending. | 27 // is triggered by an ink drop ripple animation ending. |
27 const int kHoverFadeInAfterAnimationDurationInMs = 250; | 28 const int kHoverFadeInAfterAnimationDurationInMs = 250; |
28 | 29 |
29 // The duration, in milliseconds, of the hover state fade out animation when it | 30 // The duration, in milliseconds, of the hover state fade out animation when it |
30 // is triggered by an ink drop ripple animation starting. | 31 // is triggered by an ink drop ripple animation starting. |
31 const int kHoverFadeOutBeforeAnimationDurationInMs = 0; | 32 const int kHoverFadeOutBeforeAnimationDurationInMs = 300; |
32 | 33 |
33 // The amount of time in milliseconds that |hover_| should delay after a ripple | 34 // The amount of time in milliseconds that |hover_| should delay after a ripple |
34 // animation before fading in. | 35 // animation before fading in. |
35 const int kHoverFadeInAfterAnimationDelayInMs = 1000; | 36 const int kHoverFadeInAfterAnimationDelayInMs = 1000; |
36 | 37 |
| 38 // Returns true if an ink drop with the given |ink_drop_state| should |
| 39 // automatically transition to the InkDropState::HIDDEN state. |
| 40 bool ShouldAnimateToHidden(InkDropState ink_drop_state) { |
| 41 switch (ink_drop_state) { |
| 42 case views::InkDropState::QUICK_ACTION: |
| 43 case views::InkDropState::SLOW_ACTION: |
| 44 case views::InkDropState::DEACTIVATED: |
| 45 return true; |
| 46 default: |
| 47 return false; |
| 48 } |
| 49 } |
| 50 |
37 } // namespace | 51 } // namespace |
38 | 52 |
39 InkDropAnimationControllerImpl::InkDropAnimationControllerImpl( | 53 InkDropAnimationControllerImpl::InkDropAnimationControllerImpl( |
40 InkDropHost* ink_drop_host) | 54 InkDropHost* ink_drop_host) |
41 : ink_drop_host_(ink_drop_host), | 55 : ink_drop_host_(ink_drop_host), |
42 ink_drop_large_corner_radius_(0), | 56 ink_drop_large_corner_radius_(0), |
43 ink_drop_small_corner_radius_(0), | 57 ink_drop_small_corner_radius_(0), |
44 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), | 58 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), |
| 59 can_destroy_after_hidden_animation_(true), |
45 hover_after_animation_timer_(nullptr) { | 60 hover_after_animation_timer_(nullptr) { |
46 root_layer_->set_name("InkDropAnimationControllerImpl:RootLayer"); | 61 root_layer_->set_name("InkDropAnimationControllerImpl:RootLayer"); |
47 ink_drop_host_->AddInkDropLayer(root_layer_.get()); | 62 ink_drop_host_->AddInkDropLayer(root_layer_.get()); |
48 } | 63 } |
49 | 64 |
50 InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() { | 65 InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() { |
51 // Explicitly destroy the InkDropAnimation so that this still exists if | 66 // Explicitly destroy the InkDropAnimation so that this still exists if |
52 // views::InkDropAnimationObserver methods are called on this. | 67 // views::InkDropAnimationObserver methods are called on this. |
53 DestroyInkDropAnimation(); | 68 DestroyInkDropAnimation(); |
54 ink_drop_host_->RemoveInkDropLayer(root_layer_.get()); | 69 ink_drop_host_->RemoveInkDropLayer(root_layer_.get()); |
55 } | 70 } |
56 | 71 |
57 InkDropState InkDropAnimationControllerImpl::GetInkDropState() const { | 72 InkDropState InkDropAnimationControllerImpl::GetInkDropState() const { |
58 if (!ink_drop_animation_) | 73 if (!ink_drop_animation_) |
59 return InkDropState::HIDDEN; | 74 return InkDropState::HIDDEN; |
60 return ink_drop_animation_->ink_drop_state(); | 75 return ink_drop_animation_->ink_drop_state(); |
61 } | 76 } |
62 | 77 |
63 void InkDropAnimationControllerImpl::AnimateToState( | 78 void InkDropAnimationControllerImpl::AnimateToState( |
64 InkDropState ink_drop_state) { | 79 InkDropState ink_drop_state) { |
65 if (!ink_drop_animation_) | 80 if (!ink_drop_animation_) |
66 CreateInkDropAnimation(); | 81 CreateInkDropAnimation(); |
| 82 |
| 83 // The InkDropAnimationObserver::InkDropAnimationEnded() callback needs to |
| 84 // know if it is safe to destroy the |ink_drop_animation_| and it is not safe |
| 85 // when the notification is raised within a call to |
| 86 // InkDropAnimation::AnimateToState(). |
| 87 base::AutoReset<bool> auto_reset_can_destroy_after_hidden_animation( |
| 88 &can_destroy_after_hidden_animation_, false); |
| 89 |
| 90 if (ink_drop_state != views::InkDropState::HIDDEN) { |
| 91 SetHoveredInternal(false, base::TimeDelta::FromMilliseconds( |
| 92 kHoverFadeOutBeforeAnimationDurationInMs)); |
| 93 } |
| 94 |
| 95 // Make sure the ink drop starts from the HIDDEN state it was going to auto |
| 96 // transition to it. |
| 97 if (ink_drop_animation_->ink_drop_state() == InkDropState::HIDDEN || |
| 98 ShouldAnimateToHidden(ink_drop_animation_->ink_drop_state())) { |
| 99 ink_drop_animation_->HideImmediately(); |
| 100 } |
67 ink_drop_animation_->AnimateToState(ink_drop_state); | 101 ink_drop_animation_->AnimateToState(ink_drop_state); |
68 } | 102 } |
69 | 103 |
70 void InkDropAnimationControllerImpl::SetHovered(bool is_hovered) { | 104 void InkDropAnimationControllerImpl::SetHovered(bool is_hovered) { |
71 SetHoveredInternal(is_hovered, | 105 SetHoveredInternal(is_hovered, |
72 is_hovered ? base::TimeDelta::FromMilliseconds( | 106 is_hovered ? base::TimeDelta::FromMilliseconds( |
73 kHoverFadeInFromUserInputDurationInMs) | 107 kHoverFadeInFromUserInputDurationInMs) |
74 : base::TimeDelta::FromMilliseconds( | 108 : base::TimeDelta::FromMilliseconds( |
75 kHoverFadeOutFromUserInputDurationInMs)); | 109 kHoverFadeOutFromUserInputDurationInMs)); |
76 } | 110 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 SetHoveredInternal(false, base::TimeDelta::FromMilliseconds( | 186 SetHoveredInternal(false, base::TimeDelta::FromMilliseconds( |
153 kHoverFadeOutBeforeAnimationDurationInMs)); | 187 kHoverFadeOutBeforeAnimationDurationInMs)); |
154 } | 188 } |
155 } | 189 } |
156 | 190 |
157 void InkDropAnimationControllerImpl::InkDropAnimationEnded( | 191 void InkDropAnimationControllerImpl::InkDropAnimationEnded( |
158 InkDropState ink_drop_state, | 192 InkDropState ink_drop_state, |
159 InkDropAnimationEndedReason reason) { | 193 InkDropAnimationEndedReason reason) { |
160 if (reason != SUCCESS) | 194 if (reason != SUCCESS) |
161 return; | 195 return; |
162 switch (ink_drop_state) { | 196 if (ShouldAnimateToHidden(ink_drop_state)) { |
163 case views::InkDropState::QUICK_ACTION: | 197 ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN); |
164 case views::InkDropState::SLOW_ACTION: | 198 } else if (ink_drop_state == views::InkDropState::HIDDEN) { |
165 case views::InkDropState::DEACTIVATED: | 199 StartHoverAfterAnimationTimer(); |
166 ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN); | 200 if (can_destroy_after_hidden_animation_) { |
167 break; | |
168 case views::InkDropState::HIDDEN: | |
169 // TODO(bruthig): Consider only starting the timer if the InkDropHost is | |
170 // hovered now, as oppposed to when the timer fires. | |
171 StartHoverAfterAnimationTimer(); | |
172 // TODO(bruthig): Investigate whether creating and destroying | 201 // TODO(bruthig): Investigate whether creating and destroying |
173 // InkDropAnimations is expensive and consider creating an | 202 // InkDropAnimations is expensive and consider creating an |
174 // InkDropAnimationPool. See www.crbug.com/522175. | 203 // InkDropAnimationPool. See www.crbug.com/522175. |
175 DestroyInkDropAnimation(); | 204 DestroyInkDropAnimation(); |
176 break; | 205 } |
177 default: | |
178 break; | |
179 } | 206 } |
180 } | 207 } |
181 | 208 |
182 void InkDropAnimationControllerImpl::SetHoveredInternal( | 209 void InkDropAnimationControllerImpl::SetHoveredInternal( |
183 bool is_hovered, | 210 bool is_hovered, |
184 base::TimeDelta animation_duration) { | 211 base::TimeDelta animation_duration) { |
185 StopHoverAfterAnimationTimer(); | 212 StopHoverAfterAnimationTimer(); |
186 | 213 |
187 if (IsHovered() == is_hovered) | 214 if (IsHovered() == is_hovered) |
188 return; | 215 return; |
(...skipping 28 matching lines...) Expand all Loading... |
217 } | 244 } |
218 | 245 |
219 void InkDropAnimationControllerImpl::HoverAfterAnimationTimerFired() { | 246 void InkDropAnimationControllerImpl::HoverAfterAnimationTimerFired() { |
220 SetHoveredInternal(ink_drop_host_->ShouldShowInkDropHover(), | 247 SetHoveredInternal(ink_drop_host_->ShouldShowInkDropHover(), |
221 base::TimeDelta::FromMilliseconds( | 248 base::TimeDelta::FromMilliseconds( |
222 kHoverFadeInAfterAnimationDurationInMs)); | 249 kHoverFadeInAfterAnimationDurationInMs)); |
223 hover_after_animation_timer_.reset(); | 250 hover_after_animation_timer_.reset(); |
224 } | 251 } |
225 | 252 |
226 } // namespace views | 253 } // namespace views |
OLD | NEW |