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

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_);
Peter Kasting 2012/07/21 01:55:13 This leaks if you're never added to a view hierarc
yefimt 2012/07/23 23:47:52 It is difficult to do because when BrowserActionVi
Peter Kasting 2012/07/24 04:27:29 You mean the stuff in ActionBoxMenu::PopulateMenu(
53 button_->set_drag_controller(delegate_);
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 AddChildView(button_);
95 button_->UpdateState();
96 }
97 }
98
99 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) {
100 state->name = l10n_util::GetStringUTF16(
101 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION);
102 state->role = ui::AccessibilityTypes::ROLE_GROUPING;
103 }
104
105 gfx::Size BrowserActionView::GetPreferredSize() {
106 return gfx::Size(BrowserActionsContainer::IconWidth(false),
107 BrowserActionsContainer::IconHeight());
108 }
109
110 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) {
111 View::PaintChildren(canvas);
112 ExtensionAction* action = button()->browser_action();
113 int tab_id = delegate_->GetCurrentTabId();
114 if (tab_id >= 0)
115 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id);
116 }
117
118 ////////////////////////////////////////////////////////////////////////////////
44 // BrowserActionButton 119 // BrowserActionButton
45 120
46 BrowserActionButton::BrowserActionButton(const Extension* extension, 121 BrowserActionButton::BrowserActionButton(const Extension* extension,
47 BrowserActionsContainer* panel) 122 Browser* browser,
123 BrowserActionView::Delegate* delegate)
48 : ALLOW_THIS_IN_INITIALIZER_LIST( 124 : ALLOW_THIS_IN_INITIALIZER_LIST(
49 MenuButton(this, string16(), NULL, false)), 125 MenuButton(this, string16(), NULL, false)),
126 browser_(browser),
50 browser_action_(extension->browser_action()), 127 browser_action_(extension->browser_action()),
51 extension_(extension), 128 extension_(extension),
52 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), 129 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)),
53 panel_(panel), 130 delegate_(delegate),
54 context_menu_(NULL) { 131 context_menu_(NULL),
132 disable_tooltip_(false) {
55 set_border(NULL); 133 set_border(NULL);
56 set_alignment(TextButton::ALIGN_CENTER); 134 set_alignment(TextButton::ALIGN_CENTER);
57 set_context_menu_controller(this); 135 set_context_menu_controller(this);
58 136
59 // No UpdateState() here because View hierarchy not setup yet. Our parent 137 // No UpdateState() here because View hierarchy not setup yet. Our parent
60 // should call UpdateState() after creation. 138 // should call UpdateState() after creation.
61 139
140 Profile* original_profile = browser_->profile()->GetOriginalProfile();
Peter Kasting 2012/07/21 01:55:13 Nit: You can even make this temp a NotificationSou
yefimt 2012/07/23 23:47:52 Done.
62 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, 141 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED,
63 content::Source<ExtensionAction>(browser_action_)); 142 content::Source<ExtensionAction>(browser_action_));
64 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, 143 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED,
65 content::Source<Profile>( 144 content::Source<Profile>(original_profile));
66 panel_->profile()->GetOriginalProfile()));
67 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, 145 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED,
68 content::Source<Profile>( 146 content::Source<Profile>(original_profile));
69 panel_->profile()->GetOriginalProfile()));
70 } 147 }
71 148
72 void BrowserActionButton::Destroy() { 149 void BrowserActionButton::Destroy() {
73 MaybeUnregisterExtensionCommand(false); 150 MaybeUnregisterExtensionCommand(false);
74 151
75 if (context_menu_) { 152 if (context_menu_) {
76 context_menu_->Cancel(); 153 context_menu_->Cancel();
77 MessageLoop::current()->DeleteSoon(FROM_HERE, this); 154 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
78 } else { 155 } else {
79 delete this; 156 delete this;
(...skipping 30 matching lines...) Expand all
110 187
111 bool BrowserActionButton::CanHandleAccelerators() const { 188 bool BrowserActionButton::CanHandleAccelerators() const {
112 // View::CanHandleAccelerators() checks to see if the view is visible before 189 // View::CanHandleAccelerators() checks to see if the view is visible before
113 // allowing it to process accelerators. This is not appropriate for browser 190 // allowing it to process accelerators. This is not appropriate for browser
114 // actions buttons, which can be hidden inside the overflow area. 191 // actions buttons, which can be hidden inside the overflow area.
115 return true; 192 return true;
116 } 193 }
117 194
118 void BrowserActionButton::ButtonPressed(views::Button* sender, 195 void BrowserActionButton::ButtonPressed(views::Button* sender,
119 const views::Event& event) { 196 const views::Event& event) {
120 panel_->OnBrowserActionExecuted(this); 197 delegate_->OnBrowserActionExecuted(this);
121 } 198 }
122 199
123 void BrowserActionButton::ShowContextMenuForView(View* source, 200 void BrowserActionButton::ShowContextMenuForView(View* source,
Peter Kasting 2012/07/21 01:55:13 Nit: This function and ShowContextMenu() are exact
yefimt 2012/07/23 23:47:52 I cannot comment on correctness. Moved body to a 3
124 const gfx::Point& point) { 201 const gfx::Point& point) {
125 if (!extension()->ShowConfigureContextMenus()) 202 if (!extension()->ShowConfigureContextMenus())
126 return; 203 return;
127 204
128 SetButtonPushed(); 205 SetButtonPushed();
129 206
130 // Reconstructs the menu every time because the menu's contents are dynamic. 207 // Reconstructs the menu every time because the menu's contents are dynamic.
131 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_( 208 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_(
132 new ExtensionContextMenuModel(extension(), panel_->browser())); 209 new ExtensionContextMenuModel(extension(), browser_));
133 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get()); 210 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get());
134 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); 211 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu());
135 212
136 context_menu_ = menu_runner.GetMenu(); 213 context_menu_ = menu_runner.GetMenu();
137 gfx::Point screen_loc; 214 gfx::Point screen_loc;
138 views::View::ConvertPointToScreen(this, &screen_loc); 215 views::View::ConvertPointToScreen(this, &screen_loc);
139 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()), 216 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()),
140 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) == 217 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) ==
141 views::MenuRunner::MENU_DELETED) 218 views::MenuRunner::MENU_DELETED)
142 return; 219 return;
143 220
144 SetButtonNotPushed(); 221 SetButtonNotPushed();
145 context_menu_ = NULL; 222 context_menu_ = NULL;
146 } 223 }
147 224
148 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, 225 void BrowserActionButton::OnImageLoaded(const gfx::Image& image,
149 const std::string& extension_id, 226 const std::string& extension_id,
150 int index) { 227 int index) {
151 if (!image.IsEmpty()) 228 if (!image.IsEmpty())
152 default_icon_ = *image.ToSkBitmap(); 229 default_icon_ = *image.ToSkBitmap();
153 230
154 // Call back to UpdateState() because a more specific icon might have been set 231 // Call back to UpdateState() because a more specific icon might have been set
155 // while the load was outstanding. 232 // while the load was outstanding.
156 UpdateState(); 233 UpdateState();
157 } 234 }
158 235
159 void BrowserActionButton::UpdateState() { 236 void BrowserActionButton::UpdateState() {
160 int tab_id = panel_->GetCurrentTabId(); 237 int tab_id = delegate_->GetCurrentTabId();
161 if (tab_id < 0) 238 if (tab_id < 0)
162 return; 239 return;
163 240
164 if (!IsEnabled(tab_id)) { 241 if (!IsEnabled(tab_id)) {
165 SetState(views::CustomButton::BS_DISABLED); 242 SetState(views::CustomButton::BS_DISABLED);
166 } else { 243 } else {
167 SetState(menu_visible_ ? 244 SetState(menu_visible_ ?
168 views::CustomButton::BS_NORMAL : 245 views::CustomButton::BS_NORMAL :
169 views::CustomButton::BS_PUSHED); 246 views::CustomButton::BS_PUSHED);
170 } 247 }
(...skipping 28 matching lines...) Expand all
199 SkBitmap bg_p; 276 SkBitmap bg_p;
200 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p, 277 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p,
201 SkBitmap::kARGB_8888_Config); 278 SkBitmap::kARGB_8888_Config);
202 SkCanvas bg_p_canvas(bg_p); 279 SkCanvas bg_p_canvas(bg_p);
203 bg_p_canvas.drawBitmap(icon, 280 bg_p_canvas.drawBitmap(icon,
204 SkIntToScalar((bg_p.width() - icon.width()) / 2), 281 SkIntToScalar((bg_p.width() - icon.width()) / 2),
205 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); 282 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint);
206 SetPushedIcon(bg_p); 283 SetPushedIcon(bg_p);
207 } 284 }
208 285
209 // If the browser action name is empty, show the extension name instead. 286 string16 name = GetTextForTooltip();
210 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); 287 SetTooltipText(disable_tooltip_ ? string16() : name);
211 if (name.empty())
212 name = UTF8ToUTF16(extension()->name());
213 SetTooltipText(name);
214 SetAccessibleName(name); 288 SetAccessibleName(name);
215 289
216 parent()->SchedulePaint(); 290 parent()->SchedulePaint();
217 } 291 }
218 292
219 bool BrowserActionButton::IsPopup() { 293 bool BrowserActionButton::IsPopup() {
220 int tab_id = panel_->GetCurrentTabId(); 294 int tab_id = delegate_->GetCurrentTabId();
221 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); 295 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id);
222 } 296 }
223 297
224 GURL BrowserActionButton::GetPopupUrl() { 298 GURL BrowserActionButton::GetPopupUrl() {
225 int tab_id = panel_->GetCurrentTabId(); 299 int tab_id = delegate_->GetCurrentTabId();
226 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id); 300 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id);
227 } 301 }
228 302
229 void BrowserActionButton::Observe(int type, 303 void BrowserActionButton::Observe(int type,
230 const content::NotificationSource& source, 304 const content::NotificationSource& source,
231 const content::NotificationDetails& details) { 305 const content::NotificationDetails& details) {
232 switch (type) { 306 switch (type) {
233 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: 307 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED:
234 UpdateState(); 308 UpdateState();
235 // The browser action may have become visible/hidden so we need to make 309 // The browser action may have become visible/hidden so we need to make
236 // sure the state gets updated. 310 // sure the state gets updated.
237 panel_->OnBrowserActionVisibilityChanged(); 311 delegate_->OnBrowserActionVisibilityChanged();
238 break; 312 break;
239 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED: 313 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED:
240 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { 314 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: {
241 std::pair<const std::string, const std::string>* payload = 315 std::pair<const std::string, const std::string>* payload =
242 content::Details<std::pair<const std::string, const std::string> >( 316 content::Details<std::pair<const std::string, const std::string> >(
243 details).ptr(); 317 details).ptr();
244 if (extension_->id() == payload->first && 318 if (extension_->id() == payload->first &&
245 payload->second == 319 payload->second ==
246 extension_manifest_values::kBrowserActionKeybindingEvent) { 320 extension_manifest_values::kBrowserActionKeybindingEvent) {
247 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED) 321 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED)
248 MaybeRegisterExtensionCommand(); 322 MaybeRegisterExtensionCommand();
249 else 323 else
250 MaybeUnregisterExtensionCommand(true); 324 MaybeUnregisterExtensionCommand(true);
251 } 325 }
252 break; 326 break;
253 } 327 }
254 default: 328 default:
255 NOTREACHED(); 329 NOTREACHED();
256 break; 330 break;
257 } 331 }
258 } 332 }
259 333
260 bool BrowserActionButton::Activate() { 334 bool BrowserActionButton::Activate() {
261 if (!IsPopup()) 335 if (!IsPopup())
262 return true; 336 return true;
263 337
264 panel_->OnBrowserActionExecuted(this); 338 delegate_->OnBrowserActionExecuted(this);
265 339
266 // TODO(erikkay): Run a nested modal loop while the mouse is down to 340 // TODO(erikkay): Run a nested modal loop while the mouse is down to
267 // enable menu-like drag-select behavior. 341 // enable menu-like drag-select behavior.
268 342
269 // The return value of this method is returned via OnMousePressed. 343 // 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 344 // 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 345 // widget/view, and true will grab it right back and try to send events
272 // to us. 346 // to us.
273 return false; 347 return false;
274 } 348 }
(...skipping 26 matching lines...) Expand all
301 MenuButton::OnMouseExited(event); 375 MenuButton::OnMouseExited(event);
302 else 376 else
303 TextButton::OnMouseExited(event); 377 TextButton::OnMouseExited(event);
304 } 378 }
305 379
306 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& event) { 380 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& event) {
307 return IsPopup() ? MenuButton::OnKeyReleased(event) 381 return IsPopup() ? MenuButton::OnKeyReleased(event)
308 : TextButton::OnKeyReleased(event); 382 : TextButton::OnKeyReleased(event);
309 } 383 }
310 384
385 void BrowserActionButton::ShowContextMenu(const gfx::Point& p,
386 bool is_mouse_gesture) {
387 if (!extension()->ShowConfigureContextMenus())
388 return;
389
390 SetButtonPushed();
391
392 // Reconstructs the menu every time because the menu's contents are dynamic.
393 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_(
394 new ExtensionContextMenuModel(extension(), browser_));
395 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get());
396 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu());
397
398 context_menu_ = menu_runner.GetMenu();
399 gfx::Point screen_loc;
400 views::View::ConvertPointToScreen(this, &screen_loc);
401 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()),
402 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) ==
403 views::MenuRunner::MENU_DELETED)
404 return;
405
406 SetButtonNotPushed();
407 context_menu_ = NULL;
408 }
409
311 bool BrowserActionButton::AcceleratorPressed( 410 bool BrowserActionButton::AcceleratorPressed(
312 const ui::Accelerator& accelerator) { 411 const ui::Accelerator& accelerator) {
313 panel_->OnBrowserActionExecuted(this); 412 delegate_->OnBrowserActionExecuted(this);
314 return true; 413 return true;
315 } 414 }
316 415
317 void BrowserActionButton::SetButtonPushed() { 416 void BrowserActionButton::SetButtonPushed() {
318 SetState(views::CustomButton::BS_PUSHED); 417 SetState(views::CustomButton::BS_PUSHED);
319 menu_visible_ = true; 418 menu_visible_ = true;
320 } 419 }
321 420
322 void BrowserActionButton::SetButtonNotPushed() { 421 void BrowserActionButton::SetButtonNotPushed() {
323 SetState(views::CustomButton::BS_NORMAL); 422 SetState(views::CustomButton::BS_NORMAL);
324 menu_visible_ = false; 423 menu_visible_ = false;
325 } 424 }
326 425
426 void BrowserActionButton::SetTooltipDisabled(bool disable_tooltip) {
427 disable_tooltip_ = disable_tooltip;
428 SetTooltipText(disable_tooltip_ ? string16() : GetTextForTooltip());
429 }
430
327 bool BrowserActionButton::IsEnabled(int tab_id) const { 431 bool BrowserActionButton::IsEnabled(int tab_id) const {
328 return browser_action_->GetIsVisible(tab_id); 432 return browser_action_->GetIsVisible(tab_id);
329 } 433 }
330 434
331 BrowserActionButton::~BrowserActionButton() { 435 BrowserActionButton::~BrowserActionButton() {
332 } 436 }
333 437
334 void BrowserActionButton::MaybeRegisterExtensionCommand() { 438 void BrowserActionButton::MaybeRegisterExtensionCommand() {
335 extensions::CommandService* command_service = 439 extensions::CommandService* command_service =
336 extensions::CommandServiceFactory::GetForProfile( 440 extensions::CommandServiceFactory::GetForProfile(browser_->profile());
337 panel_->browser()->profile());
338 extensions::Command browser_action_command; 441 extensions::Command browser_action_command;
339 if (command_service->GetBrowserActionCommand( 442 if (command_service->GetBrowserActionCommand(
340 extension_->id(), 443 extension_->id(),
341 extensions::CommandService::ACTIVE_ONLY, 444 extensions::CommandService::ACTIVE_ONLY,
342 &browser_action_command, 445 &browser_action_command,
343 NULL)) { 446 NULL)) {
344 keybinding_.reset(new ui::Accelerator( 447 keybinding_.reset(new ui::Accelerator(
345 browser_action_command.accelerator())); 448 browser_action_command.accelerator()));
346 panel_->GetFocusManager()->RegisterAccelerator( 449 GetFocusManager()->RegisterAccelerator(
347 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this); 450 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this);
348 } 451 }
349 } 452 }
350 453
351 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) { 454 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) {
352 if (!keybinding_.get() || !panel_->GetFocusManager()) 455 if (!keybinding_.get() || !GetFocusManager())
353 return; 456 return;
354 457
355 extensions::CommandService* command_service = 458 extensions::CommandService* command_service =
356 extensions::CommandServiceFactory::GetForProfile( 459 extensions::CommandServiceFactory::GetForProfile(browser_->profile());
357 panel_->browser()->profile());
358 460
359 extensions::Command browser_action_command; 461 extensions::Command browser_action_command;
360 if (!only_if_active || !command_service->GetBrowserActionCommand( 462 if (!only_if_active || !command_service->GetBrowserActionCommand(
361 extension_->id(), 463 extension_->id(),
362 extensions::CommandService::ACTIVE_ONLY, 464 extensions::CommandService::ACTIVE_ONLY,
363 &browser_action_command, 465 &browser_action_command,
364 NULL)) { 466 NULL)) {
365 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); 467 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this);
366 } 468 }
367 } 469 }
368 470
471 string16 BrowserActionButton::GetTextForTooltip() {
472 int tab_id = delegate_->GetCurrentTabId();
473 if (tab_id < 0)
474 return string16();
369 475
370 //////////////////////////////////////////////////////////////////////////////// 476 // If the browser action name is empty, show the extension name instead.
371 // BrowserActionView 477 std::string name = browser_action()->GetTitle(tab_id);
372 478 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 } 479 }
381
382 BrowserActionView::~BrowserActionView() {
383 RemoveChildView(button_);
384 button_->Destroy();
385 }
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