15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include <map>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/compositor_export.h"
14b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "ui/compositor/layer_animation_element.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ui {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class LayerAnimationSequence;
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ScopedLayerAnimationSettings;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ImplicitAnimationObserver;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LayerAnimationObservers are notified when animations complete.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class COMPOSITOR_EXPORT LayerAnimationObserver  {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the |sequence| ends. Not called if |sequence| is aborted.
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnLayerAnimationEnded(
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LayerAnimationSequence* sequence) = 0;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called if |sequence| is aborted for any reason. Should never do anything
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that may cause another animation to be started.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnLayerAnimationAborted(
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LayerAnimationSequence* sequence) = 0;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the animation is scheduled.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnLayerAnimationScheduled(
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LayerAnimationSequence* sequence) = 0;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::set<LayerAnimationSequence*> AttachedSequences;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LayerAnimationObserver();
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~LayerAnimationObserver();
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the animator is destroyed during an animation, the animations are
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // aborted. The resulting NotifyAborted notifications will NOT be sent to
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this observer if this function returns false. NOTE: IF YOU OVERRIDE THIS
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // FUNCTION TO RETURN TRUE, YOU MUST REMEMBER TO REMOVE YOURSELF AS AN
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // OBSERVER WHEN YOU ARE DESTROYED.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool RequiresNotificationWhenAnimatorDestroyed() const;
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when |this| is added to |sequence|'s observer list.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnAttachedToSequence(LayerAnimationSequence* sequence);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when |this| is removed to |sequence|'s observer list.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnDetachedFromSequence(LayerAnimationSequence* sequence);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Detaches this observer from all sequences it is currently observing.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StopObserving();
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const AttachedSequences& attached_sequences() const {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return attached_sequences_;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class LayerAnimationSequence;
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when |this| is added to |sequence|'s observer list.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AttachedToSequence(LayerAnimationSequence* sequence);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when |this| is removed to |sequence|'s observer list.
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This will only result in notifications if |send_notification| is true.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DetachedFromSequence(LayerAnimationSequence* sequence,
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            bool send_notification);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AttachedSequences attached_sequences_;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// An implicit animation observer is intended to be used in conjunction with a
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ScopedLayerAnimationSettings object in order to receive a notification when
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// all implicit animations complete.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class COMPOSITOR_EXPORT ImplicitAnimationObserver
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : public LayerAnimationObserver {
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ImplicitAnimationObserver();
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ImplicitAnimationObserver();
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the first animation sequence has started.
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnImplicitAnimationsScheduled() {}
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnImplicitAnimationsCompleted() = 0;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deactivates the observer and clears the collection of animations it is
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // waiting for.
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StopObservingImplicitAnimations();
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
97b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Returns whether animation for |property| was aborted.
98b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Note that if the property wasn't animated, then it couldn't have been
99b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // aborted, so this will return false for that property.
100b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  bool WasAnimationAbortedForProperty(
101b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      LayerAnimationElement::AnimatableProperty property) const;
102b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
103b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Returns whether animation for |property| was completed successfully.
104b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Note that if the property wasn't animated, then it couldn't have been
105b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // completed, so this will return false for that property.
106b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  bool WasAnimationCompletedForProperty(
107b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      LayerAnimationElement::AnimatableProperty property) const;
108b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
110b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  enum AnimationStatus {
111b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    ANIMATION_STATUS_UNKNOWN,
112b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    ANIMATION_STATUS_COMPLETED,
113b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    ANIMATION_STATUS_ABORTED,
114b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  };
115b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class ScopedLayerAnimationSettings;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // LayerAnimationObserver implementation
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnLayerAnimationEnded(
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LayerAnimationSequence* sequence) OVERRIDE;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnLayerAnimationAborted(
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LayerAnimationSequence* sequence) OVERRIDE;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnLayerAnimationScheduled(
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LayerAnimationSequence* sequence) OVERRIDE;
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnAttachedToSequence(
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LayerAnimationSequence* sequence) OVERRIDE;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnDetachedFromSequence(
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LayerAnimationSequence* sequence) OVERRIDE;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // OnImplicitAnimationsCompleted is not fired unless the observer is active.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool active() const { return active_; }
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetActive(bool active);
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CheckCompleted();
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
136b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  void UpdatePropertyAnimationStatus(LayerAnimationSequence* sequence,
137b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                                     AnimationStatus status);
138b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  AnimationStatus AnimationStatusForProperty(
139b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      LayerAnimationElement::AnimatableProperty property) const;
140b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool active_;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set to true in the destructor (if non-NULL). Used to detect deletion while
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // calling out.
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool* destroyed_;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
147b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  typedef std::map<LayerAnimationElement::AnimatableProperty,
148b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                   AnimationStatus> PropertyAnimationStatusMap;
149b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  PropertyAnimationStatusMap property_animation_status_;
150b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if OnLayerAnimationScheduled() has been called at least once.
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool first_sequence_scheduled_;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace ui
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_
158