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" | |
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 23 matching lines...) Expand all Loading... | |
52 namespace chromeos { | 54 namespace chromeos { |
53 | 55 |
54 // Temporary helper routine. Tries to first return the widget from the | 56 // Temporary helper routine. Tries to first return the widget from the |
55 // most-recently-focused normal browser window, then from a login | 57 // most-recently-focused normal browser window, then from a login |
56 // background, and finally NULL if both of those fail. | 58 // background, and finally NULL if both of those fail. |
57 // TODO(glotov): remove this in favor of enabling Bubble class act | 59 // TODO(glotov): remove this in favor of enabling Bubble class act |
58 // without |parent| specified. crosbug.com/4025 | 60 // without |parent| specified. crosbug.com/4025 |
59 static views::Widget* GetToplevelWidget() { | 61 static views::Widget* GetToplevelWidget() { |
60 #if defined(USE_AURA) | 62 #if defined(USE_AURA) |
61 // TODO(saintlou): Need to fix in PureViews. | 63 // TODO(saintlou): Need to fix in PureViews. |
64 // Is this the same as using parent->GetRootView()? | |
Daniel Erat
2011/10/19 17:28:09
yeah, i think that the root view is what we want h
alicet1
2011/10/20 15:03:08
saintlou uploaded a fix to this code. let me know
| |
62 return WebUILoginDisplay::GetLoginWindow(); | 65 return WebUILoginDisplay::GetLoginWindow(); |
63 #else | 66 #else |
64 GtkWindow* window = NULL; | 67 GtkWindow* window = NULL; |
65 | 68 |
66 // We just use the default profile here -- this gets overridden as needed | 69 // We just use the default profile here -- this gets overridden as needed |
67 // in Chrome OS depending on whether the user is logged in or not. | 70 // in Chrome OS depending on whether the user is logged in or not. |
68 Browser* browser = | 71 Browser* browser = |
69 BrowserList::FindTabbedBrowser( | 72 BrowserList::FindTabbedBrowser( |
70 ProfileManager::GetDefaultProfile(), | 73 ProfileManager::GetDefaultProfile(), |
71 true); // match_incognito | 74 true); // match_incognito |
72 if (browser) { | 75 if (browser) { |
73 window = GTK_WINDOW(browser->window()->GetNativeHandle()); | 76 window = GTK_WINDOW(browser->window()->GetNativeHandle()); |
74 } else { | 77 } else { |
75 // Otherwise, see if there's a background window that we can use. | 78 // Otherwise, see if there's a background window that we can use. |
76 BackgroundView* background = LoginUtils::Get()->GetBackgroundView(); | 79 BackgroundView* background = LoginUtils::Get()->GetBackgroundView(); |
77 if (background) | 80 if (background) |
78 window = GTK_WINDOW(background->GetNativeWindow()); | 81 window = GTK_WINDOW(background->GetNativeWindow()); |
79 } | 82 } |
80 | 83 |
81 if (window) | 84 if (window) |
82 return views::Widget::GetWidgetForNativeWindow(window); | 85 return views::Widget::GetWidgetForNativeWindow(window); |
83 else | 86 else |
84 return WebUILoginDisplay::GetLoginWindow(); | 87 return WebUILoginDisplay::GetLoginWindow(); |
85 #endif | 88 #endif |
86 } | 89 } |
87 | 90 |
91 // static | |
92 views::Widget* SettingLevelBubble::ConstructSettingLevelBubble( | |
93 SkBitmap* increase_icon, | |
94 SkBitmap* decrease_icon, | |
95 SkBitmap* zero_icon, | |
96 double percent, | |
97 bool enabled) { | |
98 views::Widget* parent = GetToplevelWidget(); | |
99 SettingLevelBubble* delegate = | |
100 new SettingLevelBubble(increase_icon, decrease_icon, zero_icon); | |
101 delegate->UpdateSettingLevelInternal(percent, enabled); | |
102 // Construct and initialize settings. | |
103 views::Widget* widget = views::BubbleDelegateView::CreateBubble( | |
104 delegate, parent); | |
105 widget->client_view()->AsBubbleView()->set_close_on_esc(false); | |
106 return widget; | |
107 } | |
108 | |
88 SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, | 109 SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, |
89 SkBitmap* decrease_icon, | 110 SkBitmap* decrease_icon, |
90 SkBitmap* disabled_icon) | 111 SkBitmap* disabled_icon) |
91 : current_percent_(-1.0), | 112 : current_percent_(-1.0), |
92 target_percent_(-1.0), | 113 target_percent_(-1.0), |
93 increase_icon_(increase_icon), | 114 increase_icon_(increase_icon), |
94 decrease_icon_(decrease_icon), | 115 decrease_icon_(decrease_icon), |
95 disabled_icon_(disabled_icon), | 116 disabled_icon_(disabled_icon), |
96 bubble_(NULL), | 117 current_icon_(NULL), |
97 view_(NULL), | 118 view_(NULL), |
98 is_animating_(false) { | 119 is_animating_(false) { |
99 } | 120 } |
100 | 121 |
101 SettingLevelBubble::~SettingLevelBubble() {} | 122 SettingLevelBubble::~SettingLevelBubble() { |
123 WindowClosing(); | |
124 } | |
102 | 125 |
103 void SettingLevelBubble::ShowBubble(double percent, bool enabled) { | 126 void SettingLevelBubble::StartHideTimer() { |
127 hide_timer_.Stop(); | |
128 hide_timer_.Start(FROM_HERE, | |
129 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), | |
130 this, &SettingLevelBubble::OnHideTimeout); | |
131 } | |
132 | |
133 void SettingLevelBubble::Init() { | |
134 SetLayoutManager(new views::FillLayout()); | |
135 view_ = new SettingLevelBubbleView(); | |
136 | |
137 view_->Init(current_icon_, current_percent_, enabled_); | |
138 CalculateAnchorPoint(); | |
139 AddChildView(view_); | |
140 } | |
141 | |
142 gfx::Point SettingLevelBubble::GetAnchorPoint() const { | |
143 return anchor_point_; | |
144 } | |
145 | |
146 void SettingLevelBubble::UpdateSettingLevelInternal(double percent, | |
147 bool enabled) { | |
148 enabled_ = enabled; | |
104 const double old_target_percent = target_percent_; | 149 const double old_target_percent = target_percent_; |
105 UpdateTargetPercent(percent); | 150 UpdateTargetPercent(percent); |
106 | 151 current_icon_ = increase_icon_; |
107 SkBitmap* icon = increase_icon_; | |
108 if (!enabled || target_percent_ == 0) | 152 if (!enabled || target_percent_ == 0) |
109 icon = disabled_icon_; | 153 current_icon_ = disabled_icon_; |
110 else if (old_target_percent >= 0 && target_percent_ < old_target_percent) | 154 else if (old_target_percent >= 0 && target_percent_ < old_target_percent) |
111 icon = decrease_icon_; | 155 current_icon_ = decrease_icon_; |
112 | |
113 if (!bubble_) { | |
114 views::Widget* parent_widget = GetToplevelWidget(); | |
115 if (parent_widget == NULL) { | |
116 LOG(WARNING) << "Unable to locate parent widget to display a bubble"; | |
117 return; | |
118 } | |
119 DCHECK(view_ == NULL); | |
120 view_ = new SettingLevelBubbleView; | |
121 view_->Init(icon, current_percent_, enabled); | |
122 | |
123 // Calculate the position in screen coordinates that the bubble should | |
124 // "point" at (since we use BubbleBorder::FLOAT, this position actually | |
125 // specifies the center of the bubble). | |
126 const gfx::Rect monitor_area = | |
127 gfx::Screen::GetMonitorAreaNearestWindow( | |
128 parent_widget->GetNativeView()); | |
129 const gfx::Size view_size = view_->GetPreferredSize(); | |
130 const gfx::Rect position_relative_to( | |
131 monitor_area.x() + kBubbleXRatio * monitor_area.width(), | |
132 monitor_area.bottom() - view_size.height() / 2 - kBubbleBottomGap, | |
133 0, 0); | |
134 | |
135 bubble_ = Bubble::ShowFocusless(parent_widget, | |
136 position_relative_to, | |
137 views::BubbleBorder::FLOAT, | |
138 view_, // contents | |
139 this, // delegate | |
140 true); // show while screen is locked | |
141 // TODO(derat): We probably shouldn't be using Bubble. It'd be nice to call | |
142 // bubble_->set_fade_away_on_close(true) here, but then, if ShowBubble() | |
143 // gets called while the bubble is fading away, we end up just adjusting the | |
144 // value on the disappearing bubble; ideally we'd have a way to cancel the | |
145 // fade and show the bubble at full opacity for another | |
146 // kBubbleShowTimeoutMs. | |
147 } else { | |
148 DCHECK(view_); | |
149 hide_timer_.Stop(); | |
150 view_->SetIcon(icon); | |
151 view_->SetEnabled(enabled); | |
152 } | |
153 | |
154 hide_timer_.Start(FROM_HERE, | |
155 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), | |
156 this, &SettingLevelBubble::OnHideTimeout); | |
157 } | 156 } |
158 | 157 |
159 void SettingLevelBubble::HideBubble() { | 158 views::BubbleBorder::ArrowLocation |
160 if (bubble_) | 159 SettingLevelBubble::GetArrowLocation() const { |
161 bubble_->Close(); | 160 return views::BubbleBorder::FLOAT; |
161 } | |
162 | |
163 void SettingLevelBubble::WindowClosing() { | |
164 hide_timer_.Stop(); | |
165 StopAnimation(); | |
166 view_ = NULL; | |
167 current_percent_ = target_percent_; | |
168 target_time_ = TimeTicks(); | |
169 last_animation_update_time_ = TimeTicks(); | |
170 last_target_update_time_ = TimeTicks(); | |
162 } | 171 } |
163 | 172 |
164 void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, | 173 void SettingLevelBubble::UpdateWithoutShowingBubble(double percent, |
165 bool enabled) { | 174 bool enabled) { |
166 UpdateTargetPercent(percent); | 175 UpdateTargetPercent(percent); |
167 if (view_) | 176 if (view_) |
168 view_->SetEnabled(enabled); | 177 view_->SetEnabled(enabled); |
169 } | 178 } |
170 | 179 |
180 void SettingLevelBubble::CalculateAnchorPoint() { | |
181 // Calculate the position in screen coordinates that the bubble should | |
182 // "point" at (since we use BubbleBorder::FLOAT, this position actually | |
183 // specifies the center of the bubble). | |
184 const gfx::Rect monitor_area = | |
185 gfx::Screen::GetMonitorAreaNearestWindow( | |
186 GetWidget()->GetTopLevelWidget()->GetNativeView()); | |
187 gfx::Size view_size = GetPreferredSize(); | |
188 anchor_point_ = gfx::Point( | |
189 monitor_area.x() + kBubbleXRatio * monitor_area.width(), | |
190 monitor_area.bottom() - view_size.height() / 2 - kBubbleBottomGap); | |
191 } | |
192 | |
171 void SettingLevelBubble::OnHideTimeout() { | 193 void SettingLevelBubble::OnHideTimeout() { |
172 HideBubble(); | 194 GetWidget()->Close(); |
173 } | 195 } |
174 | 196 |
175 void SettingLevelBubble::OnAnimationTimeout() { | 197 void SettingLevelBubble::OnAnimationTimeout() { |
176 const TimeTicks now = TimeTicks::Now(); | 198 const TimeTicks now = TimeTicks::Now(); |
177 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); | 199 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); |
178 | 200 |
179 if (remaining_ms <= 0) { | 201 if (remaining_ms <= 0) { |
180 current_percent_ = target_percent_; | 202 current_percent_ = target_percent_; |
181 StopAnimation(); | 203 StopAnimation(); |
182 } else { | 204 } else { |
183 // Figure out what fraction of the total time until we want to reach the | 205 // Figure out what fraction of the total time until we want to reach the |
184 // target has elapsed since the last update. | 206 // target has elapsed since the last update. |
185 const double remaining_percent = target_percent_ - current_percent_; | 207 const double remaining_percent = target_percent_ - current_percent_; |
186 const int64 elapsed_ms = | 208 const int64 elapsed_ms = |
187 (now - last_animation_update_time_).InMilliseconds(); | 209 (now - last_animation_update_time_).InMilliseconds(); |
188 current_percent_ += | 210 current_percent_ += |
189 remaining_percent * | 211 remaining_percent * |
190 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); | 212 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); |
191 } | 213 } |
192 last_animation_update_time_ = now; | 214 last_animation_update_time_ = now; |
193 | 215 |
194 if (view_) | 216 if (view_) |
195 view_->SetLevel(current_percent_); | 217 view_->SetLevel(current_percent_); |
196 } | 218 } |
197 | 219 |
198 void SettingLevelBubble::BubbleClosing(Bubble* bubble, bool) { | |
199 DCHECK(bubble == bubble_); | |
200 hide_timer_.Stop(); | |
201 StopAnimation(); | |
202 bubble_ = NULL; | |
203 view_ = NULL; | |
204 current_percent_ = target_percent_; | |
205 target_time_ = TimeTicks(); | |
206 last_animation_update_time_ = TimeTicks(); | |
207 last_target_update_time_ = TimeTicks(); | |
208 } | |
209 | |
210 bool SettingLevelBubble::CloseOnEscape() { | |
211 return true; | |
212 } | |
213 | |
214 bool SettingLevelBubble::FadeInOnShow() { | |
215 return false; | |
216 } | |
217 | |
218 void SettingLevelBubble::UpdateTargetPercent(double percent) { | 220 void SettingLevelBubble::UpdateTargetPercent(double percent) { |
219 target_percent_ = LimitPercent(percent); | 221 target_percent_ = LimitPercent(percent); |
220 const TimeTicks now = TimeTicks::Now(); | 222 const TimeTicks now = TimeTicks::Now(); |
221 | 223 |
222 if (current_percent_ < 0.0) { | 224 if (current_percent_ < 0.0) { |
223 // If we're setting the level for the first time, no need to animate. | 225 // If we're setting the level for the first time, no need to animate. |
224 current_percent_ = target_percent_; | 226 current_percent_ = target_percent_; |
225 if (view_) | 227 if (view_) |
226 view_->SetLevel(current_percent_); | 228 view_->SetLevel(current_percent_); |
227 } else { | 229 } else { |
(...skipping 19 matching lines...) Expand all Loading... | |
247 | 249 |
248 last_target_update_time_ = now; | 250 last_target_update_time_ = now; |
249 } | 251 } |
250 | 252 |
251 void SettingLevelBubble::StopAnimation() { | 253 void SettingLevelBubble::StopAnimation() { |
252 animation_timer_.Stop(); | 254 animation_timer_.Stop(); |
253 is_animating_ = false; | 255 is_animating_ = false; |
254 } | 256 } |
255 | 257 |
256 } // namespace chromeos | 258 } // namespace chromeos |
OLD | NEW |