OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/ui/search/toolbar_search_animator.h" |
| 6 |
| 7 #include "chrome/browser/ui/search/search_model.h" |
| 8 #include "chrome/browser/ui/search/search_types.h" |
| 9 #include "chrome/browser/ui/search/toolbar_search_animator_observer.h" |
| 10 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 11 #include "ui/base/animation/slide_animation.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 const int kBackgroundChangeDelayMs = 100; |
| 16 const int kBackgroundChangeDurationMs = 200; |
| 17 |
| 18 const double kMinOpacity = 0.0f; |
| 19 const double kMaxOpacity = 1.0f; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 namespace chrome { |
| 24 namespace search { |
| 25 |
| 26 ToolbarSearchAnimator::ToolbarSearchAnimator(SearchModel* search_model) |
| 27 : search_model_(search_model), |
| 28 animate_state_(ANIMATE_STATE_NONE) { |
| 29 search_model_->AddObserver(this); |
| 30 } |
| 31 |
| 32 ToolbarSearchAnimator::~ToolbarSearchAnimator() { |
| 33 search_model_->RemoveObserver(this); |
| 34 } |
| 35 |
| 36 void ToolbarSearchAnimator::GetCurrentBackgroundState( |
| 37 BackgroundState* background_state, |
| 38 double* search_background_opacity) const { |
| 39 // Should only be called for SEARCH mode. |
| 40 DCHECK(search_model_->mode().is_search()); |
| 41 *background_state = BACKGROUND_STATE_DEFAULT; |
| 42 *search_background_opacity = -1.0f; |
| 43 switch (animate_state_) { |
| 44 case ANIMATE_STATE_WAITING: |
| 45 *background_state = BACKGROUND_STATE_NTP; |
| 46 break; |
| 47 case ANIMATE_STATE_RUNNING: |
| 48 *background_state = BACKGROUND_STATE_NTP_SEARCH; |
| 49 *search_background_opacity = background_animation_->CurrentValueBetween( |
| 50 kMinOpacity, kMaxOpacity); |
| 51 break; |
| 52 case ANIMATE_STATE_NONE: |
| 53 break; |
| 54 } |
| 55 } |
| 56 |
| 57 void ToolbarSearchAnimator::FinishAnimation(TabContents* tab_contents) { |
| 58 Reset(tab_contents); |
| 59 } |
| 60 |
| 61 void ToolbarSearchAnimator::AddObserver( |
| 62 ToolbarSearchAnimatorObserver* observer) { |
| 63 observers_.AddObserver(observer); |
| 64 } |
| 65 |
| 66 void ToolbarSearchAnimator::RemoveObserver( |
| 67 ToolbarSearchAnimatorObserver* observer) { |
| 68 observers_.RemoveObserver(observer); |
| 69 } |
| 70 |
| 71 void ToolbarSearchAnimator::ModeChanged(const Mode& mode) { |
| 72 if (mode.is_search() && mode.animate && |
| 73 animate_state_ == ANIMATE_STATE_NONE) { |
| 74 background_change_timer_.Start( |
| 75 FROM_HERE, |
| 76 base::TimeDelta::FromMilliseconds(kBackgroundChangeDelayMs), |
| 77 this, |
| 78 &ToolbarSearchAnimator::StartBackgroundChange); |
| 79 animate_state_ = ANIMATE_STATE_WAITING; |
| 80 return; |
| 81 } |
| 82 // For all other cases, reset |animate_state_| and stop timer or animation. |
| 83 // Stopping animation will jump to the end of it. |
| 84 Reset(NULL); |
| 85 } |
| 86 |
| 87 void ToolbarSearchAnimator::AnimationProgressed( |
| 88 const ui::Animation* animation) { |
| 89 DCHECK_EQ(animation, background_animation_.get()); |
| 90 animate_state_ = ANIMATE_STATE_RUNNING; |
| 91 FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
| 92 OnToolbarBackgroundAnimatorProgressed()); |
| 93 } |
| 94 |
| 95 void ToolbarSearchAnimator::AnimationEnded(const ui::Animation* animation) { |
| 96 DCHECK_EQ(animation, background_animation_.get()); |
| 97 // Only notify observers via OnToolbarBackgroundAnimatorProgressed if the |
| 98 // animation has runs its full course i.e |animate_state_| is still |
| 99 // ANIMATE_STATE_RUNNING. |
| 100 // Animation that is canceled, i.e. |animate_state_| has been set to |
| 101 // ANIMATE_STATE_NONE in Reset, should notify observers via |
| 102 // OnToolbarBackgroundAnimatorCanceled. |
| 103 if (animate_state_ == ANIMATE_STATE_RUNNING) { |
| 104 animate_state_ = ANIMATE_STATE_NONE; |
| 105 FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
| 106 OnToolbarBackgroundAnimatorProgressed()); |
| 107 } |
| 108 } |
| 109 |
| 110 void ToolbarSearchAnimator::StartBackgroundChange() { |
| 111 if (!background_animation_.get()) { |
| 112 background_animation_.reset(new ui::SlideAnimation(this)); |
| 113 background_animation_->SetTweenType(ui::Tween::LINEAR); |
| 114 background_animation_->SetSlideDuration(kBackgroundChangeDurationMs); |
| 115 } |
| 116 background_animation_->Reset(0.0); |
| 117 background_animation_->Show(); |
| 118 } |
| 119 |
| 120 void ToolbarSearchAnimator::Reset(TabContents* tab_contents) { |
| 121 bool notify_observers = animate_state_ != ANIMATE_STATE_NONE; |
| 122 animate_state_ = ANIMATE_STATE_NONE; |
| 123 background_change_timer_.Stop(); |
| 124 // If animation is still running, stopping it will trigger AnimationEnded |
| 125 // where we've prevented from notifying observers via BackgroundChanged; |
| 126 // see comments in AnimationEnded. |
| 127 if (background_animation_.get()) |
| 128 background_animation_->Stop(); |
| 129 if (notify_observers) { |
| 130 FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
| 131 OnToolbarBackgroundAnimatorCanceled(tab_contents)); |
| 132 } |
| 133 } |
| 134 |
| 135 } // namespace search |
| 136 } // namespace chrome |
OLD | NEW |