shelf_application_menu_model.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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
  3. // found in the LICENSE file.
  4. #include "ash/shelf/shelf_application_menu_model.h"
  5. #include <algorithm>
  6. #include <limits>
  7. #include "ash/app_list/app_list_controller_impl.h"
  8. #include "ash/public/cpp/app_menu_constants.h"
  9. #include "ash/shell.h"
  10. #include "base/metrics/histogram_macros.h"
  11. #include "ui/base/models/image_model.h"
  12. #include "ui/display/types/display_constants.h"
  13. #include "ui/gfx/favicon_size.h"
  14. namespace ash {
  15. ShelfApplicationMenuModel::ShelfApplicationMenuModel(
  16. const std::u16string& title,
  17. const Items& items,
  18. ShelfItemDelegate* delegate)
  19. : ui::SimpleMenuModel(this), delegate_(delegate) {
  20. AddTitle(title);
  21. // Add an empty icon to the title for it to be aligned with the other menu
  22. // item elements. See crbug/1117650.
  23. SetIcon(0,
  24. ui::ImageModel::FromImageGenerator(
  25. base::BindRepeating([](const ui::ColorProvider* color_provider) {
  26. return gfx::ImageSkia();
  27. }),
  28. gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)));
  29. for (const auto& item : items) {
  30. enabled_commands_.emplace(item.command_id);
  31. AddItemWithIcon(item.command_id, item.title,
  32. ui::ImageModel::FromImageSkia(item.icon));
  33. }
  34. AddSeparator(ui::SPACING_SEPARATOR);
  35. DCHECK_EQ(GetItemCount(), items.size() + 2) << "Update metrics |- 2|";
  36. }
  37. ShelfApplicationMenuModel::~ShelfApplicationMenuModel() = default;
  38. bool ShelfApplicationMenuModel::IsCommandIdEnabled(int command_id) const {
  39. // This enables items added in the constructor, but not the title.
  40. return enabled_commands_.contains(command_id);
  41. }
  42. void ShelfApplicationMenuModel::ExecuteCommand(int command_id,
  43. int event_flags) {
  44. DCHECK(IsCommandIdEnabled(command_id));
  45. // Have the delegate execute its own custom command id for the given item.
  46. if (delegate_) {
  47. // Record app launch when selecting window to open from disambiguation
  48. // menu.
  49. Shell::Get()->app_list_controller()->RecordShelfAppLaunched();
  50. // The display hosting the menu is irrelevant, windows activate in-place.
  51. delegate_->ExecuteCommand(false /*from_context_menu*/, command_id,
  52. event_flags, display::kInvalidDisplayId);
  53. }
  54. // Subtract two to avoid counting the title and separator.
  55. RecordMenuItemSelectedMetrics(command_id,
  56. std::max(GetItemCount(), size_t{2}) - 2);
  57. }
  58. void ShelfApplicationMenuModel::RecordMenuItemSelectedMetrics(
  59. int command_id,
  60. int num_menu_items_enabled) {
  61. UMA_HISTOGRAM_COUNTS_100("Ash.Shelf.Menu.SelectedMenuItemIndex", command_id);
  62. UMA_HISTOGRAM_COUNTS_100("Ash.Shelf.Menu.NumItemsEnabledUponSelection",
  63. num_menu_items_enabled);
  64. }
  65. } // namespace ash