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
|
// Copyright (c) 2012 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.
#include "ui/views/window/dialog_delegate.h"
#include <utility>
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "build/build_config.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/color_palette.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/buildflags.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/views_features.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h"
#include "ui/views/window/dialog_client_view.h"
#include "ui/views/window/dialog_observer.h"
#if defined(OS_WIN)
#include "ui/base/win/shell.h"
#endif
namespace views {
////////////////////////////////////////////////////////////////////////////////
// DialogDelegate::Params:
DialogDelegate::Params::Params() = default;
DialogDelegate::Params::~Params() = default;
////////////////////////////////////////////////////////////////////////////////
// DialogDelegate:
DialogDelegate::DialogDelegate() {
WidgetDelegate::RegisterWindowWillCloseCallback(
base::BindOnce(&DialogDelegate::WindowWillClose, base::Unretained(this)));
UMA_HISTOGRAM_BOOLEAN("Dialog.DialogDelegate.Create", true);
creation_time_ = base::TimeTicks::Now();
}
// static
Widget* DialogDelegate::CreateDialogWidget(WidgetDelegate* delegate,
gfx::NativeWindow context,
gfx::NativeView parent) {
views::Widget* widget = new views::Widget;
views::Widget::InitParams params =
GetDialogWidgetInitParams(delegate, context, parent, gfx::Rect());
widget->Init(std::move(params));
return widget;
}
// static
bool DialogDelegate::CanSupportCustomFrame(gfx::NativeView parent) {
#if defined(OS_LINUX) && BUILDFLAG(ENABLE_DESKTOP_AURA)
// The new style doesn't support unparented dialogs on Linux desktop.
return parent != nullptr;
#else
#if defined(OS_WIN)
// The new style doesn't support unparented dialogs on Windows Classic themes.
if (!ui::win::IsAeroGlassEnabled())
return parent != nullptr;
#endif
return true;
#endif
}
// static
Widget::InitParams DialogDelegate::GetDialogWidgetInitParams(
WidgetDelegate* delegate,
gfx::NativeWindow context,
gfx::NativeView parent,
const gfx::Rect& bounds) {
views::Widget::InitParams params;
params.delegate = delegate;
params.bounds = bounds;
DialogDelegate* dialog = delegate->AsDialogDelegate();
if (dialog)
dialog->params_.custom_frame &= CanSupportCustomFrame(parent);
if (!dialog || dialog->use_custom_frame()) {
params.opacity = Widget::InitParams::WindowOpacity::kTranslucent;
params.remove_standard_frame = true;
#if !defined(OS_MACOSX)
// Except on Mac, the bubble frame includes its own shadow; remove any
// native shadowing. On Mac, the window server provides the shadow.
params.shadow_type = views::Widget::InitParams::ShadowType::kNone;
#endif
}
params.context = context;
params.parent = parent;
#if !defined(OS_MACOSX)
// Web-modal (ui::MODAL_TYPE_CHILD) dialogs with parents are marked as child
// widgets to prevent top-level window behavior (independent movement, etc).
// On Mac, however, the parent may be a native window (not a views::Widget),
// and so the dialog must be considered top-level to gain focus and input
// method behaviors.
params.child = parent && (delegate->GetModalType() == ui::MODAL_TYPE_CHILD);
#endif
return params;
}
int DialogDelegate::GetDefaultDialogButton() const {
if (GetParams().default_button.has_value())
return *GetParams().default_button;
if (GetDialogButtons() & ui::DIALOG_BUTTON_OK)
return ui::DIALOG_BUTTON_OK;
if (GetDialogButtons() & ui::DIALOG_BUTTON_CANCEL)
return ui::DIALOG_BUTTON_CANCEL;
return ui::DIALOG_BUTTON_NONE;
}
base::string16 DialogDelegate::GetDialogButtonLabel(
ui::DialogButton button) const {
if (!GetParams().button_labels[button].empty())
return GetParams().button_labels[button];
if (button == ui::DIALOG_BUTTON_OK)
return l10n_util::GetStringUTF16(IDS_APP_OK);
if (button == ui::DIALOG_BUTTON_CANCEL) {
if (GetDialogButtons() & ui::DIALOG_BUTTON_OK)
return l10n_util::GetStringUTF16(IDS_APP_CANCEL);
return l10n_util::GetStringUTF16(IDS_APP_CLOSE);
}
NOTREACHED();
return base::string16();
}
bool DialogDelegate::IsDialogButtonEnabled(ui::DialogButton button) const {
return params_.enabled_buttons & button;
}
bool DialogDelegate::Cancel() {
DCHECK(!already_started_close_);
if (cancel_callback_)
RunCloseCallback(std::move(cancel_callback_));
return true;
}
bool DialogDelegate::Accept() {
DCHECK(!already_started_close_);
if (accept_callback_)
RunCloseCallback(std::move(accept_callback_));
return true;
}
void DialogDelegate::RunCloseCallback(base::OnceClosure callback) {
DCHECK(!already_started_close_);
already_started_close_ = true;
std::move(callback).Run();
}
View* DialogDelegate::GetInitiallyFocusedView() {
if (params_.initially_focused_view.has_value())
return *params_.initially_focused_view;
// Focus the default button if any.
const DialogClientView* dcv = GetDialogClientView();
if (!dcv)
return nullptr;
int default_button = GetDefaultDialogButton();
if (default_button == ui::DIALOG_BUTTON_NONE)
return nullptr;
if ((default_button & GetDialogButtons()) == 0) {
// The default button is a button we don't have.
NOTREACHED();
return nullptr;
}
if (default_button & ui::DIALOG_BUTTON_OK)
return dcv->ok_button();
if (default_button & ui::DIALOG_BUTTON_CANCEL)
return dcv->cancel_button();
return nullptr;
}
DialogDelegate* DialogDelegate::AsDialogDelegate() {
return this;
}
ClientView* DialogDelegate::CreateClientView(Widget* widget) {
return new DialogClientView(widget, GetContentsView());
}
NonClientFrameView* DialogDelegate::CreateNonClientFrameView(Widget* widget) {
if (use_custom_frame())
return CreateDialogFrameView(widget);
return WidgetDelegate::CreateNonClientFrameView(widget);
}
void DialogDelegate::WindowWillClose() {
if (already_started_close_)
return;
bool new_callback_present =
close_callback_ || cancel_callback_ || accept_callback_;
if (close_callback_)
RunCloseCallback(std::move(close_callback_));
if (new_callback_present)
return;
// Old-style close behavior: if the only button was Ok, call Accept();
// otherwise call Cancel(). Note that in this case the window is already going
// to close, so the return values of Accept()/Cancel(), which normally say
// whether the window should close, are ignored.
int buttons = GetDialogButtons();
if (buttons == ui::DIALOG_BUTTON_OK)
Accept();
else
Cancel();
// This is set here instead of before the invocations of Accept()/Cancel() so
// that those methods can DCHECK that !already_started_close_. Otherwise,
// client code could (eg) call Accept() from inside the cancel callback, which
// could lead to multiple callbacks being delivered from this class.
already_started_close_ = true;
}
// static
NonClientFrameView* DialogDelegate::CreateDialogFrameView(Widget* widget) {
LayoutProvider* provider = LayoutProvider::Get();
BubbleFrameView* frame = new BubbleFrameView(
provider->GetInsetsMetric(INSETS_DIALOG_TITLE), gfx::Insets());
const BubbleBorder::Shadow kShadow = BubbleBorder::DIALOG_SHADOW;
std::unique_ptr<BubbleBorder> border = std::make_unique<BubbleBorder>(
BubbleBorder::FLOAT, kShadow, gfx::kPlaceholderColor);
border->set_use_theme_background_color(true);
DialogDelegate* delegate = widget->widget_delegate()->AsDialogDelegate();
if (delegate) {
if (delegate->GetParams().round_corners) {
border->SetCornerRadius(
base::FeatureList::IsEnabled(
features::kEnableMDRoundedCornersOnDialogs)
? provider->GetCornerRadiusMetric(views::EMPHASIS_HIGH)
: 2);
}
frame->SetFootnoteView(delegate->DisownFootnoteView());
}
frame->SetBubbleBorder(std::move(border));
return frame;
}
const DialogClientView* DialogDelegate::GetDialogClientView() const {
if (!GetWidget())
return nullptr;
const views::View* client_view = GetWidget()->client_view();
return client_view->GetClassName() == DialogClientView::kViewClassName
? static_cast<const DialogClientView*>(client_view)
: nullptr;
}
DialogClientView* DialogDelegate::GetDialogClientView() {
if (!GetWidget())
return nullptr;
views::View* client_view = GetWidget()->client_view();
return client_view->GetClassName() == DialogClientView::kViewClassName
? static_cast<DialogClientView*>(client_view)
: nullptr;
}
BubbleFrameView* DialogDelegate::GetBubbleFrameView() const {
if (!use_custom_frame())
return nullptr;
const NonClientView* view =
GetWidget() ? GetWidget()->non_client_view() : nullptr;
return view ? static_cast<BubbleFrameView*>(view->frame_view()) : nullptr;
}
views::LabelButton* DialogDelegate::GetOkButton() const {
DCHECK(GetWidget()) << "Don't call this before OnDialogInitialized";
auto* client = GetDialogClientView();
return client ? client->ok_button() : nullptr;
}
views::LabelButton* DialogDelegate::GetCancelButton() const {
DCHECK(GetWidget()) << "Don't call this before OnDialogInitialized";
auto* client = GetDialogClientView();
return client ? client->cancel_button() : nullptr;
}
views::View* DialogDelegate::GetExtraView() const {
DCHECK(GetWidget()) << "Don't call this before OnDialogInitialized";
auto* client = GetDialogClientView();
return client ? client->extra_view() : nullptr;
}
views::View* DialogDelegate::GetFootnoteViewForTesting() const {
if (!GetWidget())
return footnote_view_.get();
NonClientFrameView* frame = GetWidget()->non_client_view()->frame_view();
// CreateDialogFrameView above always uses BubbleFrameView. There are
// subclasses that override CreateDialogFrameView, but none of them override
// it to create anything other than a BubbleFrameView.
// TODO(https://crbug.com/1011446): Make CreateDialogFrameView final, then
// remove this DCHECK.
DCHECK_EQ(frame->GetClassName(), BubbleFrameView::kViewClassName);
return static_cast<BubbleFrameView*>(frame)->GetFootnoteView();
}
void DialogDelegate::AddObserver(DialogObserver* observer) {
observer_list_.AddObserver(observer);
}
void DialogDelegate::RemoveObserver(DialogObserver* observer) {
observer_list_.RemoveObserver(observer);
}
void DialogDelegate::DialogModelChanged() {
for (DialogObserver& observer : observer_list_)
observer.OnDialogChanged();
}
void DialogDelegate::SetDefaultButton(int button) {
if (params_.default_button == button)
return;
params_.default_button = button;
DialogModelChanged();
}
void DialogDelegate::SetButtons(int buttons) {
if (params_.buttons == buttons)
return;
params_.buttons = buttons;
DialogModelChanged();
}
void DialogDelegate::SetButtonEnabled(ui::DialogButton button, bool enabled) {
if (!!(params_.enabled_buttons & button) == enabled)
return;
if (enabled)
params_.enabled_buttons |= button;
else
params_.enabled_buttons &= ~button;
DialogModelChanged();
}
void DialogDelegate::SetButtonLabel(ui::DialogButton button,
base::string16 label) {
if (params_.button_labels[button] == label)
return;
params_.button_labels[button] = label;
DialogModelChanged();
}
void DialogDelegate::SetAcceptCallback(base::OnceClosure callback) {
accept_callback_ = std::move(callback);
}
void DialogDelegate::SetCancelCallback(base::OnceClosure callback) {
cancel_callback_ = std::move(callback);
}
void DialogDelegate::SetCloseCallback(base::OnceClosure callback) {
close_callback_ = std::move(callback);
}
void DialogDelegate::SetInitiallyFocusedView(View* view) {
params_.initially_focused_view = view;
}
std::unique_ptr<View> DialogDelegate::DisownExtraView() {
return std::move(extra_view_);
}
bool DialogDelegate::Close() {
WindowWillClose();
return true;
}
void DialogDelegate::ResetViewShownTimeStampForTesting() {
GetDialogClientView()->ResetViewShownTimeStampForTesting();
}
void DialogDelegate::SetButtonRowInsets(const gfx::Insets& insets) {
GetDialogClientView()->SetButtonRowInsets(insets);
}
void DialogDelegate::AcceptDialog() {
if (already_started_close_ || !Accept())
return;
already_started_close_ = true;
GetWidget()->CloseWithReason(
views::Widget::ClosedReason::kAcceptButtonClicked);
}
void DialogDelegate::CancelDialog() {
if (already_started_close_ || !Cancel())
return;
already_started_close_ = true;
GetWidget()->CloseWithReason(
views::Widget::ClosedReason::kCancelButtonClicked);
}
DialogDelegate::~DialogDelegate() {
UMA_HISTOGRAM_LONG_TIMES("Dialog.DialogDelegate.Duration",
base::TimeTicks::Now() - creation_time_);
}
ax::mojom::Role DialogDelegate::GetAccessibleWindowRole() {
return ax::mojom::Role::kDialog;
}
std::unique_ptr<View> DialogDelegate::DisownFootnoteView() {
return std::move(footnote_view_);
}
void DialogDelegate::OnWidgetInitialized() {
OnDialogInitialized();
}
////////////////////////////////////////////////////////////////////////////////
// DialogDelegateView:
DialogDelegateView::DialogDelegateView() {
// A WidgetDelegate should be deleted on DeleteDelegate.
set_owned_by_client();
UMA_HISTOGRAM_BOOLEAN("Dialog.DialogDelegateView.Create", true);
}
DialogDelegateView::~DialogDelegateView() = default;
void DialogDelegateView::DeleteDelegate() {
delete this;
}
Widget* DialogDelegateView::GetWidget() {
return View::GetWidget();
}
const Widget* DialogDelegateView::GetWidget() const {
return View::GetWidget();
}
View* DialogDelegateView::GetContentsView() {
return this;
}
void DialogDelegateView::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
if (details.is_add && details.child == this && GetWidget() &&
ui::IsAlert(GetAccessibleWindowRole())) {
NotifyAccessibilityEvent(ax::mojom::Event::kAlert, true);
}
}
} // namespace views
|