Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(836)

Side by Side Diff: chrome/browser/ui/views/toolbar/wrench_menu.cc

Issue 324393002: Extension Toolbar redesign, part 1 (overflow) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Crash fix for Linux Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/app/chrome_command_ids.h" 13 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 14 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
15 #include "chrome/browser/bookmarks/bookmark_stats.h" 15 #include "chrome/browser/bookmarks/bookmark_stats.h"
16 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/search/search.h" 18 #include "chrome/browser/search/search.h"
19 #include "chrome/browser/ui/browser.h" 19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_window.h" 20 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h" 21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/browser/ui/toolbar/wrench_menu_model.h" 22 #include "chrome/browser/ui/toolbar/wrench_menu_model.h"
23 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h" 23 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h"
24 #include "chrome/browser/ui/views/toolbar/extension_toolbar_menu_view.h"
24 #include "chrome/browser/ui/views/toolbar/wrench_menu_observer.h" 25 #include "chrome/browser/ui/views/toolbar/wrench_menu_observer.h"
25 #include "components/bookmarks/browser/bookmark_model.h" 26 #include "components/bookmarks/browser/bookmark_model.h"
26 #include "content/public/browser/host_zoom_map.h" 27 #include "content/public/browser/host_zoom_map.h"
27 #include "content/public/browser/notification_observer.h" 28 #include "content/public/browser/notification_observer.h"
28 #include "content/public/browser/notification_registrar.h" 29 #include "content/public/browser/notification_registrar.h"
29 #include "content/public/browser/notification_source.h" 30 #include "content/public/browser/notification_source.h"
30 #include "content/public/browser/notification_types.h" 31 #include "content/public/browser/notification_types.h"
31 #include "content/public/browser/user_metrics.h" 32 #include "content/public/browser/user_metrics.h"
32 #include "content/public/browser/web_contents.h" 33 #include "content/public/browser/web_contents.h"
34 #include "extensions/common/feature_switch.h"
33 #include "grit/chromium_strings.h" 35 #include "grit/chromium_strings.h"
34 #include "grit/generated_resources.h" 36 #include "grit/generated_resources.h"
35 #include "grit/theme_resources.h" 37 #include "grit/theme_resources.h"
36 #include "third_party/skia/include/core/SkCanvas.h" 38 #include "third_party/skia/include/core/SkCanvas.h"
37 #include "third_party/skia/include/core/SkPaint.h" 39 #include "third_party/skia/include/core/SkPaint.h"
38 #include "ui/base/l10n/l10n_util.h" 40 #include "ui/base/l10n/l10n_util.h"
39 #include "ui/base/layout.h" 41 #include "ui/base/layout.h"
40 #include "ui/base/resource/resource_bundle.h" 42 #include "ui/base/resource/resource_bundle.h"
41 #include "ui/gfx/canvas.h" 43 #include "ui/gfx/canvas.h"
42 #include "ui/gfx/font_list.h" 44 #include "ui/gfx/font_list.h"
(...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after
673 675
674 fullscreen_button_->SetFocusable(true); 676 fullscreen_button_->SetFocusable(true);
675 fullscreen_button_->set_request_focus_on_press(false); 677 fullscreen_button_->set_request_focus_on_press(false);
676 fullscreen_button_->set_tag(fullscreen_index); 678 fullscreen_button_->set_tag(fullscreen_index);
677 fullscreen_button_->SetImageAlignment( 679 fullscreen_button_->SetImageAlignment(
678 ImageButton::ALIGN_CENTER, ImageButton::ALIGN_MIDDLE); 680 ImageButton::ALIGN_CENTER, ImageButton::ALIGN_MIDDLE);
679 int horizontal_padding = 681 int horizontal_padding =
680 menu->use_new_menu() ? kHorizontalTouchPadding : kHorizontalPadding; 682 menu->use_new_menu() ? kHorizontalTouchPadding : kHorizontalPadding;
681 fullscreen_button_->SetBorder(views::Border::CreateEmptyBorder( 683 fullscreen_button_->SetBorder(views::Border::CreateEmptyBorder(
682 0, horizontal_padding, 0, horizontal_padding)); 684 0, horizontal_padding, 0, horizontal_padding));
683 fullscreen_button_->set_background( 685 fullscreen_button_->set_background(new InMenuButtonBackground(
684 new InMenuButtonBackground(InMenuButtonBackground::SINGLE_BUTTON, 686 InMenuButtonBackground::SINGLE_BUTTON, menu->use_new_menu()));
685 menu->use_new_menu()));
686 fullscreen_button_->SetAccessibleName( 687 fullscreen_button_->SetAccessibleName(
687 GetAccessibleNameForWrenchMenuItem( 688 GetAccessibleNameForWrenchMenuItem(
688 menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN)); 689 menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN));
689 AddChildView(fullscreen_button_); 690 AddChildView(fullscreen_button_);
690 691
691 // Need to set a font list for the zoom label width calculations. 692 // Need to set a font list for the zoom label width calculations.
692 OnNativeThemeChanged(NULL); 693 OnNativeThemeChanged(NULL);
693 UpdateZoomControls(); 694 UpdateZoomControls();
694 } 695 }
695 696
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
1139 return entry.first->IsItemCheckedAt(entry.second); 1140 return entry.first->IsItemCheckedAt(entry.second);
1140 } 1141 }
1141 1142
1142 bool WrenchMenu::IsCommandEnabled(int command_id) const { 1143 bool WrenchMenu::IsCommandEnabled(int command_id) const {
1143 if (IsBookmarkCommand(command_id)) 1144 if (IsBookmarkCommand(command_id))
1144 return true; 1145 return true;
1145 1146
1146 if (command_id == 0) 1147 if (command_id == 0)
1147 return false; // The root item. 1148 return false; // The root item.
1148 1149
1149 // The items representing the cut menu (cut/copy/paste) and zoom menu 1150 // The items representing the cut menu (cut/copy/paste), zoom menu
1150 // (increment/decrement/reset) are always enabled. The child views of these 1151 // (increment/decrement/reset) and extension toolbar view are always enabled.
1151 // items enabled state updates appropriately. 1152 // The child views of these items enabled state updates appropriately.
1152 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) 1153 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS ||
1154 command_id == IDC_EXTENSIONS_OVERFLOW_MENU)
1153 return true; 1155 return true;
1154 1156
1155 const Entry& entry = command_id_to_entry_.find(command_id)->second; 1157 const Entry& entry = command_id_to_entry_.find(command_id)->second;
1156 return entry.first->IsEnabledAt(entry.second); 1158 return entry.first->IsEnabledAt(entry.second);
1157 } 1159 }
1158 1160
1159 void WrenchMenu::ExecuteCommand(int command_id, int mouse_event_flags) { 1161 void WrenchMenu::ExecuteCommand(int command_id, int mouse_event_flags) {
1160 if (IsBookmarkCommand(command_id)) { 1162 if (IsBookmarkCommand(command_id)) {
1161 bookmark_menu_delegate_->ExecuteCommand(command_id, mouse_event_flags); 1163 bookmark_menu_delegate_->ExecuteCommand(command_id, mouse_event_flags);
1162 return; 1164 return;
1163 } 1165 }
1164 1166
1165 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) { 1167 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS ||
1168 command_id == IDC_EXTENSIONS_OVERFLOW_MENU) {
1166 // These items are represented by child views. If ExecuteCommand is invoked 1169 // These items are represented by child views. If ExecuteCommand is invoked
1167 // it means the user clicked on the area around the buttons and we should 1170 // it means the user clicked on the area around the buttons and we should
1168 // not do anyting. 1171 // not do anyting.
1169 return; 1172 return;
1170 } 1173 }
1171 1174
1172 const Entry& entry = command_id_to_entry_.find(command_id)->second; 1175 const Entry& entry = command_id_to_entry_.find(command_id)->second;
1173 return entry.first->ActivatedAt(entry.second, mouse_event_flags); 1176 return entry.first->ActivatedAt(entry.second, mouse_event_flags);
1174 } 1177 }
1175 1178
1176 bool WrenchMenu::GetAccelerator(int command_id, 1179 bool WrenchMenu::GetAccelerator(int command_id,
1177 ui::Accelerator* accelerator) const { 1180 ui::Accelerator* accelerator) const {
1178 if (IsBookmarkCommand(command_id)) 1181 if (IsBookmarkCommand(command_id))
1179 return false; 1182 return false;
1180 1183
1181 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) { 1184 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS ||
1185 command_id == IDC_EXTENSIONS_OVERFLOW_MENU) {
1182 // These have special child views; don't show the accelerator for them. 1186 // These have special child views; don't show the accelerator for them.
1183 return false; 1187 return false;
1184 } 1188 }
1185 1189
1186 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); 1190 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id);
1187 const Entry& entry = ix->second; 1191 const Entry& entry = ix->second;
1188 ui::Accelerator menu_accelerator; 1192 ui::Accelerator menu_accelerator;
1189 if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator)) 1193 if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator))
1190 return false; 1194 return false;
1191 1195
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1238 MenuModel* model) { 1242 MenuModel* model) {
1239 for (int i = 0, max = model->GetItemCount(); i < max; ++i) { 1243 for (int i = 0, max = model->GetItemCount(); i < max; ++i) {
1240 // The button container menu items have a special height which we have to 1244 // The button container menu items have a special height which we have to
1241 // use instead of the normal height. 1245 // use instead of the normal height.
1242 int height = 0; 1246 int height = 0;
1243 if (use_new_menu_ && 1247 if (use_new_menu_ &&
1244 (model->GetCommandIdAt(i) == IDC_CUT || 1248 (model->GetCommandIdAt(i) == IDC_CUT ||
1245 model->GetCommandIdAt(i) == IDC_ZOOM_MINUS)) 1249 model->GetCommandIdAt(i) == IDC_ZOOM_MINUS))
1246 height = kMenuItemContainingButtonsHeight; 1250 height = kMenuItemContainingButtonsHeight;
1247 1251
1252 scoped_ptr<ExtensionToolbarMenuView> extension_toolbar_menu_view;
1253 if (model->GetCommandIdAt(i) == IDC_EXTENSIONS_OVERFLOW_MENU) {
1254 extension_toolbar_menu_view.reset(new ExtensionToolbarMenuView(browser_));
1255 height = extension_toolbar_menu_view->GetPreferredSize().height();
1256 }
1257
1248 // Add the menu item at the end. 1258 // Add the menu item at the end.
1249 int menu_index = parent->HasSubmenu() ? 1259 int menu_index = parent->HasSubmenu() ?
1250 parent->GetSubmenu()->child_count() : 0; 1260 parent->GetSubmenu()->child_count() : 0;
1251 MenuItemView* item = AddMenuItem( 1261 MenuItemView* item = AddMenuItem(
1252 parent, menu_index, model, i, model->GetTypeAt(i), height); 1262 parent, menu_index, model, i, model->GetTypeAt(i), height);
1253 1263
1254 if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) 1264 if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU)
1255 PopulateMenu(item, model->GetSubmenuModelAt(i)); 1265 PopulateMenu(item, model->GetSubmenuModelAt(i));
1256 1266
1257 switch (model->GetCommandIdAt(i)) { 1267 switch (model->GetCommandIdAt(i)) {
1268 case IDC_EXTENSIONS_OVERFLOW_MENU:
1269 if (height > 0)
1270 item->AddChildView(extension_toolbar_menu_view.release());
1271 else
1272 item->SetVisible(false);
1273 break;
1258 case IDC_CUT: 1274 case IDC_CUT:
1259 DCHECK_EQ(MenuModel::TYPE_COMMAND, model->GetTypeAt(i)); 1275 DCHECK_EQ(MenuModel::TYPE_COMMAND, model->GetTypeAt(i));
1260 DCHECK_LT(i + 2, max); 1276 DCHECK_LT(i + 2, max);
1261 DCHECK_EQ(IDC_COPY, model->GetCommandIdAt(i + 1)); 1277 DCHECK_EQ(IDC_COPY, model->GetCommandIdAt(i + 1));
1262 DCHECK_EQ(IDC_PASTE, model->GetCommandIdAt(i + 2)); 1278 DCHECK_EQ(IDC_PASTE, model->GetCommandIdAt(i + 2));
1263 item->SetTitle(l10n_util::GetStringUTF16(IDS_EDIT2)); 1279 item->SetTitle(l10n_util::GetStringUTF16(IDS_EDIT2));
1264 item->AddChildView(new CutCopyPasteView(this, model, 1280 item->AddChildView(new CutCopyPasteView(this, model,
1265 i, i + 1, i + 2)); 1281 i, i + 1, i + 2));
1266 i += 2; 1282 i += 2;
1267 break; 1283 break;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1321 } 1337 }
1322 command_id_to_entry_[command_id].first = model; 1338 command_id_to_entry_[command_id].first = model;
1323 command_id_to_entry_[command_id].second = model_index; 1339 command_id_to_entry_[command_id].second = model_index;
1324 } 1340 }
1325 1341
1326 MenuItemView* menu_item = NULL; 1342 MenuItemView* menu_item = NULL;
1327 if (height > 0) { 1343 if (height > 0) {
1328 // For menu items with a special menu height we use our special class to be 1344 // For menu items with a special menu height we use our special class to be
1329 // able to modify the item height. 1345 // able to modify the item height.
1330 menu_item = new ButtonContainerMenuItemView(parent, command_id, height); 1346 menu_item = new ButtonContainerMenuItemView(parent, command_id, height);
1347 if (!parent->GetSubmenu())
1348 parent->CreateSubmenu();
Finnur 2014/06/26 14:55:48 This is what fixes your crash. The AddMenuItem fun
1331 parent->GetSubmenu()->AddChildViewAt(menu_item, menu_index); 1349 parent->GetSubmenu()->AddChildViewAt(menu_item, menu_index);
1332 } else { 1350 } else {
1333 // For all other cases we use the more generic way to add menu items. 1351 // For all other cases we use the more generic way to add menu items.
1334 menu_item = views::MenuModelAdapter::AddMenuItemFromModelAt( 1352 menu_item = views::MenuModelAdapter::AddMenuItemFromModelAt(
1335 model, model_index, parent, menu_index, command_id); 1353 model, model_index, parent, menu_index, command_id);
1336 } 1354 }
1337 1355
1338 if (menu_item) { 1356 if (menu_item) {
1339 // Flush all buttons to the right side of the menu for the new menu type. 1357 // Flush all buttons to the right side of the menu for the new menu type.
1340 menu_item->set_use_right_margin(!use_new_menu_); 1358 menu_item->set_use_right_margin(!use_new_menu_);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1382 0, 1400 0,
1383 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, 1401 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS,
1384 BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU); 1402 BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU);
1385 } 1403 }
1386 1404
1387 int WrenchMenu::ModelIndexFromCommandId(int command_id) const { 1405 int WrenchMenu::ModelIndexFromCommandId(int command_id) const {
1388 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); 1406 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id);
1389 DCHECK(ix != command_id_to_entry_.end()); 1407 DCHECK(ix != command_id_to_entry_.end());
1390 return ix->second.second; 1408 return ix->second.second;
1391 } 1409 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698