File: editable_combobox.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (655 lines) | stat: -rw-r--r-- 21,986 bytes parent folder | download | duplicates (3)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// Copyright 2019 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/editable_combobox/editable_combobox.h"

#include <algorithm>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

#include "base/callback_list.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/i18n/rtl.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "third_party/skia/include/core/SkColor.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/accelerators/accelerator.h"
#include "ui/base/menu_source_utils.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/combobox_model.h"
#include "ui/base/models/combobox_model_observer.h"
#include "ui/base/models/menu_model.h"
#include "ui/base/models/menu_separator_types.h"
#include "ui/base/mojom/menu_source_type.mojom.h"
#include "ui/base/ui_base_features.h"
#include "ui/color/color_provider.h"
#include "ui/events/event.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/range/range.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/flood_fill_ink_drop_ripple.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/animation/ink_drop_host.h"
#include "ui/views/animation/ink_drop_impl.h"
#include "ui/views/animation/ink_drop_ripple.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/button_controller.h"
#include "ui/views/controls/combobox/combobox_util.h"
#include "ui/views/controls/combobox/empty_combobox_model.h"
#include "ui/views/controls/menu/menu_config.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/controls/menu/menu_types.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/layout_manager.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/style/typography.h"
#include "ui/views/style/typography_provider.h"
#include "ui/views/vector_icons.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace views {

namespace {

int kEditableComboboxButtonSize = 24;
int kEditableComboboxControlsContainerInsets = 6;

class Arrow : public Button {
  METADATA_HEADER(Arrow, Button)

 public:
  explicit Arrow(PressedCallback callback) : Button(std::move(callback)) {
    SetPreferredSize(
        gfx::Size(kEditableComboboxButtonSize, kEditableComboboxButtonSize));

    button_controller()->set_notify_action(
        ButtonController::NotifyAction::kOnPress);

    ConfigureComboboxButtonInkDrop(this);
    GetViewAccessibility().SetRole(ax::mojom::Role::kButton);
    UpdateAccessibleDefaultActionVerb();
    GetViewAccessibility().SetHasPopup(ax::mojom::HasPopup::kMenu);
  }
  Arrow(const Arrow&) = delete;
  Arrow& operator=(const Arrow&) = delete;
  ~Arrow() override = default;

  double GetAnimationValue() const {
    return hover_animation().GetCurrentValue();
  }

 private:
  void PaintButtonContents(gfx::Canvas* canvas) override {
    gfx::ScopedCanvas scoped_canvas(canvas);
    canvas->ClipRect(GetContentsBounds());
    gfx::Rect arrow_bounds = GetLocalBounds();
    arrow_bounds.ClampToCenteredSize(ComboboxArrowSize());
    // Make sure the arrow use the same color as the text in the combobox.
    PaintComboboxArrow(
        GetColorProvider()->GetColor(TypographyProvider::Get().GetColorId(
            style::CONTEXT_TEXTFIELD,
            GetEnabled() ? style::STYLE_PRIMARY : style::STYLE_DISABLED)),
        arrow_bounds, canvas);
  }

  void UpdateAccessibleDefaultActionVerb() {
    if (GetEnabled()) {
      GetViewAccessibility().SetDefaultActionVerb(
          ax::mojom::DefaultActionVerb::kOpen);
    } else {
      GetViewAccessibility().RemoveDefaultActionVerb();
    }
  }

  base::CallbackListSubscription enabled_changed_subscription_ =
      AddEnabledChangedCallback(
          base::BindRepeating(&Arrow::UpdateAccessibleDefaultActionVerb,
                              base::Unretained(this)));
};

BEGIN_METADATA(Arrow)
END_METADATA

}  // namespace

std::u16string EditableCombobox::MenuDecorationStrategy::DecorateItemText(
    std::u16string text) const {
  return text;
}

