File: dialog_model_field.h

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 (756 lines) | stat: -rw-r--r-- 26,671 bytes parent folder | download | duplicates (5)
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_BASE_MODELS_DIALOG_MODEL_FIELD_H_
#define UI_BASE_MODELS_DIALOG_MODEL_FIELD_H_

#include <optional>
#include <string>
#include <string_view>

#include "base/callback_list.h"
#include "base/component_export.h"
#include "base/containers/flat_set.h"
#include "base/functional/callback.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/types/pass_key.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/models/combobox_model.h"
#include "ui/base/models/image_model.h"
#include "ui/base/ui_base_types.h"

namespace ui {

class DialogModelParagraph;
class DialogModelCheckbox;
class DialogModelCombobox;
class DialogModelCustomField;
class DialogModelMenuItem;
class DialogModelTitleItem;
class DialogModelSection;
class DialogModelTextfield;
class DialogModelPasswordField;
class Event;

class DialogModelFieldHost {
 protected:
  // This PassKey is used to make sure that some methods on DialogModel
  // are only called as part of the host integration.
  static base::PassKey<DialogModelFieldHost> GetPassKey() {
    return base::PassKey<DialogModelFieldHost>();
  }
};

// TODO(pbos): Move this to separate header.
// DialogModelLabel is an exception to below classes. This is not a
// DialogModelField but rather represents a text label and styling. This is used
// with DialogModelParagraph and DialogModelCheckbox for instance and has
// support for styling text replacements and showing a link.
class COMPONENT_EXPORT(UI_BASE) DialogModelLabel {
 public:
  // TODO(pbos): Move this definition (maybe as a ui::LinkCallback) so it can
  // be reused with views::Link.
  using Callback = base::RepeatingCallback<void(const Event& event)>;

  class COMPONENT_EXPORT(UI_BASE) TextReplacement {
   public:
    TextReplacement(const TextReplacement&);
    ~TextReplacement();

    const std::u16string& text() const { return text_; }
    bool is_emphasized() const { return is_emphasized_; }
    const std::optional<Callback>& callback() const { return callback_; }
    const std::optional<std::u16string>& accessible_name() const {
      return accessible_name_;
    }

   private:
    friend class DialogModelLabel;

    // Used for regular and emphasized text.
    explicit TextReplacement(std::u16string text, bool is_emphasized = false);
    // Used for links.
    TextReplacement(int message_id,
                    Callback closure,
                    std::u16string accessible_name = std::u16string());

    const std::u16string text_;
    const bool is_emphasized_;
    const std::optional<Callback> callback_;
    const std::optional<std::u16string> accessible_name_;
  };

  explicit DialogModelLabel(int message_id);
  explicit DialogModelLabel(std::u16string fixed_string);

  DialogModelLabel(const DialogModelLabel&);
  DialogModelLabel& operator=(const DialogModelLabel&) = delete;
  ~DialogModelLabel();

  static DialogModelLabel CreateWithReplacement(int message_id,
                                                TextReplacement replacement);
  static DialogModelLabel CreateWithReplacements(
      int message_id,
      std::vector<TextReplacement> replacements);

  // Builder methods for TextReplacements.
  static TextReplacement CreateLink(
      int message_id,
      base::RepeatingClosure closure,
      std::u16string accessible_name = std::u16string());
  static TextReplacement CreateLink(
      int message_id,
      Callback callback,
      std::u16string accessible_name = std::u16string());
  static TextReplacement CreatePlainText(std::u16string text);
  static TextReplacement CreateEmphasizedText(std::u16string text);

  // Gets the string. Not for use with replacements, in which case the caller
  // must use replacements() and message_id() to construct the final label. This
  // is required to style the final label appropriately and support replacement
  // callbacks. The caller is responsible for checking replacements().empty()
  // before calling this.
  const std::u16string& GetString() const;

  DialogModelLabel& set_is_secondary() {
    is_secondary_ = true;
    return *this;
  }

  DialogModelLabel& set_allow_character_break() {
    allow_character_break_ = true;
    return *this;
  }

  int message_id() const { return message_id_; }
  const std::vector<TextReplacement>& replacements() const {
    return replacements_;
  }

  bool is_secondary() const { return is_secondary_; }
  bool allow_character_break() const { return allow_character_break_; }

