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