Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / ash / system / audio / tray_audio.cc
blob21c2163c4d5606e2b2d098259490b23748710ede
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.
5 #include "ash/system/audio/tray_audio.h"
7 #include <cmath>
9 #include "ash/ash_constants.h"
10 #include "ash/display/display_manager.h"
11 #include "ash/metrics/user_metrics_recorder.h"
12 #include "ash/shell.h"
13 #include "ash/system/audio/tray_audio_delegate.h"
14 #include "ash/system/audio/volume_view.h"
15 #include "ash/system/tray/actionable_view.h"
16 #include "ash/system/tray/fixed_sized_scroll_view.h"
17 #include "ash/system/tray/hover_highlight_view.h"
18 #include "ash/system/tray/system_tray.h"
19 #include "ash/system/tray/system_tray_delegate.h"
20 #include "ash/system/tray/system_tray_notifier.h"
21 #include "ash/system/tray/tray_constants.h"
22 #include "ash/volume_control_delegate.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "grit/ash_resources.h"
25 #include "third_party/skia/include/core/SkCanvas.h"
26 #include "third_party/skia/include/core/SkPaint.h"
27 #include "third_party/skia/include/core/SkRect.h"
28 #include "third_party/skia/include/effects/SkGradientShader.h"
29 #include "ui/gfx/canvas.h"
30 #include "ui/gfx/display.h"
31 #include "ui/gfx/font_list.h"
32 #include "ui/gfx/image/image.h"
33 #include "ui/gfx/image/image_skia_operations.h"
34 #include "ui/views/controls/button/image_button.h"
35 #include "ui/views/controls/image_view.h"
36 #include "ui/views/controls/label.h"
37 #include "ui/views/controls/slider.h"
38 #include "ui/views/layout/box_layout.h"
39 #include "ui/views/view.h"
41 namespace ash {
43 TrayAudio::TrayAudio(SystemTray* system_tray,
44 scoped_ptr<system::TrayAudioDelegate> audio_delegate)
45 : TrayImageItem(system_tray, IDR_AURA_UBER_TRAY_VOLUME_MUTE),
46 audio_delegate_(audio_delegate.Pass()),
47 volume_view_(NULL),
48 pop_up_volume_view_(false) {
49 Shell::GetInstance()->system_tray_notifier()->AddAudioObserver(this);
50 Shell::GetScreen()->AddObserver(this);
53 TrayAudio::~TrayAudio() {
54 Shell::GetScreen()->RemoveObserver(this);
55 Shell::GetInstance()->system_tray_notifier()->RemoveAudioObserver(this);
58 // static
59 bool TrayAudio::ShowAudioDeviceMenu() {
60 #if defined(OS_CHROMEOS)
61 return true;
62 #else
63 return false;
64 #endif
67 bool TrayAudio::GetInitialVisibility() {
68 return audio_delegate_->IsOutputAudioMuted();
71 views::View* TrayAudio::CreateDefaultView(user::LoginStatus status) {
72 volume_view_ = new tray::VolumeView(this, audio_delegate_.get(), true);
73 return volume_view_;
76 views::View* TrayAudio::CreateDetailedView(user::LoginStatus status) {
77 volume_view_ = new tray::VolumeView(this, audio_delegate_.get(), false);
78 return volume_view_;
81 void TrayAudio::DestroyDefaultView() {
82 volume_view_ = NULL;
85 void TrayAudio::DestroyDetailedView() {
86 if (volume_view_) {
87 volume_view_ = NULL;
88 pop_up_volume_view_ = false;
92 bool TrayAudio::ShouldHideArrow() const {
93 return true;
96 bool TrayAudio::ShouldShowShelf() const {
97 return TrayAudio::ShowAudioDeviceMenu() && !pop_up_volume_view_;
100 void TrayAudio::OnOutputNodeVolumeChanged(uint64_t /* node_id */,
101 double /* volume */) {
102 float percent =
103 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f;
104 if (tray_view())
105 tray_view()->SetVisible(GetInitialVisibility());
107 if (volume_view_) {
108 volume_view_->SetVolumeLevel(percent);
109 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds);
110 return;
112 pop_up_volume_view_ = true;
113 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false);
116 void TrayAudio::OnOutputMuteChanged(bool /* mute_on */) {
117 if (tray_view())
118 tray_view()->SetVisible(GetInitialVisibility());
120 if (volume_view_) {
121 volume_view_->Update();
122 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds);
123 } else {
124 pop_up_volume_view_ = true;
125 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false);
129 void TrayAudio::OnAudioNodesChanged() {
130 Update();
133 void TrayAudio::OnActiveOutputNodeChanged() {
134 Update();
137 void TrayAudio::OnActiveInputNodeChanged() {
138 Update();
141 void TrayAudio::OnDisplayAdded(const gfx::Display& new_display) {
142 if (new_display.id() != gfx::Display::InternalDisplayId())
143 return;
144 ChangeInternalSpeakerChannelMode();
147 void TrayAudio::OnDisplayRemoved(const gfx::Display& old_display) {
148 if (old_display.id() != gfx::Display::InternalDisplayId())
149 return;
150 ChangeInternalSpeakerChannelMode();
153 void TrayAudio::OnDisplayMetricsChanged(const gfx::Display& display,
154 uint32_t changed_metrics) {
155 if (display.id() != gfx::Display::InternalDisplayId())
156 return;
158 if (changed_metrics & gfx::DisplayObserver::DISPLAY_METRIC_ROTATION)
159 ChangeInternalSpeakerChannelMode();
162 void TrayAudio::ChangeInternalSpeakerChannelMode() {
163 // Swap left/right channel only if it is in Yoga mode.
164 system::TrayAudioDelegate::AudioChannelMode channel_mode =
165 system::TrayAudioDelegate::NORMAL;
166 if (gfx::Display::InternalDisplayId() != gfx::Display::kInvalidDisplayID) {
167 const DisplayInfo& display_info =
168 Shell::GetInstance()->display_manager()->GetDisplayInfo(
169 gfx::Display::InternalDisplayId());
170 if (display_info.rotation() == gfx::Display::ROTATE_180)
171 channel_mode = system::TrayAudioDelegate::LEFT_RIGHT_SWAPPED;
174 audio_delegate_->SetInternalSpeakerChannelMode(channel_mode);
177 void TrayAudio::Update() {
178 if (tray_view())
179 tray_view()->SetVisible(GetInitialVisibility());
180 if (volume_view_) {
181 volume_view_->SetVolumeLevel(
182 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f);
183 volume_view_->Update();
187 } // namespace ash