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

Side by Side Diff: chrome/browser/ui/views/browser_action_view.cc

Issue 10533086: Action box menu (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Action box menu Created 8 years, 4 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/views/browser_action_view.h" 5 #include "chrome/browser/ui/views/browser_action_view.h"
6 6
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/extensions/api/commands/command_service.h" 8 #include "chrome/browser/extensions/api/commands/command_service.h"
9 #include "chrome/browser/extensions/api/commands/command_service_factory.h" 9 #include "chrome/browser/extensions/api/commands/command_service_factory.h"
10 #include "chrome/browser/extensions/extension_context_menu_model.h" 10 #include "chrome/browser/extensions/extension_context_menu_model.h"
(...skipping 23 matching lines...) Expand all
34 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height()); 34 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height());
35 alpha.allocPixels(); 35 alpha.allocPixels();
36 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); 36 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0));
37 37
38 return SkBitmapOperations::CreateMaskedBitmap(image, alpha); 38 return SkBitmapOperations::CreateMaskedBitmap(image, alpha);
39 } 39 }
40 40
41 } // namespace 41 } // namespace
42 42
43 //////////////////////////////////////////////////////////////////////////////// 43 ////////////////////////////////////////////////////////////////////////////////
44 // BrowserActionView
45
46 bool BrowserActionView::Delegate::NeedToShowMultipleIconStates() const {
47 return true;
48 }
49
50 bool BrowserActionView::Delegate::NeedToShowTooltip() const {
51 return true;
52 }
53
54 BrowserActionView::BrowserActionView(const Extension* extension,
55 Browser* browser,
56 BrowserActionView::Delegate* delegate)
57 : browser_(browser),
58 delegate_(delegate),
59 button_(NULL),
60 extension_(extension) {
61 }
62
63 BrowserActionView::~BrowserActionView() {
64 }
65
66 gfx::Canvas* BrowserActionView::GetIconWithBadge() {
67 int tab_id = delegate_->GetCurrentTabId();
68
69 SkBitmap icon = *button_->extension()->browser_action()->GetIcon(
Peter Kasting 2012/08/07 01:06:49 Nit: Break after '=' instead
70 tab_id).ToSkBitmap();
71
72 // Dim the icon if our button is disabled.
73 if (!button_->IsEnabled(tab_id))
74 icon = MakeTransparent(icon);
75
76 gfx::Canvas* canvas =
77 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false);
78
79 if (tab_id >= 0) {
80 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing);
81 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id);
82 }
83
84 return canvas;
85 }
86
87 void BrowserActionView::Layout() {
88 // We can't rely on button_->GetPreferredSize() here because that's not set
89 // correctly until the first call to
90 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be
91 // called before that when the initial bounds are set (and then not after,
92 // since the bounds don't change). So instead of setting the height from the
93 // button's preferred size, we use IconHeight(), since that's how big the
94 // button should be regardless of what it's displaying.
95 gfx::Point offset = delegate_->GetViewContentOffset();
96 button_->SetBounds(offset.x(), offset.y(), width() - offset.x(),
97 BrowserActionsContainer::IconHeight());
98 }
99
100 void BrowserActionView::ViewHierarchyChanged(bool is_add,
101 View* parent,
102 View* child) {
103 if (is_add && (child == this)) {
104 button_ = new BrowserActionButton(extension_, browser_, delegate_);
105 button_->set_drag_controller(delegate_);
106
107 AddChildView(button_);
108 button_->UpdateState();
109 }
110 }
111
112 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) {
113 state->name = l10n_util::GetStringUTF16(
114 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION);
115 state->role = ui::AccessibilityTypes::ROLE_GROUPING;
116 }
117
118 gfx::Size BrowserActionView::GetPreferredSize() {
119 return gfx::Size(BrowserActionsContainer::IconWidth(false),
120 BrowserActionsContainer::IconHeight());
121 }
122
123 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) {
124 View::PaintChildren(canvas);
125 ExtensionAction* action = button()->browser_action();
126 int tab_id = delegate_->GetCurrentTabId();
127 if (tab_id >= 0)
128 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id);
129 }
130
131 ////////////////////////////////////////////////////////////////////////////////
44 // BrowserActionButton 132 // BrowserActionButton
45 133
46 BrowserActionButton::BrowserActionButton(const Extension* extension, 134 BrowserActionButton::BrowserActionButton(const Extension* extension,
47 BrowserActionsContainer* panel) 135 Browser* browser,
136 BrowserActionView::Delegate* delegate)
48 : ALLOW_THIS_IN_INITIALIZER_LIST( 137 : ALLOW_THIS_IN_INITIALIZER_LIST(
49 MenuButton(this, string16(), NULL, false)), 138 MenuButton(this, string16(), NULL, false)),
139 browser_(browser),
50 browser_action_(extension->browser_action()), 140 browser_action_(extension->browser_action()),
51 extension_(extension), 141 extension_(extension),
52 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), 142 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)),
53 panel_(panel), 143 delegate_(delegate),
54 context_menu_(NULL) { 144 context_menu_(NULL) {
55 set_border(NULL); 145 set_border(NULL);
56 set_alignment(TextButton::ALIGN_CENTER); 146 set_alignment(TextButton::ALIGN_CENTER);
57 set_context_menu_controller(this); 147 set_context_menu_controller(this);
58 148
59 // No UpdateState() here because View hierarchy not setup yet. Our parent 149 // No UpdateState() here because View hierarchy not setup yet. Our parent
60 // should call UpdateState() after creation. 150 // should call UpdateState() after creation.
61 151
152 content::NotificationSource notification_source =
153 content::Source<Profile>(browser_->profile()->GetOriginalProfile());
62 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, 154 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED,
63 content::Source<ExtensionAction>(browser_action_)); 155 content::Source<ExtensionAction>(browser_action_));
64 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, 156 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED,
65 content::Source<Profile>( 157 notification_source);
66 panel_->profile()->GetOriginalProfile()));
67 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, 158 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED,
68 content::Source<Profile>( 159 notification_source);
69 panel_->profile()->GetOriginalProfile()));
70 } 160 }
71 161
72 void BrowserActionButton::Destroy() { 162 void BrowserActionButton::Destroy() {
73 MaybeUnregisterExtensionCommand(false); 163 MaybeUnregisterExtensionCommand(false);
74 164
75 if (context_menu_) { 165 if (context_menu_) {
76 context_menu_->Cancel(); 166 context_menu_->Cancel();
77 MessageLoop::current()->DeleteSoon(FROM_HERE, this); 167 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
78 } else { 168 } else {
79 delete this; 169 delete this;
(...skipping 30 matching lines...) Expand all
110 return true; 200 return true;
111 } 201 }
112 202
113 void BrowserActionButton::GetAccessibleState(ui::AccessibleViewState* state) { 203 void BrowserActionButton::GetAccessibleState(ui::AccessibleViewState* state) {
114 views::MenuButton::GetAccessibleState(state); 204 views::MenuButton::GetAccessibleState(state);
115 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON; 205 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
116 } 206 }
117 207
118 void BrowserActionButton::ButtonPressed(views::Button* sender, 208 void BrowserActionButton::ButtonPressed(views::Button* sender,
119 const views::Event& event) { 209 const views::Event& event) {
120 panel_->OnBrowserActionExecuted(this); 210 delegate_->OnBrowserActionExecuted(this);
121 } 211 }
122 212
123 void BrowserActionButton::ShowContextMenuForView(View* source, 213 void BrowserActionButton::ShowContextMenuForView(View* source,
124 const gfx::Point& point) { 214 const gfx::Point& point) {
125 if (!extension()->ShowConfigureContextMenus()) 215 if (!extension()->ShowConfigureContextMenus())
126 return; 216 return;
127 217
128 SetButtonPushed(); 218 SetButtonPushed();
129 219
130 // Reconstructs the menu every time because the menu's contents are dynamic. 220 // Reconstructs the menu every time because the menu's contents are dynamic.
131 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_( 221 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_(
132 new ExtensionContextMenuModel(extension(), panel_->browser())); 222 new ExtensionContextMenuModel(extension(), browser_));
133 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get()); 223 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get());
134 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); 224 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu()));
135 225
136 context_menu_ = menu_runner.GetMenu(); 226 context_menu_ = menu_runner_->GetMenu();
137 gfx::Point screen_loc; 227 gfx::Point screen_loc;
138 views::View::ConvertPointToScreen(this, &screen_loc); 228 views::View::ConvertPointToScreen(this, &screen_loc);
139 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()), 229 if (menu_runner_->RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()),
140 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) == 230 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) ==
141 views::MenuRunner::MENU_DELETED) 231 views::MenuRunner::MENU_DELETED) {
142 return; 232 return;
233 }
143 234
235 menu_runner_.reset();
144 SetButtonNotPushed(); 236 SetButtonNotPushed();
145 context_menu_ = NULL; 237 context_menu_ = NULL;
146 } 238 }
147 239
148 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, 240 void BrowserActionButton::OnImageLoaded(const gfx::Image& image,
149 const std::string& extension_id, 241 const std::string& extension_id,
150 int index) { 242 int index) {
151 browser_action_->CacheIcon(browser_action_->default_icon_path(), image); 243 browser_action_->CacheIcon(browser_action_->default_icon_path(), image);
152 244
153 // Call back to UpdateState() because a more specific icon might have been set 245 // Call back to UpdateState() because a more specific icon might have been set
154 // while the load was outstanding. 246 // while the load was outstanding.
155 UpdateState(); 247 UpdateState();
156 } 248 }
157 249
158 void BrowserActionButton::UpdateState() { 250 void BrowserActionButton::UpdateState() {
159 int tab_id = panel_->GetCurrentTabId(); 251 int tab_id = delegate_->GetCurrentTabId();
160 if (tab_id < 0) 252 if (tab_id < 0)
161 return; 253 return;
162 254
255 SetShowMultipleIconStates(delegate_->NeedToShowMultipleIconStates());
256
163 if (!IsEnabled(tab_id)) { 257 if (!IsEnabled(tab_id)) {
164 SetState(views::CustomButton::BS_DISABLED); 258 SetState(views::CustomButton::BS_DISABLED);
165 } else { 259 } else {
166 SetState(menu_visible_ ? 260 SetState(menu_visible_ ?
167 views::CustomButton::BS_PUSHED : 261 views::CustomButton::BS_PUSHED :
168 views::CustomButton::BS_NORMAL); 262 views::CustomButton::BS_NORMAL);
169 } 263 }
170 264
171 SkBitmap icon(*browser_action()->GetIcon(tab_id).ToSkBitmap()); 265 SkBitmap icon(*browser_action()->GetIcon(tab_id).ToSkBitmap());
172 if (!icon.isNull()) { 266 if (!icon.isNull()) {
(...skipping 24 matching lines...) Expand all
197 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p, 291 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p,
198 SkBitmap::kARGB_8888_Config); 292 SkBitmap::kARGB_8888_Config);
199 SkCanvas bg_p_canvas(bg_p); 293 SkCanvas bg_p_canvas(bg_p);
200 bg_p_canvas.drawBitmap(icon, 294 bg_p_canvas.drawBitmap(icon,
201 SkIntToScalar((bg_p.width() - icon.width()) / 2), 295 SkIntToScalar((bg_p.width() - icon.width()) / 2),
202 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); 296 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint);
203 SetPushedIcon(bg_p); 297 SetPushedIcon(bg_p);
204 } 298 }
205 299
206 // If the browser action name is empty, show the extension name instead. 300 // If the browser action name is empty, show the extension name instead.
207 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); 301 std::string title = browser_action()->GetTitle(tab_id);
208 if (name.empty()) 302 string16 name = UTF8ToUTF16(title.empty() ? extension()->name() : title);
209 name = UTF8ToUTF16(extension()->name()); 303 SetTooltipText(delegate_->NeedToShowTooltip() ? name : string16());
210 SetTooltipText(name);
211 SetAccessibleName(name); 304 SetAccessibleName(name);
212 305
213 parent()->SchedulePaint(); 306 parent()->SchedulePaint();
214 } 307 }
215 308
216 bool BrowserActionButton::IsPopup() { 309 bool BrowserActionButton::IsPopup() {
217 int tab_id = panel_->GetCurrentTabId(); 310 int tab_id = delegate_->GetCurrentTabId();
218 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); 311 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id);
219 } 312 }
220 313
221 GURL BrowserActionButton::GetPopupUrl() { 314 GURL BrowserActionButton::GetPopupUrl() {
222 int tab_id = panel_->GetCurrentTabId(); 315 int tab_id = delegate_->GetCurrentTabId();
223 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id); 316 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id);
224 } 317 }
225 318
226 void BrowserActionButton::Observe(int type, 319 void BrowserActionButton::Observe(int type,
227 const content::NotificationSource& source, 320 const content::NotificationSource& source,
228 const content::NotificationDetails& details) { 321 const content::NotificationDetails& details) {
229 switch (type) { 322 switch (type) {
230 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: 323 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED:
231 UpdateState(); 324 UpdateState();
232 // The browser action may have become visible/hidden so we need to make 325 // The browser action may have become visible/hidden so we need to make
233 // sure the state gets updated. 326 // sure the state gets updated.
234 panel_->OnBrowserActionVisibilityChanged(); 327 delegate_->OnBrowserActionVisibilityChanged();
235 break; 328 break;
236 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED: 329 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED:
237 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { 330 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: {
238 std::pair<const std::string, const std::string>* payload = 331 std::pair<const std::string, const std::string>* payload =
239 content::Details<std::pair<const std::string, const std::string> >( 332 content::Details<std::pair<const std::string, const std::string> >(
240 details).ptr(); 333 details).ptr();
241 if (extension_->id() == payload->first && 334 if (extension_->id() == payload->first &&
242 payload->second == 335 payload->second ==
243 extension_manifest_values::kBrowserActionCommandEvent) { 336 extension_manifest_values::kBrowserActionCommandEvent) {
244 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED) 337 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED)
245 MaybeRegisterExtensionCommand(); 338 MaybeRegisterExtensionCommand();
246 else 339 else
247 MaybeUnregisterExtensionCommand(true); 340 MaybeUnregisterExtensionCommand(true);
248 } 341 }
249 break; 342 break;
250 } 343 }
251 default: 344 default:
252 NOTREACHED(); 345 NOTREACHED();
253 break; 346 break;
254 } 347 }
255 } 348 }
256 349
257 bool BrowserActionButton::Activate() { 350 bool BrowserActionButton::Activate() {
258 if (!IsPopup()) 351 if (!IsPopup())
259 return true; 352 return true;
260 353
261 panel_->OnBrowserActionExecuted(this); 354 delegate_->OnBrowserActionExecuted(this);
262 355
263 // TODO(erikkay): Run a nested modal loop while the mouse is down to 356 // TODO(erikkay): Run a nested modal loop while the mouse is down to
264 // enable menu-like drag-select behavior. 357 // enable menu-like drag-select behavior.
265 358
266 // The return value of this method is returned via OnMousePressed. 359 // The return value of this method is returned via OnMousePressed.
267 // We need to return false here since we're handing off focus to another 360 // We need to return false here since we're handing off focus to another
268 // widget/view, and true will grab it right back and try to send events 361 // widget/view, and true will grab it right back and try to send events
269 // to us. 362 // to us.
270 return false; 363 return false;
271 } 364 }
(...skipping 28 matching lines...) Expand all
300 TextButton::OnMouseExited(event); 393 TextButton::OnMouseExited(event);
301 } 394 }
302 395
303 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& event) { 396 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& event) {
304 return IsPopup() ? MenuButton::OnKeyReleased(event) 397 return IsPopup() ? MenuButton::OnKeyReleased(event)
305 : TextButton::OnKeyReleased(event); 398 : TextButton::OnKeyReleased(event);
306 } 399 }
307 400
308 bool BrowserActionButton::AcceleratorPressed( 401 bool BrowserActionButton::AcceleratorPressed(
309 const ui::Accelerator& accelerator) { 402 const ui::Accelerator& accelerator) {
310 panel_->OnBrowserActionExecuted(this); 403 delegate_->OnBrowserActionExecuted(this);
311 return true; 404 return true;
312 } 405 }
313 406
314 void BrowserActionButton::SetButtonPushed() { 407 void BrowserActionButton::SetButtonPushed() {
315 SetState(views::CustomButton::BS_PUSHED); 408 SetState(views::CustomButton::BS_PUSHED);
316 menu_visible_ = true; 409 menu_visible_ = true;
317 } 410 }
318 411
319 void BrowserActionButton::SetButtonNotPushed() { 412 void BrowserActionButton::SetButtonNotPushed() {
320 SetState(views::CustomButton::BS_NORMAL); 413 SetState(views::CustomButton::BS_NORMAL);
321 menu_visible_ = false; 414 menu_visible_ = false;
322 } 415 }
323 416
324 bool BrowserActionButton::IsEnabled(int tab_id) const { 417 bool BrowserActionButton::IsEnabled(int tab_id) const {
325 return browser_action_->GetIsVisible(tab_id); 418 return browser_action_->GetIsVisible(tab_id);
326 } 419 }
327 420
328 BrowserActionButton::~BrowserActionButton() { 421 BrowserActionButton::~BrowserActionButton() {
329 } 422 }
330 423
331 void BrowserActionButton::MaybeRegisterExtensionCommand() { 424 void BrowserActionButton::MaybeRegisterExtensionCommand() {
332 extensions::CommandService* command_service = 425 extensions::CommandService* command_service =
333 extensions::CommandServiceFactory::GetForProfile( 426 extensions::CommandServiceFactory::GetForProfile(browser_->profile());
334 panel_->browser()->profile());
335 extensions::Command browser_action_command; 427 extensions::Command browser_action_command;
336 if (command_service->GetBrowserActionCommand( 428 if (command_service->GetBrowserActionCommand(
337 extension_->id(), 429 extension_->id(),
338 extensions::CommandService::ACTIVE_ONLY, 430 extensions::CommandService::ACTIVE_ONLY,
339 &browser_action_command, 431 &browser_action_command,
340 NULL)) { 432 NULL)) {
341 keybinding_.reset(new ui::Accelerator( 433 keybinding_.reset(new ui::Accelerator(
342 browser_action_command.accelerator())); 434 browser_action_command.accelerator()));
343 panel_->GetFocusManager()->RegisterAccelerator( 435 GetFocusManager()->RegisterAccelerator(
344 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this); 436 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this);
345 } 437 }
346 } 438 }
347 439
348 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) { 440 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) {
349 if (!keybinding_.get() || !panel_->GetFocusManager()) 441 if (!keybinding_.get() || !GetFocusManager())
350 return; 442 return;
351 443
352 extensions::CommandService* command_service = 444 extensions::CommandService* command_service =
353 extensions::CommandServiceFactory::GetForProfile( 445 extensions::CommandServiceFactory::GetForProfile(browser_->profile());
354 panel_->browser()->profile());
355 446
356 extensions::Command browser_action_command; 447 extensions::Command browser_action_command;
357 if (!only_if_active || !command_service->GetBrowserActionCommand( 448 if (!only_if_active || !command_service->GetBrowserActionCommand(
358 extension_->id(), 449 extension_->id(),
359 extensions::CommandService::ACTIVE_ONLY, 450 extensions::CommandService::ACTIVE_ONLY,
360 &browser_action_command, 451 &browser_action_command,
361 NULL)) { 452 NULL)) {
362 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); 453 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this);
363 } 454 }
364 } 455 }
365
366
367 ////////////////////////////////////////////////////////////////////////////////
368 // BrowserActionView
369
370 BrowserActionView::BrowserActionView(const Extension* extension,
371 BrowserActionsContainer* panel)
372 : panel_(panel) {
373 button_ = new BrowserActionButton(extension, panel);
374 button_->set_drag_controller(panel_);
375 AddChildView(button_);
376 button_->UpdateState();
377 }
378
379 BrowserActionView::~BrowserActionView() {
380 RemoveChildView(button_);
381 button_->Destroy();
382 }
383
384 gfx::Canvas* BrowserActionView::GetIconWithBadge() {
385 int tab_id = panel_->GetCurrentTabId();
386
387 SkBitmap icon = *button_->extension()->browser_action()->GetIcon(
388 tab_id).ToSkBitmap();
389
390 // Dim the icon if our button is disabled.
391 if (!button_->IsEnabled(tab_id))
392 icon = MakeTransparent(icon);
393
394 gfx::Canvas* canvas =
395 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false);
396
397 if (tab_id >= 0) {
398 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing);
399 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id);
400 }
401
402 return canvas;
403 }
404
405 void BrowserActionView::Layout() {
406 // We can't rely on button_->GetPreferredSize() here because that's not set
407 // correctly until the first call to
408 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be
409 // called before that when the initial bounds are set (and then not after,
410 // since the bounds don't change). So instead of setting the height from the
411 // button's preferred size, we use IconHeight(), since that's how big the
412 // button should be regardless of what it's displaying.
413 button_->SetBounds(0, ToolbarView::kVertSpacing, width(),
414 BrowserActionsContainer::IconHeight());
415 }
416
417 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) {
418 state->name = l10n_util::GetStringUTF16(
419 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION);
420 state->role = ui::AccessibilityTypes::ROLE_GROUPING;
421 }
422
423 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) {
424 View::PaintChildren(canvas);
425 ExtensionAction* action = button()->browser_action();
426 int tab_id = panel_->GetCurrentTabId();
427 if (tab_id >= 0)
428 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id);
429 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/browser_action_view.h ('k') | chrome/browser/ui/views/browser_actions_container.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698