summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/status/volume_menu_button.cc
blob: e67295eb20097dbd5e21496ffba3984609a153f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (c) 2012 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 "chrome/browser/chromeos/status/volume_menu_button.h"

#include <algorithm>

#include "base/command_line.h"
#include "base/string_number_conversions.h"
#include "chrome/browser/chromeos/status/status_area_bubble.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"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/controls/menu/submenu_view.h"

namespace chromeos {

namespace {

const int kMenuItemId = 100;  // arbitrary menu id.
// TODO(achuith): Minimum width of MenuItemView is 27, which is too wide.
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;

int GetVolumePercent() {
  AudioHandler* audio_handler = AudioHandler::GetInstance();
  if (audio_handler)
    return audio_handler->IsMuted() ? 0 : audio_handler->GetVolumePercent();
  return g_volume_percent;
}

void SetVolumePercent(int percent) {
  AudioHandler* audio_handler = AudioHandler::GetInstance();
  if (audio_handler)
    audio_handler->SetVolumePercent(percent);
  g_volume_percent = percent;
}

void AddVolumeObserver(AudioHandler::VolumeObserver* volume_observer) {
  AudioHandler::GetInstance()->AddVolumeObserver(volume_observer);
}

void RemoveVolumeObserver(AudioHandler::VolumeObserver* volume_observer) {
  if (AudioHandler::GetInstance())
    AudioHandler::GetInstance()->RemoveVolumeObserver(volume_observer);
}

////////////////////////////////////////////////////////////////////////////////
// SkBitmap helpers

const SkBitmap* GetImageNamed(int image_index) {
  return ResourceBundle::GetSharedInstance().
      GetImageNamed(image_index).ToSkBitmap();
}

const SkBitmap* GetIcon() {
  const int volume_percent = GetVolumePercent();
  int image_index = IDR_STATUSBAR_VOLUME_ICON_MUTE;

  if (volume_percent > 67)
    image_index = IDR_STATUSBAR_VOLUME_ICON3;
  else if (volume_percent > 33)
    image_index = IDR_STATUSBAR_VOLUME_ICON2;
  else if (volume_percent > 0)
    image_index = IDR_STATUSBAR_VOLUME_ICON1;

  return GetImageNamed(image_index);
}

////////////////////////////////////////////////////////////////////////////////
// VolumeControlView

class VolumeControlView : public views::View,
                          public AudioHandler::VolumeObserver {
 public:
  explicit VolumeControlView(VolumeMenuButton* volume_menu_button);
  virtual ~VolumeControlView();

 private:
  // views::View overrides:
  virtual gfx::Size GetPreferredSize() OVERRIDE;
  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
  virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE;
  virtual bool OnMouseDragged(const views::MouseEvent& event) OVERRIDE;

  // AudioHandler::VolumeObserver overrides:
  virtual void OnVolumeChanged() OVERRIDE;

  VolumeMenuButton* volume_menu_button_;  // not owned.

  const SkBitmap* slider_empty_;
  const SkBitmap* slider_full_;
  const SkBitmap* thumb_;

  int slider_w_;
  int slider_h_;
  int thumb_h_;

  DISALLOW_COPY_AND_ASSIGN(VolumeControlView);
};

VolumeControlView::VolumeControlView(
    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)),
      thumb_(GetImageNamed(IDR_STATUSBAR_VOLUME_SLIDER_THUMB)),
      slider_w_(slider_empty_->width()),
      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_);
}

