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.
5 #ifndef UI_VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_
6 #define UI_VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/observer_list.h"
13 #include "ui/gfx/animation/animation_container_observer.h"
14 #include "ui/gfx/animation/animation_delegate.h"
15 #include "ui/gfx/animation/tween.h"
16 #include "ui/gfx/rect.h"
17 #include "ui/views/views_export.h"
25 class BoundsAnimatorObserver
;
28 // Bounds animator is responsible for animating the bounds of a view from the
29 // the views current location and size to a target position and size. To use
30 // BoundsAnimator invoke AnimateViewTo for the set of views you want to
33 // BoundsAnimator internally creates an animation for each view. If you need
34 // a specific animation invoke SetAnimationForView after invoking AnimateViewTo.
35 // You can attach an AnimationDelegate to the individual animation for a view
36 // by way of SetAnimationDelegate. Additionally you can attach an observer to
37 // the BoundsAnimator that is notified when all animations are complete.
38 class VIEWS_EXPORT BoundsAnimator
: public gfx::AnimationDelegate
,
39 public gfx::AnimationContainerObserver
{
41 // If |delete_when_done| is set to true in |SetAnimationDelegate| the
42 // |AnimationDelegate| must subclass this class.
43 class OwnedAnimationDelegate
: public gfx::AnimationDelegate
{
45 virtual ~OwnedAnimationDelegate() {}
48 explicit BoundsAnimator(View
* view
);
49 virtual ~BoundsAnimator();
51 // Starts animating |view| from its current bounds to |target|. If there is
52 // already an animation running for the view it's stopped and a new one
53 // started. If an AnimationDelegate has been set for |view| it is removed
54 // (after being notified that the animation was canceled).
55 void AnimateViewTo(View
* view
, const gfx::Rect
& target
);
57 // Similar to |AnimateViewTo|, but does not reset the animation, only the
58 // target bounds. If |view| is not being animated this is the same as
59 // invoking |AnimateViewTo|.
60 void SetTargetBounds(View
* view
, const gfx::Rect
& target
);
62 // Returns the target bounds for the specified view. If |view| is not
63 // animating its current bounds is returned.
64 gfx::Rect
GetTargetBounds(View
* view
);
66 // Sets the animation for the specified view. BoundsAnimator takes ownership
67 // of the specified animation.
68 void SetAnimationForView(View
* view
, gfx::SlideAnimation
* animation
);
70 // Returns the animation for the specified view. BoundsAnimator owns the
71 // returned Animation.
72 const gfx::SlideAnimation
* GetAnimationForView(View
* view
);
74 // Stops animating the specified view.
75 void StopAnimatingView(View
* view
);
77 // Sets the delegate for the animation created for the specified view. If
78 // |delete_when_done| is true the |delegate| is deleted when done and
79 // |delegate| must subclass OwnedAnimationDelegate.
80 void SetAnimationDelegate(View
* view
,
81 gfx::AnimationDelegate
* delegate
,
82 bool delete_when_done
);
84 // Returns true if BoundsAnimator is animating the bounds of |view|.
85 bool IsAnimating(View
* view
) const;
87 // Returns true if BoundsAnimator is animating any view.
88 bool IsAnimating() const;
90 // Cancels all animations, leaving the views at their current location and
91 // size. Any views marked for deletion are deleted.
94 // Overrides default animation duration. |duration_ms| is the new duration in
96 void SetAnimationDuration(int duration_ms
);
98 // Gets the currently used animation duration.
99 int GetAnimationDuration() const { return animation_duration_ms_
; }
101 // Sets the tween type for new animations. Default is EASE_OUT.
102 void set_tween_type(gfx::Tween::Type type
) { tween_type_
= type
; }
104 void AddObserver(BoundsAnimatorObserver
* observer
);
105 void RemoveObserver(BoundsAnimatorObserver
* observer
);
108 // Creates the animation to use for animating views.
109 virtual gfx::SlideAnimation
* CreateAnimation();
112 // Tracks data about the view being animated.
115 : delete_delegate_when_done(false),
119 // If true the delegate is deleted when done.
120 bool delete_delegate_when_done
;
122 // The initial bounds.
123 gfx::Rect start_bounds
;
126 gfx::Rect target_bounds
;
128 // The animation. We own this.
129 gfx::SlideAnimation
* animation
;
131 // Additional delegate for the animation, may be null.
132 gfx::AnimationDelegate
* delegate
;
135 // Used by AnimationEndedOrCanceled.
136 enum AnimationEndType
{
141 typedef std::map
<View
*, Data
> ViewToDataMap
;
143 typedef std::map
<const gfx::Animation
*, View
*> AnimationToViewMap
;
145 // Removes references to |view| and its animation. This does NOT delete the
146 // animation or delegate.
147 void RemoveFromMaps(View
* view
);
149 // Does the necessary cleanup for |data|. If |send_cancel| is true and a
150 // delegate has been installed on |data| AnimationCanceled is invoked on it.
151 void CleanupData(bool send_cancel
, Data
* data
, View
* view
);
153 // Used when changing the animation for a view. This resets the maps for
154 // the animation used by view and returns the current animation. Ownership
155 // of the returned animation passes to the caller.
156 gfx::Animation
* ResetAnimationForView(View
* view
);
158 // Invoked from AnimationEnded and AnimationCanceled.
159 void AnimationEndedOrCanceled(const gfx::Animation
* animation
,
160 AnimationEndType type
);
162 // gfx::AnimationDelegate overrides.
163 virtual void AnimationProgressed(const gfx::Animation
* animation
) OVERRIDE
;
164 virtual void AnimationEnded(const gfx::Animation
* animation
) OVERRIDE
;
165 virtual void AnimationCanceled(const gfx::Animation
* animation
) OVERRIDE
;
167 // gfx::AnimationContainerObserver overrides.
168 virtual void AnimationContainerProgressed(
169 gfx::AnimationContainer
* container
) OVERRIDE
;
170 virtual void AnimationContainerEmpty(
171 gfx::AnimationContainer
* container
) OVERRIDE
;
173 // Parent of all views being animated.
176 ObserverList
<BoundsAnimatorObserver
> observers_
;
178 // All animations we create up with the same container.
179 scoped_refptr
<gfx::AnimationContainer
> container_
;
181 // Maps from view being animated to info about the view.
184 // Maps from animation to view.
185 AnimationToViewMap animation_to_view_
;
187 // As the animations we create update (AnimationProgressed is invoked) this
188 // is updated. When all the animations have completed for a given tick of
189 // the timer (AnimationContainerProgressed is invoked) the parent_ is asked
190 // to repaint these bounds.
191 gfx::Rect repaint_bounds_
;
193 int animation_duration_ms_
;
195 gfx::Tween::Type tween_type_
;
197 DISALLOW_COPY_AND_ASSIGN(BoundsAnimator
);
202 #endif // UI_VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_