| 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 // static |
| 88 SkBitmap* decrease_icon, | 89 views::Widget* SettingLevelBubble::CreateBubble( |
| 89 SkBitmap* disabled_icon) | 90 SkBitmap* increase_icon, |
| 90 : current_percent_(-1.0), | 91 SkBitmap* decrease_icon, |
| 91 target_percent_(-1.0), | 92 SkBitmap* zero_icon, |
| 92 increase_icon_(increase_icon), | 93 double percent, |
| 93 decrease_icon_(decrease_icon), | 94 bool enabled) { |
| 94 disabled_icon_(disabled_icon), | 95 views::Widget* parent = GetToplevelWidget(); |
| 95 bubble_(NULL), | 96 // Calculate the position in screen coordinates that the bubble should |
| 96 view_(NULL), | 97 // "point" at (since we use BubbleBorder::FLOAT, this position actually |
| 97 is_animating_(false) { | 98 // specifies the center of the bubble). |
| 99 gfx::Rect monitor_area; |
| 100 if (parent) { |
| 101 monitor_area = gfx::Screen::GetMonitorAreaNearestWindow( |
| 102 parent->GetNativeView()); |
| 103 } |
| 104 SettingLevelBubble* delegate = |
| 105 new SettingLevelBubble(increase_icon, decrease_icon, zero_icon, |
| 106 monitor_area); |
| 107 delegate->UpdateWithoutShowingBubble(percent, enabled); |
| 108 delegate->set_close_on_esc(false); |
| 109 views::Widget* widget = views::BubbleDelegateView::CreateBubble( |
| 110 delegate, parent); |
| 111 return widget; |
| 98 } | 112 } |
| 99 | 113 |
| 100 SettingLevelBubble::~SettingLevelBubble() {} | 114 void SettingLevelBubble::ShowBubble(views::Widget* widget, |
| 115 double percent, |
| 116 bool enabled) { |
| 117 DCHECK(widget); |
| 118 SettingLevelBubble* delegate = |
| 119 static_cast<SettingLevelBubble*>(widget->widget_delegate()); |
| 120 delegate->UpdateWithoutShowingBubble(percent, enabled); |
| 121 // Show the window and start the timer. When the timer |
| 122 // runs out, we start the fade sequence. |
| 123 widget->Show(); |
| 124 delegate->StartHideTimer(); |
| 125 } |
| 101 | 126 |
| 102 void SettingLevelBubble::ShowBubble(double percent, bool enabled) { | 127 SettingLevelBubble::~SettingLevelBubble() { |
| 103 const double old_target_percent = target_percent_; | 128 } |
| 104 UpdateTargetPercent(percent); | |
| 105 | 129 |
| 106 SkBitmap* icon = increase_icon_; | 130 gfx::Point SettingLevelBubble::GetAnchorPoint() const { |
| 107 if (!enabled || target_percent_ == 0) | 131 return (gfx::Point( |
| 108 icon = disabled_icon_; | 132 monitor_area_.x() + kBubbleXRatio * monitor_area_.width(), |
| 109 else if (old_target_percent >= 0 && target_percent_ < old_target_percent) | 133 monitor_area_.bottom() - view_size_.height() / 2 - kBubbleBottomGap)); |
| 110 icon = decrease_icon_; | 134 } |
| 111 | 135 |
| 112 if (!bubble_) { | 136 void SettingLevelBubble::WindowClosing() { |
| 113 views::Widget* parent_widget = GetToplevelWidget(); | 137 hide_timer_.Stop(); |
| 114 if (parent_widget == NULL) { | 138 StopAnimation(); |
| 115 LOG(WARNING) << "Unable to locate parent widget to display a bubble"; | 139 view_ = NULL; |
| 116 return; | 140 current_percent_ = target_percent_; |
| 117 } | 141 target_time_ = TimeTicks(); |
| 118 DCHECK(view_ == NULL); | 142 last_animation_update_time_ = TimeTicks(); |
| 119 view_ = new SettingLevelBubbleView; | 143 last_target_update_time_ = TimeTicks(); |
| 120 view_->Init(icon, current_percent_, enabled); | 144 } |
| 121 | 145 |
| 122 // Calculate the position in screen coordinates that the bubble should | 146 void SettingLevelBubble::StartHideTimer() { |
| 123 // "point" at (since we use BubbleBorder::FLOAT, this position actually | 147 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, | 148 hide_timer_.Start(FROM_HERE, |
| 154 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), | 149 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), |
| 155 this, &SettingLevelBubble::OnHideTimeout); | 150 this, &SettingLevelBubble::OnHideTimeout); |
| 156 } | 151 } |
| 157 | 152 |
| 158 void SettingLevelBubble::HideBubble() { | 153 void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, |
| 159 if (bubble_) | 154 bool enabled) { |
| 160 bubble_->Close(); | 155 enabled_ = enabled; |
| 156 const double old_target_percent = target_percent_; |
| 157 UpdateTargetPercent(percent); |
| 158 current_icon_ = increase_icon_; |
| 159 if (!enabled || target_percent_ == 0) |
| 160 current_icon_ = disabled_icon_; |
| 161 else if (old_target_percent >= 0 && target_percent_ < old_target_percent) |
| 162 current_icon_ = decrease_icon_; |
| 163 if (view_) { |
| 164 view_->SetEnabled(enabled); |
| 165 view_->SetIcon(current_icon_); |
| 166 } |
| 161 } | 167 } |
| 162 | 168 |
| 163 void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, | 169 void SettingLevelBubble::Init() { |
| 164 bool enabled) { | 170 SetLayoutManager(new views::FillLayout()); |
| 165 UpdateTargetPercent(percent); | 171 view_ = new SettingLevelBubbleView(); |
| 166 if (view_) | 172 view_->Init(current_icon_, current_percent_, enabled_); |
| 167 view_->SetEnabled(enabled); | 173 AddChildView(view_); |
| 174 view_size_ = GetPreferredSize(); |
| 175 } |
| 176 |
| 177 SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, |
| 178 SkBitmap* decrease_icon, |
| 179 SkBitmap* disabled_icon, |
| 180 const gfx::Rect& monitor_area) |
| 181 : BubbleDelegateView(gfx::Point(), |
| 182 views::BubbleBorder::FLOAT, |
| 183 SK_ColorWHITE), |
| 184 current_percent_(-1.0), |
| 185 target_percent_(-1.0), |
| 186 increase_icon_(increase_icon), |
| 187 decrease_icon_(decrease_icon), |
| 188 disabled_icon_(disabled_icon), |
| 189 current_icon_(NULL), |
| 190 enabled_(false), |
| 191 view_(NULL), |
| 192 is_animating_(false), |
| 193 monitor_area_(monitor_area) { |
| 168 } | 194 } |
| 169 | 195 |
| 170 void SettingLevelBubble::OnHideTimeout() { | 196 void SettingLevelBubble::OnHideTimeout() { |
| 171 HideBubble(); | 197 // Start fading away. |
| 198 StartFade(false); |
| 172 } | 199 } |
| 173 | 200 |
| 174 void SettingLevelBubble::OnAnimationTimeout() { | 201 void SettingLevelBubble::OnAnimationTimeout() { |
| 175 const TimeTicks now = TimeTicks::Now(); | 202 const TimeTicks now = TimeTicks::Now(); |
| 176 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); | 203 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); |
| 177 | 204 |
| 178 if (remaining_ms <= 0) { | 205 if (remaining_ms <= 0) { |
| 179 current_percent_ = target_percent_; | 206 current_percent_ = target_percent_; |
| 180 StopAnimation(); | 207 StopAnimation(); |
| 181 } else { | 208 } else { |
| 182 // Figure out what fraction of the total time until we want to reach the | 209 // Figure out what fraction of the total time until we want to reach the |
| 183 // target has elapsed since the last update. | 210 // target has elapsed since the last update. |
| 184 const double remaining_percent = target_percent_ - current_percent_; | 211 const double remaining_percent = target_percent_ - current_percent_; |
| 185 const int64 elapsed_ms = | 212 const int64 elapsed_ms = |
| 186 (now - last_animation_update_time_).InMilliseconds(); | 213 (now - last_animation_update_time_).InMilliseconds(); |
| 187 current_percent_ += | 214 current_percent_ += |
| 188 remaining_percent * | 215 remaining_percent * |
| 189 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); | 216 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); |
| 190 } | 217 } |
| 191 last_animation_update_time_ = now; | 218 last_animation_update_time_ = now; |
| 192 | 219 |
| 193 if (view_) | 220 if (view_) |
| 194 view_->SetLevel(current_percent_); | 221 view_->SetLevel(current_percent_); |
| 195 } | 222 } |
| 196 | 223 |
| 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) { | 224 void SettingLevelBubble::UpdateTargetPercent(double percent) { |
| 218 target_percent_ = LimitPercent(percent); | 225 target_percent_ = LimitPercent(percent); |
| 219 const TimeTicks now = TimeTicks::Now(); | 226 const TimeTicks now = TimeTicks::Now(); |
| 220 | 227 |
| 221 if (current_percent_ < 0.0) { | 228 if (current_percent_ < 0.0) { |
| 222 // If we're setting the level for the first time, no need to animate. | 229 // If we're setting the level for the first time, no need to animate. |
| 223 current_percent_ = target_percent_; | 230 current_percent_ = target_percent_; |
| 224 if (view_) | 231 if (view_) |
| 225 view_->SetLevel(current_percent_); | 232 view_->SetLevel(current_percent_); |
| 226 } else { | 233 } else { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 246 | 253 |
| 247 last_target_update_time_ = now; | 254 last_target_update_time_ = now; |
| 248 } | 255 } |
| 249 | 256 |
| 250 void SettingLevelBubble::StopAnimation() { | 257 void SettingLevelBubble::StopAnimation() { |
| 251 animation_timer_.Stop(); | 258 animation_timer_.Stop(); |
| 252 is_animating_ = false; | 259 is_animating_ = false; |
| 253 } | 260 } |
| 254 | 261 |
| 255 } // namespace chromeos | 262 } // namespace chromeos |
| OLD | NEW |