OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef SERVICES_VIEW_MANAGER_ANIMATION_RUNNER_H_ | |
6 #define SERVICES_VIEW_MANAGER_ANIMATION_RUNNER_H_ | |
7 | |
8 #include <algorithm> | |
9 #include <map> | |
10 #include <set> | |
11 #include <vector> | |
12 | |
13 #include "base/containers/scoped_ptr_hash_map.h" | |
14 #include "base/observer_list.h" | |
15 #include "base/time/time.h" | |
16 | |
17 namespace mojo { | |
18 class AnimationGroup; | |
19 } | |
20 | |
21 namespace view_manager { | |
22 | |
23 class AnimationRunnerObserver; | |
24 class ScheduledAnimationGroup; | |
25 class ServerView; | |
26 | |
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 { | |
34 public: | |
35 using AnimationId = uint32_t; | |
36 using ViewAndAnimationPair = | |
37 std::pair<ServerView*, const mojo::AnimationGroup*>; | |
38 | |
39 explicit AnimationRunner(base::TimeTicks now); | |
40 ~AnimationRunner(); | |
41 | |
42 void AddObserver(AnimationRunnerObserver* observer); | |
43 void RemoveObserver(AnimationRunnerObserver* observer); | |
44 | |
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, | |
50 base::TimeTicks now); | |
51 | |
52 // Cancels an animation scheduled by an id that was previously returned from | |
53 // Schedule(). | |
54 void CancelAnimation(AnimationId id); | |
55 | |
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); | |
60 | |
61 // Advance the animations updating values appropriately. | |
62 void Tick(base::TimeTicks time); | |
63 | |
64 // Returns true if there are animations currently scheduled. | |
65 bool HasAnimations() const { return !view_to_animation_map_.empty(); } | |
66 | |
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; | |
70 } | |
71 | |
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*>(); | |
77 } | |
78 | |
79 private: | |
80 enum CancelSource { | |
81 // Cancel is the result of scheduling another animation for the view. | |
82 CANCEL_SOURCE_SCHEDULE, | |
83 | |
84 // Cancel originates from an explicit call to cancel. | |
85 CANCEL_SOURCE_CANCEL, | |
86 }; | |
87 | |
88 using ViewToAnimationMap = | |
89 base::ScopedPtrHashMap<ServerView*, scoped_ptr<ScheduledAnimationGroup>>; | |
90 using IdToViewsMap = std::map<AnimationId, std::set<ServerView*>>; | |
91 | |
92 void CancelAnimationForViewImpl(ServerView* view, CancelSource source); | |
93 | |
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); | |
98 | |
99 AnimationId next_id_; | |
100 | |
101 base::TimeTicks last_tick_time_; | |
102 | |
103 base::ObserverList<AnimationRunnerObserver> observers_; | |
104 | |
105 ViewToAnimationMap view_to_animation_map_; | |
106 | |
107 IdToViewsMap id_to_views_map_; | |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(AnimationRunner); | |
110 }; | |
111 | |
112 } // namespace view_manager | |
113 | |
114 #endif // SERVICES_VIEW_MANAGER_ANIMATION_RUNNER_H_ | |
OLD | NEW |