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

Side by Side Diff: chrome/browser/chromeos/status/volume_menu_button.cc

Issue 10056001: chromeos: Remove old status-area related code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 8 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "chrome/browser/chromeos/status/volume_menu_button.h"
6
7 #include <algorithm>
8
9 #include "base/command_line.h"
10 #include "base/string_number_conversions.h"
11 #include "chrome/browser/chromeos/status/status_area_bubble.h"
12 #include "chrome/browser/chromeos/view_ids.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "grit/generated_resources.h"
15 #include "grit/theme_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/image/image.h"
20 #include "ui/views/controls/menu/menu_item_view.h"
21 #include "ui/views/controls/menu/menu_runner.h"
22 #include "ui/views/controls/menu/submenu_view.h"
23
24 namespace chromeos {
25
26 namespace {
27
28 const int kMenuItemId = 100; // arbitrary menu id.
29 // TODO(achuith): Minimum width of MenuItemView is 27, which is too wide.
30 const int kVolumeMenuWidth = 27;
31 const int kVolumeIconWidth = 20;
32
33 bool ShouldShowStatusAreaVolume() {
34 return CommandLine::ForCurrentProcess()->
35 HasSwitch(switches::kShowVolumeStatus);
36 }
37
38 ////////////////////////////////////////////////////////////////////////////////
39 // AudioHandler helpers
40
41 // Used when not running on a ChromeOS device.
42 static int g_volume_percent = 0;
43
44 int GetVolumePercent() {
45 AudioHandler* audio_handler = AudioHandler::GetInstance();
46 if (audio_handler)
47 return audio_handler->IsMuted() ? 0 : audio_handler->GetVolumePercent();
48 return g_volume_percent;
49 }
50
51 void SetVolumePercent(int percent) {
52 AudioHandler* audio_handler = AudioHandler::GetInstance();
53 if (audio_handler)
54 audio_handler->SetVolumePercent(percent);
55 g_volume_percent = percent;
56 }
57
58 void AddVolumeObserver(AudioHandler::VolumeObserver* volume_observer) {
59 AudioHandler::GetInstance()->AddVolumeObserver(volume_observer);
60 }
61
62 void RemoveVolumeObserver(AudioHandler::VolumeObserver* volume_observer) {
63 if (AudioHandler::GetInstance())
64 AudioHandler::GetInstance()->RemoveVolumeObserver(volume_observer);
65 }
66
67 ////////////////////////////////////////////////////////////////////////////////
68 // SkBitmap helpers
69
70 const SkBitmap* GetImageNamed(int image_index) {
71 return ResourceBundle::GetSharedInstance().
72 GetImageNamed(image_index).ToSkBitmap();
73 }
74
75 const SkBitmap* GetIcon() {
76 const int volume_percent = GetVolumePercent();
77 int image_index = IDR_STATUSBAR_VOLUME_ICON_MUTE;
78
79 if (volume_percent > 67)
80 image_index = IDR_STATUSBAR_VOLUME_ICON3;
81 else if (volume_percent > 33)
82 image_index = IDR_STATUSBAR_VOLUME_ICON2;
83 else if (volume_percent > 0)
84 image_index = IDR_STATUSBAR_VOLUME_ICON1;
85
86 return GetImageNamed(image_index);
87 }
88
89 ////////////////////////////////////////////////////////////////////////////////
90 // VolumeControlView
91
92 class VolumeControlView : public views::View,
93 public AudioHandler::VolumeObserver {
94 public:
95 explicit VolumeControlView(VolumeMenuButton* volume_menu_button);
96 virtual ~VolumeControlView();
97
98 private:
99 // views::View overrides:
100 virtual gfx::Size GetPreferredSize() OVERRIDE;
101 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
102 virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE;
103 virtual bool OnMouseDragged(const views::MouseEvent& event) OVERRIDE;
104
105 // AudioHandler::VolumeObserver overrides:
106 virtual void OnVolumeChanged() OVERRIDE;
107
108 VolumeMenuButton* volume_menu_button_; // not owned.
109
110 const SkBitmap* slider_empty_;
111 const SkBitmap* slider_full_;
112 const SkBitmap* thumb_;
113
114 int slider_w_;
115 int slider_h_;
116 int thumb_h_;
117
118 DISALLOW_COPY_AND_ASSIGN(VolumeControlView);
119 };
120
121 VolumeControlView::VolumeControlView(
122 VolumeMenuButton* volume_menu_button)
123 : volume_menu_button_(volume_menu_button),
124 slider_empty_(GetImageNamed(IDR_STATUSBAR_VOLUME_SLIDER_EMPTY)),
125 slider_full_(GetImageNamed(IDR_STATUSBAR_VOLUME_SLIDER_FULL)),
126 thumb_(GetImageNamed(IDR_STATUSBAR_VOLUME_SLIDER_THUMB)),
127 slider_w_(slider_empty_->width()),
128 slider_h_(slider_empty_->height()),
129 thumb_h_(thumb_->height()) {
130 DCHECK_EQ(slider_w_, slider_full_->width());
131 AddVolumeObserver(this);
132 }
133
134 VolumeControlView::~VolumeControlView() {
135 RemoveVolumeObserver(this);
136 }
137
138 gfx::Size VolumeControlView::GetPreferredSize() {
139 return gfx::Size(kVolumeMenuWidth, slider_h_ + thumb_h_);
140 }
141
142 void VolumeControlView::OnPaint(gfx::Canvas* canvas) {
143 const int slider_x = (width() - slider_w_) / 2;
144 const int thumb_x = (width() - thumb_->width()) / 2;
145 const int slider_empty_y = thumb_->height() / 2.0;
146
147 const int slider_empty_h = slider_h_ * (100 - GetVolumePercent()) / 100;
148 const int slider_full_y = slider_empty_h + slider_empty_y;
149 const int slider_full_h = slider_h_ - slider_empty_h;
150
151 const int thumb_y = slider_empty_h;
152
153 if (slider_empty_h != 0) {
154 canvas->DrawBitmapInt(*slider_empty_,
155 0, 0, slider_w_, slider_empty_h,
156 slider_x, slider_empty_y, slider_w_, slider_empty_h,
157 false);
158 }
159
160 if (slider_full_h != 0) {
161 canvas->DrawBitmapInt(*slider_full_,
162 0, slider_empty_h, slider_w_, slider_full_h,
163 slider_x, slider_full_y, slider_w_, slider_full_h,
164 false);
165 }
166
167 canvas->DrawBitmapInt(*thumb_, thumb_x, thumb_y);
168 }
169
170 bool VolumeControlView::OnMousePressed(const views::MouseEvent& event) {
171 return OnMouseDragged(event);
172 }
173
174 bool VolumeControlView::OnMouseDragged(const views::MouseEvent& event) {
175 const int slider_empty_y = thumb_->height() / 2.0;
176 const int new_volume = 100 - (std::max(std::min((event.y() - slider_empty_y),
177 slider_h_), 0) * 100 / slider_h_);
178 if (new_volume != GetVolumePercent()) {
179 SetVolumePercent(new_volume);
180 SchedulePaint();
181 volume_menu_button_->UpdateIcon();
182 }
183 return true;
184 }
185
186 void VolumeControlView::OnVolumeChanged() {
187 SchedulePaint();
188 }
189
190 } // namespace
191
192 ////////////////////////////////////////////////////////////////////////////////
193 // VolumeMenuButton
194
195 VolumeMenuButton::VolumeMenuButton(StatusAreaButton::Delegate* delegate)
196 : StatusAreaButton(delegate, this) {
197 set_id(VIEW_ID_STATUS_BUTTON_VOLUME);
198 UpdateIcon();
199 SetVisible(ShouldShowStatusAreaVolume());
200 AddVolumeObserver(this);
201 }
202
203 VolumeMenuButton::~VolumeMenuButton() {
204 RemoveVolumeObserver(this);
205 }
206
207 int VolumeMenuButton::icon_width() {
208 return kVolumeIconWidth;
209 }
210
211 void VolumeMenuButton::UpdateIcon() {
212 const int volume_percent = GetVolumePercent();
213 string16 tooltip_text = (volume_percent == 0)
214 ? l10n_util::GetStringUTF16(IDS_STATUSBAR_VOLUME_MUTE)
215 : l10n_util::GetStringFUTF16(IDS_STATUSBAR_VOLUME_PERCENTAGE,
216 base::IntToString16(volume_percent));
217 SetTooltipText(tooltip_text);
218 SetAccessibleName(tooltip_text);
219
220 SetIcon(*GetIcon());
221 SchedulePaint();
222 }
223
224 void VolumeMenuButton::OnLocaleChanged() {
225 UpdateIcon();
226 }
227
228 void VolumeMenuButton::OnVolumeChanged() {
229 UpdateIcon();
230 }
231
232 void VolumeMenuButton::OnMenuButtonClicked(views::View* source,
233 const gfx::Point& point) {
234 // TODO(achuith): Minimum width of MenuItemView is 27 pix which is too wide
235 // for our purposes here.
236 views::MenuItemView* menu = new views::MenuItemView(this);
237 // MenuRunner takes ownership of |menu|.
238 views::MenuRunner* menu_runner = new views::MenuRunner(menu);
239 views::MenuItemView* submenu = menu->AppendMenuItem(
240 kMenuItemId,
241 string16(),
242 views::MenuItemView::NORMAL);
243 submenu->AddChildView(new StatusAreaBubbleContentView(
244 new VolumeControlView(this), string16()));
245 menu->CreateSubmenu()->set_resize_open_menu(true);
246 menu->SetMargins(0, 0);
247 submenu->SetMargins(0, 0);
248 menu->ChildrenChanged();
249
250 gfx::Point screen_location;
251 views::View::ConvertPointToScreen(source, &screen_location);
252 gfx::Rect bounds(screen_location, source->size());
253
254 views::MenuRunner::RunResult result = menu_runner->RunMenuAt(
255 source->GetWidget()->GetTopLevelWidget(),
256 this,
257 bounds,
258 views::MenuItemView::TOPRIGHT,
259 views::MenuRunner::HAS_MNEMONICS);
260
261 if (result != views::MenuRunner::MENU_DELETED)
262 delete menu_runner;
263 }
264
265 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/status/volume_menu_button.h ('k') | chrome/browser/chromeos/system/ash_system_tray_delegate.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698