// Adapts a ui::ComboboxModel to a ui::MenuModel to be used by EditableCombobox.
// Also provides a filtering capability.
class EditableCombobox::EditableComboboxMenuModel final
    : public ui::MenuModel,
      public ui::ComboboxModelObserver {
 public:
  EditableComboboxMenuModel(EditableCombobox* owner,
                            std::unique_ptr<ui::ComboboxModel> combobox_model,
                            const bool filter_on_edit,
                            const bool show_on_empty)
      : decoration_strategy_(std::make_unique<MenuDecorationStrategy>()),
        owner_(owner),
        combobox_model_(std::move(combobox_model)),
        filter_on_edit_(filter_on_edit),
        show_on_empty_(show_on_empty) {
    UpdateItemsShown();
    observation_.Observe(combobox_model_.get());
  }

  EditableComboboxMenuModel(const EditableComboboxMenuModel&) = delete;
  EditableComboboxMenuModel& operator=(const EditableComboboxMenuModel&) =
      delete;

  ~EditableComboboxMenuModel() override = default;

  void UpdateItemsShown() {
    if (!update_items_shown_enabled_) {
      return;
    }
    items_shown_.clear();
    if (show_on_empty_ || !owner_->GetText().empty()) {
      for (size_t i = 0; i < combobox_model_->GetItemCount(); ++i) {
        if (!filter_on_edit_ ||
            base::StartsWith(combobox_model_->GetItemAt(i), owner_->GetText(),
                             base::CompareCase::INSENSITIVE_ASCII)) {
          items_shown_.push_back({i, combobox_model_->IsItemEnabledAt(i)});
        }
      }
    }
    if (menu_model_delegate()) {
      menu_model_delegate()->OnMenuStructureChanged();
    }
  }

  void SetDecorationStrategy(
      std::unique_ptr<EditableCombobox::MenuDecorationStrategy> strategy) {
    DCHECK(strategy);
    decoration_strategy_ = std::move(strategy);
    UpdateItemsShown();
  }

  void DisableUpdateItemsShown() { update_items_shown_enabled_ = false; }

  void EnableUpdateItemsShown() { update_items_shown_enabled_ = true; }

  bool UseCheckmarks() const {
    return MenuConfig::instance().check_selected_combobox_item;
  }

  std::u16string GetItemTextAt(size_t index) const {
    return combobox_model_->GetItemAt(items_shown_[index].index);
  }

  const ui::ComboboxModel* GetComboboxModel() const {
    return combobox_model_.get();
  }

  ui::ImageModel GetIconAt(size_t index) const override {
    return combobox_model_->GetDropDownIconAt(items_shown_[index].index);
  }

  // ComboboxModelObserver:
  void OnComboboxModelChanged(ui::ComboboxModel* model) override {
    UpdateItemsShown();
  }

  void OnComboboxModelDestroying(ui::ComboboxModel* model) override {
    observation_.Reset();
  }

  base::WeakPtr<ui::MenuModel> AsWeakPtr() override {
    return weak_ptr_factory_.GetWeakPtr();
  }

  size_t GetItemCount() const override { return items_shown_.size(); }

  // ui::MenuModel:
  std::u16string GetLabelAt(size_t index) const override {
    std::u16string text =
        decoration_strategy_->DecorateItemText(GetItemTextAt(index));
    base::i18n::AdjustStringForLocaleDirection(&text);
    return text;
  }

 private:
  struct ShownItem {
    size_t index;
    bool enabled;
  };

  ItemType GetTypeAt(size_t index) const override {
    return UseCheckmarks() ? TYPE_CHECK : TYPE_COMMAND;
  }

  ui::MenuSeparatorType GetSeparatorTypeAt(size_t index) const override {
    return ui::NORMAL_SEPARATOR;
  }

  int GetCommandIdAt(size_t index) const override {
    constexpr int kFirstMenuItemId = 1000;
    return static_cast<int>(index) + kFirstMenuItemId;
  }

  bool IsItemDynamicAt(size_t index) const override { return false; }

  const gfx::FontList* GetLabelFontListAt(size_t index) const override {
    return &owner_->GetFontList();
  }

  bool GetAcceleratorAt(size_t index,
                        ui::Accelerator* accelerator) const override {
    return false;
  }

  bool IsItemCheckedAt(size_t index) const override {
    return UseCheckmarks() &&
           combobox_model_->GetItemAt(items_shown_[index].index) ==
               owner_->GetText();
  }

  int GetGroupIdAt(size_t index) const override { return -1; }

  ui::ButtonMenuItemModel* GetButtonMenuItemAt(size_t index) const override {
    return nullptr;
  }

  bool IsEnabledAt(size_t index) const override {
    return items_shown_[index].enabled;
  }

  void ActivatedAt(size_t index) override { owner_->OnItemSelected(index); }

  MenuModel* GetSubmenuModelAt(size_t index) const override { return nullptr; }

  // The strategy used to customize the display of the dropdown menu.
  std::unique_ptr<MenuDecorationStrategy> decoration_strategy_;

  raw_ptr<EditableCombobox> owner_;  // Weak. Owns |this|.
  std::unique_ptr<ui::ComboboxModel> combobox_model_;

  // Whether to adapt the items shown to the textfield content.
  const bool filter_on_edit_;

  // Whether to show options when the textfield is empty.
  const bool show_on_empty_;

  // The indices of the items from |combobox_model_| that we are currently
  // showing, and whether they are enabled.
  std::vector<ShownItem> items_shown_;

  // When false, UpdateItemsShown doesn't do anything.
  bool update_items_shown_enabled_ = true;

  base::ScopedObservation<ui::ComboboxModel, ui::ComboboxModelObserver>
      observation_{this};

  base::WeakPtrFactory<EditableComboboxMenuModel> weak_ptr_factory_{this};
};

