| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/common/system/audio/volume_view.h" | 5 #include "ash/common/system/audio/volume_view.h" |
| 6 | 6 |
| 7 #include "ash/common/ash_constants.h" | 7 #include "ash/common/ash_constants.h" |
| 8 #include "ash/common/metrics/user_metrics_action.h" | 8 #include "ash/common/metrics/user_metrics_action.h" |
| 9 #include "ash/common/system/audio/tray_audio.h" | 9 #include "ash/common/system/audio/tray_audio.h" |
| 10 #include "ash/common/system/audio/tray_audio_delegate.h" | 10 #include "ash/common/system/audio/tray_audio_delegate.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 // four are used for ascending volume levels. | 39 // four are used for ascending volume levels. |
| 40 const int kVolumeLevels = 4; | 40 const int kVolumeLevels = 4; |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 namespace ash { | 44 namespace ash { |
| 45 namespace tray { | 45 namespace tray { |
| 46 | 46 |
| 47 class VolumeButton : public views::ToggleImageButton { | 47 class VolumeButton : public views::ToggleImageButton { |
| 48 public: | 48 public: |
| 49 VolumeButton(views::ButtonListener* listener, | 49 VolumeButton(views::ButtonListener* listener, |
| 50 system::TrayAudioDelegate* audio_delegate) | 50 system::TrayAudioDelegate* audio_delegate) |
| 51 : views::ToggleImageButton(listener), | 51 : views::ToggleImageButton(listener), |
| 52 audio_delegate_(audio_delegate), | 52 audio_delegate_(audio_delegate), |
| 53 image_index_(-1) { | 53 image_index_(-1) { |
| 54 SetImageAlignment(ALIGN_CENTER, ALIGN_MIDDLE); | 54 SetImageAlignment(ALIGN_CENTER, ALIGN_MIDDLE); |
| 55 image_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 55 image_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 56 IDR_AURA_UBER_TRAY_VOLUME_LEVELS); | 56 IDR_AURA_UBER_TRAY_VOLUME_LEVELS); |
| 57 Update(); | 57 Update(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 ~VolumeButton() override {} | 60 ~VolumeButton() override {} |
| 61 | 61 |
| 62 void Update() { | 62 void Update() { |
| 63 float level = | 63 float level = |
| 64 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f; | 64 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f; |
| 65 int image_index = audio_delegate_->IsOutputAudioMuted() ? | 65 int image_index = |
| 66 0 : (level == 1.0 ? | 66 audio_delegate_->IsOutputAudioMuted() |
| 67 kVolumeLevels : | 67 ? 0 |
| 68 std::max(1, int(std::ceil(level * (kVolumeLevels - 1))))); | 68 : (level == 1.0 |
| 69 ? kVolumeLevels |
| 70 : std::max(1, int(std::ceil(level * (kVolumeLevels - 1))))); |
| 69 if (image_index != image_index_) { | 71 if (image_index != image_index_) { |
| 70 gfx::Rect region(0, image_index * kVolumeImageHeight, | 72 gfx::Rect region(0, image_index * kVolumeImageHeight, kVolumeImageWidth, |
| 71 kVolumeImageWidth, kVolumeImageHeight); | 73 kVolumeImageHeight); |
| 72 gfx::ImageSkia image_skia = gfx::ImageSkiaOperations::ExtractSubset( | 74 gfx::ImageSkia image_skia = gfx::ImageSkiaOperations::ExtractSubset( |
| 73 *(image_.ToImageSkia()), region); | 75 *(image_.ToImageSkia()), region); |
| 74 SetImage(views::CustomButton::STATE_NORMAL, &image_skia); | 76 SetImage(views::CustomButton::STATE_NORMAL, &image_skia); |
| 75 image_index_ = image_index; | 77 image_index_ = image_index; |
| 76 } | 78 } |
| 77 } | 79 } |
| 78 | 80 |
| 79 private: | 81 private: |
| 80 // views::View: | 82 // views::View: |
| 81 gfx::Size GetPreferredSize() const override { | 83 gfx::Size GetPreferredSize() const override { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 more_region_ = new TrayPopupItemContainer(separator_, true, false); | 145 more_region_ = new TrayPopupItemContainer(separator_, true, false); |
| 144 more_region_->SetBorder( | 146 more_region_->SetBorder( |
| 145 views::Border::CreateEmptyBorder(0, 0, 0, kTrayPopupPaddingBetweenItems)); | 147 views::Border::CreateEmptyBorder(0, 0, 0, kTrayPopupPaddingBetweenItems)); |
| 146 AddChildView(more_region_); | 148 AddChildView(more_region_); |
| 147 | 149 |
| 148 device_type_ = new views::ImageView; | 150 device_type_ = new views::ImageView; |
| 149 more_region_->AddChildView(device_type_); | 151 more_region_->AddChildView(device_type_); |
| 150 | 152 |
| 151 more_ = new views::ImageView; | 153 more_ = new views::ImageView; |
| 152 more_->EnableCanvasFlippingForRTLUI(true); | 154 more_->EnableCanvasFlippingForRTLUI(true); |
| 153 more_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 155 more_->SetImage(ui::ResourceBundle::GetSharedInstance() |
| 154 IDR_AURA_UBER_TRAY_MORE).ToImageSkia()); | 156 .GetImageNamed(IDR_AURA_UBER_TRAY_MORE) |
| 157 .ToImageSkia()); |
| 155 more_region_->AddChildView(more_); | 158 more_region_->AddChildView(more_); |
| 156 | 159 |
| 157 set_background(views::Background::CreateSolidBackground(kBackgroundColor)); | 160 set_background(views::Background::CreateSolidBackground(kBackgroundColor)); |
| 158 | 161 |
| 159 Update(); | 162 Update(); |
| 160 } | 163 } |
| 161 | 164 |
| 162 VolumeView::~VolumeView() { | 165 VolumeView::~VolumeView() {} |
| 163 } | |
| 164 | 166 |
| 165 void VolumeView::Update() { | 167 void VolumeView::Update() { |
| 166 icon_->Update(); | 168 icon_->Update(); |
| 167 slider_->UpdateState(!audio_delegate_->IsOutputAudioMuted()); | 169 slider_->UpdateState(!audio_delegate_->IsOutputAudioMuted()); |
| 168 UpdateDeviceTypeAndMore(); | 170 UpdateDeviceTypeAndMore(); |
| 169 Layout(); | 171 Layout(); |
| 170 } | 172 } |
| 171 | 173 |
| 172 void VolumeView::SetVolumeLevel(float percent) { | 174 void VolumeView::SetVolumeLevel(float percent) { |
| 173 // Slider's value is in finer granularity than audio volume level(0.01), | 175 // Slider's value is in finer granularity than audio volume level(0.01), |
| 174 // there will be a small discrepancy between slider's value and volume level | 176 // there will be a small discrepancy between slider's value and volume level |
| 175 // on audio side. To avoid the jittering in slider UI, do not set change | 177 // on audio side. To avoid the jittering in slider UI, do not set change |
| 176 // slider value if the change is less than 1%. | 178 // slider value if the change is less than 1%. |
| 177 if (std::abs(percent-slider_->value()) < 0.01) | 179 if (std::abs(percent - slider_->value()) < 0.01) |
| 178 return; | 180 return; |
| 179 slider_->SetValue(percent); | 181 slider_->SetValue(percent); |
| 180 // It is possible that the volume was (un)muted, but the actual volume level | 182 // It is possible that the volume was (un)muted, but the actual volume level |
| 181 // did not change. In that case, setting the value of the slider won't | 183 // did not change. In that case, setting the value of the slider won't |
| 182 // trigger an update. So explicitly trigger an update. | 184 // trigger an update. So explicitly trigger an update. |
| 183 Update(); | 185 Update(); |
| 184 slider_->set_enable_accessibility_events(true); | 186 slider_->set_enable_accessibility_events(true); |
| 185 } | 187 } |
| 186 | 188 |
| 187 void VolumeView::UpdateDeviceTypeAndMore() { | 189 void VolumeView::UpdateDeviceTypeAndMore() { |
| 188 bool show_more = is_default_view_ && TrayAudio::ShowAudioDeviceMenu() && | 190 bool show_more = is_default_view_ && TrayAudio::ShowAudioDeviceMenu() && |
| 189 audio_delegate_->HasAlternativeSources(); | 191 audio_delegate_->HasAlternativeSources(); |
| 190 slider_->SetBorder(views::Border::CreateEmptyBorder( | 192 slider_->SetBorder(views::Border::CreateEmptyBorder( |
| 191 0, 0, 0, show_more ? kTrayPopupPaddingBetweenItems | 193 0, 0, 0, show_more ? kTrayPopupPaddingBetweenItems |
| 192 : kSliderRightPaddingToVolumeViewEdge)); | 194 : kSliderRightPaddingToVolumeViewEdge)); |
| 193 | 195 |
| 194 if (!show_more) { | 196 if (!show_more) { |
| 195 more_region_->SetVisible(false); | 197 more_region_->SetVisible(false); |
| 196 return; | 198 return; |
| 197 } | 199 } |
| 198 | 200 |
| 199 // Show output device icon if necessary. | 201 // Show output device icon if necessary. |
| 200 int device_icon = audio_delegate_->GetActiveOutputDeviceIconId(); | 202 int device_icon = audio_delegate_->GetActiveOutputDeviceIconId(); |
| 201 if (device_icon != system::TrayAudioDelegate::kNoAudioDeviceIcon) { | 203 if (device_icon != system::TrayAudioDelegate::kNoAudioDeviceIcon) { |
| 202 device_type_->SetVisible(true); | 204 device_type_->SetVisible(true); |
| 203 device_type_->SetImage( | 205 device_type_->SetImage(ui::ResourceBundle::GetSharedInstance() |
| 204 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 206 .GetImageNamed(device_icon) |
| 205 device_icon).ToImageSkia()); | 207 .ToImageSkia()); |
| 206 more_region_->SetLayoutManager(new views::BoxLayout( | 208 more_region_->SetLayoutManager(new views::BoxLayout( |
| 207 views::BoxLayout::kHorizontal, 0, 0, kTrayPopupPaddingBetweenItems)); | 209 views::BoxLayout::kHorizontal, 0, 0, kTrayPopupPaddingBetweenItems)); |
| 208 } else { | 210 } else { |
| 209 device_type_->SetVisible(false); | 211 device_type_->SetVisible(false); |
| 210 more_region_->SetLayoutManager(new views::BoxLayout( | 212 more_region_->SetLayoutManager(new views::BoxLayout( |
| 211 views::BoxLayout::kHorizontal, 0, 0, | 213 views::BoxLayout::kHorizontal, 0, 0, |
| 212 kTrayPopupPaddingBetweenItems + kExtraPaddingBetweenBarAndMore)); | 214 kTrayPopupPaddingBetweenItems + kExtraPaddingBetweenBarAndMore)); |
| 213 } | 215 } |
| 214 more_region_->SetVisible(true); | 216 more_region_->SetVisible(true); |
| 215 } | 217 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 separator_->SetSize(gfx::Size(kSeparatorSize, bounds().height())); | 280 separator_->SetSize(gfx::Size(kSeparatorSize, bounds().height())); |
| 279 } | 281 } |
| 280 | 282 |
| 281 void VolumeView::GetAccessibleState(ui::AXViewState* state) { | 283 void VolumeView::GetAccessibleState(ui::AXViewState* state) { |
| 282 // Intentionally overrides ActionableView, leaving |state| unset. A slider | 284 // Intentionally overrides ActionableView, leaving |state| unset. A slider |
| 283 // childview exposes accessibility data. | 285 // childview exposes accessibility data. |
| 284 } | 286 } |
| 285 | 287 |
| 286 } // namespace tray | 288 } // namespace tray |
| 287 } // namespace ash | 289 } // namespace ash |
| OLD | NEW |