Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ash / system / audio / tray_audio.cc
blob634463e525d89897b194d091c6d779acd58ec1c5
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/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 "grit/ash_strings.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/font_list.h"
31 #include "ui/gfx/image/image.h"
32 #include "ui/gfx/image/image_skia_operations.h"
33 #include "ui/views/controls/button/image_button.h"
34 #include "ui/views/controls/image_view.h"
35 #include "ui/views/controls/label.h"
36 #include "ui/views/controls/slider.h"
37 #include "ui/views/layout/box_layout.h"
38 #include "ui/views/view.h"
40 namespace ash {
42 TrayAudio::TrayAudio(SystemTray* system_tray,
43 scoped_ptr<system::TrayAudioDelegate> audio_delegate)
44 : TrayImageItem(system_tray, IDR_AURA_UBER_TRAY_VOLUME_MUTE),
45 audio_delegate_(audio_delegate.Pass()),
46 volume_view_(NULL),
47 pop_up_volume_view_(false) {
48 Shell::GetInstance()->system_tray_notifier()->AddAudioObserver(this);
51 TrayAudio::~TrayAudio() {
52 Shell::GetInstance()->system_tray_notifier()->RemoveAudioObserver(this);
55 // static
56 bool TrayAudio::ShowAudioDeviceMenu() {
57 #if defined(OS_CHROMEOS)
58 return true;
59 #else
60 return false;
61 #endif
64 bool TrayAudio::GetInitialVisibility() {
65 return audio_delegate_->IsOutputAudioMuted();
68 views::View* TrayAudio::CreateDefaultView(user::LoginStatus status) {
69 volume_view_ = new tray::VolumeView(this, audio_delegate_.get(), true);
70 return volume_view_;
73 views::View* TrayAudio::CreateDetailedView(user::LoginStatus status) {
74 volume_view_ = new tray::VolumeView(this, audio_delegate_.get(), false);
75 return volume_view_;
78 void TrayAudio::DestroyDefaultView() {
79 volume_view_ = NULL;
82 void TrayAudio::DestroyDetailedView() {
83 if (volume_view_) {
84 volume_view_ = NULL;
85 pop_up_volume_view_ = false;
89 bool TrayAudio::ShouldHideArrow() const {
90 return true;
93 bool TrayAudio::ShouldShowShelf() const {
94 return TrayAudio::ShowAudioDeviceMenu() && !pop_up_volume_view_;
97 void TrayAudio::OnOutputVolumeChanged() {
98 float percent =
99 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f;
100 if (tray_view())
101 tray_view()->SetVisible(GetInitialVisibility());
103 if (volume_view_) {
104 volume_view_->SetVolumeLevel(percent);
105 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds);
106 return;
108 pop_up_volume_view_ = true;
109 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false);
112 void TrayAudio::OnOutputMuteChanged() {
113 if (tray_view())
114 tray_view()->SetVisible(GetInitialVisibility());
116 if (volume_view_) {
117 volume_view_->Update();
118 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds);
119 } else {
120 pop_up_volume_view_ = true;
121 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false);
125 void TrayAudio::OnAudioNodesChanged() {
126 Update();
129 void TrayAudio::OnActiveOutputNodeChanged() {
130 Update();
133 void TrayAudio::OnActiveInputNodeChanged() {
134 Update();
137 void TrayAudio::Update() {
138 if (tray_view())
139 tray_view()->SetVisible(GetInitialVisibility());
140 if (volume_view_) {
141 volume_view_->SetVolumeLevel(
142 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f);
143 volume_view_->Update();
147 } // namespace ash