void VolumeControlView::OnPaint(gfx::Canvas* canvas) {
  const int slider_x = (width() - slider_w_) / 2;
  const int thumb_x = (width() - thumb_->width()) / 2;
  const int slider_empty_y = thumb_->height() / 2.0;

  const int slider_empty_h = slider_h_ * (100 - GetVolumePercent()) / 100;
  const int slider_full_y = slider_empty_h + slider_empty_y;
  const int slider_full_h = slider_h_ - slider_empty_h;

  const int thumb_y = slider_empty_h;

  if (slider_empty_h != 0) {
    canvas->DrawBitmapInt(*slider_empty_,
                          0, 0, slider_w_, slider_empty_h,
                          slider_x, slider_empty_y, slider_w_, slider_empty_h,
                          false);
  }

  if (slider_full_h != 0) {
    canvas->DrawBitmapInt(*slider_full_,
                          0, slider_empty_h, slider_w_, slider_full_h,
                          slider_x, slider_full_y, slider_w_, slider_full_h,
                          false);
  }

  canvas->DrawBitmapInt(*thumb_, thumb_x, thumb_y);
}

bool VolumeControlView::OnMousePressed(const views::MouseEvent& event) {
  return OnMouseDragged(event);
}

bool VolumeControlView::OnMouseDragged(const views::MouseEvent& event) {
  const int slider_empty_y = thumb_->height() / 2.0;
  const int new_volume = 100 - (std::max(std::min((event.y() - slider_empty_y),
      slider_h_), 0) * 100 / slider_h_);
  if (new_volume != GetVolumePercent()) {
    SetVolumePercent(new_volume);
    SchedulePaint();
    volume_menu_button_->UpdateIcon();
  }
  return true;
}

void VolumeControlView::OnVolumeChanged() {
  SchedulePaint();
}

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// VolumeMenuButton

VolumeMenuButton::VolumeMenuButton(StatusAreaButton::Delegate* delegate)
    : StatusAreaButton(delegate, this) {
  set_id(VIEW_ID_STATUS_BUTTON_VOLUME);
  UpdateIcon();
  SetVisible(ShouldShowStatusAreaVolume());
  AddVolumeObserver(this);
}

VolumeMenuButton::~VolumeMenuButton() {
  RemoveVolumeObserver(this);
}

int VolumeMenuButton::icon_width() {
  return kVolumeIconWidth;
}

void VolumeMenuButton::UpdateIcon() {
  const int volume_percent = GetVolumePercent();
  string16 tooltip_text = (volume_percent == 0)
      ? l10n_util::GetStringUTF16(IDS_STATUSBAR_VOLUME_MUTE)
      : l10n_util::GetStringFUTF16(IDS_STATUSBAR_VOLUME_PERCENTAGE,
                                   base::IntToString16(volume_percent));
  SetTooltipText(tooltip_text);
  SetAccessibleName(tooltip_text);

  SetIcon(*GetIcon());
  SchedulePaint();
}

void VolumeMenuButton::OnLocaleChanged() {
  UpdateIcon();
}

void VolumeMenuButton::OnVolumeChanged() {
  UpdateIcon();
}

void VolumeMenuButton::OnMenuButtonClicked(views::View* source,
                                           const gfx::Point& point) {
  // TODO(achuith): Minimum width of MenuItemView is 27 pix which is too wide
  // for our purposes here.
  views::MenuItemView* menu = new views::MenuItemView(this);
  // MenuRunner takes ownership of |menu|.
  views::MenuRunner* menu_runner = new views::MenuRunner(menu);
  views::MenuItemView* submenu = menu->AppendMenuItem(
      kMenuItemId,
      string16(),
      views::MenuItemView::NORMAL);
  submenu->AddChildView(new StatusAreaBubbleContentView(
      new VolumeControlView(this), string16()));
  menu->CreateSubmenu()->set_resize_open_menu(true);
  menu->SetMargins(0, 0);
  submenu->SetMargins(0, 0);
  menu->ChildrenChanged();

  gfx::Point screen_location;
  views::View::ConvertPointToScreen(source, &screen_location);
  gfx::Rect bounds(screen_location, source->size());

  views::MenuRunner::RunResult result = menu_runner->RunMenuAt(
      source->GetWidget()->GetTopLevelWidget(),
      this,
      bounds,
      views::MenuItemView::TOPRIGHT,
      views::MenuRunner::HAS_MNEMONICS);

  if (result != views::MenuRunner::MENU_DELETED)
    delete menu_runner;
}

}  // namespace chromeos