| 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/volume_view.h" | |
| 6 | |
| 7 #include "ash/common/ash_constants.h" | |
| 8 #include "ash/common/system/tray/system_tray_item.h" | |
| 9 #include "ash/common/system/tray/tray_constants.h" | |
| 10 #include "ash/common/system/tray/tray_popup_item_container.h" | |
| 11 #include "ash/shell.h" | |
| 12 #include "ash/system/audio/tray_audio.h" | |
| 13 #include "ash/system/audio/tray_audio_delegate.h" | |
| 14 #include "grit/ash_resources.h" | |
| 15 #include "grit/ash_strings.h" | |
| 16 #include "ui/base/resource/resource_bundle.h" | |
| 17 #include "ui/gfx/canvas.h" | |
| 18 #include "ui/gfx/image/image_skia_operations.h" | |
| 19 #include "ui/views/background.h" | |
| 20 #include "ui/views/border.h" | |
| 21 #include "ui/views/controls/button/image_button.h" | |
| 22 #include "ui/views/controls/image_view.h" | |
| 23 #include "ui/views/controls/separator.h" | |
| 24 #include "ui/views/layout/box_layout.h" | |
| 25 | |
| 26 namespace { | |
| 27 const int kVolumeImageWidth = 25; | |
| 28 const int kVolumeImageHeight = 25; | |
| 29 const int kSeparatorSize = 3; | |
| 30 const int kSeparatorVerticalInset = 8; | |
| 31 const int kSliderRightPaddingToVolumeViewEdge = 17; | |
| 32 const int kExtraPaddingBetweenBarAndMore = 10; | |
| 33 const int kExtraPaddingBetweenIconAndSlider = 8; | |
| 34 const int kBoxLayoutPadding = 2; | |
| 35 | |
| 36 // IDR_AURA_UBER_TRAY_VOLUME_LEVELS contains 5 images, | |
| 37 // The one for mute is at the 0 index and the other | |
| 38 // four are used for ascending volume levels. | |
| 39 const int kVolumeLevels = 4; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 namespace ash { | |
| 44 namespace tray { | |
| 45 | |
| 46 class VolumeButton : public views::ToggleImageButton { | |
| 47 public: | |
| 48 VolumeButton(views::ButtonListener* listener, | |
| 49 system::TrayAudioDelegate* audio_delegate) | |
| 50 : views::ToggleImageButton(listener), | |
| 51 audio_delegate_(audio_delegate), | |
| 52 image_index_(-1) { | |
| 53 SetImageAlignment(ALIGN_CENTER, ALIGN_MIDDLE); | |
| 54 image_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 55 IDR_AURA_UBER_TRAY_VOLUME_LEVELS); | |
| 56 Update(); | |
| 57 } | |
| 58 | |
| 59 ~VolumeButton() override {} | |
| 60 | |
| 61 void Update() { | |
| 62 float level = | |
| 63 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f; | |
| 64 int image_index = audio_delegate_->IsOutputAudioMuted() ? | |
| 65 0 : (level == 1.0 ? | |
| 66 kVolumeLevels : | |
| 67 std::max(1, int(std::ceil(level * (kVolumeLevels - 1))))); | |
| 68 if (image_index != image_index_) { | |
| 69 gfx::Rect region(0, image_index * kVolumeImageHeight, | |
| 70 kVolumeImageWidth, kVolumeImageHeight); | |
| 71 gfx::ImageSkia image_skia = gfx::ImageSkiaOperations::ExtractSubset( | |
| 72 *(image_.ToImageSkia()), region); | |
| 73 SetImage(views::CustomButton::STATE_NORMAL, &image_skia); | |
| 74 image_index_ = image_index; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 private: | |
| 79 // views::View: | |
| 80 gfx::Size GetPreferredSize() const override { | |
| 81 gfx::Size size = views::ToggleImageButton::GetPreferredSize(); | |
| 82 size.set_height(kTrayPopupItemHeight); | |
| 83 return size; | |
| 84 } | |
| 85 | |
| 86 // views::CustomButton: | |
| 87 void StateChanged() override { | |
| 88 if (state() == STATE_HOVERED || state() == STATE_PRESSED) { | |
| 89 set_background( | |
| 90 views::Background::CreateSolidBackground(kHoverBackgroundColor)); | |
| 91 } else { | |
| 92 set_background(nullptr); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 system::TrayAudioDelegate* audio_delegate_; | |
| 97 gfx::Image image_; | |
| 98 int image_index_; | |
| 99 | |
| 100 DISALLOW_COPY_AND_ASSIGN(VolumeButton); | |
| 101 }; | |
| 102 | |
| 103 VolumeView::VolumeView(SystemTrayItem* owner, | |
| 104 system::TrayAudioDelegate* audio_delegate, | |
| 105 bool is_default_view) | |
| 106 : owner_(owner), | |
| 107 audio_delegate_(audio_delegate), | |
| 108 icon_(NULL), | |
| 109 slider_(NULL), | |
| 110 device_type_(NULL), | |
| 111 more_(NULL), | |
| 112 is_default_view_(is_default_view) { | |
| 113 SetFocusBehavior(FocusBehavior::NEVER); | |
| 114 views::BoxLayout* box_layout = new views::BoxLayout( | |
| 115 views::BoxLayout::kHorizontal, 0, 0, kBoxLayoutPadding); | |
| 116 box_layout->SetDefaultFlex(0); | |
| 117 SetLayoutManager(box_layout); | |
| 118 | |
| 119 icon_ = new VolumeButton(this, audio_delegate_); | |
| 120 icon_->SetBorder(views::Border::CreateEmptyBorder( | |
| 121 0, kTrayPopupPaddingHorizontal, 0, kExtraPaddingBetweenIconAndSlider)); | |
| 122 AddChildView(icon_); | |
| 123 | |
| 124 slider_ = new views::Slider(this, views::Slider::HORIZONTAL); | |
| 125 slider_->set_focus_border_color(kFocusBorderColor); | |
| 126 slider_->SetValue( | |
| 127 static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f); | |
| 128 slider_->SetAccessibleName( | |
| 129 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
| 130 IDS_ASH_STATUS_TRAY_VOLUME)); | |
| 131 slider_->SetBorder( | |
| 132 views::Border::CreateEmptyBorder(0, 0, 0, kTrayPopupPaddingBetweenItems)); | |
| 133 AddChildView(slider_); | |
| 134 box_layout->SetFlexForView(slider_, 1); | |
| 135 | |
| 136 separator_ = new views::Separator(views::Separator::VERTICAL); | |
| 137 separator_->SetColor(kButtonStrokeColor); | |
| 138 separator_->SetPreferredSize(kSeparatorSize); | |
| 139 separator_->SetBorder(views::Border::CreateEmptyBorder( | |
| 140 kSeparatorVerticalInset, 0, kSeparatorVerticalInset, kBoxLayoutPadding)); | |
| 141 | |
| 142 more_region_ = new TrayPopupItemContainer(separator_, true, false); | |
| 143 more_region_->SetBorder( | |
| 144 views::Border::CreateEmptyBorder(0, 0, 0, kTrayPopupPaddingBetweenItems)); | |
| 145 AddChildView(more_region_); | |
| 146 | |
| 147 device_type_ = new views::ImageView; | |
| 148 more_region_->AddChildView(device_type_); | |
| 149 | |
| 150 more_ = new views::ImageView; | |
| 151 more_->EnableCanvasFlippingForRTLUI(true); | |
| 152 more_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 153 IDR_AURA_UBER_TRAY_MORE).ToImageSkia()); | |
| 154 more_region_->AddChildView(more_); | |
| 155 | |
| 156 set_background(views::Background::CreateSolidBackground(kBackgroundColor)); | |
| 157 | |
| 158 Update(); | |
| 159 } | |
| 160 | |
| 161 VolumeView::~VolumeView() { | |
| 162 } | |
| 163 | |
| 164 void VolumeView::Update() { | |
| 165 icon_->Update(); | |
| 166 slider_->UpdateState(!audio_delegate_->IsOutputAudioMuted()); | |
| 167 UpdateDeviceTypeAndMore(); | |
| 168 Layout(); | |
| 169 } | |
| 170 | |
| 171 void VolumeView::SetVolumeLevel(float percent) { | |
| 172 // Slider's value is in finer granularity than audio volume level(0.01), | |
| 173 // there will be a small discrepancy between slider's value and volume level | |
| 174 // on audio side. To avoid the jittering in slider UI, do not set change | |
| 175 // slider value if the change is less than 1%. | |
| 176 if (std::abs(percent-slider_->value()) < 0.01) | |
| 177 return; | |
| 178 slider_->SetValue(percent); | |
| 179 // It is possible that the volume was (un)muted, but the actual volume level | |
| 180 // did not change. In that case, setting the value of the slider won't | |
| 181 // trigger an update. So explicitly trigger an update. | |
| 182 Update(); | |
| 183 slider_->set_enable_accessibility_events(true); | |
| 184 } | |
| 185 | |
| 186 void VolumeView::UpdateDeviceTypeAndMore() { | |
| 187 bool show_more = is_default_view_ && TrayAudio::ShowAudioDeviceMenu() && | |
| 188 audio_delegate_->HasAlternativeSources(); | |
| 189 slider_->SetBorder(views::Border::CreateEmptyBorder( | |
| 190 0, 0, 0, show_more ? kTrayPopupPaddingBetweenItems | |
| 191 : kSliderRightPaddingToVolumeViewEdge)); | |
| 192 | |
| 193 if (!show_more) { | |
| 194 more_region_->SetVisible(false); | |
| 195 return; | |
| 196 } | |
| 197 | |
| 198 // Show output device icon if necessary. | |
| 199 int device_icon = audio_delegate_->GetActiveOutputDeviceIconId(); | |
| 200 if (device_icon != system::TrayAudioDelegate::kNoAudioDeviceIcon) { | |
| 201 device_type_->SetVisible(true); | |
| 202 device_type_->SetImage( | |
| 203 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 204 device_icon).ToImageSkia()); | |
| 205 more_region_->SetLayoutManager(new views::BoxLayout( | |
| 206 views::BoxLayout::kHorizontal, 0, 0, kTrayPopupPaddingBetweenItems)); | |
| 207 } else { | |
| 208 device_type_->SetVisible(false); | |
| 209 more_region_->SetLayoutManager(new views::BoxLayout( | |
| 210 views::BoxLayout::kHorizontal, 0, 0, | |
| 211 kTrayPopupPaddingBetweenItems + kExtraPaddingBetweenBarAndMore)); | |
| 212 } | |
| 213 more_region_->SetVisible(true); | |
| 214 } | |
| 215 | |
| 216 void VolumeView::HandleVolumeUp(float level) { | |
| 217 audio_delegate_->SetOutputVolumeLevel(level); | |
| 218 if (audio_delegate_->IsOutputAudioMuted() && | |
| 219 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { | |
| 220 audio_delegate_->SetOutputAudioIsMuted(false); | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 void VolumeView::HandleVolumeDown(float level) { | |
| 225 audio_delegate_->SetOutputVolumeLevel(level); | |
| 226 if (!audio_delegate_->IsOutputAudioMuted() && | |
| 227 level <= audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { | |
| 228 audio_delegate_->SetOutputAudioIsMuted(true); | |
| 229 } else if (audio_delegate_->IsOutputAudioMuted() && | |
| 230 level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) { | |
| 231 audio_delegate_->SetOutputAudioIsMuted(false); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 void VolumeView::ButtonPressed(views::Button* sender, const ui::Event& event) { | |
| 236 CHECK(sender == icon_); | |
| 237 bool mute_on = !audio_delegate_->IsOutputAudioMuted(); | |
| 238 audio_delegate_->SetOutputAudioIsMuted(mute_on); | |
| 239 if (!mute_on) | |
| 240 audio_delegate_->AdjustOutputVolumeToAudibleLevel(); | |
| 241 icon_->Update(); | |
| 242 } | |
| 243 | |
| 244 void VolumeView::SliderValueChanged(views::Slider* sender, | |
| 245 float value, | |
| 246 float old_value, | |
| 247 views::SliderChangeReason reason) { | |
| 248 if (reason == views::VALUE_CHANGED_BY_USER) { | |
| 249 float new_volume = value * 100.0f; | |
| 250 float current_volume = audio_delegate_->GetOutputVolumeLevel(); | |
| 251 // Do not call change audio volume if the difference is less than | |
| 252 // 1%, which is beyond cras audio api's granularity for output volume. | |
| 253 if (std::abs(new_volume - current_volume) < 1.0f) | |
| 254 return; | |
| 255 Shell::GetInstance()->metrics()->RecordUserMetricsAction( | |
| 256 is_default_view_ ? | |
| 257 ash::UMA_STATUS_AREA_CHANGED_VOLUME_MENU : | |
| 258 ash::UMA_STATUS_AREA_CHANGED_VOLUME_POPUP); | |
| 259 if (new_volume > current_volume) | |
| 260 HandleVolumeUp(new_volume); | |
| 261 else | |
| 262 HandleVolumeDown(new_volume); | |
| 263 } | |
| 264 icon_->Update(); | |
| 265 } | |
| 266 | |
| 267 bool VolumeView::PerformAction(const ui::Event& event) { | |
| 268 if (!more_region_->visible()) | |
| 269 return false; | |
| 270 owner_->TransitionDetailedView(); | |
| 271 return true; | |
| 272 } | |
| 273 | |
| 274 void VolumeView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
| 275 // Separator's prefered size is based on set bounds. When an empty bounds is | |
| 276 // set on first layout this causes BoxLayout to ignore the separator. Reset | |
| 277 // its height on each bounds change so that it is laid out properly. | |
| 278 separator_->SetSize(gfx::Size(kSeparatorSize, bounds().height())); | |
| 279 } | |
| 280 | |
| 281 void VolumeView::GetAccessibleState(ui::AXViewState* state) { | |
| 282 // Intentionally overrides ActionableView, leaving |state| unset. A slider | |
| 283 // childview exposes accessibility data. | |
| 284 } | |
| 285 | |
| 286 } // namespace tray | |
| 287 } // namespace ash | |
| OLD | NEW |