1 // Copyright 2020 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
5 #include "chrome/browser/ui/tabs/existing_base_sub_menu_model.h"
6
ExistingBaseSubMenuModel(ui::SimpleMenuModel::Delegate * parent_delegate,TabStripModel * model,int context_index,int min_command_id)7 ExistingBaseSubMenuModel::ExistingBaseSubMenuModel(
8 ui::SimpleMenuModel::Delegate* parent_delegate,
9 TabStripModel* model,
10 int context_index,
11 int min_command_id)
12 : SimpleMenuModel(this),
13 parent_delegate_(parent_delegate),
14 model_(model),
15 context_index_(context_index),
16 min_command_id_(min_command_id) {}
17
GetAcceleratorForCommandId(int command_id,ui::Accelerator * accelerator) const18 bool ExistingBaseSubMenuModel::GetAcceleratorForCommandId(
19 int command_id,
20 ui::Accelerator* accelerator) const {
21 return false;
22 }
23
IsCommandIdChecked(int command_id) const24 bool ExistingBaseSubMenuModel::IsCommandIdChecked(int command_id) const {
25 return false;
26 }
27
IsCommandIdEnabled(int command_id) const28 bool ExistingBaseSubMenuModel::IsCommandIdEnabled(int command_id) const {
29 return true;
30 }
31
32 constexpr int ExistingBaseSubMenuModel::kMinExistingWindowCommandId;
33 constexpr int ExistingBaseSubMenuModel::kMinExistingTabGroupCommandId;
34
ExecuteCommand(int command_id,int event_flags)35 void ExistingBaseSubMenuModel::ExecuteCommand(int command_id, int event_flags) {
36 if (IsNewCommand(command_id)) {
37 ExecuteNewCommand(event_flags);
38 return;
39 }
40 ExecuteExistingCommand(command_id - min_command_id_ - 1);
41 }
42
43 ExistingBaseSubMenuModel::~ExistingBaseSubMenuModel() = default;
44
MenuItemInfo(const base::string16 menu_text)45 ExistingBaseSubMenuModel::MenuItemInfo::MenuItemInfo(
46 const base::string16 menu_text)
47 : text(menu_text) {
48 image = base::nullopt;
49 }
50
MenuItemInfo(const base::string16 & menu_text,ui::ImageModel menu_image)51 ExistingBaseSubMenuModel::MenuItemInfo::MenuItemInfo(
52 const base::string16& menu_text,
53 ui::ImageModel menu_image)
54 : text(menu_text) {
55 image = base::Optional<ui::ImageModel>{menu_image};
56 }
57
58 ExistingBaseSubMenuModel::MenuItemInfo::MenuItemInfo(
59 const MenuItemInfo& menu_item_info) = default;
60
61 ExistingBaseSubMenuModel::MenuItemInfo::~MenuItemInfo() = default;
62
Build(int new_text,std::vector<MenuItemInfo> menu_item_infos)63 void ExistingBaseSubMenuModel::Build(
64 int new_text,
65 std::vector<MenuItemInfo> menu_item_infos) {
66 AddItemWithStringId(min_command_id_, new_text);
67 AddSeparator(ui::NORMAL_SEPARATOR);
68
69 // Start command ids after the parent menu's ids to avoid collisions.
70 int group_index = min_command_id_ + 1;
71 for (auto item : menu_item_infos) {
72 if (group_index > min_command_id_ + max_size)
73 break;
74
75 if (item.image.has_value()) {
76 AddItemWithIcon(group_index, item.text, item.image.value());
77 } else {
78 AddItem(group_index, item.text);
79 }
80 group_index++;
81 }
82 }
83
ExecuteNewCommand(int event_flags)84 void ExistingBaseSubMenuModel::ExecuteNewCommand(int event_flags) {}
85
ExecuteExistingCommand(int command_index)86 void ExistingBaseSubMenuModel::ExecuteExistingCommand(int command_index) {}
87