summaryrefslogtreecommitdiff
path: root/chromium/ui/base/models/dialog_model_field.h
blob: 35a622dad5e575f16b024bde7eb8bc6fba2de40d (plain)
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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 "base/callback.h"
#include "base/containers/flat_set.h"
#include "base/strings/string16.h"
#include "base/util/type_safety/pass_key.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/models/combobox_model.h"

namespace ui {

class DialogModel;
class DialogModelButton;
class DialogModelBodyText;
class DialogModelCheckbox;
class DialogModelCombobox;
class DialogModelHost;
class DialogModelTextfield;
class Event;

// 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 DialogModelBodyText and DialogModelCheckbox for instance and has support
// for showing a link.
class COMPONENT_EXPORT(UI_BASE) DialogModelLabel {
 public:
  struct COMPONENT_EXPORT(UI_BASE) Link {
    // 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)>;

    Link(int message_id, Callback callback);
    Link(int message_id, base::RepeatingClosure closure);
    Link(const Link&);
    ~Link();

    const int message_id;
    const Callback callback;
  };

  explicit DialogModelLabel(int message_id);
  DialogModelLabel(const DialogModelLabel&);
  DialogModelLabel& operator=(const DialogModelLabel&) = delete;
  ~DialogModelLabel();

  static DialogModelLabel CreateWithLink(int message_id, Link link);

  static DialogModelLabel CreateWithLinks(int message_id,
                                          std::vector<Link> links);

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

  int message_id(util::PassKey<DialogModelHost>) const { return message_id_; }
  const std::vector<Link> links(util::PassKey<DialogModelHost>) const {
    return links_;
  }
  bool is_secondary(util::PassKey<DialogModelHost>) const {
    return is_secondary_;
  }

 private:
  explicit DialogModelLabel(int message_id, std::vector<Link> links);

  const int message_id_;
  const std::vector<Link> links_;
  bool is_secondary_ = 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 unique id in
// construction parameters (::Params::SetUniqueId()). 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 { kButton, kBodyText, kCheckbox, kCombobox, kTextfield };

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

  // Methods with util::PassKey<DialogModelHost> are only intended to be called
  // by the DialogModelHost implementation.
  Type type(util::PassKey<DialogModelHost>) const { return type_; }
  const base::flat_set<Accelerator>& accelerators(
      util::PassKey<DialogModelHost>) const {
    return accelerators_;
  }
  DialogModelButton* AsButton(util::PassKey<DialogModelHost>);
  DialogModelBodyText* AsBodyText(util::PassKey<DialogModelHost>);
  DialogModelCheckbox* AsCheckbox(util::PassKey<DialogModelHost>);
  DialogModelCombobox* AsCombobox(util::PassKey<DialogModelHost>);
  DialogModelTextfield* AsTextfield(util::PassKey<DialogModelHost>);

 protected:
  // Children of this class need to be constructed through DialogModel to help
  // enforce that they're added to the model.
  DialogModelField(util::PassKey<DialogModel>,
                   DialogModel* model,
                   Type type,
                   int unique_id,
                   base::flat_set<Accelerator> accelerators);

  DialogModelButton* AsButton();
  DialogModelBodyText* AsBodyText();
  DialogModelCheckbox* AsCheckbox();
  DialogModelCombobox* AsCombobox();
  DialogModelTextfield* AsTextfield();

 private:
  friend class DialogModel;

  DialogModel* const model_;
  const Type type_;
  const int unique_id_;

  const base::flat_set<Accelerator> accelerators_;
};

// Field class representing a dialog button.
class COMPONENT_EXPORT(UI_BASE) DialogModelButton : public DialogModelField {
 public:
  class COMPONENT_EXPORT(UI_BASE) Params {
   public:
    Params();
    Params(const Params&) = delete;
    Params& operator=(const Params&) = delete;
    ~Params();

    Params& SetUniqueId(int unique_id);

    Params& AddAccelerator(Accelerator accelerator);
    Params& SetAccessibleName(base::string16 accessible_name);

   private:
    friend class DialogModelButton;

    int unique_id_ = -1;
    base::flat_set<Accelerator> accelerators_;
  };

  // Note that this is constructed through a DialogModel which adds it to model
  // fields.
  DialogModelButton(util::PassKey<DialogModel> pass_key,
                    DialogModel* model,
                    base::RepeatingCallback<void(const Event&)> callback,
                    base::string16 label,
                    const Params& params);
  DialogModelButton(const DialogModelButton&) = delete;
  DialogModelButton& operator=(const DialogModelButton&) = delete;
  ~DialogModelButton() override;

  // Methods with util::PassKey<DialogModelHost> are only intended to be called
  // by the DialogModelHost implementation.
  const base::string16& label(util::PassKey<DialogModelHost>) const {
    return label_;
  }
  void OnPressed(util::PassKey<DialogModelHost>, const Event& event);

