Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: chrome/browser/chromeos/setting_level_bubble.cc

Issue 8319008: aura: brightness and volume bubble. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: update Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
88 // static
89 views::Widget* SettingLevelBubble::CreateSettingLevelBubble(
90 SkBitmap* increase_icon,
91 SkBitmap* decrease_icon,
92 SkBitmap* zero_icon,
93 double percent,
94 bool enabled) {
95 views::Widget* parent = GetToplevelWidget();
96 SettingLevelBubble* delegate =
97 new SettingLevelBubble(increase_icon, decrease_icon, zero_icon);
98 delegate->UpdateSetting(percent, enabled);
99 // Construct and initialize settings.
Daniel Erat 2011/10/21 00:25:21 this comment seems unnecessary
alicet1 2011/10/21 18:11:36 Done.
100 views::Widget* widget = views::BubbleDelegateView::CreateBubble(
101 delegate, parent);
102 delegate->set_close_on_esc(false);
103 return widget;
104 }
105
106 views::Widget* SettingLevelBubble::ShowBubble(views::Widget* widget,
107 SkBitmap* increase_icon,
108 SkBitmap* decrease_icon,
109 SkBitmap* disabled_icon,
110 double percent,
111 bool enabled) {
112 if (!widget) {
113 widget = CreateSettingLevelBubble(increase_icon,
114 decrease_icon,
115 disabled_icon,
116 percent,
117 enabled);
118 } else {
119 static_cast<SettingLevelBubble*>(widget->widget_delegate())
120 ->UpdateSetting(percent, enabled);
121 }
122 // Show the window and start the timer. When The timer
Daniel Erat 2011/10/21 00:25:21 nit: s/The/the/
alicet1 2011/10/21 18:11:36 Done.
123 // runs out, we start the fade sequence.
124 widget->Show();
125 static_cast<SettingLevelBubble*>(widget->widget_delegate())
126 ->StartHideTimer();
127 return widget;
128 }
129
87 SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon, 130 SettingLevelBubble::SettingLevelBubble(SkBitmap* increase_icon,
88 SkBitmap* decrease_icon, 131 SkBitmap* decrease_icon,
89 SkBitmap* disabled_icon) 132 SkBitmap* disabled_icon)
90 : current_percent_(-1.0), 133 : BubbleDelegateView(gfx::Point(),
134 views::BubbleBorder::FLOAT,
135 SK_ColorWHITE),
136 current_percent_(-1.0),
91 target_percent_(-1.0), 137 target_percent_(-1.0),
92 increase_icon_(increase_icon), 138 increase_icon_(increase_icon),
93 decrease_icon_(decrease_icon), 139 decrease_icon_(decrease_icon),
94 disabled_icon_(disabled_icon), 140 disabled_icon_(disabled_icon),
95 bubble_(NULL), 141 current_icon_(NULL),
96 view_(NULL), 142 view_(NULL),
97 is_animating_(false) { 143 is_animating_(false) {
98 } 144 }
99 145
100 SettingLevelBubble::~SettingLevelBubble() {} 146 SettingLevelBubble::~SettingLevelBubble() {
147 }
101 148
102 void SettingLevelBubble::ShowBubble(double percent, bool enabled) { 149 void SettingLevelBubble::StartHideTimer() {
103 const double old_target_percent = target_percent_; 150 hide_timer_.Stop();
104 UpdateTargetPercent(percent);
105
106 SkBitmap* icon = increase_icon_;
107 if (!enabled || target_percent_ == 0)
108 icon = disabled_icon_;
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 {
147 DCHECK(view_);
148 hide_timer_.Stop();
149 view_->SetIcon(icon);
150 view_->SetEnabled(enabled);
151 }
152
153 hide_timer_.Start(FROM_HERE, 151 hide_timer_.Start(FROM_HERE,
154 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs), 152 base::TimeDelta::FromMilliseconds(kBubbleShowTimeoutMs),
155 this, &SettingLevelBubble::OnHideTimeout); 153 this, &SettingLevelBubble::OnHideTimeout);
156 } 154 }
157 155
158 void SettingLevelBubble::HideBubble() { 156 void SettingLevelBubble::Init() {
159 if (bubble_) 157 SetLayoutManager(new views::FillLayout());
160 bubble_->Close(); 158 view_ = new SettingLevelBubbleView();
159
Daniel Erat 2011/10/21 00:25:21 nit: remove extra blank line
alicet1 2011/10/21 18:11:36 Done.
160 view_->Init(current_icon_, current_percent_, enabled_);
161 AddChildView(view_);
162 }
163
164 void SettingLevelBubble::UpdateSetting(double percent, bool enabled) {
165 enabled_ = enabled;
166 const double old_target_percent = target_percent_;
167 UpdateTargetPercent(percent);
168 current_icon_ = increase_icon_;
169 if (!enabled || target_percent_ == 0)
170 current_icon_ = disabled_icon_;
171 else if (old_target_percent >= 0 && target_percent_ < old_target_percent)
172 current_icon_ = decrease_icon_;
173 }
174
175 void SettingLevelBubble::WindowClosing() {
176 hide_timer_.Stop();
177 StopAnimation();
178 view_ = NULL;
179 current_percent_ = target_percent_;
180 target_time_ = TimeTicks();
181 last_animation_update_time_ = TimeTicks();
182 last_target_update_time_ = TimeTicks();
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 gfx::Point SettingLevelBubble::GetAnchorPoint() {
193 // Calculate the position in screen coordinates that the bubble should
194 // "point" at (since we use BubbleBorder::FLOAT, this position actually
195 // specifies the center of the bubble).
196 const gfx::Rect monitor_area =
197 gfx::Screen::GetMonitorAreaNearestWindow(
198 GetWidget()->GetTopLevelWidget()->GetNativeView());
199 gfx::Size view_size = GetPreferredSize();
200 return (gfx::Point(
201 monitor_area.x() + kBubbleXRatio * monitor_area.width(),
202 monitor_area.bottom() - view_size.height() / 2 - kBubbleBottomGap));
203 }
204
170 void SettingLevelBubble::OnHideTimeout() { 205 void SettingLevelBubble::OnHideTimeout() {
171 HideBubble(); 206 // Start fading away.
207 StartFade(false/* fade_in */);
Daniel Erat 2011/10/21 00:25:21 nit: add one space between 'false' and comment
alicet1 2011/10/21 18:11:36 Done.
172 } 208 }
173 209
174 void SettingLevelBubble::OnAnimationTimeout() { 210 void SettingLevelBubble::OnAnimationTimeout() {
175 const TimeTicks now = TimeTicks::Now(); 211 const TimeTicks now = TimeTicks::Now();
176 const int64 remaining_ms = (target_time_ - now).InMilliseconds(); 212 const int64 remaining_ms = (target_time_ - now).InMilliseconds();
177 213
178 if (remaining_ms <= 0) { 214 if (remaining_ms <= 0) {
179 current_percent_ = target_percent_; 215 current_percent_ = target_percent_;
180 StopAnimation(); 216 StopAnimation();
181 } else { 217 } else {
182 // Figure out what fraction of the total time until we want to reach the 218 // Figure out what fraction of the total time until we want to reach the
183 // target has elapsed since the last update. 219 // target has elapsed since the last update.
184 const double remaining_percent = target_percent_ - current_percent_; 220 const double remaining_percent = target_percent_ - current_percent_;
185 const int64 elapsed_ms = 221 const int64 elapsed_ms =
186 (now - last_animation_update_time_).InMilliseconds(); 222 (now - last_animation_update_time_).InMilliseconds();
187 current_percent_ += 223 current_percent_ +=
188 remaining_percent * 224 remaining_percent *
189 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms)); 225 (static_cast<double>(elapsed_ms) / (elapsed_ms + remaining_ms));
190 } 226 }
191 last_animation_update_time_ = now; 227 last_animation_update_time_ = now;
192 228
193 if (view_) 229 if (view_)
194 view_->SetLevel(current_percent_); 230 view_->SetLevel(current_percent_);
195 } 231 }
196 232
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) { 233 void SettingLevelBubble::UpdateTargetPercent(double percent) {
218 target_percent_ = LimitPercent(percent); 234 target_percent_ = LimitPercent(percent);
219 const TimeTicks now = TimeTicks::Now(); 235 const TimeTicks now = TimeTicks::Now();
220 236
221 if (current_percent_ < 0.0) { 237 if (current_percent_ < 0.0) {
222 // If we're setting the level for the first time, no need to animate. 238 // If we're setting the level for the first time, no need to animate.
223 current_percent_ = target_percent_; 239 current_percent_ = target_percent_;
224 if (view_) 240 if (view_)
225 view_->SetLevel(current_percent_); 241 view_->SetLevel(current_percent_);
226 } else { 242 } else {
(...skipping 19 matching lines...) Expand all
246 262
247 last_target_update_time_ = now; 263 last_target_update_time_ = now;
248 } 264 }
249 265
250 void SettingLevelBubble::StopAnimation() { 266 void SettingLevelBubble::StopAnimation() {
251 animation_timer_.Stop(); 267 animation_timer_.Stop();
252 is_animating_ = false; 268 is_animating_ = false;
253 } 269 }
254 270
255 } // namespace chromeos 271 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698