layer_animation_observer.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright (c) 2012 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/compositor/layer_animation_observer.h"
  5. #include "ui/compositor/layer_animation_sequence.h"
  6. namespace ui {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. // LayerAnimationObserver
  9. LayerAnimationObserver::LayerAnimationObserver() {
  10. }
  11. LayerAnimationObserver::~LayerAnimationObserver() {
  12. StopObserving();
  13. }
  14. void LayerAnimationObserver::OnLayerAnimationStarted(
  15. LayerAnimationSequence* sequence) {}
  16. bool LayerAnimationObserver::RequiresNotificationWhenAnimatorDestroyed() const {
  17. return false;
  18. }
  19. void LayerAnimationObserver::OnAttachedToSequence(
  20. LayerAnimationSequence* sequence) {
  21. }
  22. void LayerAnimationObserver::OnDetachedFromSequence(
  23. LayerAnimationSequence* sequence) {
  24. }
  25. void LayerAnimationObserver::StopObserving() {
  26. while (!attached_sequences_.empty()) {
  27. LayerAnimationSequence* sequence = *attached_sequences_.begin();
  28. sequence->RemoveObserver(this);
  29. }
  30. }
  31. void LayerAnimationObserver::AttachedToSequence(
  32. LayerAnimationSequence* sequence) {
  33. DCHECK(attached_sequences_.find(sequence) == attached_sequences_.end());
  34. attached_sequences_.insert(sequence);
  35. OnAttachedToSequence(sequence);
  36. }
  37. void LayerAnimationObserver::DetachedFromSequence(
  38. LayerAnimationSequence* sequence, bool send_notification) {
  39. if (attached_sequences_.find(sequence) != attached_sequences_.end())
  40. attached_sequences_.erase(sequence);
  41. if (send_notification)
  42. OnDetachedFromSequence(sequence);
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////
  45. // ImplicitAnimationObserver
  46. ImplicitAnimationObserver::ImplicitAnimationObserver() = default;
  47. ImplicitAnimationObserver::~ImplicitAnimationObserver() = default;
  48. void ImplicitAnimationObserver::SetActive(bool active) {
  49. active_ = active;
  50. CheckCompleted();
  51. }
  52. void ImplicitAnimationObserver::StopObservingImplicitAnimations() {
  53. SetActive(false);
  54. StopObserving();
  55. }
  56. bool ImplicitAnimationObserver::WasAnimationAbortedForProperty(
  57. LayerAnimationElement::AnimatableProperty property) const {
  58. return AnimationStatusForProperty(property) == ANIMATION_STATUS_ABORTED;
  59. }
  60. bool ImplicitAnimationObserver::WasAnimationCompletedForProperty(
  61. LayerAnimationElement::AnimatableProperty property) const {
  62. return AnimationStatusForProperty(property) == ANIMATION_STATUS_COMPLETED;
  63. }
  64. void ImplicitAnimationObserver::OnLayerAnimationEnded(
  65. LayerAnimationSequence* sequence) {
  66. UpdatePropertyAnimationStatus(sequence, ANIMATION_STATUS_COMPLETED);
  67. auto weak_this = weak_factory_.GetWeakPtr();
  68. sequence->RemoveObserver(this);
  69. if (!weak_this)
  70. return;
  71. DCHECK(attached_sequences().find(sequence) == attached_sequences().end());
  72. CheckCompleted();
  73. }
  74. void ImplicitAnimationObserver::OnLayerAnimationAborted(
  75. LayerAnimationSequence* sequence) {
  76. UpdatePropertyAnimationStatus(sequence, ANIMATION_STATUS_ABORTED);
  77. auto weak_this = weak_factory_.GetWeakPtr();
  78. sequence->RemoveObserver(this);
  79. if (!weak_this)
  80. return;
  81. DCHECK(attached_sequences().find(sequence) == attached_sequences().end());
  82. CheckCompleted();
  83. }
  84. void ImplicitAnimationObserver::OnLayerAnimationScheduled(
  85. LayerAnimationSequence* sequence) {
  86. if (!first_sequence_scheduled_) {
  87. first_sequence_scheduled_ = true;
  88. OnImplicitAnimationsScheduled();
  89. }
  90. }
  91. void ImplicitAnimationObserver::OnAttachedToSequence(
  92. LayerAnimationSequence* sequence) {
  93. }
  94. void ImplicitAnimationObserver::OnDetachedFromSequence(
  95. LayerAnimationSequence* sequence) {
  96. DCHECK(attached_sequences().find(sequence) == attached_sequences().end());
  97. CheckCompleted();
  98. }
  99. void ImplicitAnimationObserver::CheckCompleted() {
  100. if (active_ && attached_sequences().empty()) {
  101. active_ = false;
  102. OnImplicitAnimationsCompleted();
  103. }
  104. }
  105. void ImplicitAnimationObserver::UpdatePropertyAnimationStatus(
  106. LayerAnimationSequence* sequence,
  107. AnimationStatus status) {
  108. LayerAnimationElement::AnimatableProperties properties =
  109. sequence->properties();
  110. for (unsigned i = LayerAnimationElement::FIRST_PROPERTY;
  111. i != LayerAnimationElement::SENTINEL;
  112. i = i << 1) {
  113. if (i & properties) {
  114. LayerAnimationElement::AnimatableProperty property =
  115. static_cast<LayerAnimationElement::AnimatableProperty>(i);
  116. property_animation_status_[property] = status;
  117. }
  118. }
  119. }
  120. ImplicitAnimationObserver::AnimationStatus
  121. ImplicitAnimationObserver::AnimationStatusForProperty(
  122. LayerAnimationElement::AnimatableProperty property) const {
  123. auto iter = property_animation_status_.find(property);
  124. return iter == property_animation_status_.end() ? ANIMATION_STATUS_UNKNOWN :
  125. iter->second;
  126. }
  127. } // namespace ui