Chromium Code Reviews| Index: ash/common/shelf/shelf_background_animator.cc |
| diff --git a/ash/common/shelf/shelf_background_animator.cc b/ash/common/shelf/shelf_background_animator.cc |
| index 2ebf9f012b950117412c3322505c5554cf26cebb..d0aad30691bdb8f4721f88e211b61a910b426597 100644 |
| --- a/ash/common/shelf/shelf_background_animator.cc |
| +++ b/ash/common/shelf/shelf_background_animator.cc |
| @@ -10,15 +10,24 @@ |
| #include "ash/common/shelf/shelf_background_animator_observer.h" |
| #include "ash/common/shelf/shelf_constants.h" |
| #include "ash/common/shelf/wm_shelf.h" |
| +#include "ui/gfx/animation/slide_animation.h" |
| namespace ash { |
| -namespace { |
| -// The total number of animators that will call BackgroundAnimationEnded(). |
| -const int kNumAnimators = 3; |
| +const int ShelfBackgroundAnimator::kMaxAlpha = 255; |
|
James Cook
2017/02/08 00:31:27
Can this be initialized in header?
bruthig
2017/02/10 18:49:09
Done.
|
| -const int kMaxAlpha = 255; |
| -} // namespace |
| +void ShelfBackgroundAnimator::AnimationValues::UpdateCurrentValues(double t) { |
| + current_alpha_ = |
| + gfx::Tween::IntValueBetween(t, initial_alpha_, target_alpha_); |
| +} |
| + |
| +void ShelfBackgroundAnimator::AnimationValues::SetTargetValues( |
| + int target_alpha) { |
| + DCHECK_LE(target_alpha, kMaxAlpha); |
| + DCHECK_GE(target_alpha, 0); |
| + initial_alpha_ = current_alpha_; |
| + target_alpha_ = target_alpha; |
| +} |
| ShelfBackgroundAnimator::ShelfBackgroundAnimator( |
| ShelfBackgroundType background_type, |
| @@ -28,7 +37,7 @@ ShelfBackgroundAnimator::ShelfBackgroundAnimator( |
| wm_shelf_->AddObserver(this); |
| // Initialize animators so that adding observers get notified with consistent |
| // values. |
| - AnimateBackground(background_type, BACKGROUND_CHANGE_IMMEDIATE); |
| + AnimateBackground(background_type, gfx::ANIMATION_CHANGE_IMMEDIATE); |
| } |
| ShelfBackgroundAnimator::~ShelfBackgroundAnimator() { |
| @@ -39,7 +48,7 @@ ShelfBackgroundAnimator::~ShelfBackgroundAnimator() { |
| void ShelfBackgroundAnimator::AddObserver( |
| ShelfBackgroundAnimatorObserver* observer) { |
| observers_.AddObserver(observer); |
| - Initialize(observer); |
| + NotifyObserver(observer); |
| } |
| void ShelfBackgroundAnimator::RemoveObserver( |
| @@ -47,140 +56,121 @@ void ShelfBackgroundAnimator::RemoveObserver( |
| observers_.RemoveObserver(observer); |
| } |
| -void ShelfBackgroundAnimator::Initialize( |
| - ShelfBackgroundAnimatorObserver* observer) const { |
| - observer->UpdateShelfOpaqueBackground(opaque_background_animator_->alpha()); |
| - observer->UpdateShelfItemBackground(item_background_animator_->alpha()); |
| +void ShelfBackgroundAnimator::NotifyObserver( |
| + ShelfBackgroundAnimatorObserver* observer) { |
| + observer->UpdateShelfBackground(shelf_background_values_.current_alpha()); |
| + observer->UpdateShelfItemBackground(item_background_values_.current_alpha()); |
| } |
| void ShelfBackgroundAnimator::PaintBackground( |
| ShelfBackgroundType background_type, |
| - BackgroundAnimatorChangeType change_type) { |
| + gfx::AnimationChangeType change_type) { |
| if (target_background_type_ == background_type && |
| - change_type == BACKGROUND_CHANGE_ANIMATE) { |
| + change_type == gfx::ANIMATION_CHANGE_ANIMATE) { |
| return; |
| } |
| AnimateBackground(background_type, change_type); |
| } |
| -void ShelfBackgroundAnimator::OnBackgroundTypeChanged( |
| - ShelfBackgroundType background_type, |
| - BackgroundAnimatorChangeType change_type) { |
| - PaintBackground(background_type, change_type); |
| +void ShelfBackgroundAnimator::AnimationProgressed( |
| + const gfx::Animation* animation) { |
| + shelf_background_values_.UpdateCurrentValues(animation->GetCurrentValue()); |
| + item_background_values_.UpdateCurrentValues(animation->GetCurrentValue()); |
| + NotifyObservers(); |
| } |
| -void ShelfBackgroundAnimator::UpdateBackground(BackgroundAnimator* animator, |
| - int alpha) { |
| - OnAlphaChanged(animator, alpha); |
| +void ShelfBackgroundAnimator::AnimationEnded(const gfx::Animation* animation) { |
| + shelf_background_values_.UpdateCurrentValues(animation->GetCurrentValue()); |
| + item_background_values_.UpdateCurrentValues(animation->GetCurrentValue()); |
| + NotifyObservers(); |
| } |
| -void ShelfBackgroundAnimator::BackgroundAnimationEnded( |
| - BackgroundAnimator* animator) { |
| - ++successful_animator_count_; |
| - DCHECK_LE(successful_animator_count_, kNumAnimators); |
| - // UpdateBackground() is only called when alpha values change, this ensures |
| - // observers are always notified for every background change. |
| - OnAlphaChanged(animator, animator->alpha()); |
| +void ShelfBackgroundAnimator::OnBackgroundTypeChanged( |
| + ShelfBackgroundType background_type, |
| + gfx::AnimationChangeType change_type) { |
| + PaintBackground(background_type, change_type); |
| } |
| -void ShelfBackgroundAnimator::OnAlphaChanged(BackgroundAnimator* animator, |
| - int alpha) { |
| - if (animator == opaque_background_animator_.get()) { |
| - for (auto& observer : observers_) |
| - observer.UpdateShelfOpaqueBackground(alpha); |
| - } else if (animator == item_background_animator_.get()) { |
| - for (auto& observer : observers_) |
| - observer.UpdateShelfItemBackground(alpha); |
| - } else { |
| - NOTREACHED(); |
| - } |
| +void ShelfBackgroundAnimator::NotifyObservers() { |
| + for (auto& observer : observers_) |
| + NotifyObserver(&observer); |
| } |
| void ShelfBackgroundAnimator::AnimateBackground( |
| ShelfBackgroundType background_type, |
| - BackgroundAnimatorChangeType change_type) { |
| - // Ensure BackgroundAnimationEnded() has been called for all the |
| - // BackgroundAnimators owned by this so that |successful_animator_count_| |
| - // is stable and doesn't get updated as a side effect of destroying/animating |
| - // the animators. |
| - StopAnimators(); |
| - |
| - bool show_background = true; |
| - if (can_reuse_animators_ && previous_background_type_ == background_type) { |
| - DCHECK_EQ(opaque_background_animator_->paints_background(), |
| - item_background_animator_->paints_background()); |
| - |
| - show_background = !opaque_background_animator_->paints_background(); |
| + gfx::AnimationChangeType change_type) { |
| + const bool was_animating = animator_ && animator_->is_animating(); |
| + StopAnimator(); |
| + |
| + const bool change_immediately = |
| + change_type == gfx::ANIMATION_CHANGE_IMMEDIATE; |
| + if (can_reuse_animator_ && previous_background_type_ == background_type) { |
| + if (animator_->IsShowing()) { |
|
James Cook
2017/02/08 00:31:27
You're checking for null animator_ on line 103. Ca
bruthig
2017/02/10 18:49:09
Comment added, WDYT?
|
| + if (change_immediately) |
| + animator_->HideImmediately(); |
| + else |
| + animator_->Hide(); |
| + } else { |
| + if (change_immediately) |
| + animator_->ShowImmediately(); |
| + else |
| + animator_->Show(); |
| + } |
| } else { |
| - CreateAnimators(background_type, change_type); |
| - |
| - // If all the previous animators completed successfully and the animation |
| - // was between 2 distinct states, then the last alpha values are valid |
| - // end state values. |
| - can_reuse_animators_ = target_background_type_ != background_type && |
| - successful_animator_count_ == kNumAnimators; |
| + // If the previous animator completed successfully and the animation was |
| + // between 2 distinct states, then the last values are valid end state |
| + // values. |
| + can_reuse_animator_ = |
| + target_background_type_ != background_type && !was_animating; |
| + |
| + CreateAnimator(background_type); |
| + |
| + if (change_immediately) |
| + animator_->ShowImmediately(); |
| + else |
| + animator_->Show(); |
| } |
| - successful_animator_count_ = 0; |
| - |
| - opaque_background_animator_->SetPaintsBackground(show_background, |
| - change_type); |
| - item_background_animator_->SetPaintsBackground(show_background, change_type); |
| - |
| if (target_background_type_ != background_type) { |
| previous_background_type_ = target_background_type_; |
| target_background_type_ = background_type; |
| } |
| } |
| -void ShelfBackgroundAnimator::CreateAnimators( |
| - ShelfBackgroundType background_type, |
| - BackgroundAnimatorChangeType change_type) { |
| - const int opaque_background_alpha = |
| - opaque_background_animator_ ? opaque_background_animator_->alpha() : 0; |
| - const int item_background_alpha = |
| - item_background_animator_ ? item_background_animator_->alpha() : 0; |
| - |
| - const bool is_material = MaterialDesignController::IsShelfMaterial(); |
| +void ShelfBackgroundAnimator::CreateAnimator( |
| + ShelfBackgroundType background_type) { |
| int duration_ms = 0; |
| + int target_shelf_background = 0; |
| + int target_shelf_item_background = 0; |
| switch (background_type) { |
| case SHELF_BACKGROUND_DEFAULT: |
| - duration_ms = is_material ? 500 : 1000; |
| - opaque_background_animator_.reset( |
| - new BackgroundAnimator(this, opaque_background_alpha, 0)); |
| - item_background_animator_.reset( |
| - new BackgroundAnimator(this, item_background_alpha, |
| - GetShelfConstant(SHELF_BACKGROUND_ALPHA))); |
| + duration_ms = 500; |
| + target_shelf_background = 0; |
| + target_shelf_item_background = kShelfTranslucentAlpha; |
| break; |
| case SHELF_BACKGROUND_OVERLAP: |
| - duration_ms = is_material ? 500 : 1000; |
| - opaque_background_animator_.reset(new BackgroundAnimator( |
| - this, opaque_background_alpha, |
| - is_material ? GetShelfConstant(SHELF_BACKGROUND_ALPHA) : 0)); |
| - item_background_animator_.reset(new BackgroundAnimator( |
| - this, item_background_alpha, |
| - is_material ? 0 : GetShelfConstant(SHELF_BACKGROUND_ALPHA))); |
| + duration_ms = 500; |
| + target_shelf_background = kShelfTranslucentAlpha; |
| + target_shelf_item_background = 0; |
| break; |
| case SHELF_BACKGROUND_MAXIMIZED: |
| - duration_ms = is_material ? 250 : 1000; |
| - opaque_background_animator_.reset( |
| - new BackgroundAnimator(this, opaque_background_alpha, kMaxAlpha)); |
| - item_background_animator_.reset(new BackgroundAnimator( |
| - this, item_background_alpha, is_material ? 0 : kMaxAlpha)); |
| + duration_ms = 250; |
| + target_shelf_background = kMaxAlpha; |
| + target_shelf_item_background = 0; |
| break; |
| } |
| - opaque_background_animator_->SetDuration(duration_ms); |
| - item_background_animator_->SetDuration(duration_ms); |
| + animator_.reset(new gfx::SlideAnimation(this)); |
| + animator_->SetSlideDuration(duration_ms); |
| + shelf_background_values_.SetTargetValues(target_shelf_background); |
| + item_background_values_.SetTargetValues(target_shelf_item_background); |
| } |
| -void ShelfBackgroundAnimator::StopAnimators() { |
| - if (opaque_background_animator_) |
| - opaque_background_animator_->Stop(); |
| - if (item_background_animator_) |
| - item_background_animator_->Stop(); |
| +void ShelfBackgroundAnimator::StopAnimator() { |
| + if (animator_) |
| + animator_->Stop(); |
| } |
| } // namespace ash |