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
|
// 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.
#include "ui/base/models/dialog_model.h"
#include <memory>
#include "base/callback_helpers.h"
#include "base/ranges/algorithm.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/models/dialog_model_field.h"
#include "ui/base/ui_base_types.h"
namespace ui {
DialogModel::Builder::Builder(std::unique_ptr<DialogModelDelegate> delegate)
: model_(std::make_unique<DialogModel>(base::PassKey<Builder>(),
std::move(delegate))) {}
DialogModel::Builder::Builder() : Builder(nullptr) {}
DialogModel::Builder::~Builder() {
DCHECK(!model_) << "Model should've been built.";
}
std::unique_ptr<DialogModel> DialogModel::Builder::Build() {
DCHECK(model_);
return std::move(model_);
}
DialogModel::Builder& DialogModel::Builder::AddOkButton(
base::OnceClosure callback,
std::u16string label,
const DialogModelButton::Params& params) {
DCHECK(!model_->accept_action_callback_);
model_->accept_action_callback_ = std::move(callback);
// NOTREACHED() is used below to make sure this callback isn't used.
// DialogModelHost should be using OnDialogAccepted() instead.
model_->ok_button_.emplace(
model_->GetPassKey(), model_.get(),
base::BindRepeating([](const Event&) { NOTREACHED(); }), std::move(label),
params);
return *this;
}
DialogModel::Builder& DialogModel::Builder::AddCancelButton(
base::OnceClosure callback,
std::u16string label,
const DialogModelButton::Params& params) {
DCHECK(!model_->cancel_action_callback_);
model_->cancel_action_callback_ = std::move(callback);
// NOTREACHED() is used below to make sure this callback isn't used.
// DialogModelHost should be using OnDialogCanceled() instead.
model_->cancel_button_.emplace(
model_->GetPassKey(), model_.get(),
base::BindRepeating([](const Event&) { NOTREACHED(); }), std::move(label),
params);
return *this;
}
DialogModel::Builder& DialogModel::Builder::AddExtraButton(
base::RepeatingCallback<void(const Event&)> callback,
std::u16string label,
const DialogModelButton::Params& params) {
DCHECK(!model_->extra_button_);
DCHECK(!model_->extra_link_);
model_->extra_button_.emplace(model_->GetPassKey(), model_.get(),
std::move(callback), std::move(label), params);
return *this;
}
DialogModel::Builder& DialogModel::Builder::AddExtraLink(
DialogModelLabel::TextReplacement link) {
DCHECK(!model_->extra_button_);
DCHECK(!model_->extra_link_);
model_->extra_link_.emplace(std::move(link));
return *this;
}
DialogModel::Builder& DialogModel::Builder::OverrideDefaultButton(
DialogButton button) {
// This can only be called once.
DCHECK(!model_->override_default_button_);
// Confirm the button exists.
switch (button) {
case DIALOG_BUTTON_NONE:
NOTREACHED();
break;
case DIALOG_BUTTON_OK:
DCHECK(model_->ok_button_);
break;
case DIALOG_BUTTON_CANCEL:
DCHECK(model_->cancel_button_);
break;
}
model_->override_default_button_ = button;
return *this;
}
DialogModel::Builder& DialogModel::Builder::SetInitiallyFocusedField(
ElementIdentifier id) {
// This must be called with a non-null id
DCHECK(id);
// This can only be called once.
DCHECK(!model_->initially_focused_field_);
model_->initially_focused_field_ = id;
return *this;
}
DialogModel::DialogModel(base::PassKey<Builder>,
std::unique_ptr<DialogModelDelegate> delegate)
: delegate_(std::move(delegate)) {
if (delegate_)
delegate_->set_dialog_model(this);
}
DialogModel::~DialogModel() = default;
void DialogModel::AddParagraph(const DialogModelLabel& label,
std::u16string header,
ElementIdentifier id) {
AddField(std::make_unique<DialogModelParagraph>(GetPassKey(), this, label,
header, id));
}
void DialogModel::AddCheckbox(ElementIdentifier id,
const DialogModelLabel& label,
const DialogModelCheckbox::Params& params) {
AddField(std::make_unique<DialogModelCheckbox>(GetPassKey(), this, id, label,
params));
}
void DialogModel::AddCombobox(ElementIdentifier id,
std::u16string label,
std::unique_ptr<ui::ComboboxModel> combobox_model,
const DialogModelCombobox::Params& params) {
AddField(std::make_unique<DialogModelCombobox>(
GetPassKey(), this, id, std::move(label), std::move(combobox_model),
params));
}
void DialogModel::AddSeparator() {
AddField(std::make_unique<DialogModelSeparator>(GetPassKey(), this));
}
void DialogModel::AddMenuItem(ImageModel icon,
std::u16string label,
base::RepeatingCallback<void(int)> callback,
const DialogModelMenuItem::Params& params) {
AddField(std::make_unique<DialogModelMenuItem>(
GetPassKey(), this, std::move(icon), std::move(label),
std::move(callback), params));
}
void DialogModel::AddTextfield(ElementIdentifier id,
std::u16string label,
std::u16string text,
const DialogModelTextfield::Params& params) {
AddField(std::make_unique<DialogModelTextfield>(
GetPassKey(), this, id, std::move(label), std::move(text), params));
}
void DialogModel::AddCustomField(
std::unique_ptr<DialogModelCustomField::Field> field,
ElementIdentifier id) {
AddField(std::make_unique<DialogModelCustomField>(GetPassKey(), this, id,
std::move(field)));
}
bool DialogModel::HasField(ElementIdentifier id) const {
return base::ranges::any_of(fields_,
[id](auto& field) { return field->id_ == id; });
}
DialogModelField* DialogModel::GetFieldByUniqueId(ElementIdentifier id) {
// Assert that there is one and only one.
DCHECK_EQ(1, static_cast<int>(base::ranges::count_if(
fields_, [id](auto& field) { return field->id_ == id; })));
for (auto& field : fields_) {
if (field->id_ == id)
return field.get();
}
return nullptr;
}
DialogModelCheckbox* DialogModel::GetCheckboxByUniqueId(ElementIdentifier id) {
return GetFieldByUniqueId(id)->AsCheckbox();
}
DialogModelCombobox* DialogModel::GetComboboxByUniqueId(ElementIdentifier id) {
return GetFieldByUniqueId(id)->AsCombobox();
}
DialogModelTextfield* DialogModel::GetTextfieldByUniqueId(
ElementIdentifier id) {
return GetFieldByUniqueId(id)->AsTextfield();
}
void DialogModel::OnDialogAcceptAction(base::PassKey<DialogModelHost>) {
if (accept_action_callback_)
std::move(accept_action_callback_).Run();
}
void DialogModel::OnDialogCancelAction(base::PassKey<DialogModelHost>) {
if (cancel_action_callback_)
std::move(cancel_action_callback_).Run();
}
void DialogModel::OnDialogCloseAction(base::PassKey<DialogModelHost>) {
if (close_action_callback_)
std::move(close_action_callback_).Run();
}
void DialogModel::OnDialogDestroying(base::PassKey<DialogModelHost>) {
if (dialog_destroying_callback_)
std::move(dialog_destroying_callback_).Run();
}
void DialogModel::AddField(std::unique_ptr<DialogModelField> field) {
fields_.push_back(std::move(field));
if (host_)
host_->OnFieldAdded(fields_.back().get());
}
} // namespace ui
|