 private:
  explicit DialogModelLabel(int message_id,
                            std::vector<TextReplacement> replacements);

  const int message_id_;
  const std::u16string string_;

  // Set of replacements that will be added to `message_id_`.
  const std::vector<TextReplacement> replacements_;

  bool is_secondary_ = false;
  bool allow_character_break_ = false;
};

// These "field" classes represent entries in a DialogModel. They are owned
// by the model and either created through the model or DialogModel::Builder.
// These entries can be referred to by setting the field's ElementIdentifier in
// construction parameters (::Params::SetId()). They can then
// later be acquired through DialogModel::GetFieldByUniqueId() methods.
// These fields own the data corresponding to their field. For instance, the
// text of a textfield in a model is read using DialogModelTextfield::text() and
// stays in sync with the visible dialog (through DialogModelHosts).
class COMPONENT_EXPORT(UI_BASE) DialogModelField {
 public:
  enum Type {
    kParagraph,
    kCheckbox,
    kCombobox,
    kCustom,
    kMenuItem,
    kSeparator,  // TODO(pbos): Remove kSeparator once it can be implied by
                 // having multiple subsequent kSections (3 sections imply 2
                 // separators).
    kSection,
    kTextfield,
    kPasswordField,
    kTitleItem  // TODO(pengchaocai): Remove kTitleItem once DialogModel
                // supports multiple sections.
  };

  class COMPONENT_EXPORT(UI_BASE) Params {
   public:
    Params() = default;
    Params(const Params&) = delete;
    Params& operator=(const Params&) = delete;
    ~Params() = default;

    Params& SetVisible(bool is_visible) {
      is_visible_ = is_visible;
      return *this;
    }

   private:
    friend class DialogModel;
    friend class DialogModelField;

    bool is_visible_ = true;
  };

  DialogModelField(const DialogModelField&) = delete;
  DialogModelField& operator=(const DialogModelField&) = delete;
  virtual ~DialogModelField();

  [[nodiscard]] base::CallbackListSubscription AddOnFieldChangedCallback(
      base::RepeatingClosure on_field_changed);

  Type type() const { return type_; }

  void SetVisible(bool visible);
  bool is_visible() const { return is_visible_; }

  const base::flat_set<Accelerator>& accelerators() const {
    return accelerators_;
  }
  ElementIdentifier id() const { return id_; }

  DialogModelParagraph* AsParagraph();
  DialogModelCheckbox* AsCheckbox();
  DialogModelCombobox* AsCombobox();
  DialogModelMenuItem* AsMenuItem();
  const DialogModelMenuItem* AsMenuItem() const;
  const DialogModelTitleItem* AsTitleItem() const;
  DialogModelTextfield* AsTextfield();
  DialogModelPasswordField* AsPasswordField();
  DialogModelSection* AsSection();
  DialogModelCustomField* AsCustomField();

 protected:
  DialogModelField(Type type,
                   ElementIdentifier id,
                   base::flat_set<Accelerator> accelerators,
                   const DialogModelField::Params& params);

  void NotifyOnFieldChanged();

 private:
  friend class DialogModel;
  FRIEND_TEST_ALL_PREFIXES(DialogModelButtonTest, UsesParamsUniqueId);

  const Type type_;
  const ElementIdentifier id_;

  const base::flat_set<Accelerator> accelerators_;

  bool is_visible_;

  base::RepeatingClosureList on_field_changed_;
};

// Field class representing a paragraph.
class COMPONENT_EXPORT(UI_BASE) DialogModelParagraph : public DialogModelField {
 public:
  DialogModelParagraph(const DialogModelLabel& label,
                       std::u16string header,
                       ElementIdentifier id);
  DialogModelParagraph(const DialogModelParagraph&) = delete;
  DialogModelParagraph& operator=(const DialogModelParagraph&) = delete;
  ~DialogModelParagraph() override;

  const DialogModelLabel& label() const { return label_; }

  const std::u16string header() const { return header_; }

 private:
  const DialogModelLabel label_;
  const std::u16string header_;
};

// Field class representing a checkbox with descriptive text.
class COMPONENT_EXPORT(UI_BASE) DialogModelCheckbox : public DialogModelField {
 public:
  class COMPONENT_EXPORT(UI_BASE) Params : public DialogModelField::Params {
   public:
    Params() = default;
    Params(const Params&) = delete;
    Params& operator=(const Params&) = delete;
    ~Params() = default;

