| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "ash/common/shelf/shelf_application_menu_model.h" | 5 #include "ash/common/shelf/shelf_application_menu_model.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "ash/common/shelf/shelf_item_delegate.h" |
| 12 #include "ash/public/cpp/shelf_application_menu_item.h" | 13 #include "ash/public/cpp/shelf_application_menu_item.h" |
| 13 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 const int kInvalidCommandId = std::numeric_limits<int>::max(); | 18 const int kInvalidCommandId = std::numeric_limits<int>::max(); |
| 18 | 19 |
| 19 } // namespace | 20 } // namespace |
| 20 | 21 |
| 21 namespace ash { | 22 namespace ash { |
| 22 | 23 |
| 23 ShelfApplicationMenuModel::ShelfApplicationMenuModel( | 24 ShelfApplicationMenuModel::ShelfApplicationMenuModel( |
| 24 const base::string16& title, | 25 const base::string16& title, |
| 25 ShelfAppMenuItemList items) | 26 ShelfAppMenuItemList items, |
| 26 : ui::SimpleMenuModel(this), items_(std::move(items)) { | 27 ShelfItemDelegate* delegate) |
| 28 : ui::SimpleMenuModel(this), items_(std::move(items)), delegate_(delegate) { |
| 27 AddSeparator(ui::SPACING_SEPARATOR); | 29 AddSeparator(ui::SPACING_SEPARATOR); |
| 28 AddItem(kInvalidCommandId, title); | 30 AddItem(kInvalidCommandId, title); |
| 29 AddSeparator(ui::SPACING_SEPARATOR); | 31 AddSeparator(ui::SPACING_SEPARATOR); |
| 30 | 32 |
| 31 for (size_t i = 0; i < items_.size(); i++) { | 33 for (size_t i = 0; i < items_.size(); i++) { |
| 32 ShelfApplicationMenuItem* item = items_[i].get(); | 34 ShelfApplicationMenuItem* item = items_[i].get(); |
| 33 AddItem(i, item->title()); | 35 AddItem(i, item->title()); |
| 34 if (!item->icon().IsEmpty()) | 36 if (!item->icon().IsEmpty()) |
| 35 SetIcon(GetIndexOfCommandId(i), item->icon()); | 37 SetIcon(GetIndexOfCommandId(i), item->icon()); |
| 36 } | 38 } |
| 37 | 39 |
| 38 // SimpleMenuModel does not allow two consecutive spacing separator items. | 40 // SimpleMenuModel does not allow two consecutive spacing separator items. |
| 39 // This only occurs in tests; users should not see menus with no |items_|. | 41 // This only occurs in tests; users should not see menus with no |items_|. |
| 40 if (!items_.empty()) | 42 if (!items_.empty()) |
| 41 AddSeparator(ui::SPACING_SEPARATOR); | 43 AddSeparator(ui::SPACING_SEPARATOR); |
| 42 } | 44 } |
| 43 | 45 |
| 44 ShelfApplicationMenuModel::~ShelfApplicationMenuModel() {} | 46 ShelfApplicationMenuModel::~ShelfApplicationMenuModel() {} |
| 45 | 47 |
| 46 bool ShelfApplicationMenuModel::IsCommandIdChecked(int command_id) const { | 48 bool ShelfApplicationMenuModel::IsCommandIdChecked(int command_id) const { |
| 47 return false; | 49 return false; |
| 48 } | 50 } |
| 49 | 51 |
| 50 bool ShelfApplicationMenuModel::IsCommandIdEnabled(int command_id) const { | 52 bool ShelfApplicationMenuModel::IsCommandIdEnabled(int command_id) const { |
| 51 return command_id >= 0 && static_cast<size_t>(command_id) < items_.size(); | 53 return command_id >= 0 && static_cast<size_t>(command_id) < items_.size(); |
| 52 } | 54 } |
| 53 | 55 |
| 54 void ShelfApplicationMenuModel::ExecuteCommand(int command_id, | 56 void ShelfApplicationMenuModel::ExecuteCommand(int command_id, |
| 55 int event_flags) { | 57 int event_flags) { |
| 58 DCHECK(delegate_); |
| 56 DCHECK(IsCommandIdEnabled(command_id)); | 59 DCHECK(IsCommandIdEnabled(command_id)); |
| 57 items_[command_id]->Execute(event_flags); | 60 // Have the delegate execute its own custom command id for the given item. |
| 61 delegate_->ExecuteCommand(items_[command_id]->command_id(), event_flags); |
| 58 RecordMenuItemSelectedMetrics(command_id, items_.size()); | 62 RecordMenuItemSelectedMetrics(command_id, items_.size()); |
| 59 } | 63 } |
| 60 | 64 |
| 61 void ShelfApplicationMenuModel::RecordMenuItemSelectedMetrics( | 65 void ShelfApplicationMenuModel::RecordMenuItemSelectedMetrics( |
| 62 int command_id, | 66 int command_id, |
| 63 int num_menu_items_enabled) { | 67 int num_menu_items_enabled) { |
| 64 UMA_HISTOGRAM_COUNTS_100("Ash.Shelf.Menu.SelectedMenuItemIndex", command_id); | 68 UMA_HISTOGRAM_COUNTS_100("Ash.Shelf.Menu.SelectedMenuItemIndex", command_id); |
| 65 UMA_HISTOGRAM_COUNTS_100("Ash.Shelf.Menu.NumItemsEnabledUponSelection", | 69 UMA_HISTOGRAM_COUNTS_100("Ash.Shelf.Menu.NumItemsEnabledUponSelection", |
| 66 num_menu_items_enabled); | 70 num_menu_items_enabled); |
| 67 } | 71 } |
| 68 | 72 |
| 69 } // namespace ash | 73 } // namespace ash |
| OLD | NEW |