OLD | NEW |
(Empty) | |
| 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 |
| 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 VolumeButton(views::ButtonListener* listener, |
| 44 system::TrayAudioDelegate* audio_delegate) |
| 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 VolumeSlider(views::SliderListener* listener, |
| 93 system::TrayAudioDelegate* audio_delegate) |
| 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_->GetActiveOutputDeviceIconId(); |
| 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 } else if (audio_delegate_->IsOutputAudioMuted() && |
| 238 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { |
| 239 audio_delegate_->SetOutputAudioIsMuted(false); |
| 240 } |
| 241 } |
| 242 |
| 243 void VolumeView::Layout() { |
| 244 views::View::Layout(); |
| 245 |
| 246 if (!more_->visible()) { |
| 247 int w = width() - slider_->bounds().x() - |
| 248 kSliderRightPaddingToVolumeViewEdge; |
| 249 slider_->SetSize(gfx::Size(w, slider_->height())); |
| 250 return; |
| 251 } |
| 252 |
| 253 // Make sure the chevron always has the full size. |
| 254 gfx::Size size = more_->GetPreferredSize(); |
| 255 gfx::Rect bounds(size); |
| 256 bounds.set_x(width() - size.width() - kTrayPopupPaddingBetweenItems); |
| 257 bounds.set_y((height() - size.height()) / 2); |
| 258 more_->SetBoundsRect(bounds); |
| 259 |
| 260 // Layout either bar_ or device_type_ at the left of the more_ button. |
| 261 views::View* view_left_to_more; |
| 262 if (device_type_->visible()) |
| 263 view_left_to_more = device_type_; |
| 264 else |
| 265 view_left_to_more = bar_; |
| 266 gfx::Size view_size = view_left_to_more->GetPreferredSize(); |
| 267 gfx::Rect view_bounds(view_size); |
| 268 view_bounds.set_x(more_->bounds().x() - view_size.width() - |
| 269 kExtraPaddingBetweenBarAndMore); |
| 270 view_bounds.set_y((height() - view_size.height()) / 2); |
| 271 view_left_to_more->SetBoundsRect(view_bounds); |
| 272 |
| 273 // Layout vertical bar next to view_left_to_more if device_type_ is visible. |
| 274 if (device_type_->visible()) { |
| 275 gfx::Size bar_size = bar_->GetPreferredSize(); |
| 276 gfx::Rect bar_bounds(bar_size); |
| 277 bar_bounds.set_x(view_left_to_more->bounds().x() - bar_size.width()); |
| 278 bar_bounds.set_y((height() - bar_size.height()) / 2); |
| 279 bar_->SetBoundsRect(bar_bounds); |
| 280 } |
| 281 |
| 282 // Layout slider, calculate slider width. |
| 283 gfx::Rect slider_bounds = slider_->bounds(); |
| 284 slider_bounds.set_width( |
| 285 bar_->bounds().x() |
| 286 - (device_type_->visible() ? 0 : kTrayPopupPaddingBetweenItems) |
| 287 - slider_bounds.x()); |
| 288 slider_->SetBoundsRect(slider_bounds); |
| 289 } |
| 290 |
| 291 void VolumeView::ButtonPressed(views::Button* sender, const ui::Event& event) { |
| 292 CHECK(sender == icon_); |
| 293 bool mute_on = !audio_delegate_->IsOutputAudioMuted(); |
| 294 audio_delegate_->SetOutputAudioIsMuted(mute_on); |
| 295 if (!mute_on) |
| 296 audio_delegate_->AdjustOutputVolumeToAudibleLevel(); |
| 297 icon_->Update(); |
| 298 } |
| 299 |
| 300 void VolumeView::SliderValueChanged(views::Slider* sender, |
| 301 float value, |
| 302 float old_value, |
| 303 views::SliderChangeReason reason) { |
| 304 if (reason == views::VALUE_CHANGED_BY_USER) { |
| 305 float new_volume = value * 100.0f; |
| 306 float current_volume = audio_delegate_->GetOutputVolumeLevel(); |
| 307 // Do not call change audio volume if the difference is less than |
| 308 // 1%, which is beyond cras audio api's granularity for output volume. |
| 309 if (std::abs(new_volume - current_volume) < 1.0f) |
| 310 return; |
| 311 Shell::GetInstance()->metrics()->RecordUserMetricsAction( |
| 312 is_default_view_ ? |
| 313 ash::UMA_STATUS_AREA_CHANGED_VOLUME_MENU : |
| 314 ash::UMA_STATUS_AREA_CHANGED_VOLUME_POPUP); |
| 315 if (new_volume > current_volume) |
| 316 HandleVolumeUp(new_volume); |
| 317 else |
| 318 HandleVolumeDown(new_volume); |
| 319 } |
| 320 icon_->Update(); |
| 321 } |
| 322 |
| 323 bool VolumeView::PerformAction(const ui::Event& event) { |
| 324 if (!more_->visible()) |
| 325 return false; |
| 326 owner_->TransitionDetailedView(); |
| 327 return true; |
| 328 } |
| 329 |
| 330 } // namespace tray |
| 331 } // namespace internal |
| 332 } // namespace ash |
OLD | NEW |