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 |
87 SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, | 88 // SettingLevelBubbleDelegateView ---------------------------------------------- |
88 SkBitmap* decrease_icon, | 89 class SettingLevelBubbleDelegateView : public views::BubbleDelegateView { |
89 SkBitmap* disabled_icon) | 90 public: |
90 : current_percent_(-1.0), | 91 // BubbleDelegate overrides: |
91 target_percent_(-1.0), | 92 virtual gfx::Point GetAnchorPoint() OVERRIDE; |
92 increase_icon_(increase_icon), | 93 |
93 decrease_icon_(decrease_icon), | 94 // Create the bubble delegate view. |
94 disabled_icon_(disabled_icon), | 95 explicit SettingLevelBubbleDelegateView(views::Widget* parent); |
95 bubble_(NULL), | 96 virtual ~SettingLevelBubbleDelegateView(); |
96 view_(NULL), | 97 |
97 is_animating_(false) { | 98 SettingLevelBubbleView* view() { return view_; } |
99 | |
100 protected: | |
101 // BubbleDelegate overrides: | |
102 virtual void Init() OVERRIDE; | |
103 | |
104 private: | |
105 views::Widget* parent_; | |
106 | |
107 SettingLevelBubbleView* view_; | |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(SettingLevelBubbleDelegateView); | |
110 }; | |
111 | |
112 gfx::Point SettingLevelBubbleDelegateView::GetAnchorPoint() { | |
113 gfx::Size view_size = GetPreferredSize(); | |
114 // Calculate the position in screen coordinates that the bubble should | |
115 // "point" at (since we use BubbleBorder::FLOAT, this position actually | |
116 // specifies the center of the bubble). | |
117 gfx::Rect monitor_area = | |
118 gfx::Screen::GetMonitorAreaNearestWindow( | |
119 parent_->GetNativeView()); | |
120 return (gfx::Point( | |
121 monitor_area.x() + kBubbleXRatio * monitor_area.width(), | |
122 monitor_area.bottom() - view_size.height() / 2 - kBubbleBottomGap)); | |
98 } | 123 } |
99 | 124 |
100 SettingLevelBubble::~SettingLevelBubble() {} | 125 SettingLevelBubbleDelegateView::SettingLevelBubbleDelegateView( |
126 views::Widget* parent) | |
127 : BubbleDelegateView(gfx::Point(), | |
128 views::BubbleBorder::FLOAT, | |
129 SK_ColorWHITE), | |
130 parent_(parent), | |
131 view_(NULL) { | |
132 set_close_on_esc(false); | |
133 } | |
134 | |
135 SettingLevelBubbleDelegateView::~SettingLevelBubbleDelegateView() { | |
136 view_ = NULL; | |
137 } | |
138 | |
139 void SettingLevelBubbleDelegateView::Init() { | |
140 SetLayoutManager(new views::FillLayout()); | |
141 view_ = new SettingLevelBubbleView(); | |
Daniel Erat
2011/10/26 05:08:01
should probably call the SettingLevelBubbleView's
alicet1
2011/10/26 16:12:48
yes, didn't do that for a reason -- the view's ini
| |
142 AddChildView(view_); | |
143 } | |
144 | |
145 // SettingLevelBubble ---------------------------------------------- | |
146 void SettingLevelBubble::OnWidgetClosing(views::Widget* widget) { | |
147 if (view_ && view_->GetWidget() == widget) { | |
148 view_->GetWidget()->RemoveObserver(this); | |
149 view_= NULL; | |
Daniel Erat
2011/10/26 05:08:01
nit: "view_ ="
alicet1
2011/10/26 16:12:48
Done.
| |
150 } | |
151 // Update states. | |
152 current_percent_ = target_percent_; | |
153 target_time_ = TimeTicks(); | |
154 last_animation_update_time_ = TimeTicks(); | |
155 last_target_update_time_ = TimeTicks(); | |
156 hide_timer_.Stop(); | |
157 StopAnimation(); | |
158 } | |
101 | 159 |
102 void SettingLevelBubble::ShowBubble(double percent, bool enabled) { | 160 void SettingLevelBubble::ShowBubble(double percent, bool enabled) { |
103 const double old_target_percent = target_percent_; | 161 if (!view_) |
104 UpdateTargetPercent(percent); | 162 view_ = CreateView(); |
105 | 163 UpdateWithoutShowingBubble(percent, enabled); |
106 SkBitmap* icon = increase_icon_; | 164 view_->GetWidget()->Show(); |
107 if (!enabled || target_percent_ == 0) | 165 // Show the widget and start the timer. When the timer |
108 icon = disabled_icon_; | 166 // runs out, we start the fade sequence. |
109 else if (old_target_percent >= 0 && target_percent_ < old_target_percent) | 167 hide_timer_.Stop(); |
110 icon = decrease_icon_; | |
111 | |
112 if (!bubble_) { | |
113 views::Widget* parent_widget = GetToplevelWidget(); | |
114 if (parent_widget == NULL) { | |
115 LOG(WARNING) << "Unable to locate parent widget to display a bubble"; | |
116 return; | |
117 } | |
118 DCHECK(view_ == NULL); | |
119 view_ = new SettingLevelBubbleView; | |
120 view_->Init(icon, current_percent_, enabled); | |
121 | |
122 // Calculate the position in screen coordinates that the bubble should | |
123 // "point" at (since we use BubbleBorder::FLOAT, this position actually | |
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, | 168 hide_timer_.Start(FROM_HERE, |
154 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), | 169 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), |
155 this, &SettingLevelBubble::OnHideTimeout); | 170 this, &SettingLevelBubble::OnHideTimeout); |
156 } | 171 } |
157 | 172 |
158 void SettingLevelBubble::HideBubble() { | 173 void SettingLevelBubble::HideBubble() { |
159 if (bubble_) | 174 if (view_) { |
160 bubble_->Close(); | 175 view_->GetWidget()->Close(); |
176 view_ = NULL; | |
177 } | |
178 } | |
179 | |
180 SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, | |
181 SkBitmap* decrease_icon, | |
182 SkBitmap* disabled_icon) | |
183 : current_percent_(-1.0), | |
184 target_percent_(-1.0), | |
185 increase_icon_(increase_icon), | |
186 decrease_icon_(decrease_icon), | |
187 disabled_icon_(disabled_icon), | |
188 is_animating_(false) { | |
189 view_ = CreateView(); | |
190 } | |
191 | |
192 SettingLevelBubble::~SettingLevelBubble() { | |
193 view_ = NULL; | |
194 } | |
195 | |
196 SettingLevelBubbleView* SettingLevelBubble::CreateView() { | |
197 views::Widget* parent = GetToplevelWidget(); | |
198 SettingLevelBubbleDelegateView* delegate = | |
199 new SettingLevelBubbleDelegateView(parent); | |
200 views::Widget* widget = | |
201 views::BubbleDelegateView::CreateBubble(delegate, parent); | |
202 widget->AddObserver(this); | |
203 // Hold on to the content view. | |
204 return delegate->view(); | |
161 } | 205 } |
162 | 206 |
163 void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, | 207 void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, |
164 bool enabled) { | 208 bool enabled) { |
209 const double old_target_percent = target_percent_; | |
165 UpdateTargetPercent(percent); | 210 UpdateTargetPercent(percent); |
166 if (view_) | 211 SkBitmap* current_icon = increase_icon_; |
167 view_->SetEnabled(enabled); | 212 if (!enabled || target_percent_ == 0) |
213 current_icon = disabled_icon_; | |
214 else if (old_target_percent >= 0 && target_percent_ < old_target_percent) | |
215 current_icon = decrease_icon_; | |
216 if (view_) { | |
Daniel Erat
2011/10/26 05:08:01
nit: no curly braces for single-line if statements
alicet1
2011/10/26 16:12:48
Done.
| |
217 view_->Init(current_icon, percent, enabled); | |
Daniel Erat
2011/10/26 05:08:01
i think that it's the case in all of the chrome co
alicet1
2011/10/26 16:12:48
removed.
| |
218 } | |
168 } | 219 } |
169 | 220 |
170 void SettingLevelBubble::OnHideTimeout() { | 221 void SettingLevelBubble::OnHideTimeout() { |
171 HideBubble(); | 222 // Start fading away. |
223 if (view_) { | |
224 SettingLevelBubbleDelegateView* delegate = | |
225 static_cast<SettingLevelBubbleDelegateView*> | |
226 (view_->GetWidget()->widget_delegate()); | |
227 delegate->StartFade(false); | |
228 } | |
172 } | 229 } |
173 | 230 |
174 void SettingLevelBubble::OnAnimationTimeout() { | 231 void SettingLevelBubble::OnAnimationTimeout() { |
175 const TimeTicks now = TimeTicks::Now(); | 232 const TimeTicks now = TimeTicks::Now(); |
176 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); | 233 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); |
177 | 234 |
178 if (remaining_ms <= 0) { | 235 if (remaining_ms <= 0) { |
179 current_percent_ = target_percent_; | 236 current_percent_ = target_percent_; |
180 StopAnimation(); | 237 StopAnimation(); |
181 } else { | 238 } else { |
182 // Figure out what fraction of the total time until we want to reach the | 239 // Figure out what fraction of the total time until we want to reach the |
183 // target has elapsed since the last update. | 240 // target has elapsed since the last update. |
184 const double remaining_percent = target_percent_ - current_percent_; | 241 const double remaining_percent = target_percent_ - current_percent_; |
185 const int64 elapsed_ms = | 242 const int64 elapsed_ms = |
186 (now - last_animation_update_time_).InMilliseconds(); | 243 (now - last_animation_update_time_).InMilliseconds(); |
187 current_percent_ += | 244 current_percent_ += |
188 remaining_percent * | 245 remaining_percent * |
189 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); | 246 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); |
190 } | 247 } |
191 last_animation_update_time_ = now; | 248 last_animation_update_time_ = now; |
192 | 249 |
193 if (view_) | 250 if (view_) |
194 view_->SetLevel(current_percent_); | 251 view_->SetLevel(current_percent_); |
195 } | 252 } |
196 | 253 |
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) { | 254 void SettingLevelBubble::UpdateTargetPercent(double percent) { |
218 target_percent_ = LimitPercent(percent); | 255 target_percent_ = LimitPercent(percent); |
219 const TimeTicks now = TimeTicks::Now(); | 256 const TimeTicks now = TimeTicks::Now(); |
220 | 257 |
221 if (current_percent_ < 0.0) { | 258 if (current_percent_ < 0.0) { |
222 // If we're setting the level for the first time, no need to animate. | 259 // If we're setting the level for the first time, no need to animate. |
223 current_percent_ = target_percent_; | 260 current_percent_ = target_percent_; |
224 if (view_) | 261 if (view_) |
225 view_->SetLevel(current_percent_); | 262 view_->SetLevel(current_percent_); |
226 } else { | 263 } else { |
(...skipping 19 matching lines...) Expand all Loading... | |
246 | 283 |
247 last_target_update_time_ = now; | 284 last_target_update_time_ = now; |
248 } | 285 } |
249 | 286 |
250 void SettingLevelBubble::StopAnimation() { | 287 void SettingLevelBubble::StopAnimation() { |
251 animation_timer_.Stop(); | 288 animation_timer_.Stop(); |
252 is_animating_ = false; | 289 is_animating_ = false; |
253 } | 290 } |
254 | 291 |
255 } // namespace chromeos | 292 } // namespace chromeos |
OLD | NEW |