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/bubble/bubble_view.h" | |
msw
2011/10/20 19:36:13
Merge and remove bubble_view.h soon.
alicet1
2011/10/20 22:36:30
Done.
| |
20 #include "views/layout/fill_layout.h" | |
19 #include "views/widget/root_view.h" | 21 #include "views/widget/root_view.h" |
20 | 22 |
21 using base::TimeDelta; | 23 using base::TimeDelta; |
22 using base::TimeTicks; | 24 using base::TimeTicks; |
23 using std::max; | 25 using std::max; |
24 using std::min; | 26 using std::min; |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
28 // How long should the bubble be shown onscreen whenever the setting changes? | 30 // 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()); | 79 window = GTK_WINDOW(background->GetNativeWindow()); |
78 #endif | 80 #endif |
79 } | 81 } |
80 | 82 |
81 if (window) | 83 if (window) |
82 return views::Widget::GetWidgetForNativeWindow(window); | 84 return views::Widget::GetWidgetForNativeWindow(window); |
83 else | 85 else |
84 return WebUILoginDisplay::GetLoginWindow(); | 86 return WebUILoginDisplay::GetLoginWindow(); |
85 } | 87 } |
86 | 88 |
89 // static | |
90 views::Widget* SettingLevelBubble::CreateSettingLevelBubble( | |
91 SkBitmap* increase_icon, | |
92 SkBitmap* decrease_icon, | |
93 SkBitmap* zero_icon, | |
94 double percent, | |
95 bool enabled) { | |
96 views::Widget* parent = GetToplevelWidget(); | |
97 SettingLevelBubble* delegate = | |
98 new SettingLevelBubble(increase_icon, decrease_icon, zero_icon); | |
99 delegate->UpdateSetting(percent, enabled); | |
100 // Construct and initialize settings. | |
101 views::Widget* widget = views::BubbleDelegateView::CreateBubble( | |
102 delegate, parent); | |
103 widget->client_view()->AsBubbleView()->set_close_on_esc(false); | |
msw
2011/10/20 19:36:13
Merge for BubbleDelegateView::set_close_on_esc
alicet1
2011/10/20 22:36:30
Done.
| |
104 return widget; | |
105 } | |
106 | |
87 SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, | 107 SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, |
88 SkBitmap* decrease_icon, | 108 SkBitmap* decrease_icon, |
89 SkBitmap* disabled_icon) | 109 SkBitmap* disabled_icon) |
90 : current_percent_(-1.0), | 110 : current_percent_(-1.0), |
91 target_percent_(-1.0), | 111 target_percent_(-1.0), |
92 increase_icon_(increase_icon), | 112 increase_icon_(increase_icon), |
93 decrease_icon_(decrease_icon), | 113 decrease_icon_(decrease_icon), |
94 disabled_icon_(disabled_icon), | 114 disabled_icon_(disabled_icon), |
95 bubble_(NULL), | 115 current_icon_(NULL), |
96 view_(NULL), | 116 view_(NULL), |
97 is_animating_(false) { | 117 is_animating_(false) { |
98 } | 118 } |
99 | 119 |
100 SettingLevelBubble::~SettingLevelBubble() {} | 120 SettingLevelBubble::~SettingLevelBubble() { |
121 WindowClosing(); | |
msw
2011/10/20 19:36:13
This shouldn't be necessary.
alicet1
2011/10/20 22:36:30
Done.
| |
122 } | |
101 | 123 |
102 void SettingLevelBubble::ShowBubble(double percent, bool enabled) { | 124 void SettingLevelBubble::StartHideTimer() { |
125 hide_timer_.Stop(); | |
126 hide_timer_.Start(FROM_HERE, | |
127 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), | |
128 this, &SettingLevelBubble::OnHideTimeout); | |
129 } | |
130 | |
131 void SettingLevelBubble::Init() { | |
132 SetLayoutManager(new views::FillLayout()); | |
133 view_ = new SettingLevelBubbleView(); | |
134 | |
135 view_->Init(current_icon_, current_percent_, enabled_); | |
136 CalculateAnchorPoint(); | |
msw
2011/10/20 19:36:13
I wonder if you can calculate the anchor point ear
alicet1
2011/10/20 22:36:30
yeah, I need the view first.
| |
137 AddChildView(view_); | |
138 } | |
139 | |
140 gfx::Point SettingLevelBubble::GetAnchorPoint() const { | |
141 return anchor_point_; | |
142 } | |
143 | |
144 void SettingLevelBubble::UpdateSetting(double percent, | |
145 bool enabled) { | |
msw
2011/10/20 19:36:13
This fits on one line.
alicet1
2011/10/20 22:36:30
Done.
| |
146 enabled_ = enabled; | |
103 const double old_target_percent = target_percent_; | 147 const double old_target_percent = target_percent_; |
104 UpdateTargetPercent(percent); | 148 UpdateTargetPercent(percent); |
105 | 149 current_icon_ = increase_icon_; |
106 SkBitmap* icon = increase_icon_; | |
107 if (!enabled || target_percent_ == 0) | 150 if (!enabled || target_percent_ == 0) |
108 icon = disabled_icon_; | 151 current_icon_ = disabled_icon_; |
109 else if (old_target_percent >= 0 && target_percent_ < old_target_percent) | 152 else if (old_target_percent >= 0 && target_percent_ < old_target_percent) |
110 icon = decrease_icon_; | 153 current_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, | |
154 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), | |
155 this, &SettingLevelBubble::OnHideTimeout); | |
156 } | 154 } |
157 | 155 |
158 void SettingLevelBubble::HideBubble() { | 156 views::BubbleBorder::ArrowLocation |
159 if (bubble_) | 157 SettingLevelBubble::GetArrowLocation() const { |
160 bubble_->Close(); | 158 return views::BubbleBorder::FLOAT; |
159 } | |
160 | |
161 void SettingLevelBubble::WindowClosing() { | |
162 hide_timer_.Stop(); | |
163 StopAnimation(); | |
164 view_ = NULL; | |
165 current_percent_ = target_percent_; | |
166 target_time_ = TimeTicks(); | |
167 last_animation_update_time_ = TimeTicks(); | |
168 last_target_update_time_ = TimeTicks(); | |
161 } | 169 } |
162 | 170 |
163 void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, | 171 void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, |
164 bool enabled) { | 172 bool enabled) { |
165 UpdateTargetPercent(percent); | 173 UpdateTargetPercent(percent); |
166 if (view_) | 174 if (view_) |
167 view_->SetEnabled(enabled); | 175 view_->SetEnabled(enabled); |
168 } | 176 } |
169 | 177 |
178 void SettingLevelBubble::CalculateAnchorPoint() { | |
179 // Calculate the position in screen coordinates that the bubble should | |
180 // "point" at (since we use BubbleBorder::FLOAT, this position actually | |
181 // specifies the center of the bubble). | |
182 const gfx::Rect monitor_area = | |
183 gfx::Screen::GetMonitorAreaNearestWindow( | |
184 GetWidget()->GetTopLevelWidget()->GetNativeView()); | |
185 gfx::Size view_size = GetPreferredSize(); | |
186 anchor_point_ = gfx::Point( | |
187 monitor_area.x() + kBubbleXRatio * monitor_area.width(), | |
188 monitor_area.bottom() - view_size.height() / 2 - kBubbleBottomGap); | |
189 } | |
190 | |
170 void SettingLevelBubble::OnHideTimeout() { | 191 void SettingLevelBubble::OnHideTimeout() { |
171 HideBubble(); | 192 GetWidget()->Close(); |
172 } | 193 } |
173 | 194 |
174 void SettingLevelBubble::OnAnimationTimeout() { | 195 void SettingLevelBubble::OnAnimationTimeout() { |
175 const TimeTicks now = TimeTicks::Now(); | 196 const TimeTicks now = TimeTicks::Now(); |
176 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); | 197 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); |
177 | 198 |
178 if (remaining_ms <= 0) { | 199 if (remaining_ms <= 0) { |
179 current_percent_ = target_percent_; | 200 current_percent_ = target_percent_; |
180 StopAnimation(); | 201 StopAnimation(); |
181 } else { | 202 } else { |
182 // Figure out what fraction of the total time until we want to reach the | 203 // Figure out what fraction of the total time until we want to reach the |
183 // target has elapsed since the last update. | 204 // target has elapsed since the last update. |
184 const double remaining_percent = target_percent_ - current_percent_; | 205 const double remaining_percent = target_percent_ - current_percent_; |
185 const int64 elapsed_ms = | 206 const int64 elapsed_ms = |
186 (now - last_animation_update_time_).InMilliseconds(); | 207 (now - last_animation_update_time_).InMilliseconds(); |
187 current_percent_ += | 208 current_percent_ += |
188 remaining_percent * | 209 remaining_percent * |
189 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); | 210 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); |
190 } | 211 } |
191 last_animation_update_time_ = now; | 212 last_animation_update_time_ = now; |
192 | 213 |
193 if (view_) | 214 if (view_) |
194 view_->SetLevel(current_percent_); | 215 view_->SetLevel(current_percent_); |
195 } | 216 } |
196 | 217 |
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) { | 218 void SettingLevelBubble::UpdateTargetPercent(double percent) { |
218 target_percent_ = LimitPercent(percent); | 219 target_percent_ = LimitPercent(percent); |
219 const TimeTicks now = TimeTicks::Now(); | 220 const TimeTicks now = TimeTicks::Now(); |
220 | 221 |
221 if (current_percent_ < 0.0) { | 222 if (current_percent_ < 0.0) { |
222 // If we're setting the level for the first time, no need to animate. | 223 // If we're setting the level for the first time, no need to animate. |
223 current_percent_ = target_percent_; | 224 current_percent_ = target_percent_; |
224 if (view_) | 225 if (view_) |
225 view_->SetLevel(current_percent_); | 226 view_->SetLevel(current_percent_); |
226 } else { | 227 } else { |
(...skipping 19 matching lines...) Expand all Loading... | |
246 | 247 |
247 last_target_update_time_ = now; | 248 last_target_update_time_ = now; |
248 } | 249 } |
249 | 250 |
250 void SettingLevelBubble::StopAnimation() { | 251 void SettingLevelBubble::StopAnimation() { |
251 animation_timer_.Stop(); | 252 animation_timer_.Stop(); |
252 is_animating_ = false; | 253 is_animating_ = false; |
253 } | 254 } |
254 | 255 |
255 } // namespace chromeos | 256 } // namespace chromeos |
OLD | NEW |