Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1083)

Side by Side Diff: ash/common/system/chromeos/audio/volume_view.cc

Issue 2778503002: Remove TrayAudioDelegate (Closed)
Patch Set: nits Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/common/system/chromeos/audio/volume_view.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
32 namespace ash {
30 namespace { 33 namespace {
31 34
32 const gfx::VectorIcon* const kVolumeLevelIcons[] = { 35 const gfx::VectorIcon* const kVolumeLevelIcons[] = {
33 &ash::kSystemMenuVolumeMuteIcon, // Muted. 36 &kSystemMenuVolumeMuteIcon, // Muted.
34 &ash::kSystemMenuVolumeLowIcon, // Low volume. 37 &kSystemMenuVolumeLowIcon, // Low volume.
35 &ash::kSystemMenuVolumeMediumIcon, // Medium volume. 38 &kSystemMenuVolumeMediumIcon, // Medium volume.
36 &ash::kSystemMenuVolumeHighIcon, // High volume. 39 &kSystemMenuVolumeHighIcon, // High volume.
37 &ash::kSystemMenuVolumeHighIcon, // Full volume. 40 &kSystemMenuVolumeHighIcon, // Full volume.
38 }; 41 };
39 42
43 const gfx::VectorIcon& GetActiveOutputDeviceVectorIcon() {
44 chromeos::AudioDevice device;
45 if (CrasAudioHandler::Get()->GetPrimaryActiveOutputDevice(&device)) {
46 if (device.type == chromeos::AUDIO_TYPE_HEADPHONE)
47 return kSystemMenuHeadsetIcon;
48 if (device.type == chromeos::AUDIO_TYPE_USB)
49 return kSystemMenuUsbIcon;
50 if (device.type == chromeos::AUDIO_TYPE_BLUETOOTH)
51 return kSystemMenuBluetoothIcon;
52 if (device.type == chromeos::AUDIO_TYPE_HDMI)
53 return kSystemMenuHdmiIcon;
54 }
55 return gfx::kNoneIcon;
56 }
57
40 } // namespace 58 } // namespace
41 59
42 namespace ash {
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 CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
68 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f; 82 float level = audio_handler->GetOutputVolumePercent() / 100.0f;
69 int volume_levels = arraysize(kVolumeLevelIcons) - 1; 83 int volume_levels = arraysize(kVolumeLevelIcons) - 1;
70 int image_index = 84 int image_index =
71 audio_delegate_->IsOutputAudioMuted() 85 audio_handler->IsOutputMuted()
72 ? 0 86 ? 0
73 : (level == 1.0 ? volume_levels 87 : (level == 1.0 ? volume_levels
74 : std::max(1, static_cast<int>(std::ceil( 88 : std::max(1, static_cast<int>(std::ceil(
75 level * (volume_levels - 1))))); 89 level * (volume_levels - 1)))));
76 gfx::ImageSkia image_skia = 90 gfx::ImageSkia image_skia =
77 gfx::CreateVectorIcon(*kVolumeLevelIcons[image_index], kMenuIconColor); 91 gfx::CreateVectorIcon(*kVolumeLevelIcons[image_index], kMenuIconColor);
78 image_->SetImage(&image_skia); 92 image_->SetImage(&image_skia);
79 image_index_ = image_index; 93 image_index_ = image_index;
80 } 94 }
81 95
82 private: 96 private:
83 // views::View: 97 // views::View:
84 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { 98 void GetAccessibleNodeData(ui::AXNodeData* node_data) override {
85 node_data->SetName( 99 node_data->SetName(
86 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VOLUME_MUTE)); 100 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VOLUME_MUTE));
87 node_data->role = ui::AX_ROLE_TOGGLE_BUTTON; 101 node_data->role = ui::AX_ROLE_TOGGLE_BUTTON;
88 if (audio_delegate_->IsOutputAudioMuted()) 102 if (CrasAudioHandler::Get()->IsOutputMuted())
89 node_data->AddStateFlag(ui::AX_STATE_PRESSED); 103 node_data->AddStateFlag(ui::AX_STATE_PRESSED);
90 } 104 }
91 105
92 system::TrayAudioDelegate* audio_delegate_;
93 views::ImageView* image_; 106 views::ImageView* image_;
94 int image_index_; 107 int image_index_;
95 108
96 DISALLOW_COPY_AND_ASSIGN(VolumeButton); 109 DISALLOW_COPY_AND_ASSIGN(VolumeButton);
97 }; 110 };
98 111
99 VolumeView::VolumeView(SystemTrayItem* owner, 112 VolumeView::VolumeView(SystemTrayItem* owner,
100 system::TrayAudioDelegate* audio_delegate,
101 bool is_default_view) 113 bool is_default_view)
102 : owner_(owner), 114 : owner_(owner),
103 tri_view_(TrayPopupUtils::CreateMultiTargetRowView()), 115 tri_view_(TrayPopupUtils::CreateMultiTargetRowView()),
104 audio_delegate_(audio_delegate),
105 more_button_(nullptr), 116 more_button_(nullptr),
106 icon_(nullptr), 117 icon_(nullptr),
107 slider_(nullptr), 118 slider_(nullptr),
108 device_type_(nullptr), 119 device_type_(nullptr),
109 is_default_view_(is_default_view) { 120 is_default_view_(is_default_view) {
110 SetLayoutManager(new views::FillLayout); 121 SetLayoutManager(new views::FillLayout);
111 AddChildView(tri_view_); 122 AddChildView(tri_view_);
112 123
113 icon_ = new VolumeButton(owner, this, audio_delegate_); 124 icon_ = new VolumeButton(owner, this);
114 tri_view_->AddView(TriView::Container::START, icon_); 125 tri_view_->AddView(TriView::Container::START, icon_);
115 126
116 slider_ = TrayPopupUtils::CreateSlider(this); 127 slider_ = TrayPopupUtils::CreateSlider(this);
117 slider_->SetValue( 128 slider_->SetValue(CrasAudioHandler::Get()->GetOutputVolumePercent() / 100.0f);
118 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f);
119 slider_->SetAccessibleName( 129 slider_->SetAccessibleName(
120 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VOLUME)); 130 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VOLUME));
121 tri_view_->AddView(TriView::Container::CENTER, slider_); 131 tri_view_->AddView(TriView::Container::CENTER, slider_);
122 132
123 set_background(views::Background::CreateSolidBackground(kBackgroundColor)); 133 set_background(views::Background::CreateSolidBackground(kBackgroundColor));
124 134
125 if (!is_default_view_) { 135 if (!is_default_view_) {
126 tri_view_->SetContainerVisible(TriView::Container::END, false); 136 tri_view_->SetContainerVisible(TriView::Container::END, false);
127 Update(); 137 Update();
128 return; 138 return;
(...skipping 15 matching lines...) Expand all
144 more_button_->AddChildView(TrayPopupUtils::CreateMoreImageView()); 154 more_button_->AddChildView(TrayPopupUtils::CreateMoreImageView());
145 more_button_->SetAccessibleName( 155 more_button_->SetAccessibleName(
146 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO)); 156 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO));
147 Update(); 157 Update();
148 } 158 }
149 159
150 VolumeView::~VolumeView() {} 160 VolumeView::~VolumeView() {}
151 161
152 void VolumeView::Update() { 162 void VolumeView::Update() {
153 icon_->Update(); 163 icon_->Update();
154 slider_->UpdateState(!audio_delegate_->IsOutputAudioMuted()); 164 slider_->UpdateState(!CrasAudioHandler::Get()->IsOutputMuted());
155 UpdateDeviceTypeAndMore(); 165 UpdateDeviceTypeAndMore();
156 Layout(); 166 Layout();
157 } 167 }
158 168
159 void VolumeView::SetVolumeLevel(float percent) { 169 void VolumeView::SetVolumeLevel(float percent) {
160 // Update volume level to the current audio level. 170 // Update volume level to the current audio level.
161 Update(); 171 Update();
162 172
163 // Slider's value is in finer granularity than audio volume level(0.01), 173 // 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 174 // 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 175 // on audio side. To avoid the jittering in slider UI, do not set change
166 // slider value if the change is less than 1%. 176 // slider value if the change is less than 1%.
167 if (std::abs(percent - slider_->value()) < 0.01) 177 if (std::abs(percent - slider_->value()) < 0.01)
168 return; 178 return;
169 slider_->SetValue(percent); 179 slider_->SetValue(percent);
170 // It is possible that the volume was (un)muted, but the actual volume level 180 // 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 181 // did not change. In that case, setting the value of the slider won't
172 // trigger an update. So explicitly trigger an update. 182 // trigger an update. So explicitly trigger an update.
173 Update(); 183 Update();
174 slider_->set_enable_accessibility_events(true); 184 slider_->set_enable_accessibility_events(true);
175 } 185 }
176 186
177 void VolumeView::UpdateDeviceTypeAndMore() { 187 void VolumeView::UpdateDeviceTypeAndMore() {
178 bool show_more = is_default_view_ && audio_delegate_->HasAlternativeSources(); 188 CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
189 bool show_more =
190 is_default_view_ && (audio_handler->has_alternative_output() ||
191 audio_handler->has_alternative_input());
179 192
180 if (!show_more) 193 if (!show_more)
181 return; 194 return;
182 195
183 const gfx::VectorIcon& device_icon = 196 const gfx::VectorIcon& device_icon = GetActiveOutputDeviceVectorIcon();
184 audio_delegate_->GetActiveOutputDeviceVectorIcon();
185 const bool target_visibility = !device_icon.is_empty(); 197 const bool target_visibility = !device_icon.is_empty();
186 if (target_visibility) 198 if (target_visibility)
187 device_type_->SetImage(gfx::CreateVectorIcon(device_icon, kMenuIconColor)); 199 device_type_->SetImage(gfx::CreateVectorIcon(device_icon, kMenuIconColor));
188 if (device_type_->visible() != target_visibility) { 200 if (device_type_->visible() != target_visibility) {
189 device_type_->SetVisible(target_visibility); 201 device_type_->SetVisible(target_visibility);
190 device_type_->InvalidateLayout(); 202 device_type_->InvalidateLayout();
191 } 203 }
192 } 204 }
193 205
194 void VolumeView::HandleVolumeUp(int level) { 206 void VolumeView::HandleVolumeUp(int level) {
195 audio_delegate_->SetOutputVolumeLevel(level); 207 CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
196 if (audio_delegate_->IsOutputAudioMuted() && 208 audio_handler->SetOutputVolumePercent(level);
197 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { 209 if (audio_handler->IsOutputMuted() &&
198 audio_delegate_->SetOutputAudioIsMuted(false); 210 level > audio_handler->GetOutputDefaultVolumeMuteThreshold()) {
211 audio_handler->SetOutputMute(false);
199 } 212 }
200 } 213 }
201 214
202 void VolumeView::HandleVolumeDown(int level) { 215 void VolumeView::HandleVolumeDown(int level) {
203 audio_delegate_->SetOutputVolumeLevel(level); 216 CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
204 if (!audio_delegate_->IsOutputAudioMuted() && 217 audio_handler->SetOutputVolumePercent(level);
205 level <= audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { 218 if (!audio_handler->IsOutputMuted() &&
206 audio_delegate_->SetOutputAudioIsMuted(true); 219 level <= audio_handler->GetOutputDefaultVolumeMuteThreshold()) {
207 } else if (audio_delegate_->IsOutputAudioMuted() && 220 audio_handler->SetOutputMute(true);
208 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { 221 } else if (audio_handler->IsOutputMuted() &&
209 audio_delegate_->SetOutputAudioIsMuted(false); 222 level > audio_handler->GetOutputDefaultVolumeMuteThreshold()) {
223 audio_handler->SetOutputMute(false);
210 } 224 }
211 } 225 }
212 226
213 void VolumeView::ButtonPressed(views::Button* sender, const ui::Event& event) { 227 void VolumeView::ButtonPressed(views::Button* sender, const ui::Event& event) {
214 if (sender == icon_) { 228 if (sender == icon_) {
215 bool mute_on = !audio_delegate_->IsOutputAudioMuted(); 229 CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
216 audio_delegate_->SetOutputAudioIsMuted(mute_on); 230 bool mute_on = !audio_handler->IsOutputMuted();
231 audio_handler->SetOutputMute(mute_on);
217 if (!mute_on) 232 if (!mute_on)
218 audio_delegate_->AdjustOutputVolumeToAudibleLevel(); 233 audio_handler->AdjustOutputVolumeToAudibleLevel();
219 icon_->Update(); 234 icon_->Update();
220 } else if (sender == more_button_) { 235 } else if (sender == more_button_) {
221 owner_->TransitionDetailedView(); 236 owner_->TransitionDetailedView();
222 } else { 237 } else {
223 NOTREACHED() << "Unexpected sender=" << sender->GetClassName() << "."; 238 NOTREACHED() << "Unexpected sender=" << sender->GetClassName() << ".";
224 } 239 }
225 } 240 }
226 241
227 void VolumeView::SliderValueChanged(views::Slider* sender, 242 void VolumeView::SliderValueChanged(views::Slider* sender,
228 float value, 243 float value,
229 float old_value, 244 float old_value,
230 views::SliderChangeReason reason) { 245 views::SliderChangeReason reason) {
231 if (reason == views::VALUE_CHANGED_BY_USER) { 246 if (reason == views::VALUE_CHANGED_BY_USER) {
232 int new_volume = static_cast<int>(value * 100); 247 int new_volume = static_cast<int>(value * 100);
233 int current_volume = audio_delegate_->GetOutputVolumeLevel(); 248 int current_volume = CrasAudioHandler::Get()->GetOutputVolumePercent();
234 if (new_volume == current_volume) 249 if (new_volume == current_volume)
235 return; 250 return;
236 WmShell::Get()->RecordUserMetricsAction( 251 WmShell::Get()->RecordUserMetricsAction(
237 is_default_view_ ? UMA_STATUS_AREA_CHANGED_VOLUME_MENU 252 is_default_view_ ? UMA_STATUS_AREA_CHANGED_VOLUME_MENU
238 : UMA_STATUS_AREA_CHANGED_VOLUME_POPUP); 253 : UMA_STATUS_AREA_CHANGED_VOLUME_POPUP);
239 if (new_volume > current_volume) 254 if (new_volume > current_volume)
240 HandleVolumeUp(new_volume); 255 HandleVolumeUp(new_volume);
241 else 256 else
242 HandleVolumeDown(new_volume); 257 HandleVolumeDown(new_volume);
243 } 258 }
244 icon_->Update(); 259 icon_->Update();
245 } 260 }
246 261
247 } // namespace tray 262 } // namespace tray
248 } // namespace ash 263 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/chromeos/audio/volume_view.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698