    Params& SetIsChecked(bool is_checked) {
      is_checked_ = is_checked;
      return *this;
    }

    Params& SetVisible(bool is_visible) {
      DialogModelField::Params::SetVisible(is_visible);
      return *this;
    }

   private:
    friend class DialogModelCheckbox;

    bool is_checked_ = false;
  };

  DialogModelCheckbox(ElementIdentifier id,
                      const DialogModelLabel& label,
                      const Params& params);
  DialogModelCheckbox(const DialogModelCheckbox&) = delete;
  DialogModelCheckbox& operator=(const DialogModelCheckbox&) = delete;
  ~DialogModelCheckbox() override;

  bool is_checked() const { return is_checked_; }

  void OnChecked(base::PassKey<DialogModelFieldHost>, bool is_checked);
  const DialogModelLabel& label() const { return label_; }

 private:
  const DialogModelLabel label_;
  bool is_checked_;
};

// Field class representing a combobox and corresponding label to describe the
// combobox:
//
//     <label>   [combobox]
// Ex: Folder    [My Bookmarks]
class COMPONENT_EXPORT(UI_BASE) DialogModelCombobox : public DialogModelField {
 public:
  class COMPONENT_EXPORT(UI_BASE) Params : public DialogModelField::Params {
   public:
    Params();
    Params(const Params&) = delete;
    Params& operator=(const Params&) = delete;
    ~Params();

    Params& AddAccelerator(Accelerator accelerator);

    Params& SetAccessibleName(std::u16string accessible_name) {
      accessible_name_ = std::move(accessible_name);
      return *this;
    }

    // The combobox callback is invoked when an item has been selected. This
    // nominally happens when selecting an item in the combobox menu. The
    // selection notably does not change by hovering different items in the
    // combobox menu or navigating it with up/down keys as long as the menu is
    // open.
    Params& SetCallback(base::RepeatingClosure callback);

    Params& SetVisible(bool is_visible) {
      DialogModelField::Params::SetVisible(is_visible);
      return *this;
    }

   private:
    friend class DialogModelCombobox;

    ElementIdentifier id_;
    std::u16string accessible_name_;
    base::RepeatingClosure callback_;
    base::flat_set<Accelerator> accelerators_;
  };

  DialogModelCombobox(ElementIdentifier id,
                      std::u16string label,
                      std::unique_ptr<ui::ComboboxModel> combobox_model,
                      const Params& params);
  DialogModelCombobox(const DialogModelCombobox&) = delete;
  DialogModelCombobox& operator=(const DialogModelCombobox&) = delete;
  ~DialogModelCombobox() override;

  size_t selected_index() const { return selected_index_; }
  ui::ComboboxModel* combobox_model() { return combobox_model_.get(); }

  const std::u16string& label() const { return label_; }
  const std::u16string& accessible_name() const { return accessible_name_; }
  void OnSelectedIndexChanged(base::PassKey<DialogModelFieldHost>,
                              size_t selected_index);
  void OnPerformAction(base::PassKey<DialogModelFieldHost>);

 private:
  friend class DialogModel;

  const std::u16string label_;
  const std::u16string accessible_name_;
  size_t selected_index_;
  std::unique_ptr<ui::ComboboxModel> combobox_model_;
  base::RepeatingClosure callback_;
};

// Field class representing a menu item:
//
//     <icon> <label>
// Ex: [icon] Open URL
class COMPONENT_EXPORT(UI_BASE) DialogModelMenuItem : public DialogModelField {
 public:
  class COMPONENT_EXPORT(UI_BASE) Params : public DialogModelField::Params {
   public:
    Params();
    Params(const Params&) = delete;
    Params& operator=(const Params&) = delete;
    ~Params();

    Params& SetIsEnabled(bool is_enabled);
    Params& SetId(ElementIdentifier id);

    Params& SetVisible(bool is_visible) {
      DialogModelField::Params::SetVisible(is_visible);
      return *this;
    }

   private:
    friend class DialogModelMenuItem;

    bool is_enabled_ = true;
    ElementIdentifier id_;
  };

