Chromium Code Reviews| Index: chrome/browser/chromeos/status/volume_menu_button.cc |
| =================================================================== |
| --- chrome/browser/chromeos/status/volume_menu_button.cc (revision 119435) |
| +++ chrome/browser/chromeos/status/volume_menu_button.cc (working copy) |
| @@ -6,10 +6,12 @@ |
| #include <algorithm> |
| +#include "base/command_line.h" |
| #include "base/string_number_conversions.h" |
| -#include "chrome/browser/chromeos/audio/audio_handler.h" |
| #include "chrome/browser/chromeos/status/status_area_bubble.h" |
| +#include "chrome/browser/chromeos/system/runtime_environment.h" |
| #include "chrome/browser/chromeos/view_ids.h" |
| +#include "chrome/common/chrome_switches.h" |
| #include "grit/generated_resources.h" |
| #include "grit/theme_resources.h" |
| #include "ui/base/l10n/l10n_util.h" |
| @@ -21,37 +23,50 @@ |
| #include "ui/views/controls/menu/menu_runner.h" |
| #include "ui/views/controls/menu/submenu_view.h" |
| +namespace chromeos { |
| + |
| namespace { |
| -static const int kMenuItemId = 100; // arbitrary menu id. |
| +const int kMenuItemId = 100; // arbitrary menu id. |
| // TODO(achuith): Minimum width of MenuItemView is 27, which is too wide. |
| -static const int kVolumeMenuWidth = 27; |
| -static const int kVolumeIconWidth = 20; |
| +const int kVolumeMenuWidth = 27; |
| +const int kVolumeIconWidth = 20; |
| +bool ShouldShowStatusAreaVolume() { |
| + return CommandLine::ForCurrentProcess()-> |
| + HasSwitch(switches::kShowVolumeStatus); |
| +} |
| + |
| //////////////////////////////////////////////////////////////////////////////// |
| // AudioHandler helpers |
| // Used when not running on a ChromeOS device. |
| static int g_volume_percent = 0; |
| -chromeos::AudioHandler* GetAudioHandler() { |
| - chromeos::AudioHandler* audio_handler = chromeos::AudioHandler::GetInstance(); |
| - return audio_handler && audio_handler->IsInitialized() ? |
| - audio_handler : NULL; |
| -} |
| - |
| int GetVolumePercent() { |
| - chromeos::AudioHandler* audio_handler = GetAudioHandler(); |
| - return audio_handler ? audio_handler->GetVolumePercent() : g_volume_percent; |
| + AudioHandler* audio_handler = AudioHandler::GetInstanceIfInitialized(); |
| + if (audio_handler) |
| + return audio_handler->IsMuted() ? 0 : audio_handler->GetVolumePercent(); |
| + return g_volume_percent; |
| } |
| void SetVolumePercent(int percent) { |
| - chromeos::AudioHandler* audio_handler = GetAudioHandler(); |
| + AudioHandler* audio_handler = AudioHandler::GetInstanceIfInitialized(); |
| if (audio_handler) |
| audio_handler->SetVolumePercent(percent); |
| g_volume_percent = percent; |
| } |
| +void AddVolumeObserver(AudioHandler::VolumeObserver* volume_observer) { |
| + if (system::runtime_environment::IsRunningOnChromeOS()) |
| + AudioHandler::GetInstance()->AddVolumeObserver(volume_observer); |
| +} |
| + |
| +void RemoveVolumeObserver(AudioHandler::VolumeObserver* volume_observer) { |
| + if (system::runtime_environment::IsRunningOnChromeOS()) |
| + AudioHandler::GetInstance()->RemoveVolumeObserver(volume_observer); |
| +} |
| + |
| //////////////////////////////////////////////////////////////////////////////// |
| // SkBitmap helpers |
| @@ -77,10 +92,13 @@ |
| //////////////////////////////////////////////////////////////////////////////// |
| // VolumeControlView |
| -class VolumeControlView : public views::View { |
| +class VolumeControlView : public views::View, |
| + public AudioHandler::VolumeObserver { |
| public: |
| - explicit VolumeControlView(chromeos::VolumeMenuButton* volume_menu_button); |
| + explicit VolumeControlView(VolumeMenuButton* volume_menu_button); |
|
Daniel Erat
2012/01/31 01:06:02
nit: delete blank line
achuithb
2012/01/31 10:47:32
Done.
|
| + virtual ~VolumeControlView(); |
| + |
| private: |
| // views::View overrides: |
| virtual gfx::Size GetPreferredSize() OVERRIDE; |
| @@ -88,8 +106,11 @@ |
| virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE; |
| virtual bool OnMouseDragged(const views::MouseEvent& event) OVERRIDE; |
| - chromeos::VolumeMenuButton* volume_menu_button_; |
| + // AudioHandler::VolumeObserver overrides: |
| + virtual void OnVolumeChanged() OVERRIDE; |
| + VolumeMenuButton* volume_menu_button_; |
|
Daniel Erat
2012/01/31 01:06:02
nit: add "// not owned" to the end of the line if
achuithb
2012/01/31 10:47:32
Done.
|
| + |
| const SkBitmap* slider_empty_; |
| const SkBitmap* slider_full_; |
| const SkBitmap* thumb_; |
| @@ -102,7 +123,7 @@ |
| }; |
| VolumeControlView::VolumeControlView( |
| - chromeos::VolumeMenuButton* volume_menu_button) |
| + VolumeMenuButton* volume_menu_button) |
| : volume_menu_button_(volume_menu_button), |
| slider_empty_(GetImageNamed(IDR_STATUSBAR_VOLUME_SLIDER_EMPTY)), |
| slider_full_(GetImageNamed(IDR_STATUSBAR_VOLUME_SLIDER_FULL)), |
| @@ -111,8 +132,13 @@ |
| slider_h_(slider_empty_->height()), |
| thumb_h_(thumb_->height()) { |
| DCHECK_EQ(slider_w_, slider_full_->width()); |
| + AddVolumeObserver(this); |
| } |
| +VolumeControlView::~VolumeControlView() { |
| + RemoveVolumeObserver(this); |
| +} |
| + |
| gfx::Size VolumeControlView::GetPreferredSize() { |
| return gfx::Size(kVolumeMenuWidth, slider_h_ + thumb_h_); |
| } |
| @@ -161,10 +187,12 @@ |
| return true; |
| } |
| +void VolumeControlView::OnVolumeChanged() { |
| + SchedulePaint(); |
| +} |
| + |
| } // namespace |
| -namespace chromeos { |
| - |
| //////////////////////////////////////////////////////////////////////////////// |
| // VolumeMenuButton |
| @@ -172,12 +200,12 @@ |
| : StatusAreaButton(delegate, this) { |
| set_id(VIEW_ID_STATUS_BUTTON_VOLUME); |
| UpdateIcon(); |
| - // TODO(achuith): Query SystemKeyEventListener to determine when we |
| - // can show statusbar volume controls. |
| - SetVisible(false); |
| + SetVisible(ShouldShowStatusAreaVolume()); |
| + AddVolumeObserver(this); |
| } |
| VolumeMenuButton::~VolumeMenuButton() { |
| + RemoveVolumeObserver(this); |
| } |
| int VolumeMenuButton::icon_width() { |
| @@ -201,6 +229,10 @@ |
| UpdateIcon(); |
| } |
| +void VolumeMenuButton::OnVolumeChanged() { |
| + UpdateIcon(); |
| +} |
| + |
| void VolumeMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
| // TODO(achuith): Minimum width of MenuItemView is 27 pix which is too wide |
| // for our purposes here. |