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
|
// Copyright 2022 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/views/controls/combobox/combobox_menu_model.h"
#include "ui/base/models/combobox_model.h"
ComboboxMenuModel::ComboboxMenuModel(views::Combobox* owner,
ui::ComboboxModel* model)
: owner_(owner), model_(model) {}
ComboboxMenuModel::~ComboboxMenuModel() = default;
bool ComboboxMenuModel::UseCheckmarks() const {
switch (model_->GetCheckmarkConfig()) {
case ui::ComboboxModel::ItemCheckmarkConfig::kDefault:
return views::MenuConfig::instance().check_selected_combobox_item;
case ui::ComboboxModel::ItemCheckmarkConfig::kDisabled:
return false;
case ui::ComboboxModel::ItemCheckmarkConfig::kEnabled:
return true;
}
}
// Overridden from MenuModel:
base::WeakPtr<ui::MenuModel> ComboboxMenuModel::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
size_t ComboboxMenuModel::GetItemCount() const {
return model_->GetItemCount();
}
ui::MenuModel::ItemType ComboboxMenuModel::GetTypeAt(size_t index) const {
if (model_->IsItemSeparatorAt(index)) {
return TYPE_SEPARATOR;
}
if (model_->IsItemTitleAt(index)) {
return TYPE_TITLE;
}
return UseCheckmarks() ? TYPE_CHECK : TYPE_COMMAND;
}
ui::MenuSeparatorType ComboboxMenuModel::GetSeparatorTypeAt(
size_t index) const {
return ui::NORMAL_SEPARATOR;
}
int ComboboxMenuModel::GetCommandIdAt(size_t index) const {
// Define the id of the first item in the menu (since it needs to be > 0)
constexpr int kFirstMenuItemId = 1000;
return static_cast<int>(index) + kFirstMenuItemId;
}
std::u16string ComboboxMenuModel::GetLabelAt(size_t index) const {
// Inserting the Unicode formatting characters if necessary so that the
// text is displayed correctly in right-to-left UIs.
std::u16string text = model_->GetItemAt(index);
base::i18n::AdjustStringForLocaleDirection(&text);
return text;
}
std::u16string ComboboxMenuModel::GetSecondaryLabelAt(size_t index) const {
std::u16string text = model_->GetDropDownSecondaryTextAt(index);
base::i18n::AdjustStringForLocaleDirection(&text);
return text;
}
bool ComboboxMenuModel::IsItemDynamicAt(size_t index) const {
return true;
}
const gfx::FontList* ComboboxMenuModel::GetLabelFontListAt(size_t index) const {
return &owner_->GetFontList();
}
bool ComboboxMenuModel::GetAcceleratorAt(size_t index,
ui::Accelerator* accelerator) const {
return false;
}
bool ComboboxMenuModel::IsItemCheckedAt(size_t index) const {
return UseCheckmarks() && index == owner_->GetSelectedIndex();
}
int ComboboxMenuModel::GetGroupIdAt(size_t index) const {
return -1;
}
ui::ImageModel ComboboxMenuModel::GetIconAt(size_t index) const {
return model_->GetDropDownIconAt(index);
}
ui::ButtonMenuItemModel* ComboboxMenuModel::GetButtonMenuItemAt(
size_t index) const {
return nullptr;
}
bool ComboboxMenuModel::IsEnabledAt(size_t index) const {
return model_->IsItemEnabledAt(index);
}
void ComboboxMenuModel::ActivatedAt(size_t index) {
owner_->MenuSelectionAt(index);
}
void ComboboxMenuModel::ActivatedAt(size_t index, int event_flags) {
ActivatedAt(index);
}
ui::MenuModel* ComboboxMenuModel::GetSubmenuModelAt(size_t index) const {
return nullptr;
}
std::optional<ui::ColorId> ComboboxMenuModel::GetForegroundColorId(
size_t index) {
return model_->GetDropdownForegroundColorIdAt(index);
}
std::optional<ui::ColorId> ComboboxMenuModel::GetSubmenuBackgroundColorId(
size_t index) {
return model_->GetDropdownBackgroundColorIdAt(index);
}
std::optional<ui::ColorId> ComboboxMenuModel::GetSelectedBackgroundColorId(
size_t index) {
return model_->GetDropdownSelectedBackgroundColorIdAt(index);
}
|