| OLD | NEW |
| (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/system/audio/tray_audio.h" | |
| 6 | |
| 7 #include <cmath> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "ash/common/ash_constants.h" | |
| 11 #include "ash/common/system/tray/actionable_view.h" | |
| 12 #include "ash/common/system/tray/fixed_sized_scroll_view.h" | |
| 13 #include "ash/common/system/tray/hover_highlight_view.h" | |
| 14 #include "ash/common/system/tray/system_tray_delegate.h" | |
| 15 #include "ash/common/system/tray/tray_constants.h" | |
| 16 #include "ash/common/system/volume_control_delegate.h" | |
| 17 #include "ash/display/display_manager.h" | |
| 18 #include "ash/metrics/user_metrics_recorder.h" | |
| 19 #include "ash/shell.h" | |
| 20 #include "ash/system/audio/tray_audio_delegate.h" | |
| 21 #include "ash/system/audio/volume_view.h" | |
| 22 #include "ash/system/tray/system_tray.h" | |
| 23 #include "ash/system/tray/system_tray_notifier.h" | |
| 24 #include "base/strings/utf_string_conversions.h" | |
| 25 #include "grit/ash_resources.h" | |
| 26 #include "third_party/skia/include/core/SkCanvas.h" | |
| 27 #include "third_party/skia/include/core/SkPaint.h" | |
| 28 #include "third_party/skia/include/core/SkRect.h" | |
| 29 #include "third_party/skia/include/effects/SkGradientShader.h" | |
| 30 #include "ui/display/display.h" | |
| 31 #include "ui/gfx/canvas.h" | |
| 32 #include "ui/gfx/font_list.h" | |
| 33 #include "ui/gfx/image/image.h" | |
| 34 #include "ui/gfx/image/image_skia_operations.h" | |
| 35 #include "ui/views/controls/button/image_button.h" | |
| 36 #include "ui/views/controls/image_view.h" | |
| 37 #include "ui/views/controls/label.h" | |
| 38 #include "ui/views/controls/slider.h" | |
| 39 #include "ui/views/layout/box_layout.h" | |
| 40 #include "ui/views/view.h" | |
| 41 | |
| 42 namespace ash { | |
| 43 | |
| 44 TrayAudio::TrayAudio(SystemTray* system_tray, | |
| 45 std::unique_ptr<system::TrayAudioDelegate> audio_delegate) | |
| 46 : TrayImageItem(system_tray, IDR_AURA_UBER_TRAY_VOLUME_MUTE), | |
| 47 audio_delegate_(std::move(audio_delegate)), | |
| 48 volume_view_(NULL), | |
| 49 pop_up_volume_view_(false) { | |
| 50 Shell::GetInstance()->system_tray_notifier()->AddAudioObserver(this); | |
| 51 display::Screen::GetScreen()->AddObserver(this); | |
| 52 } | |
| 53 | |
| 54 TrayAudio::~TrayAudio() { | |
| 55 display::Screen::GetScreen()->RemoveObserver(this); | |
| 56 Shell::GetInstance()->system_tray_notifier()->RemoveAudioObserver(this); | |
| 57 } | |
| 58 | |
| 59 // static | |
| 60 bool TrayAudio::ShowAudioDeviceMenu() { | |
| 61 #if defined(OS_CHROMEOS) | |
| 62 return true; | |
| 63 #else | |
| 64 return false; | |
| 65 #endif | |
| 66 } | |
| 67 | |
| 68 bool TrayAudio::GetInitialVisibility() { | |
| 69 return audio_delegate_->IsOutputAudioMuted(); | |
| 70 } | |
| 71 | |
| 72 views::View* TrayAudio::CreateDefaultView(LoginStatus status) { | |
| 73 volume_view_ = new tray::VolumeView(this, audio_delegate_.get(), true); | |
| 74 return volume_view_; | |
| 75 } | |
| 76 | |
| 77 views::View* TrayAudio::CreateDetailedView(LoginStatus status) { | |
| 78 volume_view_ = new tray::VolumeView(this, audio_delegate_.get(), false); | |
| 79 return volume_view_; | |
| 80 } | |
| 81 | |
| 82 void TrayAudio::DestroyDefaultView() { | |
| 83 volume_view_ = NULL; | |
| 84 } | |
| 85 | |
| 86 void TrayAudio::DestroyDetailedView() { | |
| 87 if (volume_view_) { | |
| 88 volume_view_ = NULL; | |
| 89 pop_up_volume_view_ = false; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 bool TrayAudio::ShouldHideArrow() const { | |
| 94 return true; | |
| 95 } | |
| 96 | |
| 97 bool TrayAudio::ShouldShowShelf() const { | |
| 98 return TrayAudio::ShowAudioDeviceMenu() && !pop_up_volume_view_; | |
| 99 } | |
| 100 | |
| 101 void TrayAudio::OnOutputNodeVolumeChanged(uint64_t /* node_id */, | |
| 102 double /* volume */) { | |
| 103 float percent = | |
| 104 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f; | |
| 105 if (tray_view()) | |
| 106 tray_view()->SetVisible(GetInitialVisibility()); | |
| 107 | |
| 108 if (volume_view_) { | |
| 109 volume_view_->SetVolumeLevel(percent); | |
| 110 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds); | |
| 111 return; | |
| 112 } | |
| 113 pop_up_volume_view_ = true; | |
| 114 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false); | |
| 115 } | |
| 116 | |
| 117 void TrayAudio::OnOutputMuteChanged(bool /* mute_on */, bool system_adjust) { | |
| 118 if (tray_view()) | |
| 119 tray_view()->SetVisible(GetInitialVisibility()); | |
| 120 | |
| 121 if (volume_view_) { | |
| 122 volume_view_->Update(); | |
| 123 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds); | |
| 124 } else if (!system_adjust) { | |
| 125 pop_up_volume_view_ = true; | |
| 126 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 void TrayAudio::OnAudioNodesChanged() { | |
| 131 Update(); | |
| 132 } | |
| 133 | |
| 134 void TrayAudio::OnActiveOutputNodeChanged() { | |
| 135 Update(); | |
| 136 } | |
| 137 | |
| 138 void TrayAudio::OnActiveInputNodeChanged() { | |
| 139 Update(); | |
| 140 } | |
| 141 | |
| 142 void TrayAudio::ChangeInternalSpeakerChannelMode() { | |
| 143 // Swap left/right channel only if it is in Yoga mode. | |
| 144 system::TrayAudioDelegate::AudioChannelMode channel_mode = | |
| 145 system::TrayAudioDelegate::NORMAL; | |
| 146 if (display::Display::HasInternalDisplay()) { | |
| 147 const DisplayInfo& display_info = | |
| 148 Shell::GetInstance()->display_manager()->GetDisplayInfo( | |
| 149 display::Display::InternalDisplayId()); | |
| 150 if (display_info.GetActiveRotation() == display::Display::ROTATE_180) | |
| 151 channel_mode = system::TrayAudioDelegate::LEFT_RIGHT_SWAPPED; | |
| 152 } | |
| 153 | |
| 154 audio_delegate_->SetInternalSpeakerChannelMode(channel_mode); | |
| 155 } | |
| 156 | |
| 157 void TrayAudio::OnDisplayAdded(const display::Display& new_display) { | |
| 158 if (!new_display.IsInternal()) | |
| 159 return; | |
| 160 ChangeInternalSpeakerChannelMode(); | |
| 161 } | |
| 162 | |
| 163 void TrayAudio::OnDisplayRemoved(const display::Display& old_display) { | |
| 164 if (!old_display.IsInternal()) | |
| 165 return; | |
| 166 ChangeInternalSpeakerChannelMode(); | |
| 167 } | |
| 168 | |
| 169 void TrayAudio::OnDisplayMetricsChanged(const display::Display& display, | |
| 170 uint32_t changed_metrics) { | |
| 171 if (!display.IsInternal()) | |
| 172 return; | |
| 173 | |
| 174 if (changed_metrics & display::DisplayObserver::DISPLAY_METRIC_ROTATION) | |
| 175 ChangeInternalSpeakerChannelMode(); | |
| 176 } | |
| 177 | |
| 178 void TrayAudio::Update() { | |
| 179 if (tray_view()) | |
| 180 tray_view()->SetVisible(GetInitialVisibility()); | |
| 181 if (volume_view_) { | |
| 182 volume_view_->SetVolumeLevel( | |
| 183 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f); | |
| 184 volume_view_->Update(); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 } // namespace ash | |
| OLD | NEW |