OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ash/system/audio/volume_view.h" | |
6 | |
7 #include "ash/ash_constants.h" | |
8 #include "ash/ash_switches.h" | |
9 #include "ash/shell.h" | |
10 #include "ash/system/audio/tray_audio_delegate.h" | |
11 #include "ash/system/tray/system_tray_item.h" | |
12 #include "ash/system/tray/tray_constants.h" | |
13 #include "grit/ash_resources.h" | |
14 #include "grit/ash_strings.h" | |
15 #include "ui/base/resource/resource_bundle.h" | |
16 #include "ui/gfx/canvas.h" | |
17 #include "ui/gfx/image/image_skia_operations.h" | |
18 #include "ui/views/controls/button/image_button.h" | |
19 #include "ui/views/controls/image_view.h" | |
20 #include "ui/views/layout/box_layout.h" | |
21 | |
22 namespace { | |
23 const int kVolumeImageWidth = 25; | |
24 const int kVolumeImageHeight = 25; | |
25 const int kBarSeparatorWidth = 25; | |
26 const int kBarSeparatorHeight = 30; | |
27 const int kSliderRightPaddingToVolumeViewEdge = 17; | |
28 const int kExtraPaddingBetweenBarAndMore = 10; | |
29 | |
30 // IDR_AURA_UBER_TRAY_VOLUME_LEVELS contains 5 images, | |
31 // The one for mute is at the 0 index and the other | |
32 // four are used for ascending volume levels. | |
33 const int kVolumeLevels = 4; | |
34 | |
35 } // namespace | |
36 | |
37 namespace ash { | |
38 namespace internal { | |
39 namespace tray { | |
40 | |
41 class VolumeButton : public views::ToggleImageButton { | |
42 public: | |
43 explicit VolumeButton(views::ButtonListener* listener, | |
44 system::TrayAudioDelegate& audio_delegate) | |
jennyz
2014/02/14 21:36:43
Remove explicit, no need if there are more than on
zturner
2014/02/15 00:57:45
Done.
| |
45 : views::ToggleImageButton(listener), | |
46 audio_delegate_(audio_delegate), | |
47 image_index_(-1) { | |
48 SetImageAlignment(ALIGN_CENTER, ALIGN_MIDDLE); | |
49 image_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
50 IDR_AURA_UBER_TRAY_VOLUME_LEVELS); | |
51 SetPreferredSize(gfx::Size(kTrayPopupItemHeight, kTrayPopupItemHeight)); | |
52 Update(); | |
53 } | |
54 | |
55 virtual ~VolumeButton() {} | |
56 | |
57 void Update() { | |
58 float level = | |
59 static_cast<float>(audio_delegate_.GetOutputVolumeLevel()) / 100.0f; | |
60 int image_index = audio_delegate_.IsOutputAudioMuted() ? | |
61 0 : (level == 1.0 ? | |
62 kVolumeLevels : | |
63 std::max(1, int(std::ceil(level * (kVolumeLevels - 1))))); | |
64 if (image_index != image_index_) { | |
65 gfx::Rect region(0, image_index * kVolumeImageHeight, | |
66 kVolumeImageWidth, kVolumeImageHeight); | |
67 gfx::ImageSkia image_skia = gfx::ImageSkiaOperations::ExtractSubset( | |
68 *(image_.ToImageSkia()), region); | |
69 SetImage(views::CustomButton::STATE_NORMAL, &image_skia); | |
70 image_index_ = image_index; | |
71 } | |
72 SchedulePaint(); | |
73 } | |
74 | |
75 private: | |
76 // Overridden from views::View. | |
77 virtual gfx::Size GetPreferredSize() OVERRIDE { | |
78 gfx::Size size = views::ToggleImageButton::GetPreferredSize(); | |
79 size.set_height(kTrayPopupItemHeight); | |
80 return size; | |
81 } | |
82 | |
83 system::TrayAudioDelegate& audio_delegate_; | |
84 gfx::Image image_; | |
85 int image_index_; | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(VolumeButton); | |
88 }; | |
89 | |
90 class VolumeSlider : public views::Slider { | |
91 public: | |
92 explicit VolumeSlider(views::SliderListener* listener, | |
93 system::TrayAudioDelegate& audio_delegate) | |
jennyz
2014/02/14 21:36:43
Remove explicit. Why not just pass system::TrayAud
zturner
2014/02/15 00:57:45
Done.
| |
94 : views::Slider(listener, views::Slider::HORIZONTAL), | |
95 audio_delegate_(audio_delegate) { | |
96 set_focus_border_color(kFocusBorderColor); | |
97 SetValue( | |
98 static_cast<float>(audio_delegate_.GetOutputVolumeLevel()) / 100.0f); | |
99 SetAccessibleName( | |
100 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
101 IDS_ASH_STATUS_TRAY_VOLUME)); | |
102 Update(); | |
103 } | |
104 virtual ~VolumeSlider() {} | |
105 | |
106 void Update() { | |
107 UpdateState(!audio_delegate_.IsOutputAudioMuted()); | |
108 } | |
109 | |
110 private: | |
111 system::TrayAudioDelegate& audio_delegate_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(VolumeSlider); | |
114 }; | |
115 | |
116 // Vertical bar separator that can be placed on the VolumeView. | |
117 class BarSeparator : public views::View { | |
118 public: | |
119 BarSeparator() {} | |
120 virtual ~BarSeparator() {} | |
121 | |
122 // Overriden from views::View. | |
123 virtual gfx::Size GetPreferredSize() OVERRIDE { | |
124 return gfx::Size(kBarSeparatorWidth, kBarSeparatorHeight); | |
125 } | |
126 | |
127 private: | |
128 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
129 canvas->FillRect(gfx::Rect(width() / 2, 0, 1, height()), | |
130 kButtonStrokeColor); | |
131 } | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(BarSeparator); | |
134 }; | |
135 | |
136 VolumeView::VolumeView(SystemTrayItem* owner, | |
137 system::TrayAudioDelegate& audio_delegate, | |
138 bool is_default_view) | |
139 : owner_(owner), | |
140 audio_delegate_(audio_delegate), | |
141 icon_(NULL), | |
142 slider_(NULL), | |
143 bar_(NULL), | |
144 device_type_(NULL), | |
145 more_(NULL), | |
146 is_default_view_(is_default_view) { | |
147 SetFocusable(false); | |
148 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, | |
149 kTrayPopupPaddingHorizontal, 0, kTrayPopupPaddingBetweenItems)); | |
150 | |
151 icon_ = new VolumeButton(this, audio_delegate_); | |
152 AddChildView(icon_); | |
153 | |
154 slider_ = new VolumeSlider(this, audio_delegate_); | |
155 AddChildView(slider_); | |
156 | |
157 bar_ = new BarSeparator; | |
158 AddChildView(bar_); | |
159 | |
160 device_type_ = new views::ImageView; | |
161 AddChildView(device_type_); | |
162 | |
163 more_ = new views::ImageView; | |
164 more_->EnableCanvasFlippingForRTLUI(true); | |
165 more_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
166 IDR_AURA_UBER_TRAY_MORE).ToImageSkia()); | |
167 AddChildView(more_); | |
168 | |
169 Update(); | |
170 } | |
171 | |
172 VolumeView::~VolumeView() { | |
173 } | |
174 | |
175 void VolumeView::Update() { | |
176 icon_->Update(); | |
177 slider_->Update(); | |
178 UpdateDeviceTypeAndMore(); | |
179 Layout(); | |
180 } | |
181 | |
182 void VolumeView::SetVolumeLevel(float percent) { | |
183 // Slider's value is in finer granularity than audio volume level(0.01), | |
184 // there will be a small discrepancy between slider's value and volume level | |
185 // on audio side. To avoid the jittering in slider UI, do not set change | |
186 // slider value if the change is less than 1%. | |
187 if (std::abs(percent-slider_->value()) < 0.01) | |
188 return; | |
189 // The change in volume will be reflected via accessibility system events, | |
190 // so we prevent the UI event from being sent here. | |
191 slider_->set_enable_accessibility_events(false); | |
192 slider_->SetValue(percent); | |
193 // It is possible that the volume was (un)muted, but the actual volume level | |
194 // did not change. In that case, setting the value of the slider won't | |
195 // trigger an update. So explicitly trigger an update. | |
196 Update(); | |
197 slider_->set_enable_accessibility_events(true); | |
198 } | |
199 | |
200 void VolumeView::UpdateDeviceTypeAndMore() { | |
201 if (!ash::switches::ShowAudioDeviceMenu() || !is_default_view_) { | |
202 more_->SetVisible(false); | |
203 bar_->SetVisible(false); | |
204 device_type_->SetVisible(false); | |
205 return; | |
206 } | |
207 | |
208 bool show_more = audio_delegate_.HasAlternativeSources(); | |
209 more_->SetVisible(show_more); | |
210 bar_->SetVisible(show_more); | |
211 | |
212 // Show output device icon if necessary. | |
213 int device_icon = audio_delegate_.GetDeviceIconId(); | |
214 if (device_icon != system::TrayAudioDelegate::kNoAudioDeviceIcon) { | |
215 device_type_->SetVisible(true); | |
216 device_type_->SetImage( | |
217 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
218 device_icon).ToImageSkia()); | |
219 } else { | |
220 device_type_->SetVisible(false); | |
221 } | |
222 } | |
223 | |
224 void VolumeView::HandleVolumeUp(float level) { | |
225 audio_delegate_.SetOutputVolumeLevel(level); | |
226 if (audio_delegate_.IsOutputAudioMuted() && | |
227 level > audio_delegate_.GetOutputDefaultVolumeMuteLevel()) { | |
228 audio_delegate_.SetOutputAudioIsMuted(false); | |
229 } | |
230 } | |
231 | |
232 void VolumeView::HandleVolumeDown(float level) { | |
233 audio_delegate_.SetOutputVolumeLevel(level); | |
234 if (!audio_delegate_.IsOutputAudioMuted() && | |
235 level <= audio_delegate_.GetOutputDefaultVolumeMuteLevel()) { | |
236 audio_delegate_.SetOutputAudioIsMuted(true); | |
237 } | |
jennyz
2014/02/14 21:36:43
You removed the else if case in original HandleVol
zturner
2014/02/15 00:57:45
I added this back. I think I removed it because t
| |
238 } | |
239 | |
240 void VolumeView::Layout() { | |
241 views::View::Layout(); | |
242 | |
243 if (!more_->visible()) { | |
244 int w = width() - slider_->bounds().x() - | |
245 kSliderRightPaddingToVolumeViewEdge; | |
246 slider_->SetSize(gfx::Size(w, slider_->height())); | |
247 return; | |
248 } | |
249 | |
250 // Make sure the chevron always has the full size. | |
251 gfx::Size size = more_->GetPreferredSize(); | |
252 gfx::Rect bounds(size); | |
253 bounds.set_x(width() - size.width() - kTrayPopupPaddingBetweenItems); | |
254 bounds.set_y((height() - size.height()) / 2); | |
255 more_->SetBoundsRect(bounds); | |
256 | |
257 // Layout either bar_ or device_type_ at the left of the more_ button. | |
258 views::View* view_left_to_more; | |
259 if (device_type_->visible()) | |
260 view_left_to_more = device_type_; | |
261 else | |
262 view_left_to_more = bar_; | |
263 gfx::Size view_size = view_left_to_more->GetPreferredSize(); | |
264 gfx::Rect view_bounds(view_size); | |
265 view_bounds.set_x(more_->bounds().x() - view_size.width() - | |
266 kExtraPaddingBetweenBarAndMore); | |
267 view_bounds.set_y((height() - view_size.height()) / 2); | |
268 view_left_to_more->SetBoundsRect(view_bounds); | |
269 | |
270 // Layout vertical bar next to view_left_to_more if device_type_ is visible. | |
271 if (device_type_->visible()) { | |
272 gfx::Size bar_size = bar_->GetPreferredSize(); | |
273 gfx::Rect bar_bounds(bar_size); | |
274 bar_bounds.set_x(view_left_to_more->bounds().x() - bar_size.width()); | |
275 bar_bounds.set_y((height() - bar_size.height()) / 2); | |
276 bar_->SetBoundsRect(bar_bounds); | |
277 } | |
278 | |
279 // Layout slider, calculate slider width. | |
280 gfx::Rect slider_bounds = slider_->bounds(); | |
281 slider_bounds.set_width( | |
282 bar_->bounds().x() | |
283 - (device_type_->visible() ? 0 : kTrayPopupPaddingBetweenItems) | |
284 - slider_bounds.x()); | |
285 slider_->SetBoundsRect(slider_bounds); | |
286 } | |
287 | |
288 void VolumeView::ButtonPressed(views::Button* sender, const ui::Event& event) { | |
289 CHECK(sender == icon_); | |
290 bool mute_on = !audio_delegate_.IsOutputAudioMuted(); | |
291 audio_delegate_.SetOutputAudioIsMuted(mute_on); | |
292 if (!mute_on) | |
293 audio_delegate_.AdjustOutputVolumeToAudibleLevel(); | |
294 icon_->Update(); | |
295 } | |
296 | |
297 void VolumeView::SliderValueChanged(views::Slider* sender, | |
298 float value, | |
299 float old_value, | |
300 views::SliderChangeReason reason) { | |
301 if (reason == views::VALUE_CHANGED_BY_USER) { | |
302 float new_volume = value * 100.0f; | |
303 float current_volume = audio_delegate_.GetOutputVolumeLevel(); | |
304 // Do not call change audio volume if the difference is less than | |
305 // 1%, which is beyond cras audio api's granularity for output volume. | |
306 if (std::abs(new_volume - current_volume) < 1.0f) | |
307 return; | |
308 Shell::GetInstance()->metrics()->RecordUserMetricsAction( | |
309 is_default_view_ ? | |
310 ash::UMA_STATUS_AREA_CHANGED_VOLUME_MENU : | |
311 ash::UMA_STATUS_AREA_CHANGED_VOLUME_POPUP); | |
312 if (new_volume > current_volume) | |
313 HandleVolumeUp(new_volume); | |
314 else | |
315 HandleVolumeDown(new_volume); | |
316 } | |
317 icon_->Update(); | |
318 } | |
319 | |
320 bool VolumeView::PerformAction(const ui::Event& event) { | |
321 if (!more_->visible()) | |
322 return false; | |
323 owner_->TransitionDetailedView(); | |
324 return true; | |
325 } | |
326 | |
327 } // namespace tray | |
328 } // namespace internal | |
329 } // namespace ash | |
OLD | NEW |