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