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 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 int paste_index) | 565 int paste_index) |
566 : WrenchMenuView(menu, menu_model) { | 566 : WrenchMenuView(menu, menu_model) { |
567 InMenuButton* cut = CreateAndConfigureButton( | 567 InMenuButton* cut = CreateAndConfigureButton( |
568 IDS_CUT, InMenuButtonBackground::LEFT_BUTTON, | 568 IDS_CUT, InMenuButtonBackground::LEFT_BUTTON, |
569 cut_index); | 569 cut_index); |
570 InMenuButton* copy = CreateAndConfigureButton( | 570 InMenuButton* copy = CreateAndConfigureButton( |
571 IDS_COPY, InMenuButtonBackground::CENTER_BUTTON, | 571 IDS_COPY, InMenuButtonBackground::CENTER_BUTTON, |
572 copy_index); | 572 copy_index); |
573 InMenuButton* paste = CreateAndConfigureButton( | 573 InMenuButton* paste = CreateAndConfigureButton( |
574 IDS_PASTE, | 574 IDS_PASTE, |
575 menu->use_new_menu() && menu->supports_new_separators() ? | 575 menu->use_new_menu() && menu->supports_new_separators_ ? |
576 InMenuButtonBackground::CENTER_BUTTON : | 576 InMenuButtonBackground::CENTER_BUTTON : |
577 InMenuButtonBackground::RIGHT_BUTTON, | 577 InMenuButtonBackground::RIGHT_BUTTON, |
578 paste_index); | 578 paste_index); |
579 copy->SetOtherButtons(cut, paste); | 579 copy->SetOtherButtons(cut, paste); |
580 } | 580 } |
581 | 581 |
582 // Overridden from View. | 582 // Overridden from View. |
583 virtual gfx::Size GetPreferredSize() const OVERRIDE { | 583 virtual gfx::Size GetPreferredSize() const OVERRIDE { |
584 // Returned height doesn't matter as MenuItemView forces everything to the | 584 // Returned height doesn't matter as MenuItemView forces everything to the |
585 // height of the menuitemview. | 585 // height of the menuitemview. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 decrement_button_ = CreateButtonWithAccName( | 647 decrement_button_ = CreateButtonWithAccName( |
648 IDS_ZOOM_MINUS2, InMenuButtonBackground::LEFT_BUTTON, | 648 IDS_ZOOM_MINUS2, InMenuButtonBackground::LEFT_BUTTON, |
649 decrement_index, IDS_ACCNAME_ZOOM_MINUS2); | 649 decrement_index, IDS_ACCNAME_ZOOM_MINUS2); |
650 | 650 |
651 zoom_label_ = new Label( | 651 zoom_label_ = new Label( |
652 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100)); | 652 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100)); |
653 zoom_label_->SetAutoColorReadabilityEnabled(false); | 653 zoom_label_->SetAutoColorReadabilityEnabled(false); |
654 zoom_label_->SetHorizontalAlignment(gfx::ALIGN_RIGHT); | 654 zoom_label_->SetHorizontalAlignment(gfx::ALIGN_RIGHT); |
655 | 655 |
656 InMenuButtonBackground* center_bg = new InMenuButtonBackground( | 656 InMenuButtonBackground* center_bg = new InMenuButtonBackground( |
657 menu->use_new_menu() && menu->supports_new_separators() ? | 657 menu->use_new_menu() && menu->supports_new_separators_ ? |
658 InMenuButtonBackground::RIGHT_BUTTON : | 658 InMenuButtonBackground::RIGHT_BUTTON : |
659 InMenuButtonBackground::CENTER_BUTTON, | 659 InMenuButtonBackground::CENTER_BUTTON, |
660 menu->use_new_menu()); | 660 menu->use_new_menu()); |
661 zoom_label_->set_background(center_bg); | 661 zoom_label_->set_background(center_bg); |
662 | 662 |
663 AddChildView(zoom_label_); | 663 AddChildView(zoom_label_); |
664 zoom_label_width_ = MaxWidthForZoomLabel(); | 664 zoom_label_width_ = MaxWidthForZoomLabel(); |
665 | 665 |
666 increment_button_ = CreateButtonWithAccName( | 666 increment_button_ = CreateButtonWithAccName( |
667 IDS_ZOOM_PLUS2, InMenuButtonBackground::RIGHT_BUTTON, | 667 IDS_ZOOM_PLUS2, InMenuButtonBackground::RIGHT_BUTTON, |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
939 private: | 939 private: |
940 WrenchMenu* wrench_menu_; | 940 WrenchMenu* wrench_menu_; |
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, int run_flags) | 949 WrenchMenu::WrenchMenu(Browser* browser, |
| 950 bool use_new_menu, |
| 951 bool supports_new_separators) |
950 : root_(NULL), | 952 : root_(NULL), |
951 browser_(browser), | 953 browser_(browser), |
952 selected_menu_model_(NULL), | 954 selected_menu_model_(NULL), |
953 selected_index_(0), | 955 selected_index_(0), |
954 bookmark_menu_(NULL), | 956 bookmark_menu_(NULL), |
955 feedback_menu_item_(NULL), | 957 feedback_menu_item_(NULL), |
956 run_flags_(run_flags) { | 958 use_new_menu_(use_new_menu), |
| 959 supports_new_separators_(supports_new_separators) { |
957 registrar_.Add(this, chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED, | 960 registrar_.Add(this, chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED, |
958 content::Source<Profile>(browser_->profile())); | 961 content::Source<Profile>(browser_->profile())); |
959 } | 962 } |
960 | 963 |
961 WrenchMenu::~WrenchMenu() { | 964 WrenchMenu::~WrenchMenu() { |
962 if (bookmark_menu_delegate_.get()) { | 965 if (bookmark_menu_delegate_.get()) { |
963 BookmarkModel* model = BookmarkModelFactory::GetForProfile( | 966 BookmarkModel* model = BookmarkModelFactory::GetForProfile( |
964 browser_->profile()); | 967 browser_->profile()); |
965 if (model) | 968 if (model) |
966 model->RemoveObserver(this); | 969 model->RemoveObserver(this); |
967 } | 970 } |
968 FOR_EACH_OBSERVER(WrenchMenuObserver, observer_list_, WrenchMenuDestroyed()); | 971 FOR_EACH_OBSERVER(WrenchMenuObserver, observer_list_, WrenchMenuDestroyed()); |
969 } | 972 } |
970 | 973 |
971 void WrenchMenu::Init(ui::MenuModel* model) { | 974 void WrenchMenu::Init(ui::MenuModel* model) { |
972 DCHECK(!root_); | 975 DCHECK(!root_); |
973 root_ = new MenuItemView(this); | 976 root_ = new MenuItemView(this); |
974 root_->set_has_icons(true); // We have checks, radios and icons, set this | 977 root_->set_has_icons(true); // We have checks, radios and icons, set this |
975 // so we get the taller menu style. | 978 // so we get the taller menu style. |
976 PopulateMenu(root_, model); | 979 PopulateMenu(root_, model); |
977 | 980 |
978 #if defined(DEBUG) | 981 #if defined(DEBUG) |
979 // Verify that the reserved command ID's for bookmarks menu are not used. | 982 // Verify that the reserved command ID's for bookmarks menu are not used. |
980 for (int i = WrenchMenuModel:kMinBookmarkCommandId; | 983 for (int i = WrenchMenuModel:kMinBookmarkCommandId; |
981 i <= WrenchMenuModel::kMaxBookmarkCommandId; ++i) | 984 i <= WrenchMenuModel::kMaxBookmarkCommandId; ++i) |
982 DCHECK(command_id_to_entry_.find(i) == command_id_to_entry_.end()); | 985 DCHECK(command_id_to_entry_.find(i) == command_id_to_entry_.end()); |
983 #endif // defined(DEBUG) | 986 #endif // defined(DEBUG) |
984 | 987 |
985 int32 types = views::MenuRunner::HAS_MNEMONICS; | 988 menu_runner_.reset( |
986 if (for_drop()) { | 989 new views::MenuRunner(root_, views::MenuRunner::HAS_MNEMONICS)); |
987 // We add NESTED_DRAG since currently the only operation to open the wrench | |
988 // menu for is an extension action drag, which is controlled by the child | |
989 // BrowserActionsContainer view. | |
990 types |= views::MenuRunner::FOR_DROP | views::MenuRunner::NESTED_DRAG; | |
991 } | |
992 menu_runner_.reset(new views::MenuRunner(root_, types)); | |
993 } | 990 } |
994 | 991 |
995 void WrenchMenu::RunMenu(views::MenuButton* host) { | 992 void WrenchMenu::RunMenu(views::MenuButton* host) { |
996 gfx::Point screen_loc; | 993 gfx::Point screen_loc; |
997 views::View::ConvertPointToScreen(host, &screen_loc); | 994 views::View::ConvertPointToScreen(host, &screen_loc); |
998 gfx::Rect bounds(screen_loc, host->size()); | 995 gfx::Rect bounds(screen_loc, host->size()); |
999 content::RecordAction(UserMetricsAction("ShowAppMenu")); | 996 content::RecordAction(UserMetricsAction("ShowAppMenu")); |
1000 if (menu_runner_->RunMenuAt(host->GetWidget(), | 997 if (menu_runner_->RunMenuAt(host->GetWidget(), |
1001 host, | 998 host, |
1002 bounds, | 999 bounds, |
1003 views::MENU_ANCHOR_TOPRIGHT, | 1000 views::MENU_ANCHOR_TOPRIGHT, |
1004 ui::MENU_SOURCE_NONE) == | 1001 ui::MENU_SOURCE_NONE) == |
1005 views::MenuRunner::MENU_DELETED) | 1002 views::MenuRunner::MENU_DELETED) |
1006 return; | 1003 return; |
1007 if (bookmark_menu_delegate_.get()) { | 1004 if (bookmark_menu_delegate_.get()) { |
1008 BookmarkModel* model = BookmarkModelFactory::GetForProfile( | 1005 BookmarkModel* model = BookmarkModelFactory::GetForProfile( |
1009 browser_->profile()); | 1006 browser_->profile()); |
1010 if (model) | 1007 if (model) |
1011 model->RemoveObserver(this); | 1008 model->RemoveObserver(this); |
1012 } | 1009 } |
1013 if (selected_menu_model_) | 1010 if (selected_menu_model_) |
1014 selected_menu_model_->ActivatedAt(selected_index_); | 1011 selected_menu_model_->ActivatedAt(selected_index_); |
1015 } | 1012 } |
1016 | 1013 |
1017 void WrenchMenu::CloseMenu() { | |
1018 if (menu_runner_.get()) | |
1019 menu_runner_->Cancel(); | |
1020 } | |
1021 | |
1022 bool WrenchMenu::IsShowing() { | 1014 bool WrenchMenu::IsShowing() { |
1023 return menu_runner_.get() && menu_runner_->IsRunning(); | 1015 return menu_runner_.get() && menu_runner_->IsRunning(); |
1024 } | 1016 } |
1025 | 1017 |
1026 void WrenchMenu::AddObserver(WrenchMenuObserver* observer) { | 1018 void WrenchMenu::AddObserver(WrenchMenuObserver* observer) { |
1027 observer_list_.AddObserver(observer); | 1019 observer_list_.AddObserver(observer); |
1028 } | 1020 } |
1029 | 1021 |
1030 void WrenchMenu::RemoveObserver(WrenchMenuObserver* observer) { | 1022 void WrenchMenu::RemoveObserver(WrenchMenuObserver* observer) { |
1031 observer_list_.RemoveObserver(observer); | 1023 observer_list_.RemoveObserver(observer); |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1253 NOTREACHED(); | 1245 NOTREACHED(); |
1254 } | 1246 } |
1255 } | 1247 } |
1256 | 1248 |
1257 void WrenchMenu::PopulateMenu(MenuItemView* parent, | 1249 void WrenchMenu::PopulateMenu(MenuItemView* parent, |
1258 MenuModel* model) { | 1250 MenuModel* model) { |
1259 for (int i = 0, max = model->GetItemCount(); i < max; ++i) { | 1251 for (int i = 0, max = model->GetItemCount(); i < max; ++i) { |
1260 // The button container menu items have a special height which we have to | 1252 // The button container menu items have a special height which we have to |
1261 // use instead of the normal height. | 1253 // use instead of the normal height. |
1262 int height = 0; | 1254 int height = 0; |
1263 if (use_new_menu() && | 1255 if (use_new_menu_ && |
1264 (model->GetCommandIdAt(i) == IDC_CUT || | 1256 (model->GetCommandIdAt(i) == IDC_CUT || |
1265 model->GetCommandIdAt(i) == IDC_ZOOM_MINUS)) | 1257 model->GetCommandIdAt(i) == IDC_ZOOM_MINUS)) |
1266 height = kMenuItemContainingButtonsHeight; | 1258 height = kMenuItemContainingButtonsHeight; |
1267 | 1259 |
1268 scoped_ptr<ExtensionToolbarMenuView> extension_toolbar_menu_view; | 1260 scoped_ptr<ExtensionToolbarMenuView> extension_toolbar_menu_view; |
1269 if (model->GetCommandIdAt(i) == IDC_EXTENSIONS_OVERFLOW_MENU) { | 1261 if (model->GetCommandIdAt(i) == IDC_EXTENSIONS_OVERFLOW_MENU) { |
1270 extension_toolbar_menu_view.reset( | 1262 extension_toolbar_menu_view.reset(new ExtensionToolbarMenuView(browser_)); |
1271 new ExtensionToolbarMenuView(browser_, this)); | |
1272 height = extension_toolbar_menu_view->GetPreferredSize().height(); | 1263 height = extension_toolbar_menu_view->GetPreferredSize().height(); |
1273 } | 1264 } |
1274 | 1265 |
1275 // Add the menu item at the end. | 1266 // Add the menu item at the end. |
1276 int menu_index = parent->HasSubmenu() ? | 1267 int menu_index = parent->HasSubmenu() ? |
1277 parent->GetSubmenu()->child_count() : 0; | 1268 parent->GetSubmenu()->child_count() : 0; |
1278 MenuItemView* item = AddMenuItem( | 1269 MenuItemView* item = AddMenuItem( |
1279 parent, menu_index, model, i, model->GetTypeAt(i), height); | 1270 parent, menu_index, model, i, model->GetTypeAt(i), height); |
1280 | 1271 |
1281 if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) | 1272 if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1365 parent->CreateSubmenu(); | 1356 parent->CreateSubmenu(); |
1366 parent->GetSubmenu()->AddChildViewAt(menu_item, menu_index); | 1357 parent->GetSubmenu()->AddChildViewAt(menu_item, menu_index); |
1367 } else { | 1358 } else { |
1368 // For all other cases we use the more generic way to add menu items. | 1359 // For all other cases we use the more generic way to add menu items. |
1369 menu_item = views::MenuModelAdapter::AddMenuItemFromModelAt( | 1360 menu_item = views::MenuModelAdapter::AddMenuItemFromModelAt( |
1370 model, model_index, parent, menu_index, command_id); | 1361 model, model_index, parent, menu_index, command_id); |
1371 } | 1362 } |
1372 | 1363 |
1373 if (menu_item) { | 1364 if (menu_item) { |
1374 // Flush all buttons to the right side of the menu for the new menu type. | 1365 // Flush all buttons to the right side of the menu for the new menu type. |
1375 menu_item->set_use_right_margin(!use_new_menu()); | 1366 menu_item->set_use_right_margin(!use_new_menu_); |
1376 menu_item->SetVisible(model->IsVisibleAt(model_index)); | 1367 menu_item->SetVisible(model->IsVisibleAt(model_index)); |
1377 | 1368 |
1378 if (menu_type == MenuModel::TYPE_COMMAND && model->HasIcons()) { | 1369 if (menu_type == MenuModel::TYPE_COMMAND && model->HasIcons()) { |
1379 gfx::Image icon; | 1370 gfx::Image icon; |
1380 if (model->GetIconAt(model_index, &icon)) | 1371 if (model->GetIconAt(model_index, &icon)) |
1381 menu_item->SetIcon(*icon.ToImageSkia()); | 1372 menu_item->SetIcon(*icon.ToImageSkia()); |
1382 } | 1373 } |
1383 } | 1374 } |
1384 | 1375 |
1385 return menu_item; | 1376 return menu_item; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1417 0, | 1408 0, |
1418 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, | 1409 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, |
1419 BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU); | 1410 BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU); |
1420 } | 1411 } |
1421 | 1412 |
1422 int WrenchMenu::ModelIndexFromCommandId(int command_id) const { | 1413 int WrenchMenu::ModelIndexFromCommandId(int command_id) const { |
1423 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); | 1414 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); |
1424 DCHECK(ix != command_id_to_entry_.end()); | 1415 DCHECK(ix != command_id_to_entry_.end()); |
1425 return ix->second.second; | 1416 return ix->second.second; |
1426 } | 1417 } |
OLD | NEW |