Chromium Code Reviews| Index: chrome/browser/ui/search/toolbar_search_animator.cc |
| diff --git a/chrome/browser/ui/search/toolbar_search_animator.cc b/chrome/browser/ui/search/toolbar_search_animator.cc |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..25e00f3d622acf998505a867aa0cfc7888d06ce0 |
| --- /dev/null |
| +++ b/chrome/browser/ui/search/toolbar_search_animator.cc |
| @@ -0,0 +1,136 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/search/toolbar_search_animator.h" |
| + |
| +#include "chrome/browser/ui/search/search_model.h" |
| +#include "chrome/browser/ui/search/search_types.h" |
| +#include "chrome/browser/ui/search/toolbar_search_animator_observer.h" |
| +#include "chrome/browser/ui/tab_contents/tab_contents.h" |
| +#include "ui/base/animation/slide_animation.h" |
| + |
| +namespace { |
| + |
| +const int kBackgroundChangeDelayMs = 100; |
| +const int kBackgroundChangeDurationMs = 200; |
| + |
| +const int kMinOpacity = 0; |
|
dhollowa
2012/06/26 18:16:27
As per sky's comment, these should be floats from
kuan
2012/06/26 23:25:49
Done.
|
| +const int kMaxOpacity = 0xFF; |
| + |
| +} // namespace |
| + |
| +namespace chrome { |
| +namespace search { |
| + |
| +ToolbarSearchAnimator::ToolbarSearchAnimator(SearchModel* search_model) |
| + : search_model_(search_model), |
| + animate_state_(ANIMATE_STATE_NONE) { |
| + search_model_->AddObserver(this); |
| +} |
| + |
| +ToolbarSearchAnimator::~ToolbarSearchAnimator() { |
| + Reset(false /* don't notify observers */, NULL); |
|
sky
2012/06/26 17:11:27
Why do you need to reset in the destructor?
kuan
2012/06/26 23:25:49
i did it to stop timer and animation, but i guess
sky
2012/06/27 00:06:17
That's right.
|
| + search_model_->RemoveObserver(this); |
| +} |
| + |
| +void ToolbarSearchAnimator::GetCurrentBackgroundState( |
| + BackgroundState* background_state, |
| + int* new_background_opacity) const { |
| + // Should only be called for SEARCH mode. |
| + DCHECK(search_model_->mode().is_search()); |
| + *background_state = BACKGROUND_STATE_SHOW_NON_APPLICABLE; |
| + *new_background_opacity = -1; |
| + switch (animate_state_) { |
| + case ANIMATE_STATE_WAITING: |
| + *background_state = BACKGROUND_STATE_SHOW_NTP; |
| + break; |
| + case ANIMATE_STATE_RUNNING: |
| + *background_state = BACKGROUND_STATE_SHOW_NTP_SEARCH; |
| + *new_background_opacity = background_animation_->CurrentValueBetween( |
| + kMinOpacity, kMaxOpacity); |
| + break; |
| + case ANIMATE_STATE_NONE: |
| + break; |
| + } |
| +} |
| + |
| +void ToolbarSearchAnimator::FinishAnimation(TabContents* tab_contents) { |
| + Reset(true /* notify observers */, tab_contents); |
| +} |
| + |
| +void ToolbarSearchAnimator::AddObserver( |
| + ToolbarSearchAnimatorObserver* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| + |
| +void ToolbarSearchAnimator::RemoveObserver( |
| + ToolbarSearchAnimatorObserver* observer) { |
| + observers_.RemoveObserver(observer); |
| +} |
| + |
| +void ToolbarSearchAnimator::ModeChanged(const Mode& mode) { |
| + if (mode.is_search() && mode.animate && |
| + animate_state_ == ANIMATE_STATE_NONE) { |
| + background_change_timer_.Start( |
| + FROM_HERE, |
| + base::TimeDelta::FromMilliseconds(kBackgroundChangeDelayMs), |
| + this, |
| + &ToolbarSearchAnimator::StartBackgroundChange); |
| + animate_state_ = ANIMATE_STATE_WAITING; |
| + return; |
| + } |
| + // For all other cases, reset |animate_state_| and stop timer or animation. |
| + // Stopping animation will jump to the end of it. |
| + Reset(true /* notify observers */, NULL); |
| +} |
| + |
| +void ToolbarSearchAnimator::AnimationProgressed( |
| + const ui::Animation* animation) { |
| + DCHECK(animation == background_animation_.get()); |
|
sky
2012/06/26 17:11:27
DCHECK_EQ
kuan
2012/06/26 23:25:49
Done.
|
| + animate_state_ = ANIMATE_STATE_RUNNING; |
| + FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
| + BackgroundChanging()); |
| +} |
| + |
| +void ToolbarSearchAnimator::AnimationEnded(const ui::Animation* animation) { |
| + DCHECK(animation == background_animation_.get()); |
|
sky
2012/06/26 17:11:27
DCHECK_EG
kuan
2012/06/26 23:25:49
Done.
|
| + // Only notify observers via BackgroundChange if the animation has runs its |
| + // full course i.e |animate_state_| is still ANIMATE_STATE_RUNNING. |
| + // Animation that is canceled, i.e. |animate_state_| has been set to |
| + // ANIMATE_STATE_NONE in Reset, should notify observers via |
| + // BackgroundChangeCanceled. |
| + if (animate_state_ == ANIMATE_STATE_RUNNING) { |
| + animate_state_ = ANIMATE_STATE_NONE; |
| + FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
| + BackgroundChanged()); |
| + } |
| +} |
| + |
| +void ToolbarSearchAnimator::StartBackgroundChange() { |
| + if (!background_animation_.get()) { |
| + background_animation_.reset(new ui::SlideAnimation(this)); |
| + background_animation_->SetTweenType(ui::Tween::LINEAR); |
| + background_animation_->SetSlideDuration(kBackgroundChangeDurationMs); |
| + } |
| + background_animation_->Reset(0.0); |
| + background_animation_->Show(); |
| +} |
| + |
| +void ToolbarSearchAnimator::Reset(bool notify_observers, |
| + TabContents* tab_contents) { |
| + animate_state_ = ANIMATE_STATE_NONE; |
| + background_change_timer_.Stop(); |
| + // If animation is still running, stopping it will trigger AnimationEnded |
| + // where we've prevented from notifying observers via BackgroundChanged; |
| + // see comments in AnimationEnded. |
| + if (background_animation_.get()) |
| + background_animation_->Stop(); |
| + if (notify_observers) { |
| + FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
| + BackgroundChangeCanceled(tab_contents)); |
| + } |
| +} |
| + |
| +} // namespace search |
| +} // namespace chrome |