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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/remote_cocoa/app_shim/mojo_menu_model.h"
#include "ui/base/models/image_model.h"
namespace remote_cocoa {
MojoMenuModel::MojoMenuModel(std::vector<mojom::MenuItemPtr> menu_items,
mojom::MenuHost* menu_host)
: menu_items_(std::move(menu_items)), menu_host_(menu_host) {
submenus_.resize(menu_items_.size());
}
MojoMenuModel::~MojoMenuModel() = default;
base::WeakPtr<ui::MenuModel> MojoMenuModel::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
size_t MojoMenuModel::GetItemCount() const {
return menu_items_.size();
}
ui::MenuModel::ItemType MojoMenuModel::GetTypeAt(size_t index) const {
switch (menu_items_[index]->which()) {
case mojom::MenuItem::Tag::kSeparator:
return TYPE_SEPARATOR;
case mojom::MenuItem::Tag::kRegular:
return TYPE_COMMAND;
case mojom::MenuItem::Tag::kSubmenu:
return TYPE_SUBMENU;
}
NOTREACHED();
}
ui::MenuSeparatorType MojoMenuModel::GetSeparatorTypeAt(size_t index) const {
// Not used by MenuControllerCocoa.
return ui::NORMAL_SEPARATOR;
}
int MojoMenuModel::GetCommandIdAt(size_t index) const {
return GetCommonFieldsAt(index)->command_id;
}
std::u16string MojoMenuModel::GetLabelAt(size_t index) const {
return GetCommonFieldsAt(index)->label;
}
bool MojoMenuModel::IsItemDynamicAt(size_t index) const {
// For now we don't support dynamic menu items. This doesn't matter since only
// dynamic items in submenus would be meaningful anyway (as a MojoMenuModel is
// never displayed more than once, so the top-level menu can't be shown
// multiple times).
return false;
}
bool MojoMenuModel::MayHaveMnemonicsAt(size_t index) const {
return GetCommonFieldsAt(index)->may_have_mnemonics;
}
bool MojoMenuModel::GetAcceleratorAt(size_t index,
ui::Accelerator* accelerator) const {
// On macOS context menus never have accelerators. Since this class is only
// used for context menus, this always returns false.
return false;
}
bool MojoMenuModel::IsItemCheckedAt(size_t index) const {
return GetCommonFieldsAt(index)->is_checked;
}
int MojoMenuModel::GetGroupIdAt(size_t index) const {
// Not used by MenuControllerCocoa.
return -1;
}
ui::ImageModel MojoMenuModel::GetIconAt(size_t index) const {
if (const auto& icon = GetCommonFieldsAt(index)->icon; !icon.isNull()) {
return ui::ImageModel::FromImageSkia(icon);
}
return {};
}
ui::ButtonMenuItemModel* MojoMenuModel::GetButtonMenuItemAt(
size_t index) const {
// Not used by MenuControllerCocoa.
return nullptr;
}
bool MojoMenuModel::IsEnabledAt(size_t index) const {
return GetCommonFieldsAt(index)->is_enabled;
}
bool MojoMenuModel::IsVisibleAt(size_t index) const {
return GetCommonFieldsAt(index)->is_visible;
}
bool MojoMenuModel::IsAlertedAt(size_t index) const {
return GetCommonFieldsAt(index)->is_alerted;
}
bool MojoMenuModel::IsNewFeatureAt(size_t index) const {
return GetCommonFieldsAt(index)->is_new_feature;
}
MojoMenuModel* MojoMenuModel::GetSubmenuModelAt(size_t index) const {
if (!menu_items_[index]->is_submenu()) {
return nullptr;
}
std::unique_ptr<MojoMenuModel>& submenu = submenus_[index];
if (!submenu) {
submenu = std::make_unique<MojoMenuModel>(
std::move(menu_items_[index]->get_submenu()->children), menu_host_);
}
return submenu.get();
}
void MojoMenuModel::ActivatedAt(size_t index) {
ActivatedAt(index, /*event_flags=*/0);
}
void MojoMenuModel::ActivatedAt(size_t index, int event_flags) {
menu_host_->CommandActivated(GetCommonFieldsAt(index)->command_id,
event_flags);
}
mojom::MenuItemCommonFields* MojoMenuModel::GetCommonFieldsAt(
size_t index) const {
switch (menu_items_[index]->which()) {
case mojom::MenuItem::Tag::kSeparator:
return menu_items_[index]->get_separator().get();
case mojom::MenuItem::Tag::kRegular:
return menu_items_[index]->get_regular().get();
case mojom::MenuItem::Tag::kSubmenu:
return menu_items_[index]->get_submenu()->common.get();
}
NOTREACHED();
}
} // namespace remote_cocoa
|