| 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/common/system/audio/volume_view.h" | |
| 6 | |
| 7 #include "ash/common/ash_constants.h" | |
| 8 #include "ash/common/material_design/material_design_controller.h" | |
| 9 #include "ash/common/metrics/user_metrics_action.h" | |
| 10 #include "ash/common/system/audio/tray_audio.h" | |
| 11 #include "ash/common/system/audio/tray_audio_delegate.h" | |
| 12 #include "ash/common/system/tray/system_tray_item.h" | |
| 13 #include "ash/common/system/tray/tray_constants.h" | |
| 14 #include "ash/common/system/tray/tray_popup_item_container.h" | |
| 15 #include "ash/common/wm_shell.h" | |
| 16 #include "ash/resources/vector_icons/vector_icons.h" | |
| 17 #include "grit/ash_resources.h" | |
| 18 #include "grit/ash_strings.h" | |
| 19 #include "ui/accessibility/ax_node_data.h" | |
| 20 #include "ui/base/resource/resource_bundle.h" | |
| 21 #include "ui/gfx/canvas.h" | |
| 22 #include "ui/gfx/image/image_skia_operations.h" | |
| 23 #include "ui/gfx/paint_vector_icon.h" | |
| 24 #include "ui/gfx/vector_icon_types.h" | |
| 25 #include "ui/views/background.h" | |
| 26 #include "ui/views/border.h" | |
| 27 #include "ui/views/controls/button/image_button.h" | |
| 28 #include "ui/views/controls/image_view.h" | |
| 29 #include "ui/views/controls/separator.h" | |
| 30 #include "ui/views/controls/slider.h" | |
| 31 #include "ui/views/layout/box_layout.h" | |
| 32 #include "ui/views/painter.h" | |
| 33 | |
| 34 namespace { | |
| 35 const int kVolumeImageWidth = 25; | |
| 36 const int kVolumeImageHeight = 25; | |
| 37 const int kSeparatorSize = 3; | |
| 38 const int kSeparatorVerticalInset = 8; | |
| 39 const int kSliderRightPaddingToVolumeViewEdge = 17; | |
| 40 const int kExtraPaddingBetweenBarAndMore = 10; | |
| 41 const int kExtraPaddingBetweenIconAndSlider = 8; | |
| 42 const int kBoxLayoutPadding = 2; | |
| 43 | |
| 44 // IDR_AURA_UBER_TRAY_VOLUME_LEVELS contains 5 images, | |
| 45 // The one for mute is at the 0 index and the other | |
| 46 // four are used for ascending volume levels. | |
| 47 const int kVolumeLevels = 4; | |
| 48 | |
| 49 const gfx::VectorIcon* const kVolumeLevelIcons[] = { | |
| 50 &ash::kSystemMenuVolumeMuteIcon, // Muted. | |
| 51 &ash::kSystemMenuVolumeLowIcon, // Low volume. | |
| 52 &ash::kSystemMenuVolumeMediumIcon, // Medium volume. | |
| 53 &ash::kSystemMenuVolumeHighIcon, // High volume. | |
| 54 &ash::kSystemMenuVolumeHighIcon, // Full volume. | |
| 55 }; | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 namespace ash { | |
| 60 namespace tray { | |
| 61 | |
| 62 class VolumeButton : public views::ToggleImageButton { | |
| 63 public: | |
| 64 VolumeButton(views::ButtonListener* listener, | |
| 65 system::TrayAudioDelegate* audio_delegate) | |
| 66 : views::ToggleImageButton(listener), | |
| 67 audio_delegate_(audio_delegate), | |
| 68 image_index_(-1) { | |
| 69 SetFocusBehavior(FocusBehavior::ALWAYS); | |
| 70 SetFocusPainter(views::Painter::CreateSolidFocusPainter( | |
| 71 kFocusBorderColor, gfx::Insets(1, 1, 1, 1))); | |
| 72 SetImageAlignment(ALIGN_CENTER, ALIGN_MIDDLE); | |
| 73 if (!MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
| 74 image_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 75 IDR_AURA_UBER_TRAY_VOLUME_LEVELS); | |
| 76 } | |
| 77 Update(); | |
| 78 } | |
| 79 | |
| 80 ~VolumeButton() override {} | |
| 81 | |
| 82 void Update() { | |
| 83 float level = | |
| 84 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f; | |
| 85 int volume_levels = MaterialDesignController::IsSystemTrayMenuMaterial() | |
| 86 ? arraysize(kVolumeLevelIcons) - 1 | |
| 87 : kVolumeLevels; | |
| 88 int image_index = | |
| 89 audio_delegate_->IsOutputAudioMuted() | |
| 90 ? 0 | |
| 91 : (level == 1.0 ? volume_levels | |
| 92 : std::max(1, static_cast<int>(std::ceil( | |
| 93 level * (volume_levels - 1))))); | |
| 94 if (image_index != image_index_) { | |
| 95 gfx::ImageSkia image_skia; | |
| 96 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
| 97 image_skia = gfx::CreateVectorIcon(*kVolumeLevelIcons[image_index], | |
| 98 kMenuIconColor); | |
| 99 } else { | |
| 100 gfx::Rect region(0, image_index * kVolumeImageHeight, kVolumeImageWidth, | |
| 101 kVolumeImageHeight); | |
| 102 image_skia = gfx::ImageSkiaOperations::ExtractSubset( | |
| 103 *(image_.ToImageSkia()), region); | |
| 104 } | |
| 105 SetImage(views::CustomButton::STATE_NORMAL, &image_skia); | |
| 106 image_index_ = image_index; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 private: | |
| 111 // views::View: | |
| 112 gfx::Size GetPreferredSize() const override { | |
| 113 gfx::Size size = views::ToggleImageButton::GetPreferredSize(); | |
| 114 size.set_height(GetTrayConstant(TRAY_POPUP_ITEM_HEIGHT)); | |
| 115 return size; | |
| 116 } | |
| 117 | |
| 118 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { | |
| 119 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 120 node_data->SetName( | |
| 121 bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_VOLUME_MUTE)); | |
| 122 node_data->role = ui::AX_ROLE_TOGGLE_BUTTON; | |
| 123 if (audio_delegate_->IsOutputAudioMuted()) | |
| 124 node_data->AddStateFlag(ui::AX_STATE_PRESSED); | |
| 125 } | |
| 126 | |
| 127 // views::CustomButton: | |
| 128 void StateChanged() override { | |
| 129 if (state() == STATE_HOVERED || state() == STATE_PRESSED) { | |
| 130 set_background( | |
| 131 views::Background::CreateSolidBackground(kHoverBackgroundColor)); | |
| 132 } else { | |
| 133 set_background(nullptr); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 system::TrayAudioDelegate* audio_delegate_; | |
| 138 gfx::Image image_; | |
| 139 int image_index_; | |
| 140 | |
| 141 DISALLOW_COPY_AND_ASSIGN(VolumeButton); | |
| 142 }; | |
| 143 | |
| 144 VolumeView::VolumeView(SystemTrayItem* owner, | |
| 145 system::TrayAudioDelegate* audio_delegate, | |
| 146 bool is_default_view) | |
| 147 : ActionableView(owner), | |
| 148 audio_delegate_(audio_delegate), | |
| 149 icon_(NULL), | |
| 150 slider_(NULL), | |
| 151 device_type_(NULL), | |
| 152 more_(NULL), | |
| 153 is_default_view_(is_default_view) { | |
| 154 SetFocusBehavior(FocusBehavior::NEVER); | |
| 155 views::BoxLayout* box_layout = new views::BoxLayout( | |
| 156 views::BoxLayout::kHorizontal, 0, 0, kBoxLayoutPadding); | |
| 157 box_layout->SetDefaultFlex(0); | |
| 158 SetLayoutManager(box_layout); | |
| 159 | |
| 160 icon_ = new VolumeButton(this, audio_delegate_); | |
| 161 icon_->SetBorder(views::CreateEmptyBorder(0, kTrayPopupPaddingHorizontal, 0, | |
| 162 kExtraPaddingBetweenIconAndSlider)); | |
| 163 AddChildView(icon_); | |
| 164 slider_ = views::Slider::CreateSlider( | |
| 165 ash::MaterialDesignController::IsSystemTrayMenuMaterial(), this); | |
| 166 | |
| 167 if (ash::MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
| 168 slider_->SetBorder(views::CreateEmptyBorder( | |
| 169 gfx::Insets(0, kTrayPopupSliderPaddingMD) + slider_->GetInsets())); | |
| 170 } else { | |
| 171 slider_->SetBorder( | |
| 172 views::CreateEmptyBorder(0, 0, 0, kTrayPopupPaddingBetweenItems)); | |
| 173 } | |
| 174 | |
| 175 slider_->set_focus_border_color(kFocusBorderColor); | |
| 176 slider_->SetValue( | |
| 177 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f); | |
| 178 slider_->SetAccessibleName( | |
| 179 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
| 180 IDS_ASH_STATUS_TRAY_VOLUME)); | |
| 181 AddChildView(slider_); | |
| 182 box_layout->SetFlexForView(slider_, 1); | |
| 183 | |
| 184 separator_ = new views::Separator(views::Separator::VERTICAL); | |
| 185 separator_->SetColor(kButtonStrokeColor); | |
| 186 separator_->SetPreferredSize(kSeparatorSize); | |
| 187 separator_->SetBorder(views::CreateEmptyBorder( | |
| 188 kSeparatorVerticalInset, 0, kSeparatorVerticalInset, kBoxLayoutPadding)); | |
| 189 | |
| 190 more_region_ = new TrayPopupItemContainer(separator_, true); | |
| 191 more_region_->SetBorder( | |
| 192 views::CreateEmptyBorder(0, 0, 0, kTrayPopupPaddingBetweenItems)); | |
| 193 AddChildView(more_region_); | |
| 194 | |
| 195 device_type_ = new views::ImageView; | |
| 196 more_region_->AddChildView(device_type_); | |
| 197 | |
| 198 more_ = new views::ImageView; | |
| 199 more_->EnableCanvasFlippingForRTLUI(true); | |
| 200 | |
| 201 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
| 202 more_->SetImage( | |
| 203 gfx::CreateVectorIcon(kSystemMenuArrowRightIcon, kMenuIconColor)); | |
| 204 } else { | |
| 205 more_->SetImage(ui::ResourceBundle::GetSharedInstance() | |
| 206 .GetImageNamed(IDR_AURA_UBER_TRAY_MORE) | |
| 207 .ToImageSkia()); | |
| 208 } | |
| 209 | |
| 210 more_region_->AddChildView(more_); | |
| 211 | |
| 212 set_background(views::Background::CreateSolidBackground(kBackgroundColor)); | |
| 213 | |
| 214 Update(); | |
| 215 } | |
| 216 | |
| 217 VolumeView::~VolumeView() {} | |
| 218 | |
| 219 void VolumeView::Update() { | |
| 220 icon_->Update(); | |
| 221 slider_->UpdateState(!audio_delegate_->IsOutputAudioMuted()); | |
| 222 UpdateDeviceTypeAndMore(); | |
| 223 Layout(); | |
| 224 } | |
| 225 | |
| 226 void VolumeView::SetVolumeLevel(float percent) { | |
| 227 // Slider's value is in finer granularity than audio volume level(0.01), | |
| 228 // there will be a small discrepancy between slider's value and volume level | |
| 229 // on audio side. To avoid the jittering in slider UI, do not set change | |
| 230 // slider value if the change is less than 1%. | |
| 231 if (std::abs(percent - slider_->value()) < 0.01) | |
| 232 return; | |
| 233 slider_->SetValue(percent); | |
| 234 // It is possible that the volume was (un)muted, but the actual volume level | |
| 235 // did not change. In that case, setting the value of the slider won't | |
| 236 // trigger an update. So explicitly trigger an update. | |
| 237 Update(); | |
| 238 slider_->set_enable_accessibility_events(true); | |
| 239 } | |
| 240 | |
| 241 void VolumeView::UpdateDeviceTypeAndMore() { | |
| 242 bool show_more = is_default_view_ && TrayAudio::ShowAudioDeviceMenu() && | |
| 243 audio_delegate_->HasAlternativeSources(); | |
| 244 if (!ash::MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
| 245 slider_->SetBorder(views::CreateEmptyBorder( | |
| 246 0, 0, 0, show_more ? kTrayPopupPaddingBetweenItems | |
| 247 : kSliderRightPaddingToVolumeViewEdge)); | |
| 248 } | |
| 249 if (!show_more) { | |
| 250 more_region_->SetVisible(false); | |
| 251 return; | |
| 252 } | |
| 253 | |
| 254 // Show output device icon if necessary. | |
| 255 device_type_->SetVisible(false); | |
| 256 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
| 257 const gfx::VectorIcon& device_icon = | |
| 258 audio_delegate_->GetActiveOutputDeviceVectorIcon(); | |
| 259 if (!device_icon.is_empty()) { | |
| 260 device_type_->SetImage( | |
| 261 gfx::CreateVectorIcon(device_icon, kMenuIconColor)); | |
| 262 device_type_->SetVisible(true); | |
| 263 } | |
| 264 } else { | |
| 265 int device_icon = audio_delegate_->GetActiveOutputDeviceIconId(); | |
| 266 if (device_icon != system::TrayAudioDelegate::kNoAudioDeviceIcon) { | |
| 267 device_type_->SetImage(ui::ResourceBundle::GetSharedInstance() | |
| 268 .GetImageNamed(device_icon) | |
| 269 .ToImageSkia()); | |
| 270 device_type_->SetVisible(true); | |
| 271 } | |
| 272 } | |
| 273 int spacing = kTrayPopupPaddingBetweenItems; | |
| 274 if (!device_type_->visible()) | |
| 275 spacing += kExtraPaddingBetweenBarAndMore; | |
| 276 more_region_->SetLayoutManager( | |
| 277 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, spacing)); | |
| 278 more_region_->SetVisible(true); | |
| 279 } | |
| 280 | |
| 281 void VolumeView::HandleVolumeUp(float level) { | |
| 282 audio_delegate_->SetOutputVolumeLevel(level); | |
| 283 if (audio_delegate_->IsOutputAudioMuted() && | |
| 284 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { | |
| 285 audio_delegate_->SetOutputAudioIsMuted(false); | |
| 286 } | |
| 287 } | |
| 288 | |
| 289 void VolumeView::HandleVolumeDown(float level) { | |
| 290 audio_delegate_->SetOutputVolumeLevel(level); | |
| 291 if (!audio_delegate_->IsOutputAudioMuted() && | |
| 292 level <= audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { | |
| 293 audio_delegate_->SetOutputAudioIsMuted(true); | |
| 294 } else if (audio_delegate_->IsOutputAudioMuted() && | |
| 295 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { | |
| 296 audio_delegate_->SetOutputAudioIsMuted(false); | |
| 297 } | |
| 298 } | |
| 299 | |
| 300 void VolumeView::ButtonPressed(views::Button* sender, const ui::Event& event) { | |
| 301 if (sender != icon_) { | |
| 302 ActionableView::ButtonPressed(sender, event); | |
| 303 return; | |
| 304 } | |
| 305 | |
| 306 bool mute_on = !audio_delegate_->IsOutputAudioMuted(); | |
| 307 audio_delegate_->SetOutputAudioIsMuted(mute_on); | |
| 308 if (!mute_on) | |
| 309 audio_delegate_->AdjustOutputVolumeToAudibleLevel(); | |
| 310 icon_->Update(); | |
| 311 } | |
| 312 | |
| 313 void VolumeView::SliderValueChanged(views::Slider* sender, | |
| 314 float value, | |
| 315 float old_value, | |
| 316 views::SliderChangeReason reason) { | |
| 317 if (reason == views::VALUE_CHANGED_BY_USER) { | |
| 318 float new_volume = value * 100.0f; | |
| 319 float current_volume = audio_delegate_->GetOutputVolumeLevel(); | |
| 320 // Do not call change audio volume if the difference is less than | |
| 321 // 1%, which is beyond cras audio api's granularity for output volume. | |
| 322 if (std::abs(new_volume - current_volume) < 1.0f) | |
| 323 return; | |
| 324 WmShell::Get()->RecordUserMetricsAction( | |
| 325 is_default_view_ ? UMA_STATUS_AREA_CHANGED_VOLUME_MENU | |
| 326 : UMA_STATUS_AREA_CHANGED_VOLUME_POPUP); | |
| 327 if (new_volume > current_volume) | |
| 328 HandleVolumeUp(new_volume); | |
| 329 else | |
| 330 HandleVolumeDown(new_volume); | |
| 331 } | |
| 332 icon_->Update(); | |
| 333 } | |
| 334 | |
| 335 bool VolumeView::PerformAction(const ui::Event& event) { | |
| 336 if (!more_region_->visible()) | |
| 337 return false; | |
| 338 owner()->TransitionDetailedView(); | |
| 339 return true; | |
| 340 } | |
| 341 | |
| 342 void VolumeView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
| 343 // Separator's prefered size is based on set bounds. When an empty bounds is | |
| 344 // set on first layout this causes BoxLayout to ignore the separator. Reset | |
| 345 // its height on each bounds change so that it is laid out properly. | |
| 346 separator_->SetSize(gfx::Size(kSeparatorSize, bounds().height())); | |
| 347 } | |
| 348 | |
| 349 void VolumeView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | |
| 350 // Intentionally overrides ActionableView, leaving |state| unset. A slider | |
| 351 // childview exposes accessibility data. | |
| 352 } | |
| 353 | |
| 354 } // namespace tray | |
| 355 } // namespace ash | |
| OLD | NEW |