forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshelf_application_menu_model.cc
75 lines (58 loc) · 2.36 KB
/
shelf_application_menu_model.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/shelf/shelf_application_menu_model.h"
#include <stddef.h>
#include <limits>
#include <utility>
#include "ash/public/cpp/shelf_item_delegate.h"
#include "base/metrics/histogram_macros.h"
#include "ui/gfx/image/image.h"
namespace {
const int kInvalidCommandId = std::numeric_limits<int>::max();
} // namespace
namespace ash {
ShelfApplicationMenuModel::ShelfApplicationMenuModel(
const base::string16& title,
std::vector<mojom::MenuItemPtr> items,
ShelfItemDelegate* delegate)
: ui::SimpleMenuModel(this), items_(std::move(items)), delegate_(delegate) {
AddSeparator(ui::SPACING_SEPARATOR);
AddItem(kInvalidCommandId, title);
AddSeparator(ui::SPACING_SEPARATOR);
for (size_t i = 0; i < items_.size(); i++) {
mojom::MenuItem* item = items_[i].get();
AddItem(i, item->label);
if (!item->image.isNull()) {
SetIcon(GetIndexOfCommandId(i),
gfx::Image::CreateFrom1xBitmap(item->image));
}
}
// SimpleMenuModel does not allow two consecutive spacing separator items.
// This only occurs in tests; users should not see menus with no |items_|.
if (!items_.empty())
AddSeparator(ui::SPACING_SEPARATOR);
}
ShelfApplicationMenuModel::~ShelfApplicationMenuModel() {}
bool ShelfApplicationMenuModel::IsCommandIdChecked(int command_id) const {
return false;
}
bool ShelfApplicationMenuModel::IsCommandIdEnabled(int command_id) const {
return command_id >= 0 && static_cast<size_t>(command_id) < items_.size();
}
void ShelfApplicationMenuModel::ExecuteCommand(int command_id,
int event_flags) {
DCHECK(IsCommandIdEnabled(command_id));
// Have the delegate execute its own custom command id for the given item.
if (delegate_)
delegate_->ExecuteCommand(items_[command_id]->command_id, event_flags);
RecordMenuItemSelectedMetrics(command_id, items_.size());
}
void ShelfApplicationMenuModel::RecordMenuItemSelectedMetrics(
int command_id,
int num_menu_items_enabled) {
UMA_HISTOGRAM_COUNTS_100("Ash.Shelf.Menu.SelectedMenuItemIndex", command_id);
UMA_HISTOGRAM_COUNTS_100("Ash.Shelf.Menu.NumItemsEnabledUponSelection",
num_menu_items_enabled);
}
} // namespace ash