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
39namespace ash {
40
41TrayAudio::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
50TrayAudio::~TrayAudio() {
51  Shell::GetInstance()->system_tray_notifier()->RemoveAudioObserver(this);
52}
53
54// static
55bool TrayAudio::ShowAudioDeviceMenu() {
56#if defined(OS_CHROMEOS)
57  return true;
58#else
59  return false;
60#endif
61}
62
63bool TrayAudio::GetInitialVisibility() {
64  return audio_delegate_->IsOutputAudioMuted();
65}
66
67views::View* TrayAudio::CreateDefaultView(user::LoginStatus status) {
68  volume_view_ = new tray::VolumeView(this, audio_delegate_.get(), true);
69  return volume_view_;
70}
71
72views::View* TrayAudio::CreateDetailedView(user::LoginStatus status) {
73  volume_view_ = new tray::VolumeView(this, audio_delegate_.get(), false);
74  return volume_view_;
75}
76
77void TrayAudio::DestroyDefaultView() {
78  volume_view_ = NULL;
79}
80
81void TrayAudio::DestroyDetailedView() {
82  if (volume_view_) {
83    volume_view_ = NULL;
84    pop_up_volume_view_ = false;
85  }
86}
87
88bool TrayAudio::ShouldHideArrow() const {
89  return true;
90}
91
92bool TrayAudio::ShouldShowShelf() const {
93  return TrayAudio::ShowAudioDeviceMenu() && !pop_up_volume_view_;
94}
95
96void 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
111void 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
124void TrayAudio::OnAudioNodesChanged() {
125  Update();
126}
127
128void TrayAudio::OnActiveOutputNodeChanged() {
129  Update();
130}
131
132void TrayAudio::OnActiveInputNodeChanged() {
133  Update();
134}
135
136void 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