OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ash/system/audio/volume_view.h" | 5 #include "ash/system/audio/volume_view.h" |
6 | 6 |
7 #include "ash/ash_constants.h" | 7 #include "ash/ash_constants.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "ash/system/audio/tray_audio.h" | 9 #include "ash/system/audio/tray_audio.h" |
10 #include "ash/system/audio/tray_audio_delegate.h" | 10 #include "ash/system/audio/tray_audio_delegate.h" |
11 #include "ash/system/tray/system_tray_item.h" | 11 #include "ash/system/tray/system_tray_item.h" |
12 #include "ash/system/tray/tray_constants.h" | 12 #include "ash/system/tray/tray_constants.h" |
13 #include "ash/system/tray/tray_popup_item_container.h" | |
13 #include "grit/ash_resources.h" | 14 #include "grit/ash_resources.h" |
14 #include "grit/ash_strings.h" | 15 #include "grit/ash_strings.h" |
15 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
16 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
17 #include "ui/gfx/image/image_skia_operations.h" | 18 #include "ui/gfx/image/image_skia_operations.h" |
19 #include "ui/views/background.h" | |
20 #include "ui/views/border.h" | |
18 #include "ui/views/controls/button/image_button.h" | 21 #include "ui/views/controls/button/image_button.h" |
19 #include "ui/views/controls/image_view.h" | 22 #include "ui/views/controls/image_view.h" |
23 #include "ui/views/controls/separator.h" | |
20 #include "ui/views/layout/box_layout.h" | 24 #include "ui/views/layout/box_layout.h" |
21 | 25 |
22 namespace { | 26 namespace { |
23 const int kVolumeImageWidth = 25; | 27 const int kVolumeImageWidth = 25; |
24 const int kVolumeImageHeight = 25; | 28 const int kVolumeImageHeight = 25; |
25 const int kBarSeparatorWidth = 25; | 29 const int kSeparatorSize = 3; |
26 const int kBarSeparatorHeight = 30; | 30 const int kSeparatorVerticalInset = 8; |
27 const int kSliderRightPaddingToVolumeViewEdge = 17; | 31 const int kSliderRightPaddingToVolumeViewEdge = 17; |
28 const int kExtraPaddingBetweenBarAndMore = 10; | 32 const int kExtraPaddingBetweenBarAndMore = 10; |
33 const int kExtraPaddingBetweenIconAndSlider = 8; | |
34 const int kBoxLayoutPadding = 2; | |
29 | 35 |
30 // IDR_AURA_UBER_TRAY_VOLUME_LEVELS contains 5 images, | 36 // IDR_AURA_UBER_TRAY_VOLUME_LEVELS contains 5 images, |
31 // The one for mute is at the 0 index and the other | 37 // The one for mute is at the 0 index and the other |
32 // four are used for ascending volume levels. | 38 // four are used for ascending volume levels. |
33 const int kVolumeLevels = 4; | 39 const int kVolumeLevels = 4; |
34 | 40 |
35 } // namespace | 41 } // namespace |
36 | 42 |
37 namespace ash { | 43 namespace ash { |
38 namespace tray { | 44 namespace tray { |
(...skipping 21 matching lines...) Expand all Loading... | |
60 kVolumeLevels : | 66 kVolumeLevels : |
61 std::max(1, int(std::ceil(level * (kVolumeLevels - 1))))); | 67 std::max(1, int(std::ceil(level * (kVolumeLevels - 1))))); |
62 if (image_index != image_index_) { | 68 if (image_index != image_index_) { |
63 gfx::Rect region(0, image_index * kVolumeImageHeight, | 69 gfx::Rect region(0, image_index * kVolumeImageHeight, |
64 kVolumeImageWidth, kVolumeImageHeight); | 70 kVolumeImageWidth, kVolumeImageHeight); |
65 gfx::ImageSkia image_skia = gfx::ImageSkiaOperations::ExtractSubset( | 71 gfx::ImageSkia image_skia = gfx::ImageSkiaOperations::ExtractSubset( |
66 *(image_.ToImageSkia()), region); | 72 *(image_.ToImageSkia()), region); |
67 SetImage(views::CustomButton::STATE_NORMAL, &image_skia); | 73 SetImage(views::CustomButton::STATE_NORMAL, &image_skia); |
68 image_index_ = image_index; | 74 image_index_ = image_index; |
69 } | 75 } |
70 SchedulePaint(); | |
71 } | 76 } |
72 | 77 |
73 private: | 78 private: |
74 // Overridden from views::View. | 79 // views::View: |
75 gfx::Size GetPreferredSize() const override { | 80 gfx::Size GetPreferredSize() const override { |
76 gfx::Size size = views::ToggleImageButton::GetPreferredSize(); | 81 gfx::Size size = views::ToggleImageButton::GetPreferredSize(); |
77 size.set_height(kTrayPopupItemHeight); | 82 size.set_height(kTrayPopupItemHeight); |
78 return size; | 83 return size; |
79 } | 84 } |
80 | 85 |
86 // views::CustomButton: | |
87 void StateChanged() override { | |
88 if (state() == STATE_HOVERED || state() == STATE_PRESSED) { | |
89 set_background( | |
90 views::Background::CreateSolidBackground(kHoverBackgroundColor)); | |
91 } else { | |
92 set_background(nullptr); | |
93 } | |
94 } | |
95 | |
81 system::TrayAudioDelegate* audio_delegate_; | 96 system::TrayAudioDelegate* audio_delegate_; |
82 gfx::Image image_; | 97 gfx::Image image_; |
83 int image_index_; | 98 int image_index_; |
84 | 99 |
85 DISALLOW_COPY_AND_ASSIGN(VolumeButton); | 100 DISALLOW_COPY_AND_ASSIGN(VolumeButton); |
86 }; | 101 }; |
87 | 102 |
88 class VolumeSlider : public views::Slider { | |
89 public: | |
90 VolumeSlider(views::SliderListener* listener, | |
91 system::TrayAudioDelegate* audio_delegate) | |
92 : views::Slider(listener, views::Slider::HORIZONTAL), | |
93 audio_delegate_(audio_delegate) { | |
94 set_focus_border_color(kFocusBorderColor); | |
95 SetValue( | |
96 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f); | |
97 SetAccessibleName( | |
98 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
99 IDS_ASH_STATUS_TRAY_VOLUME)); | |
100 Update(); | |
101 } | |
102 ~VolumeSlider() override {} | |
103 | |
104 void Update() { | |
105 UpdateState(!audio_delegate_->IsOutputAudioMuted()); | |
106 } | |
107 | |
108 private: | |
109 system::TrayAudioDelegate* audio_delegate_; | |
110 | |
111 DISALLOW_COPY_AND_ASSIGN(VolumeSlider); | |
112 }; | |
113 | |
114 // Vertical bar separator that can be placed on the VolumeView. | |
115 class BarSeparator : public views::View { | |
116 public: | |
117 BarSeparator() {} | |
118 ~BarSeparator() override {} | |
119 | |
120 // Overriden from views::View. | |
121 gfx::Size GetPreferredSize() const override { | |
122 return gfx::Size(kBarSeparatorWidth, kBarSeparatorHeight); | |
123 } | |
124 | |
125 private: | |
126 void OnPaint(gfx::Canvas* canvas) override { | |
127 canvas->FillRect(gfx::Rect(width() / 2, 0, 1, height()), | |
128 kButtonStrokeColor); | |
129 } | |
130 | |
131 DISALLOW_COPY_AND_ASSIGN(BarSeparator); | |
132 }; | |
133 | |
134 VolumeView::VolumeView(SystemTrayItem* owner, | 103 VolumeView::VolumeView(SystemTrayItem* owner, |
135 system::TrayAudioDelegate* audio_delegate, | 104 system::TrayAudioDelegate* audio_delegate, |
136 bool is_default_view) | 105 bool is_default_view) |
137 : owner_(owner), | 106 : owner_(owner), |
138 audio_delegate_(audio_delegate), | 107 audio_delegate_(audio_delegate), |
139 icon_(NULL), | 108 icon_(NULL), |
140 slider_(NULL), | 109 slider_(NULL), |
141 bar_(NULL), | |
142 device_type_(NULL), | 110 device_type_(NULL), |
143 more_(NULL), | 111 more_(NULL), |
144 is_default_view_(is_default_view) { | 112 is_default_view_(is_default_view) { |
145 SetFocusable(false); | 113 SetFocusable(false); |
146 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, | 114 views::BoxLayout* box_layout = new views::BoxLayout( |
147 kTrayPopupPaddingHorizontal, 0, kTrayPopupPaddingBetweenItems)); | 115 views::BoxLayout::kHorizontal, 0, 0, kBoxLayoutPadding); |
116 box_layout->SetDefaultFlex(0); | |
117 SetLayoutManager(box_layout); | |
148 | 118 |
149 icon_ = new VolumeButton(this, audio_delegate_); | 119 icon_ = new VolumeButton(this, audio_delegate_); |
120 icon_->SetBorder(views::Border::CreateEmptyBorder( | |
121 0, kTrayPopupPaddingHorizontal, 0, kExtraPaddingBetweenIconAndSlider)); | |
150 AddChildView(icon_); | 122 AddChildView(icon_); |
151 | 123 |
152 slider_ = new VolumeSlider(this, audio_delegate_); | 124 slider_ = new views::Slider(this, views::Slider::HORIZONTAL); |
125 slider_->set_focus_border_color(kFocusBorderColor); | |
126 slider_->SetValue( | |
127 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f); | |
128 slider_->SetAccessibleName( | |
129 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
130 IDS_ASH_STATUS_TRAY_VOLUME)); | |
131 slider_->SetBorder( | |
132 views::Border::CreateEmptyBorder(0, 0, 0, kTrayPopupPaddingBetweenItems)); | |
153 AddChildView(slider_); | 133 AddChildView(slider_); |
134 box_layout->SetFlexForView(slider_, 1); | |
154 | 135 |
155 bar_ = new BarSeparator; | 136 separator_ = new views::Separator(views::Separator::VERTICAL); |
156 AddChildView(bar_); | 137 separator_->SetColor(kButtonStrokeColor); |
138 separator_->SetPreferredSize(kSeparatorSize); | |
139 separator_->SetBorder(views::Border::CreateEmptyBorder( | |
140 kSeparatorVerticalInset, 0, kSeparatorVerticalInset, kBoxLayoutPadding)); | |
141 | |
142 more_region_ = new TrayPopupItemContainer(separator_, true, false); | |
143 more_region_->SetBorder( | |
144 views::Border::CreateEmptyBorder(0, 0, 0, kTrayPopupPaddingBetweenItems)); | |
145 AddChildView(more_region_); | |
157 | 146 |
158 device_type_ = new views::ImageView; | 147 device_type_ = new views::ImageView; |
159 AddChildView(device_type_); | 148 more_region_->AddChildView(device_type_); |
160 | 149 |
161 more_ = new views::ImageView; | 150 more_ = new views::ImageView; |
162 more_->EnableCanvasFlippingForRTLUI(true); | 151 more_->EnableCanvasFlippingForRTLUI(true); |
163 more_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 152 more_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
164 IDR_AURA_UBER_TRAY_MORE).ToImageSkia()); | 153 IDR_AURA_UBER_TRAY_MORE).ToImageSkia()); |
165 AddChildView(more_); | 154 more_region_->AddChildView(more_); |
sadrul
2015/01/26 18:02:55
Have you considered using TrayItemMore instead of
jonross
2015/01/26 19:19:01
I had considered it, however TrayItemMore is a bit
| |
155 | |
156 set_background(views::Background::CreateSolidBackground(kBackgroundColor)); | |
166 | 157 |
167 Update(); | 158 Update(); |
168 } | 159 } |
169 | 160 |
170 VolumeView::~VolumeView() { | 161 VolumeView::~VolumeView() { |
171 } | 162 } |
172 | 163 |
173 void VolumeView::Update() { | 164 void VolumeView::Update() { |
174 icon_->Update(); | 165 icon_->Update(); |
175 slider_->Update(); | 166 slider_->UpdateState(!audio_delegate_->IsOutputAudioMuted()); |
176 UpdateDeviceTypeAndMore(); | 167 UpdateDeviceTypeAndMore(); |
177 Layout(); | 168 Layout(); |
178 } | 169 } |
179 | 170 |
180 void VolumeView::SetVolumeLevel(float percent) { | 171 void VolumeView::SetVolumeLevel(float percent) { |
181 // Slider's value is in finer granularity than audio volume level(0.01), | 172 // Slider's value is in finer granularity than audio volume level(0.01), |
182 // there will be a small discrepancy between slider's value and volume level | 173 // there will be a small discrepancy between slider's value and volume level |
183 // on audio side. To avoid the jittering in slider UI, do not set change | 174 // on audio side. To avoid the jittering in slider UI, do not set change |
184 // slider value if the change is less than 1%. | 175 // slider value if the change is less than 1%. |
185 if (std::abs(percent-slider_->value()) < 0.01) | 176 if (std::abs(percent-slider_->value()) < 0.01) |
186 return; | 177 return; |
187 // The change in volume will be reflected via accessibility system events, | 178 // The change in volume will be reflected via accessibility system events, |
188 // so we prevent the UI event from being sent here. | 179 // so we prevent the UI event from being sent here. |
189 slider_->set_enable_accessibility_events(false); | 180 slider_->set_enable_accessibility_events(false); |
190 slider_->SetValue(percent); | 181 slider_->SetValue(percent); |
191 // It is possible that the volume was (un)muted, but the actual volume level | 182 // It is possible that the volume was (un)muted, but the actual volume level |
192 // did not change. In that case, setting the value of the slider won't | 183 // did not change. In that case, setting the value of the slider won't |
193 // trigger an update. So explicitly trigger an update. | 184 // trigger an update. So explicitly trigger an update. |
194 Update(); | 185 Update(); |
195 slider_->set_enable_accessibility_events(true); | 186 slider_->set_enable_accessibility_events(true); |
196 } | 187 } |
197 | 188 |
198 void VolumeView::UpdateDeviceTypeAndMore() { | 189 void VolumeView::UpdateDeviceTypeAndMore() { |
199 if (!TrayAudio::ShowAudioDeviceMenu() || !is_default_view_) { | 190 bool show_more = is_default_view_ && TrayAudio::ShowAudioDeviceMenu() && |
200 more_->SetVisible(false); | 191 audio_delegate_->HasAlternativeSources(); |
201 bar_->SetVisible(false); | 192 slider_->SetBorder(views::Border::CreateEmptyBorder( |
202 device_type_->SetVisible(false); | 193 0, 0, 0, show_more ? kTrayPopupPaddingBetweenItems |
194 : kSliderRightPaddingToVolumeViewEdge)); | |
195 | |
196 if (!show_more) { | |
197 more_region_->SetVisible(false); | |
203 return; | 198 return; |
204 } | 199 } |
205 | 200 |
206 bool show_more = audio_delegate_->HasAlternativeSources(); | |
207 more_->SetVisible(show_more); | |
208 bar_->SetVisible(show_more); | |
209 | |
210 // Show output device icon if necessary. | 201 // Show output device icon if necessary. |
211 int device_icon = audio_delegate_->GetActiveOutputDeviceIconId(); | 202 int device_icon = audio_delegate_->GetActiveOutputDeviceIconId(); |
212 if (device_icon != system::TrayAudioDelegate::kNoAudioDeviceIcon) { | 203 if (device_icon != system::TrayAudioDelegate::kNoAudioDeviceIcon) { |
213 device_type_->SetVisible(true); | 204 device_type_->SetVisible(true); |
214 device_type_->SetImage( | 205 device_type_->SetImage( |
215 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 206 ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
216 device_icon).ToImageSkia()); | 207 device_icon).ToImageSkia()); |
208 more_region_->SetLayoutManager(new views::BoxLayout( | |
209 views::BoxLayout::kHorizontal, 0, 0, kTrayPopupPaddingBetweenItems)); | |
217 } else { | 210 } else { |
218 device_type_->SetVisible(false); | 211 device_type_->SetVisible(false); |
212 more_region_->SetLayoutManager(new views::BoxLayout( | |
213 views::BoxLayout::kHorizontal, 0, 0, | |
214 kTrayPopupPaddingBetweenItems + kExtraPaddingBetweenBarAndMore)); | |
219 } | 215 } |
216 more_region_->SetVisible(true); | |
220 } | 217 } |
221 | 218 |
222 void VolumeView::HandleVolumeUp(float level) { | 219 void VolumeView::HandleVolumeUp(float level) { |
223 audio_delegate_->SetOutputVolumeLevel(level); | 220 audio_delegate_->SetOutputVolumeLevel(level); |
224 if (audio_delegate_->IsOutputAudioMuted() && | 221 if (audio_delegate_->IsOutputAudioMuted() && |
225 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { | 222 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { |
226 audio_delegate_->SetOutputAudioIsMuted(false); | 223 audio_delegate_->SetOutputAudioIsMuted(false); |
227 } | 224 } |
228 } | 225 } |
229 | 226 |
230 void VolumeView::HandleVolumeDown(float level) { | 227 void VolumeView::HandleVolumeDown(float level) { |
231 audio_delegate_->SetOutputVolumeLevel(level); | 228 audio_delegate_->SetOutputVolumeLevel(level); |
232 if (!audio_delegate_->IsOutputAudioMuted() && | 229 if (!audio_delegate_->IsOutputAudioMuted() && |
233 level <= audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { | 230 level <= audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { |
234 audio_delegate_->SetOutputAudioIsMuted(true); | 231 audio_delegate_->SetOutputAudioIsMuted(true); |
235 } else if (audio_delegate_->IsOutputAudioMuted() && | 232 } else if (audio_delegate_->IsOutputAudioMuted() && |
236 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { | 233 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { |
237 audio_delegate_->SetOutputAudioIsMuted(false); | 234 audio_delegate_->SetOutputAudioIsMuted(false); |
238 } | 235 } |
239 } | 236 } |
240 | 237 |
241 void VolumeView::Layout() { | |
242 views::View::Layout(); | |
243 | |
244 if (!more_->visible()) { | |
245 int w = width() - slider_->bounds().x() - | |
246 kSliderRightPaddingToVolumeViewEdge; | |
247 slider_->SetSize(gfx::Size(w, slider_->height())); | |
248 return; | |
249 } | |
250 | |
251 // Make sure the chevron always has the full size. | |
252 gfx::Size size = more_->GetPreferredSize(); | |
253 gfx::Rect bounds(size); | |
254 bounds.set_x(width() - size.width() - kTrayPopupPaddingBetweenItems); | |
255 bounds.set_y((height() - size.height()) / 2); | |
256 more_->SetBoundsRect(bounds); | |
257 | |
258 // Layout either bar_ or device_type_ at the left of the more_ button. | |
259 views::View* view_left_to_more; | |
260 if (device_type_->visible()) | |
261 view_left_to_more = device_type_; | |
262 else | |
263 view_left_to_more = bar_; | |
264 gfx::Size view_size = view_left_to_more->GetPreferredSize(); | |
265 gfx::Rect view_bounds(view_size); | |
266 view_bounds.set_x(more_->bounds().x() - view_size.width() - | |
267 kExtraPaddingBetweenBarAndMore); | |
268 view_bounds.set_y((height() - view_size.height()) / 2); | |
269 view_left_to_more->SetBoundsRect(view_bounds); | |
270 | |
271 // Layout vertical bar next to view_left_to_more if device_type_ is visible. | |
272 if (device_type_->visible()) { | |
273 gfx::Size bar_size = bar_->GetPreferredSize(); | |
274 gfx::Rect bar_bounds(bar_size); | |
275 bar_bounds.set_x(view_left_to_more->bounds().x() - bar_size.width()); | |
276 bar_bounds.set_y((height() - bar_size.height()) / 2); | |
277 bar_->SetBoundsRect(bar_bounds); | |
278 } | |
279 | |
280 // Layout slider, calculate slider width. | |
281 gfx::Rect slider_bounds = slider_->bounds(); | |
282 slider_bounds.set_width( | |
283 bar_->bounds().x() | |
284 - (device_type_->visible() ? 0 : kTrayPopupPaddingBetweenItems) | |
285 - slider_bounds.x()); | |
286 slider_->SetBoundsRect(slider_bounds); | |
287 } | |
288 | |
289 void VolumeView::ButtonPressed(views::Button* sender, const ui::Event& event) { | 238 void VolumeView::ButtonPressed(views::Button* sender, const ui::Event& event) { |
290 CHECK(sender == icon_); | 239 CHECK(sender == icon_); |
291 bool mute_on = !audio_delegate_->IsOutputAudioMuted(); | 240 bool mute_on = !audio_delegate_->IsOutputAudioMuted(); |
292 audio_delegate_->SetOutputAudioIsMuted(mute_on); | 241 audio_delegate_->SetOutputAudioIsMuted(mute_on); |
293 if (!mute_on) | 242 if (!mute_on) |
294 audio_delegate_->AdjustOutputVolumeToAudibleLevel(); | 243 audio_delegate_->AdjustOutputVolumeToAudibleLevel(); |
295 icon_->Update(); | 244 icon_->Update(); |
296 } | 245 } |
297 | 246 |
298 void VolumeView::SliderValueChanged(views::Slider* sender, | 247 void VolumeView::SliderValueChanged(views::Slider* sender, |
(...skipping 13 matching lines...) Expand all Loading... | |
312 ash::UMA_STATUS_AREA_CHANGED_VOLUME_POPUP); | 261 ash::UMA_STATUS_AREA_CHANGED_VOLUME_POPUP); |
313 if (new_volume > current_volume) | 262 if (new_volume > current_volume) |
314 HandleVolumeUp(new_volume); | 263 HandleVolumeUp(new_volume); |
315 else | 264 else |
316 HandleVolumeDown(new_volume); | 265 HandleVolumeDown(new_volume); |
317 } | 266 } |
318 icon_->Update(); | 267 icon_->Update(); |
319 } | 268 } |
320 | 269 |
321 bool VolumeView::PerformAction(const ui::Event& event) { | 270 bool VolumeView::PerformAction(const ui::Event& event) { |
322 if (!more_->visible()) | 271 if (!more_region_->visible()) |
323 return false; | 272 return false; |
324 owner_->TransitionDetailedView(); | 273 owner_->TransitionDetailedView(); |
325 return true; | 274 return true; |
326 } | 275 } |
327 | 276 |
277 void VolumeView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
278 // Separator's prefered size is based on set bounds. When an empty bounds is | |
279 // set on first layout this causes BoxLayout to ignore the separator. Reset | |
280 // its height on each bounds change so that it is laid out properly. | |
281 separator_->SetSize(gfx::Size(kSeparatorSize, bounds().height())); | |
282 } | |
283 | |
328 } // namespace tray | 284 } // namespace tray |
329 } // namespace ash | 285 } // namespace ash |
OLD | NEW |