Chromium Code Reviews| Index: chrome/browser/chromeos/setting_level_bubble.cc |
| diff --git a/chrome/browser/chromeos/setting_level_bubble.cc b/chrome/browser/chromeos/setting_level_bubble.cc |
| index c546f88455987beeed36cab168edef82d1719c95..c20f7f31d217b7cd427b74f2d74d7dc4930010c8 100644 |
| --- a/chrome/browser/chromeos/setting_level_bubble.cc |
| +++ b/chrome/browser/chromeos/setting_level_bubble.cc |
| @@ -14,8 +14,9 @@ |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/browser/ui/browser_list.h" |
| #include "chrome/browser/ui/browser_window.h" |
| -#include "chrome/browser/ui/views/bubble/bubble.h" |
| #include "ui/gfx/screen.h" |
| +#include "views/bubble/bubble_delegate.h" |
| +#include "views/layout/fill_layout.h" |
| #include "views/widget/root_view.h" |
| using base::TimeDelta; |
| @@ -84,91 +85,117 @@ static views::Widget* GetToplevelWidget() { |
| return WebUILoginDisplay::GetLoginWindow(); |
| } |
| -SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, |
| - SkBitmap* decrease_icon, |
| - SkBitmap* disabled_icon) |
| - : current_percent_(-1.0), |
| - target_percent_(-1.0), |
| - increase_icon_(increase_icon), |
| - decrease_icon_(decrease_icon), |
| - disabled_icon_(disabled_icon), |
| - bubble_(NULL), |
| - view_(NULL), |
| - is_animating_(false) { |
| +// static |
| +views::Widget* SettingLevelBubble::CreateBubble( |
| + SkBitmap* increase_icon, |
| + SkBitmap* decrease_icon, |
| + SkBitmap* zero_icon, |
| + double percent, |
| + bool enabled) { |
| + views::Widget* parent = GetToplevelWidget(); |
| + // Calculate the position in screen coordinates that the bubble should |
| + // "point" at (since we use BubbleBorder::FLOAT, this position actually |
| + // specifies the center of the bubble). |
| + gfx::Rect monitor_area; |
| + if (parent) { |
| + monitor_area = gfx::Screen::GetMonitorAreaNearestWindow( |
| + parent->GetNativeView()); |
| + } |
| + SettingLevelBubble* delegate = |
| + new SettingLevelBubble(increase_icon, decrease_icon, zero_icon, |
| + monitor_area); |
| + delegate->UpdateWithoutShowingBubble(percent, enabled); |
| + delegate->set_close_on_esc(false); |
| + views::Widget* widget = views::BubbleDelegateView::CreateBubble( |
| + delegate, parent); |
| + return widget; |
| } |
| -SettingLevelBubble::~SettingLevelBubble() {} |
| +void SettingLevelBubble::ShowBubble(views::Widget* widget, |
| + double percent, |
| + bool enabled) { |
| + DCHECK(widget); |
| + SettingLevelBubble* delegate = |
| + static_cast<SettingLevelBubble*>(widget->widget_delegate()); |
| + delegate->UpdateWithoutShowingBubble(percent, enabled); |
| + // Show the window and start the timer. When the timer |
| + // runs out, we start the fade sequence. |
| + widget->Show(); |
| + delegate->StartHideTimer(); |
| +} |
| -void SettingLevelBubble::ShowBubble(double percent, bool enabled) { |
| - const double old_target_percent = target_percent_; |
| - UpdateTargetPercent(percent); |
| +SettingLevelBubble::~SettingLevelBubble() { |
| +} |
| - SkBitmap* icon = increase_icon_; |
| - if (!enabled || target_percent_ == 0) |
| - icon = disabled_icon_; |
| - else if (old_target_percent >= 0 && target_percent_ < old_target_percent) |
| - icon = decrease_icon_; |
| +gfx::Point SettingLevelBubble::GetAnchorPoint() const { |
|
alicet1
2011/10/24 17:56:56
msw: I think we can consider making GetAnchorPoint
msw
2011/10/24 19:38:22
That's reasonable, you can go ahead and change tha
|
| + return (gfx::Point( |
| + monitor_area_.x() + kBubbleXRatio * monitor_area_.width(), |
| + monitor_area_.bottom() - view_size_.height() / 2 - kBubbleBottomGap)); |
| +} |
| - if (!bubble_) { |
| - views::Widget* parent_widget = GetToplevelWidget(); |
| - if (parent_widget == NULL) { |
| - LOG(WARNING) << "Unable to locate parent widget to display a bubble"; |
| - return; |
| - } |
| - DCHECK(view_ == NULL); |
| - view_ = new SettingLevelBubbleView; |
| - view_->Init(icon, current_percent_, enabled); |
| - |
| - // Calculate the position in screen coordinates that the bubble should |
| - // "point" at (since we use BubbleBorder::FLOAT, this position actually |
| - // specifies the center of the bubble). |
| - const gfx::Rect monitor_area = |
| - gfx::Screen::GetMonitorAreaNearestWindow( |
| - parent_widget->GetNativeView()); |
| - const gfx::Size view_size = view_->GetPreferredSize(); |
| - const gfx::Rect position_relative_to( |
| - monitor_area.x() + kBubbleXRatio * monitor_area.width(), |
| - monitor_area.bottom() - view_size.height() / 2 - kBubbleBottomGap, |
| - 0, 0); |
| - |
| - bubble_ = Bubble::ShowFocusless(parent_widget, |
| - position_relative_to, |
| - views::BubbleBorder::FLOAT, |
| - view_, // contents |
| - this, // delegate |
| - true); // show while screen is locked |
| - // TODO(derat): We probably shouldn't be using Bubble. It'd be nice to call |
| - // bubble_->set_fade_away_on_close(true) here, but then, if ShowBubble() |
| - // gets called while the bubble is fading away, we end up just adjusting the |
| - // value on the disappearing bubble; ideally we'd have a way to cancel the |
| - // fade and show the bubble at full opacity for another |
| - // kBubbleShowTimeoutMs. |
| - } else { |
| - DCHECK(view_); |
| - hide_timer_.Stop(); |
| - view_->SetIcon(icon); |
| - view_->SetEnabled(enabled); |
| - } |
| +void SettingLevelBubble::WindowClosing() { |
| + hide_timer_.Stop(); |
| + StopAnimation(); |
| + view_ = NULL; |
| + current_percent_ = target_percent_; |
| + target_time_ = TimeTicks(); |
| + last_animation_update_time_ = TimeTicks(); |
| + last_target_update_time_ = TimeTicks(); |
| +} |
| +void SettingLevelBubble::StartHideTimer() { |
| + hide_timer_.Stop(); |
| hide_timer_.Start(FROM_HERE, |
| base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), |
| this, &SettingLevelBubble::OnHideTimeout); |
| } |
| -void SettingLevelBubble::HideBubble() { |
| - if (bubble_) |
| - bubble_->Close(); |
| -} |
| - |
| void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, |
| bool enabled) { |
| + enabled_ = enabled; |
| + const double old_target_percent = target_percent_; |
| UpdateTargetPercent(percent); |
| - if (view_) |
| + current_icon_ = increase_icon_; |
| + if (!enabled || target_percent_ == 0) |
| + current_icon_ = disabled_icon_; |
| + else if (old_target_percent >= 0 && target_percent_ < old_target_percent) |
| + current_icon_ = decrease_icon_; |
| + if (view_) { |
| view_->SetEnabled(enabled); |
| + view_->SetIcon(current_icon_); |
| + } |
| +} |
| + |
| +void SettingLevelBubble::Init() { |
| + SetLayoutManager(new views::FillLayout()); |
| + view_ = new SettingLevelBubbleView(); |
| + view_->Init(current_icon_, current_percent_, enabled_); |
| + AddChildView(view_); |
| + view_size_ = GetPreferredSize(); |
| +} |
| + |
| +SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, |
| + SkBitmap* decrease_icon, |
| + SkBitmap* disabled_icon, |
| + const gfx::Rect& monitor_area) |
| + : BubbleDelegateView(gfx::Point(), |
| + views::BubbleBorder::FLOAT, |
| + SK_ColorWHITE), |
| + current_percent_(-1.0), |
| + target_percent_(-1.0), |
| + increase_icon_(increase_icon), |
| + decrease_icon_(decrease_icon), |
| + disabled_icon_(disabled_icon), |
| + current_icon_(NULL), |
| + enabled_(false), |
| + view_(NULL), |
| + is_animating_(false), |
| + monitor_area_(monitor_area) { |
| } |
| void SettingLevelBubble::OnHideTimeout() { |
| - HideBubble(); |
| + // Start fading away. |
| + StartFade(false); |
| } |
| void SettingLevelBubble::OnAnimationTimeout() { |
| @@ -194,26 +221,6 @@ void SettingLevelBubble::OnAnimationTimeout() { |
| view_->SetLevel(current_percent_); |
| } |
| -void SettingLevelBubble::BubbleClosing(Bubble* bubble, bool) { |
| - DCHECK(bubble == bubble_); |
| - hide_timer_.Stop(); |
| - StopAnimation(); |
| - bubble_ = NULL; |
| - view_ = NULL; |
| - current_percent_ = target_percent_; |
| - target_time_ = TimeTicks(); |
| - last_animation_update_time_ = TimeTicks(); |
| - last_target_update_time_ = TimeTicks(); |
| -} |
| - |
| -bool SettingLevelBubble::CloseOnEscape() { |
| - return true; |
| -} |
| - |
| -bool SettingLevelBubble::FadeInOnShow() { |
| - return false; |
| -} |
| - |
| void SettingLevelBubble::UpdateTargetPercent(double percent) { |
| target_percent_ = LimitPercent(percent); |
| const TimeTicks now = TimeTicks::Now(); |