OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #ifndef VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_ | |
6 #define VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_ | |
7 #pragma once | |
8 | |
9 #include <map> | |
10 | |
11 #include "base/memory/ref_counted.h" | |
12 #include "ui/base/animation/animation_container_observer.h" | |
13 #include "ui/base/animation/animation_delegate.h" | |
14 #include "ui/gfx/rect.h" | |
15 #include "views/views_export.h" | |
16 | |
17 namespace ui { | |
18 class SlideAnimation; | |
19 } | |
20 | |
21 namespace views { | |
22 | |
23 class BoundsAnimator; | |
24 class View; | |
25 | |
26 class BoundsAnimatorObserver { | |
27 public: | |
28 // Invoked when all animations are complete. | |
29 virtual void OnBoundsAnimatorDone(BoundsAnimator* animator) = 0; | |
30 }; | |
31 | |
32 // Bounds animator is responsible for animating the bounds of a view from the | |
33 // the views current location and size to a target position and size. To use | |
34 // BoundsAnimator invoke AnimateViewTo for the set of views you want to | |
35 // animate. | |
36 // | |
37 // BoundsAnimator internally creates an animation for each view. If you need | |
38 // a specific animation invoke SetAnimationForView after invoking AnimateViewTo. | |
39 // You can attach an AnimationDelegate to the individual animation for a view | |
40 // by way of SetAnimationDelegate. Additionally you can attach an observer to | |
41 // the BoundsAnimator that is notified when all animations are complete. | |
42 class VIEWS_EXPORT BoundsAnimator : public ui::AnimationDelegate, | |
43 public ui::AnimationContainerObserver { | |
44 public: | |
45 // If |delete_when_done| is set to true in |SetAnimationDelegate| the | |
46 // |AnimationDelegate| must subclass this class. | |
47 class OwnedAnimationDelegate : public ui::AnimationDelegate { | |
48 public: | |
49 virtual ~OwnedAnimationDelegate() {} | |
50 }; | |
51 | |
52 explicit BoundsAnimator(View* view); | |
53 virtual ~BoundsAnimator(); | |
54 | |
55 // Starts animating |view| from its current bounds to |target|. If there is | |
56 // already an animation running for the view it's stopped and a new one | |
57 // started. If an AnimationDelegate has been set for |view| it is removed | |
58 // (after being notified that the animation was canceled). | |
59 void AnimateViewTo(View* view, const gfx::Rect& target); | |
60 | |
61 // Similar to |AnimateViewTo|, but does not reset the animation, only the | |
62 // target bounds. If |view| is not being animated this is the same as | |
63 // invoking |AnimateViewTo|. | |
64 void SetTargetBounds(View* view, const gfx::Rect& target); | |
65 | |
66 // Sets the animation for the specified view. BoundsAnimator takes ownership | |
67 // of the specified animation. | |
68 void SetAnimationForView(View* view, ui::SlideAnimation* animation); | |
69 | |
70 // Returns the animation for the specified view. BoundsAnimator owns the | |
71 // returned Animation. | |
72 const ui::SlideAnimation* GetAnimationForView(View* view); | |
73 | |
74 // Stops animating the specified view. | |
75 void StopAnimatingView(View* view); | |
76 | |
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 ui::AnimationDelegate* delegate, | |
82 bool delete_when_done); | |
83 | |
84 // Returns true if BoundsAnimator is animating the bounds of |view|. | |
85 bool IsAnimating(View* view) const; | |
86 | |
87 // Returns true if BoundsAnimator is animating any view. | |
88 bool IsAnimating() const; | |
89 | |
90 // Cancels all animations, leaving the views at their current location and | |
91 // size. Any views marked for deletion are deleted. | |
92 void Cancel(); | |
93 | |
94 void set_observer(BoundsAnimatorObserver* observer) { | |
95 observer_ = observer; | |
96 } | |
97 | |
98 protected: | |
99 // Creates the animation to use for animating views. | |
100 virtual ui::SlideAnimation* CreateAnimation(); | |
101 | |
102 private: | |
103 // Tracks data about the view being animated. | |
104 struct Data { | |
105 Data() | |
106 : delete_delegate_when_done(false), | |
107 animation(NULL), | |
108 delegate(NULL) {} | |
109 | |
110 // If true the delegate is deleted when done. | |
111 bool delete_delegate_when_done; | |
112 | |
113 // The initial bounds. | |
114 gfx::Rect start_bounds; | |
115 | |
116 // Target bounds. | |
117 gfx::Rect target_bounds; | |
118 | |
119 // The animation. We own this. | |
120 ui::SlideAnimation* animation; | |
121 | |
122 // Additional delegate for the animation, may be null. | |
123 ui::AnimationDelegate* delegate; | |
124 }; | |
125 | |
126 // Used by AnimationEndedOrCanceled. | |
127 enum AnimationEndType { | |
128 ANIMATION_ENDED, | |
129 ANIMATION_CANCELED | |
130 }; | |
131 | |
132 typedef std::map<View*, Data> ViewToDataMap; | |
133 | |
134 typedef std::map<const ui::Animation*, View*> AnimationToViewMap; | |
135 | |
136 // Removes references to |view| and its animation. This does NOT delete the | |
137 // animation or delegate. | |
138 void RemoveFromMaps(View* view); | |
139 | |
140 // Does the necessary cleanup for |data|. If |send_cancel| is true and a | |
141 // delegate has been installed on |data| AnimationCanceled is invoked on it. | |
142 void CleanupData(bool send_cancel, Data* data, View* view); | |
143 | |
144 // Used when changing the animation for a view. This resets the maps for | |
145 // the animation used by view and returns the current animation. Ownership | |
146 // of the returned animation passes to the caller. | |
147 ui::Animation* ResetAnimationForView(View* view); | |
148 | |
149 // Invoked from AnimationEnded and AnimationCanceled. | |
150 void AnimationEndedOrCanceled(const ui::Animation* animation, | |
151 AnimationEndType type); | |
152 | |
153 // ui::AnimationDelegate overrides. | |
154 virtual void AnimationProgressed(const ui::Animation* animation); | |
155 virtual void AnimationEnded(const ui::Animation* animation); | |
156 virtual void AnimationCanceled(const ui::Animation* animation); | |
157 | |
158 // ui::AnimationContainerObserver overrides. | |
159 virtual void AnimationContainerProgressed(ui::AnimationContainer* container); | |
160 virtual void AnimationContainerEmpty(ui::AnimationContainer* container); | |
161 | |
162 // Parent of all views being animated. | |
163 View* parent_; | |
164 | |
165 BoundsAnimatorObserver* observer_; | |
166 | |
167 // All animations we create up with the same container. | |
168 scoped_refptr<ui::AnimationContainer> container_; | |
169 | |
170 // Maps from view being animated to info about the view. | |
171 ViewToDataMap data_; | |
172 | |
173 // Maps from animation to view. | |
174 AnimationToViewMap animation_to_view_; | |
175 | |
176 // As the animations we create update (AnimationProgressed is invoked) this | |
177 // is updated. When all the animations have completed for a given tick of | |
178 // the timer (AnimationContainerProgressed is invoked) the parent_ is asked | |
179 // to repaint these bounds. | |
180 gfx::Rect repaint_bounds_; | |
181 | |
182 DISALLOW_COPY_AND_ASSIGN(BoundsAnimator); | |
183 }; | |
184 | |
185 } // namespace views | |
186 | |
187 #endif // VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_ | |
OLD | NEW |