// This class adds itself as the pre-target handler for the RootView of the
// EditableCombobox. We use it to close the menu when press events happen in the
// RootView but not inside the EditableComboobox's textfield.
class EditableCombobox::EditableComboboxPreTargetHandler
    : public ui::EventHandler {
 public:
  EditableComboboxPreTargetHandler(EditableCombobox* owner, View* root_view)
      : owner_(owner), root_view_(root_view) {
    root_view_->AddPreTargetHandler(this);
  }

  EditableComboboxPreTargetHandler(const EditableComboboxPreTargetHandler&) =
      delete;
  EditableComboboxPreTargetHandler& operator=(
      const EditableComboboxPreTargetHandler&) = delete;

  ~EditableComboboxPreTargetHandler() override { StopObserving(); }

  // ui::EventHandler overrides.
  void OnMouseEvent(ui::MouseEvent* event) override {
    if (event->type() == ui::EventType::kMousePressed &&
        event->button_flags() == event->changed_button_flags()) {
      HandlePressEvent(event->root_location());
    }
  }

  void OnTouchEvent(ui::TouchEvent* event) override {
    if (event->type() == ui::EventType::kTouchPressed) {
      HandlePressEvent(event->root_location());
    }
  }

 private:
  void HandlePressEvent(const gfx::Point& root_location) {
    View* handler = root_view_->GetEventHandlerForPoint(root_location);
    if (handler == owner_->textfield_ || handler == owner_->arrow_) {
      return;
    }
    owner_->CloseMenu();
  }

  void StopObserving() {
    if (!root_view_) {
      return;
    }
    root_view_->RemovePreTargetHandler(this);
    root_view_ = nullptr;
  }

  raw_ptr<EditableCombobox> owner_;
  raw_ptr<View> root_view_;
};

EditableCombobox::EditableCombobox()
    : EditableCombobox(std::make_unique<internal::EmptyComboboxModel>()) {}

EditableCombobox::EditableCombobox(
    std::unique_ptr<ui::ComboboxModel> combobox_model,
    const bool filter_on_edit,
    const bool show_on_empty,
    const int text_context,
    const int text_style,
    const bool display_arrow)
    : textfield_(AddChildView(std::make_unique<Textfield>())),
      text_context_(text_context),
      text_style_(text_style),
      filter_on_edit_(filter_on_edit),
      show_on_empty_(show_on_empty) {
  SetModel(std::move(combobox_model));
  observation_.Observe(textfield_.get());
  textfield_->set_controller(this);
  textfield_->SetFontList(GetFontList());
  views::FocusRing::Get(textfield_)->SetOutsetFocusRingDisabled(true);

  control_elements_container_ = AddChildView(std::make_unique<BoxLayoutView>());
  control_elements_container_->SetInsideBorderInsets(
      gfx::Insets::TLBR(kEditableComboboxControlsContainerInsets, 0,
                        kEditableComboboxControlsContainerInsets,
                        kEditableComboboxControlsContainerInsets));
  control_elements_container_->SetBetweenChildSpacing(
      kComboboxArrowPaddingWidth);
  if (display_arrow) {
    arrow_ = AddControlElement(std::make_unique<Arrow>(base::BindRepeating(
        &EditableCombobox::ArrowButtonPressed, base::Unretained(this))));
    // We need this so the arrow icon is not covered when the combo box view is
    // hovered
    arrow_->SetPaintToLayer();
    arrow_->layer()->SetFillsBoundsOpaquely(false);
  }

  SetLayoutManager(std::make_unique<DelegatingLayoutManager>(this));
  GetViewAccessibility().SetRole(ax::mojom::Role::kComboBoxGrouping);
  GetViewAccessibility().SetValue(GetText());
}

EditableCombobox::~EditableCombobox() {
  CloseMenu();
  textfield_->set_controller(nullptr);
}

