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

ash / shelf / shelf_application_menu_model.cc [blame]

// Copyright 2017 The Chromium Authors
// 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 <algorithm>
#include <limits>

#include "ash/app_list/app_list_controller_impl.h"
#include "ash/public/cpp/app_menu_constants.h"
#include "ash/shell.h"
#include "base/metrics/histogram_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/display/types/display_constants.h"
#include "ui/gfx/favicon_size.h"

namespace ash {

ShelfApplicationMenuModel::ShelfApplicationMenuModel(
    const std::u16string& title,
    const Items& items,
    ShelfItemDelegate* delegate)
    : ui::SimpleMenuModel(this), delegate_(delegate) {
  AddTitle(title);
  for (const auto& item : items) {
    enabled_commands_.emplace(item.command_id);
    AddItemWithIcon(item.command_id, item.title,
                    ui::ImageModel::FromImageSkia(item.icon));
  }
  AddSeparator(ui::SPACING_SEPARATOR);
  DCHECK_EQ(GetItemCount(), items.size() + 2) << "Update metrics |- 2|";
}

ShelfApplicationMenuModel::~ShelfApplicationMenuModel() = default;

bool ShelfApplicationMenuModel::IsCommandIdEnabled(int command_id) const {
  // This enables items added in the constructor, but not the title.
  return enabled_commands_.contains(command_id);
}

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_) {
    // Record app launch when selecting window to open from disambiguation
    // menu.
    Shell::Get()->app_list_controller()->RecordShelfAppLaunched();

    // The display hosting the menu is irrelevant, windows activate in-place.
    delegate_->ExecuteCommand(false /*from_context_menu*/, command_id,
                              event_flags, display::kInvalidDisplayId);
  }
  // Subtract two to avoid counting the title and separator.
  RecordMenuItemSelectedMetrics(command_id,
                                std::max(GetItemCount(), size_t{2}) - 2);
}

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