// Copyright 2012 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.h" #include #include #include #include "base/bind.h" #include "base/check_op.h" #include "base/memory/raw_ptr.h" #include "build/build_config.h" #include "ui/accessibility/ax_action_data.h" #include "ui/accessibility/ax_enums.mojom.h" #include "ui/accessibility/ax_node_data.h" #include "ui/base/ime/input_method.h" #include "ui/base/menu_source_utils.h" #include "ui/base/metadata/metadata_impl_macros.h" #include "ui/base/models/image_model.h" #include "ui/base/ui_base_types.h" #include "ui/color/color_id.h" #include "ui/color/color_provider.h" #include "ui/events/event.h" #include "ui/gfx/canvas.h" #include "ui/gfx/color_palette.h" #include "ui/gfx/scoped_canvas.h" #include "ui/gfx/text_utils.h" #include "ui/views/animation/flood_fill_ink_drop_ripple.h" #include "ui/views/animation/ink_drop.h" #include "ui/views/animation/ink_drop_impl.h" #include "ui/views/background.h" #include "ui/views/controls/button/button.h" #include "ui/views/controls/button/button_controller.h" #include "ui/views/controls/combobox/combobox_menu_model.h" #include "ui/views/controls/combobox/combobox_util.h" #include "ui/views/controls/combobox/empty_combobox_model.h" #include "ui/views/controls/focus_ring.h" #include "ui/views/controls/focusable_border.h" #include "ui/views/controls/menu/menu_config.h" #include "ui/views/controls/menu/menu_runner.h" #include "ui/views/controls/prefix_selector.h" #include "ui/views/layout/layout_provider.h" #include "ui/views/mouse_constants.h" #include "ui/views/style/platform_style.h" #include "ui/views/style/typography.h" #include "ui/views/view_utils.h" #include "ui/views/widget/widget.h" namespace views { namespace { SkColor GetTextColorForEnableState(const Combobox& combobox, bool enabled) { const int style = enabled ? style::STYLE_PRIMARY : style::STYLE_DISABLED; return style::GetColor(combobox, style::CONTEXT_TEXTFIELD, style); } // The transparent button which holds a button state but is not rendered. class TransparentButton : public Button { public: explicit TransparentButton(PressedCallback callback) : Button(std::move(callback)) { SetFocusBehavior(FocusBehavior::NEVER); button_controller()->set_notify_action( ButtonController::NotifyAction::kOnPress); InkDrop::Get(this)->SetMode(views::InkDropHost::InkDropMode::ON); SetHasInkDropActionOnClick(true); InkDrop::UseInkDropForSquareRipple(InkDrop::Get(this), /*highlight_on_hover=*/false); views::InkDrop::Get(this)->SetBaseColorCallback(base::BindRepeating( [](Button* host) { // This button will be used like a LabelButton, so use the same // foreground base color as a label button. return color_utils::DeriveDefaultIconColor( views::style::GetColor(*host, views::style::CONTEXT_BUTTON, views::style::STYLE_PRIMARY)); }, this)); InkDrop::Get(this)->SetCreateRippleCallback(base::BindRepeating( [](Button* host) -> std::unique_ptr { return std::make_unique( host->size(), InkDrop::Get(host)->GetInkDropCenterBasedOnLastEvent(), host->GetColorProvider()->GetColor(ui::kColorLabelForeground), InkDrop::Get(host)->GetVisibleOpacity()); }, this)); } TransparentButton(const TransparentButton&) = delete; TransparentButton& operator&=(const TransparentButton&) = delete; ~TransparentButton() override = default; bool OnMousePressed(const ui::MouseEvent& mouse_event) override { #if !BUILDFLAG(IS_MAC) // On Mac, comboboxes do not take focus on mouse click, but on other // platforms they do. parent()->RequestFocus(); #endif return Button::OnMousePressed(mouse_event); } double GetAnimationValue() const { return hover_animation().GetCurrentValue(); } void UpdateInkDrop(bool show_on_press_and_hover) { if (show_on_press_and_hover) { // We must use UseInkDropForFloodFillRipple here because // UseInkDropForSquareRipple hides the InkDrop when the ripple effect is // active instead of layering underneath it causing flashing. InkDrop::UseInkDropForFloodFillRipple(InkDrop::Get(this), /*highlight_on_hover=*/true); } else { InkDrop::UseInkDropForSquareRipple(InkDrop::Get(this), /*highlight_on_hover=*/false); } } }; } // namespace //////////////////////////////////////////////////////////////////////////////// // Combobox, public: Combobox::Combobox(int text_context, int text_style) : Combobox(std::make_unique()) {} Combobox::Combobox(std::unique_ptr model, int text_context, int text_style) : Combobox(model.get(), text_context, text_style) { owned_model_ = std::move(model); } Combobox::Combobox(ui::ComboboxModel* model, int text_context, int text_style) : text_context_(text_context), text_style_(text_style), arrow_button_(new TransparentButton( base::BindRepeating(&Combobox::ArrowButtonPressed, base::Unretained(this)))) { SetModel(model); #if BUILDFLAG(IS_MAC) SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); #else SetFocusBehavior(FocusBehavior::ALWAYS); #endif SetBackgroundColorId(ui::kColorTextfieldBackground); UpdateBorder(); arrow_button_->SetVisible(should_show_arrow_); AddChildView(arrow_button_.get()); // A layer is applied to make sure that canvas bounds are snapped to pixel // boundaries (for the sake of drawing the arrow). SetPaintToLayer(); layer()->SetFillsBoundsOpaquely(false); FocusRing::Install(this); } Combobox::~Combobox() { if (GetInputMethod() && selector_.get()) { // Combobox should have been blurred before destroy. DCHECK(selector_.get() != GetInputMethod()->GetTextInputClient()); } } const gfx::FontList& Combobox::GetFontList() const { return style::GetFont(text_context_, text_style_); } void Combobox::SetSelectedIndex(absl::optional index) { if (selected_index_ == index) return; // TODO(pbos): Add (D)CHECKs to validate the selected index. selected_index_ = index; if (size_to_largest_label_) { OnPropertyChanged(&selected_index_, kPropertyEffectsPaint); } else { content_size_ = GetContentSize(); OnPropertyChanged(&selected_index_, kPropertyEffectsPreferredSizeChanged); } } base::CallbackListSubscription Combobox::AddSelectedIndexChangedCallback( views::PropertyChangedCallback callback) { return AddPropertyChangedCallback(&selected_index_, std::move(callback)); } bool Combobox::SelectValue(const std::u16string& value) { for (size_t i = 0; i < GetModel()->GetItemCount(); ++i) { if (value == GetModel()->GetItemAt(i)) { SetSelectedIndex(i); return true; } } return false; } void Combobox::SetOwnedModel(std::unique_ptr model) { // The swap keeps the outgoing model alive for SetModel(). owned_model_.swap(model); SetModel(owned_model_.get()); } void Combobox::SetModel(ui::ComboboxModel* model) { if (!model) { SetOwnedModel(std::make_unique()); return; } if (model_) { DCHECK(observation_.IsObservingSource(model_.get())); observation_.Reset(); } model_ = model; if (model_) { model_ = model; menu_model_ = std::make_unique(this, model_); observation_.Observe(model_.get()); SetSelectedIndex(model_->GetDefaultIndex()); OnComboboxModelChanged(model_); } } std::u16string Combobox::GetTooltipTextAndAccessibleName() const { return arrow_button_->GetTooltipText(); } void Combobox::SetTooltipTextAndAccessibleName( const std::u16string& tooltip_text) { arrow_button_->SetTooltipText(tooltip_text); if (accessible_name_.empty()) accessible_name_ = tooltip_text; } void Combobox::SetAccessibleName(const std::u16string& name) { accessible_name_ = name; } std::u16string Combobox::GetAccessibleName() const { return accessible_name_; } void Combobox::SetInvalid(bool invalid) { if (invalid == invalid_) return; invalid_ = invalid; if (views::FocusRing::Get(this)) views::FocusRing::Get(this)->SetInvalid(invalid); UpdateBorder(); OnPropertyChanged(&selected_index_, kPropertyEffectsPaint); } void Combobox::SetBorderColorId(ui::ColorId color_id) { border_color_id_ = color_id; UpdateBorder(); } void Combobox::SetBackgroundColorId(ui::ColorId color_id) { SetBackground(CreateThemedRoundedRectBackground( color_id, FocusableBorder::kCornerRadiusDp)); } void Combobox::SetEventHighlighting(bool should_highlight) { should_highlight_ = should_highlight; AsViewClass(arrow_button_) ->UpdateInkDrop(should_highlight); } void Combobox::SetSizeToLargestLabel(bool size_to_largest_label) { if (size_to_largest_label_ == size_to_largest_label) return; size_to_largest_label_ = size_to_largest_label; content_size_ = GetContentSize(); OnPropertyChanged(&selected_index_, kPropertyEffectsPreferredSizeChanged); } bool Combobox::IsMenuRunning() const { return menu_runner_ && menu_runner_->IsRunning(); } void Combobox::OnThemeChanged() { View::OnThemeChanged(); OnContentSizeMaybeChanged(); } size_t Combobox::GetRowCount() { return GetModel()->GetItemCount(); } absl::optional Combobox::GetSelectedRow() { return selected_index_; } void Combobox::SetSelectedRow(absl::optional row) { absl::optional prev_index = selected_index_; SetSelectedIndex(row); if (selected_index_ != prev_index) OnPerformAction(); } std::u16string Combobox::GetTextForRow(size_t row) { return GetModel()->IsItemSeparatorAt(row) ? std::u16string() : GetModel()->GetItemAt(row); } //////////////////////////////////////////////////////////////////////////////// // Combobox, View overrides: gfx::Size Combobox::CalculatePreferredSize() const { // Limit how small a combobox can be. constexpr int kMinComboboxWidth = 25; // The preferred size will drive the local bounds which in turn is used to set // the minimum width for the dropdown list. int width = std::max(kMinComboboxWidth, content_size_.width()) + LayoutProvider::Get()->GetDistanceMetric( DISTANCE_TEXTFIELD_HORIZONTAL_TEXT_PADDING) * 2 + GetInsets().width(); // If an arrow is being shown, add extra width to include that arrow. if (should_show_arrow_) { width += kComboboxArrowContainerWidth; } const int height = LayoutProvider::GetControlHeightForFont( text_context_, text_style_, GetFontList()); return gfx::Size(width, height); } void Combobox::OnBoundsChanged(const gfx::Rect& previous_bounds) { arrow_button_->SetBounds(0, 0, width(), height()); } bool Combobox::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { // Escape should close the drop down list when it is active, not host UI. if (e.key_code() != ui::VKEY_ESCAPE || e.IsShiftDown() || e.IsControlDown() || e.IsAltDown() || e.IsAltGrDown()) { return false; } return !!menu_runner_; } bool Combobox::OnKeyPressed(const ui::KeyEvent& e) { // TODO(oshima): handle IME. DCHECK_EQ(e.type(), ui::ET_KEY_PRESSED); DCHECK(selected_index_.has_value()); DCHECK_LT(selected_index_.value(), GetModel()->GetItemCount()); #if BUILDFLAG(IS_MAC) if (e.key_code() != ui::VKEY_DOWN && e.key_code() != ui::VKEY_UP && e.key_code() != ui::VKEY_SPACE && e.key_code() != ui::VKEY_HOME && e.key_code() != ui::VKEY_END) { return false; } ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD); return true; #else const auto index_at_or_after = [](ui::ComboboxModel* model, size_t index) -> absl::optional { for (; index < model->GetItemCount(); ++index) { if (!model->IsItemSeparatorAt(index) && model->IsItemEnabledAt(index)) return index; } return absl::nullopt; }; const auto index_before = [](ui::ComboboxModel* model, size_t index) -> absl::optional { for (; index > 0; --index) { const auto prev = index - 1; if (!model->IsItemSeparatorAt(prev) && model->IsItemEnabledAt(prev)) return prev; } return absl::nullopt; }; absl::optional new_index; switch (e.key_code()) { // Show the menu on F4 without modifiers. case ui::VKEY_F4: if (e.IsAltDown() || e.IsAltGrDown() || e.IsControlDown()) return false; ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD); return true; // Move to the next item if any, or show the menu on Alt+Down like Windows. case ui::VKEY_DOWN: if (e.IsAltDown()) { ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD); return true; } new_index = index_at_or_after(GetModel(), selected_index_.value() + 1); break; // Move to the end of the list. case ui::VKEY_END: case ui::VKEY_NEXT: // Page down. new_index = index_before(GetModel(), GetModel()->GetItemCount()); break; // Move to the beginning of the list. case ui::VKEY_HOME: case ui::VKEY_PRIOR: // Page up. new_index = index_at_or_after(GetModel(), 0); break; // Move to the previous item if any. case ui::VKEY_UP: new_index = index_before(GetModel(), selected_index_.value()); break; case ui::VKEY_RETURN: case ui::VKEY_SPACE: ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD); return true; default: return false; } if (new_index.has_value()) { if (menu_selection_at_callback_) menu_selection_at_callback_.Run(new_index.value()); SetSelectedIndex(new_index); OnPerformAction(); } return true; #endif // BUILDFLAG(IS_MAC) } void Combobox::OnPaint(gfx::Canvas* canvas) { OnPaintBackground(canvas); PaintIconAndText(canvas); OnPaintBorder(canvas); } void Combobox::OnFocus() { if (GetInputMethod()) GetInputMethod()->SetFocusedTextInputClient(GetPrefixSelector()); View::OnFocus(); // Border renders differently when focused. SchedulePaint(); } void Combobox::OnBlur() { if (GetInputMethod()) GetInputMethod()->DetachTextInputClient(GetPrefixSelector()); if (selector_) selector_->OnViewBlur(); // Border renders differently when focused. SchedulePaint(); } void Combobox::GetAccessibleNodeData(ui::AXNodeData* node_data) { // ax::mojom::Role::kComboBox is for UI elements with a dropdown and // an editable text field, which views::Combobox does not have. Use // ax::mojom::Role::kPopUpButton to match an HTML