File: shelf_application_menu_model.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (68 lines) | stat: -rw-r--r-- 2,512 bytes parent folder | download | duplicates (7)
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
// 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