ink_drop_ripple.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include "ui/views/animation/ink_drop_ripple.h"
  5. #include "base/bind.h"
  6. #include "base/callback_helpers.h"
  7. #include "base/command_line.h"
  8. #include "ui/base/ui_base_switches.h"
  9. #include "ui/compositor/callback_layer_animation_observer.h"
  10. #include "ui/compositor/layer.h"
  11. namespace views {
  12. const float InkDropRipple::kHiddenOpacity = 0.f;
  13. InkDropRipple::InkDropRipple() = default;
  14. InkDropRipple::~InkDropRipple() = default;
  15. void InkDropRipple::AnimateToState(InkDropState ink_drop_state) {
  16. // Does not return early if |target_ink_drop_state_| == |ink_drop_state| for
  17. // two reasons.
  18. // 1. The attached observers must be notified of all animations started and
  19. // ended.
  20. // 2. Not all state transitions is are valid, especially no-op transitions,
  21. // and these invalid transitions will be logged as warnings in
  22. // AnimateStateChange().
  23. animation_observer_ = CreateAnimationObserver(ink_drop_state);
  24. InkDropState old_ink_drop_state = target_ink_drop_state_;
  25. // Assign to |target_ink_drop_state_| before calling AnimateStateChange() so
  26. // that any observers notified as a side effect of the AnimateStateChange()
  27. // will get the target InkDropState when calling GetInkDropState().
  28. target_ink_drop_state_ = ink_drop_state;
  29. if (old_ink_drop_state == InkDropState::HIDDEN &&
  30. target_ink_drop_state_ != InkDropState::HIDDEN) {
  31. GetRootLayer()->SetVisible(true);
  32. }
  33. AnimateStateChange(old_ink_drop_state, target_ink_drop_state_);
  34. animation_observer_->SetActive();
  35. // |this| may be deleted! |animation_observer_| might synchronously call
  36. // AnimationEndedCallback which can delete |this|.
  37. }
  38. void InkDropRipple::SnapToState(InkDropState ink_drop_state) {
  39. AbortAllAnimations();
  40. if (ink_drop_state == InkDropState::ACTIVATED)
  41. GetRootLayer()->SetVisible(true);
  42. else if (ink_drop_state == InkDropState::HIDDEN)
  43. SetStateToHidden();
  44. target_ink_drop_state_ = ink_drop_state;
  45. animation_observer_ = CreateAnimationObserver(ink_drop_state);
  46. animation_observer_->SetActive();
  47. // |this| may be deleted! |animation_observer_| might synchronously call
  48. // AnimationEndedCallback which can delete |this|.
  49. }
  50. void InkDropRipple::SnapToActivated() {
  51. SnapToState(InkDropState::ACTIVATED);
  52. }
  53. bool InkDropRipple::IsVisible() {
  54. return GetRootLayer()->visible();
  55. }
  56. void InkDropRipple::SnapToHidden() {
  57. SnapToState(InkDropState::HIDDEN);
  58. }
  59. test::InkDropRippleTestApi* InkDropRipple::GetTestApi() {
  60. return nullptr;
  61. }
  62. ui::LayerAnimationObserver* InkDropRipple::GetLayerAnimationObserver() {
  63. return animation_observer_.get();
  64. }
  65. void InkDropRipple::AnimationStartedCallback(
  66. InkDropState ink_drop_state,
  67. const ui::CallbackLayerAnimationObserver& observer) {
  68. if (observer_)
  69. observer_->AnimationStarted(ink_drop_state);
  70. }
  71. bool InkDropRipple::AnimationEndedCallback(
  72. InkDropState ink_drop_state,
  73. const ui::CallbackLayerAnimationObserver& observer) {
  74. if (ink_drop_state == InkDropState::HIDDEN)
  75. SetStateToHidden();
  76. if (observer_)
  77. observer_->AnimationEnded(ink_drop_state,
  78. observer.aborted_count()
  79. ? InkDropAnimationEndedReason::PRE_EMPTED
  80. : InkDropAnimationEndedReason::SUCCESS);
  81. // |this| may be deleted!
  82. return false;
  83. }
  84. std::unique_ptr<ui::CallbackLayerAnimationObserver>
  85. InkDropRipple::CreateAnimationObserver(InkDropState ink_drop_state) {
  86. return std::make_unique<ui::CallbackLayerAnimationObserver>(
  87. base::BindRepeating(&InkDropRipple::AnimationStartedCallback,
  88. base::Unretained(this), ink_drop_state),
  89. base::BindRepeating(&InkDropRipple::AnimationEndedCallback,
  90. base::Unretained(this), ink_drop_state));
  91. }
  92. } // namespace views