Chromium Code Reviews| 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> | |
| 8 | |
| 7 #include "ash/shell.h" | 9 #include "ash/shell.h" |
| 8 #include "ash/system/tray/system_tray_delegate.h" | 10 #include "ash/system/tray/system_tray_delegate.h" |
| 9 #include "ash/system/tray/tray_constants.h" | 11 #include "ash/system/tray/tray_constants.h" |
| 10 #include "ash/system/tray/tray_views.h" | 12 #include "ash/system/tray/tray_views.h" |
| 11 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 12 #include "grit/ash_strings.h" | 14 #include "grit/ash_strings.h" |
| 13 #include "grit/ui_resources.h" | 15 #include "grit/ui_resources.h" |
| 14 #include "third_party/skia/include/core/SkCanvas.h" | 16 #include "third_party/skia/include/core/SkCanvas.h" |
| 15 #include "third_party/skia/include/core/SkPaint.h" | 17 #include "third_party/skia/include/core/SkPaint.h" |
| 16 #include "third_party/skia/include/core/SkRect.h" | 18 #include "third_party/skia/include/core/SkRect.h" |
| 17 #include "third_party/skia/include/effects/SkGradientShader.h" | 19 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
| 20 #include "ui/gfx/image/image.h" | 22 #include "ui/gfx/image/image.h" |
| 23 #include "ui/gfx/image/image_skia_operations.h" | |
| 21 #include "ui/views/controls/button/image_button.h" | 24 #include "ui/views/controls/button/image_button.h" |
| 22 #include "ui/views/controls/image_view.h" | 25 #include "ui/views/controls/image_view.h" |
| 23 #include "ui/views/controls/label.h" | 26 #include "ui/views/controls/label.h" |
| 24 #include "ui/views/controls/slider.h" | 27 #include "ui/views/controls/slider.h" |
| 25 #include "ui/views/layout/box_layout.h" | 28 #include "ui/views/layout/box_layout.h" |
| 26 #include "ui/views/view.h" | 29 #include "ui/views/view.h" |
| 27 | 30 |
| 28 namespace ash { | 31 namespace ash { |
| 29 namespace internal { | 32 namespace internal { |
| 30 | 33 |
| 31 namespace { | 34 namespace { |
| 32 const int kVolumeImageWidth = 25; | 35 const int kVolumeImageWidth = 25; |
| 33 const int kVolumeImageHeight = 25; | 36 const int kVolumeImageHeight = 25; |
| 37 | |
| 38 // IDR_AURA_UBER_TRAY_VOLUME_LEVELS contains 5 images, | |
|
Daniel Erat
2012/07/24 00:50:10
nit: "... contains 5 images. The one for mute is
oshima
2012/07/24 11:34:33
Done.
| |
| 39 // the one for muted at 0 index, and the rest of 4 images are | |
| 40 // used to represent volume levels. | |
| 34 const int kVolumeLevel = 4; | 41 const int kVolumeLevel = 4; |
|
Daniel Erat
2012/07/24 00:50:10
nit: rename to kVolumeLevels?
oshima
2012/07/24 11:34:33
Done.
| |
| 35 } | 42 } |
| 36 | 43 |
| 37 namespace tray { | 44 namespace tray { |
| 38 | 45 |
| 39 class VolumeButton : public views::ToggleImageButton { | 46 class VolumeButton : public views::ToggleImageButton { |
| 40 public: | 47 public: |
| 41 explicit VolumeButton(views::ButtonListener* listener) | 48 explicit VolumeButton(views::ButtonListener* listener) |
| 42 : views::ToggleImageButton(listener), | 49 : views::ToggleImageButton(listener), |
| 43 image_index_(-1) { | 50 image_index_(-1) { |
| 44 SetImageAlignment(ALIGN_CENTER, ALIGN_MIDDLE); | 51 SetImageAlignment(ALIGN_CENTER, ALIGN_MIDDLE); |
| 45 image_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 52 image_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 46 IDR_AURA_UBER_TRAY_VOLUME_LEVELS); | 53 IDR_AURA_UBER_TRAY_VOLUME_LEVELS); |
| 47 SetPreferredSize(gfx::Size(kTrayPopupItemHeight, kTrayPopupItemHeight)); | 54 SetPreferredSize(gfx::Size(kTrayPopupItemHeight, kTrayPopupItemHeight)); |
| 48 Update(); | 55 Update(); |
| 49 } | 56 } |
| 50 | 57 |
| 51 virtual ~VolumeButton() {} | 58 virtual ~VolumeButton() {} |
| 52 | 59 |
| 53 void Update() { | 60 void Update() { |
| 54 ash::SystemTrayDelegate* delegate = | 61 ash::SystemTrayDelegate* delegate = |
| 55 ash::Shell::GetInstance()->tray_delegate(); | 62 ash::Shell::GetInstance()->tray_delegate(); |
| 56 int level = static_cast<int>(delegate->GetVolumeLevel() * 100); | 63 float level = delegate->GetVolumeLevel(); |
| 57 int image_index = level / (100 / kVolumeLevel); | 64 int image_index = delegate->IsAudioMuted() ? |
| 58 if (level > 0 && image_index == 0) | 65 0 : (level == 1.0 ? |
| 59 ++image_index; | 66 kVolumeLevel : std::ceil(level * (kVolumeLevel -1))); |
|
Daniel Erat
2012/07/24 00:50:10
nit: space between "-" and "1"
oshima
2012/07/24 11:34:33
Done.
| |
| 60 if (level == 100) | 67 |
| 61 image_index = kVolumeLevel - 1; | |
| 62 else if (image_index == kVolumeLevel - 1) | |
| 63 --image_index; | |
| 64 // Index 0 is reserved for mute. | |
| 65 if (delegate->IsAudioMuted()) | |
| 66 image_index = 0; | |
| 67 else | |
| 68 ++image_index; | |
| 69 if (image_index != image_index_) { | 68 if (image_index != image_index_) { |
| 70 SkIRect region = SkIRect::MakeXYWH(0, image_index * kVolumeImageHeight, | 69 gfx::Rect region(0, image_index * kVolumeImageHeight, |
| 71 kVolumeImageWidth, kVolumeImageHeight); | 70 kVolumeImageWidth, kVolumeImageHeight); |
| 72 gfx::ImageSkia image_skia; | 71 gfx::ImageSkia image_skia = gfx::ImageSkiaOperations::ExtractSubset( |
| 73 image_.ToImageSkia()->extractSubset(&image_skia, region); | 72 *(image_.ToImageSkia()), region); |
| 74 SetImage(views::CustomButton::BS_NORMAL, &image_skia); | 73 SetImage(views::CustomButton::BS_NORMAL, &image_skia); |
| 75 image_index_ = image_index; | 74 image_index_ = image_index; |
| 76 } | 75 } |
| 77 SchedulePaint(); | 76 SchedulePaint(); |
| 78 } | 77 } |
| 79 | 78 |
| 80 private: | 79 private: |
| 81 // Overridden from views::View. | 80 // Overridden from views::View. |
| 82 virtual gfx::Size GetPreferredSize() OVERRIDE { | 81 virtual gfx::Size GetPreferredSize() OVERRIDE { |
| 83 gfx::Size size = views::ToggleImageButton::GetPreferredSize(); | 82 gfx::Size size = views::ToggleImageButton::GetPreferredSize(); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 percent = 0.0; | 210 percent = 0.0; |
| 212 volume_view_->SetVolumeLevel(percent); | 211 volume_view_->SetVolumeLevel(percent); |
| 213 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds); | 212 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds); |
| 214 return; | 213 return; |
| 215 } | 214 } |
| 216 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false); | 215 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false); |
| 217 } | 216 } |
| 218 | 217 |
| 219 } // namespace internal | 218 } // namespace internal |
| 220 } // namespace ash | 219 } // namespace ash |
| OLD | NEW |