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 } |
101 | 134 |
| 135 SettingLevelBubbleDelegateView::~SettingLevelBubbleDelegateView() { |
| 136 view_ = NULL; |
| 137 } |
| 138 |
| 139 void SettingLevelBubbleDelegateView::Init() { |
| 140 SetLayoutManager(new views::FillLayout()); |
| 141 view_ = new SettingLevelBubbleView(); |
| 142 AddChildView(view_); |
| 143 } |
| 144 |
| 145 // SettingLevelBubble ---------------------------------------------------------- |
102 void SettingLevelBubble::ShowBubble(double percent, bool enabled) { | 146 void SettingLevelBubble::ShowBubble(double percent, bool enabled) { |
| 147 hide_timer_.Stop(); |
| 148 |
| 149 // Set up target percent and icon. |
103 const double old_target_percent = target_percent_; | 150 const double old_target_percent = target_percent_; |
104 UpdateTargetPercent(percent); | 151 UpdateTargetPercent(percent); |
| 152 SkBitmap* current_icon = increase_icon_; |
| 153 if (!enabled || target_percent_ == 0) |
| 154 current_icon = disabled_icon_; |
| 155 else if (old_target_percent >= 0 && target_percent_ < old_target_percent) |
| 156 current_icon = decrease_icon_; |
105 | 157 |
106 SkBitmap* icon = increase_icon_; | 158 if (!view_) { |
107 if (!enabled || target_percent_ == 0) | 159 view_ = CreateView(); |
108 icon = disabled_icon_; | 160 view_->Init(current_icon, percent, enabled); |
109 else if (old_target_percent >= 0 && target_percent_ < old_target_percent) | |
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 { | 161 } else { |
147 DCHECK(view_); | 162 // Reset fade sequence, if the bubble is already fading. |
148 hide_timer_.Stop(); | 163 SettingLevelBubbleDelegateView* delegate = |
149 view_->SetIcon(icon); | 164 static_cast<SettingLevelBubbleDelegateView*> |
| 165 (view_->GetWidget()->widget_delegate()); |
| 166 delegate->ResetFade(); |
| 167 view_->SetIcon(current_icon); |
150 view_->SetEnabled(enabled); | 168 view_->SetEnabled(enabled); |
151 } | 169 } |
152 | 170 view_->GetWidget()->Show(); |
| 171 // When the timer runs out, start the fade sequence. |
153 hide_timer_.Start(FROM_HERE, | 172 hide_timer_.Start(FROM_HERE, |
154 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), | 173 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), |
155 this, &SettingLevelBubble::OnHideTimeout); | 174 this, &SettingLevelBubble::OnHideTimeout); |
156 } | 175 } |
157 | 176 |
158 void SettingLevelBubble::HideBubble() { | 177 void SettingLevelBubble::HideBubble() { |
159 if (bubble_) | 178 hide_timer_.Stop(); |
160 bubble_->Close(); | 179 if (view_) { |
| 180 view_->GetWidget()->Close(); |
| 181 view_ = NULL; |
| 182 } |
161 } | 183 } |
162 | 184 |
163 void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, | 185 void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, |
164 bool enabled) { | 186 bool enabled) { |
165 UpdateTargetPercent(percent); | 187 UpdateTargetPercent(percent); |
166 if (view_) | 188 if (view_) |
167 view_->SetEnabled(enabled); | 189 view_->SetEnabled(enabled); |
168 } | 190 } |
169 | 191 |
| 192 SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, |
| 193 SkBitmap* decrease_icon, |
| 194 SkBitmap* disabled_icon) |
| 195 : current_percent_(-1.0), |
| 196 target_percent_(-1.0), |
| 197 increase_icon_(increase_icon), |
| 198 decrease_icon_(decrease_icon), |
| 199 disabled_icon_(disabled_icon), |
| 200 view_(NULL), |
| 201 is_animating_(false) { |
| 202 } |
| 203 |
| 204 SettingLevelBubble::~SettingLevelBubble() { |
| 205 view_ = NULL; |
| 206 } |
| 207 |
| 208 void SettingLevelBubble::OnWidgetClosing(views::Widget* widget) { |
| 209 if (view_ && view_->GetWidget() == widget) { |
| 210 view_->GetWidget()->RemoveObserver(this); |
| 211 view_ = NULL; |
| 212 } |
| 213 // Update states. |
| 214 current_percent_ = target_percent_; |
| 215 target_time_ = TimeTicks(); |
| 216 last_animation_update_time_ = TimeTicks(); |
| 217 last_target_update_time_ = TimeTicks(); |
| 218 hide_timer_.Stop(); |
| 219 StopAnimation(); |
| 220 } |
| 221 |
| 222 SettingLevelBubbleView* SettingLevelBubble::CreateView() { |
| 223 views::Widget* parent = GetToplevelWidget(); |
| 224 SettingLevelBubbleDelegateView* delegate = |
| 225 new SettingLevelBubbleDelegateView(parent); |
| 226 views::Widget* widget = |
| 227 views::BubbleDelegateView::CreateBubble(delegate, parent); |
| 228 widget->AddObserver(this); |
| 229 // Hold on to the content view. |
| 230 return delegate->view(); |
| 231 } |
| 232 |
170 void SettingLevelBubble::OnHideTimeout() { | 233 void SettingLevelBubble::OnHideTimeout() { |
171 HideBubble(); | 234 // Start fading away. |
| 235 if (view_) { |
| 236 SettingLevelBubbleDelegateView* delegate = |
| 237 static_cast<SettingLevelBubbleDelegateView*> |
| 238 (view_->GetWidget()->widget_delegate()); |
| 239 delegate->StartFade(false); |
| 240 } |
172 } | 241 } |
173 | 242 |
174 void SettingLevelBubble::OnAnimationTimeout() { | 243 void SettingLevelBubble::OnAnimationTimeout() { |
175 const TimeTicks now = TimeTicks::Now(); | 244 const TimeTicks now = TimeTicks::Now(); |
176 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); | 245 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); |
177 | 246 |
178 if (remaining_ms <= 0) { | 247 if (remaining_ms <= 0) { |
179 current_percent_ = target_percent_; | 248 current_percent_ = target_percent_; |
180 StopAnimation(); | 249 StopAnimation(); |
181 } else { | 250 } else { |
182 // Figure out what fraction of the total time until we want to reach the | 251 // Figure out what fraction of the total time until we want to reach the |
183 // target has elapsed since the last update. | 252 // target has elapsed since the last update. |
184 const double remaining_percent = target_percent_ - current_percent_; | 253 const double remaining_percent = target_percent_ - current_percent_; |
185 const int64 elapsed_ms = | 254 const int64 elapsed_ms = |
186 (now - last_animation_update_time_).InMilliseconds(); | 255 (now - last_animation_update_time_).InMilliseconds(); |
187 current_percent_ += | 256 current_percent_ += |
188 remaining_percent * | 257 remaining_percent * |
189 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); | 258 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); |
190 } | 259 } |
191 last_animation_update_time_ = now; | 260 last_animation_update_time_ = now; |
192 | 261 |
193 if (view_) | 262 if (view_) |
194 view_->SetLevel(current_percent_); | 263 view_->SetLevel(current_percent_); |
195 } | 264 } |
196 | 265 |
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) { | 266 void SettingLevelBubble::UpdateTargetPercent(double percent) { |
218 target_percent_ = LimitPercent(percent); | 267 target_percent_ = LimitPercent(percent); |
219 const TimeTicks now = TimeTicks::Now(); | 268 const TimeTicks now = TimeTicks::Now(); |
220 | 269 |
221 if (current_percent_ < 0.0) { | 270 if (current_percent_ < 0.0) { |
222 // If we're setting the level for the first time, no need to animate. | 271 // If we're setting the level for the first time, no need to animate. |
223 current_percent_ = target_percent_; | 272 current_percent_ = target_percent_; |
224 if (view_) | 273 if (view_) |
225 view_->SetLevel(current_percent_); | 274 view_->SetLevel(current_percent_); |
226 } else { | 275 } else { |
(...skipping 19 matching lines...) Expand all Loading... |
246 | 295 |
247 last_target_update_time_ = now; | 296 last_target_update_time_ = now; |
248 } | 297 } |
249 | 298 |
250 void SettingLevelBubble::StopAnimation() { | 299 void SettingLevelBubble::StopAnimation() { |
251 animation_timer_.Stop(); | 300 animation_timer_.Stop(); |
252 is_animating_ = false; | 301 is_animating_ = false; |
253 } | 302 } |
254 | 303 |
255 } // namespace chromeos | 304 } // namespace chromeos |
OLD | NEW |