Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: ui/views/animation/ink_drop_animation_controller_impl.cc

Issue 1422593003: Made material design ink drop QUICK_ACTION animation more visible. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed varkha@'s comments and minor fixes. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
8 #include "base/timer/timer.h"
9 #include "ui/compositor/layer.h"
7 #include "ui/views/animation/ink_drop_animation.h" 10 #include "ui/views/animation/ink_drop_animation.h"
11 #include "ui/views/animation/ink_drop_consumer.h"
8 #include "ui/views/animation/ink_drop_host.h" 12 #include "ui/views/animation/ink_drop_host.h"
13 #include "ui/views/animation/ink_drop_hover.h"
9 14
10 namespace views { 15 namespace views {
11 16
17 namespace {
18
19 // The duration, in milliseconds, of the hover state fade in animation when it
20 // is triggered by user input.
21 const int kHoverFadeInFromUserInputDurationInMs = 250;
22
23 // The duration, in milliseconds, of the hover state fade out animation when it
24 // is triggered by user input.
25 const int kHoverFadeOutFromUserInputDurationInMs = 250;
26
27 // The duration, in milliseconds, of the hover state fade in animation when it
28 // is triggered by an ink drop ripple animation ending.
29 const int kHoverFadeInAfterAnimationDurationInMs = 250;
30
31 // The duration, in milliseconds, of the hover state fade out animation when it
32 // is triggered by an ink drop ripple animation starting.
33 const int kHoverFadeOutBeforeAnimationDurationInMs = 0;
34
35 // The amount of time in milliseconds that |hover_| should delay after a ripple
36 // animation before fading in.
37 const int kHoverFadeInAfterAnimationDelayInMs = 1000;
38
39 } // namespace
40
12 InkDropAnimationControllerImpl::InkDropAnimationControllerImpl( 41 InkDropAnimationControllerImpl::InkDropAnimationControllerImpl(
13 InkDropHost* ink_drop_host) 42 InkDropHost* ink_drop_host,
14 : ink_drop_host_(ink_drop_host) {} 43 InkDropConsumer* ink_drop_consumer)
44 : ink_drop_host_(ink_drop_host),
45 ink_drop_consumer_(ink_drop_consumer),
46 ink_drop_large_corner_radius_(0),
47 ink_drop_small_corner_radius_(0),
48 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)),
49 can_destroy_after_hidden_animation_(true) {
50 ink_drop_host_->AddInkDropLayer(root_layer_.get());
51 }
15 52
16 InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() { 53 InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() {
17 // Explicitly destroy the InkDropAnimation so that this still exists if 54 // Explicitly destroy the InkDropAnimation so that this still exists if
18 // views::InkDropAnimationObserver methods are called on this. 55 // views::InkDropAnimationObserver methods are called on this.
19 DestroyInkDropAnimation(); 56 DestroyInkDropAnimation();
57 ink_drop_host_->RemoveInkDropLayer(root_layer_.get());
20 } 58 }
21 59
22 InkDropState InkDropAnimationControllerImpl::GetInkDropState() const { 60 InkDropState InkDropAnimationControllerImpl::GetInkDropState() const {
23 if (!ink_drop_animation_) 61 if (!ink_drop_animation_)
24 return InkDropState::HIDDEN; 62 return InkDropState::HIDDEN;
25 return ink_drop_animation_->ink_drop_state(); 63 return ink_drop_animation_->ink_drop_state();
26 } 64 }
27 65
28 void InkDropAnimationControllerImpl::AnimateToState( 66 void InkDropAnimationControllerImpl::AnimateToState(
29 InkDropState ink_drop_state) { 67 InkDropState ink_drop_state) {
30 if (!ink_drop_animation_) 68 if (!ink_drop_animation_)
31 CreateInkDropAnimation(); 69 CreateInkDropAnimation();
70
71 // The InkDropAnimationObserver::InkDropAnimationEnded() callback needs to
72 // know if it is safe to destroy the |ink_drop_animation_| and it is not safe
73 // when the notification is raised within a call to
74 // InkDropAnimation::AnimateToState().
75 base::AutoReset<bool> auto_reset_can_destroy_after_hidden_animation(
76 &can_destroy_after_hidden_animation_, false);
77
78 // Make sure the ink drop completes its full state transitions if it was going
79 // to auto transition to the HIDDEN state.
80 if (WillAutoAnimateToHidden())
81 ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN);
32 ink_drop_animation_->AnimateToState(ink_drop_state); 82 ink_drop_animation_->AnimateToState(ink_drop_state);
33 } 83 }
34 84
85 bool InkDropAnimationControllerImpl::WillAutoAnimateToHidden() const {
86 if (!ink_drop_animation_)
87 return false;
88 switch (ink_drop_animation_->ink_drop_state()) {
89 case views::InkDropState::QUICK_ACTION:
90 case views::InkDropState::SLOW_ACTION:
91 case views::InkDropState::DEACTIVATED:
92 return true;
93 default:
94 return false;
95 }
96 }
97
98 void InkDropAnimationControllerImpl::SetHovered(bool is_hovered) {
99 SetHoveredInternal(is_hovered,
100 is_hovered ? base::TimeDelta::FromMilliseconds(
101 kHoverFadeInFromUserInputDurationInMs)
102 : base::TimeDelta::FromMilliseconds(
103 kHoverFadeOutFromUserInputDurationInMs));
104 }
105
106 bool InkDropAnimationControllerImpl::IsHovered() const {
107 return hover_ && hover_->IsVisible();
108 }
109
35 gfx::Size InkDropAnimationControllerImpl::GetInkDropLargeSize() const { 110 gfx::Size InkDropAnimationControllerImpl::GetInkDropLargeSize() const {
36 return ink_drop_large_size_; 111 return ink_drop_large_size_;
37 } 112 }
38 113
39 void InkDropAnimationControllerImpl::SetInkDropSize(const gfx::Size& large_size, 114 void InkDropAnimationControllerImpl::SetInkDropSize(const gfx::Size& large_size,
40 int large_corner_radius, 115 int large_corner_radius,
41 const gfx::Size& small_size, 116 const gfx::Size& small_size,
42 int small_corner_radius) { 117 int small_corner_radius) {
43 // TODO(bruthig): Fix the ink drop animations to work for non-square sizes. 118 // TODO(bruthig): Fix the ink drop animations to work for non-square sizes.
44 DCHECK_EQ(large_size.width(), large_size.height()) 119 DCHECK_EQ(large_size.width(), large_size.height())
45 << "The ink drop animation does not currently support non-square sizes."; 120 << "The ink drop animation does not currently support non-square sizes.";
46 DCHECK_EQ(small_size.width(), small_size.height()) 121 DCHECK_EQ(small_size.width(), small_size.height())
47 << "The ink drop animation does not currently support non-square sizes."; 122 << "The ink drop animation does not currently support non-square sizes.";
48 ink_drop_large_size_ = large_size; 123 ink_drop_large_size_ = large_size;
49 ink_drop_large_corner_radius_ = large_corner_radius; 124 ink_drop_large_corner_radius_ = large_corner_radius;
50 ink_drop_small_size_ = small_size; 125 ink_drop_small_size_ = small_size;
51 ink_drop_small_corner_radius_ = small_corner_radius; 126 ink_drop_small_corner_radius_ = small_corner_radius;
52 ink_drop_animation_.reset(); 127
128 DestroyInkDropAnimation();
129 DestroyInkDropHover();
53 } 130 }
54 131
55 void InkDropAnimationControllerImpl::SetInkDropCenter( 132 void InkDropAnimationControllerImpl::SetInkDropCenter(
56 const gfx::Point& center_point) { 133 const gfx::Point& center_point) {
57 ink_drop_center_ = center_point; 134 ink_drop_center_ = center_point;
58 if (ink_drop_animation_) 135 if (ink_drop_animation_)
59 ink_drop_animation_->SetCenterPoint(ink_drop_center_); 136 ink_drop_animation_->SetCenterPoint(ink_drop_center_);
137 if (hover_)
138 hover_->SetCenterPoint(ink_drop_center_);
60 } 139 }
61 140
62 void InkDropAnimationControllerImpl::CreateInkDropAnimation() { 141 void InkDropAnimationControllerImpl::CreateInkDropAnimation() {
63 DestroyInkDropAnimation(); 142 DestroyInkDropAnimation();
64 143
65 ink_drop_animation_.reset(new InkDropAnimation( 144 ink_drop_animation_.reset(new InkDropAnimation(
66 ink_drop_large_size_, ink_drop_large_corner_radius_, ink_drop_small_size_, 145 ink_drop_large_size_, ink_drop_large_corner_radius_, ink_drop_small_size_,
67 ink_drop_small_corner_radius_)); 146 ink_drop_small_corner_radius_));
68 147
69 ink_drop_animation_->AddObserver(this); 148 ink_drop_animation_->AddObserver(this);
70 ink_drop_animation_->SetCenterPoint(ink_drop_center_); 149 ink_drop_animation_->SetCenterPoint(ink_drop_center_);
71 ink_drop_host_->AddInkDropLayer(ink_drop_animation_->root_layer()); 150 root_layer_->Add(ink_drop_animation_->root_layer());
72 } 151 }
73 152
74 void InkDropAnimationControllerImpl::DestroyInkDropAnimation() { 153 void InkDropAnimationControllerImpl::DestroyInkDropAnimation() {
75 if (!ink_drop_animation_) 154 if (!ink_drop_animation_)
76 return; 155 return;
77 ink_drop_host_->RemoveInkDropLayer(ink_drop_animation_->root_layer()); 156 root_layer_->Remove(ink_drop_animation_->root_layer());
78 ink_drop_animation_->RemoveObserver(this); 157 ink_drop_animation_->RemoveObserver(this);
79 ink_drop_animation_.reset(); 158 ink_drop_animation_.reset();
80 } 159 }
81 160
161 void InkDropAnimationControllerImpl::CreateInkDropHover() {
162 DestroyInkDropHover();
163
164 hover_.reset(
165 new InkDropHover(ink_drop_small_size_, ink_drop_small_corner_radius_));
166 hover_->SetCenterPoint(ink_drop_center_);
167 root_layer_->Add(hover_->layer());
168 }
169
170 void InkDropAnimationControllerImpl::DestroyInkDropHover() {
171 if (!hover_)
172 return;
173 root_layer_->Remove(hover_->layer());
174 hover_.reset();
175 }
176
82 void InkDropAnimationControllerImpl::InkDropAnimationStarted( 177 void InkDropAnimationControllerImpl::InkDropAnimationStarted(
83 InkDropState ink_drop_state) {} 178 InkDropState ink_drop_state) {
179 if (ink_drop_state != views::InkDropState::HIDDEN) {
180 SetHoveredInternal(false, base::TimeDelta::FromMilliseconds(
181 kHoverFadeOutBeforeAnimationDurationInMs));
182 }
183 }
84 184
85 void InkDropAnimationControllerImpl::InkDropAnimationEnded( 185 void InkDropAnimationControllerImpl::InkDropAnimationEnded(
86 InkDropState ink_drop_state, 186 InkDropState ink_drop_state,
87 InkDropAnimationEndedReason reason) { 187 InkDropAnimationEndedReason reason) {
88 if (reason != SUCCESS) 188 if (reason != SUCCESS)
89 return; 189 return;
90 switch (ink_drop_state) { 190 if (WillAutoAnimateToHidden())
91 case views::InkDropState::QUICK_ACTION: 191 ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN);
92 case views::InkDropState::SLOW_ACTION: 192 else if (ink_drop_state == views::InkDropState::HIDDEN) {
93 case views::InkDropState::DEACTIVATED: 193 StartHoverAfterAnimationTimer();
94 ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN); 194 if (can_destroy_after_hidden_animation_) {
95 break;
96 case views::InkDropState::HIDDEN:
97 // TODO(bruthig): Investigate whether creating and destroying 195 // TODO(bruthig): Investigate whether creating and destroying
98 // InkDropAnimations is expensive and consider creating an 196 // InkDropAnimations is expensive and consider creating an
99 // InkDropAnimationPool. See www.crbug.com/522175. 197 // InkDropAnimationPool. See www.crbug.com/522175.
100 DestroyInkDropAnimation(); 198 DestroyInkDropAnimation();
101 break; 199 }
102 default:
103 break;
104 } 200 }
105 } 201 }
106 202
203 void InkDropAnimationControllerImpl::SetHoveredInternal(
204 bool is_hovered,
205 base::TimeDelta animation_duration) {
206 StopHoverAfterAnimationTimer();
207
208 if (IsHovered() == is_hovered)
209 return;
210
211 if (is_hovered) {
212 if (!hover_)
213 CreateInkDropHover();
214 if (GetInkDropState() == views::InkDropState::HIDDEN) {
215 hover_->FadeIn(animation_duration);
216 }
217 } else {
218 hover_->FadeOut(animation_duration);
219 }
220 }
221
222 void InkDropAnimationControllerImpl::StartHoverAfterAnimationTimer() {
223 StopHoverAfterAnimationTimer();
224
225 hover_after_animation_timer_.reset(new base::OneShotTimer());
226 hover_after_animation_timer_->Start(
227 FROM_HERE,
228 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterAnimationDelayInMs),
229 this, &InkDropAnimationControllerImpl::HoverAfterAnimationTimerFired);
230 }
231
232 void InkDropAnimationControllerImpl::StopHoverAfterAnimationTimer() {
233 hover_after_animation_timer_.reset();
234 }
235
236 void InkDropAnimationControllerImpl::HoverAfterAnimationTimerFired() {
237 hover_after_animation_timer_.reset();
238 SetHoveredInternal(ink_drop_consumer_->ShouldShowInkDropHover(),
239 base::TimeDelta::FromMilliseconds(
240 kHoverFadeInAfterAnimationDurationInMs));
241 }
242
107 } // namespace views 243 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698