Chromium Code Reviews| 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 ScopedVector<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(group.release()); | |
| 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 ScopedVector<ScheduledAnimationGroup> groups; | |
|
sky
2016/06/16 16:36:33
ScopedVector is deprecated.
mfomitchev
2016/06/22 21:59:35
Done.
| |
| 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_.get(group->window()); | |
| 67 if (current_group) | |
| 68 current_group->SetValuesToTargetValuesForPropertiesNotIn(*group); | |
| 69 | |
| 70 CancelAnimationForWindowImpl(group->window(), CANCEL_SOURCE_SCHEDULE); | |
| 71 } | |
| 72 | |
| 73 // Update internal maps | |
| 74 for (auto* group : groups) { | |
| 75 group->ObtainStartValues(); | |
| 76 window_to_animation_map_.set(group->window(), base::WrapUnique(group)); | |
| 77 DCHECK(!id_to_windows_map_[animation_id].count(group->window())); | |
| 78 id_to_windows_map_[animation_id].insert(group->window()); | |
| 79 } | |
| 80 // |window_to_animation_map_| owns the individual group objects. | |
| 81 groups.weak_clear(); | |
| 82 | |
| 83 FOR_EACH_OBSERVER(AnimationRunnerObserver, observers_, | |
| 84 OnAnimationScheduled(animation_id)); | |
| 85 return animation_id; | |
| 86 } | |
| 87 | |
| 88 void AnimationRunner::CancelAnimation(AnimationId id) { | |
| 89 if (id_to_windows_map_.count(id) == 0) | |
| 90 return; | |
| 91 | |
| 92 std::set<ServerWindow*> windows(id_to_windows_map_[id]); | |
| 93 for (ServerWindow* window : windows) | |
| 94 CancelAnimationForWindow(window); | |
| 95 } | |
| 96 | |
| 97 void AnimationRunner::CancelAnimationForWindow(ServerWindow* window) { | |
| 98 CancelAnimationForWindowImpl(window, CANCEL_SOURCE_CANCEL); | |
| 99 } | |
| 100 | |
| 101 void AnimationRunner::Tick(base::TimeTicks time) { | |
| 102 DCHECK(time >= last_tick_time_); | |
| 103 last_tick_time_ = time; | |
| 104 if (window_to_animation_map_.empty()) | |
| 105 return; | |
| 106 | |
| 107 // The animation ids of any windows whose animation completes are added here. | |
| 108 // We notify after processing all windows so that if an observer mutates us in | |
| 109 // some way we're aren't left in a weird state. | |
| 110 std::set<AnimationId> animations_completed; | |
| 111 for (WindowToAnimationMap::iterator i = window_to_animation_map_.begin(); | |
| 112 i != window_to_animation_map_.end();) { | |
| 113 bool animation_done = i->second->Tick(time); | |
| 114 if (animation_done) { | |
| 115 const AnimationId animation_id = i->second->id(); | |
| 116 ServerWindow* window = i->first; | |
| 117 ++i; | |
| 118 bool animation_completed = RemoveWindowFromMaps(window); | |
| 119 if (animation_completed) | |
| 120 animations_completed.insert(animation_id); | |
| 121 } else { | |
| 122 ++i; | |
| 123 } | |
| 124 } | |
| 125 for (const AnimationId& id : animations_completed) { | |
| 126 FOR_EACH_OBSERVER(AnimationRunnerObserver, observers_, OnAnimationDone(id)); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 void AnimationRunner::CancelAnimationForWindowImpl(ServerWindow* window, | |
| 131 CancelSource source) { | |
| 132 if (!window_to_animation_map_.contains(window)) | |
| 133 return; | |
| 134 | |
| 135 const AnimationId animation_id = window_to_animation_map_.get(window)->id(); | |
| 136 if (RemoveWindowFromMaps(window)) { | |
| 137 // This was the last window in the group. | |
| 138 if (source == CANCEL_SOURCE_CANCEL) { | |
| 139 FOR_EACH_OBSERVER(AnimationRunnerObserver, observers_, | |
| 140 OnAnimationCanceled(animation_id)); | |
| 141 } else { | |
| 142 FOR_EACH_OBSERVER(AnimationRunnerObserver, observers_, | |
| 143 OnAnimationInterrupted(animation_id)); | |
| 144 } | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 bool AnimationRunner::RemoveWindowFromMaps(ServerWindow* window) { | |
| 149 DCHECK(window_to_animation_map_.contains(window)); | |
| 150 | |
| 151 const AnimationId animation_id = window_to_animation_map_.get(window)->id(); | |
| 152 window_to_animation_map_.erase(window); | |
| 153 | |
| 154 DCHECK(id_to_windows_map_.count(animation_id)); | |
| 155 id_to_windows_map_[animation_id].erase(window); | |
| 156 if (!id_to_windows_map_[animation_id].empty()) | |
| 157 return false; | |
| 158 | |
| 159 id_to_windows_map_.erase(animation_id); | |
| 160 return true; | |
| 161 } | |
| 162 | |
| 163 } // namespace ws | |
| 164 } // namespace mus | |
| OLD | NEW |