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