| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/system/audio/tray_volume.h" | 5 #include "ash/system/audio/tray_volume.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "ash/accelerators/accelerator_controller.h" |
| 9 #include "ash/shell.h" | 10 #include "ash/shell.h" |
| 10 #include "ash/system/tray/system_tray_delegate.h" | |
| 11 #include "ash/system/tray/tray_constants.h" | 11 #include "ash/system/tray/tray_constants.h" |
| 12 #include "ash/system/tray/tray_views.h" | 12 #include "ash/system/tray/tray_views.h" |
| 13 #include "ash/volume_control_delegate.h" |
| 13 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 14 #include "grit/ash_strings.h" | 15 #include "grit/ash_strings.h" |
| 15 #include "grit/ui_resources.h" | 16 #include "grit/ui_resources.h" |
| 16 #include "third_party/skia/include/core/SkCanvas.h" | 17 #include "third_party/skia/include/core/SkCanvas.h" |
| 17 #include "third_party/skia/include/core/SkPaint.h" | 18 #include "third_party/skia/include/core/SkPaint.h" |
| 18 #include "third_party/skia/include/core/SkRect.h" | 19 #include "third_party/skia/include/core/SkRect.h" |
| 19 #include "third_party/skia/include/effects/SkGradientShader.h" | 20 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 21 #include "ui/gfx/canvas.h" | 22 #include "ui/gfx/canvas.h" |
| 22 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
| 23 #include "ui/gfx/image/image_skia_operations.h" | 24 #include "ui/gfx/image/image_skia_operations.h" |
| 24 #include "ui/views/controls/button/image_button.h" | 25 #include "ui/views/controls/button/image_button.h" |
| 25 #include "ui/views/controls/image_view.h" | 26 #include "ui/views/controls/image_view.h" |
| 26 #include "ui/views/controls/label.h" | 27 #include "ui/views/controls/label.h" |
| 27 #include "ui/views/controls/slider.h" | 28 #include "ui/views/controls/slider.h" |
| 28 #include "ui/views/layout/box_layout.h" | 29 #include "ui/views/layout/box_layout.h" |
| 29 #include "ui/views/view.h" | 30 #include "ui/views/view.h" |
| 30 | 31 |
| 31 namespace ash { | 32 namespace ash { |
| 32 namespace internal { | 33 namespace internal { |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 const int kVolumeImageWidth = 25; | 36 const int kVolumeImageWidth = 25; |
| 36 const int kVolumeImageHeight = 25; | 37 const int kVolumeImageHeight = 25; |
| 37 | 38 |
| 38 // IDR_AURA_UBER_TRAY_VOLUME_LEVELS contains 5 images, | 39 // IDR_AURA_UBER_TRAY_VOLUME_LEVELS contains 5 images, |
| 39 // The one for mute is at the 0 index and the other | 40 // The one for mute is at the 0 index and the other |
| 40 // four are used for ascending volume levels. | 41 // four are used for ascending volume levels. |
| 41 const int kVolumeLevels = 4; | 42 const int kVolumeLevels = 4; |
| 43 |
| 44 bool IsAudioMuted() { |
| 45 ash::VolumeControlDelegate* delegate = |
| 46 Shell::GetInstance()->accelerator_controller()->volume_control_delegate(); |
| 47 return delegate ? delegate->IsAudioMuted() : false; |
| 42 } | 48 } |
| 43 | 49 |
| 50 float GetVolumeLevel() { |
| 51 ash::VolumeControlDelegate* delegate = |
| 52 Shell::GetInstance()->accelerator_controller()->volume_control_delegate(); |
| 53 DCHECK(delegate); |
| 54 return delegate->GetVolumeLevel(); |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 44 namespace tray { | 59 namespace tray { |
| 45 | 60 |
| 46 class VolumeButton : public views::ToggleImageButton { | 61 class VolumeButton : public views::ToggleImageButton { |
| 47 public: | 62 public: |
| 48 explicit VolumeButton(views::ButtonListener* listener) | 63 explicit VolumeButton(views::ButtonListener* listener) |
| 49 : views::ToggleImageButton(listener), | 64 : views::ToggleImageButton(listener), |
| 50 image_index_(-1) { | 65 image_index_(-1) { |
| 51 SetImageAlignment(ALIGN_CENTER, ALIGN_MIDDLE); | 66 SetImageAlignment(ALIGN_CENTER, ALIGN_MIDDLE); |
| 52 image_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 67 image_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 53 IDR_AURA_UBER_TRAY_VOLUME_LEVELS); | 68 IDR_AURA_UBER_TRAY_VOLUME_LEVELS); |
| 54 SetPreferredSize(gfx::Size(kTrayPopupItemHeight, kTrayPopupItemHeight)); | 69 SetPreferredSize(gfx::Size(kTrayPopupItemHeight, kTrayPopupItemHeight)); |
| 55 Update(); | 70 Update(); |
| 56 } | 71 } |
| 57 | 72 |
| 58 virtual ~VolumeButton() {} | 73 virtual ~VolumeButton() {} |
| 59 | 74 |
| 60 void Update() { | 75 void Update() { |
| 61 ash::SystemTrayDelegate* delegate = | 76 float level = GetVolumeLevel(); |
| 62 ash::Shell::GetInstance()->tray_delegate(); | 77 int image_index = IsAudioMuted() ? |
| 63 float level = delegate->GetVolumeLevel(); | |
| 64 int image_index = delegate->IsAudioMuted() ? | |
| 65 0 : (level == 1.0 ? | 78 0 : (level == 1.0 ? |
| 66 kVolumeLevels : | 79 kVolumeLevels : |
| 67 std::max(1, int(std::ceil(level * (kVolumeLevels - 1))))); | 80 std::max(1, int(std::ceil(level * (kVolumeLevels - 1))))); |
| 68 if (image_index != image_index_) { | 81 if (image_index != image_index_) { |
| 69 gfx::Rect region(0, image_index * kVolumeImageHeight, | 82 gfx::Rect region(0, image_index * kVolumeImageHeight, |
| 70 kVolumeImageWidth, kVolumeImageHeight); | 83 kVolumeImageWidth, kVolumeImageHeight); |
| 71 gfx::ImageSkia image_skia = gfx::ImageSkiaOperations::ExtractSubset( | 84 gfx::ImageSkia image_skia = gfx::ImageSkiaOperations::ExtractSubset( |
| 72 *(image_.ToImageSkia()), region); | 85 *(image_.ToImageSkia()), region); |
| 73 SetImage(views::CustomButton::BS_NORMAL, &image_skia); | 86 SetImage(views::CustomButton::BS_NORMAL, &image_skia); |
| 74 image_index_ = image_index; | 87 image_index_ = image_index; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 94 public: | 107 public: |
| 95 explicit MuteButton(views::ButtonListener* listener) | 108 explicit MuteButton(views::ButtonListener* listener) |
| 96 : TrayBarButtonWithTitle(listener, | 109 : TrayBarButtonWithTitle(listener, |
| 97 IDS_ASH_STATUS_TRAY_VOLUME_MUTE, | 110 IDS_ASH_STATUS_TRAY_VOLUME_MUTE, |
| 98 kTrayBarButtonWidth) { | 111 kTrayBarButtonWidth) { |
| 99 Update(); | 112 Update(); |
| 100 } | 113 } |
| 101 virtual ~MuteButton() {} | 114 virtual ~MuteButton() {} |
| 102 | 115 |
| 103 void Update() { | 116 void Update() { |
| 104 ash::SystemTrayDelegate* delegate = | 117 UpdateButton(IsAudioMuted()); |
| 105 ash::Shell::GetInstance()->tray_delegate(); | |
| 106 UpdateButton(delegate->IsAudioMuted()); | |
| 107 SchedulePaint(); | 118 SchedulePaint(); |
| 108 } | 119 } |
| 109 | 120 |
| 110 DISALLOW_COPY_AND_ASSIGN(MuteButton); | 121 DISALLOW_COPY_AND_ASSIGN(MuteButton); |
| 111 }; | 122 }; |
| 112 | 123 |
| 113 class VolumeSlider : public views::Slider { | 124 class VolumeSlider : public views::Slider { |
| 114 public: | 125 public: |
| 115 explicit VolumeSlider(views::SliderListener* listener) | 126 explicit VolumeSlider(views::SliderListener* listener) |
| 116 : views::Slider(listener, views::Slider::HORIZONTAL) { | 127 : views::Slider(listener, views::Slider::HORIZONTAL) { |
| 117 set_focus_border_color(kFocusBorderColor); | 128 set_focus_border_color(kFocusBorderColor); |
| 118 SetValue(ash::Shell::GetInstance()->tray_delegate()->GetVolumeLevel()); | 129 SetValue(GetVolumeLevel()); |
| 119 SetAccessibleName( | 130 SetAccessibleName( |
| 120 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | 131 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| 121 IDS_ASH_STATUS_TRAY_VOLUME)); | 132 IDS_ASH_STATUS_TRAY_VOLUME)); |
| 122 Update(); | 133 Update(); |
| 123 } | 134 } |
| 124 virtual ~VolumeSlider() {} | 135 virtual ~VolumeSlider() {} |
| 125 | 136 |
| 126 void Update() { | 137 void Update() { |
| 127 UpdateState(!ash::Shell::GetInstance()->tray_delegate()->IsAudioMuted()); | 138 UpdateState(!IsAudioMuted()); |
| 128 } | 139 } |
| 129 | 140 |
| 130 DISALLOW_COPY_AND_ASSIGN(VolumeSlider); | 141 DISALLOW_COPY_AND_ASSIGN(VolumeSlider); |
| 131 }; | 142 }; |
| 132 | 143 |
| 133 class VolumeView : public views::View, | 144 class VolumeView : public views::View, |
| 134 public views::ButtonListener, | 145 public views::ButtonListener, |
| 135 public views::SliderListener { | 146 public views::SliderListener { |
| 136 public: | 147 public: |
| 137 VolumeView() { | 148 VolumeView() { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 // Overridden from views::View. | 183 // Overridden from views::View. |
| 173 virtual void OnBoundsChanged(const gfx::Rect& old_bounds) OVERRIDE { | 184 virtual void OnBoundsChanged(const gfx::Rect& old_bounds) OVERRIDE { |
| 174 int w = width() - slider_->x(); | 185 int w = width() - slider_->x(); |
| 175 slider_->SetSize(gfx::Size(w, slider_->height())); | 186 slider_->SetSize(gfx::Size(w, slider_->height())); |
| 176 } | 187 } |
| 177 | 188 |
| 178 // Overridden from views::ButtonListener. | 189 // Overridden from views::ButtonListener. |
| 179 virtual void ButtonPressed(views::Button* sender, | 190 virtual void ButtonPressed(views::Button* sender, |
| 180 const views::Event& event) OVERRIDE { | 191 const views::Event& event) OVERRIDE { |
| 181 CHECK(sender == icon_ || sender == mute_); | 192 CHECK(sender == icon_ || sender == mute_); |
| 182 ash::SystemTrayDelegate* delegate = | 193 ash::VolumeControlDelegate* delegate = |
| 183 ash::Shell::GetInstance()->tray_delegate(); | 194 ash::Shell::GetInstance()->accelerator_controller() |
| 184 delegate->SetAudioMuted(!delegate->IsAudioMuted()); | 195 ->volume_control_delegate(); |
| 196 if (delegate) |
| 197 delegate->SetAudioMuted(!IsAudioMuted()); |
| 185 } | 198 } |
| 186 | 199 |
| 187 // Overridden from views:SliderListener. | 200 // Overridden from views:SliderListener. |
| 188 virtual void SliderValueChanged(views::Slider* sender, | 201 virtual void SliderValueChanged(views::Slider* sender, |
| 189 float value, | 202 float value, |
| 190 float old_value, | 203 float old_value, |
| 191 views::SliderChangeReason reason) OVERRIDE { | 204 views::SliderChangeReason reason) OVERRIDE { |
| 192 if (reason == views::VALUE_CHANGED_BY_USER) { | 205 if (reason == views::VALUE_CHANGED_BY_USER) { |
| 193 ash::SystemTrayDelegate* delegate = | 206 ash::VolumeControlDelegate* delegate = |
| 194 ash::Shell::GetInstance()->tray_delegate(); | 207 ash::Shell::GetInstance()->accelerator_controller() |
| 208 ->volume_control_delegate(); |
| 209 DCHECK(delegate); |
| 195 delegate->SetVolumeLevel(value); | 210 delegate->SetVolumeLevel(value); |
| 196 } | 211 } |
| 197 icon_->Update(); | 212 icon_->Update(); |
| 198 } | 213 } |
| 199 | 214 |
| 200 VolumeButton* icon_; | 215 VolumeButton* icon_; |
| 201 MuteButton* mute_; | 216 MuteButton* mute_; |
| 202 VolumeSlider* slider_; | 217 VolumeSlider* slider_; |
| 203 | 218 |
| 204 DISALLOW_COPY_AND_ASSIGN(VolumeView); | 219 DISALLOW_COPY_AND_ASSIGN(VolumeView); |
| 205 }; | 220 }; |
| 206 | 221 |
| 207 } // namespace tray | 222 } // namespace tray |
| 208 | 223 |
| 209 TrayVolume::TrayVolume() | 224 TrayVolume::TrayVolume() |
| 210 : TrayImageItem(IDR_AURA_UBER_TRAY_VOLUME_MUTE), | 225 : TrayImageItem(IDR_AURA_UBER_TRAY_VOLUME_MUTE), |
| 211 volume_view_(NULL), | 226 volume_view_(NULL), |
| 212 is_default_view_(false) { | 227 is_default_view_(false) { |
| 213 } | 228 } |
| 214 | 229 |
| 215 TrayVolume::~TrayVolume() { | 230 TrayVolume::~TrayVolume() { |
| 216 } | 231 } |
| 217 | 232 |
| 218 bool TrayVolume::GetInitialVisibility() { | 233 bool TrayVolume::GetInitialVisibility() { |
| 219 ash::SystemTrayDelegate* delegate = | 234 return IsAudioMuted(); |
| 220 ash::Shell::GetInstance()->tray_delegate(); | |
| 221 return delegate->IsAudioMuted(); | |
| 222 } | 235 } |
| 223 | 236 |
| 224 views::View* TrayVolume::CreateDefaultView(user::LoginStatus status) { | 237 views::View* TrayVolume::CreateDefaultView(user::LoginStatus status) { |
| 225 volume_view_ = new tray::VolumeView; | 238 volume_view_ = new tray::VolumeView; |
| 226 is_default_view_ = true; | 239 is_default_view_ = true; |
| 227 return volume_view_; | 240 return volume_view_; |
| 228 } | 241 } |
| 229 | 242 |
| 230 views::View* TrayVolume::CreateDetailedView(user::LoginStatus status) { | 243 views::View* TrayVolume::CreateDetailedView(user::LoginStatus status) { |
| 231 volume_view_ = new tray::VolumeView; | 244 volume_view_ = new tray::VolumeView; |
| 232 is_default_view_ = false; | 245 is_default_view_ = false; |
| 233 return volume_view_; | 246 return volume_view_; |
| 234 } | 247 } |
| 235 | 248 |
| 236 void TrayVolume::DestroyDefaultView() { | 249 void TrayVolume::DestroyDefaultView() { |
| 237 if (is_default_view_) | 250 if (is_default_view_) |
| 238 volume_view_ = NULL; | 251 volume_view_ = NULL; |
| 239 } | 252 } |
| 240 | 253 |
| 241 void TrayVolume::DestroyDetailedView() { | 254 void TrayVolume::DestroyDetailedView() { |
| 242 if (!is_default_view_) | 255 if (!is_default_view_) |
| 243 volume_view_ = NULL; | 256 volume_view_ = NULL; |
| 244 } | 257 } |
| 245 | 258 |
| 259 void TrayVolume::UpdateAfterAshInit() { |
| 260 // Update the tray view after ash init, since VolumeControlDelegate won't be |
| 261 // created until after the shell and system tray has been initialized. |
| 262 if (tray_view()) |
| 263 tray_view()->SetVisible(GetInitialVisibility()); |
| 264 } |
| 265 |
| 246 void TrayVolume::OnVolumeChanged(float percent) { | 266 void TrayVolume::OnVolumeChanged(float percent) { |
| 247 if (tray_view()) | 267 if (tray_view()) |
| 248 tray_view()->SetVisible(GetInitialVisibility()); | 268 tray_view()->SetVisible(GetInitialVisibility()); |
| 249 | 269 |
| 250 if (volume_view_) { | 270 if (volume_view_) { |
| 251 if (ash::Shell::GetInstance()->tray_delegate()->IsAudioMuted()) | 271 if (IsAudioMuted()) |
| 252 percent = 0.0; | 272 percent = 0.0; |
| 253 volume_view_->SetVolumeLevel(percent); | 273 volume_view_->SetVolumeLevel(percent); |
| 254 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds); | 274 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds); |
| 255 return; | 275 return; |
| 256 } | 276 } |
| 257 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false); | 277 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false); |
| 258 } | 278 } |
| 259 | 279 |
| 260 void TrayVolume::OnMuteToggled() { | 280 void TrayVolume::OnMuteToggled() { |
| 261 if (tray_view()) | 281 if (tray_view()) |
| 262 tray_view()->SetVisible(GetInitialVisibility()); | 282 tray_view()->SetVisible(GetInitialVisibility()); |
| 263 | 283 |
| 264 if (volume_view_) | 284 if (volume_view_) |
| 265 volume_view_->Update(); | 285 volume_view_->Update(); |
| 266 else | 286 else |
| 267 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false); | 287 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false); |
| 268 } | 288 } |
| 269 | 289 |
| 270 } // namespace internal | 290 } // namespace internal |
| 271 } // namespace ash | 291 } // namespace ash |
| OLD | NEW |