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" | |
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 : target_background_type_(SHELF_BACKGROUND_DEFAULT), | |
James Cook
2016/06/14 17:50:00
optional: You could init these in the header
bruthig
2016/07/26 19:50:01
Done.
| |
25 previous_background_type_(SHELF_BACKGROUND_MAXIMIZED), | |
26 previous_opaque_background_alpha_(0), | |
27 previous_asset_background_alpha_(0), | |
28 previous_item_background_alpha_(0), | |
29 can_reuse_animators_(false), | |
30 successful_animator_count_(0), | |
31 has_animated_once_(false) {} | |
32 | |
33 ShelfBackgroundAnimator::~ShelfBackgroundAnimator() {} | |
34 | |
35 void ShelfBackgroundAnimator::AddObserver( | |
36 ShelfBackgroundAnimatorObserver* observer) { | |
37 observers_.AddObserver(observer); | |
38 } | |
39 | |
40 void ShelfBackgroundAnimator::RemoveObserver( | |
41 ShelfBackgroundAnimatorObserver* observer) { | |
42 observers_.RemoveObserver(observer); | |
43 } | |
44 | |
45 void ShelfBackgroundAnimator::PaintBackground( | |
46 ShelfBackgroundType background_type, | |
47 BackgroundAnimatorChangeType change_type) { | |
48 // The first call to animate should always succeed so that observers can | |
49 // initialize themselves. | |
50 // | |
51 // It simplifies animator re-use logic if the first/existing animation request | |
52 // wins. The BackgroundAnimator performs a similar check anyway. | |
53 if (has_animated_once_ && target_background_type_ == background_type) { | |
James Cook
2016/06/14 17:50:01
I don't understand how this matches the comment. I
bruthig
2016/07/26 19:50:01
Updated, PTAL.
| |
54 return; | |
55 } | |
James Cook
2016/06/14 17:50:01
nit: no braces
bruthig
2016/07/26 19:50:01
Done.
| |
56 | |
57 // Ensure BackgroundAnimationEnded() has been called for all the | |
58 // BackgroundAnimators owned by this so that |successful_animator_count_| | |
59 // is stable and doesn't get updated as a side effect of destroying/animating | |
60 // the animators. | |
61 StopAnimators(); | |
62 | |
63 bool show_background = true; | |
64 if (can_reuse_animators_ && previous_background_type_ == background_type) { | |
65 DCHECK_EQ(opaque_background_animator_->paints_background(), | |
66 asset_background_animator_->paints_background()); | |
67 DCHECK_EQ(asset_background_animator_->paints_background(), | |
68 item_background_animator_->paints_background()); | |
69 | |
70 show_background = !opaque_background_animator_->paints_background(); | |
71 } else { | |
72 CreateAnimators(background_type, change_type); | |
73 ConfigureAnimators(background_type, change_type); | |
74 | |
75 // If all the previous animators completed successfully, then all the | |
76 // |previous_shelf_*_alpha_| values are valid end state values. | |
77 can_reuse_animators_ = successful_animator_count_ == kNumAnimators; | |
78 } | |
79 | |
80 successful_animator_count_ = 0; | |
81 | |
82 opaque_background_animator_->SetPaintsBackground(show_background, | |
83 change_type); | |
84 asset_background_animator_->SetPaintsBackground(show_background, change_type); | |
85 item_background_animator_->SetPaintsBackground(show_background, change_type); | |
86 | |
87 if (target_background_type_ != background_type) { | |
88 previous_background_type_ = target_background_type_; | |
89 target_background_type_ = background_type; | |
90 } | |
91 | |
92 has_animated_once_ = true; | |
93 } | |
94 | |
95 void ShelfBackgroundAnimator::OnBackgroundUpdated( | |
James Cook
2016/06/14 17:50:01
Per earlier comment about notifications - are you
bruthig
2016/07/26 19:50:02
Yeah, this is ok, the ShelfBackgroundAnimator only
| |
96 ShelfBackgroundType background_type, | |
97 BackgroundAnimatorChangeType change_type) { | |
98 PaintBackground(background_type, change_type); | |
99 } | |
100 | |
101 void ShelfBackgroundAnimator::UpdateBackground(BackgroundAnimator* animator, | |
102 int alpha) { | |
103 if (animator == opaque_background_animator_.get()) { | |
104 FOR_EACH_OBSERVER(ShelfBackgroundAnimatorObserver, observers_, | |
105 UpdateShelfOpaqueBackground(alpha)); | |
106 previous_opaque_background_alpha_ = alpha; | |
107 } else if (animator == asset_background_animator_.get()) { | |
108 FOR_EACH_OBSERVER(ShelfBackgroundAnimatorObserver, observers_, | |
109 UpdateShelfAssetBackground(alpha)); | |
110 previous_asset_background_alpha_ = alpha; | |
111 } else if (animator == item_background_animator_.get()) { | |
112 FOR_EACH_OBSERVER(ShelfBackgroundAnimatorObserver, observers_, | |
113 UpdateShelfItemBackground(alpha)); | |
114 previous_item_background_alpha_ = alpha; | |
115 } else { | |
116 NOTREACHED(); | |
117 } | |
118 } | |
119 | |
120 void ShelfBackgroundAnimator::BackgroundAnimationEnded( | |
121 BackgroundAnimator* animator, | |
122 bool successful) { | |
123 if (successful) { | |
124 ++successful_animator_count_; | |
125 DCHECK_LE(successful_animator_count_, kNumAnimators); | |
126 } | |
127 } | |
128 | |
129 void ShelfBackgroundAnimator::CreateAnimators( | |
130 ShelfBackgroundType background_type, | |
131 BackgroundAnimatorChangeType change_type) { | |
132 const bool is_material = MaterialDesignController::IsShelfMaterial(); | |
133 | |
134 switch (background_type) { | |
135 case SHELF_BACKGROUND_DEFAULT: | |
136 opaque_background_animator_.reset( | |
137 new BackgroundAnimator(this, previous_opaque_background_alpha_, 0)); | |
138 asset_background_animator_.reset( | |
139 new BackgroundAnimator(this, previous_asset_background_alpha_, 0)); | |
140 item_background_animator_.reset( | |
141 new BackgroundAnimator(this, previous_item_background_alpha_, | |
142 GetShelfConstant(SHELF_BACKGROUND_ALPHA))); | |
143 break; | |
144 case SHELF_BACKGROUND_OVERLAP: | |
145 opaque_background_animator_.reset(new BackgroundAnimator( | |
146 this, previous_opaque_background_alpha_, | |
147 is_material ? GetShelfConstant(SHELF_BACKGROUND_ALPHA) : 0)); | |
148 asset_background_animator_.reset(new BackgroundAnimator( | |
149 this, previous_asset_background_alpha_, | |
150 is_material ? 0 : GetShelfConstant(SHELF_BACKGROUND_ALPHA))); | |
151 item_background_animator_.reset(new BackgroundAnimator( | |
152 this, previous_item_background_alpha_, | |
153 is_material ? 0 : GetShelfConstant(SHELF_BACKGROUND_ALPHA))); | |
154 break; | |
155 case SHELF_BACKGROUND_MAXIMIZED: | |
156 opaque_background_animator_.reset(new BackgroundAnimator( | |
157 this, previous_opaque_background_alpha_, kMaxAlpha)); | |
158 asset_background_animator_.reset(new BackgroundAnimator( | |
159 this, previous_asset_background_alpha_, | |
160 is_material ? 0 : GetShelfConstant(SHELF_BACKGROUND_ALPHA))); | |
161 item_background_animator_.reset(new BackgroundAnimator( | |
162 this, previous_item_background_alpha_, is_material ? 0 : kMaxAlpha)); | |
163 break; | |
164 } | |
165 } | |
166 | |
167 void ShelfBackgroundAnimator::StopAnimators() { | |
168 if (opaque_background_animator_) | |
169 opaque_background_animator_->Stop(); | |
170 if (asset_background_animator_) | |
171 asset_background_animator_->Stop(); | |
172 if (item_background_animator_) | |
173 item_background_animator_->Stop(); | |
174 } | |
175 | |
176 void ShelfBackgroundAnimator::ConfigureAnimators( | |
177 ShelfBackgroundType background_type, | |
178 BackgroundAnimatorChangeType change_type) { | |
179 const bool is_material = MaterialDesignController::IsShelfMaterial(); | |
180 int duration_ms = 0; | |
181 if (change_type != BACKGROUND_CHANGE_IMMEDIATE) { | |
182 switch (background_type) { | |
183 case SHELF_BACKGROUND_DEFAULT: | |
184 duration_ms = is_material ? 500 : 1000; | |
185 break; | |
186 case SHELF_BACKGROUND_OVERLAP: | |
187 duration_ms = is_material ? 500 : 1000; | |
188 break; | |
189 case SHELF_BACKGROUND_MAXIMIZED: | |
190 duration_ms = is_material ? 250 : 1000; | |
191 break; | |
192 } | |
193 } | |
194 | |
195 opaque_background_animator_->SetDuration(duration_ms); | |
196 asset_background_animator_->SetDuration(duration_ms); | |
197 item_background_animator_->SetDuration(duration_ms); | |
198 } | |
199 | |
200 } // namespace ash | |
OLD | NEW |