OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "ash/common/shelf/shelf_background_animator.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "ash/common/material_design/material_design_controller.h" | |
10 #include "ash/common/shelf/shelf_background_animator_observer.h" | |
11 #include "ash/common/shelf/shelf_constants.h" | |
12 #include "ui/gfx/animation/tween.h" | |
James Cook
2016/07/27 00:30:38
needed?
bruthig
2016/07/27 17:05:44
Removed.
| |
13 | |
14 namespace ash { | |
15 | |
16 namespace { | |
17 // The total number of animators that will call BackgroundAnimationEnded(). | |
18 const int kNumAnimators = 3; | |
19 | |
20 const int kMaxAlpha = 255; | |
21 } // namespace | |
22 | |
23 ShelfBackgroundAnimator::ShelfBackgroundAnimator( | |
24 ShelfBackgroundType background_type) { | |
25 // Initialize animators so that adding observers get notified with consistent | |
26 // values. | |
27 AnimateBackground(background_type, BACKGROUND_CHANGE_IMMEDIATE); | |
28 } | |
29 | |
30 ShelfBackgroundAnimator::~ShelfBackgroundAnimator() {} | |
31 | |
32 void ShelfBackgroundAnimator::AddObserver( | |
33 ShelfBackgroundAnimatorObserver* observer) { | |
34 observers_.AddObserver(observer); | |
35 | |
36 observer->UpdateShelfOpaqueBackground(opaque_background_animator_->alpha()); | |
37 observer->UpdateShelfAssetBackground(asset_background_animator_->alpha()); | |
38 observer->UpdateShelfItemBackground(item_background_animator_->alpha()); | |
39 } | |
40 | |
41 void ShelfBackgroundAnimator::RemoveObserver( | |
42 ShelfBackgroundAnimatorObserver* observer) { | |
43 observers_.RemoveObserver(observer); | |
44 } | |
45 | |
46 void ShelfBackgroundAnimator::PaintBackground( | |
47 ShelfBackgroundType background_type, | |
48 BackgroundAnimatorChangeType change_type) { | |
49 if (target_background_type_ == background_type && | |
50 change_type == BACKGROUND_CHANGE_ANIMATE) { | |
51 return; | |
52 } | |
53 | |
54 AnimateBackground(background_type, change_type); | |
55 } | |
56 | |
57 void ShelfBackgroundAnimator::OnBackgroundChanged( | |
58 ShelfBackgroundType background_type, | |
59 BackgroundAnimatorChangeType change_type) { | |
60 PaintBackground(background_type, change_type); | |
61 } | |
62 | |
63 void ShelfBackgroundAnimator::UpdateBackground(BackgroundAnimator* animator, | |
64 int alpha) { | |
65 OnAlphaChanged(animator, alpha); | |
66 } | |
67 | |
68 void ShelfBackgroundAnimator::BackgroundAnimationEnded( | |
69 BackgroundAnimator* animator) { | |
70 ++successful_animator_count_; | |
71 DCHECK_LE(successful_animator_count_, kNumAnimators); | |
72 // UpdateBackground() is only called when alpha values change, this ensures | |
73 // observers are always notified for every background change. | |
74 OnAlphaChanged(animator, animator->alpha()); | |
75 } | |
76 | |
77 void ShelfBackgroundAnimator::OnAlphaChanged(BackgroundAnimator* animator, | |
78 int alpha) { | |
79 if (animator == opaque_background_animator_.get()) { | |
80 FOR_EACH_OBSERVER(ShelfBackgroundAnimatorObserver, observers_, | |
81 UpdateShelfOpaqueBackground(alpha)); | |
82 } else if (animator == asset_background_animator_.get()) { | |
83 FOR_EACH_OBSERVER(ShelfBackgroundAnimatorObserver, observers_, | |
84 UpdateShelfAssetBackground(alpha)); | |
85 } else if (animator == item_background_animator_.get()) { | |
86 FOR_EACH_OBSERVER(ShelfBackgroundAnimatorObserver, observers_, | |
87 UpdateShelfItemBackground(alpha)); | |
88 } else { | |
89 NOTREACHED(); | |
90 } | |
91 } | |
92 | |
93 void ShelfBackgroundAnimator::AnimateBackground( | |
94 ShelfBackgroundType background_type, | |
95 BackgroundAnimatorChangeType change_type) { | |
96 // Ensure BackgroundAnimationEnded() has been called for all the | |
97 // BackgroundAnimators owned by this so that |successful_animator_count_| | |
98 // is stable and doesn't get updated as a side effect of destroying/animating | |
99 // the animators. | |
100 StopAnimators(); | |
101 | |
102 bool show_background = true; | |
103 if (can_reuse_animators_ && previous_background_type_ == background_type) { | |
104 DCHECK_EQ(opaque_background_animator_->paints_background(), | |
105 asset_background_animator_->paints_background()); | |
106 DCHECK_EQ(asset_background_animator_->paints_background(), | |
107 item_background_animator_->paints_background()); | |
108 | |
109 show_background = !opaque_background_animator_->paints_background(); | |
110 } else { | |
111 CreateAnimators(background_type, change_type); | |
112 | |
113 // If all the previous animators completed successfully and the animation | |
114 // was between 2 distinct states, then the last alpha values are valid | |
115 // end state values. | |
116 can_reuse_animators_ = target_background_type_ != background_type && | |
117 successful_animator_count_ == kNumAnimators; | |
118 } | |
119 | |
120 successful_animator_count_ = 0; | |
121 | |
122 opaque_background_animator_->SetPaintsBackground(show_background, | |
123 change_type); | |
124 asset_background_animator_->SetPaintsBackground(show_background, change_type); | |
125 item_background_animator_->SetPaintsBackground(show_background, change_type); | |
126 | |
127 if (target_background_type_ != background_type) { | |
128 previous_background_type_ = target_background_type_; | |
129 target_background_type_ = background_type; | |
130 } | |
131 } | |
132 | |
133 void ShelfBackgroundAnimator::CreateAnimators( | |
134 ShelfBackgroundType background_type, | |
135 BackgroundAnimatorChangeType change_type) { | |
136 const int opaque_background_alpha = | |
137 opaque_background_animator_ ? opaque_background_animator_->alpha() : 0; | |
138 const int asset_background_alpha = | |
139 asset_background_animator_ ? asset_background_animator_->alpha() : 0; | |
140 const int item_background_alpha = | |
141 item_background_animator_ ? item_background_animator_->alpha() : 0; | |
142 | |
143 const bool is_material = MaterialDesignController::IsShelfMaterial(); | |
144 int duration_ms = 0; | |
145 | |
146 switch (background_type) { | |
147 case SHELF_BACKGROUND_DEFAULT: | |
148 duration_ms = is_material ? 500 : 1000; | |
149 opaque_background_animator_.reset( | |
150 new BackgroundAnimator(this, opaque_background_alpha, 0)); | |
151 asset_background_animator_.reset( | |
152 new BackgroundAnimator(this, asset_background_alpha, 0)); | |
153 item_background_animator_.reset( | |
154 new BackgroundAnimator(this, item_background_alpha, | |
155 GetShelfConstant(SHELF_BACKGROUND_ALPHA))); | |
156 break; | |
157 case SHELF_BACKGROUND_OVERLAP: | |
158 duration_ms = is_material ? 500 : 1000; | |
159 opaque_background_animator_.reset(new BackgroundAnimator( | |
160 this, opaque_background_alpha, | |
161 is_material ? GetShelfConstant(SHELF_BACKGROUND_ALPHA) : 0)); | |
162 asset_background_animator_.reset(new BackgroundAnimator( | |
163 this, asset_background_alpha, | |
164 is_material ? 0 : GetShelfConstant(SHELF_BACKGROUND_ALPHA))); | |
165 item_background_animator_.reset(new BackgroundAnimator( | |
166 this, item_background_alpha, | |
167 is_material ? 0 : GetShelfConstant(SHELF_BACKGROUND_ALPHA))); | |
168 break; | |
169 case SHELF_BACKGROUND_MAXIMIZED: | |
170 duration_ms = is_material ? 250 : 1000; | |
171 opaque_background_animator_.reset( | |
172 new BackgroundAnimator(this, opaque_background_alpha, kMaxAlpha)); | |
173 asset_background_animator_.reset(new BackgroundAnimator( | |
174 this, asset_background_alpha, | |
175 is_material ? 0 : GetShelfConstant(SHELF_BACKGROUND_ALPHA))); | |
176 item_background_animator_.reset(new BackgroundAnimator( | |
177 this, item_background_alpha, is_material ? 0 : kMaxAlpha)); | |
178 break; | |
179 } | |
180 | |
181 opaque_background_animator_->SetDuration(duration_ms); | |
182 asset_background_animator_->SetDuration(duration_ms); | |
183 item_background_animator_->SetDuration(duration_ms); | |
184 } | |
185 | |
186 void ShelfBackgroundAnimator::StopAnimators() { | |
187 if (opaque_background_animator_) | |
188 opaque_background_animator_->Stop(); | |
189 if (asset_background_animator_) | |
190 asset_background_animator_->Stop(); | |
191 if (item_background_animator_) | |
192 item_background_animator_->Stop(); | |
193 } | |
194 | |
195 } // namespace ash | |
OLD | NEW |