| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/views/browser_actions_container.h" | 5 #include "chrome/browser/views/browser_actions_container.h" |
| 6 | 6 |
| 7 #include "app/gfx/canvas.h" | 7 #include "app/gfx/canvas.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "app/slide_animation.h" | 9 #include "app/slide_animation.h" |
| 10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "chrome/browser/views/extensions/extension_popup.h" | 26 #include "chrome/browser/views/extensions/extension_popup.h" |
| 27 #include "chrome/common/notification_source.h" | 27 #include "chrome/common/notification_source.h" |
| 28 #include "chrome/common/notification_type.h" | 28 #include "chrome/common/notification_type.h" |
| 29 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
| 30 #include "grit/app_resources.h" | 30 #include "grit/app_resources.h" |
| 31 #include "third_party/skia/include/core/SkBitmap.h" | 31 #include "third_party/skia/include/core/SkBitmap.h" |
| 32 #include "third_party/skia/include/core/SkTypeface.h" | 32 #include "third_party/skia/include/core/SkTypeface.h" |
| 33 #include "third_party/skia/include/effects/SkGradientShader.h" | 33 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 34 #include "views/controls/button/menu_button.h" | 34 #include "views/controls/button/menu_button.h" |
| 35 #include "views/controls/button/text_button.h" | 35 #include "views/controls/button/text_button.h" |
| 36 #include "views/controls/menu/menu_2.h" |
| 36 #include "views/drag_utils.h" | 37 #include "views/drag_utils.h" |
| 37 #include "views/window/window.h" | 38 #include "views/window/window.h" |
| 38 | 39 |
| 39 #include "grit/theme_resources.h" | 40 #include "grit/theme_resources.h" |
| 40 | 41 |
| 41 namespace { | 42 namespace { |
| 42 | 43 |
| 43 // The size (both dimensions) of the buttons for page actions. | 44 // The size (both dimensions) of the buttons for page actions. |
| 44 static const int kButtonSize = 29; | 45 static const int kButtonSize = 29; |
| 45 | 46 |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 if (showing_context_menu_) { | 219 if (showing_context_menu_) { |
| 219 SetButtonPushed(); | 220 SetButtonPushed(); |
| 220 | 221 |
| 221 // Get the top left point of this button in screen coordinates. | 222 // Get the top left point of this button in screen coordinates. |
| 222 gfx::Point point = gfx::Point(0, 0); | 223 gfx::Point point = gfx::Point(0, 0); |
| 223 ConvertPointToScreen(this, &point); | 224 ConvertPointToScreen(this, &point); |
| 224 | 225 |
| 225 // Make the menu appear below the button. | 226 // Make the menu appear below the button. |
| 226 point.Offset(0, height()); | 227 point.Offset(0, height()); |
| 227 | 228 |
| 228 panel_->GetContextMenu()->Run(extension(), extension()->browser_action(), | 229 // Reconstructs the menu every time because the menu's contents are dynamic. |
| 229 panel_, panel_->profile()->GetPrefs(), point); | 230 context_menu_contents_.reset(new ExtensionContextMenuModel( |
| 231 extension(), panel_->browser(), panel_)); |
| 232 context_menu_menu_.reset(new views::Menu2(context_menu_contents_.get())); |
| 233 context_menu_menu_->RunContextMenuAt(point); |
| 230 | 234 |
| 231 SetButtonNotPushed(); | 235 SetButtonNotPushed(); |
| 232 return false; | 236 return false; |
| 233 } else if (IsPopup()) { | 237 } else if (IsPopup()) { |
| 234 return MenuButton::OnMousePressed(e); | 238 return MenuButton::OnMousePressed(e); |
| 235 } | 239 } |
| 236 return TextButton::OnMousePressed(e); | 240 return TextButton::OnMousePressed(e); |
| 237 } | 241 } |
| 238 | 242 |
| 239 void BrowserActionButton::OnMouseReleased(const views::MouseEvent& e, | 243 void BrowserActionButton::OnMouseReleased(const views::MouseEvent& e, |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 // how many icons we'll show and set initial size to that. | 372 // how many icons we'll show and set initial size to that. |
| 369 predefined_width = IconCountToWidth(model_->size()); | 373 predefined_width = IconCountToWidth(model_->size()); |
| 370 } | 374 } |
| 371 container_size_ = gfx::Size(predefined_width, kButtonSize); | 375 container_size_ = gfx::Size(predefined_width, kButtonSize); |
| 372 } | 376 } |
| 373 | 377 |
| 374 BrowserActionsContainer::~BrowserActionsContainer() { | 378 BrowserActionsContainer::~BrowserActionsContainer() { |
| 375 if (model_) | 379 if (model_) |
| 376 model_->RemoveObserver(this); | 380 model_->RemoveObserver(this); |
| 377 StopShowFolderDropMenuTimer(); | 381 StopShowFolderDropMenuTimer(); |
| 378 CloseMenus(); | |
| 379 HidePopup(); | 382 HidePopup(); |
| 380 DeleteBrowserActionViews(); | 383 DeleteBrowserActionViews(); |
| 381 } | 384 } |
| 382 | 385 |
| 383 // Static. | 386 // Static. |
| 384 void BrowserActionsContainer::RegisterUserPrefs(PrefService* prefs) { | 387 void BrowserActionsContainer::RegisterUserPrefs(PrefService* prefs) { |
| 385 prefs->RegisterIntegerPref(prefs::kBrowserActionContainerWidth, 0); | 388 prefs->RegisterIntegerPref(prefs::kBrowserActionContainerWidth, 0); |
| 386 } | 389 } |
| 387 | 390 |
| 388 int BrowserActionsContainer::GetCurrentTabId() const { | 391 int BrowserActionsContainer::GetCurrentTabId() const { |
| 389 TabContents* tab_contents = browser_->GetSelectedTabContents(); | 392 TabContents* tab_contents = browser_->GetSelectedTabContents(); |
| 390 if (!tab_contents) | 393 if (!tab_contents) |
| 391 return -1; | 394 return -1; |
| 392 | 395 |
| 393 return tab_contents->controller().session_id().id(); | 396 return tab_contents->controller().session_id().id(); |
| 394 } | 397 } |
| 395 | 398 |
| 396 ExtensionActionContextMenu* BrowserActionsContainer::GetContextMenu() { | |
| 397 if (!context_menu_.get()) | |
| 398 context_menu_.reset(new ExtensionActionContextMenu()); | |
| 399 return context_menu_.get(); | |
| 400 } | |
| 401 | |
| 402 BrowserActionView* BrowserActionsContainer::GetBrowserActionView( | 399 BrowserActionView* BrowserActionsContainer::GetBrowserActionView( |
| 403 Extension* extension) { | 400 ExtensionAction* action) { |
| 404 for (BrowserActionViews::iterator iter = | 401 for (BrowserActionViews::iterator iter = |
| 405 browser_action_views_.begin(); iter != browser_action_views_.end(); | 402 browser_action_views_.begin(); iter != browser_action_views_.end(); |
| 406 ++iter) { | 403 ++iter) { |
| 407 if ((*iter)->button()->extension() == extension) | 404 if ((*iter)->button()->browser_action() == action) |
| 408 return *iter; | 405 return *iter; |
| 409 } | 406 } |
| 410 | 407 |
| 411 return NULL; | 408 return NULL; |
| 412 } | 409 } |
| 413 | 410 |
| 414 void BrowserActionsContainer::RefreshBrowserActionViews() { | 411 void BrowserActionsContainer::RefreshBrowserActionViews() { |
| 415 for (size_t i = 0; i < browser_action_views_.size(); ++i) | 412 for (size_t i = 0; i < browser_action_views_.size(); ++i) |
| 416 browser_action_views_[i]->button()->UpdateState(); | 413 browser_action_views_[i]->button()->UpdateState(); |
| 417 } | 414 } |
| 418 | 415 |
| 419 void BrowserActionsContainer::CloseMenus() { | 416 void BrowserActionsContainer::CloseOverflowMenu() { |
| 420 if (context_menu_.get()) | |
| 421 context_menu_->Cancel(); | |
| 422 // Close the overflow menu if open. | |
| 423 if (overflow_menu_) | 417 if (overflow_menu_) |
| 424 overflow_menu_->CancelMenu(); | 418 overflow_menu_->CancelMenu(); |
| 425 } | 419 } |
| 426 | 420 |
| 427 void BrowserActionsContainer::StopShowFolderDropMenuTimer() { | 421 void BrowserActionsContainer::StopShowFolderDropMenuTimer() { |
| 428 show_menu_task_factory_.RevokeAll(); | 422 show_menu_task_factory_.RevokeAll(); |
| 429 } | 423 } |
| 430 | 424 |
| 431 void BrowserActionsContainer::StartShowFolderDropMenuTimer() { | 425 void BrowserActionsContainer::StartShowFolderDropMenuTimer() { |
| 432 int delay = View::GetMenuShowDelay(); | 426 int delay = View::GetMenuShowDelay(); |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 | 865 |
| 872 void BrowserActionsContainer::BrowserActionAdded(Extension* extension, | 866 void BrowserActionsContainer::BrowserActionAdded(Extension* extension, |
| 873 int index) { | 867 int index) { |
| 874 #if defined(DEBUG) | 868 #if defined(DEBUG) |
| 875 for (size_t i = 0; i < browser_action_views_.size(); ++i) { | 869 for (size_t i = 0; i < browser_action_views_.size(); ++i) { |
| 876 DCHECK(browser_action_views_[i]->button()->extension() != extension) << | 870 DCHECK(browser_action_views_[i]->button()->extension() != extension) << |
| 877 "Asked to add a browser action view for an extension that already " | 871 "Asked to add a browser action view for an extension that already " |
| 878 "exists."; | 872 "exists."; |
| 879 } | 873 } |
| 880 #endif | 874 #endif |
| 881 | 875 CloseOverflowMenu(); |
| 882 CloseMenus(); | |
| 883 | 876 |
| 884 if (!ShouldDisplayBrowserAction(extension)) | 877 if (!ShouldDisplayBrowserAction(extension)) |
| 885 return; | 878 return; |
| 886 | 879 |
| 887 if (profile_->IsOffTheRecord()) | 880 if (profile_->IsOffTheRecord()) |
| 888 index = model_->OriginalIndexToIncognito(index); | 881 index = model_->OriginalIndexToIncognito(index); |
| 889 | 882 |
| 890 // Before we change anything, determine the number of visible browser actions. | 883 // Before we change anything, determine the number of visible browser actions. |
| 891 size_t visible_actions = VisibleBrowserActions(); | 884 size_t visible_actions = VisibleBrowserActions(); |
| 892 | 885 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 911 | 904 |
| 912 // We don't want the chevron to appear while we animate. See documentation | 905 // We don't want the chevron to appear while we animate. See documentation |
| 913 // in the header for why we do this. | 906 // in the header for why we do this. |
| 914 suppress_chevron_ = !chevron_->IsVisible(); | 907 suppress_chevron_ = !chevron_->IsVisible(); |
| 915 | 908 |
| 916 Animate(SlideAnimation::NONE, target_size); | 909 Animate(SlideAnimation::NONE, target_size); |
| 917 } | 910 } |
| 918 } | 911 } |
| 919 | 912 |
| 920 void BrowserActionsContainer::BrowserActionRemoved(Extension* extension) { | 913 void BrowserActionsContainer::BrowserActionRemoved(Extension* extension) { |
| 921 CloseMenus(); | 914 CloseOverflowMenu(); |
| 922 | 915 |
| 923 if (popup_ && popup_->host()->extension() == extension) | 916 if (popup_ && popup_->host()->extension() == extension) |
| 924 HidePopup(); | 917 HidePopup(); |
| 925 | 918 |
| 926 // Before we change anything, determine the number of visible browser | 919 // Before we change anything, determine the number of visible browser |
| 927 // actions. | 920 // actions. |
| 928 size_t visible_actions = VisibleBrowserActions(); | 921 size_t visible_actions = VisibleBrowserActions(); |
| 929 | 922 |
| 930 for (BrowserActionViews::iterator iter = | 923 for (BrowserActionViews::iterator iter = |
| 931 browser_action_views_.begin(); iter != browser_action_views_.end(); | 924 browser_action_views_.begin(); iter != browser_action_views_.end(); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1067 profile_->GetPrefs()->SetInteger(prefs::kBrowserActionContainerWidth, | 1060 profile_->GetPrefs()->SetInteger(prefs::kBrowserActionContainerWidth, |
| 1068 container_size_.width()); | 1061 container_size_.width()); |
| 1069 } | 1062 } |
| 1070 | 1063 |
| 1071 void BrowserActionsContainer::NotifyMenuDeleted( | 1064 void BrowserActionsContainer::NotifyMenuDeleted( |
| 1072 BrowserActionOverflowMenuController* controller) { | 1065 BrowserActionOverflowMenuController* controller) { |
| 1073 DCHECK(controller == overflow_menu_); | 1066 DCHECK(controller == overflow_menu_); |
| 1074 overflow_menu_ = NULL; | 1067 overflow_menu_ = NULL; |
| 1075 } | 1068 } |
| 1076 | 1069 |
| 1077 void BrowserActionsContainer::ShowPopupForDevToolsWindow(Extension* extension, | 1070 void BrowserActionsContainer::InspectPopup( |
| 1078 ExtensionAction* extension_action) { | 1071 ExtensionAction* action) { |
| 1079 OnBrowserActionExecuted(GetBrowserActionView(extension)->button(), | 1072 OnBrowserActionExecuted(GetBrowserActionView(action)->button(), |
| 1080 true); // inspect_with_devtools | 1073 true); // inspect_with_devtools |
| 1081 } | 1074 } |
| 1082 | 1075 |
| 1083 void BrowserActionsContainer::ExtensionPopupClosed(ExtensionPopup* popup) { | 1076 void BrowserActionsContainer::ExtensionPopupClosed(ExtensionPopup* popup) { |
| 1084 // ExtensionPopup is ref-counted, so we don't need to delete it. | 1077 // ExtensionPopup is ref-counted, so we don't need to delete it. |
| 1085 DCHECK_EQ(popup_, popup); | 1078 DCHECK_EQ(popup_, popup); |
| 1086 popup_ = NULL; | 1079 popup_ = NULL; |
| 1087 popup_button_->SetButtonNotPushed(); | 1080 popup_button_->SetButtonNotPushed(); |
| 1088 popup_button_ = NULL; | 1081 popup_button_ = NULL; |
| 1089 } | 1082 } |
| 1090 | 1083 |
| 1091 bool BrowserActionsContainer::ShouldDisplayBrowserAction(Extension* extension) { | 1084 bool BrowserActionsContainer::ShouldDisplayBrowserAction(Extension* extension) { |
| 1092 // Only display incognito-enabled extensions while in incognito mode. | 1085 // Only display incognito-enabled extensions while in incognito mode. |
| 1093 return (!profile_->IsOffTheRecord() || | 1086 return (!profile_->IsOffTheRecord() || |
| 1094 profile_->GetExtensionsService()->IsIncognitoEnabled(extension)); | 1087 profile_->GetExtensionsService()->IsIncognitoEnabled(extension)); |
| 1095 } | 1088 } |
| OLD | NEW |