Chromium Code Reviews| 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/common/system/chromeos/audio/volume_view.h" | 5 #include "ash/common/system/chromeos/audio/volume_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ash/common/metrics/user_metrics_action.h" | 9 #include "ash/common/metrics/user_metrics_action.h" |
| 10 #include "ash/common/system/chromeos/audio/tray_audio_delegate.h" | |
| 11 #include "ash/common/system/tray/actionable_view.h" | 10 #include "ash/common/system/tray/actionable_view.h" |
| 12 #include "ash/common/system/tray/system_tray_item.h" | 11 #include "ash/common/system/tray/system_tray_item.h" |
| 13 #include "ash/common/system/tray/tray_constants.h" | 12 #include "ash/common/system/tray/tray_constants.h" |
| 14 #include "ash/common/system/tray/tray_popup_utils.h" | 13 #include "ash/common/system/tray/tray_popup_utils.h" |
| 15 #include "ash/common/system/tray/tri_view.h" | 14 #include "ash/common/system/tray/tri_view.h" |
| 16 #include "ash/common/wm_shell.h" | 15 #include "ash/common/wm_shell.h" |
| 17 #include "ash/resources/vector_icons/vector_icons.h" | 16 #include "ash/resources/vector_icons/vector_icons.h" |
| 18 #include "ash/strings/grit/ash_strings.h" | 17 #include "ash/strings/grit/ash_strings.h" |
| 18 #include "chromeos/audio/cras_audio_handler.h" | |
| 19 #include "ui/accessibility/ax_node_data.h" | 19 #include "ui/accessibility/ax_node_data.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "ui/gfx/paint_vector_icon.h" | 21 #include "ui/gfx/paint_vector_icon.h" |
| 22 #include "ui/gfx/vector_icon_types.h" | 22 #include "ui/gfx/vector_icon_types.h" |
| 23 #include "ui/views/background.h" | 23 #include "ui/views/background.h" |
| 24 #include "ui/views/border.h" | 24 #include "ui/views/border.h" |
| 25 #include "ui/views/controls/button/custom_button.h" | 25 #include "ui/views/controls/button/custom_button.h" |
| 26 #include "ui/views/controls/image_view.h" | 26 #include "ui/views/controls/image_view.h" |
| 27 #include "ui/views/controls/slider.h" | 27 #include "ui/views/controls/slider.h" |
| 28 #include "ui/views/layout/fill_layout.h" | 28 #include "ui/views/layout/fill_layout.h" |
| 29 | 29 |
| 30 using chromeos::CrasAudioHandler; | |
| 31 | |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| 32 const gfx::VectorIcon* const kVolumeLevelIcons[] = { | 34 const gfx::VectorIcon* const kVolumeLevelIcons[] = { |
| 33 &ash::kSystemMenuVolumeMuteIcon, // Muted. | 35 &ash::kSystemMenuVolumeMuteIcon, // Muted. |
| 34 &ash::kSystemMenuVolumeLowIcon, // Low volume. | 36 &ash::kSystemMenuVolumeLowIcon, // Low volume. |
| 35 &ash::kSystemMenuVolumeMediumIcon, // Medium volume. | 37 &ash::kSystemMenuVolumeMediumIcon, // Medium volume. |
| 36 &ash::kSystemMenuVolumeHighIcon, // High volume. | 38 &ash::kSystemMenuVolumeHighIcon, // High volume. |
| 37 &ash::kSystemMenuVolumeHighIcon, // Full volume. | 39 &ash::kSystemMenuVolumeHighIcon, // Full volume. |
| 38 }; | 40 }; |
| 39 | 41 |
| 42 const gfx::VectorIcon& GetActiveOutputDeviceVectorIcon() { | |
| 43 chromeos::AudioDevice device; | |
| 44 if (CrasAudioHandler::Get()->GetPrimaryActiveOutputDevice(&device)) { | |
| 45 if (device.type == chromeos::AUDIO_TYPE_HEADPHONE) | |
| 46 return ash::kSystemMenuHeadsetIcon; | |
| 47 if (device.type == chromeos::AUDIO_TYPE_USB) | |
| 48 return ash::kSystemMenuUsbIcon; | |
| 49 if (device.type == chromeos::AUDIO_TYPE_BLUETOOTH) | |
| 50 return ash::kSystemMenuBluetoothIcon; | |
| 51 if (device.type == chromeos::AUDIO_TYPE_HDMI) | |
| 52 return ash::kSystemMenuHdmiIcon; | |
| 53 } | |
| 54 return gfx::kNoneIcon; | |
| 55 } | |
| 56 | |
| 40 } // namespace | 57 } // namespace |
| 41 | 58 |
| 42 namespace ash { | 59 namespace ash { |
|
James Cook
2017/03/25 03:45:06
nit: move namespace ash:: up above namespace {} an
mohsen
2017/03/27 22:22:18
Done.
| |
| 43 namespace tray { | 60 namespace tray { |
| 44 | 61 |
| 45 class VolumeButton : public ButtonListenerActionableView { | 62 class VolumeButton : public ButtonListenerActionableView { |
| 46 public: | 63 public: |
| 47 VolumeButton(SystemTrayItem* owner, | 64 VolumeButton(SystemTrayItem* owner, views::ButtonListener* listener) |
| 48 views::ButtonListener* listener, | |
| 49 system::TrayAudioDelegate* audio_delegate) | |
| 50 : ButtonListenerActionableView(owner, | 65 : ButtonListenerActionableView(owner, |
| 51 TrayPopupInkDropStyle::HOST_CENTERED, | 66 TrayPopupInkDropStyle::HOST_CENTERED, |
| 52 listener), | 67 listener), |
| 53 audio_delegate_(audio_delegate), | |
| 54 image_(TrayPopupUtils::CreateMainImageView()), | 68 image_(TrayPopupUtils::CreateMainImageView()), |
| 55 image_index_(-1) { | 69 image_index_(-1) { |
| 56 TrayPopupUtils::ConfigureContainer(TriView::Container::START, this); | 70 TrayPopupUtils::ConfigureContainer(TriView::Container::START, this); |
| 57 AddChildView(image_); | 71 AddChildView(image_); |
| 58 SetInkDropMode(InkDropMode::ON); | 72 SetInkDropMode(InkDropMode::ON); |
| 59 Update(); | 73 Update(); |
| 60 | 74 |
| 61 set_notify_enter_exit_on_child(true); | 75 set_notify_enter_exit_on_child(true); |
| 62 } | 76 } |
| 63 | 77 |
| 64 ~VolumeButton() override {} | 78 ~VolumeButton() override {} |
| 65 | 79 |
| 66 void Update() { | 80 void Update() { |
| 67 float level = | 81 float level = CrasAudioHandler::Get()->GetOutputVolumePercent() / 100.0f; |
| 68 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f; | |
| 69 int volume_levels = arraysize(kVolumeLevelIcons) - 1; | 82 int volume_levels = arraysize(kVolumeLevelIcons) - 1; |
| 70 int image_index = | 83 int image_index = |
| 71 audio_delegate_->IsOutputAudioMuted() | 84 CrasAudioHandler::Get()->IsOutputMuted() |
| 72 ? 0 | 85 ? 0 |
| 73 : (level == 1.0 ? volume_levels | 86 : (level == 1.0 ? volume_levels |
| 74 : std::max(1, static_cast<int>(std::ceil( | 87 : std::max(1, static_cast<int>(std::ceil( |
| 75 level * (volume_levels - 1))))); | 88 level * (volume_levels - 1))))); |
| 76 gfx::ImageSkia image_skia = | 89 gfx::ImageSkia image_skia = |
| 77 gfx::CreateVectorIcon(*kVolumeLevelIcons[image_index], kMenuIconColor); | 90 gfx::CreateVectorIcon(*kVolumeLevelIcons[image_index], kMenuIconColor); |
| 78 image_->SetImage(&image_skia); | 91 image_->SetImage(&image_skia); |
| 79 image_index_ = image_index; | 92 image_index_ = image_index; |
| 80 } | 93 } |
| 81 | 94 |
| 82 private: | 95 private: |
| 83 // views::View: | 96 // views::View: |
| 84 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { | 97 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { |
| 85 node_data->SetName( | 98 node_data->SetName( |
| 86 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VOLUME_MUTE)); | 99 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VOLUME_MUTE)); |
| 87 node_data->role = ui::AX_ROLE_TOGGLE_BUTTON; | 100 node_data->role = ui::AX_ROLE_TOGGLE_BUTTON; |
| 88 if (audio_delegate_->IsOutputAudioMuted()) | 101 if (CrasAudioHandler::Get()->IsOutputMuted()) |
| 89 node_data->AddStateFlag(ui::AX_STATE_PRESSED); | 102 node_data->AddStateFlag(ui::AX_STATE_PRESSED); |
| 90 } | 103 } |
| 91 | 104 |
| 92 system::TrayAudioDelegate* audio_delegate_; | |
| 93 views::ImageView* image_; | 105 views::ImageView* image_; |
| 94 int image_index_; | 106 int image_index_; |
| 95 | 107 |
| 96 DISALLOW_COPY_AND_ASSIGN(VolumeButton); | 108 DISALLOW_COPY_AND_ASSIGN(VolumeButton); |
| 97 }; | 109 }; |
| 98 | 110 |
| 99 VolumeView::VolumeView(SystemTrayItem* owner, | 111 VolumeView::VolumeView(SystemTrayItem* owner, |
| 100 system::TrayAudioDelegate* audio_delegate, | |
| 101 bool is_default_view) | 112 bool is_default_view) |
| 102 : owner_(owner), | 113 : owner_(owner), |
| 103 tri_view_(TrayPopupUtils::CreateMultiTargetRowView()), | 114 tri_view_(TrayPopupUtils::CreateMultiTargetRowView()), |
| 104 audio_delegate_(audio_delegate), | |
| 105 more_button_(nullptr), | 115 more_button_(nullptr), |
| 106 icon_(nullptr), | 116 icon_(nullptr), |
| 107 slider_(nullptr), | 117 slider_(nullptr), |
| 108 device_type_(nullptr), | 118 device_type_(nullptr), |
| 109 is_default_view_(is_default_view) { | 119 is_default_view_(is_default_view) { |
| 110 SetLayoutManager(new views::FillLayout); | 120 SetLayoutManager(new views::FillLayout); |
| 111 AddChildView(tri_view_); | 121 AddChildView(tri_view_); |
| 112 | 122 |
| 113 icon_ = new VolumeButton(owner, this, audio_delegate_); | 123 icon_ = new VolumeButton(owner, this); |
| 114 tri_view_->AddView(TriView::Container::START, icon_); | 124 tri_view_->AddView(TriView::Container::START, icon_); |
| 115 | 125 |
| 116 slider_ = TrayPopupUtils::CreateSlider(this); | 126 slider_ = TrayPopupUtils::CreateSlider(this); |
| 117 slider_->SetValue( | 127 slider_->SetValue(CrasAudioHandler::Get()->GetOutputVolumePercent() / 100.0f); |
| 118 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f); | |
| 119 slider_->SetAccessibleName( | 128 slider_->SetAccessibleName( |
| 120 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VOLUME)); | 129 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VOLUME)); |
| 121 tri_view_->AddView(TriView::Container::CENTER, slider_); | 130 tri_view_->AddView(TriView::Container::CENTER, slider_); |
| 122 | 131 |
| 123 set_background(views::Background::CreateSolidBackground(kBackgroundColor)); | 132 set_background(views::Background::CreateSolidBackground(kBackgroundColor)); |
| 124 | 133 |
| 125 if (!is_default_view_) { | 134 if (!is_default_view_) { |
| 126 tri_view_->SetContainerVisible(TriView::Container::END, false); | 135 tri_view_->SetContainerVisible(TriView::Container::END, false); |
| 127 Update(); | 136 Update(); |
| 128 return; | 137 return; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 144 more_button_->AddChildView(TrayPopupUtils::CreateMoreImageView()); | 153 more_button_->AddChildView(TrayPopupUtils::CreateMoreImageView()); |
| 145 more_button_->SetAccessibleName( | 154 more_button_->SetAccessibleName( |
| 146 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO)); | 155 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO)); |
| 147 Update(); | 156 Update(); |
| 148 } | 157 } |
| 149 | 158 |
| 150 VolumeView::~VolumeView() {} | 159 VolumeView::~VolumeView() {} |
| 151 | 160 |
| 152 void VolumeView::Update() { | 161 void VolumeView::Update() { |
| 153 icon_->Update(); | 162 icon_->Update(); |
| 154 slider_->UpdateState(!audio_delegate_->IsOutputAudioMuted()); | 163 slider_->UpdateState(!CrasAudioHandler::Get()->IsOutputMuted()); |
| 155 UpdateDeviceTypeAndMore(); | 164 UpdateDeviceTypeAndMore(); |
| 156 Layout(); | 165 Layout(); |
| 157 } | 166 } |
| 158 | 167 |
| 159 void VolumeView::SetVolumeLevel(float percent) { | 168 void VolumeView::SetVolumeLevel(float percent) { |
| 160 // Update volume level to the current audio level. | 169 // Update volume level to the current audio level. |
| 161 Update(); | 170 Update(); |
| 162 | 171 |
| 163 // 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), |
| 164 // 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 |
| 165 // 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 |
| 166 // slider value if the change is less than 1%. | 175 // slider value if the change is less than 1%. |
| 167 if (std::abs(percent - slider_->value()) < 0.01) | 176 if (std::abs(percent - slider_->value()) < 0.01) |
| 168 return; | 177 return; |
| 169 slider_->SetValue(percent); | 178 slider_->SetValue(percent); |
| 170 // It is possible that the volume was (un)muted, but the actual volume level | 179 // It is possible that the volume was (un)muted, but the actual volume level |
| 171 // did not change. In that case, setting the value of the slider won't | 180 // did not change. In that case, setting the value of the slider won't |
| 172 // trigger an update. So explicitly trigger an update. | 181 // trigger an update. So explicitly trigger an update. |
| 173 Update(); | 182 Update(); |
| 174 slider_->set_enable_accessibility_events(true); | 183 slider_->set_enable_accessibility_events(true); |
| 175 } | 184 } |
| 176 | 185 |
| 177 void VolumeView::UpdateDeviceTypeAndMore() { | 186 void VolumeView::UpdateDeviceTypeAndMore() { |
| 178 bool show_more = is_default_view_ && audio_delegate_->HasAlternativeSources(); | 187 CrasAudioHandler* audio_handler = CrasAudioHandler::Get(); |
| 188 bool show_more = | |
| 189 is_default_view_ && (audio_handler->has_alternative_output() || | |
| 190 audio_handler->has_alternative_input()); | |
| 179 | 191 |
| 180 if (!show_more) | 192 if (!show_more) |
| 181 return; | 193 return; |
| 182 | 194 |
| 183 const gfx::VectorIcon& device_icon = | 195 const gfx::VectorIcon& device_icon = GetActiveOutputDeviceVectorIcon(); |
| 184 audio_delegate_->GetActiveOutputDeviceVectorIcon(); | |
| 185 const bool target_visibility = !device_icon.is_empty(); | 196 const bool target_visibility = !device_icon.is_empty(); |
| 186 if (target_visibility) | 197 if (target_visibility) |
| 187 device_type_->SetImage(gfx::CreateVectorIcon(device_icon, kMenuIconColor)); | 198 device_type_->SetImage(gfx::CreateVectorIcon(device_icon, kMenuIconColor)); |
| 188 if (device_type_->visible() != target_visibility) { | 199 if (device_type_->visible() != target_visibility) { |
| 189 device_type_->SetVisible(target_visibility); | 200 device_type_->SetVisible(target_visibility); |
| 190 device_type_->InvalidateLayout(); | 201 device_type_->InvalidateLayout(); |
| 191 } | 202 } |
| 192 } | 203 } |
| 193 | 204 |
| 194 void VolumeView::HandleVolumeUp(int level) { | 205 void VolumeView::HandleVolumeUp(int level) { |
| 195 audio_delegate_->SetOutputVolumeLevel(level); | 206 CrasAudioHandler::Get()->SetOutputVolumePercent(level); |
| 196 if (audio_delegate_->IsOutputAudioMuted() && | 207 if (CrasAudioHandler::Get()->IsOutputMuted() && |
| 197 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { | 208 level > CrasAudioHandler::Get()->GetOutputDefaultVolumeMuteThreshold()) { |
| 198 audio_delegate_->SetOutputAudioIsMuted(false); | 209 CrasAudioHandler::Get()->SetOutputMute(false); |
| 199 } | 210 } |
| 200 } | 211 } |
| 201 | 212 |
| 202 void VolumeView::HandleVolumeDown(int level) { | 213 void VolumeView::HandleVolumeDown(int level) { |
| 203 audio_delegate_->SetOutputVolumeLevel(level); | 214 CrasAudioHandler::Get()->SetOutputVolumePercent(level); |
|
James Cook
2017/03/25 03:45:06
optional: cache CrasAudioHandler* in a local var s
mohsen
2017/03/27 22:22:18
Done. Here, and in a few other places.
| |
| 204 if (!audio_delegate_->IsOutputAudioMuted() && | 215 if (!CrasAudioHandler::Get()->IsOutputMuted() && |
| 205 level <= audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { | 216 level <= CrasAudioHandler::Get()->GetOutputDefaultVolumeMuteThreshold()) { |
| 206 audio_delegate_->SetOutputAudioIsMuted(true); | 217 CrasAudioHandler::Get()->SetOutputMute(true); |
| 207 } else if (audio_delegate_->IsOutputAudioMuted() && | 218 } else if (CrasAudioHandler::Get()->IsOutputMuted() && |
| 208 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { | 219 level > CrasAudioHandler::Get() |
| 209 audio_delegate_->SetOutputAudioIsMuted(false); | 220 ->GetOutputDefaultVolumeMuteThreshold()) { |
| 221 CrasAudioHandler::Get()->SetOutputMute(false); | |
| 210 } | 222 } |
| 211 } | 223 } |
| 212 | 224 |
| 213 void VolumeView::ButtonPressed(views::Button* sender, const ui::Event& event) { | 225 void VolumeView::ButtonPressed(views::Button* sender, const ui::Event& event) { |
| 214 if (sender == icon_) { | 226 if (sender == icon_) { |
| 215 bool mute_on = !audio_delegate_->IsOutputAudioMuted(); | 227 bool mute_on = !CrasAudioHandler::Get()->IsOutputMuted(); |
| 216 audio_delegate_->SetOutputAudioIsMuted(mute_on); | 228 CrasAudioHandler::Get()->SetOutputMute(mute_on); |
| 217 if (!mute_on) | 229 if (!mute_on) |
| 218 audio_delegate_->AdjustOutputVolumeToAudibleLevel(); | 230 CrasAudioHandler::Get()->AdjustOutputVolumeToAudibleLevel(); |
| 219 icon_->Update(); | 231 icon_->Update(); |
| 220 } else if (sender == more_button_) { | 232 } else if (sender == more_button_) { |
| 221 owner_->TransitionDetailedView(); | 233 owner_->TransitionDetailedView(); |
| 222 } else { | 234 } else { |
| 223 NOTREACHED() << "Unexpected sender=" << sender->GetClassName() << "."; | 235 NOTREACHED() << "Unexpected sender=" << sender->GetClassName() << "."; |
| 224 } | 236 } |
| 225 } | 237 } |
| 226 | 238 |
| 227 void VolumeView::SliderValueChanged(views::Slider* sender, | 239 void VolumeView::SliderValueChanged(views::Slider* sender, |
| 228 float value, | 240 float value, |
| 229 float old_value, | 241 float old_value, |
| 230 views::SliderChangeReason reason) { | 242 views::SliderChangeReason reason) { |
| 231 if (reason == views::VALUE_CHANGED_BY_USER) { | 243 if (reason == views::VALUE_CHANGED_BY_USER) { |
| 232 int new_volume = static_cast<int>(value * 100); | 244 int new_volume = static_cast<int>(value * 100); |
| 233 int current_volume = audio_delegate_->GetOutputVolumeLevel(); | 245 int current_volume = CrasAudioHandler::Get()->GetOutputVolumePercent(); |
| 234 if (new_volume == current_volume) | 246 if (new_volume == current_volume) |
| 235 return; | 247 return; |
| 236 WmShell::Get()->RecordUserMetricsAction( | 248 WmShell::Get()->RecordUserMetricsAction( |
| 237 is_default_view_ ? UMA_STATUS_AREA_CHANGED_VOLUME_MENU | 249 is_default_view_ ? UMA_STATUS_AREA_CHANGED_VOLUME_MENU |
| 238 : UMA_STATUS_AREA_CHANGED_VOLUME_POPUP); | 250 : UMA_STATUS_AREA_CHANGED_VOLUME_POPUP); |
| 239 if (new_volume > current_volume) | 251 if (new_volume > current_volume) |
| 240 HandleVolumeUp(new_volume); | 252 HandleVolumeUp(new_volume); |
| 241 else | 253 else |
| 242 HandleVolumeDown(new_volume); | 254 HandleVolumeDown(new_volume); |
| 243 } | 255 } |
| 244 icon_->Update(); | 256 icon_->Update(); |
| 245 } | 257 } |
| 246 | 258 |
| 247 } // namespace tray | 259 } // namespace tray |
| 248 } // namespace ash | 260 } // namespace ash |
| OLD | NEW |