| 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 #include "components/mus/ws/animation_runner.h" | |
| 6 | |
| 7 #include "base/memory/scoped_vector.h" | |
| 8 #include "components/mus/ws/animation_runner_observer.h" | |
| 9 #include "components/mus/ws/scheduled_animation_group.h" | |
| 10 #include "components/mus/ws/server_window.h" | |
| 11 | |
| 12 namespace mus { | |
| 13 namespace ws { | |
| 14 namespace { | |
| 15 | |
| 16 bool ConvertWindowAndAnimationPairsToScheduledAnimationGroups( | |
| 17 const std::vector<AnimationRunner::WindowAndAnimationPair>& pairs, | |
| 18 AnimationRunner::AnimationId id, | |
| 19 base::TimeTicks now, | |
| 20 std::vector<std::unique_ptr<ScheduledAnimationGroup>>* groups) { | |
| 21 for (const auto& window_animation_pair : pairs) { | |
| 22 DCHECK(window_animation_pair.first); | |
| 23 DCHECK(window_animation_pair.second); | |
| 24 std::unique_ptr<ScheduledAnimationGroup> group( | |
| 25 ScheduledAnimationGroup::Create(window_animation_pair.first, now, id, | |
| 26 *(window_animation_pair.second))); | |
| 27 if (!group.get()) | |
| 28 return false; | |
| 29 groups->push_back(std::move(group)); | |
| 30 } | |
| 31 return true; | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 AnimationRunner::AnimationRunner(base::TimeTicks now) | |
| 37 : next_id_(1), last_tick_time_(now) {} | |
| 38 | |
| 39 AnimationRunner::~AnimationRunner() {} | |
| 40 | |
| 41 void AnimationRunner::AddObserver(AnimationRunnerObserver* observer) { | |
| 42 observers_.AddObserver(observer); | |
| 43 } | |
| 44 | |
| 45 void AnimationRunner::RemoveObserver(AnimationRunnerObserver* observer) { | |
| 46 observers_.RemoveObserver(observer); | |
| 47 } | |
| 48 | |
| 49 AnimationRunner::AnimationId AnimationRunner::Schedule( | |
| 50 const std::vector<WindowAndAnimationPair>& pairs, | |
| 51 base::TimeTicks now) { | |
| 52 DCHECK_GE(now, last_tick_time_); | |
| 53 | |
| 54 const AnimationId animation_id = next_id_++; | |
| 55 std::vector<std::unique_ptr<ScheduledAnimationGroup>> groups; | |
| 56 if (!ConvertWindowAndAnimationPairsToScheduledAnimationGroups( | |
| 57 pairs, animation_id, now, &groups)) { | |
| 58 return 0; | |
| 59 } | |
| 60 | |
| 61 // Cancel any animations for the affected windows. If the cancelled animations | |
| 62 // also animated properties that are not animated by the new group - instantly | |
| 63 // set those to the target value. | |
| 64 for (auto& group : groups) { | |
| 65 ScheduledAnimationGroup* current_group = | |
| 66 window_to_animation_map_[group->window()].get(); | |
| 67 if (current_group) | |
| 68 current_group->SetValuesToTargetValuesForPropertiesNotIn(*group.get()); | |
| 69 | |
| 70 CancelAnimationForWindowImpl(group->window(), CANCEL_SOURCE_SCHEDULE); | |
| 71 } | |
| 72 | |
| 73 // Update internal maps | |
| 74 for (auto& group : groups) { | |
| 75 group->ObtainStartValues(); | |
| 76 ServerWindow* window = group->window(); | |
| 77 window_to_animation_map_[window] = std::move(group); | |
| 78 DCHECK(!id_to_windows_map_[animation_id].count(window)); | |
| 79 id_to_windows_map_[animation_id].insert(window); | |
| 80 } | |
| 81 | |
| 82 FOR_EACH_OBSERVER(AnimationRunnerObserver, observers_, | |
| 83 OnAnimationScheduled(animation_id)); | |
| 84 return animation_id; | |
| 85 } | |
| 86 | |
| 87 void AnimationRunner::CancelAnimation(AnimationId id) { | |
| 88 if (id_to_windows_map_.count(id) == 0) | |
| 89 return; | |
| 90 | |
| 91 std::set<ServerWindow*> windows(id_to_windows_map_[id]); | |
| 92 for (ServerWindow* window : windows) | |
| 93 CancelAnimationForWindow(window); | |
| 94 } | |
| 95 | |
| 96 void AnimationRunner::CancelAnimationForWindow(ServerWindow* window) { | |
| 97 CancelAnimationForWindowImpl(window, CANCEL_SOURCE_CANCEL); | |
| 98 } | |
| 99 | |
| 100 void AnimationRunner::Tick(base::TimeTicks time) { | |
| 101 DCHECK(time >= last_tick_time_); | |
| 102 last_tick_time_ = time; | |
| 103 if (window_to_animation_map_.empty()) | |
| 104 return; | |
| 105 | |
| 106 // The animation ids of any windows whose animation completes are added here. | |
| 107 // We notify after processing all windows so that if an observer mutates us in | |
| 108 // some way we're aren't left in a weird state. | |
| 109 std::set<AnimationId> animations_completed; | |
| 110 for (WindowToAnimationMap::iterator i = window_to_animation_map_.begin(); | |
| 111 i != window_to_animation_map_.end();) { | |
| 112 bool animation_done = i->second->Tick(time); | |
| 113 if (animation_done) { | |
| 114 const AnimationId animation_id = i->second->id(); | |
| 115 ServerWindow* window = i->first; | |
| 116 ++i; | |
| 117 bool animation_completed = RemoveWindowFromMaps(window); | |
| 118 if (animation_completed) | |
| 119 animations_completed.insert(animation_id); | |
| 120 } else { | |
| 121 ++i; | |
| 122 } | |
| 123 } | |
| 124 for (const AnimationId& id : animations_completed) { | |
| 125 FOR_EACH_OBSERVER(AnimationRunnerObserver, observers_, OnAnimationDone(id)); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void AnimationRunner::CancelAnimationForWindowImpl(ServerWindow* window, | |
| 130 CancelSource source) { | |
| 131 if (!window_to_animation_map_[window]) | |
| 132 return; | |
| 133 | |
| 134 const AnimationId animation_id = window_to_animation_map_[window]->id(); | |
| 135 if (RemoveWindowFromMaps(window)) { | |
| 136 // This was the last window in the group. | |
| 137 if (source == CANCEL_SOURCE_CANCEL) { | |
| 138 FOR_EACH_OBSERVER(AnimationRunnerObserver, observers_, | |
| 139 OnAnimationCanceled(animation_id)); | |
| 140 } else { | |
| 141 FOR_EACH_OBSERVER(AnimationRunnerObserver, observers_, | |
| 142 OnAnimationInterrupted(animation_id)); | |
| 143 } | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 bool AnimationRunner::RemoveWindowFromMaps(ServerWindow* window) { | |
| 148 DCHECK(window_to_animation_map_[window]); | |
| 149 | |
| 150 const AnimationId animation_id = window_to_animation_map_[window]->id(); | |
| 151 window_to_animation_map_.erase(window); | |
| 152 | |
| 153 DCHECK(id_to_windows_map_.count(animation_id)); | |
| 154 id_to_windows_map_[animation_id].erase(window); | |
| 155 if (!id_to_windows_map_[animation_id].empty()) | |
| 156 return false; | |
| 157 | |
| 158 id_to_windows_map_.erase(animation_id); | |
| 159 return true; | |
| 160 } | |
| 161 | |
| 162 } // namespace ws | |
| 163 } // namespace mus | |
| OLD | NEW |