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 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.
| |
19 const int kMaxOpacity = 0xFF; | |
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 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.
| |
34 search_model_->RemoveObserver(this); | |
35 } | |
36 | |
37 void ToolbarSearchAnimator::GetCurrentBackgroundState( | |
38 BackgroundState* background_state, | |
39 int* new_background_opacity) const { | |
40 // Should only be called for SEARCH mode. | |
41 DCHECK(search_model_->mode().is_search()); | |
42 *background_state = BACKGROUND_STATE_SHOW_NON_APPLICABLE; | |
43 *new_background_opacity = -1; | |
44 switch (animate_state_) { | |
45 case ANIMATE_STATE_WAITING: | |
46 *background_state = BACKGROUND_STATE_SHOW_NTP; | |
47 break; | |
48 case ANIMATE_STATE_RUNNING: | |
49 *background_state = BACKGROUND_STATE_SHOW_NTP_SEARCH; | |
50 *new_background_opacity = background_animation_->CurrentValueBetween( | |
51 kMinOpacity, kMaxOpacity); | |
52 break; | |
53 case ANIMATE_STATE_NONE: | |
54 break; | |
55 } | |
56 } | |
57 | |
58 void ToolbarSearchAnimator::FinishAnimation(TabContents* tab_contents) { | |
59 Reset(true /* notify observers */, tab_contents); | |
60 } | |
61 | |
62 void ToolbarSearchAnimator::AddObserver( | |
63 ToolbarSearchAnimatorObserver* observer) { | |
64 observers_.AddObserver(observer); | |
65 } | |
66 | |
67 void ToolbarSearchAnimator::RemoveObserver( | |
68 ToolbarSearchAnimatorObserver* observer) { | |
69 observers_.RemoveObserver(observer); | |
70 } | |
71 | |
72 void ToolbarSearchAnimator::ModeChanged(const Mode& mode) { | |
73 if (mode.is_search() && mode.animate && | |
74 animate_state_ == ANIMATE_STATE_NONE) { | |
75 background_change_timer_.Start( | |
76 FROM_HERE, | |
77 base::TimeDelta::FromMilliseconds(kBackgroundChangeDelayMs), | |
78 this, | |
79 &ToolbarSearchAnimator::StartBackgroundChange); | |
80 animate_state_ = ANIMATE_STATE_WAITING; | |
81 return; | |
82 } | |
83 // For all other cases, reset |animate_state_| and stop timer or animation. | |
84 // Stopping animation will jump to the end of it. | |
85 Reset(true /* notify observers */, NULL); | |
86 } | |
87 | |
88 void ToolbarSearchAnimator::AnimationProgressed( | |
89 const ui::Animation* animation) { | |
90 DCHECK(animation == background_animation_.get()); | |
sky
2012/06/26 17:11:27
DCHECK_EQ
kuan
2012/06/26 23:25:49
Done.
| |
91 animate_state_ = ANIMATE_STATE_RUNNING; | |
92 FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, | |
93 BackgroundChanging()); | |
94 } | |
95 | |
96 void ToolbarSearchAnimator::AnimationEnded(const ui::Animation* animation) { | |
97 DCHECK(animation == background_animation_.get()); | |
sky
2012/06/26 17:11:27
DCHECK_EG
kuan
2012/06/26 23:25:49
Done.
| |
98 // Only notify observers via BackgroundChange if the animation has runs its | |
99 // full course i.e |animate_state_| is still 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 // BackgroundChangeCanceled. | |
103 if (animate_state_ == ANIMATE_STATE_RUNNING) { | |
104 animate_state_ = ANIMATE_STATE_NONE; | |
105 FOR_EACH_OBSERVER(ToolbarSearchAnimatorObserver, observers_, | |
106 BackgroundChanged()); | |
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(bool notify_observers, | |
121 TabContents* tab_contents) { | |
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 BackgroundChangeCanceled(tab_contents)); | |
132 } | |
133 } | |
134 | |
135 } // namespace search | |
136 } // namespace chrome | |
OLD | NEW |