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 |
index c8160fb047e1318caf60f21b74858725baffa66d..998f8ead94fba380df7bf484465b62a8e260751d 100644 |
--- a/chrome/browser/ui/search/toolbar_search_animator.cc |
+++ b/chrome/browser/ui/search/toolbar_search_animator.cc |
@@ -9,6 +9,7 @@ |
#include "chrome/browser/ui/search/toolbar_search_animator_observer.h" |
#include "chrome/browser/ui/tab_contents/tab_contents.h" |
#include "chrome/browser/ui/webui/instant_ui.h" |
+#include "ui/base/animation/multi_animation.h" |
#include "ui/base/animation/slide_animation.h" |
namespace { |
@@ -16,6 +17,8 @@ namespace { |
const int kBackgroundChangeDelayMs = 100; |
const int kBackgroundChangeDurationMs = 200; |
+const int kSeparatorFadeDurationMs = 100; |
+ |
const double kMinOpacity = 0.0f; |
const double kMaxOpacity = 1.0f; |
@@ -25,34 +28,41 @@ namespace chrome { |
namespace search { |
ToolbarSearchAnimator::ToolbarSearchAnimator(SearchModel* search_model) |
- : search_model_(search_model), |
- animate_state_(ANIMATE_STATE_NONE) { |
+ : search_model_(search_model) { |
search_model_->AddObserver(this); |
+ |
+ ui::MultiAnimation::Parts parts; |
+ parts.push_back(ui::MultiAnimation::Part( |
+ kBackgroundChangeDelayMs * InstantUI::GetSlowAnimationScaleFactor(), |
+ ui::Tween::ZERO)); |
+ parts.push_back(ui::MultiAnimation::Part( |
+ kBackgroundChangeDurationMs * InstantUI::GetSlowAnimationScaleFactor(), |
+ ui::Tween::LINEAR)); |
+ background_animation_.reset(new ui::MultiAnimation(parts)); |
+ background_animation_->set_continuous(false); |
+ |
+ separator_animation_.reset(new ui::SlideAnimation(this)); |
+ separator_animation_->SetTweenType(ui::Tween::LINEAR); |
+ separator_animation_->SetSlideDuration( |
+ kSeparatorFadeDurationMs * InstantUI::GetSlowAnimationScaleFactor()); |
} |
ToolbarSearchAnimator::~ToolbarSearchAnimator() { |
+ background_animation_->Stop(); |
+ separator_animation_->Stop(); |
search_model_->RemoveObserver(this); |
} |
-void ToolbarSearchAnimator::GetCurrentBackgroundState( |
- BackgroundState* background_state, |
- double* search_background_opacity) const { |
- // Should only be called for SEARCH mode. |
- DCHECK(search_model_->mode().is_search()); |
- *background_state = BACKGROUND_STATE_DEFAULT; |
- *search_background_opacity = -1.0f; |
- switch (animate_state_) { |
- case ANIMATE_STATE_WAITING: |
- *background_state = BACKGROUND_STATE_NTP; |
- break; |
- case ANIMATE_STATE_RUNNING: |
- *background_state = BACKGROUND_STATE_NTP_SEARCH; |
- *search_background_opacity = background_animation_->CurrentValueBetween( |
- kMinOpacity, kMaxOpacity); |
- break; |
- case ANIMATE_STATE_NONE: |
- break; |
- } |
+double ToolbarSearchAnimator::GetGradientOpacity() const { |
+ if (background_animation_->is_animating()) |
+ return background_animation_->CurrentValueBetween(kMinOpacity, kMaxOpacity); |
+ return search_model_->mode().is_ntp() ? kMinOpacity : kMaxOpacity; |
+} |
+ |
+double ToolbarSearchAnimator::GetSeparatorOpacity() const { |
+ if (separator_animation_->is_animating()) |
+ return separator_animation_->CurrentValueBetween(kMinOpacity, kMaxOpacity); |
+ return search_model_->mode().is_default() ? kMaxOpacity : kMinOpacity; |
} |
void ToolbarSearchAnimator::FinishAnimation(TabContents* tab_contents) { |
@@ -69,71 +79,123 @@ void ToolbarSearchAnimator::RemoveObserver( |
observers_.RemoveObserver(observer); |
} |
-void ToolbarSearchAnimator::ModeChanged(const Mode& mode) { |
- int delay_ms = kBackgroundChangeDelayMs * |
- InstantUI::GetSlowAnimationScaleFactor(); |
- if (mode.is_search() && mode.animate && |
- animate_state_ == ANIMATE_STATE_NONE) { |
- background_change_timer_.Start( |
- FROM_HERE, |
- base::TimeDelta::FromMilliseconds(delay_ms), |
- this, |
- &ToolbarSearchAnimator::StartBackgroundChange); |
- animate_state_ = ANIMATE_STATE_WAITING; |
+void ToolbarSearchAnimator::ModeChanged(const Mode& old_mode, |
+ const Mode& new_mode) { |
+ // If mode transitions from |NTP| to |SEARCH| and we're not animating |
dhollowa
2012/08/01 22:26:23
"If the mode"...
kuan
2012/08/02 21:54:03
Done.
|
+ // background, start fading in gradient background. |
+ // TODO(kuan): check with UX folks if we need to animate from gradient to flat |
+ // when mode transitions from |SEARCH| or |DEFAULT| to |NTP|. |
+ if (new_mode.animate && old_mode.is_ntp() && new_mode.is_search() && |
+ !background_animation_->is_animating()) { |
+ StartBackgroundChange(); |
+ return; |
+ } |
+ |
+ // If mode transitions from non-|DEFAULT| to |DEFAULT|, fade in separator; |
+ // if we're already fading out separator, reverse to fade it in. |
+ // Note that if we're still fading in gradient background when this mode |
+ // transition happens (e.g. when user quickly hits enter on the auto-completed |
+ // query in omnibox, triggering mode to transition from |SEARCH| to |
+ // |DEFAULT|), we should continue fading in the gradient background to its |
+ // full course. |
+ // TODO(kuan): however, for now, sometimes, the first auto-completed query in |
+ // omnibox is pre-rendered and gets swapped in when chosen by user, causing |
+ // OnTabDetached for original tab to be fired, and hence causing us to Reset, |
+ // immediately jumping to end state of background animation. I'll look into |
+ // this together with the above TODO for reverse background animation. |
+ if (new_mode.animate && (new_mode.is_default() || |
+ (separator_animation_->is_animating() && |
+ separator_animation_->IsClosing()))) { |
+ StartSeparatorFade(true); |
+ return; |
+ } |
+ |
+ // If mode transitions from |DEFAULT| to non-|DEFAULT|, fade out separator; |
+ // if we're already fading in separator, reverse to fade it out. |
+ // TODO(kuan): for now, if we're still fading in gradient background when this |
+ // mode transition happens (e.g. when user quickly hits back button on a |
+ // website that he/she just entered from a |SEARCH| mode, triggering mode to |
+ // transition from |DEFAULT| to |NTP|), we'll continue fading in the gradient |
+ // background to its full course, which is undesirable. I'll handle this |
+ // together with the above TODO for reverse background animation. |
+ if (new_mode.animate && (old_mode.is_default() || |
+ (separator_animation_->is_animating() && |
+ separator_animation_->IsShowing()))) { |
+ StartSeparatorFade(false); |
return; |
} |
- // For all other cases, reset |animate_state_| and stop timer or animation. |
+ |
+ // For all other cases, reset animate states and stop animation(s). |
// Stopping animation will jump to the end of it. |
Reset(NULL); |
} |
void ToolbarSearchAnimator::AnimationProgressed( |
const ui::Animation* animation) { |
- DCHECK_EQ(animation, background_animation_.get()); |
- animate_state_ = ANIMATE_STATE_RUNNING; |
- FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
- OnToolbarBackgroundAnimatorProgressed()); |
+ if (animation == background_animation_.get()) { |
+ FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
+ OnToolbarBackgroundAnimatorProgressed()); |
+ return; |
+ } |
+ |
+ if (animation == separator_animation_.get()) { |
+ FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
+ OnToolbarSeparatorAnimatorProgressed()); |
+ } |
} |
void ToolbarSearchAnimator::AnimationEnded(const ui::Animation* animation) { |
- DCHECK_EQ(animation, background_animation_.get()); |
- // Only notify observers via OnToolbarBackgroundAnimatorProgressed 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 |
- // OnToolbarBackgroundAnimatorCanceled. |
- if (animate_state_ == ANIMATE_STATE_RUNNING) { |
- animate_state_ = ANIMATE_STATE_NONE; |
+ // We only get this callback when |animation| has run its full course. |
+ // If animation was canceled (in Reset()), we won't get an AnimationEnded() |
+ // callback because we had cleared the animation's delegate in Reset(). |
+ if (animation == background_animation_.get()) { |
FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
OnToolbarBackgroundAnimatorProgressed()); |
+ return; |
+ } |
+ |
+ if (animation == separator_animation_.get()) { |
+ FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
+ OnToolbarSeparatorAnimatorProgressed()); |
} |
} |
void ToolbarSearchAnimator::StartBackgroundChange() { |
- if (!background_animation_.get()) { |
- background_animation_.reset(new ui::SlideAnimation(this)); |
- background_animation_->SetTweenType(ui::Tween::LINEAR); |
- background_animation_->SetSlideDuration( |
- kBackgroundChangeDurationMs * InstantUI::GetSlowAnimationScaleFactor()); |
+ background_animation_->set_delegate(this); |
+ background_animation_->Start(); |
+} |
+ |
+void ToolbarSearchAnimator::StartSeparatorFade(bool show) { |
+ if (!separator_animation_->is_animating()) { |
+ separator_animation_->set_delegate(this); |
+ separator_animation_->Reset(show ? 0.0 : 1.0); |
} |
- background_animation_->Reset(0.0); |
- background_animation_->Show(); |
+ if (show) |
+ separator_animation_->Show(); |
+ else |
+ separator_animation_->Hide(); |
} |
void ToolbarSearchAnimator::Reset(TabContents* tab_contents) { |
- bool notify_observers = animate_state_ != ANIMATE_STATE_NONE; |
- 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) { |
+ bool notify_background_observers = background_animation_->is_animating(); |
+ bool notify_separator_observers = separator_animation_->is_animating(); |
+ |
+ // Clear the animations' delegates so that we don't get AnimationEnded() |
+ // callbacks. |
+ background_animation_->set_delegate(NULL); |
+ separator_animation_->set_delegate(NULL); |
+ background_animation_->Stop(); |
+ separator_animation_->Stop(); |
+ |
+ // Notify observers of animation(s) cancelation. |
+ if (notify_background_observers) { |
FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
OnToolbarBackgroundAnimatorCanceled(tab_contents)); |
} |
+ if (notify_separator_observers) { |
+ FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, |
+ OnToolbarSeparatorAnimatorCanceled()); |
+ } |
} |
} // namespace search |