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

Unified Diff: ash/common/shelf/shelf_application_menu_model.cc

Issue 2671923002: mash: Cleanup ash shelf application menu code. (Closed)
Patch Set: Fix tests; cleanup. Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: ash/common/shelf/shelf_application_menu_model.cc
diff --git a/ash/common/shelf/shelf_application_menu_model.cc b/ash/common/shelf/shelf_application_menu_model.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8695831c9d8ed5f6ed1e2129d2fb6d1a7bc81987
--- /dev/null
+++ b/ash/common/shelf/shelf_application_menu_model.cc
@@ -0,0 +1,72 @@
+// 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/common/shelf/shelf_application_menu_model.h"
+
+#include <stddef.h>
+
+#include <utility>
+
+#include "ash/public/cpp/shelf_application_menu_item.h"
+#include "base/metrics/histogram_macros.h"
+
+namespace {
+
+const int kInvalidCommandId = std::numeric_limits<int>::max();
James Cook 2017/02/06 17:30:21 nit: #include <limits> or use -1
msw 2017/02/07 09:12:00 I included <limits>, negative values sadly doesn't
James Cook 2017/02/07 16:32:24 Acknowledged.
+
+const char kNumItemsEnabledHistogramName[] =
+ "Ash.Shelf.Menu.NumItemsEnabledUponSelection";
James Cook 2017/02/06 17:30:21 I would just inline these histogram names below.
msw 2017/02/07 09:12:00 Done.
+
+const char kSelectedMenuItemIndexHistogramName[] =
+ "Ash.Shelf.Menu.SelectedMenuItemIndex";
+
+} // namespace
+
+namespace ash {
+
+ShelfApplicationMenuModel::ShelfApplicationMenuModel(
+ const base::string16& title,
+ ShelfApplicationMenuItems items)
+ : ui::SimpleMenuModel(this), items_(std::move(items)) {
+ AddSeparator(ui::SPACING_SEPARATOR);
James Cook 2017/02/06 17:30:21 Do we want this if items_ is empty? In the old cod
msw 2017/02/07 09:12:00 Hmm, there are tests for application menus without
James Cook 2017/02/07 16:32:24 I would just add a comment somewhere in here that
msw 2017/02/07 18:19:15 Done.
+ AddItem(kInvalidCommandId, title);
+ AddSeparator(ui::SPACING_SEPARATOR);
+
+ for (size_t i = 0; i < items_.size(); i++) {
+ ShelfApplicationMenuItem* item = items_[i].get();
+ AddItem(i, item->title());
+ if (!item->icon().IsEmpty())
+ SetIcon(GetIndexOfCommandId(i), item->icon());
+ }
+
+ 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();
James Cook 2017/02/06 17:30:21 optional fyi: size_t{foo} or int{bar} is acceptabl
msw 2017/02/07 09:12:00 Interesting, but I don't see any uses of that cast
+}
+
+void ShelfApplicationMenuModel::ExecuteCommand(int command_id,
+ int event_flags) {
+ DCHECK(IsCommandIdEnabled(command_id));
+ items_[command_id]->Execute(event_flags);
+ RecordMenuItemSelectedMetrics(command_id, items_.size());
+}
+
+void ShelfApplicationMenuModel::RecordMenuItemSelectedMetrics(
+ int command_id,
+ int num_menu_items_enabled) {
+ UMA_HISTOGRAM_COUNTS_100(kSelectedMenuItemIndexHistogramName, command_id);
+ UMA_HISTOGRAM_COUNTS_100(kNumItemsEnabledHistogramName,
+ num_menu_items_enabled);
+}
+
+} // namespace ash

Powered by Google App Engine
This is Rietveld 408576698