OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/setting_level_bubble.h" | 5 #include "chrome/browser/chromeos/setting_level_bubble.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "chrome/browser/chromeos/login/background_view.h" | 9 #include "chrome/browser/chromeos/login/background_view.h" |
10 #include "chrome/browser/chromeos/login/login_utils.h" | 10 #include "chrome/browser/chromeos/login/login_utils.h" |
11 #include "chrome/browser/chromeos/login/webui_login_display.h" | 11 #include "chrome/browser/chromeos/login/webui_login_display.h" |
12 #include "chrome/browser/chromeos/setting_level_bubble_view.h" | 12 #include "chrome/browser/chromeos/setting_level_bubble_view.h" |
13 #include "chrome/browser/profiles/profile_manager.h" | 13 #include "chrome/browser/profiles/profile_manager.h" |
14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
16 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
17 #include "chrome/browser/ui/views/bubble/bubble.h" | |
18 #include "ui/gfx/screen.h" | 17 #include "ui/gfx/screen.h" |
18 #include "views/bubble/bubble_delegate.h" | |
19 #include "views/layout/fill_layout.h" | |
19 #include "views/widget/root_view.h" | 20 #include "views/widget/root_view.h" |
20 | 21 |
21 using base::TimeDelta; | 22 using base::TimeDelta; |
22 using base::TimeTicks; | 23 using base::TimeTicks; |
23 using std::max; | 24 using std::max; |
24 using std::min; | 25 using std::min; |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 // How long should the bubble be shown onscreen whenever the setting changes? | 29 // How long should the bubble be shown onscreen whenever the setting changes? |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 window = GTK_WINDOW(background->GetNativeWindow()); | 78 window = GTK_WINDOW(background->GetNativeWindow()); |
78 #endif | 79 #endif |
79 } | 80 } |
80 | 81 |
81 if (window) | 82 if (window) |
82 return views::Widget::GetWidgetForNativeWindow(window); | 83 return views::Widget::GetWidgetForNativeWindow(window); |
83 else | 84 else |
84 return WebUILoginDisplay::GetLoginWindow(); | 85 return WebUILoginDisplay::GetLoginWindow(); |
85 } | 86 } |
86 | 87 |
88 // static | |
89 views::Widget* SettingLevelBubble::CreateBubble( | |
90 SkBitmap* increase_icon, | |
91 SkBitmap* decrease_icon, | |
92 SkBitmap* zero_icon, | |
93 double percent, | |
94 bool enabled) { | |
95 views::Widget* parent = GetToplevelWidget(); | |
96 // Calculate the position in screen coordinates that the bubble should | |
97 // "point" at (since we use BubbleBorder::FLOAT, this position actually | |
98 // specifies the center of the bubble). | |
99 const gfx::Rect monitor_area = | |
100 gfx::Screen::GetMonitorAreaNearestWindow(parent->GetNativeView()); | |
101 SettingLevelBubble* delegate = | |
102 new SettingLevelBubble(increase_icon, decrease_icon, zero_icon, | |
103 monitor_area); | |
104 delegate->UpdateWithoutShowingBubble(percent, enabled); | |
105 delegate->set_close_on_esc(false); | |
106 views::Widget* widget = views::BubbleDelegateView::CreateBubble( | |
107 delegate, parent); | |
108 return widget; | |
109 } | |
110 | |
111 void SettingLevelBubble::ShowBubble(views::Widget* widget, | |
112 double percent, | |
113 bool enabled) { | |
114 DCHECK(widget); | |
115 SettingLevelBubble* delegate = | |
116 static_cast<SettingLevelBubble*>(widget->widget_delegate()); | |
117 delegate->UpdateWithoutShowingBubble(percent, enabled); | |
118 // Show the window and start the timer. When the timer | |
119 // runs out, we start the fade sequence. | |
120 widget->Show(); | |
121 delegate->StartHideTimer(); | |
122 } | |
123 | |
87 SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, | 124 SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, |
88 SkBitmap* decrease_icon, | 125 SkBitmap* decrease_icon, |
89 SkBitmap* disabled_icon) | 126 SkBitmap* disabled_icon, |
90 : current_percent_(-1.0), | 127 const gfx::Rect& monitor_area) |
128 : BubbleDelegateView(gfx::Point(), | |
129 views::BubbleBorder::FLOAT, | |
130 SK_ColorWHITE), | |
131 current_percent_(-1.0), | |
91 target_percent_(-1.0), | 132 target_percent_(-1.0), |
92 increase_icon_(increase_icon), | 133 increase_icon_(increase_icon), |
93 decrease_icon_(decrease_icon), | 134 decrease_icon_(decrease_icon), |
94 disabled_icon_(disabled_icon), | 135 disabled_icon_(disabled_icon), |
95 bubble_(NULL), | 136 current_icon_(NULL), |
96 view_(NULL), | 137 view_(NULL), |
97 is_animating_(false) { | 138 is_animating_(false), |
139 monitor_area_(monitor_area) { | |
msw
2011/10/22 00:44:48
enabled_ should probably be initialized here.
afai
alicet1
2011/10/24 15:46:38
Done.
| |
98 } | 140 } |
99 | 141 |
100 SettingLevelBubble::~SettingLevelBubble() {} | 142 SettingLevelBubble::~SettingLevelBubble() { |
143 } | |
101 | 144 |
102 void SettingLevelBubble::ShowBubble(double percent, bool enabled) { | 145 gfx::Point SettingLevelBubble::GetAnchorPoint() const { |
103 const double old_target_percent = target_percent_; | 146 return (gfx::Point( |
104 UpdateTargetPercent(percent); | 147 monitor_area_.x() + kBubbleXRatio * monitor_area_.width(), |
148 monitor_area_.bottom() - view_size_.height() / 2 - kBubbleBottomGap)); | |
149 } | |
105 | 150 |
106 SkBitmap* icon = increase_icon_; | 151 void SettingLevelBubble::Init() { |
107 if (!enabled || target_percent_ == 0) | 152 SetLayoutManager(new views::FillLayout()); |
108 icon = disabled_icon_; | 153 view_ = new SettingLevelBubbleView(); |
109 else if (old_target_percent >= 0 && target_percent_ < old_target_percent) | 154 view_->Init(current_icon_, current_percent_, enabled_); |
110 icon = decrease_icon_; | 155 AddChildView(view_); |
156 view_size_ = GetPreferredSize(); | |
157 } | |
111 | 158 |
112 if (!bubble_) { | 159 void SettingLevelBubble::WindowClosing() { |
113 views::Widget* parent_widget = GetToplevelWidget(); | 160 hide_timer_.Stop(); |
114 if (parent_widget == NULL) { | 161 StopAnimation(); |
115 LOG(WARNING) << "Unable to locate parent widget to display a bubble"; | 162 view_ = NULL; |
116 return; | 163 current_percent_ = target_percent_; |
117 } | 164 target_time_ = TimeTicks(); |
118 DCHECK(view_ == NULL); | 165 last_animation_update_time_ = TimeTicks(); |
119 view_ = new SettingLevelBubbleView; | 166 last_target_update_time_ = TimeTicks(); |
120 view_->Init(icon, current_percent_, enabled); | 167 } |
121 | 168 |
122 // Calculate the position in screen coordinates that the bubble should | 169 void SettingLevelBubble::StartHideTimer() { |
123 // "point" at (since we use BubbleBorder::FLOAT, this position actually | 170 hide_timer_.Stop(); |
124 // specifies the center of the bubble). | |
125 const gfx::Rect monitor_area = | |
126 gfx::Screen::GetMonitorAreaNearestWindow( | |
127 parent_widget->GetNativeView()); | |
128 const gfx::Size view_size = view_->GetPreferredSize(); | |
129 const gfx::Rect position_relative_to( | |
130 monitor_area.x() + kBubbleXRatio * monitor_area.width(), | |
131 monitor_area.bottom() - view_size.height() / 2 - kBubbleBottomGap, | |
132 0, 0); | |
133 | |
134 bubble_ = Bubble::ShowFocusless(parent_widget, | |
135 position_relative_to, | |
136 views::BubbleBorder::FLOAT, | |
137 view_, // contents | |
138 this, // delegate | |
139 true); // show while screen is locked | |
140 // TODO(derat): We probably shouldn't be using Bubble. It'd be nice to call | |
141 // bubble_->set_fade_away_on_close(true) here, but then, if ShowBubble() | |
142 // gets called while the bubble is fading away, we end up just adjusting the | |
143 // value on the disappearing bubble; ideally we'd have a way to cancel the | |
144 // fade and show the bubble at full opacity for another | |
145 // kBubbleShowTimeoutMs. | |
146 } else { | |
147 DCHECK(view_); | |
148 hide_timer_.Stop(); | |
149 view_->SetIcon(icon); | |
150 view_->SetEnabled(enabled); | |
151 } | |
152 | |
153 hide_timer_.Start(FROM_HERE, | 171 hide_timer_.Start(FROM_HERE, |
154 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), | 172 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), |
155 this, &SettingLevelBubble::OnHideTimeout); | 173 this, &SettingLevelBubble::OnHideTimeout); |
156 } | 174 } |
157 | 175 |
158 void SettingLevelBubble::HideBubble() { | |
159 if (bubble_) | |
160 bubble_->Close(); | |
161 } | |
162 | |
163 void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, | 176 void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, |
164 bool enabled) { | 177 bool enabled) { |
178 enabled_ = enabled; | |
179 const double old_target_percent = target_percent_; | |
165 UpdateTargetPercent(percent); | 180 UpdateTargetPercent(percent); |
166 if (view_) | 181 current_icon_ = increase_icon_; |
182 if (!enabled || target_percent_ == 0) | |
183 current_icon_ = disabled_icon_; | |
184 else if (old_target_percent >= 0 && target_percent_ < old_target_percent) | |
185 current_icon_ = decrease_icon_; | |
186 if (view_) { | |
167 view_->SetEnabled(enabled); | 187 view_->SetEnabled(enabled); |
188 view_->SetIcon(current_icon_); | |
189 } | |
168 } | 190 } |
169 | 191 |
170 void SettingLevelBubble::OnHideTimeout() { | 192 void SettingLevelBubble::OnHideTimeout() { |
171 HideBubble(); | 193 // Start fading away. |
194 StartFade(false); | |
172 } | 195 } |
173 | 196 |
174 void SettingLevelBubble::OnAnimationTimeout() { | 197 void SettingLevelBubble::OnAnimationTimeout() { |
175 const TimeTicks now = TimeTicks::Now(); | 198 const TimeTicks now = TimeTicks::Now(); |
176 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); | 199 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); |
177 | 200 |
178 if (remaining_ms <= 0) { | 201 if (remaining_ms <= 0) { |
179 current_percent_ = target_percent_; | 202 current_percent_ = target_percent_; |
180 StopAnimation(); | 203 StopAnimation(); |
181 } else { | 204 } else { |
182 // Figure out what fraction of the total time until we want to reach the | 205 // Figure out what fraction of the total time until we want to reach the |
183 // target has elapsed since the last update. | 206 // target has elapsed since the last update. |
184 const double remaining_percent = target_percent_ - current_percent_; | 207 const double remaining_percent = target_percent_ - current_percent_; |
185 const int64 elapsed_ms = | 208 const int64 elapsed_ms = |
186 (now - last_animation_update_time_).InMilliseconds(); | 209 (now - last_animation_update_time_).InMilliseconds(); |
187 current_percent_ += | 210 current_percent_ += |
188 remaining_percent * | 211 remaining_percent * |
189 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); | 212 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); |
190 } | 213 } |
191 last_animation_update_time_ = now; | 214 last_animation_update_time_ = now; |
192 | 215 |
193 if (view_) | 216 if (view_) |
194 view_->SetLevel(current_percent_); | 217 view_->SetLevel(current_percent_); |
195 } | 218 } |
196 | 219 |
197 void SettingLevelBubble::BubbleClosing(Bubble* bubble, bool) { | |
198 DCHECK(bubble == bubble_); | |
199 hide_timer_.Stop(); | |
200 StopAnimation(); | |
201 bubble_ = NULL; | |
202 view_ = NULL; | |
203 current_percent_ = target_percent_; | |
204 target_time_ = TimeTicks(); | |
205 last_animation_update_time_ = TimeTicks(); | |
206 last_target_update_time_ = TimeTicks(); | |
207 } | |
208 | |
209 bool SettingLevelBubble::CloseOnEscape() { | |
210 return true; | |
211 } | |
212 | |
213 bool SettingLevelBubble::FadeInOnShow() { | |
214 return false; | |
215 } | |
216 | |
217 void SettingLevelBubble::UpdateTargetPercent(double percent) { | 220 void SettingLevelBubble::UpdateTargetPercent(double percent) { |
218 target_percent_ = LimitPercent(percent); | 221 target_percent_ = LimitPercent(percent); |
219 const TimeTicks now = TimeTicks::Now(); | 222 const TimeTicks now = TimeTicks::Now(); |
220 | 223 |
221 if (current_percent_ < 0.0) { | 224 if (current_percent_ < 0.0) { |
222 // If we're setting the level for the first time, no need to animate. | 225 // If we're setting the level for the first time, no need to animate. |
223 current_percent_ = target_percent_; | 226 current_percent_ = target_percent_; |
224 if (view_) | 227 if (view_) |
225 view_->SetLevel(current_percent_); | 228 view_->SetLevel(current_percent_); |
226 } else { | 229 } else { |
(...skipping 19 matching lines...) Expand all Loading... | |
246 | 249 |
247 last_target_update_time_ = now; | 250 last_target_update_time_ = now; |
248 } | 251 } |
249 | 252 |
250 void SettingLevelBubble::StopAnimation() { | 253 void SettingLevelBubble::StopAnimation() { |
251 animation_timer_.Stop(); | 254 animation_timer_.Stop(); |
252 is_animating_ = false; | 255 is_animating_ = false; |
253 } | 256 } |
254 | 257 |
255 } // namespace chromeos | 258 } // namespace chromeos |
OLD | NEW |