| OLD | NEW |
| 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_actions_host_delegate.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 "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
| 17 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
| 18 #include "grit/theme_resources_standard.h" | 19 #include "grit/theme_resources_standard.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" |
| 23 #include "ui/views/controls/menu/menu_model_adapter.h" | 24 #include "ui/views/controls/menu/menu_model_adapter.h" |
| 24 #include "ui/views/controls/menu/menu_runner.h" | 25 #include "ui/views/controls/menu/menu_runner.h" |
| 25 | 26 |
| 26 using extensions::Extension; | 27 using extensions::Extension; |
| 27 | 28 |
| 28 //////////////////////////////////////////////////////////////////////////////// | 29 //////////////////////////////////////////////////////////////////////////////// |
| 29 // BrowserActionButton | 30 // BrowserActionButton |
| 30 | 31 |
| 31 BrowserActionButton::BrowserActionButton(const Extension* extension, | 32 BrowserActionButton::BrowserActionButton(const Extension* extension, |
| 32 BrowserActionsContainer* panel) | 33 BrowserActionsHostDelegate* panel) |
| 33 : ALLOW_THIS_IN_INITIALIZER_LIST( | 34 : ALLOW_THIS_IN_INITIALIZER_LIST( |
| 34 MenuButton(this, string16(), NULL, false)), | 35 MenuButton(this, string16(), NULL, false)), |
| 35 browser_action_(extension->browser_action()), | 36 browser_action_(extension->browser_action()), |
| 36 extension_(extension), | 37 extension_(extension), |
| 37 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), | 38 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), |
| 38 panel_(panel), | 39 panel_(panel), |
| 39 context_menu_(NULL) { | 40 context_menu_(NULL) { |
| 40 set_border(NULL); | 41 set_border(NULL); |
| 41 set_alignment(TextButton::ALIGN_CENTER); | 42 set_alignment(TextButton::ALIGN_CENTER); |
| 42 | 43 |
| 43 // No UpdateState() here because View hierarchy not setup yet. Our parent | 44 // No UpdateState() here because View hierarchy not setup yet. Our parent |
| 44 // should call UpdateState() after creation. | 45 // should call UpdateState() after creation. |
| 45 | 46 |
| 46 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 47 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
| 47 content::Source<ExtensionAction>(browser_action_)); | 48 content::Source<ExtensionAction>(browser_action_)); |
| 48 } | 49 } |
| 49 | 50 |
| 50 void BrowserActionButton::Destroy() { | 51 void BrowserActionButton::Destroy() { |
| 51 if (keybinding_.get() && panel_->GetFocusManager()) | 52 if (keybinding_.get() && GetFocusManager()) |
| 52 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); | 53 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); |
| 53 | 54 |
| 54 if (context_menu_) { | 55 if (context_menu_) { |
| 55 context_menu_->Cancel(); | 56 context_menu_->Cancel(); |
| 56 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 57 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 57 } else { | 58 } else { |
| 58 delete this; | 59 delete this; |
| 59 } | 60 } |
| 60 } | 61 } |
| 61 | 62 |
| 62 void BrowserActionButton::ViewHierarchyChanged( | 63 void BrowserActionButton::ViewHierarchyChanged( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 76 ImageLoadingTracker::DONT_CACHE); | 77 ImageLoadingTracker::DONT_CACHE); |
| 77 } else { | 78 } else { |
| 78 // Set the icon to be the default extensions icon. | 79 // Set the icon to be the default extensions icon. |
| 79 default_icon_ = *ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 80 default_icon_ = *ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 80 IDR_EXTENSIONS_FAVICON).ToSkBitmap(); | 81 IDR_EXTENSIONS_FAVICON).ToSkBitmap(); |
| 81 UpdateState(); | 82 UpdateState(); |
| 82 } | 83 } |
| 83 | 84 |
| 84 extensions::CommandService* command_service = | 85 extensions::CommandService* command_service = |
| 85 extensions::CommandServiceFactory::GetForProfile( | 86 extensions::CommandServiceFactory::GetForProfile( |
| 86 panel_->browser()->profile()); | 87 panel_->GetBrowser()->profile()); |
| 87 const extensions::Command* browser_action_command = | 88 const extensions::Command* browser_action_command = |
| 88 command_service->GetBrowserActionCommand( | 89 command_service->GetBrowserActionCommand( |
| 89 extension_->id(), | 90 extension_->id(), |
| 90 extensions::CommandService::ACTIVE_ONLY); | 91 extensions::CommandService::ACTIVE_ONLY); |
| 91 if (browser_action_command) { | 92 if (browser_action_command) { |
| 92 keybinding_.reset(new ui::Accelerator( | 93 keybinding_.reset(new ui::Accelerator( |
| 93 browser_action_command->accelerator())); | 94 browser_action_command->accelerator())); |
| 94 panel_->GetFocusManager()->RegisterAccelerator( | 95 GetFocusManager()->RegisterAccelerator( |
| 95 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this); | 96 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this); |
| 96 } | 97 } |
| 97 } | 98 } |
| 98 | 99 |
| 99 MenuButton::ViewHierarchyChanged(is_add, parent, child); | 100 MenuButton::ViewHierarchyChanged(is_add, parent, child); |
| 100 } | 101 } |
| 101 | 102 |
| 102 bool BrowserActionButton::CanHandleAccelerators() const { | 103 bool BrowserActionButton::CanHandleAccelerators() const { |
| 103 // View::CanHandleAccelerators() checks to see if the view is visible before | 104 // View::CanHandleAccelerators() checks to see if the view is visible before |
| 104 // allowing it to process accelerators. This is not appropriate for browser | 105 // allowing it to process accelerators. This is not appropriate for browser |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 | 242 |
| 242 void BrowserActionButton::ShowContextMenu(const gfx::Point& p, | 243 void BrowserActionButton::ShowContextMenu(const gfx::Point& p, |
| 243 bool is_mouse_gesture) { | 244 bool is_mouse_gesture) { |
| 244 if (!extension()->ShowConfigureContextMenus()) | 245 if (!extension()->ShowConfigureContextMenus()) |
| 245 return; | 246 return; |
| 246 | 247 |
| 247 SetButtonPushed(); | 248 SetButtonPushed(); |
| 248 | 249 |
| 249 // Reconstructs the menu every time because the menu's contents are dynamic. | 250 // Reconstructs the menu every time because the menu's contents are dynamic. |
| 250 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_( | 251 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_( |
| 251 new ExtensionContextMenuModel(extension(), panel_->browser())); | 252 new ExtensionContextMenuModel(extension(), panel_->GetBrowser())); |
| 252 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get()); | 253 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get()); |
| 253 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); | 254 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); |
| 254 | 255 |
| 255 context_menu_ = menu_runner.GetMenu(); | 256 context_menu_ = menu_runner.GetMenu(); |
| 256 gfx::Point screen_loc; | 257 gfx::Point screen_loc; |
| 257 views::View::ConvertPointToScreen(this, &screen_loc); | 258 views::View::ConvertPointToScreen(this, &screen_loc); |
| 258 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()), | 259 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()), |
| 259 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) == | 260 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) == |
| 260 views::MenuRunner::MENU_DELETED) | 261 views::MenuRunner::MENU_DELETED) |
| 261 return; | 262 return; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 280 menu_visible_ = false; | 281 menu_visible_ = false; |
| 281 } | 282 } |
| 282 | 283 |
| 283 BrowserActionButton::~BrowserActionButton() { | 284 BrowserActionButton::~BrowserActionButton() { |
| 284 } | 285 } |
| 285 | 286 |
| 286 //////////////////////////////////////////////////////////////////////////////// | 287 //////////////////////////////////////////////////////////////////////////////// |
| 287 // BrowserActionView | 288 // BrowserActionView |
| 288 | 289 |
| 289 BrowserActionView::BrowserActionView(const Extension* extension, | 290 BrowserActionView::BrowserActionView(const Extension* extension, |
| 290 BrowserActionsContainer* panel) | 291 BrowserActionsHostDelegate* panel) |
| 291 : panel_(panel) { | 292 : panel_(panel) { |
| 292 button_ = new BrowserActionButton(extension, panel); | 293 button_ = new BrowserActionButton(extension, panel_); |
| 293 button_->set_drag_controller(panel_); | 294 button_->set_drag_controller(panel_); |
| 294 AddChildView(button_); | 295 AddChildView(button_); |
| 295 button_->UpdateState(); | 296 button_->UpdateState(); |
| 297 set_border(views::Border::CreateSolidBorder(1, |
| 298 SkColorSetRGB(0x00, 0x00, 0x00))); // REMOVE ME |
| 296 } | 299 } |
| 297 | 300 |
| 298 BrowserActionView::~BrowserActionView() { | 301 BrowserActionView::~BrowserActionView() { |
| 299 RemoveChildView(button_); | 302 RemoveChildView(button_); |
| 300 button_->Destroy(); | 303 button_->Destroy(); |
| 301 } | 304 } |
| 302 | 305 |
| 303 gfx::Canvas* BrowserActionView::GetIconWithBadge() { | 306 gfx::Canvas* BrowserActionView::GetIconWithBadge() { |
| 304 int tab_id = panel_->GetCurrentTabId(); | 307 int tab_id = panel_->GetCurrentTabId(); |
| 305 | 308 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 318 } | 321 } |
| 319 | 322 |
| 320 void BrowserActionView::Layout() { | 323 void BrowserActionView::Layout() { |
| 321 // We can't rely on button_->GetPreferredSize() here because that's not set | 324 // We can't rely on button_->GetPreferredSize() here because that's not set |
| 322 // correctly until the first call to | 325 // correctly until the first call to |
| 323 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be | 326 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be |
| 324 // called before that when the initial bounds are set (and then not after, | 327 // called before that when the initial bounds are set (and then not after, |
| 325 // since the bounds don't change). So instead of setting the height from the | 328 // since the bounds don't change). So instead of setting the height from the |
| 326 // button's preferred size, we use IconHeight(), since that's how big the | 329 // button's preferred size, we use IconHeight(), since that's how big the |
| 327 // button should be regardless of what it's displaying. | 330 // button should be regardless of what it's displaying. |
| 328 button_->SetBounds(0, ToolbarView::kVertSpacing, width(), | 331 gfx::Size size = panel_->GetContentOffset(); |
| 332 button_->SetBounds(size.width(), size.height(), width(), |
| 329 BrowserActionsContainer::IconHeight()); | 333 BrowserActionsContainer::IconHeight()); |
| 330 } | 334 } |
| 331 | 335 |
| 332 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) { | 336 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) { |
| 333 state->name = l10n_util::GetStringUTF16( | 337 state->name = l10n_util::GetStringUTF16( |
| 334 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION); | 338 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION); |
| 335 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | 339 state->role = ui::AccessibilityTypes::ROLE_GROUPING; |
| 336 } | 340 } |
| 337 | 341 |
| 342 gfx::Size BrowserActionView::GetPreferredSize() { |
| 343 return gfx::Size(Extension::kBrowserActionIconMaxSize+10, // FIXME |
| 344 Extension::kBrowserActionIconMaxSize+10); |
| 345 } |
| 346 |
| 338 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | 347 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { |
| 339 View::PaintChildren(canvas); | 348 View::PaintChildren(canvas); |
| 340 ExtensionAction* action = button()->browser_action(); | 349 ExtensionAction* action = button()->browser_action(); |
| 341 int tab_id = panel_->GetCurrentTabId(); | 350 int tab_id = panel_->GetCurrentTabId(); |
| 342 if (tab_id >= 0) | 351 if (tab_id >= 0) |
| 343 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); | 352 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); |
| 344 } | 353 } |
| OLD | NEW |