| 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.h" | 5 #include "ui/views/animation/ink_drop_ripple.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "ui/base/ui_base_switches.h" | 10 #include "ui/base/ui_base_switches.h" |
| 11 #include "ui/compositor/callback_layer_animation_observer.h" | 11 #include "ui/compositor/callback_layer_animation_observer.h" |
| 12 #include "ui/compositor/layer.h" | 12 #include "ui/compositor/layer.h" |
| 13 | 13 |
| 14 namespace views { | 14 namespace views { |
| 15 | 15 |
| 16 const double InkDropAnimation::kSlowAnimationDurationFactor = 3.0; | 16 const double InkDropRipple::kSlowAnimationDurationFactor = 3.0; |
| 17 | 17 |
| 18 bool InkDropAnimation::UseFastAnimations() { | 18 bool InkDropRipple::UseFastAnimations() { |
| 19 static bool fast = | 19 static bool fast = |
| 20 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 20 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 21 (::switches::kMaterialDesignInkDropAnimationSpeed)) != | 21 (::switches::kMaterialDesignInkDropAnimationSpeed)) != |
| 22 ::switches::kMaterialDesignInkDropAnimationSpeedSlow; | 22 ::switches::kMaterialDesignInkDropAnimationSpeedSlow; |
| 23 return fast; | 23 return fast; |
| 24 } | 24 } |
| 25 | 25 |
| 26 const float InkDropAnimation::kHiddenOpacity = 0.f; | 26 const float InkDropRipple::kHiddenOpacity = 0.f; |
| 27 const float InkDropAnimation::kVisibleOpacity = 0.175f; | 27 const float InkDropRipple::kVisibleOpacity = 0.175f; |
| 28 | 28 |
| 29 InkDropAnimation::InkDropAnimation() | 29 InkDropRipple::InkDropRipple() |
| 30 : target_ink_drop_state_(InkDropState::HIDDEN), observer_(nullptr) {} | 30 : target_ink_drop_state_(InkDropState::HIDDEN), observer_(nullptr) {} |
| 31 | 31 |
| 32 InkDropAnimation::~InkDropAnimation() {} | 32 InkDropRipple::~InkDropRipple() {} |
| 33 | 33 |
| 34 void InkDropAnimation::AnimateToState(InkDropState ink_drop_state) { | 34 void InkDropRipple::AnimateToState(InkDropState ink_drop_state) { |
| 35 // Does not return early if |target_ink_drop_state_| == |ink_drop_state| for | 35 // Does not return early if |target_ink_drop_state_| == |ink_drop_state| for |
| 36 // two reasons. | 36 // two reasons. |
| 37 // 1. The attached observers must be notified of all animations started and | 37 // 1. The attached observers must be notified of all animations started and |
| 38 // ended. | 38 // ended. |
| 39 // 2. Not all state transitions is are valid, especially no-op transitions, | 39 // 2. Not all state transitions is are valid, especially no-op transitions, |
| 40 // and these should be detected by DCHECKs in AnimateStateChange(). | 40 // and these should be detected by DCHECKs in AnimateStateChange(). |
| 41 | 41 |
| 42 // |animation_observer| will be deleted when AnimationEndedCallback() returns | 42 // |animation_observer| will be deleted when AnimationEndedCallback() returns |
| 43 // true. | 43 // true. |
| 44 // TODO(bruthig): Implement a safer ownership model for the | 44 // TODO(bruthig): Implement a safer ownership model for the |
| 45 // |animation_observer|. | 45 // |animation_observer|. |
| 46 ui::CallbackLayerAnimationObserver* animation_observer = | 46 ui::CallbackLayerAnimationObserver* animation_observer = |
| 47 new ui::CallbackLayerAnimationObserver( | 47 new ui::CallbackLayerAnimationObserver( |
| 48 base::Bind(&InkDropAnimation::AnimationStartedCallback, | 48 base::Bind(&InkDropRipple::AnimationStartedCallback, |
| 49 base::Unretained(this), ink_drop_state), | 49 base::Unretained(this), ink_drop_state), |
| 50 base::Bind(&InkDropAnimation::AnimationEndedCallback, | 50 base::Bind(&InkDropRipple::AnimationEndedCallback, |
| 51 base::Unretained(this), ink_drop_state)); | 51 base::Unretained(this), ink_drop_state)); |
| 52 | 52 |
| 53 InkDropState old_ink_drop_state = target_ink_drop_state_; | 53 InkDropState old_ink_drop_state = target_ink_drop_state_; |
| 54 // Assign to |target_ink_drop_state_| before calling AnimateStateChange() so | 54 // Assign to |target_ink_drop_state_| before calling AnimateStateChange() so |
| 55 // that any observers notified as a side effect of the AnimateStateChange() | 55 // that any observers notified as a side effect of the AnimateStateChange() |
| 56 // will get the target InkDropState when calling GetInkDropState(). | 56 // will get the target InkDropState when calling GetInkDropState(). |
| 57 target_ink_drop_state_ = ink_drop_state; | 57 target_ink_drop_state_ = ink_drop_state; |
| 58 | 58 |
| 59 if (old_ink_drop_state == InkDropState::HIDDEN && | 59 if (old_ink_drop_state == InkDropState::HIDDEN && |
| 60 target_ink_drop_state_ != InkDropState::HIDDEN) { | 60 target_ink_drop_state_ != InkDropState::HIDDEN) { |
| 61 GetRootLayer()->SetVisible(true); | 61 GetRootLayer()->SetVisible(true); |
| 62 } | 62 } |
| 63 | 63 |
| 64 AnimateStateChange(old_ink_drop_state, target_ink_drop_state_, | 64 AnimateStateChange(old_ink_drop_state, target_ink_drop_state_, |
| 65 animation_observer); | 65 animation_observer); |
| 66 animation_observer->SetActive(); | 66 animation_observer->SetActive(); |
| 67 // |this| may be deleted! |animation_observer| might synchronously call | 67 // |this| may be deleted! |animation_observer| might synchronously call |
| 68 // AnimationEndedCallback which can delete |this|. | 68 // AnimationEndedCallback which can delete |this|. |
| 69 } | 69 } |
| 70 | 70 |
| 71 void InkDropAnimation::SnapToActivated() { | 71 void InkDropRipple::SnapToActivated() { |
| 72 AbortAllAnimations(); | 72 AbortAllAnimations(); |
| 73 // |animation_observer| will be deleted when AnimationEndedCallback() returns | 73 // |animation_observer| will be deleted when AnimationEndedCallback() returns |
| 74 // true. | 74 // true. |
| 75 // TODO(bruthig): Implement a safer ownership model for the | 75 // TODO(bruthig): Implement a safer ownership model for the |
| 76 // |animation_observer|. | 76 // |animation_observer|. |
| 77 ui::CallbackLayerAnimationObserver* animation_observer = | 77 ui::CallbackLayerAnimationObserver* animation_observer = |
| 78 new ui::CallbackLayerAnimationObserver( | 78 new ui::CallbackLayerAnimationObserver( |
| 79 base::Bind(&InkDropAnimation::AnimationStartedCallback, | 79 base::Bind(&InkDropRipple::AnimationStartedCallback, |
| 80 base::Unretained(this), InkDropState::ACTIVATED), | 80 base::Unretained(this), InkDropState::ACTIVATED), |
| 81 base::Bind(&InkDropAnimation::AnimationEndedCallback, | 81 base::Bind(&InkDropRipple::AnimationEndedCallback, |
| 82 base::Unretained(this), InkDropState::ACTIVATED)); | 82 base::Unretained(this), InkDropState::ACTIVATED)); |
| 83 GetRootLayer()->SetVisible(true); | 83 GetRootLayer()->SetVisible(true); |
| 84 target_ink_drop_state_ = InkDropState::ACTIVATED; | 84 target_ink_drop_state_ = InkDropState::ACTIVATED; |
| 85 animation_observer->SetActive(); | 85 animation_observer->SetActive(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void InkDropAnimation::HideImmediately() { | 88 void InkDropRipple::HideImmediately() { |
| 89 AbortAllAnimations(); | 89 AbortAllAnimations(); |
| 90 SetStateToHidden(); | 90 SetStateToHidden(); |
| 91 target_ink_drop_state_ = InkDropState::HIDDEN; | 91 target_ink_drop_state_ = InkDropState::HIDDEN; |
| 92 } | 92 } |
| 93 | 93 |
| 94 test::InkDropAnimationTestApi* InkDropAnimation::GetTestApi() { | 94 test::InkDropRippleTestApi* InkDropRipple::GetTestApi() { |
| 95 return nullptr; | 95 return nullptr; |
| 96 } | 96 } |
| 97 | 97 |
| 98 void InkDropAnimation::AnimationStartedCallback( | 98 void InkDropRipple::AnimationStartedCallback( |
| 99 InkDropState ink_drop_state, | 99 InkDropState ink_drop_state, |
| 100 const ui::CallbackLayerAnimationObserver& observer) { | 100 const ui::CallbackLayerAnimationObserver& observer) { |
| 101 observer_->AnimationStarted(ink_drop_state); | 101 observer_->AnimationStarted(ink_drop_state); |
| 102 } | 102 } |
| 103 | 103 |
| 104 bool InkDropAnimation::AnimationEndedCallback( | 104 bool InkDropRipple::AnimationEndedCallback( |
| 105 InkDropState ink_drop_state, | 105 InkDropState ink_drop_state, |
| 106 const ui::CallbackLayerAnimationObserver& observer) { | 106 const ui::CallbackLayerAnimationObserver& observer) { |
| 107 if (ink_drop_state == InkDropState::HIDDEN) | 107 if (ink_drop_state == InkDropState::HIDDEN) |
| 108 SetStateToHidden(); | 108 SetStateToHidden(); |
| 109 observer_->AnimationEnded(ink_drop_state, | 109 observer_->AnimationEnded(ink_drop_state, |
| 110 observer.aborted_count() | 110 observer.aborted_count() |
| 111 ? InkDropAnimationEndedReason::PRE_EMPTED | 111 ? InkDropAnimationEndedReason::PRE_EMPTED |
| 112 : InkDropAnimationEndedReason::SUCCESS); | 112 : InkDropAnimationEndedReason::SUCCESS); |
| 113 // |this| may be deleted! | 113 // |this| may be deleted! |
| 114 return true; | 114 return true; |
| 115 } | 115 } |
| 116 | 116 |
| 117 } // namespace views | 117 } // namespace views |
| OLD | NEW |