void EditableCombobox::SetModel(std::unique_ptr<ui::ComboboxModel> model) {
  CloseMenu();
  menu_model_ = std::make_unique<EditableComboboxMenuModel>(
      this, std::move(model), filter_on_edit_, show_on_empty_);
}

std::u16string_view EditableCombobox::GetText() const {
  return textfield_->GetText();
}

void EditableCombobox::SetText(std::u16string_view text) {
  textfield_->SetText(text);
  // SetText does not actually notify the TextfieldController, so we call the
  // handling code directly.
  HandleNewContent(text);
}

void EditableCombobox::SetInvalid(bool invalid) {
  textfield_->SetInvalid(invalid);
}

std::u16string_view EditableCombobox::GetPlaceholderText() const {
  return textfield_->GetPlaceholderText();
}

void EditableCombobox::SetPlaceholderText(std::u16string_view text) {
  textfield_->SetPlaceholderText(text);
}

const gfx::FontList& EditableCombobox::GetFontList() const {
  return TypographyProvider::Get().GetFont(text_context_, text_style_);
}

void EditableCombobox::SelectRange(const gfx::Range& range) {
  textfield_->SetSelectedRange(range);
}

void EditableCombobox::OnAccessibleNameChanged(const std::u16string& new_name) {
  textfield_->GetViewAccessibility().SetName(new_name);
  if (arrow_) {
    arrow_->GetViewAccessibility().SetName(new_name);
  }
}

void EditableCombobox::SetMenuDecorationStrategy(
    std::unique_ptr<MenuDecorationStrategy> strategy) {
  DCHECK(menu_model_);
  menu_model_->SetDecorationStrategy(std::move(strategy));
}

void EditableCombobox::UpdateMenu() {
  menu_model_->UpdateItemsShown();
}

gfx::Size EditableCombobox::CalculatePreferredSize(
    const SizeBounds& available_size) const {
  gfx::Size preferred_size = textfield_->GetPreferredSize({});
  preferred_size.SetToMax(control_elements_container_->GetPreferredSize({}));
  return preferred_size;
}

void EditableCombobox::RequestFocus() {
  textfield_->RequestFocus();
}

bool EditableCombobox::GetNeedsNotificationWhenVisibleBoundsChange() const {
  return true;
}

void EditableCombobox::OnVisibleBoundsChanged() {
  CloseMenu();
}

void EditableCombobox::ContentsChanged(Textfield* sender,
                                       const std::u16string& new_contents) {
  HandleNewContent(new_contents);
  ShowDropDownMenu(ui::mojom::MenuSourceType::kKeyboard);
}

bool EditableCombobox::HandleKeyEvent(Textfield* sender,
                                      const ui::KeyEvent& key_event) {
  if (key_event.type() == ui::EventType::kKeyPressed &&
      (key_event.key_code() == ui::VKEY_UP ||
       key_event.key_code() == ui::VKEY_DOWN)) {
    ShowDropDownMenu(ui::mojom::MenuSourceType::kKeyboard);
    return true;
  }
  return false;
}

void EditableCombobox::OnViewBlurred(View* observed_view) {
  CloseMenu();
}

void EditableCombobox::OnLayoutIsAnimatingChanged(
    views::AnimatingLayoutManager* source,
    bool is_animating) {
  dropdown_blocked_for_animation_ = is_animating;
  if (dropdown_blocked_for_animation_) {
    CloseMenu();
  }
}

// TODO(crbug.com/329471666): Refactor Textfield to obviate the need for this.
ProposedLayout EditableCombobox::CalculateProposedLayout(
    const SizeBounds& size_bounds) const {
  ProposedLayout layout;
  layout.host_size =
      gfx::Size(size_bounds.width().value(), size_bounds.height().value());
  layout.child_layouts.emplace_back(
      textfield_.get(), textfield_->GetVisible(),
      gfx::Rect(0, 0, layout.host_size.width(), layout.host_size.height()));
  const int preferred_width =
      control_elements_container_->GetPreferredSize({}).width();
  layout.child_layouts.emplace_back(
      control_elements_container_.get(),
      control_elements_container_->GetVisible(),
      gfx::Rect(layout.host_size.width() - preferred_width, 0, preferred_width,
                layout.host_size.height()));
  return layout;
}

bool EditableCombobox::ShouldApplyInkDropEffects() {
  return arrow_ && InkDrop::Get(arrow_) && GetWidget();
}

void EditableCombobox::CloseMenu() {
  if (ShouldApplyInkDropEffects()) {
    InkDrop::Get(arrow_)->AnimateToState(InkDropState::DEACTIVATED, nullptr);
    InkDrop::Get(arrow_)->GetInkDrop()->SetHovered(arrow_->IsMouseHovered());
  }
  menu_runner_.reset();
  pre_target_handler_.reset();
}

