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 144 145 146 147 148 149 150 151
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/models/dialog_model_menu_model_adapter.h"
#include "ui/base/models/dialog_model.h"
namespace ui {
DialogModelMenuModelAdapter::DialogModelMenuModelAdapter(
std::unique_ptr<DialogModel> model)
: model_(std::move(model)) {}
DialogModelMenuModelAdapter::~DialogModelMenuModelAdapter() = default;
void DialogModelMenuModelAdapter::Close() {
// TODO(pbos): Implement, or document why menus can't be closed through this
// interface.
NOTREACHED();
}
// TODO(pbos): This should probably not be hosting a DialogModel but rather
// another model with DialogModelSection(s).
void DialogModelMenuModelAdapter::OnDialogButtonChanged() {
NOTREACHED();
}
base::WeakPtr<ui::MenuModel> DialogModelMenuModelAdapter::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
size_t DialogModelMenuModelAdapter::GetItemCount() const {
return model_->fields(DialogModelHost::GetPassKey()).size();
}
MenuModel::ItemType DialogModelMenuModelAdapter::GetTypeAt(size_t index) const {
const auto type = GetField(index)->type();
if (type == DialogModelField::kTitleItem) {
return TYPE_TITLE;
}
return type == DialogModelField::kSeparator ? TYPE_SEPARATOR : TYPE_COMMAND;
}
MenuSeparatorType DialogModelMenuModelAdapter::GetSeparatorTypeAt(
size_t index) const {
CHECK_EQ(GetField(index)->type(), DialogModelField::kSeparator);
return MenuSeparatorType::NORMAL_SEPARATOR;
}
int DialogModelMenuModelAdapter::GetCommandIdAt(size_t index) const {
const auto type = GetField(index)->type();
if (type == DialogModelField::kTitleItem) {
return ui::MenuModel::kTitleId;
}
// TODO(pbos): Figure out what this should be. Combobox seems to offset by
// 1000. Dunno why.
return static_cast<int>(index + 1234);
}
std::u16string DialogModelMenuModelAdapter::GetLabelAt(size_t index) const {
const DialogModelField* const field = GetField(index);
if (field->type() == DialogModelField::kTitleItem) {
return field->AsTitleItem()->label();
}
return field->AsMenuItem()->label();
}
bool DialogModelMenuModelAdapter::IsItemDynamicAt(size_t index) const {
return false;
}
bool DialogModelMenuModelAdapter::GetAcceleratorAt(
size_t index,
ui::Accelerator* accelerator) const {
// TODO(pbos): Add support for accelerators.
return false;
}
bool DialogModelMenuModelAdapter::IsItemCheckedAt(size_t index) const {
// TODO(pbos): Add support for checkbox items.
return false;
}
int DialogModelMenuModelAdapter::GetGroupIdAt(size_t index) const {
NOTREACHED();
}
ImageModel DialogModelMenuModelAdapter::GetIconAt(size_t index) const {
const DialogModelField* const field = GetField(index);
if (field->type() == DialogModelField::kTitleItem) {
return ImageModel();
}
return field->AsMenuItem()->icon();
}
ButtonMenuItemModel* DialogModelMenuModelAdapter::GetButtonMenuItemAt(
size_t index) const {
NOTREACHED();
}
bool DialogModelMenuModelAdapter::IsEnabledAt(size_t index) const {
CHECK_LT(index, GetItemCount());
const DialogModelField* const field = GetField(index);
// Non-interactive title should be disabled.
if (field->type() == DialogModelField::kTitleItem) {
return false;
}
return field->type() != DialogModelField::kSeparator &&
field->AsMenuItem()->is_enabled();
}
ui::ElementIdentifier DialogModelMenuModelAdapter::GetElementIdentifierAt(
size_t index) const {
CHECK_LT(index, GetItemCount());
const DialogModelField* const field = GetField(index);
if (field->type() == DialogModelField::kTitleItem) {
return field->id();
}
return field->AsMenuItem()->id();
}
MenuModel* DialogModelMenuModelAdapter::GetSubmenuModelAt(size_t index) const {
NOTREACHED();
}
void DialogModelMenuModelAdapter::ActivatedAt(size_t index) {
// If this flags investigate why the ActivatedAt(index, event_flags) isn't
// being called.
NOTREACHED();
}
void DialogModelMenuModelAdapter::ActivatedAt(size_t index, int event_flags) {
DialogModelMenuItem* menu_item = GetField(index)->AsMenuItem();
menu_item->OnActivated(DialogModelFieldHost::GetPassKey(), event_flags);
}
const DialogModelField* DialogModelMenuModelAdapter::GetField(
size_t index) const {
CHECK_LT(index, GetItemCount());
return model_->fields(DialogModelHost::GetPassKey())[index].get();
}
DialogModelField* DialogModelMenuModelAdapter::GetField(size_t index) {
return const_cast<DialogModelField*>(
const_cast<const DialogModelMenuModelAdapter*>(this)->GetField(index));
}
} // namespace ui
|