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