1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/javascript_dialogs/views/app_modal_dialog_view_views.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "build/build_config.h"
9 #include "components/javascript_dialogs/app_modal_dialog_controller.h"
10 #include "components/strings/grit/components_strings.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/events/keycodes/keyboard_codes.h"
13 #include "ui/views/controls/message_box_view.h"
14 #include "ui/views/controls/textfield/textfield.h"
15 #include "ui/views/widget/widget.h"
16
17 namespace javascript_dialogs {
18
19 ////////////////////////////////////////////////////////////////////////////////
20 // AppModalDialogViewViews, public:
21
AppModalDialogViewViews(AppModalDialogController * controller)22 AppModalDialogViewViews::AppModalDialogViewViews(
23 AppModalDialogController* controller)
24 : controller_(controller) {
25 SetOwnedByWidget(true);
26 message_box_view_ = new views::MessageBoxView(
27 controller->message_text(), /* detect_directionality = */ true);
28
29 if (controller->javascript_dialog_type() ==
30 content::JAVASCRIPT_DIALOG_TYPE_PROMPT) {
31 message_box_view_->SetPromptField(controller->default_prompt_text());
32 }
33
34 message_box_view_->AddAccelerator(
35 ui::Accelerator(ui::VKEY_C, ui::EF_CONTROL_DOWN));
36 if (controller->display_suppress_checkbox()) {
37 message_box_view_->SetCheckBoxLabel(
38 l10n_util::GetStringUTF16(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION));
39 }
40
41 DialogDelegate::SetButtons(
42 controller_->javascript_dialog_type() ==
43 content::JAVASCRIPT_DIALOG_TYPE_ALERT
44 ? ui::DIALOG_BUTTON_OK
45 : (ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL));
46 DialogDelegate::SetAcceptCallback(base::BindOnce(
47 [](AppModalDialogViewViews* dialog) {
48 dialog->controller_->OnAccept(
49 dialog->message_box_view_->GetInputText(),
50 dialog->message_box_view_->IsCheckBoxSelected());
51 },
52 base::Unretained(this)));
53 auto cancel_callback = [](AppModalDialogViewViews* dialog) {
54 dialog->controller_->OnCancel(
55 dialog->message_box_view_->IsCheckBoxSelected());
56 };
57 DialogDelegate::SetCancelCallback(
58 base::BindOnce(cancel_callback, base::Unretained(this)));
59 DialogDelegate::SetCloseCallback(
60 base::BindOnce(cancel_callback, base::Unretained(this)));
61
62 if (controller_->is_before_unload_dialog()) {
63 DialogDelegate::SetButtonLabel(
64 ui::DIALOG_BUTTON_OK,
65 l10n_util::GetStringUTF16(
66 controller_->is_reload()
67 ? IDS_BEFORERELOAD_MESSAGEBOX_OK_BUTTON_LABEL
68 : IDS_BEFOREUNLOAD_MESSAGEBOX_OK_BUTTON_LABEL));
69 }
70 }
71
72 AppModalDialogViewViews::~AppModalDialogViewViews() = default;
73
74 ////////////////////////////////////////////////////////////////////////////////
75 // AppModalDialogViewViews, AppModalDialogView implementation:
76
ShowAppModalDialog()77 void AppModalDialogViewViews::ShowAppModalDialog() {
78 GetWidget()->Show();
79 }
80
ActivateAppModalDialog()81 void AppModalDialogViewViews::ActivateAppModalDialog() {
82 GetWidget()->Show();
83 GetWidget()->Activate();
84 }
85
CloseAppModalDialog()86 void AppModalDialogViewViews::CloseAppModalDialog() {
87 GetWidget()->Close();
88 }
89
AcceptAppModalDialog()90 void AppModalDialogViewViews::AcceptAppModalDialog() {
91 AcceptDialog();
92 }
93
CancelAppModalDialog()94 void AppModalDialogViewViews::CancelAppModalDialog() {
95 CancelDialog();
96 }
97
IsShowing() const98 bool AppModalDialogViewViews::IsShowing() const {
99 return GetWidget()->IsVisible();
100 }
101
102 //////////////////////////////////////////////////////////////////////////////
103 // AppModalDialogViewViews, views::DialogDelegate implementation:
104
GetWindowTitle() const105 base::string16 AppModalDialogViewViews::GetWindowTitle() const {
106 return controller_->title();
107 }
108
GetModalType() const109 ui::ModalType AppModalDialogViewViews::GetModalType() const {
110 #if defined(OS_CHROMEOS)
111 // TODO(https://crbug.com/1127133): Remove this hack. This works around the
112 // linked bug. This dialog should be window-modal on ChromeOS as well.
113 return ui::MODAL_TYPE_SYSTEM;
114 #else
115 return ui::MODAL_TYPE_WINDOW;
116 #endif
117 }
118
GetContentsView()119 views::View* AppModalDialogViewViews::GetContentsView() {
120 return message_box_view_;
121 }
122
GetInitiallyFocusedView()123 views::View* AppModalDialogViewViews::GetInitiallyFocusedView() {
124 if (message_box_view_->GetVisiblePromptField())
125 return message_box_view_->GetVisiblePromptField();
126 return views::DialogDelegate::GetInitiallyFocusedView();
127 }
128
ShouldShowCloseButton() const129 bool AppModalDialogViewViews::ShouldShowCloseButton() const {
130 return false;
131 }
132
WindowClosing()133 void AppModalDialogViewViews::WindowClosing() {
134 controller_->OnClose();
135 }
136
GetWidget()137 views::Widget* AppModalDialogViewViews::GetWidget() {
138 return message_box_view_->GetWidget();
139 }
140
GetWidget() const141 const views::Widget* AppModalDialogViewViews::GetWidget() const {
142 return message_box_view_->GetWidget();
143 }
144
145 } // namespace javascript_dialogs
146