Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2157)

Unified Diff: ash/system/audio/volume_view.cc

Issue 165393013: Resubmit 'Refactor the TrayAudio code so that it can be used by other platforms.' (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase to ToT Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ash/system/audio/volume_view.h ('k') | ash/system/chromeos/audio/audio_detailed_view.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ash/system/audio/volume_view.cc
diff --git a/ash/system/audio/volume_view.cc b/ash/system/audio/volume_view.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5cb3970af2426870c4a024364de26ef3ae91d699
--- /dev/null
+++ b/ash/system/audio/volume_view.cc
@@ -0,0 +1,332 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ash/system/audio/volume_view.h"
+
+#include "ash/ash_constants.h"
+#include "ash/ash_switches.h"
+#include "ash/shell.h"
+#include "ash/system/audio/tray_audio_delegate.h"
+#include "ash/system/tray/system_tray_item.h"
+#include "ash/system/tray/tray_constants.h"
+#include "grit/ash_resources.h"
+#include "grit/ash_strings.h"
+#include "ui/base/resource/resource_bundle.h"
+#include "ui/gfx/canvas.h"
+#include "ui/gfx/image/image_skia_operations.h"
+#include "ui/views/controls/button/image_button.h"
+#include "ui/views/controls/image_view.h"
+#include "ui/views/layout/box_layout.h"
+
+namespace {
+const int kVolumeImageWidth = 25;
+const int kVolumeImageHeight = 25;
+const int kBarSeparatorWidth = 25;
+const int kBarSeparatorHeight = 30;
+const int kSliderRightPaddingToVolumeViewEdge = 17;
+const int kExtraPaddingBetweenBarAndMore = 10;
+
+// IDR_AURA_UBER_TRAY_VOLUME_LEVELS contains 5 images,
+// The one for mute is at the 0 index and the other
+// four are used for ascending volume levels.
+const int kVolumeLevels = 4;
+
+} // namespace
+
+namespace ash {
+namespace internal {
+namespace tray {
+
+class VolumeButton : public views::ToggleImageButton {
+ public:
+ VolumeButton(views::ButtonListener* listener,
+ system::TrayAudioDelegate* audio_delegate)
+ : views::ToggleImageButton(listener),
+ audio_delegate_(audio_delegate),
+ image_index_(-1) {
+ SetImageAlignment(ALIGN_CENTER, ALIGN_MIDDLE);
+ image_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
+ IDR_AURA_UBER_TRAY_VOLUME_LEVELS);
+ SetPreferredSize(gfx::Size(kTrayPopupItemHeight, kTrayPopupItemHeight));
+ Update();
+ }
+
+ virtual ~VolumeButton() {}
+
+ void Update() {
+ float level =
+ static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f;
+ int image_index = audio_delegate_->IsOutputAudioMuted() ?
+ 0 : (level == 1.0 ?
+ kVolumeLevels :
+ std::max(1, int(std::ceil(level * (kVolumeLevels - 1)))));
+ if (image_index != image_index_) {
+ gfx::Rect region(0, image_index * kVolumeImageHeight,
+ kVolumeImageWidth, kVolumeImageHeight);
+ gfx::ImageSkia image_skia = gfx::ImageSkiaOperations::ExtractSubset(
+ *(image_.ToImageSkia()), region);
+ SetImage(views::CustomButton::STATE_NORMAL, &image_skia);
+ image_index_ = image_index;
+ }
+ SchedulePaint();
+ }
+
+ private:
+ // Overridden from views::View.
+ virtual gfx::Size GetPreferredSize() OVERRIDE {
+ gfx::Size size = views::ToggleImageButton::GetPreferredSize();
+ size.set_height(kTrayPopupItemHeight);
+ return size;
+ }
+
+ system::TrayAudioDelegate* audio_delegate_;
+ gfx::Image image_;
+ int image_index_;
+
+ DISALLOW_COPY_AND_ASSIGN(VolumeButton);
+};
+
+class VolumeSlider : public views::Slider {
+ public:
+ VolumeSlider(views::SliderListener* listener,
+ system::TrayAudioDelegate* audio_delegate)
+ : views::Slider(listener, views::Slider::HORIZONTAL),
+ audio_delegate_(audio_delegate) {
+ set_focus_border_color(kFocusBorderColor);
+ SetValue(
+ static_cast<float>(audio_delegate_->GetOutputVolumeLevel()) / 100.0f);
+ SetAccessibleName(
+ ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
+ IDS_ASH_STATUS_TRAY_VOLUME));
+ Update();
+ }
+ virtual ~VolumeSlider() {}
+
+ void Update() {
+ UpdateState(!audio_delegate_->IsOutputAudioMuted());
+ }
+
+ private:
+ system::TrayAudioDelegate* audio_delegate_;
+
+ DISALLOW_COPY_AND_ASSIGN(VolumeSlider);
+};
+
+// Vertical bar separator that can be placed on the VolumeView.
+class BarSeparator : public views::View {
+ public:
+ BarSeparator() {}
+ virtual ~BarSeparator() {}
+
+ // Overriden from views::View.
+ virtual gfx::Size GetPreferredSize() OVERRIDE {
+ return gfx::Size(kBarSeparatorWidth, kBarSeparatorHeight);
+ }
+
+ private:
+ virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
+ canvas->FillRect(gfx::Rect(width() / 2, 0, 1, height()),
+ kButtonStrokeColor);
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(BarSeparator);
+};
+
+VolumeView::VolumeView(SystemTrayItem* owner,
+ system::TrayAudioDelegate* audio_delegate,
+ bool is_default_view)
+ : owner_(owner),
+ audio_delegate_(audio_delegate),
+ icon_(NULL),
+ slider_(NULL),
+ bar_(NULL),
+ device_type_(NULL),
+ more_(NULL),
+ is_default_view_(is_default_view) {
+ SetFocusable(false);
+ SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
+ kTrayPopupPaddingHorizontal, 0, kTrayPopupPaddingBetweenItems));
+
+ icon_ = new VolumeButton(this, audio_delegate_);
+ AddChildView(icon_);
+
+ slider_ = new VolumeSlider(this, audio_delegate_);
+ AddChildView(slider_);
+
+ bar_ = new BarSeparator;
+ AddChildView(bar_);
+
+ device_type_ = new views::ImageView;
+ AddChildView(device_type_);
+
+ more_ = new views::ImageView;
+ more_->EnableCanvasFlippingForRTLUI(true);
+ more_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
+ IDR_AURA_UBER_TRAY_MORE).ToImageSkia());
+ AddChildView(more_);
+
+ Update();
+}
+
+VolumeView::~VolumeView() {
+}
+
+void VolumeView::Update() {
+ icon_->Update();
+ slider_->Update();
+ UpdateDeviceTypeAndMore();
+ Layout();
+}
+
+void VolumeView::SetVolumeLevel(float percent) {
+ // Slider's value is in finer granularity than audio volume level(0.01),
+ // there will be a small discrepancy between slider's value and volume level
+ // on audio side. To avoid the jittering in slider UI, do not set change
+ // slider value if the change is less than 1%.
+ if (std::abs(percent-slider_->value()) < 0.01)
+ return;
+ // The change in volume will be reflected via accessibility system events,
+ // so we prevent the UI event from being sent here.
+ slider_->set_enable_accessibility_events(false);
+ slider_->SetValue(percent);
+ // It is possible that the volume was (un)muted, but the actual volume level
+ // did not change. In that case, setting the value of the slider won't
+ // trigger an update. So explicitly trigger an update.
+ Update();
+ slider_->set_enable_accessibility_events(true);
+}
+
+void VolumeView::UpdateDeviceTypeAndMore() {
+ if (!ash::switches::ShowAudioDeviceMenu() || !is_default_view_) {
+ more_->SetVisible(false);
+ bar_->SetVisible(false);
+ device_type_->SetVisible(false);
+ return;
+ }
+
+ bool show_more = audio_delegate_->HasAlternativeSources();
+ more_->SetVisible(show_more);
+ bar_->SetVisible(show_more);
+
+ // Show output device icon if necessary.
+ int device_icon = audio_delegate_->GetActiveOutputDeviceIconId();
+ if (device_icon != system::TrayAudioDelegate::kNoAudioDeviceIcon) {
+ device_type_->SetVisible(true);
+ device_type_->SetImage(
+ ui::ResourceBundle::GetSharedInstance().GetImageNamed(
+ device_icon).ToImageSkia());
+ } else {
+ device_type_->SetVisible(false);
+ }
+}
+
+void VolumeView::HandleVolumeUp(float level) {
+ audio_delegate_->SetOutputVolumeLevel(level);
+ if (audio_delegate_->IsOutputAudioMuted() &&
+ level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) {
+ audio_delegate_->SetOutputAudioIsMuted(false);
+ }
+}
+
+void VolumeView::HandleVolumeDown(float level) {
+ audio_delegate_->SetOutputVolumeLevel(level);
+ if (!audio_delegate_->IsOutputAudioMuted() &&
+ level <= audio_delegate_->GetOutputDefaultVolumeMuteLevel()) {
+ audio_delegate_->SetOutputAudioIsMuted(true);
+ } else if (audio_delegate_->IsOutputAudioMuted() &&
+ level > audio_delegate_->GetOutputDefaultVolumeMuteLevel()) {
+ audio_delegate_->SetOutputAudioIsMuted(false);
+ }
+}
+
+void VolumeView::Layout() {
+ views::View::Layout();
+
+ if (!more_->visible()) {
+ int w = width() - slider_->bounds().x() -
+ kSliderRightPaddingToVolumeViewEdge;
+ slider_->SetSize(gfx::Size(w, slider_->height()));
+ return;
+ }
+
+ // Make sure the chevron always has the full size.
+ gfx::Size size = more_->GetPreferredSize();
+ gfx::Rect bounds(size);
+ bounds.set_x(width() - size.width() - kTrayPopupPaddingBetweenItems);
+ bounds.set_y((height() - size.height()) / 2);
+ more_->SetBoundsRect(bounds);
+
+ // Layout either bar_ or device_type_ at the left of the more_ button.
+ views::View* view_left_to_more;
+ if (device_type_->visible())
+ view_left_to_more = device_type_;
+ else
+ view_left_to_more = bar_;
+ gfx::Size view_size = view_left_to_more->GetPreferredSize();
+ gfx::Rect view_bounds(view_size);
+ view_bounds.set_x(more_->bounds().x() - view_size.width() -
+ kExtraPaddingBetweenBarAndMore);
+ view_bounds.set_y((height() - view_size.height()) / 2);
+ view_left_to_more->SetBoundsRect(view_bounds);
+
+ // Layout vertical bar next to view_left_to_more if device_type_ is visible.
+ if (device_type_->visible()) {
+ gfx::Size bar_size = bar_->GetPreferredSize();
+ gfx::Rect bar_bounds(bar_size);
+ bar_bounds.set_x(view_left_to_more->bounds().x() - bar_size.width());
+ bar_bounds.set_y((height() - bar_size.height()) / 2);
+ bar_->SetBoundsRect(bar_bounds);
+ }
+
+ // Layout slider, calculate slider width.
+ gfx::Rect slider_bounds = slider_->bounds();
+ slider_bounds.set_width(
+ bar_->bounds().x()
+ - (device_type_->visible() ? 0 : kTrayPopupPaddingBetweenItems)
+ - slider_bounds.x());
+ slider_->SetBoundsRect(slider_bounds);
+}
+
+void VolumeView::ButtonPressed(views::Button* sender, const ui::Event& event) {
+ CHECK(sender == icon_);
+ bool mute_on = !audio_delegate_->IsOutputAudioMuted();
+ audio_delegate_->SetOutputAudioIsMuted(mute_on);
+ if (!mute_on)
+ audio_delegate_->AdjustOutputVolumeToAudibleLevel();
+ icon_->Update();
+}
+
+void VolumeView::SliderValueChanged(views::Slider* sender,
+ float value,
+ float old_value,
+ views::SliderChangeReason reason) {
+ if (reason == views::VALUE_CHANGED_BY_USER) {
+ float new_volume = value * 100.0f;
+ float current_volume = audio_delegate_->GetOutputVolumeLevel();
+ // Do not call change audio volume if the difference is less than
+ // 1%, which is beyond cras audio api's granularity for output volume.
+ if (std::abs(new_volume - current_volume) < 1.0f)
+ return;
+ Shell::GetInstance()->metrics()->RecordUserMetricsAction(
+ is_default_view_ ?
+ ash::UMA_STATUS_AREA_CHANGED_VOLUME_MENU :
+ ash::UMA_STATUS_AREA_CHANGED_VOLUME_POPUP);
+ if (new_volume > current_volume)
+ HandleVolumeUp(new_volume);
+ else
+ HandleVolumeDown(new_volume);
+ }
+ icon_->Update();
+}
+
+bool VolumeView::PerformAction(const ui::Event& event) {
+ if (!more_->visible())
+ return false;
+ owner_->TransitionDetailedView();
+ return true;
+}
+
+} // namespace tray
+} // namespace internal
+} // namespace ash
« no previous file with comments | « ash/system/audio/volume_view.h ('k') | ash/system/chromeos/audio/audio_detailed_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698