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/system/audio/tray_audio.h"
6
7 #include <cmath>
8
9 #include "ash/ash_constants.h"
10 #include "ash/metrics/user_metrics_recorder.h"
11 #include "ash/shell.h"
12 #include "ash/system/audio/tray_audio_delegate.h"
13 #include "ash/system/audio/volume_view.h"
14 #include "ash/system/tray/actionable_view.h"
15 #include "ash/system/tray/fixed_sized_scroll_view.h"
16 #include "ash/system/tray/hover_highlight_view.h"
17 #include "ash/system/tray/system_tray.h"
18 #include "ash/system/tray/system_tray_delegate.h"
19 #include "ash/system/tray/system_tray_notifier.h"
20 #include "ash/system/tray/tray_constants.h"
21 #include "ash/volume_control_delegate.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "grit/ash_resources.h"
24 #include "third_party/skia/include/core/SkCanvas.h"
25 #include "third_party/skia/include/core/SkPaint.h"
26 #include "third_party/skia/include/core/SkRect.h"
27 #include "third_party/skia/include/effects/SkGradientShader.h"
28 #include "ui/gfx/canvas.h"
29 #include "ui/gfx/font_list.h"
30 #include "ui/gfx/image/image.h"
31 #include "ui/gfx/image/image_skia_operations.h"
32 #include "ui/views/controls/button/image_button.h"
33 #include "ui/views/controls/image_view.h"
34 #include "ui/views/controls/label.h"
35 #include "ui/views/controls/slider.h"
36 #include "ui/views/layout/box_layout.h"
37 #include "ui/views/view.h"
38
39 namespace ash {
40
TrayAudio(SystemTray * system_tray,scoped_ptr<system::TrayAudioDelegate> audio_delegate)41 TrayAudio::TrayAudio(SystemTray* system_tray,
42 scoped_ptr<system::TrayAudioDelegate> audio_delegate)
43 : TrayImageItem(system_tray, IDR_AURA_UBER_TRAY_VOLUME_MUTE),
44 audio_delegate_(audio_delegate.Pass()),
45 volume_view_(NULL),
46 pop_up_volume_view_(false) {
47 Shell::GetInstance()->system_tray_notifier()->AddAudioObserver(this);
48 }
49
~TrayAudio()50 TrayAudio::~TrayAudio() {
51 Shell::GetInstance()->system_tray_notifier()->RemoveAudioObserver(this);
52 }
53
54 // static
ShowAudioDeviceMenu()55 bool TrayAudio::ShowAudioDeviceMenu() {
56 #if defined(OS_CHROMEOS)
57 return true;
58 #else
59 return false;
60 #endif
61 }
62
GetInitialVisibility()63 bool TrayAudio::GetInitialVisibility() {
64 return audio_delegate_->IsOutputAudioMuted();
65 }
66
CreateDefaultView(user::LoginStatus status)67 views::View* TrayAudio::CreateDefaultView(user::LoginStatus status) {
68 volume_view_ = new tray::VolumeView(this, audio_delegate_.get(), true);
69 return volume_view_;
70 }
71
CreateDetailedView(user::LoginStatus status)72 views::View* TrayAudio::CreateDetailedView(user::LoginStatus status) {
73 volume_view_ = new tray::VolumeView(this, audio_delegate_.get(), false);
74 return volume_view_;
75 }
76
DestroyDefaultView()77 void TrayAudio::DestroyDefaultView() {
78 volume_view_ = NULL;
79 }
80
DestroyDetailedView()81 void TrayAudio::DestroyDetailedView() {
82 if (volume_view_) {
83 volume_view_ = NULL;
84 pop_up_volume_view_ = false;
85 }
86 }
87
ShouldHideArrow() const88 bool TrayAudio::ShouldHideArrow() const {
89 return true;
90 }
91
ShouldShowShelf() const92 bool TrayAudio::ShouldShowShelf() const {
93 return TrayAudio::ShowAudioDeviceMenu() && !pop_up_volume_view_;
94 }
95
OnOutputVolumeChanged()96 void TrayAudio::OnOutputVolumeChanged() {
97 float percent =
98 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f;
99 if (tray_view())
100 tray_view()->SetVisible(GetInitialVisibility());
101
102 if (volume_view_) {
103 volume_view_->SetVolumeLevel(percent);
104 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds);
105 return;
106 }
107 pop_up_volume_view_ = true;
108 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false);
109 }
110
OnOutputMuteChanged()111 void TrayAudio::OnOutputMuteChanged() {
112 if (tray_view())
113 tray_view()->SetVisible(GetInitialVisibility());
114
115 if (volume_view_) {
116 volume_view_->Update();
117 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds);
118 } else {
119 pop_up_volume_view_ = true;
120 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false);
121 }
122 }
123
OnAudioNodesChanged()124 void TrayAudio::OnAudioNodesChanged() {
125 Update();
126 }
127
OnActiveOutputNodeChanged()128 void TrayAudio::OnActiveOutputNodeChanged() {
129 Update();
130 }
131
OnActiveInputNodeChanged()132 void TrayAudio::OnActiveInputNodeChanged() {
133 Update();
134 }
135
Update()136 void TrayAudio::Update() {
137 if (tray_view())
138 tray_view()->SetVisible(GetInitialVisibility());
139 if (volume_view_) {
140 volume_view_->SetVolumeLevel(
141 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f);
142 volume_view_->Update();
143 }
144 }
145
146 } // namespace ash
147