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

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

Issue 2447523002: [ash-md] Added different highlighting modes to the InkDropImpl. (Closed)
Patch Set: Fixed compile errors. Created 4 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_impl.h" 5 #include "ui/views/animation/ink_drop_impl.h"
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/timer/timer.h" 8 #include "base/timer/timer.h"
9 #include "ui/compositor/layer.h" 9 #include "ui/compositor/layer.h"
10 #include "ui/views/animation/ink_drop_highlight.h" 10 #include "ui/views/animation/ink_drop_highlight.h"
11 #include "ui/views/animation/ink_drop_host.h" 11 #include "ui/views/animation/ink_drop_host.h"
12 #include "ui/views/animation/square_ink_drop_ripple.h" 12 #include "ui/views/animation/square_ink_drop_ripple.h"
13 13
14 namespace views { 14 namespace views {
15 15
16 namespace { 16 namespace {
17 17
18 // The duration, in milliseconds, of the highlight state fade in animation when 18 // The duration, in milliseconds for the highlight state fade in/out animations
19 // it is triggered by user input. 19 // when it is triggered by a hover changed event.
20 const int kHighlightFadeInFromUserInputDurationMs = 250; 20 const int kHighlightFadeInOnHoverChangeDurationMs = 250;
21 const int kHighlightFadeOutOnHoverChangeDurationMs = 250;
21 22
22 // The duration, in milliseconds, of the highlight state fade out animation when 23 // The duration, in milliseconds for the highlight state fade in/out animations
23 // it is triggered by user input. 24 // when it is triggered by a focus changed event.
24 const int kHighlightFadeOutFromUserInputDurationMs = 250; 25 const int kHighlightFadeInOnFocusChangeDurationMs = 0;
26 const int kHighlightFadeOutOnFocusChangeDurationMs = 0;
25 27
26 // The duration, in milliseconds, of the highlight state fade in animation when 28 // The duration, in milliseconds, for showing/hiding the highlight when
27 // it is triggered by an ink drop ripple animation ending. 29 // triggered by ripple visibility changes for the HIDE_ON_RIPPLE
28 const int kHighlightFadeInAfterRippleDurationMs = 250; 30 // AutoHighlightMode.
31 const int kHighlightFadeInOnRippleHidingDurationMs = 250;
32 const int kHighlightFadeOutOnRippleShowingDurationMs = 120;
29 33
30 // The duration, in milliseconds, of the highlight state fade out animation when 34 // The duration, in milliseconds, for showing/hiding the highlight when
31 // it is triggered by an ink drop ripple animation starting. 35 // triggered by ripple visibility changes for the SHOW_ON_RIPPLE
32 const int kHighlightFadeOutBeforeRippleDurationMs = 120; 36 // AutoHighlightMode.
37 const int kHighlightFadeInOnRippleShowingDurationMs = 250;
38 const int kHighlightFadeOutOnRippleHidingDurationMs = 120;
33 39
34 // The amount of time in milliseconds that |highlight_| should delay after a 40 // The amount of time in milliseconds that |highlight_| should delay after a
35 // ripple animation before fading in, for highlight due to mouse hover. 41 // ripple animation before fading in, for highlight due to mouse hover.
36 const int kHoverFadeInAfterRippleDelayMs = 1000; 42 const int kHoverFadeInAfterRippleDelayMs = 1000;
37 43
38 // Returns true if an ink drop with the given |ink_drop_state| should 44 // Returns true if an ink drop with the given |ink_drop_state| should
39 // automatically transition to the InkDropState::HIDDEN state. 45 // automatically transition to the InkDropState::HIDDEN state.
40 bool ShouldAnimateToHidden(InkDropState ink_drop_state) { 46 bool ShouldAnimateToHidden(InkDropState ink_drop_state) {
41 switch (ink_drop_state) { 47 switch (ink_drop_state) {
42 case views::InkDropState::ACTION_TRIGGERED: 48 case views::InkDropState::ACTION_TRIGGERED:
43 case views::InkDropState::ALTERNATE_ACTION_TRIGGERED: 49 case views::InkDropState::ALTERNATE_ACTION_TRIGGERED:
44 case views::InkDropState::DEACTIVATED: 50 case views::InkDropState::DEACTIVATED:
45 return true; 51 return true;
46 default: 52 default:
47 return false; 53 return false;
48 } 54 }
49 } 55 }
50 56
51 } // namespace 57 } // namespace
52 58
59 // Creates the different HighlightStates instances.
60 class InkDropImpl::HighlightStateFactory {
61 public:
62 HighlightStateFactory(AutoHighlightMode highlight_mode,
63 InkDropImpl* ink_drop);
64
65 // Returns the initial state.
66 std::unique_ptr<InkDropImpl::HighlightState> CreateStartState();
67
68 std::unique_ptr<InkDropImpl::HighlightState> CreateHiddenState(
69 base::TimeDelta animation_duration,
70 bool explode);
71
72 std::unique_ptr<InkDropImpl::HighlightState> CreateVisibleState(
73 base::TimeDelta animation_duration,
74 bool explode);
75
76 InkDropImpl* ink_drop() { return ink_drop_; }
77
78 private:
79 // Defines which state machine flavors to use.
80 AutoHighlightMode highlight_mode_;
81
82 // The ink drop to invoke highlight changes on.
83 InkDropImpl* ink_drop_;
84
85 DISALLOW_COPY_AND_ASSIGN(HighlightStateFactory);
86 };
87
88 // Base HighlightState defining functions to handle all input that may affects
89 // the highlight state.
sky 2016/11/02 02:52:11 Please document what the expectation is for subcla
bruthig 2016/11/04 18:50:36 Done.
90 class InkDropImpl::HighlightState {
91 public:
92 virtual ~HighlightState() {}
93
94 // Called when |this| becomes the current state. Allows subclasses to perform
95 // any work that should not be done in the constructor.
96 virtual void Enter() {}
97
98 // Called just before |this| is removed as the current state. Allows
99 // subclasses to perform any work that should not be done in the destructor.
100 virtual void Exit() {}
101
102 // Called when the value of InkDropImpl::show_highlight_on_hover_ changes.
103 virtual void ShowOnHoverChanged() {}
sky 2016/11/02 02:52:11 optional: is there a reason you aren't making thes
bruthig 2016/11/04 18:50:37 Done.
104
105 // Called when the value of InkDropImpl::is_hovered_ changes.
106 virtual void OnHoverChanged() {}
107
108 // Called when the value of InkDropImpl::show_highlight_on_focus_ changes.
109 virtual void ShowOnFocusChanged() {}
110
111 // Called when the value of InkDropImpl::is_focused_ changes.
112 virtual void OnFocusChanged() {}
113
114 // Called when an ink drop ripple animation is started.
115 virtual void AnimationStarted(InkDropState ink_drop_state) {}
116
117 // Called when an ink drop ripple animation has ended.
118 virtual void AnimationEnded(InkDropState ink_drop_state,
119 InkDropAnimationEndedReason reason) {}
120
121 protected:
122 explicit HighlightState(HighlightStateFactory* state_factory)
123 : state_factory_(state_factory) {}
124
125 HighlightStateFactory* state_factory() { return state_factory_; }
126
127 // Returns the ink drop that has |this| as the current state.
128 InkDropImpl* GetInkDrop();
129
130 private:
131 // Used by |this| to create the new states to transition to.
132 HighlightStateFactory* state_factory_;
133
134 DISALLOW_COPY_AND_ASSIGN(HighlightState);
135 };
136
137 // HighlightState definition
138
139 InkDropImpl* InkDropImpl::HighlightState::GetInkDrop() {
140 return state_factory_->ink_drop();
141 }
142
143 //
144 // AutoHighlightMode::NONE states
145 //
146
147 // Animates the highlight to hidden upon entering this state. Transitions to a
148 // visible state based on hover/focus changes.
149 class InkDropImpl::NoAutoHighlightHiddenState
150 : public InkDropImpl::HighlightState {
151 public:
152 NoAutoHighlightHiddenState(HighlightStateFactory* state_factory,
153 base::TimeDelta animation_duration,
154 bool explode);
155
156 // InkDropImpl::HighlightState:
157 void Enter() override;
158 void ShowOnHoverChanged() override;
159 void OnHoverChanged() override;
160 void ShowOnFocusChanged() override;
161 void OnFocusChanged() override;
162
163 private:
164 // Handles all changes to the hover/focus status.
165 void HandleHoverAndFocusChangeChanges(int animation_duration_ms);
166
167 // The fade out animation duration.
168 base::TimeDelta animation_duration_;
169
170 // True when the highlight should explode while fading out.
171 bool explode_;
172
173 DISALLOW_COPY_AND_ASSIGN(NoAutoHighlightHiddenState);
174 };
175
176 // Animates the highlight to visible upon entering this state. Transitions to a
177 // hidden state based on hover/focus changes.
178 class InkDropImpl::NoAutoHighlightVisibleState
179 : public InkDropImpl::HighlightState {
180 public:
181 NoAutoHighlightVisibleState(HighlightStateFactory* state_factory,
182 base::TimeDelta animation_duration,
183 bool explode);
184
185 // InkDropImpl::HighlightState:
186 void Enter() override;
187 void ShowOnHoverChanged() override;
188 void OnHoverChanged() override;
189 void ShowOnFocusChanged() override;
190 void OnFocusChanged() override;
191
192 private:
193 // Handles all changes to the hover/focus status.
194 void HandleHoverAndFocusChangeChanges(int animation_duration_ms);
195
196 // The fade in animation duration.
197 base::TimeDelta animation_duration_;
198
199 // True when the highlight should explode while fading in.
200 bool explode_;
201
202 DISALLOW_COPY_AND_ASSIGN(NoAutoHighlightVisibleState);
203 };
204
205 // NoAutoHighlightHiddenState definition
206
207 InkDropImpl::NoAutoHighlightHiddenState::NoAutoHighlightHiddenState(
208 HighlightStateFactory* state_factory,
209 base::TimeDelta animation_duration,
210 bool explode)
211 : InkDropImpl::HighlightState(state_factory),
212 animation_duration_(animation_duration),
213 explode_(explode) {}
214
215 void InkDropImpl::NoAutoHighlightHiddenState::Enter() {
216 GetInkDrop()->SetHighlight(false, animation_duration_, explode_);
217 }
218
219 void InkDropImpl::NoAutoHighlightHiddenState::ShowOnHoverChanged() {
220 HandleHoverAndFocusChangeChanges(kHighlightFadeInOnHoverChangeDurationMs);
221 }
222
223 void InkDropImpl::NoAutoHighlightHiddenState::OnHoverChanged() {
224 HandleHoverAndFocusChangeChanges(kHighlightFadeInOnHoverChangeDurationMs);
225 }
226
227 void InkDropImpl::NoAutoHighlightHiddenState::ShowOnFocusChanged() {
228 HandleHoverAndFocusChangeChanges(kHighlightFadeInOnFocusChangeDurationMs);
229 }
230
231 void InkDropImpl::NoAutoHighlightHiddenState::OnFocusChanged() {
232 HandleHoverAndFocusChangeChanges(kHighlightFadeInOnFocusChangeDurationMs);
233 }
234
235 void InkDropImpl::NoAutoHighlightHiddenState::HandleHoverAndFocusChangeChanges(
236 int animation_duration_ms) {
237 if (GetInkDrop()->ShouldHighlight()) {
238 GetInkDrop()->SetHighlightState(state_factory()->CreateVisibleState(
239 base::TimeDelta::FromMilliseconds(animation_duration_ms), false));
240 }
241 }
242
243 // NoAutoHighlightVisibleState definition
244
245 InkDropImpl::NoAutoHighlightVisibleState::NoAutoHighlightVisibleState(
246 HighlightStateFactory* state_factory,
247 base::TimeDelta animation_duration,
248 bool explode)
249 : InkDropImpl::HighlightState(state_factory),
250 animation_duration_(animation_duration),
251 explode_(explode) {}
252
253 void InkDropImpl::NoAutoHighlightVisibleState::Enter() {
254 GetInkDrop()->SetHighlight(true, animation_duration_, explode_);
255 }
256
257 void InkDropImpl::NoAutoHighlightVisibleState::ShowOnHoverChanged() {
258 HandleHoverAndFocusChangeChanges(kHighlightFadeOutOnHoverChangeDurationMs);
259 }
260
261 void InkDropImpl::NoAutoHighlightVisibleState::OnHoverChanged() {
262 HandleHoverAndFocusChangeChanges(kHighlightFadeOutOnHoverChangeDurationMs);
263 }
264
265 void InkDropImpl::NoAutoHighlightVisibleState::ShowOnFocusChanged() {
266 HandleHoverAndFocusChangeChanges(kHighlightFadeOutOnFocusChangeDurationMs);
267 }
268
269 void InkDropImpl::NoAutoHighlightVisibleState::OnFocusChanged() {
270 HandleHoverAndFocusChangeChanges(kHighlightFadeOutOnFocusChangeDurationMs);
271 }
272
273 void InkDropImpl::NoAutoHighlightVisibleState::HandleHoverAndFocusChangeChanges(
274 int animation_duration_ms) {
275 if (!GetInkDrop()->ShouldHighlight()) {
276 GetInkDrop()->SetHighlightState(state_factory()->CreateHiddenState(
277 base::TimeDelta::FromMilliseconds(animation_duration_ms), false));
278 }
279 }
280
281 //
282 // AutoHighlightMode::HIDE_ON_RIPPLE states
283 //
284
285 // Extends the base hidden state to re-show the highlight after the ripple
286 // becomes hidden.
287 class InkDropImpl::HideHighlightOnRippleHiddenState
288 : public InkDropImpl::NoAutoHighlightHiddenState {
289 public:
290 HideHighlightOnRippleHiddenState(HighlightStateFactory* state_factory,
291 base::TimeDelta animation_duration,
292 bool explode);
293
294 // InkDropImpl::NoAutoHighlightHiddenState:
295 void ShowOnHoverChanged() override;
296 void OnHoverChanged() override;
297 void ShowOnFocusChanged() override;
298 void OnFocusChanged() override;
299 void AnimationStarted(InkDropState ink_drop_state) override;
300 void AnimationEnded(InkDropState ink_drop_state,
301 InkDropAnimationEndedReason reason) override;
302
303 private:
304 // Starts the |highlight_after_ripple_timer_|. This will stop the current
305 // |highlight_after_ripple_timer_| instance if it exists.
306 void StartHighlightAfterRippleTimer();
307
308 // Callback for when the |highlight_after_ripple_timer_| fires.
309 void HighlightAfterRippleTimerFired();
310
311 // The timer used to delay the highlight fade in after an ink drop ripple
312 // animation.
313 std::unique_ptr<base::Timer> highlight_after_ripple_timer_;
314
315 DISALLOW_COPY_AND_ASSIGN(HideHighlightOnRippleHiddenState);
316 };
317
318 // Extends the base visible state to hide the highlight when the ripple becomes
319 // visible.
320 class InkDropImpl::HideHighlightOnRippleVisibleState
321 : public InkDropImpl::NoAutoHighlightVisibleState {
322 public:
323 HideHighlightOnRippleVisibleState(HighlightStateFactory* state_factory,
324 base::TimeDelta animation_duration,
325 bool explode);
326
327 // InkDropImpl::NoAutoHighlightVisibleState:
328 void AnimationStarted(InkDropState ink_drop_state) override;
329
330 private:
331 DISALLOW_COPY_AND_ASSIGN(HideHighlightOnRippleVisibleState);
332 };
333
334 // HideHighlightOnRippleHiddenState definition
335
336 InkDropImpl::HideHighlightOnRippleHiddenState::HideHighlightOnRippleHiddenState(
337 HighlightStateFactory* state_factory,
338 base::TimeDelta animation_duration,
339 bool explode)
340 : InkDropImpl::NoAutoHighlightHiddenState(state_factory,
341 animation_duration,
342 explode),
343 highlight_after_ripple_timer_(nullptr) {
344 LOG(ERROR) << " *** " << __FUNCTION__ << "() line=" << __LINE__ << "";
345 }
346
347 void InkDropImpl::HideHighlightOnRippleHiddenState::ShowOnHoverChanged() {
348 if (GetInkDrop()->GetTargetInkDropState() != InkDropState::HIDDEN)
349 return;
350 NoAutoHighlightHiddenState::ShowOnHoverChanged();
351 }
352
353 void InkDropImpl::HideHighlightOnRippleHiddenState::OnHoverChanged() {
354 if (GetInkDrop()->GetTargetInkDropState() != InkDropState::HIDDEN)
355 return;
356 NoAutoHighlightHiddenState::OnHoverChanged();
357 }
358
359 void InkDropImpl::HideHighlightOnRippleHiddenState::ShowOnFocusChanged() {
360 if (GetInkDrop()->GetTargetInkDropState() != InkDropState::HIDDEN)
361 return;
362 NoAutoHighlightHiddenState::ShowOnFocusChanged();
363 }
364
365 void InkDropImpl::HideHighlightOnRippleHiddenState::OnFocusChanged() {
366 if (GetInkDrop()->GetTargetInkDropState() != InkDropState::HIDDEN)
367 return;
368 NoAutoHighlightHiddenState::OnFocusChanged();
369 }
370
371 void InkDropImpl::HideHighlightOnRippleHiddenState::AnimationStarted(
372 InkDropState ink_drop_state) {
373 if (ink_drop_state == views::InkDropState::DEACTIVATED &&
374 GetInkDrop()->is_focused_) {
375 GetInkDrop()->ink_drop_ripple_->HideImmediately();
376 GetInkDrop()->SetHighlightState(
377 state_factory()->CreateVisibleState(base::TimeDelta(), false));
378 }
379 }
380
381 void InkDropImpl::HideHighlightOnRippleHiddenState::AnimationEnded(
382 InkDropState ink_drop_state,
383 InkDropAnimationEndedReason reason) {
384 if (ink_drop_state == InkDropState::HIDDEN) {
385 // Re-highlight, as necessary. For hover, there's a delay; for focus, jump
386 // straight into the animation.
387 if (GetInkDrop()->ShouldHighlightBasedOnFocus()) {
388 GetInkDrop()->SetHighlightState(
389 state_factory()->CreateVisibleState(base::TimeDelta(), false));
390 return;
391 } else {
392 StartHighlightAfterRippleTimer();
393 }
394 }
395 }
396
397 void InkDropImpl::HideHighlightOnRippleHiddenState::
398 StartHighlightAfterRippleTimer() {
399 highlight_after_ripple_timer_.reset(new base::OneShotTimer);
400 highlight_after_ripple_timer_->Start(
401 FROM_HERE,
402 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterRippleDelayMs),
403 base::Bind(&InkDropImpl::HideHighlightOnRippleHiddenState::
404 HighlightAfterRippleTimerFired,
405 base::Unretained(this)));
406 }
407
408 void InkDropImpl::HideHighlightOnRippleHiddenState::
409 HighlightAfterRippleTimerFired() {
410 highlight_after_ripple_timer_.reset();
411 if (GetInkDrop()->GetTargetInkDropState() == InkDropState::HIDDEN &&
412 GetInkDrop()->ShouldHighlight()) {
413 GetInkDrop()->SetHighlightState(state_factory()->CreateVisibleState(
414 base::TimeDelta::FromMilliseconds(
415 kHighlightFadeInOnRippleHidingDurationMs),
416 true));
417 }
418 }
419
420 // HideHighlightOnRippleVisibleState definition
421
422 InkDropImpl::HideHighlightOnRippleVisibleState::
423 HideHighlightOnRippleVisibleState(HighlightStateFactory* state_factory,
424 base::TimeDelta animation_duration,
425 bool explode)
426 : InkDropImpl::NoAutoHighlightVisibleState(state_factory,
427 animation_duration,
428 explode) {
429 LOG(ERROR) << " *** " << __FUNCTION__ << "() line=" << __LINE__ << "";
430 }
431
432 void InkDropImpl::HideHighlightOnRippleVisibleState::AnimationStarted(
433 InkDropState ink_drop_state) {
434 if (ink_drop_state != InkDropState::HIDDEN) {
435 GetInkDrop()->SetHighlightState(state_factory()->CreateHiddenState(
436 base::TimeDelta::FromMilliseconds(
437 kHighlightFadeOutOnRippleShowingDurationMs),
438 true));
439 }
440 }
441
442 //
443 // AutoHighlightMode::SHOW_ON_RIPPLE states
444 //
445
446 // Extends the base hidden state to show the highlight when the ripple becomes
447 // visible.
448 class InkDropImpl::ShowHighlightOnRippleHiddenState
449 : public InkDropImpl::NoAutoHighlightHiddenState {
450 public:
451 ShowHighlightOnRippleHiddenState(HighlightStateFactory* state_factory,
452 base::TimeDelta animation_duration,
453 bool explode);
454
455 // InkDropImpl::NoAutoHighlightHiddenState:
456 void AnimationStarted(InkDropState ink_drop_state) override;
457
458 private:
459 DISALLOW_COPY_AND_ASSIGN(ShowHighlightOnRippleHiddenState);
460 };
461
462 // Extends the base visible state to hide the highlight when the ripple becomes
463 // hidden.
464 class InkDropImpl::ShowHighlightOnRippleVisibleState
465 : public InkDropImpl::NoAutoHighlightVisibleState {
466 public:
467 ShowHighlightOnRippleVisibleState(HighlightStateFactory* state_factory,
468 base::TimeDelta animation_duration,
469 bool explode);
470
471 // InkDropImpl::NoAutoHighlightVisibleState:
472 void ShowOnHoverChanged() override;
473 void OnHoverChanged() override;
474 void ShowOnFocusChanged() override;
475 void OnFocusChanged() override;
476 void AnimationStarted(InkDropState ink_drop_state) override;
477
478 private:
479 DISALLOW_COPY_AND_ASSIGN(ShowHighlightOnRippleVisibleState);
480 };
481
482 // ShowHighlightOnRippleHiddenState definition
483
484 InkDropImpl::ShowHighlightOnRippleHiddenState::ShowHighlightOnRippleHiddenState(
485 HighlightStateFactory* state_factory,
486 base::TimeDelta animation_duration,
487 bool explode)
488 : InkDropImpl::NoAutoHighlightHiddenState(state_factory,
489 animation_duration,
490 explode) {
491 LOG(ERROR) << " *** " << __FUNCTION__ << "() line=" << __LINE__ << "";
492 }
493
494 void InkDropImpl::ShowHighlightOnRippleHiddenState::AnimationStarted(
495 InkDropState ink_drop_state) {
496 if (ink_drop_state != views::InkDropState::HIDDEN) {
497 GetInkDrop()->SetHighlightState(state_factory()->CreateVisibleState(
498 base::TimeDelta::FromMilliseconds(
499 kHighlightFadeInOnRippleShowingDurationMs),
500 false));
501 }
502 }
503
504 // ShowHighlightOnRippleVisibleState definition
505
506 InkDropImpl::ShowHighlightOnRippleVisibleState::
507 ShowHighlightOnRippleVisibleState(HighlightStateFactory* state_factory,
508 base::TimeDelta animation_duration,
509 bool explode)
510 : InkDropImpl::NoAutoHighlightVisibleState(state_factory,
511 animation_duration,
512 explode) {
513 LOG(ERROR) << " *** " << __FUNCTION__ << "() line=" << __LINE__ << "";
514 }
515
516 void InkDropImpl::ShowHighlightOnRippleVisibleState::ShowOnHoverChanged() {
517 if (GetInkDrop()->GetTargetInkDropState() != InkDropState::HIDDEN)
518 return;
519 NoAutoHighlightVisibleState::ShowOnHoverChanged();
520 }
521
522 void InkDropImpl::ShowHighlightOnRippleVisibleState::OnHoverChanged() {
523 if (GetInkDrop()->GetTargetInkDropState() != InkDropState::HIDDEN)
524 return;
525 NoAutoHighlightVisibleState::OnHoverChanged();
526 }
527
528 void InkDropImpl::ShowHighlightOnRippleVisibleState::ShowOnFocusChanged() {
529 if (GetInkDrop()->GetTargetInkDropState() != InkDropState::HIDDEN)
530 return;
531 NoAutoHighlightVisibleState::ShowOnFocusChanged();
532 }
533
534 void InkDropImpl::ShowHighlightOnRippleVisibleState::OnFocusChanged() {
535 if (GetInkDrop()->GetTargetInkDropState() != InkDropState::HIDDEN)
536 return;
537 NoAutoHighlightVisibleState::OnFocusChanged();
538 }
539
540 void InkDropImpl::ShowHighlightOnRippleVisibleState::AnimationStarted(
541 InkDropState ink_drop_state) {
542 if (ink_drop_state == InkDropState::HIDDEN &&
543 !GetInkDrop()->ShouldHighlight()) {
544 GetInkDrop()->SetHighlightState(state_factory()->CreateHiddenState(
545 base::TimeDelta::FromMilliseconds(
546 kHighlightFadeOutOnRippleHidingDurationMs),
547 false));
548 }
549 }
550
551 InkDropImpl::HighlightStateFactory::HighlightStateFactory(
552 InkDropImpl::AutoHighlightMode highlight_mode,
553 InkDropImpl* ink_drop)
554 : highlight_mode_(highlight_mode), ink_drop_(ink_drop) {}
555
556 std::unique_ptr<InkDropImpl::HighlightState>
557 InkDropImpl::HighlightStateFactory::CreateStartState() {
558 switch (highlight_mode_) {
559 case InkDropImpl::AutoHighlightMode::NONE:
560 return base::MakeUnique<NoAutoHighlightHiddenState>(
561 this, base::TimeDelta(), false);
562 case InkDropImpl::AutoHighlightMode::HIDE_ON_RIPPLE:
563 return base::MakeUnique<HideHighlightOnRippleHiddenState>(
564 this, base::TimeDelta(), false);
565 case InkDropImpl::AutoHighlightMode::SHOW_ON_RIPPLE:
566 return base::MakeUnique<ShowHighlightOnRippleHiddenState>(
567 this, base::TimeDelta(), false);
568 }
569 // Required for some compilers.
570 NOTREACHED();
571 return nullptr;
572 }
573
574 std::unique_ptr<InkDropImpl::HighlightState>
575 InkDropImpl::HighlightStateFactory::CreateHiddenState(
576 base::TimeDelta animation_duration,
577 bool explode) {
578 switch (highlight_mode_) {
579 case InkDropImpl::AutoHighlightMode::NONE:
580 return base::MakeUnique<NoAutoHighlightHiddenState>(
581 this, animation_duration, explode);
582 case InkDropImpl::AutoHighlightMode::HIDE_ON_RIPPLE:
583 return base::MakeUnique<HideHighlightOnRippleHiddenState>(
584 this, animation_duration, explode);
585 case InkDropImpl::AutoHighlightMode::SHOW_ON_RIPPLE:
586 return base::MakeUnique<ShowHighlightOnRippleHiddenState>(
587 this, animation_duration, explode);
588 }
589 // Required for some compilers.
590 NOTREACHED();
591 return nullptr;
592 }
593
594 std::unique_ptr<InkDropImpl::HighlightState>
595 InkDropImpl::HighlightStateFactory::CreateVisibleState(
596 base::TimeDelta animation_duration,
597 bool explode) {
598 switch (highlight_mode_) {
599 case InkDropImpl::AutoHighlightMode::NONE:
600 return base::MakeUnique<NoAutoHighlightVisibleState>(
601 this, animation_duration, explode);
602 case InkDropImpl::AutoHighlightMode::HIDE_ON_RIPPLE:
603 return base::MakeUnique<HideHighlightOnRippleVisibleState>(
604 this, animation_duration, explode);
605 case InkDropImpl::AutoHighlightMode::SHOW_ON_RIPPLE:
606 return base::MakeUnique<ShowHighlightOnRippleVisibleState>(
607 this, animation_duration, explode);
608 }
609 // Required for some compilers.
610 NOTREACHED();
611 return nullptr;
612 }
613
53 InkDropImpl::InkDropImpl(InkDropHost* ink_drop_host) 614 InkDropImpl::InkDropImpl(InkDropHost* ink_drop_host)
54 : ink_drop_host_(ink_drop_host), 615 : ink_drop_host_(ink_drop_host),
55 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), 616 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)),
56 root_layer_added_to_host_(false), 617 root_layer_added_to_host_(false),
618 show_highlight_on_hover_(true),
619 show_highlight_on_focus_(false),
57 is_hovered_(false), 620 is_hovered_(false),
58 is_focused_(false), 621 is_focused_(false) {
59 highlight_after_ripple_timer_(nullptr) { 622 SetAutoHighlightMode(AutoHighlightMode::NONE);
60 root_layer_->set_name("InkDropImpl:RootLayer"); 623 root_layer_->set_name("InkDropImpl:RootLayer");
61 } 624 }
62 625
63 InkDropImpl::~InkDropImpl() { 626 InkDropImpl::~InkDropImpl() {
64 // Explicitly destroy the InkDropRipple so that this still exists if 627 // Explicitly destroy the InkDropRipple so that this still exists if
65 // views::InkDropRippleObserver methods are called on this. 628 // views::InkDropRippleObserver methods are called on this.
66 DestroyInkDropRipple(); 629 DestroyInkDropRipple();
67 DestroyInkDropHighlight(); 630 DestroyInkDropHighlight();
68 } 631 }
69 632
633 void InkDropImpl::SetShowHighlightOnHover(bool show_highlight_on_hover) {
634 show_highlight_on_hover_ = show_highlight_on_hover;
635 highlight_state_->ShowOnHoverChanged();
636 }
637
638 void InkDropImpl::SetShowHighlightOnFocus(bool show_highlight_on_focus) {
639 show_highlight_on_focus_ = show_highlight_on_focus;
640 highlight_state_->ShowOnFocusChanged();
641 }
642
643 void InkDropImpl::SetAutoHighlightMode(AutoHighlightMode auto_highlight_mode) {
644 highlight_state_factory_ =
645 base::MakeUnique<HighlightStateFactory>(auto_highlight_mode, this);
646 SetHighlightState(highlight_state_factory_->CreateStartState());
647 }
648
70 InkDropState InkDropImpl::GetTargetInkDropState() const { 649 InkDropState InkDropImpl::GetTargetInkDropState() const {
71 if (!ink_drop_ripple_) 650 if (!ink_drop_ripple_)
72 return InkDropState::HIDDEN; 651 return InkDropState::HIDDEN;
73 return ink_drop_ripple_->target_ink_drop_state(); 652 return ink_drop_ripple_->target_ink_drop_state();
74 } 653 }
75 654
76 void InkDropImpl::AnimateToState(InkDropState ink_drop_state) { 655 void InkDropImpl::AnimateToState(InkDropState ink_drop_state) {
77 // Never animate hidden -> hidden, since that will add layers which may never 656 // Never animate hidden -> hidden, since that will add layers which may never
78 // be needed. Other same-state transitions may restart animations. 657 // be needed. Other same-state transitions may restart animations.
79 if (ink_drop_state == InkDropState::HIDDEN && 658 if (ink_drop_state == InkDropState::HIDDEN &&
80 GetTargetInkDropState() == InkDropState::HIDDEN) 659 GetTargetInkDropState() == InkDropState::HIDDEN)
81 return; 660 return;
82 661
83 DestroyHiddenTargetedAnimations(); 662 DestroyHiddenTargetedAnimations();
84 if (!ink_drop_ripple_) 663 if (!ink_drop_ripple_)
85 CreateInkDropRipple(); 664 CreateInkDropRipple();
86
87 if (ink_drop_ripple_->OverridesHighlight()) {
88 // When deactivating and the host is focused, snap back to the highlight
89 // state. (In the case of highlighting due to hover, we'll animate the
90 // highlight back in after a delay.)
91 if (ink_drop_state == views::InkDropState::DEACTIVATED && is_focused_) {
92 ink_drop_ripple_->HideImmediately();
93 SetHighlight(true, base::TimeDelta(), false);
94 return;
95 }
96
97 if (ink_drop_state != views::InkDropState::HIDDEN) {
98 SetHighlight(false, base::TimeDelta::FromMilliseconds(
99 kHighlightFadeOutBeforeRippleDurationMs),
100 true);
101 }
102 }
103
104 ink_drop_ripple_->AnimateToState(ink_drop_state); 665 ink_drop_ripple_->AnimateToState(ink_drop_state);
105 } 666 }
106 667
107 void InkDropImpl::SnapToActivated() { 668 void InkDropImpl::SnapToActivated() {
108 DestroyHiddenTargetedAnimations(); 669 DestroyHiddenTargetedAnimations();
109 if (!ink_drop_ripple_) 670 if (!ink_drop_ripple_)
110 CreateInkDropRipple(); 671 CreateInkDropRipple();
111
112 if (ink_drop_ripple_->OverridesHighlight())
113 SetHighlight(false, base::TimeDelta(), false);
114
115 ink_drop_ripple_->SnapToActivated(); 672 ink_drop_ripple_->SnapToActivated();
116 } 673 }
117 674
118 void InkDropImpl::SetHovered(bool is_hovered) { 675 void InkDropImpl::SetHovered(bool is_hovered) {
119 is_hovered_ = is_hovered; 676 is_hovered_ = is_hovered;
120 SetHighlight(ShouldHighlight(), 677 highlight_state_->OnHoverChanged();
121 ShouldHighlight()
122 ? base::TimeDelta::FromMilliseconds(
123 kHighlightFadeInFromUserInputDurationMs)
124 : base::TimeDelta::FromMilliseconds(
125 kHighlightFadeOutFromUserInputDurationMs),
126 false);
127 } 678 }
128 679
129 void InkDropImpl::SetFocused(bool is_focused) { 680 void InkDropImpl::SetFocused(bool is_focused) {
130 is_focused_ = is_focused; 681 is_focused_ = is_focused;
131 SetHighlight(ShouldHighlight(), base::TimeDelta(), false); 682 highlight_state_->OnFocusChanged();
132 } 683 }
133 684
134 void InkDropImpl::DestroyHiddenTargetedAnimations() { 685 void InkDropImpl::DestroyHiddenTargetedAnimations() {
135 if (ink_drop_ripple_ && 686 if (ink_drop_ripple_ &&
136 (ink_drop_ripple_->target_ink_drop_state() == InkDropState::HIDDEN || 687 (ink_drop_ripple_->target_ink_drop_state() == InkDropState::HIDDEN ||
137 ShouldAnimateToHidden(ink_drop_ripple_->target_ink_drop_state()))) { 688 ShouldAnimateToHidden(ink_drop_ripple_->target_ink_drop_state()))) {
138 DestroyInkDropRipple(); 689 DestroyInkDropRipple();
139 } 690 }
140 } 691 }
141 692
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 } 742 }
192 } 743 }
193 744
194 bool InkDropImpl::IsHighlightFadingInOrVisible() const { 745 bool InkDropImpl::IsHighlightFadingInOrVisible() const {
195 return highlight_ && highlight_->IsFadingInOrVisible(); 746 return highlight_ && highlight_->IsFadingInOrVisible();
196 } 747 }
197 748
198 // ----------------------------------------------------------------------------- 749 // -----------------------------------------------------------------------------
199 // views::InkDropRippleObserver: 750 // views::InkDropRippleObserver:
200 751
201 void InkDropImpl::AnimationStarted(InkDropState ink_drop_state) {} 752 void InkDropImpl::AnimationStarted(InkDropState ink_drop_state) {
753 highlight_state_->AnimationStarted(ink_drop_state);
754 }
202 755
203 void InkDropImpl::AnimationEnded(InkDropState ink_drop_state, 756 void InkDropImpl::AnimationEnded(InkDropState ink_drop_state,
204 InkDropAnimationEndedReason reason) { 757 InkDropAnimationEndedReason reason) {
758 highlight_state_->AnimationEnded(ink_drop_state, reason);
205 if (reason != InkDropAnimationEndedReason::SUCCESS) 759 if (reason != InkDropAnimationEndedReason::SUCCESS)
206 return; 760 return;
207 if (ShouldAnimateToHidden(ink_drop_state)) { 761 if (ShouldAnimateToHidden(ink_drop_state)) {
208 ink_drop_ripple_->AnimateToState(views::InkDropState::HIDDEN); 762 ink_drop_ripple_->AnimateToState(views::InkDropState::HIDDEN);
209 } else if (ink_drop_state == views::InkDropState::HIDDEN) { 763 } else if (ink_drop_state == views::InkDropState::HIDDEN) {
210 // Re-highlight, as necessary. For hover, there's a delay; for focus, jump
211 // straight into the animation.
212 if (!IsHighlightFadingInOrVisible()) {
213 if (is_focused_)
214 HighlightAfterRippleTimerFired();
215 else if (is_hovered_)
216 StartHighlightAfterRippleTimer();
217 }
218
219 // TODO(bruthig): Investigate whether creating and destroying 764 // TODO(bruthig): Investigate whether creating and destroying
220 // InkDropRipples is expensive and consider creating an 765 // InkDropRipples is expensive and consider creating an
221 // InkDropRipplePool. See www.crbug.com/522175. 766 // InkDropRipplePool. See www.crbug.com/522175.
222 DestroyInkDropRipple(); 767 DestroyInkDropRipple();
223 } 768 }
224 } 769 }
225 770
226 // ----------------------------------------------------------------------------- 771 // -----------------------------------------------------------------------------
227 // views::InkDropHighlightObserver: 772 // views::InkDropHighlightObserver:
228 773
229 void InkDropImpl::AnimationStarted( 774 void InkDropImpl::AnimationStarted(
230 InkDropHighlight::AnimationType animation_type) {} 775 InkDropHighlight::AnimationType animation_type) {}
231 776
232 void InkDropImpl::AnimationEnded(InkDropHighlight::AnimationType animation_type, 777 void InkDropImpl::AnimationEnded(InkDropHighlight::AnimationType animation_type,
233 InkDropAnimationEndedReason reason) { 778 InkDropAnimationEndedReason reason) {
234 if (animation_type == InkDropHighlight::FADE_OUT && 779 if (animation_type == InkDropHighlight::FADE_OUT &&
235 reason == InkDropAnimationEndedReason::SUCCESS) { 780 reason == InkDropAnimationEndedReason::SUCCESS) {
236 DestroyInkDropHighlight(); 781 DestroyInkDropHighlight();
237 } 782 }
238 } 783 }
239 784
240 void InkDropImpl::SetHighlight(bool should_highlight, 785 void InkDropImpl::SetHighlight(bool should_highlight,
241 base::TimeDelta animation_duration, 786 base::TimeDelta animation_duration,
242 bool explode) { 787 bool explode) {
243 highlight_after_ripple_timer_.reset();
244
245 if (IsHighlightFadingInOrVisible() == should_highlight) 788 if (IsHighlightFadingInOrVisible() == should_highlight)
246 return; 789 return;
247 790
248 if (should_highlight) { 791 if (should_highlight) {
249 CreateInkDropHighlight(); 792 CreateInkDropHighlight();
250 if (highlight_ && 793 if (highlight_)
bruthig 2016/11/01 21:00:10 Add TODO to remove this check since all CreateInkD
bruthig 2016/11/04 18:50:36 Done.
251 !(ink_drop_ripple_ && ink_drop_ripple_->IsVisible() &&
252 ink_drop_ripple_->OverridesHighlight())) {
253 highlight_->FadeIn(animation_duration); 794 highlight_->FadeIn(animation_duration);
254 }
255 } else { 795 } else {
256 highlight_->FadeOut(animation_duration, explode); 796 highlight_->FadeOut(animation_duration, explode);
257 } 797 }
258 } 798 }
259 799
260 bool InkDropImpl::ShouldHighlight() const { 800 bool InkDropImpl::ShouldHighlight() const {
261 return is_focused_ || is_hovered_; 801 return ShouldHighlightBasedOnFocus() ||
802 (show_highlight_on_hover_ && is_hovered_);
262 } 803 }
263 804
264 void InkDropImpl::StartHighlightAfterRippleTimer() { 805 bool InkDropImpl::ShouldHighlightBasedOnFocus() const {
265 highlight_after_ripple_timer_.reset(new base::OneShotTimer); 806 return show_highlight_on_focus_ && is_focused_;
266 highlight_after_ripple_timer_->Start(
267 FROM_HERE,
268 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterRippleDelayMs),
269 base::Bind(&InkDropImpl::HighlightAfterRippleTimerFired,
270 base::Unretained(this)));
271 } 807 }
272 808
273 void InkDropImpl::HighlightAfterRippleTimerFired() { 809 void InkDropImpl::SetHighlightState(
274 SetHighlight(true, base::TimeDelta::FromMilliseconds( 810 std::unique_ptr<HighlightState> highlight_state) {
275 kHighlightFadeInAfterRippleDurationMs), 811 if (highlight_state_)
sky 2016/11/02 02:52:11 I do like the new code. It's easier to understand.
bruthig 2016/11/04 18:50:36 Yaay :)
276 true); 812 highlight_state_->Exit();
277 highlight_after_ripple_timer_.reset(); 813 highlight_state_ = std::move(highlight_state);
814 highlight_state_->Enter();
278 } 815 }
279 816
280 } // namespace views 817 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698