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

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