  DialogModelMenuItem(ImageModel icon,
                      std::u16string label,
                      base::RepeatingCallback<void(int)> callback,
                      const Params& params);
  DialogModelMenuItem(const DialogModelMenuItem&) = delete;
  DialogModelMenuItem& operator=(const DialogModelMenuItem&) = delete;
  ~DialogModelMenuItem() override;

  const ImageModel& icon() const { return icon_; }
  const std::u16string& label() const { return label_; }
  bool is_enabled() const { return is_enabled_; }
  void OnActivated(base::PassKey<DialogModelFieldHost>, int event_flags);

 private:
  const ImageModel icon_;
  const std::u16string label_;
  const bool is_enabled_;
  base::RepeatingCallback<void(int)> callback_;
};

// Field class representing a separator.
class COMPONENT_EXPORT(UI_BASE) DialogModelSeparator : public DialogModelField {
 public:
  DialogModelSeparator();
  DialogModelSeparator(const DialogModelSeparator&) = delete;
  DialogModelSeparator& operator=(const DialogModelSeparator&) = delete;
  ~DialogModelSeparator() override;
};

// Field class representing a title.
// TODO(pengchaocai): Remove DialogModelTitleItem once DialogModel supports
// multiple sections and titles live in sections as optional strings.
class COMPONENT_EXPORT(UI_BASE) DialogModelTitleItem : public DialogModelField {
 public:
  explicit DialogModelTitleItem(std::u16string label,
                                ElementIdentifier id = ElementIdentifier());
  DialogModelTitleItem(const DialogModelSeparator&) = delete;
  DialogModelTitleItem& operator=(const DialogModelSeparator&) = delete;
  ~DialogModelTitleItem() override;
  const std::u16string& label() const { return label_; }

 private:
  const std::u16string label_;
};

// Field class representing a textfield and corresponding label to describe the
// textfield:
//
//     <label>   [textfield]
// Ex: Name      [My email]
class COMPONENT_EXPORT(UI_BASE) DialogModelTextfield : public DialogModelField {
 public:
  class COMPONENT_EXPORT(UI_BASE) Params : public DialogModelField::Params {
   public:
    Params();
    Params(const Params&) = delete;
    Params& operator=(const Params&) = delete;
    ~Params();

    Params& AddAccelerator(Accelerator accelerator);

    Params& SetAccessibleName(std::u16string accessible_name) {
      accessible_name_ = std::move(accessible_name);
      return *this;
    }

    Params& SetVisible(bool is_visible) {
      DialogModelField::Params::SetVisible(is_visible);
      return *this;
    }

   private:
    friend class DialogModelTextfield;

    ElementIdentifier id_;
    std::u16string accessible_name_;
    base::flat_set<Accelerator> accelerators_;
  };

  DialogModelTextfield(ElementIdentifier id,
                       std::u16string label,
                       std::u16string text,
                       const Params& params);
  DialogModelTextfield(const DialogModelTextfield&) = delete;
  DialogModelTextfield& operator=(const DialogModelTextfield&) = delete;
  ~DialogModelTextfield() override;

  const std::u16string& text() const { return text_; }

  const std::u16string& label() const { return label_; }
  const std::u16string& accessible_name() const { return accessible_name_; }
  void OnTextChanged(base::PassKey<DialogModelFieldHost>,
                     std::u16string_view text);

 private:
  friend class DialogModel;

