OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/toolbar/browser_actions_container.h" | 5 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "chrome/browser/extensions/extension_action_manager.h" | 9 #include "chrome/browser/extensions/extension_action_manager.h" |
10 #include "chrome/browser/extensions/extension_util.h" | 10 #include "chrome/browser/extensions/extension_util.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 // The maximum number of icons to show per row when in overflow mode (showing | 59 // The maximum number of icons to show per row when in overflow mode (showing |
60 // icons in the application menu). | 60 // icons in the application menu). |
61 // TODO(devlin): Compute the right number of icons to show, depending on the | 61 // TODO(devlin): Compute the right number of icons to show, depending on the |
62 // menu width. | 62 // menu width. |
63 #if defined(OS_LINUX) | 63 #if defined(OS_LINUX) |
64 const int kIconsPerMenuRow = 8; // The menu on Linux is wider. | 64 const int kIconsPerMenuRow = 8; // The menu on Linux is wider. |
65 #else | 65 #else |
66 const int kIconsPerMenuRow = 7; | 66 const int kIconsPerMenuRow = 7; |
67 #endif | 67 #endif |
68 | 68 |
69 // Returns the ExtensionAction for the given |extension|. | |
70 ExtensionAction* GetExtensionAction(const Extension& extension, | |
71 Profile* profile) { | |
72 extensions::ExtensionActionManager* action_manager = | |
73 extensions::ExtensionActionManager::Get(profile); | |
74 ExtensionAction* action = action_manager->GetBrowserAction(extension); | |
75 return action ? action : action_manager->GetPageAction(extension); | |
76 } | |
77 | |
78 // A version of MenuButton with almost empty insets to fit properly on the | 69 // A version of MenuButton with almost empty insets to fit properly on the |
79 // toolbar. | 70 // toolbar. |
80 class ChevronMenuButton : public views::MenuButton { | 71 class ChevronMenuButton : public views::MenuButton { |
81 public: | 72 public: |
82 ChevronMenuButton(views::ButtonListener* listener, | 73 ChevronMenuButton(views::ButtonListener* listener, |
83 const base::string16& text, | 74 const base::string16& text, |
84 views::MenuButtonListener* menu_button_listener, | 75 views::MenuButtonListener* menu_button_listener, |
85 bool show_menu_marker) | 76 bool show_menu_marker) |
86 : views::MenuButton(listener, | 77 : views::MenuButton(listener, |
87 text, | 78 text, |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 void BrowserActionsContainer::RefreshBrowserActionViews() { | 211 void BrowserActionsContainer::RefreshBrowserActionViews() { |
221 for (size_t i = 0; i < browser_action_views_.size(); ++i) | 212 for (size_t i = 0; i < browser_action_views_.size(); ++i) |
222 browser_action_views_[i]->UpdateState(); | 213 browser_action_views_[i]->UpdateState(); |
223 } | 214 } |
224 | 215 |
225 void BrowserActionsContainer::CreateBrowserActionViews() { | 216 void BrowserActionsContainer::CreateBrowserActionViews() { |
226 DCHECK(browser_action_views_.empty()); | 217 DCHECK(browser_action_views_.empty()); |
227 if (!model_) | 218 if (!model_) |
228 return; | 219 return; |
229 | 220 |
221 extensions::ExtensionActionManager* action_manager = | |
222 extensions::ExtensionActionManager::Get(profile_); | |
230 const extensions::ExtensionList& toolbar_items = model_->toolbar_items(); | 223 const extensions::ExtensionList& toolbar_items = model_->toolbar_items(); |
231 for (extensions::ExtensionList::const_iterator i(toolbar_items.begin()); | 224 for (extensions::ExtensionList::const_iterator i(toolbar_items.begin()); |
232 i != toolbar_items.end(); ++i) { | 225 i != toolbar_items.end(); ++i) { |
233 if (!ShouldDisplayBrowserAction(i->get())) | 226 if (!ShouldDisplayBrowserAction(i->get())) |
234 continue; | 227 continue; |
235 | 228 |
236 BrowserActionView* view = | 229 BrowserActionView* view = |
237 new BrowserActionView(i->get(), | 230 new BrowserActionView(i->get(), |
238 GetExtensionAction(*i->get(), profile_), | 231 action_manager->GetExtensionAction(**i), |
239 browser_, | 232 browser_, |
240 this); | 233 this); |
241 browser_action_views_.push_back(view); | 234 browser_action_views_.push_back(view); |
242 AddChildView(view); | 235 AddChildView(view); |
243 } | 236 } |
244 } | 237 } |
245 | 238 |
246 void BrowserActionsContainer::DeleteBrowserActionViews() { | 239 void BrowserActionsContainer::DeleteBrowserActionViews() { |
247 HideActivePopup(); | 240 HideActivePopup(); |
248 if (overflow_menu_) | 241 if (overflow_menu_) |
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
806 CloseOverflowMenu(); | 799 CloseOverflowMenu(); |
807 | 800 |
808 if (!ShouldDisplayBrowserAction(extension)) | 801 if (!ShouldDisplayBrowserAction(extension)) |
809 return; | 802 return; |
810 | 803 |
811 // Add the new browser action to the vector and the view hierarchy. | 804 // Add the new browser action to the vector and the view hierarchy. |
812 if (profile_->IsOffTheRecord()) | 805 if (profile_->IsOffTheRecord()) |
813 index = model_->OriginalIndexToIncognito(index); | 806 index = model_->OriginalIndexToIncognito(index); |
814 BrowserActionView* view = | 807 BrowserActionView* view = |
815 new BrowserActionView(extension, | 808 new BrowserActionView(extension, |
816 GetExtensionAction(*extension, profile_), | 809 extensions::ExtensionActionManager::Get(profile_)-> |
810 GetExtensionAction(*extension), | |
817 browser_, | 811 browser_, |
818 this); | 812 this); |
819 browser_action_views_.insert(browser_action_views_.begin() + index, view); | 813 browser_action_views_.insert(browser_action_views_.begin() + index, view); |
820 AddChildViewAt(view, index); | 814 AddChildViewAt(view, index); |
821 | 815 |
822 // If we are still initializing the container, don't bother animating. | 816 // If we are still initializing the container, don't bother animating. |
823 if (!model_->extensions_initialized()) | 817 if (!model_->extensions_initialized()) |
824 return; | 818 return; |
825 | 819 |
826 // Enlarge the container if it was already at maximum size and we're not in | 820 // Enlarge the container if it was already at maximum size and we're not in |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
903 } | 897 } |
904 | 898 |
905 void BrowserActionsContainer::ToolbarExtensionUpdated( | 899 void BrowserActionsContainer::ToolbarExtensionUpdated( |
906 const Extension* extension) { | 900 const Extension* extension) { |
907 BrowserActionView* view = GetViewForExtension(extension); | 901 BrowserActionView* view = GetViewForExtension(extension); |
908 if (view) | 902 if (view) |
909 view->UpdateState(); | 903 view->UpdateState(); |
910 } | 904 } |
911 | 905 |
912 bool BrowserActionsContainer::ShowExtensionActionPopup( | 906 bool BrowserActionsContainer::ShowExtensionActionPopup( |
913 const Extension* extension) { | 907 const Extension* extension, bool grant_active_tab) { |
Peter Kasting
2014/08/27 19:41:19
Nit: One arg per line
Devlin
2014/08/27 20:47:16
Done.
| |
914 return ShowPopupForExtension(extension, false, false); | 908 // Don't override another popup, and only show in the active window. |
909 if (popup_owner_ || !browser_->window()->IsActive()) | |
910 return false; | |
911 | |
912 BrowserActionView* view = GetViewForExtension(extension); | |
913 return view ? view->view_controller()->ExecuteAction( | |
914 ExtensionPopup::SHOW, grant_active_tab) : false; | |
Peter Kasting
2014/08/27 19:41:19
Nit: This seems a little briefer:
return view &
Devlin
2014/08/27 20:47:16
Done.
| |
915 } | 915 } |
916 | 916 |
917 void BrowserActionsContainer::ToolbarVisibleCountChanged() { | 917 void BrowserActionsContainer::ToolbarVisibleCountChanged() { |
918 int old_container_width = container_width_; | 918 int old_container_width = container_width_; |
919 SetContainerWidth(); | 919 SetContainerWidth(); |
920 if (old_container_width != container_width_) | 920 if (old_container_width != container_width_) |
921 Animate(gfx::Tween::EASE_OUT, GetIconCount()); | 921 Animate(gfx::Tween::EASE_OUT, GetIconCount()); |
922 } | 922 } |
923 | 923 |
924 void BrowserActionsContainer::ToolbarHighlightModeChanged( | 924 void BrowserActionsContainer::ToolbarHighlightModeChanged( |
925 bool is_highlighting) { | 925 bool is_highlighting) { |
926 // The visual highlighting is done in OnPaint(). It's a bit of a pain that | 926 // The visual highlighting is done in OnPaint(). It's a bit of a pain that |
927 // we delete and recreate everything here, but given everything else going on | 927 // we delete and recreate everything here, but given everything else going on |
928 // (the lack of highlight, n more extensions appearing, etc), it's not worth | 928 // (the lack of highlight, n more extensions appearing, etc), it's not worth |
929 // the extra complexity to create and insert only the new extensions. | 929 // the extra complexity to create and insert only the new extensions. |
930 DeleteBrowserActionViews(); | 930 DeleteBrowserActionViews(); |
931 CreateBrowserActionViews(); | 931 CreateBrowserActionViews(); |
932 Animate(gfx::Tween::LINEAR, browser_action_views_.size()); | 932 Animate(gfx::Tween::LINEAR, browser_action_views_.size()); |
933 } | 933 } |
934 | 934 |
935 Browser* BrowserActionsContainer::GetBrowser() { | |
936 return browser_; | |
937 } | |
938 | |
935 void BrowserActionsContainer::LoadImages() { | 939 void BrowserActionsContainer::LoadImages() { |
936 ui::ThemeProvider* tp = GetThemeProvider(); | 940 ui::ThemeProvider* tp = GetThemeProvider(); |
937 if (!tp || !chevron_) | 941 if (!tp || !chevron_) |
938 return; | 942 return; |
939 | 943 |
940 chevron_->SetImage(views::Button::STATE_NORMAL, | 944 chevron_->SetImage(views::Button::STATE_NORMAL, |
941 *tp->GetImageSkiaNamed(IDR_BROWSER_ACTIONS_OVERFLOW)); | 945 *tp->GetImageSkiaNamed(IDR_BROWSER_ACTIONS_OVERFLOW)); |
942 | 946 |
943 const int kImages[] = IMAGE_GRID(IDR_DEVELOPER_MODE_HIGHLIGHT); | 947 const int kImages[] = IMAGE_GRID(IDR_DEVELOPER_MODE_HIGHLIGHT); |
944 highlight_painter_.reset(views::Painter::CreateImageGridPainter(kImages)); | 948 highlight_painter_.reset(views::Painter::CreateImageGridPainter(kImages)); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1047 } | 1051 } |
1048 } | 1052 } |
1049 | 1053 |
1050 bool BrowserActionsContainer::ShouldDisplayBrowserAction( | 1054 bool BrowserActionsContainer::ShouldDisplayBrowserAction( |
1051 const Extension* extension) const { | 1055 const Extension* extension) const { |
1052 // Only display incognito-enabled extensions while in incognito mode. | 1056 // Only display incognito-enabled extensions while in incognito mode. |
1053 return !profile_->IsOffTheRecord() || | 1057 return !profile_->IsOffTheRecord() || |
1054 extensions::util::IsIncognitoEnabled(extension->id(), profile_); | 1058 extensions::util::IsIncognitoEnabled(extension->id(), profile_); |
1055 } | 1059 } |
1056 | 1060 |
1057 bool BrowserActionsContainer::ShowPopupForExtension( | |
1058 const extensions::Extension* extension, | |
1059 bool grant_tab_permissions, | |
1060 bool can_override) { | |
1061 // If the popup cannot override other views, then no other popups can be | |
1062 // showing, and it must be shown in the active widow with a visible toolbar. | |
1063 // TODO(justinlin): Remove toolbar check when http://crbug.com/308645 is | |
1064 // fixed. | |
1065 if (!can_override && | |
1066 (popup_owner_ || | |
1067 !browser_->window()->IsActive() || | |
1068 !browser_->window()->IsToolbarVisible())) { | |
1069 return false; | |
1070 } | |
1071 | |
1072 BrowserActionView* view = GetViewForExtension(extension); | |
1073 return view ? view->view_controller()->ExecuteAction( | |
1074 ExtensionPopup::SHOW, grant_tab_permissions) : false; | |
1075 } | |
1076 | |
1077 BrowserActionView* BrowserActionsContainer::GetViewForExtension( | 1061 BrowserActionView* BrowserActionsContainer::GetViewForExtension( |
1078 const Extension* extension) { | 1062 const Extension* extension) { |
1079 for (BrowserActionViews::iterator view = browser_action_views_.begin(); | 1063 for (BrowserActionViews::iterator view = browser_action_views_.begin(); |
1080 view != browser_action_views_.end(); ++view) { | 1064 view != browser_action_views_.end(); ++view) { |
1081 if ((*view)->extension() == extension) | 1065 if ((*view)->extension() == extension) |
1082 return *view; | 1066 return *view; |
1083 } | 1067 } |
1084 | 1068 |
1085 return NULL; | 1069 return NULL; |
1086 } | 1070 } |
(...skipping 13 matching lines...) Expand all Loading... | |
1100 size_t absolute_model_size = | 1084 size_t absolute_model_size = |
1101 model_size == -1 ? extensions.size() : model_size; | 1085 model_size == -1 ? extensions.size() : model_size; |
1102 | 1086 |
1103 // The main container will try to show |model_size| icons, but reduce if there | 1087 // The main container will try to show |model_size| icons, but reduce if there |
1104 // aren't enough displayable icons to do so. | 1088 // aren't enough displayable icons to do so. |
1105 size_t main_displayed = std::min(displayable_icon_count, absolute_model_size); | 1089 size_t main_displayed = std::min(displayable_icon_count, absolute_model_size); |
1106 // The overflow will display the extras, if any. | 1090 // The overflow will display the extras, if any. |
1107 return in_overflow_mode() ? | 1091 return in_overflow_mode() ? |
1108 displayable_icon_count - main_displayed : main_displayed; | 1092 displayable_icon_count - main_displayed : main_displayed; |
1109 } | 1093 } |
OLD | NEW |