1 // Copyright 2014 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 COMPONENTS_VIEW_MANAGER_ANIMATION_RUNNER_H_
6 #define COMPONENTS_VIEW_MANAGER_ANIMATION_RUNNER_H_
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/observer_list.h"
15 #include "base/time/time.h"
21 namespace view_manager
{
23 class AnimationRunnerObserver
;
24 class ScheduledAnimationGroup
;
27 // AnimationRunner is responsible for maintaing and running a set of animations.
28 // The animations are represented as a set of AnimationGroups. New animations
29 // are scheduled by way of Schedule(). A |view| may only have one animation
30 // running at a time. Schedule()ing a new animation for a view already animating
31 // implicitly cancels the current animation for the view. Animations progress
32 // by way of the Tick() function.
33 class AnimationRunner
{
35 using AnimationId
= uint32_t;
36 using ViewAndAnimationPair
=
37 std::pair
<ServerView
*, const mojo::AnimationGroup
*>;
39 explicit AnimationRunner(base::TimeTicks now
);
42 void AddObserver(AnimationRunnerObserver
* observer
);
43 void RemoveObserver(AnimationRunnerObserver
* observer
);
45 // Schedules animations. If any of the groups are not valid no animations are
46 // scheuled and 0 is returned. If there is an existing animation in progress
47 // for any of the views it is canceled and any properties that were animating
48 // but are no longer animating are set to their target value.
49 AnimationId
Schedule(const std::vector
<ViewAndAnimationPair
>& views
,
52 // Cancels an animation scheduled by an id that was previously returned from
54 void CancelAnimation(AnimationId id
);
56 // Cancels the animation scheduled for |view|. Does nothing if there is no
57 // animation scheduled for |view|. This does not change |view|. That is, any
58 // in progress animations are stopped.
59 void CancelAnimationForView(ServerView
* view
);
61 // Advance the animations updating values appropriately.
62 void Tick(base::TimeTicks time
);
64 // Returns true if there are animations currently scheduled.
65 bool HasAnimations() const { return !view_to_animation_map_
.empty(); }
67 // Returns true if the animation identified by |id| is valid and animating.
68 bool IsAnimating(AnimationId id
) const {
69 return id_to_views_map_
.count(id
) > 0;
72 // Returns the views that are currently animating for |id|. Returns an empty
73 // set if |id| does not identify a valid animation.
74 std::set
<ServerView
*> GetViewsAnimating(AnimationId id
) {
75 return IsAnimating(id
) ? id_to_views_map_
.find(id
)->second
76 : std::set
<ServerView
*>();
81 // Cancel is the result of scheduling another animation for the view.
82 CANCEL_SOURCE_SCHEDULE
,
84 // Cancel originates from an explicit call to cancel.
88 using ViewToAnimationMap
=
89 base::ScopedPtrHashMap
<ServerView
*, scoped_ptr
<ScheduledAnimationGroup
>>;
90 using IdToViewsMap
= std::map
<AnimationId
, std::set
<ServerView
*>>;
92 void CancelAnimationForViewImpl(ServerView
* view
, CancelSource source
);
94 // Removes |view| from both |view_to_animation_map_| and |id_to_views_map_|.
95 // Returns true if there are no more views animating with the animation id
96 // the view is associated with.
97 bool RemoveViewFromMaps(ServerView
* view
);
101 base::TimeTicks last_tick_time_
;
103 base::ObserverList
<AnimationRunnerObserver
> observers_
;
105 ViewToAnimationMap view_to_animation_map_
;
107 IdToViewsMap id_to_views_map_
;
109 DISALLOW_COPY_AND_ASSIGN(AnimationRunner
);
112 } // namespace view_manager
114 #endif // COMPONENTS_VIEW_MANAGER_ANIMATION_RUNNER_H_