  const std::u16string label_;
  const std::u16string accessible_name_;
  std::u16string text_;
};

// Field class representing a password field and corresponding label to describe
// the password field. The password can be revealed by clicking on the eye icon.
// If the user enters an incorrect password, the field can be invalidated by
// calling `Invalidate()`:
// - the password field is cleared ;
// - the text field is invalidated, causing its outline to be red ;
// - `incorrect_password_text` is shown below the password field.
// The password field becomes valid again automatically when a new character is
// entered.
class COMPONENT_EXPORT(UI_BASE) DialogModelPasswordField
    : public DialogModelField {
 public:
  // using Params = DialogModelField::Params;

  DialogModelPasswordField(ElementIdentifier id,
                           std::u16string label,
                           std::u16string accessible_name,
                           std::u16string incorrect_password_text,
                           const DialogModelPasswordField::Params& params);
  DialogModelPasswordField(const DialogModelPasswordField&) = delete;
  DialogModelPasswordField& operator=(const DialogModelPasswordField&) = delete;
  ~DialogModelPasswordField() override;

  const std::u16string& text() const { return text_; }

  // Clears the password field, and displays `incorrect_password_text()` until
  // the user starts typing again.
  // Typically used when the user clicks the OK button after they are finished
  // typing, and the password is wrong.
  void Invalidate();

  const std::u16string& label() const { return label_; }
  const std::u16string& accessible_name() const { return accessible_name_; }
  const std::u16string& incorrect_password_text() const {
    return incorrect_password_text_;
  }

  void OnTextChanged(base::PassKey<DialogModelFieldHost>,
                     std::u16string_view text);
  base::CallbackListSubscription AddOnInvalidateCallback(
      base::PassKey<DialogModelFieldHost>,
      base::RepeatingClosure closure);

 private:
  friend class DialogModel;

  const std::u16string label_;
  const std::u16string accessible_name_;
  const std::u16string incorrect_password_text_;
  std::u16string text_;

  base::RepeatingClosureList on_invalidate_closures_;
};

// Field base class representing a "custom" field. Used for instance to inject
// custom Views into dialogs that use DialogModel.
class COMPONENT_EXPORT(UI_BASE) DialogModelCustomField
    : public DialogModelField {
 public:
  // Base class for fields held by DialogModelField. Calling code is responsible
  // for providing the subclass expected by the DialogModelHost used.
  class COMPONENT_EXPORT(UI_BASE) Field {
   public:
    virtual ~Field();
  };

  DialogModelCustomField(ElementIdentifier id,
                         std::unique_ptr<DialogModelCustomField::Field> field);
  DialogModelCustomField(const DialogModelCustomField&) = delete;
  DialogModelCustomField& operator=(const DialogModelCustomField&) = delete;
  ~DialogModelCustomField() override;

  DialogModelCustomField::Field* field() { return field_.get(); }

 private:
  friend class DialogModel;

  std::unique_ptr<DialogModelCustomField::Field> field_;
};

// Field class representing a section. A section is a list of fields which may
// include subsections too (tree structure).
class COMPONENT_EXPORT(UI_BASE) DialogModelSection final
    : public DialogModelField {
 public:
  class COMPONENT_EXPORT(UI_BASE) Builder final {
   public:
    Builder();
    Builder(const Builder&) = delete;
    Builder& operator=(const Builder&) = delete;
    ~Builder();

    [[nodiscard]] std::unique_ptr<DialogModelSection> Build();

    Builder& AddParagraph(const DialogModelLabel& label,
                          std::u16string header = std::u16string(),
                          ElementIdentifier id = ElementIdentifier()) {
      section_->AddParagraph(label, std::move(header), id);
      return *this;
    }

    Builder& AddCheckbox(ElementIdentifier id,
                         const DialogModelLabel& label,
                         const DialogModelCheckbox::Params& params =
                             DialogModelCheckbox::Params()) {
      section_->AddCheckbox(id, label, params);
      return *this;
    }

    Builder& AddCombobox(ElementIdentifier id,
                         std::u16string label,
                         std::unique_ptr<ui::ComboboxModel> combobox_model,
                         const DialogModelCombobox::Params& params =
                             DialogModelCombobox::Params()) {
      section_->AddCombobox(id, std::move(label), std::move(combobox_model),
                            params);
      return *this;
    }

    Builder& AddMenuItem(ImageModel icon,
                         std::u16string label,
                         base::RepeatingCallback<void(int)> callback,
                         const DialogModelMenuItem::Params& params =
                             DialogModelMenuItem::Params()) {
      section_->AddMenuItem(std::move(icon), std::move(label),
                            std::move(callback), params);
      return *this;
    }

    Builder& AddSeparator() {
      section_->AddSeparator();
      return *this;
    }

    Builder& AddTextfield(ElementIdentifier id,
                          std::u16string label,
                          std::u16string text,
                          const DialogModelTextfield::Params& params =
                              DialogModelTextfield::Params()) {
      section_->AddTextfield(id, std::move(label), std::move(text), params);
      return *this;
    }

    Builder& AddCustomField(
        std::unique_ptr<DialogModelCustomField::Field> field,
        ElementIdentifier id = ElementIdentifier()) {
      section_->AddCustomField(std::move(field), id);
      return *this;
    }

   private:
    std::unique_ptr<DialogModelSection> section_;
  };

  // TODO(pbos): Params may make sense here? An optional title should be here?
  // TODO(pbos): We may also want to add on_field_added as a callback to that
  // Params struct once it exists.
  DialogModelSection();
  DialogModelSection(const DialogModelSection&) = delete;
  DialogModelSection& operator=(const DialogModelSection&) = delete;
  ~DialogModelSection() override;

  [[nodiscard]] base::CallbackListSubscription AddOnFieldAddedCallback(
      base::RepeatingCallback<void(DialogModelField*)> on_field_added);

  [[nodiscard]] base::CallbackListSubscription AddOnFieldChangedCallback(
      base::RepeatingCallback<void(DialogModelField*)> on_field_changed);

  const std::vector<std::unique_ptr<DialogModelField>>& fields() const {
    return fields_;
  }

  DialogModelField* GetFieldByUniqueId(ElementIdentifier id);
  DialogModelCheckbox* GetCheckboxByUniqueId(ElementIdentifier id);
  DialogModelCombobox* GetComboboxByUniqueId(ElementIdentifier id);
  DialogModelTextfield* GetTextfieldByUniqueId(ElementIdentifier id);
  DialogModelPasswordField* GetPasswordFieldByUniqueId(ElementIdentifier id);

  // Adds a paragraph at the end of the section. A paragraph consists of a
  // label and an optional header.
  void AddParagraph(const DialogModelLabel& label,
                    std::u16string header = std::u16string(),
                    ElementIdentifier id = ElementIdentifier());

  // Adds a checkbox ([checkbox] label) at the end of the section.
  void AddCheckbox(ElementIdentifier id,
                   const DialogModelLabel& label,
                   const DialogModelCheckbox::Params& params =
                       DialogModelCheckbox::Params());

  // Adds a labeled combobox (label: [model]) at the end of the section.
  void AddCombobox(ElementIdentifier id,
                   std::u16string label,
                   std::unique_ptr<ui::ComboboxModel> combobox_model,
                   const DialogModelCombobox::Params& params =
                       DialogModelCombobox::Params());

  // Adds a menu item at the end of the section.
  void AddMenuItem(ImageModel icon,
                   std::u16string label,
                   base::RepeatingCallback<void(int)> callback,
                   const DialogModelMenuItem::Params& params =
                       DialogModelMenuItem::Params());

  // Adds a menu item at the end of the section.
  // TODO(pengchaocai): Refactor this method once dialog_model supports multiple
  // DialogModelSection, when the title would be an optional member of `this`
  // and explicitly adding it might not be needed.
  void AddTitleItem(std::u16string label,
                    ElementIdentifier id = ElementIdentifier());

  // Adds a separator at the end of the section.
  void AddSeparator();

  // Adds a labeled textfield (label: [text]) at the end of the section.
  void AddTextfield(ElementIdentifier id,
                    std::u16string label,
                    std::u16string text,
                    const DialogModelTextfield::Params& params =
                        DialogModelTextfield::Params());

  // Adds a labeled password field (label: [password field]) at the end of the
  // section.
  void AddPasswordField(ElementIdentifier id,
                        std::u16string label,
                        std::u16string accessible_text,
                        std::u16string incorrect_password_text,
                        const DialogModelPasswordField::Params& params =
                            DialogModelPasswordField::Params());

  // Adds a custom field at the end of the section. This is used to inject
  // framework-specific custom UI into dialogs that are otherwise constructed as
  // DialogModelBase derivatives.
  void AddCustomField(std::unique_ptr<DialogModelCustomField::Field> field,
                      ElementIdentifier id = ElementIdentifier());

 private:
  void AddField(std::unique_ptr<DialogModelField> field);
  void OnFieldChanged(DialogModelField* field);

  base::RepeatingCallbackList<void(DialogModelField*)> on_field_added_;
  base::RepeatingCallbackList<void(DialogModelField*)> on_field_changed_;

  std::vector<std::unique_ptr<DialogModelField>> fields_;

  std::vector<base::CallbackListSubscription> field_subscriptions_;
};

}  // namespace ui

#endif  // UI_BASE_MODELS_DIALOG_MODEL_FIELD_H_