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/wrench_menu.h" | 5 #include "chrome/browser/ui/views/toolbar/wrench_menu.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <set> | 9 #include <set> |
10 | 10 |
(...skipping 930 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
941 ui::MenuModel* model_; | 941 ui::MenuModel* model_; |
942 views::MenuItemView* menu_item_; | 942 views::MenuItemView* menu_item_; |
943 | 943 |
944 DISALLOW_COPY_AND_ASSIGN(RecentTabsMenuModelDelegate); | 944 DISALLOW_COPY_AND_ASSIGN(RecentTabsMenuModelDelegate); |
945 }; | 945 }; |
946 | 946 |
947 // WrenchMenu ------------------------------------------------------------------ | 947 // WrenchMenu ------------------------------------------------------------------ |
948 | 948 |
949 WrenchMenu::WrenchMenu(Browser* browser, | 949 WrenchMenu::WrenchMenu(Browser* browser, |
950 bool use_new_menu, | 950 bool use_new_menu, |
951 bool supports_new_separators) | 951 bool supports_new_separators, |
| 952 bool for_drop) |
952 : root_(NULL), | 953 : root_(NULL), |
953 browser_(browser), | 954 browser_(browser), |
954 selected_menu_model_(NULL), | 955 selected_menu_model_(NULL), |
955 selected_index_(0), | 956 selected_index_(0), |
956 bookmark_menu_(NULL), | 957 bookmark_menu_(NULL), |
957 feedback_menu_item_(NULL), | 958 feedback_menu_item_(NULL), |
958 use_new_menu_(use_new_menu), | 959 use_new_menu_(use_new_menu), |
959 supports_new_separators_(supports_new_separators) { | 960 supports_new_separators_(supports_new_separators), |
| 961 for_drop_(for_drop) { |
960 registrar_.Add(this, chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED, | 962 registrar_.Add(this, chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED, |
961 content::Source<Profile>(browser_->profile())); | 963 content::Source<Profile>(browser_->profile())); |
962 } | 964 } |
963 | 965 |
964 WrenchMenu::~WrenchMenu() { | 966 WrenchMenu::~WrenchMenu() { |
965 if (bookmark_menu_delegate_.get()) { | 967 if (bookmark_menu_delegate_.get()) { |
966 BookmarkModel* model = BookmarkModelFactory::GetForProfile( | 968 BookmarkModel* model = BookmarkModelFactory::GetForProfile( |
967 browser_->profile()); | 969 browser_->profile()); |
968 if (model) | 970 if (model) |
969 model->RemoveObserver(this); | 971 model->RemoveObserver(this); |
970 } | 972 } |
971 FOR_EACH_OBSERVER(WrenchMenuObserver, observer_list_, WrenchMenuDestroyed()); | 973 FOR_EACH_OBSERVER(WrenchMenuObserver, observer_list_, WrenchMenuDestroyed()); |
972 } | 974 } |
973 | 975 |
974 void WrenchMenu::Init(ui::MenuModel* model) { | 976 void WrenchMenu::Init(ui::MenuModel* model) { |
975 DCHECK(!root_); | 977 DCHECK(!root_); |
976 root_ = new MenuItemView(this); | 978 root_ = new MenuItemView(this); |
977 root_->set_has_icons(true); // We have checks, radios and icons, set this | 979 root_->set_has_icons(true); // We have checks, radios and icons, set this |
978 // so we get the taller menu style. | 980 // so we get the taller menu style. |
979 PopulateMenu(root_, model); | 981 PopulateMenu(root_, model); |
980 | 982 |
981 #if defined(DEBUG) | 983 #if defined(DEBUG) |
982 // Verify that the reserved command ID's for bookmarks menu are not used. | 984 // Verify that the reserved command ID's for bookmarks menu are not used. |
983 for (int i = WrenchMenuModel:kMinBookmarkCommandId; | 985 for (int i = WrenchMenuModel:kMinBookmarkCommandId; |
984 i <= WrenchMenuModel::kMaxBookmarkCommandId; ++i) | 986 i <= WrenchMenuModel::kMaxBookmarkCommandId; ++i) |
985 DCHECK(command_id_to_entry_.find(i) == command_id_to_entry_.end()); | 987 DCHECK(command_id_to_entry_.find(i) == command_id_to_entry_.end()); |
986 #endif // defined(DEBUG) | 988 #endif // defined(DEBUG) |
987 | 989 |
988 menu_runner_.reset( | 990 int32 types = views::MenuRunner::HAS_MNEMONICS | |
989 new views::MenuRunner(root_, views::MenuRunner::HAS_MNEMONICS)); | 991 (for_drop_ ? views::MenuRunner::FOR_DROP : 0); |
| 992 menu_runner_.reset(new views::MenuRunner(root_, types)); |
990 } | 993 } |
991 | 994 |
992 void WrenchMenu::RunMenu(views::MenuButton* host) { | 995 void WrenchMenu::RunMenu(views::MenuButton* host) { |
993 gfx::Point screen_loc; | 996 gfx::Point screen_loc; |
994 views::View::ConvertPointToScreen(host, &screen_loc); | 997 views::View::ConvertPointToScreen(host, &screen_loc); |
995 gfx::Rect bounds(screen_loc, host->size()); | 998 gfx::Rect bounds(screen_loc, host->size()); |
996 content::RecordAction(UserMetricsAction("ShowAppMenu")); | 999 content::RecordAction(UserMetricsAction("ShowAppMenu")); |
997 if (menu_runner_->RunMenuAt(host->GetWidget(), | 1000 if (menu_runner_->RunMenuAt(host->GetWidget(), |
998 host, | 1001 host, |
999 bounds, | 1002 bounds, |
1000 views::MENU_ANCHOR_TOPRIGHT, | 1003 views::MENU_ANCHOR_TOPRIGHT, |
1001 ui::MENU_SOURCE_NONE) == | 1004 ui::MENU_SOURCE_NONE) == |
1002 views::MenuRunner::MENU_DELETED) | 1005 views::MenuRunner::MENU_DELETED) |
1003 return; | 1006 return; |
1004 if (bookmark_menu_delegate_.get()) { | 1007 if (bookmark_menu_delegate_.get()) { |
1005 BookmarkModel* model = BookmarkModelFactory::GetForProfile( | 1008 BookmarkModel* model = BookmarkModelFactory::GetForProfile( |
1006 browser_->profile()); | 1009 browser_->profile()); |
1007 if (model) | 1010 if (model) |
1008 model->RemoveObserver(this); | 1011 model->RemoveObserver(this); |
1009 } | 1012 } |
1010 if (selected_menu_model_) | 1013 if (selected_menu_model_) |
1011 selected_menu_model_->ActivatedAt(selected_index_); | 1014 selected_menu_model_->ActivatedAt(selected_index_); |
1012 } | 1015 } |
1013 | 1016 |
| 1017 void WrenchMenu::CloseMenu() { |
| 1018 if (menu_runner_.get()) |
| 1019 menu_runner_->Cancel(); |
| 1020 } |
| 1021 |
1014 bool WrenchMenu::IsShowing() { | 1022 bool WrenchMenu::IsShowing() { |
1015 return menu_runner_.get() && menu_runner_->IsRunning(); | 1023 return menu_runner_.get() && menu_runner_->IsRunning(); |
1016 } | 1024 } |
1017 | 1025 |
1018 void WrenchMenu::AddObserver(WrenchMenuObserver* observer) { | 1026 void WrenchMenu::AddObserver(WrenchMenuObserver* observer) { |
1019 observer_list_.AddObserver(observer); | 1027 observer_list_.AddObserver(observer); |
1020 } | 1028 } |
1021 | 1029 |
1022 void WrenchMenu::RemoveObserver(WrenchMenuObserver* observer) { | 1030 void WrenchMenu::RemoveObserver(WrenchMenuObserver* observer) { |
1023 observer_list_.RemoveObserver(observer); | 1031 observer_list_.RemoveObserver(observer); |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1218 // it's opened. See ToolbarView::RunMenu() and Init() of this class. | 1226 // it's opened. See ToolbarView::RunMenu() and Init() of this class. |
1219 menu->GetSubmenu()->GetWidget()-> | 1227 menu->GetSubmenu()->GetWidget()-> |
1220 SetVisibilityChangedAnimationsEnabled(false); | 1228 SetVisibilityChangedAnimationsEnabled(false); |
1221 } | 1229 } |
1222 } | 1230 } |
1223 | 1231 |
1224 bool WrenchMenu::ShouldCloseOnDragComplete() { | 1232 bool WrenchMenu::ShouldCloseOnDragComplete() { |
1225 return false; | 1233 return false; |
1226 } | 1234 } |
1227 | 1235 |
| 1236 bool WrenchMenu::InNestedDrag() { |
| 1237 // We're only in a nested drag (that our MenuController wouldn't know about) |
| 1238 // if the menu was opened specifically for a drag-and-drop operation. |
| 1239 return for_drop_; |
| 1240 } |
| 1241 |
1228 void WrenchMenu::BookmarkModelChanged() { | 1242 void WrenchMenu::BookmarkModelChanged() { |
1229 DCHECK(bookmark_menu_delegate_.get()); | 1243 DCHECK(bookmark_menu_delegate_.get()); |
1230 if (!bookmark_menu_delegate_->is_mutating_model()) | 1244 if (!bookmark_menu_delegate_->is_mutating_model()) |
1231 root_->Cancel(); | 1245 root_->Cancel(); |
1232 } | 1246 } |
1233 | 1247 |
1234 void WrenchMenu::Observe(int type, | 1248 void WrenchMenu::Observe(int type, |
1235 const content::NotificationSource& source, | 1249 const content::NotificationSource& source, |
1236 const content::NotificationDetails& details) { | 1250 const content::NotificationDetails& details) { |
1237 switch (type) { | 1251 switch (type) { |
(...skipping 14 matching lines...) Expand all Loading... |
1252 // The button container menu items have a special height which we have to | 1266 // The button container menu items have a special height which we have to |
1253 // use instead of the normal height. | 1267 // use instead of the normal height. |
1254 int height = 0; | 1268 int height = 0; |
1255 if (use_new_menu_ && | 1269 if (use_new_menu_ && |
1256 (model->GetCommandIdAt(i) == IDC_CUT || | 1270 (model->GetCommandIdAt(i) == IDC_CUT || |
1257 model->GetCommandIdAt(i) == IDC_ZOOM_MINUS)) | 1271 model->GetCommandIdAt(i) == IDC_ZOOM_MINUS)) |
1258 height = kMenuItemContainingButtonsHeight; | 1272 height = kMenuItemContainingButtonsHeight; |
1259 | 1273 |
1260 scoped_ptr<ExtensionToolbarMenuView> extension_toolbar_menu_view; | 1274 scoped_ptr<ExtensionToolbarMenuView> extension_toolbar_menu_view; |
1261 if (model->GetCommandIdAt(i) == IDC_EXTENSIONS_OVERFLOW_MENU) { | 1275 if (model->GetCommandIdAt(i) == IDC_EXTENSIONS_OVERFLOW_MENU) { |
1262 extension_toolbar_menu_view.reset(new ExtensionToolbarMenuView(browser_)); | 1276 extension_toolbar_menu_view.reset( |
| 1277 new ExtensionToolbarMenuView(browser_, this)); |
1263 height = extension_toolbar_menu_view->GetPreferredSize().height(); | 1278 height = extension_toolbar_menu_view->GetPreferredSize().height(); |
1264 } | 1279 } |
1265 | 1280 |
1266 // Add the menu item at the end. | 1281 // Add the menu item at the end. |
1267 int menu_index = parent->HasSubmenu() ? | 1282 int menu_index = parent->HasSubmenu() ? |
1268 parent->GetSubmenu()->child_count() : 0; | 1283 parent->GetSubmenu()->child_count() : 0; |
1269 MenuItemView* item = AddMenuItem( | 1284 MenuItemView* item = AddMenuItem( |
1270 parent, menu_index, model, i, model->GetTypeAt(i), height); | 1285 parent, menu_index, model, i, model->GetTypeAt(i), height); |
1271 | 1286 |
1272 if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) | 1287 if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1408 0, | 1423 0, |
1409 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, | 1424 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, |
1410 BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU); | 1425 BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU); |
1411 } | 1426 } |
1412 | 1427 |
1413 int WrenchMenu::ModelIndexFromCommandId(int command_id) const { | 1428 int WrenchMenu::ModelIndexFromCommandId(int command_id) const { |
1414 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); | 1429 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); |
1415 DCHECK(ix != command_id_to_entry_.end()); | 1430 DCHECK(ix != command_id_to_entry_.end()); |
1416 return ix->second.second; | 1431 return ix->second.second; |
1417 } | 1432 } |
OLD | NEW |