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

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

Issue 2732813002: chromeos: Move files in //ash/common to //ash, part 1 (Closed)
Patch Set: rebase 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
OLDNEW
(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/chromeos/audio/volume_view.h"
6
7 #include <algorithm>
8
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"
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/system/tray/tray_popup_utils.h"
16 #include "ash/common/system/tray/tri_view.h"
17 #include "ash/common/wm_shell.h"
18 #include "ash/resources/vector_icons/vector_icons.h"
19 #include "ash/strings/grit/ash_strings.h"
20 #include "ui/accessibility/ax_node_data.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/gfx/paint_vector_icon.h"
23 #include "ui/gfx/vector_icon_types.h"
24 #include "ui/views/background.h"
25 #include "ui/views/border.h"
26 #include "ui/views/controls/button/custom_button.h"
27 #include "ui/views/controls/image_view.h"
28 #include "ui/views/controls/slider.h"
29 #include "ui/views/layout/fill_layout.h"
30
31 namespace {
32
33 const gfx::VectorIcon* const kVolumeLevelIcons[] = {
34 &ash::kSystemMenuVolumeMuteIcon, // Muted.
35 &ash::kSystemMenuVolumeLowIcon, // Low volume.
36 &ash::kSystemMenuVolumeMediumIcon, // Medium volume.
37 &ash::kSystemMenuVolumeHighIcon, // High volume.
38 &ash::kSystemMenuVolumeHighIcon, // Full volume.
39 };
40
41 } // namespace
42
43 namespace ash {
44 namespace tray {
45
46 class VolumeButton : public ButtonListenerActionableView {
47 public:
48 VolumeButton(SystemTrayItem* owner,
49 views::ButtonListener* listener,
50 system::TrayAudioDelegate* audio_delegate)
51 : ButtonListenerActionableView(owner,
52 TrayPopupInkDropStyle::HOST_CENTERED,
53 listener),
54 audio_delegate_(audio_delegate),
55 image_(TrayPopupUtils::CreateMainImageView()),
56 image_index_(-1) {
57 TrayPopupUtils::ConfigureContainer(TriView::Container::START, this);
58 AddChildView(image_);
59 SetInkDropMode(InkDropMode::ON);
60 Update();
61
62 set_notify_enter_exit_on_child(true);
63 }
64
65 ~VolumeButton() override {}
66
67 void Update() {
68 float level =
69 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f;
70 int volume_levels = arraysize(kVolumeLevelIcons) - 1;
71 int image_index =
72 audio_delegate_->IsOutputAudioMuted()
73 ? 0
74 : (level == 1.0 ? volume_levels
75 : std::max(1, static_cast<int>(std::ceil(
76 level * (volume_levels - 1)))));
77 gfx::ImageSkia image_skia =
78 gfx::CreateVectorIcon(*kVolumeLevelIcons[image_index], kMenuIconColor);
79 image_->SetImage(&image_skia);
80 image_index_ = image_index;
81 }
82
83 private:
84 // views::View:
85 void GetAccessibleNodeData(ui::AXNodeData* node_data) override {
86 node_data->SetName(
87 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VOLUME_MUTE));
88 node_data->role = ui::AX_ROLE_TOGGLE_BUTTON;
89 if (audio_delegate_->IsOutputAudioMuted())
90 node_data->AddStateFlag(ui::AX_STATE_PRESSED);
91 }
92
93 system::TrayAudioDelegate* audio_delegate_;
94 views::ImageView* image_;
95 int image_index_;
96
97 DISALLOW_COPY_AND_ASSIGN(VolumeButton);
98 };
99
100 VolumeView::VolumeView(SystemTrayItem* owner,
101 system::TrayAudioDelegate* audio_delegate,
102 bool is_default_view)
103 : owner_(owner),
104 tri_view_(TrayPopupUtils::CreateMultiTargetRowView()),
105 audio_delegate_(audio_delegate),
106 more_button_(nullptr),
107 icon_(nullptr),
108 slider_(nullptr),
109 device_type_(nullptr),
110 is_default_view_(is_default_view) {
111 SetLayoutManager(new views::FillLayout);
112 AddChildView(tri_view_);
113
114 icon_ = new VolumeButton(owner, this, audio_delegate_);
115 tri_view_->AddView(TriView::Container::START, icon_);
116
117 slider_ = TrayPopupUtils::CreateSlider(this);
118 slider_->SetValue(
119 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f);
120 slider_->SetAccessibleName(
121 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VOLUME));
122 tri_view_->AddView(TriView::Container::CENTER, slider_);
123
124 set_background(views::Background::CreateSolidBackground(kBackgroundColor));
125
126 if (!is_default_view_) {
127 tri_view_->SetContainerVisible(TriView::Container::END, false);
128 Update();
129 return;
130 }
131
132 more_button_ = new ButtonListenerActionableView(
133 owner_, TrayPopupInkDropStyle::INSET_BOUNDS, this);
134 TrayPopupUtils::ConfigureContainer(TriView::Container::END, more_button_);
135
136 more_button_->SetInkDropMode(views::InkDropHostView::InkDropMode::ON);
137 more_button_->SetBorder(views::CreateEmptyBorder(gfx::Insets(
138 0, kTrayPopupButtonEndMargin)));
139 tri_view_->AddView(TriView::Container::END, more_button_);
140
141 device_type_ = TrayPopupUtils::CreateMoreImageView();
142 device_type_->SetVisible(false);
143 more_button_->AddChildView(device_type_);
144
145 more_button_->AddChildView(TrayPopupUtils::CreateMoreImageView());
146
147 Update();
148 }
149
150 VolumeView::~VolumeView() {}
151
152 void VolumeView::Update() {
153 icon_->Update();
154 slider_->UpdateState(!audio_delegate_->IsOutputAudioMuted());
155 UpdateDeviceTypeAndMore();
156 Layout();
157 }
158
159 void VolumeView::SetVolumeLevel(float percent) {
160 // Update volume level to the current audio level.
161 Update();
162
163 // 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
165 // on audio side. To avoid the jittering in slider UI, do not set change
166 // slider value if the change is less than 1%.
167 if (std::abs(percent - slider_->value()) < 0.01)
168 return;
169 slider_->SetValue(percent);
170 // 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
172 // trigger an update. So explicitly trigger an update.
173 Update();
174 slider_->set_enable_accessibility_events(true);
175 }
176
177 void VolumeView::UpdateDeviceTypeAndMore() {
178 bool show_more = is_default_view_ && audio_delegate_->HasAlternativeSources();
179
180 if (!show_more)
181 return;
182
183 const gfx::VectorIcon& device_icon =
184 audio_delegate_->GetActiveOutputDeviceVectorIcon();
185 const bool target_visibility = !device_icon.is_empty();
186 if (target_visibility)
187 device_type_->SetImage(gfx::CreateVectorIcon(device_icon, kMenuIconColor));
188 if (device_type_->visible() != target_visibility) {
189 device_type_->SetVisible(target_visibility);
190 device_type_->InvalidateLayout();
191 }
192 }
193
194 void VolumeView::HandleVolumeUp(int level) {
195 audio_delegate_->SetOutputVolumeLevel(level);
196 if (audio_delegate_->IsOutputAudioMuted() &&
197 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) {
198 audio_delegate_->SetOutputAudioIsMuted(false);
199 }
200 }
201
202 void VolumeView::HandleVolumeDown(int level) {
203 audio_delegate_->SetOutputVolumeLevel(level);
204 if (!audio_delegate_->IsOutputAudioMuted() &&
205 level <= audio_delegate_->GetOutputDefaultVolumeMuteLevel()) {
206 audio_delegate_->SetOutputAudioIsMuted(true);
207 } else if (audio_delegate_->IsOutputAudioMuted() &&
208 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) {
209 audio_delegate_->SetOutputAudioIsMuted(false);
210 }
211 }
212
213 void VolumeView::ButtonPressed(views::Button* sender, const ui::Event& event) {
214 if (sender == icon_) {
215 bool mute_on = !audio_delegate_->IsOutputAudioMuted();
216 audio_delegate_->SetOutputAudioIsMuted(mute_on);
217 if (!mute_on)
218 audio_delegate_->AdjustOutputVolumeToAudibleLevel();
219 icon_->Update();
220 } else if (sender == more_button_) {
221 owner_->TransitionDetailedView();
222 } else {
223 NOTREACHED() << "Unexpected sender=" << sender->GetClassName() << ".";
224 }
225 }
226
227 void VolumeView::SliderValueChanged(views::Slider* sender,
228 float value,
229 float old_value,
230 views::SliderChangeReason reason) {
231 if (reason == views::VALUE_CHANGED_BY_USER) {
232 int new_volume = static_cast<int>(value * 100);
233 int current_volume = audio_delegate_->GetOutputVolumeLevel();
234 if (new_volume == current_volume)
235 return;
236 WmShell::Get()->RecordUserMetricsAction(
237 is_default_view_ ? UMA_STATUS_AREA_CHANGED_VOLUME_MENU
238 : UMA_STATUS_AREA_CHANGED_VOLUME_POPUP);
239 if (new_volume > current_volume)
240 HandleVolumeUp(new_volume);
241 else
242 HandleVolumeDown(new_volume);
243 }
244 icon_->Update();
245 }
246
247 } // namespace tray
248 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698