 private:
  friend class DialogModel;

  const base::string16 label_;
  // The button callback gets called when the button is activated. Whether
  // that happens on key-press, release, etc. is implementation (and platform)
  // dependent.
  base::RepeatingCallback<void(const Event&)> callback_;
};

// Field class representing body text.
class COMPONENT_EXPORT(UI_BASE) DialogModelBodyText : public DialogModelField {
 public:
  // Note that this is constructed through a DialogModel which adds it to model
  // fields.
  DialogModelBodyText(util::PassKey<DialogModel> pass_key,
                      DialogModel* model,
                      const DialogModelLabel& label);
  DialogModelBodyText(const DialogModelBodyText&) = delete;
  DialogModelBodyText& operator=(const DialogModelBodyText&) = delete;
  ~DialogModelBodyText() override;

  const DialogModelLabel& label(util::PassKey<DialogModelHost>) const {
    return label_;
  }

 private:
  const DialogModelLabel label_;
};

// Field class representing a checkbox with descriptive text.
class COMPONENT_EXPORT(UI_BASE) DialogModelCheckbox : public DialogModelField {
 public:
  // Note that this is constructed through a DialogModel which adds it to model
  // fields.
  DialogModelCheckbox(util::PassKey<DialogModel> pass_key,
                      DialogModel* model,
                      int unique_id,
                      const DialogModelLabel& label);
  DialogModelCheckbox(const DialogModelCheckbox&) = delete;
  DialogModelCheckbox& operator=(const DialogModelCheckbox&) = delete;
  ~DialogModelCheckbox() override;

  bool is_checked() const { return is_checked_; }

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

 private:
  const DialogModelLabel label_;
  bool is_checked_ = false;
};

// 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:
    Params();
    Params(const Params&) = delete;
    Params& operator=(const Params&) = delete;
    ~Params();

    Params& SetUniqueId(int unique_id);

    Params& AddAccelerator(Accelerator accelerator);
    Params& SetAccessibleName(base::string16 accessible_name);

    // 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);

   private:
    friend class DialogModelCombobox;

    int unique_id_ = -1;
    base::string16 accessible_name_;
    base::RepeatingClosure callback_;
    base::flat_set<Accelerator> accelerators_;
  };

  // Note that this is constructed through a DialogModel which adds it to model
  // fields.
  DialogModelCombobox(util::PassKey<DialogModel> pass_key,
                      DialogModel* model,
                      base::string16 label,
                      std::unique_ptr<ui::ComboboxModel> combobox_model,
                      const Params& params);
  DialogModelCombobox(const DialogModelCombobox&) = delete;
  DialogModelCombobox& operator=(const DialogModelCombobox&) = delete;
  ~DialogModelCombobox() override;

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

  // Methods with util::PassKey<DialogModelHost> are only intended to be called
  // by the DialogModelHost implementation.
  const base::string16& label(util::PassKey<DialogModelHost>) const {
    return label_;
  }
  const base::string16& accessible_name(util::PassKey<DialogModelHost>) const {
    return accessible_name_;
  }
  void OnSelectedIndexChanged(util::PassKey<DialogModelHost>,
                              int selected_index);
  void OnPerformAction(util::PassKey<DialogModelHost>);

 private:
  friend class DialogModel;

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

// 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:
    Params();
    Params(const Params&) = delete;
    Params& operator=(const Params&) = delete;
    ~Params();

    Params& SetUniqueId(int unique_id);

    Params& AddAccelerator(Accelerator accelerator);
    Params& SetAccessibleName(base::string16 accessible_name);

   private:
    friend class DialogModelTextfield;

    int unique_id_ = -1;
    base::string16 accessible_name_;
    base::flat_set<Accelerator> accelerators_;
  };

  // Note that this is constructed through a DialogModel which adds it to model
  // fields.
  DialogModelTextfield(util::PassKey<DialogModel> pass_key,
                       DialogModel* model,
                       base::string16 label,
                       base::string16 text,
                       const Params& params);
  DialogModelTextfield(const DialogModelTextfield&) = delete;
  DialogModelTextfield& operator=(const DialogModelTextfield&) = delete;
  ~DialogModelTextfield() override;

  const base::string16& text() const { return text_; }

  // Methods with util::PassKey<DialogModelHost> are only intended to be called
  // by the DialogModelHost implementation.
  const base::string16& label(util::PassKey<DialogModelHost>) const {
    return label_;
  }
  const base::string16& accessible_name(util::PassKey<DialogModelHost>) const {
    return accessible_name_;
  }
  void OnTextChanged(util::PassKey<DialogModelHost>, base::string16 text);

 private:
  friend class DialogModel;

  const base::string16 label_;
  const base::string16 accessible_name_;
  base::string16 text_;
};

}  // namespace ui

#endif  // UI_BASE_MODELS_DIALOG_MODEL_FIELD_H_