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

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, 5 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"
11 #include "chrome/browser/ui/browser.h" 11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/views/browser_actions_container.h" 12 #include "chrome/browser/ui/views/browser_actions_container.h"
13 #include "chrome/browser/ui/views/browser_action_view.h"
13 #include "chrome/browser/ui/views/toolbar_view.h" 14 #include "chrome/browser/ui/views/toolbar_view.h"
14 #include "chrome/common/chrome_notification_types.h" 15 #include "chrome/common/chrome_notification_types.h"
15 #include "chrome/common/extensions/extension.h" 16 #include "chrome/common/extensions/extension.h"
16 #include "chrome/common/extensions/extension_manifest_constants.h" 17 #include "chrome/common/extensions/extension_manifest_constants.h"
17 #include "grit/generated_resources.h" 18 #include "grit/generated_resources.h"
18 #include "grit/theme_resources.h" 19 #include "grit/theme_resources.h"
19 #include "ui/base/accessibility/accessible_view_state.h" 20 #include "ui/base/accessibility/accessible_view_state.h"
20 #include "ui/base/l10n/l10n_util.h" 21 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/resource/resource_bundle.h" 22 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/gfx/canvas.h" 23 #include "ui/gfx/canvas.h"
(...skipping 11 matching lines...) Expand all
34 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height()); 35 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height());
35 alpha.allocPixels(); 36 alpha.allocPixels();
36 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); 37 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0));
37 38
38 return SkBitmapOperations::CreateMaskedBitmap(image, alpha); 39 return SkBitmapOperations::CreateMaskedBitmap(image, alpha);
39 } 40 }
40 41
41 } // namespace 42 } // namespace
42 43
43 //////////////////////////////////////////////////////////////////////////////// 44 ////////////////////////////////////////////////////////////////////////////////
45 // BrowserActionView
46
47 BrowserActionView::BrowserActionView(const Extension* extension,
48 Browser* browser,
49 BrowserActionView::Delegate* delegate)
50 : browser_(browser),
51 delegate_(delegate) {
52 button_ = new BrowserActionButton(extension, browser_, delegate_);
msw 2012/07/24 23:41:55 nit: move this into the initializer list, or init
yefimt 2012/07/25 21:09:21 Done.
53 button_->set_owned_by_client();
54 button_->set_drag_controller(delegate_);
55 }
56
57 BrowserActionView::~BrowserActionView() {
58 RemoveChildView(button_);
59 button_->Destroy();
60 }
61
62 gfx::Canvas* BrowserActionView::GetIconWithBadge() {
63 int tab_id = delegate_->GetCurrentTabId();
64
65 SkBitmap icon = button_->extension()->browser_action()->GetIcon(tab_id);
66 if (icon.isNull())
67 icon = button_->default_icon();
68
69 gfx::Canvas* canvas =
70 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false);
71
72 if (tab_id >= 0) {
msw 2012/07/24 23:41:55 nit: can this be merged with (or made more similar
yefimt 2012/07/25 21:09:21 Not sure, I dont know this code enough
msw 2012/07/25 23:02:03 Okay, you didn't change the logic AFAICT, so it's
73 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing);
74 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id);
75 }
76
77 return canvas;
78 }
79
80 void BrowserActionView::Layout() {
81 // We can't rely on button_->GetPreferredSize() here because that's not set
82 // correctly until the first call to
83 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be
84 // called before that when the initial bounds are set (and then not after,
85 // since the bounds don't change). So instead of setting the height from the
86 // button's preferred size, we use IconHeight(), since that's how big the
87 // button should be regardless of what it's displaying.
88 gfx::Size size = delegate_->GetViewContentOffset();
89 button_->SetBounds(size.width(), size.height(), width(),
90 BrowserActionsContainer::IconHeight());
91 }
92
93 void BrowserActionView::ViewHierarchyChanged(bool is_add,
94 View* parent,
95 View* child) {
96 if (is_add && (child == this)) {
97 AddChildView(button_);
98 button_->UpdateState();
99 }
100 }
101
102 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) {
103 state->name = l10n_util::GetStringUTF16(
104 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION);
105 state->role = ui::AccessibilityTypes::ROLE_GROUPING;
106 }
107
108 gfx::Size BrowserActionView::GetPreferredSize() {
109 return gfx::Size(BrowserActionsContainer::IconWidth(false),
110 BrowserActionsContainer::IconHeight());
111 }
112
113 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) {
114 View::PaintChildren(canvas);
115 ExtensionAction* action = button()->browser_action();
116 int tab_id = delegate_->GetCurrentTabId();
117 if (tab_id >= 0)
118 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id);
119 }
120
121 ////////////////////////////////////////////////////////////////////////////////
44 // BrowserActionButton 122 // BrowserActionButton
45 123
46 BrowserActionButton::BrowserActionButton(const Extension* extension, 124 BrowserActionButton::BrowserActionButton(const Extension* extension,
47 BrowserActionsContainer* panel) 125 Browser* browser,
126 BrowserActionView::Delegate* delegate)
48 : ALLOW_THIS_IN_INITIALIZER_LIST( 127 : ALLOW_THIS_IN_INITIALIZER_LIST(
49 MenuButton(this, string16(), NULL, false)), 128 MenuButton(this, string16(), NULL, false)),
129 browser_(browser),
50 browser_action_(extension->browser_action()), 130 browser_action_(extension->browser_action()),
51 extension_(extension), 131 extension_(extension),
52 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), 132 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)),
53 panel_(panel), 133 delegate_(delegate),
54 context_menu_(NULL) { 134 context_menu_(NULL),
135 disable_tooltip_(false) {
55 set_border(NULL); 136 set_border(NULL);
56 set_alignment(TextButton::ALIGN_CENTER); 137 set_alignment(TextButton::ALIGN_CENTER);
57 set_context_menu_controller(this); 138 set_context_menu_controller(this);
58 139
59 // No UpdateState() here because View hierarchy not setup yet. Our parent 140 // No UpdateState() here because View hierarchy not setup yet. Our parent
60 // should call UpdateState() after creation. 141 // should call UpdateState() after creation.
61 142
143 content::NotificationSource notification_source =
144 content::Source<Profile>(browser_->profile()->GetOriginalProfile());
62 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, 145 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED,
63 content::Source<ExtensionAction>(browser_action_)); 146 content::Source<ExtensionAction>(browser_action_));
64 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, 147 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED,
65 content::Source<Profile>( 148 notification_source);
66 panel_->profile()->GetOriginalProfile()));
67 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, 149 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED,
68 content::Source<Profile>( 150 notification_source);
69 panel_->profile()->GetOriginalProfile()));
70 } 151 }
71 152
72 void BrowserActionButton::Destroy() { 153 void BrowserActionButton::Destroy() {
73 MaybeUnregisterExtensionCommand(false); 154 MaybeUnregisterExtensionCommand(false);
74 155
75 if (context_menu_) { 156 if (context_menu_) {
76 context_menu_->Cancel(); 157 context_menu_->Cancel();
77 MessageLoop::current()->DeleteSoon(FROM_HERE, this); 158 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
78 } else { 159 } else {
79 delete this; 160 delete this;
(...skipping 30 matching lines...) Expand all
110 191
111 bool BrowserActionButton::CanHandleAccelerators() const { 192 bool BrowserActionButton::CanHandleAccelerators() const {
112 // View::CanHandleAccelerators() checks to see if the view is visible before 193 // View::CanHandleAccelerators() checks to see if the view is visible before
113 // allowing it to process accelerators. This is not appropriate for browser 194 // allowing it to process accelerators. This is not appropriate for browser
114 // actions buttons, which can be hidden inside the overflow area. 195 // actions buttons, which can be hidden inside the overflow area.
115 return true; 196 return true;
116 } 197 }
117 198
118 void BrowserActionButton::ButtonPressed(views::Button* sender, 199 void BrowserActionButton::ButtonPressed(views::Button* sender,
119 const views::Event& event) { 200 const views::Event& event) {
120 panel_->OnBrowserActionExecuted(this); 201 delegate_->OnBrowserActionExecuted(this);
121 } 202 }
122 203
123 void BrowserActionButton::ShowContextMenuForView(View* source, 204 void BrowserActionButton::ShowContextMenuForView(View* source,
124 const gfx::Point& point) { 205 const gfx::Point& point) {
125 if (!extension()->ShowConfigureContextMenus()) 206 ShowContextMenuImpl();
126 return;
127
128 SetButtonPushed();
129
130 // Reconstructs the menu every time because the menu's contents are dynamic.
131 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_(
132 new ExtensionContextMenuModel(extension(), panel_->browser()));
133 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get());
134 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu());
135
136 context_menu_ = menu_runner.GetMenu();
137 gfx::Point screen_loc;
138 views::View::ConvertPointToScreen(this, &screen_loc);
139 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()),
140 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) ==
141 views::MenuRunner::MENU_DELETED)
142 return;
143
144 SetButtonNotPushed();
145 context_menu_ = NULL;
146 } 207 }
147 208
148 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, 209 void BrowserActionButton::OnImageLoaded(const gfx::Image& image,
149 const std::string& extension_id, 210 const std::string& extension_id,
150 int index) { 211 int index) {
151 if (!image.IsEmpty()) 212 if (!image.IsEmpty())
152 default_icon_ = *image.ToSkBitmap(); 213 default_icon_ = *image.ToSkBitmap();
153 214
154 // Call back to UpdateState() because a more specific icon might have been set 215 // Call back to UpdateState() because a more specific icon might have been set
155 // while the load was outstanding. 216 // while the load was outstanding.
156 UpdateState(); 217 UpdateState();
157 } 218 }
158 219
159 void BrowserActionButton::UpdateState() { 220 void BrowserActionButton::UpdateState() {
160 int tab_id = panel_->GetCurrentTabId(); 221 int tab_id = delegate_->GetCurrentTabId();
161 if (tab_id < 0) 222 if (tab_id < 0)
162 return; 223 return;
163 224
164 if (!IsEnabled(tab_id)) { 225 if (!IsEnabled(tab_id)) {
165 SetState(views::CustomButton::BS_DISABLED); 226 SetState(views::CustomButton::BS_DISABLED);
166 } else { 227 } else {
167 SetState(menu_visible_ ? 228 SetState(menu_visible_ ?
168 views::CustomButton::BS_NORMAL : 229 views::CustomButton::BS_NORMAL :
169 views::CustomButton::BS_PUSHED); 230 views::CustomButton::BS_PUSHED);
170 } 231 }
(...skipping 28 matching lines...) Expand all
199 SkBitmap bg_p; 260 SkBitmap bg_p;
200 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p, 261 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p,
201 SkBitmap::kARGB_8888_Config); 262 SkBitmap::kARGB_8888_Config);
202 SkCanvas bg_p_canvas(bg_p); 263 SkCanvas bg_p_canvas(bg_p);
203 bg_p_canvas.drawBitmap(icon, 264 bg_p_canvas.drawBitmap(icon,
204 SkIntToScalar((bg_p.width() - icon.width()) / 2), 265 SkIntToScalar((bg_p.width() - icon.width()) / 2),
205 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); 266 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint);
206 SetPushedIcon(bg_p); 267 SetPushedIcon(bg_p);
207 } 268 }
208 269
209 // If the browser action name is empty, show the extension name instead. 270 string16 name = GetTextForTooltip();
210 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); 271 SetTooltipText(disable_tooltip_ ? string16() : name);
211 if (name.empty())
212 name = UTF8ToUTF16(extension()->name());
213 SetTooltipText(name);
214 SetAccessibleName(name); 272 SetAccessibleName(name);
215 273
216 parent()->SchedulePaint(); 274 parent()->SchedulePaint();
217 } 275 }
218 276
219 bool BrowserActionButton::IsPopup() { 277 bool BrowserActionButton::IsPopup() {
220 int tab_id = panel_->GetCurrentTabId(); 278 int tab_id = delegate_->GetCurrentTabId();
221 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); 279 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id);
222 } 280 }
223 281
224 GURL BrowserActionButton::GetPopupUrl() { 282 GURL BrowserActionButton::GetPopupUrl() {
225 int tab_id = panel_->GetCurrentTabId(); 283 int tab_id = delegate_->GetCurrentTabId();
226 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id); 284 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id);
227 } 285 }
228 286
229 void BrowserActionButton::Observe(int type, 287 void BrowserActionButton::Observe(int type,
230 const content::NotificationSource& source, 288 const content::NotificationSource& source,
231 const content::NotificationDetails& details) { 289 const content::NotificationDetails& details) {
232 switch (type) { 290 switch (type) {
233 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: 291 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED:
234 UpdateState(); 292 UpdateState();
235 // The browser action may have become visible/hidden so we need to make 293 // The browser action may have become visible/hidden so we need to make
236 // sure the state gets updated. 294 // sure the state gets updated.
237 panel_->OnBrowserActionVisibilityChanged(); 295 delegate_->OnBrowserActionVisibilityChanged();
238 break; 296 break;
239 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED: 297 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED:
240 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { 298 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: {
241 std::pair<const std::string, const std::string>* payload = 299 std::pair<const std::string, const std::string>* payload =
242 content::Details<std::pair<const std::string, const std::string> >( 300 content::Details<std::pair<const std::string, const std::string> >(
243 details).ptr(); 301 details).ptr();
244 if (extension_->id() == payload->first && 302 if (extension_->id() == payload->first &&
245 payload->second == 303 payload->second ==
246 extension_manifest_values::kBrowserActionKeybindingEvent) { 304 extension_manifest_values::kBrowserActionKeybindingEvent) {
247 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED) 305 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED)
248 MaybeRegisterExtensionCommand(); 306 MaybeRegisterExtensionCommand();
249 else 307 else
250 MaybeUnregisterExtensionCommand(true); 308 MaybeUnregisterExtensionCommand(true);
251 } 309 }
252 break; 310 break;
253 } 311 }
254 default: 312 default:
255 NOTREACHED(); 313 NOTREACHED();
256 break; 314 break;
257 } 315 }
258 } 316 }
259 317
260 bool BrowserActionButton::Activate() { 318 bool BrowserActionButton::Activate() {
261 if (!IsPopup()) 319 if (!IsPopup())
262 return true; 320 return true;
263 321
264 panel_->OnBrowserActionExecuted(this); 322 delegate_->OnBrowserActionExecuted(this);
265 323
266 // TODO(erikkay): Run a nested modal loop while the mouse is down to 324 // TODO(erikkay): Run a nested modal loop while the mouse is down to
267 // enable menu-like drag-select behavior. 325 // enable menu-like drag-select behavior.
268 326
269 // The return value of this method is returned via OnMousePressed. 327 // The return value of this method is returned via OnMousePressed.
270 // We need to return false here since we're handing off focus to another 328 // We need to return false here since we're handing off focus to another
271 // widget/view, and true will grab it right back and try to send events 329 // widget/view, and true will grab it right back and try to send events
272 // to us. 330 // to us.
273 return false; 331 return false;
274 } 332 }
(...skipping 26 matching lines...) Expand all
301 MenuButton::OnMouseExited(event); 359 MenuButton::OnMouseExited(event);
302 else 360 else
303 TextButton::OnMouseExited(event); 361 TextButton::OnMouseExited(event);
304 } 362 }
305 363
306 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& event) { 364 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& event) {
307 return IsPopup() ? MenuButton::OnKeyReleased(event) 365 return IsPopup() ? MenuButton::OnKeyReleased(event)
308 : TextButton::OnKeyReleased(event); 366 : TextButton::OnKeyReleased(event);
309 } 367 }
310 368
369 void BrowserActionButton::ShowContextMenu(const gfx::Point& p,
370 bool is_mouse_gesture) {
371 ShowContextMenuImpl();
372 }
373
311 bool BrowserActionButton::AcceleratorPressed( 374 bool BrowserActionButton::AcceleratorPressed(
312 const ui::Accelerator& accelerator) { 375 const ui::Accelerator& accelerator) {
313 panel_->OnBrowserActionExecuted(this); 376 delegate_->OnBrowserActionExecuted(this);
314 return true; 377 return true;
315 } 378 }
316 379
317 void BrowserActionButton::SetButtonPushed() { 380 void BrowserActionButton::SetButtonPushed() {
318 SetState(views::CustomButton::BS_PUSHED); 381 SetState(views::CustomButton::BS_PUSHED);
319 menu_visible_ = true; 382 menu_visible_ = true;
320 } 383 }
321 384
322 void BrowserActionButton::SetButtonNotPushed() { 385 void BrowserActionButton::SetButtonNotPushed() {
323 SetState(views::CustomButton::BS_NORMAL); 386 SetState(views::CustomButton::BS_NORMAL);
324 menu_visible_ = false; 387 menu_visible_ = false;
325 } 388 }
326 389
390 void BrowserActionButton::SetTooltipDisabled(bool disable_tooltip) {
391 disable_tooltip_ = disable_tooltip;
392 SetTooltipText(disable_tooltip_ ? string16() : GetTextForTooltip());
msw 2012/07/24 23:41:55 nit: shouldn't |disable_tooltip_| be checked in Ge
msw 2012/07/25 23:02:03 Ah, I may have just conflated the purpose of the r
yefimt 2012/07/31 00:10:11 Done.
393 }
394
327 bool BrowserActionButton::IsEnabled(int tab_id) const { 395 bool BrowserActionButton::IsEnabled(int tab_id) const {
328 return browser_action_->GetIsVisible(tab_id); 396 return browser_action_->GetIsVisible(tab_id);
329 } 397 }
330 398
331 BrowserActionButton::~BrowserActionButton() { 399 BrowserActionButton::~BrowserActionButton() {
332 } 400 }
333 401
334 void BrowserActionButton::MaybeRegisterExtensionCommand() { 402 void BrowserActionButton::MaybeRegisterExtensionCommand() {
335 extensions::CommandService* command_service = 403 extensions::CommandService* command_service =
336 extensions::CommandServiceFactory::GetForProfile( 404 extensions::CommandServiceFactory::GetForProfile(browser_->profile());
337 panel_->browser()->profile());
338 extensions::Command browser_action_command; 405 extensions::Command browser_action_command;
339 if (command_service->GetBrowserActionCommand( 406 if (command_service->GetBrowserActionCommand(
340 extension_->id(), 407 extension_->id(),
341 extensions::CommandService::ACTIVE_ONLY, 408 extensions::CommandService::ACTIVE_ONLY,
342 &browser_action_command, 409 &browser_action_command,
343 NULL)) { 410 NULL)) {
344 keybinding_.reset(new ui::Accelerator( 411 keybinding_.reset(new ui::Accelerator(
345 browser_action_command.accelerator())); 412 browser_action_command.accelerator()));
346 panel_->GetFocusManager()->RegisterAccelerator( 413 GetFocusManager()->RegisterAccelerator(
347 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this); 414 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this);
348 } 415 }
349 } 416 }
350 417
351 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) { 418 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) {
352 if (!keybinding_.get() || !panel_->GetFocusManager()) 419 if (!keybinding_.get() || !GetFocusManager())
353 return; 420 return;
354 421
355 extensions::CommandService* command_service = 422 extensions::CommandService* command_service =
356 extensions::CommandServiceFactory::GetForProfile( 423 extensions::CommandServiceFactory::GetForProfile(browser_->profile());
357 panel_->browser()->profile());
358 424
359 extensions::Command browser_action_command; 425 extensions::Command browser_action_command;
360 if (!only_if_active || !command_service->GetBrowserActionCommand( 426 if (!only_if_active || !command_service->GetBrowserActionCommand(
361 extension_->id(), 427 extension_->id(),
362 extensions::CommandService::ACTIVE_ONLY, 428 extensions::CommandService::ACTIVE_ONLY,
363 &browser_action_command, 429 &browser_action_command,
364 NULL)) { 430 NULL)) {
365 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); 431 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this);
366 } 432 }
367 } 433 }
368 434
435 string16 BrowserActionButton::GetTextForTooltip() {
436 int tab_id = delegate_->GetCurrentTabId();
437 if (tab_id < 0)
438 return string16();
369 439
370 //////////////////////////////////////////////////////////////////////////////// 440 // If the browser action name is empty, show the extension name instead.
371 // BrowserActionView 441 std::string name = browser_action()->GetTitle(tab_id);
372 442 return UTF8ToUTF16(name.empty() ? extension()->name() : name);
373 BrowserActionView::BrowserActionView(const Extension* extension,
374 BrowserActionsContainer* panel)
375 : panel_(panel) {
376 button_ = new BrowserActionButton(extension, panel);
377 button_->set_drag_controller(panel_);
378 AddChildView(button_);
379 button_->UpdateState();
380 } 443 }
381 444
382 BrowserActionView::~BrowserActionView() { 445 void BrowserActionButton::ShowContextMenuImpl() {
383 RemoveChildView(button_); 446 if (!extension()->ShowConfigureContextMenus())
384 button_->Destroy(); 447 return;
448
449 SetButtonPushed();
450
451 // Reconstructs the menu every time because the menu's contents are dynamic.
452 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_(
453 new ExtensionContextMenuModel(extension(), browser_));
454 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get());
455 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu());
456
457 context_menu_ = menu_runner.GetMenu();
458 gfx::Point screen_loc;
459 views::View::ConvertPointToScreen(this, &screen_loc);
460 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()),
461 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) ==
462 views::MenuRunner::MENU_DELETED)
463 return;
464
465 SetButtonNotPushed();
466 context_menu_ = NULL;
385 } 467 }
386
387 gfx::Canvas* BrowserActionView::GetIconWithBadge() {
388 int tab_id = panel_->GetCurrentTabId();
389
390 SkBitmap icon = button_->extension()->browser_action()->GetIcon(tab_id);
391 if (icon.isNull())
392 icon = button_->default_icon();
393
394 // Dim the icon if our button is disabled.
395 if (!button_->IsEnabled(tab_id))
396 icon = MakeTransparent(icon);
397
398 gfx::Canvas* canvas =
399 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false);
400
401 if (tab_id >= 0) {
402 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing);
403 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id);
404 }
405
406 return canvas;
407 }
408
409 void BrowserActionView::Layout() {
410 // We can't rely on button_->GetPreferredSize() here because that's not set
411 // correctly until the first call to
412 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be
413 // called before that when the initial bounds are set (and then not after,
414 // since the bounds don't change). So instead of setting the height from the
415 // button's preferred size, we use IconHeight(), since that's how big the
416 // button should be regardless of what it's displaying.
417 button_->SetBounds(0, ToolbarView::kVertSpacing, width(),
418 BrowserActionsContainer::IconHeight());
419 }
420
421 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) {
422 state->name = l10n_util::GetStringUTF16(
423 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION);
424 state->role = ui::AccessibilityTypes::ROLE_GROUPING;
425 }
426
427 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) {
428 View::PaintChildren(canvas);
429 ExtensionAction* action = button()->browser_action();
430 int tab_id = panel_->GetCurrentTabId();
431 if (tab_id >= 0)
432 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id);
433 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698