Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: ash/common/shelf/shelf_background_animator.cc

Issue 2191363002: Reland of "Apply Shelf/Dock opacity animations for the Material Design style." (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added ShelfBackgoundAnimator::Initialize(ShelfBackgroundAnimatorObserver*). Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "ash/common/shelf/wm_shelf.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 ShelfBackgroundType background_type,
25 WmShelf* wm_shelf)
26 : wm_shelf_(wm_shelf) {
27 if (wm_shelf_)
28 wm_shelf_->AddObserver(this);
29 // Initialize animators so that adding observers get notified with consistent
30 // values.
31 AnimateBackground(background_type, BACKGROUND_CHANGE_IMMEDIATE);
32 }
33
34 ShelfBackgroundAnimator::~ShelfBackgroundAnimator() {
35 if (wm_shelf_)
36 wm_shelf_->RemoveObserver(this);
37 }
38
39 void ShelfBackgroundAnimator::AddObserver(
40 ShelfBackgroundAnimatorObserver* observer) {
41 observers_.AddObserver(observer);
42 Initialize(observer);
43 }
44
45 void ShelfBackgroundAnimator::RemoveObserver(
46 ShelfBackgroundAnimatorObserver* observer) {
47 observers_.RemoveObserver(observer);
48 }
49
50 void ShelfBackgroundAnimator::Initialize(
51 ShelfBackgroundAnimatorObserver* observer) const {
52 observer->UpdateShelfOpaqueBackground(opaque_background_animator_->alpha());
53 observer->UpdateShelfAssetBackground(asset_background_animator_->alpha());
54 observer->UpdateShelfItemBackground(item_background_animator_->alpha());
55 }
56
57 void ShelfBackgroundAnimator::PaintBackground(
58 ShelfBackgroundType background_type,
59 BackgroundAnimatorChangeType change_type) {
60 if (target_background_type_ == background_type &&
61 change_type == BACKGROUND_CHANGE_ANIMATE) {
62 return;
63 }
64
65 AnimateBackground(background_type, change_type);
66 }
67
68 void ShelfBackgroundAnimator::OnBackgroundTypeChanged(
69 ShelfBackgroundType background_type,
70 BackgroundAnimatorChangeType change_type) {
71 PaintBackground(background_type, change_type);
72 }
73
74 void ShelfBackgroundAnimator::UpdateBackground(BackgroundAnimator* animator,
75 int alpha) {
76 OnAlphaChanged(animator, alpha);
77 }
78
79 void ShelfBackgroundAnimator::BackgroundAnimationEnded(
80 BackgroundAnimator* animator) {
81 ++successful_animator_count_;
82 DCHECK_LE(successful_animator_count_, kNumAnimators);
83 // UpdateBackground() is only called when alpha values change, this ensures
84 // observers are always notified for every background change.
85 OnAlphaChanged(animator, animator->alpha());
86 }
87
88 void ShelfBackgroundAnimator::OnAlphaChanged(BackgroundAnimator* animator,
89 int alpha) {
90 if (animator == opaque_background_animator_.get()) {
91 FOR_EACH_OBSERVER(ShelfBackgroundAnimatorObserver, observers_,
92 UpdateShelfOpaqueBackground(alpha));
93 } else if (animator == asset_background_animator_.get()) {
94 FOR_EACH_OBSERVER(ShelfBackgroundAnimatorObserver, observers_,
95 UpdateShelfAssetBackground(alpha));
96 } else if (animator == item_background_animator_.get()) {
97 FOR_EACH_OBSERVER(ShelfBackgroundAnimatorObserver, observers_,
98 UpdateShelfItemBackground(alpha));
99 } else {
100 NOTREACHED();
101 }
102 }
103
104 void ShelfBackgroundAnimator::AnimateBackground(
105 ShelfBackgroundType background_type,
106 BackgroundAnimatorChangeType change_type) {
107 // Ensure BackgroundAnimationEnded() has been called for all the
108 // BackgroundAnimators owned by this so that |successful_animator_count_|
109 // is stable and doesn't get updated as a side effect of destroying/animating
110 // the animators.
111 StopAnimators();
112
113 bool show_background = true;
114 if (can_reuse_animators_ && previous_background_type_ == background_type) {
115 DCHECK_EQ(opaque_background_animator_->paints_background(),
116 asset_background_animator_->paints_background());
117 DCHECK_EQ(asset_background_animator_->paints_background(),
118 item_background_animator_->paints_background());
119
120 show_background = !opaque_background_animator_->paints_background();
121 } else {
122 CreateAnimators(background_type, change_type);
123
124 // If all the previous animators completed successfully and the animation
125 // was between 2 distinct states, then the last alpha values are valid
126 // end state values.
127 can_reuse_animators_ = target_background_type_ != background_type &&
128 successful_animator_count_ == kNumAnimators;
129 }
130
131 successful_animator_count_ = 0;
132
133 opaque_background_animator_->SetPaintsBackground(show_background,
134 change_type);
135 asset_background_animator_->SetPaintsBackground(show_background, change_type);
136 item_background_animator_->SetPaintsBackground(show_background, change_type);
137
138 if (target_background_type_ != background_type) {
139 previous_background_type_ = target_background_type_;
140 target_background_type_ = background_type;
141 }
142 }
143
144 void ShelfBackgroundAnimator::CreateAnimators(
145 ShelfBackgroundType background_type,
146 BackgroundAnimatorChangeType change_type) {
147 const int opaque_background_alpha =
148 opaque_background_animator_ ? opaque_background_animator_->alpha() : 0;
149 const int asset_background_alpha =
150 asset_background_animator_ ? asset_background_animator_->alpha() : 0;
151 const int item_background_alpha =
152 item_background_animator_ ? item_background_animator_->alpha() : 0;
153
154 const bool is_material = MaterialDesignController::IsShelfMaterial();
155 int duration_ms = 0;
156
157 switch (background_type) {
158 case SHELF_BACKGROUND_DEFAULT:
159 duration_ms = is_material ? 500 : 1000;
160 opaque_background_animator_.reset(
161 new BackgroundAnimator(this, opaque_background_alpha, 0));
162 asset_background_animator_.reset(
163 new BackgroundAnimator(this, asset_background_alpha, 0));
164 item_background_animator_.reset(
165 new BackgroundAnimator(this, item_background_alpha,
166 GetShelfConstant(SHELF_BACKGROUND_ALPHA)));
167 break;
168 case SHELF_BACKGROUND_OVERLAP:
169 duration_ms = is_material ? 500 : 1000;
170 opaque_background_animator_.reset(new BackgroundAnimator(
171 this, opaque_background_alpha,
172 is_material ? GetShelfConstant(SHELF_BACKGROUND_ALPHA) : 0));
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,
178 is_material ? 0 : GetShelfConstant(SHELF_BACKGROUND_ALPHA)));
179 break;
180 case SHELF_BACKGROUND_MAXIMIZED:
181 duration_ms = is_material ? 250 : 1000;
182 opaque_background_animator_.reset(
183 new BackgroundAnimator(this, opaque_background_alpha, kMaxAlpha));
184 asset_background_animator_.reset(new BackgroundAnimator(
185 this, asset_background_alpha,
186 is_material ? 0 : GetShelfConstant(SHELF_BACKGROUND_ALPHA)));
187 item_background_animator_.reset(new BackgroundAnimator(
188 this, item_background_alpha, is_material ? 0 : kMaxAlpha));
189 break;
190 }
191
192 opaque_background_animator_->SetDuration(duration_ms);
193 asset_background_animator_->SetDuration(duration_ms);
194 item_background_animator_->SetDuration(duration_ms);
195 }
196
197 void ShelfBackgroundAnimator::StopAnimators() {
198 if (opaque_background_animator_)
199 opaque_background_animator_->Stop();
200 if (asset_background_animator_)
201 asset_background_animator_->Stop();
202 if (item_background_animator_)
203 item_background_animator_->Stop();
204 }
205
206 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/shelf/shelf_background_animator.h ('k') | ash/common/shelf/shelf_background_animator_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698