void EditableCombobox::OnItemSelected(size_t index) {
  std::u16string selected_item_text = menu_model_->GetItemTextAt(index);
  textfield_->SetText(selected_item_text);
  // SetText does not actually notify the TextfieldController, so we call the
  // handling code directly.
  HandleNewContent(selected_item_text);
}

void EditableCombobox::HandleNewContent(std::u16string_view new_content) {
  DCHECK_EQ(GetText(), new_content);
  // We notify |callback_| before updating |menu_model_|'s items shown. This
  // gives the user a chance to modify the ComboboxModel beforehand if they wish
  // to do so.
  // We disable UpdateItemsShown while we notify the listener in case it
  // modifies the ComboboxModel, then calls OnComboboxModelChanged and thus
  // UpdateItemsShown. That way UpdateItemsShown doesn't do its work twice.
  if (!content_changed_callback_.is_null()) {
    menu_model_->DisableUpdateItemsShown();
    content_changed_callback_.Run();
    menu_model_->EnableUpdateItemsShown();
  }
  UpdateMenu();
  GetViewAccessibility().SetValue(GetText());
}

void EditableCombobox::ArrowButtonPressed(const ui::Event& event) {
  textfield_->RequestFocus();
  if (ShouldApplyInkDropEffects()) {
    InkDrop::Get(arrow_)->AnimateToState(InkDropState::ACTIVATED, nullptr);
  }
  if (menu_runner_ && menu_runner_->IsRunning()) {
    CloseMenu();
  } else {
    ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
  }
}

void EditableCombobox::ShowDropDownMenu(ui::mojom::MenuSourceType source_type) {
  constexpr int kMenuBorderWidthTop = 1;

  if (dropdown_blocked_for_animation_) {
    return;
  }

  if (!menu_model_->GetItemCount()) {
    CloseMenu();
    return;
  }
  if (menu_runner_ && menu_runner_->IsRunning()) {
    return;
  }
  if (!GetWidget()) {
    return;
  }

  // Since we don't capture the mouse, we want to see the events that happen in
  // the EditableCombobox's RootView to get a chance to close the menu if they
  // happen outside |textfield_|. Events that happen over the menu belong to
  // another Widget and they don't go through this pre-target handler.
  // Events that happen outside both the menu and the RootView cause
  // OnViewBlurred to be called, which also closes the menu.
  pre_target_handler_ = std::make_unique<EditableComboboxPreTargetHandler>(
      this, GetWidget()->GetRootView());

  gfx::Rect local_bounds = textfield_->GetLocalBounds();

  // Menu's requested position's width should be the same as local bounds so the
  // border of the menu lines up with the border of the combobox. The y
  // coordinate however should be shifted to the bottom with the border width
  // not to overlap with the combobox border.
  gfx::Point menu_position(local_bounds.origin());
  menu_position.set_y(menu_position.y() + kMenuBorderWidthTop);
  View::ConvertPointToScreen(this, &menu_position);
  gfx::Rect bounds(menu_position, local_bounds.size());

  menu_runner_ = std::make_unique<MenuRunner>(
      menu_model_.get(), MenuRunner::EDITABLE_COMBOBOX,
      base::BindRepeating(&EditableCombobox::CloseMenu,
                          base::Unretained(this)));
  menu_runner_->RunMenuAt(GetWidget(), nullptr, bounds,
                          MenuAnchorPosition::kTopLeft, source_type);
}

void EditableCombobox::UpdateTextfieldInsets() {
  textfield_->SetExtraInsets(gfx::Insets::TLBR(
      0, 0, 0,
      std::max(control_elements_container_->GetPreferredSize({}).width() -
                   kComboboxArrowPaddingWidth,
               0)));
}

const ui::MenuModel* EditableCombobox::GetMenuModelForTesting() const {
  return menu_model_.get();
}

std::u16string EditableCombobox::GetItemTextForTesting(size_t index) const {
  return menu_model_->GetLabelAt(index);
}

const ui::ComboboxModel* EditableCombobox::GetComboboxModel() const {
  DCHECK(menu_model_);
  return menu_model_->GetComboboxModel();
}

BEGIN_METADATA(EditableCombobox)
ADD_PROPERTY_METADATA(std::u16string_view, Text)
ADD_PROPERTY_METADATA(std::u16string_view, PlaceholderText)
END_METADATA

}  // namespace views