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 COMPONENTS_MUS_WS_ANIMATION_RUNNER_H_ | |
6 #define COMPONENTS_MUS_WS_ANIMATION_RUNNER_H_ | |
7 | |
8 #include <algorithm> | |
9 #include <map> | |
10 #include <memory> | |
11 #include <set> | |
12 #include <unordered_map> | |
13 #include <vector> | |
14 | |
15 #include "base/observer_list.h" | |
16 #include "base/time/time.h" | |
17 | |
18 namespace mus { | |
19 namespace mojom { | |
20 class AnimationGroup; | |
21 } | |
22 | |
23 namespace ws { | |
24 | |
25 class AnimationRunnerObserver; | |
26 class ScheduledAnimationGroup; | |
27 class ServerWindow; | |
28 | |
29 // AnimationRunner is responsible for maintaining and running a set of | |
30 // animations. The animations are represented as a set of AnimationGroups. New | |
31 // animations are scheduled by way of Schedule(). A |window| may only have one | |
32 // animation running at a time. Schedule()ing a new animation for a window | |
33 // already animating implicitly cancels the current animation for the window. | |
34 // Animations progress by way of the Tick() function. | |
35 class AnimationRunner { | |
36 public: | |
37 using AnimationId = uint32_t; | |
38 using WindowAndAnimationPair = | |
39 std::pair<ServerWindow*, const mojom::AnimationGroup*>; | |
40 | |
41 explicit AnimationRunner(base::TimeTicks now); | |
42 ~AnimationRunner(); | |
43 | |
44 void AddObserver(AnimationRunnerObserver* observer); | |
45 void RemoveObserver(AnimationRunnerObserver* observer); | |
46 | |
47 // Schedules animations. If any of the groups are not valid no animations are | |
48 // scheuled and 0 is returned. If there is an existing animation in progress | |
49 // for any of the windows it is canceled and any properties that were | |
50 // animating but are no longer animating are set to their target value. | |
51 AnimationId Schedule(const std::vector<WindowAndAnimationPair>& groups, | |
52 base::TimeTicks now); | |
53 | |
54 // Cancels an animation scheduled by an id that was previously returned from | |
55 // Schedule(). | |
56 void CancelAnimation(AnimationId id); | |
57 | |
58 // Cancels the animation scheduled for |window|. Does nothing if there is no | |
59 // animation scheduled for |window|. This does not change |window|. That is, | |
60 // any in progress animations are stopped. | |
61 void CancelAnimationForWindow(ServerWindow* window); | |
62 | |
63 // Advance the animations updating values appropriately. | |
64 void Tick(base::TimeTicks time); | |
65 | |
66 // Returns true if there are animations currently scheduled. | |
67 bool HasAnimations() const { return !window_to_animation_map_.empty(); } | |
68 | |
69 // Returns true if the animation identified by |id| is valid and animating. | |
70 bool IsAnimating(AnimationId id) const { | |
71 return id_to_windows_map_.count(id) > 0; | |
72 } | |
73 | |
74 // Returns the windows that are currently animating for |id|. Returns an empty | |
75 // set if |id| does not identify a valid animation. | |
76 std::set<ServerWindow*> GetWindowsAnimating(AnimationId id) { | |
77 return IsAnimating(id) ? id_to_windows_map_.find(id)->second | |
78 : std::set<ServerWindow*>(); | |
79 } | |
80 | |
81 private: | |
82 enum CancelSource { | |
83 // Cancel is the result of scheduling another animation for the window. | |
84 CANCEL_SOURCE_SCHEDULE, | |
85 | |
86 // Cancel originates from an explicit call to cancel. | |
87 CANCEL_SOURCE_CANCEL, | |
88 }; | |
89 | |
90 using WindowToAnimationMap = | |
91 std::unordered_map<ServerWindow*, | |
92 std::unique_ptr<ScheduledAnimationGroup>>; | |
93 using IdToWindowsMap = std::map<AnimationId, std::set<ServerWindow*>>; | |
94 | |
95 void CancelAnimationForWindowImpl(ServerWindow* window, CancelSource source); | |
96 | |
97 // Removes |window| from both |window_to_animation_map_| and | |
98 // |id_to_windows_map_|. | |
99 // Returns true if there are no more windows animating with the animation id | |
100 // the window is associated with. | |
101 bool RemoveWindowFromMaps(ServerWindow* window); | |
102 | |
103 AnimationId next_id_; | |
104 | |
105 base::TimeTicks last_tick_time_; | |
106 | |
107 base::ObserverList<AnimationRunnerObserver> observers_; | |
108 | |
109 WindowToAnimationMap window_to_animation_map_; | |
110 | |
111 IdToWindowsMap id_to_windows_map_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(AnimationRunner); | |
114 }; | |
115 | |
116 } // namespace ws | |
117 } // namespace mus | |
118 | |
119 #endif // COMPONENTS_MUS_WS_ANIMATION_RUNNER_H_ | |
OLD | NEW |