Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: chrome/browser/ui/views/srt_prompt_dialog.cc

Issue 2795133002: Chrome Cleaner UI: Add a dialog class for the new UI (Closed)
Patch Set: Rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "chrome/browser/ui/views/srt_prompt_dialog.h"
6
7 #include <vector>
8
9 #include "base/strings/string16.h"
10 #include "chrome/app/vector_icons/vector_icons.h"
11 #include "chrome/browser/safe_browsing/srt_prompt_controller.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_dialogs.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
16 #include "components/constrained_window/constrained_window_views.h"
17 #include "components/web_modal/web_contents_modal_dialog_host.h"
18 #include "ui/base/ui_base_types.h"
19 #include "ui/events/event.h"
20 #include "ui/gfx/geometry/size.h"
21 #include "ui/gfx/native_widget_types.h"
22 #include "ui/gfx/paint_vector_icon.h"
23 #include "ui/gfx/text_constants.h"
24 #include "ui/native_theme/native_theme.h"
25 #include "ui/views/border.h"
26 #include "ui/views/controls/label.h"
27 #include "ui/views/controls/scroll_view.h"
28 #include "ui/views/controls/separator.h"
29 #include "ui/views/layout/box_layout.h"
30 #include "ui/views/layout/fill_layout.h"
31 #include "ui/views/layout/grid_layout.h"
32 #include "ui/views/layout/layout_constants.h"
33 #include "ui/views/widget/widget.h"
34
35 namespace chrome {
36
37 void ShowSRTPrompt(Browser* browser,
38 safe_browsing::SRTPromptController* controller) {
39 SRTPromptDialog* dialog = new SRTPromptDialog(controller);
40 dialog->Show(browser);
41 }
42
43 } // namespace chrome
44
45 namespace {
46
47 using LabelInfo = safe_browsing::SRTPromptController::LabelInfo;
48
49 constexpr int kDialogWidth = 448;
50 constexpr int kDetailsSectionMaxHeight = 150;
51 constexpr int kBulletColumnWidth = 10;
52 // Constants used for the layout of the label views.
53 constexpr int kMainColumSetId = 0;
54 constexpr int kBulletColumnSetId = 1;
55
56 // Returns a view containing |item| with insets defined by |top|, |left|,
57 // |bottom|, and |right|.
58 views::View* CreateViewWithInsets(views::View* item,
59 int top,
60 int left,
61 int bottom,
62 int right) {
63 views::View* view = new views::View();
64 view->SetLayoutManager(new views::FillLayout());
65 view->SetBorder(views::CreateEmptyBorder(top, left, bottom, right));
66 view->AddChildView(item);
67 return view;
68 }
69
70 // Helper function used by |CreateLabelView()| below that adds |labels| to
71 // |label_view|.
72 void AddLabelsToLabelView(views::View* label_view,
73 views::GridLayout* layout,
74 const std::vector<LabelInfo>& labels) {
75 static constexpr base::char16 kBulletPoint[] = {0x2022, 0};
76
77 bool first_label = true;
78 bool last_label_was_bullet = false;
79 for (const LabelInfo& label_info : labels) {
80 const bool is_bullet = label_info.type == LabelInfo::BULLET_ITEM;
81 views::Label* label = new views::Label(label_info.text);
82 label->SetMultiLine(true);
83 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
84
85 // Do not add a padding row if
86 // - this is the first label being added, or
87 // - a bullet item is being added and the last label was also a bullet item.
88 bool skip_padding = first_label || (is_bullet && last_label_was_bullet);
89 if (!skip_padding)
90 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
91
92 layout->StartRow(0, is_bullet ? kBulletColumnSetId : kMainColumSetId);
93 if (is_bullet) {
94 views::Label* bullet = new views::Label(base::string16(kBulletPoint));
95 layout->AddView(bullet);
96 }
97 layout->AddView(label);
98
99 last_label_was_bullet = is_bullet;
100 first_label = false;
101 }
102 }
103
104 // Creates a view that displays two types of labels: multiline labels
105 // representing whole paragraphs or indented labels that are start with a bullet
106 // point.
107 //
108 // A vertical padding of size |top_vertical_space| is added before the labels.
109 views::View* CreateLabelView(int top_vertical_space,
110 const std::vector<LabelInfo>& labels) {
111 views::View* label_view = new views::View();
112 views::GridLayout* layout = new views::GridLayout(label_view);
113 layout->SetInsets(0, views::kButtonHEdgeMarginNew, 0,
114 views::kButtonHEdgeMarginNew);
115
116 label_view->SetLayoutManager(layout);
117
118 views::ColumnSet* main_column_set = layout->AddColumnSet(kMainColumSetId);
119 main_column_set->AddColumn(views::GridLayout::FILL,
120 views::GridLayout::LEADING,
121 /*resize_percent=*/1, views::GridLayout::USE_PREF,
122 /*fixed_width=*/0,
123 /*min_width=*/0);
124
125 views::ColumnSet* bullet_column_set_ =
126 layout->AddColumnSet(kBulletColumnSetId);
127 bullet_column_set_->AddPaddingColumn(
128 /*resize_percent=*/0, views::kUnrelatedControlLargeHorizontalSpacing);
129 bullet_column_set_->AddColumn(
130 views::GridLayout::FILL, views::GridLayout::LEADING,
131 /*resize_percent=*/0, views::GridLayout::USE_PREF,
132 /*fixed_width=*/0,
133 /*min_width=*/0);
134 bullet_column_set_->AddPaddingColumn(/*resize_percent=*/0,
135 kBulletColumnWidth);
136 bullet_column_set_->AddColumn(
137 views::GridLayout::FILL, views::GridLayout::LEADING,
138 /*resize_percent=*/1, views::GridLayout::USE_PREF,
139 /*fixed_width=*/0,
140 /*min_width=*/0);
141
142 if (top_vertical_space > 0)
143 layout->AddPaddingRow(/*vertical_resize=*/0, top_vertical_space);
144
145 AddLabelsToLabelView(label_view, layout, labels);
146
147 layout->AddPaddingRow(/*vertical_resize=*/0,
148 views::kUnrelatedControlLargeHorizontalSpacing);
149 return label_view;
150 }
151
152 } // namespace
153
154 ////////////////////////////////////////////////////////////////////////////////
155 // SRTPromptDialog
156
157 SRTPromptDialog::SRTPromptDialog(safe_browsing::SRTPromptController* controller)
158 : browser_(nullptr),
159 controller_(controller),
160 details_view_(new views::View()),
161 details_button_(nullptr) {
162 DCHECK(controller_);
163
164 SetLayoutManager(new views::BoxLayout(
165 /*orientation=*/views::BoxLayout::kVertical,
166 /*inside_border_horizontal_spacing=*/0,
167 /*inside_border_vertical_spacing=*/views::kPanelVertMargin,
168 /*between_child_spacing=*/0));
169
170 AddChildView(CreateLabelView(0, controller_->GetMainText()));
171 AddChildView(new views::Separator());
172
173 // The details section starts off empty and will be populated when the user
174 // clicks the button to expand the details section. Its child views are
175 // removed when the section is closed.
176 details_view_->SetLayoutManager(new views::FillLayout());
177 details_view_->SetVisible(false);
178 AddChildView(details_view_);
179
180 details_button_ =
181 new views::LabelButton(this, controller_->GetShowDetailsLabel());
182 details_button_->SetEnabledTextColors(GetDetailsButtonColor());
183 UpdateDetailsButton();
184 AddChildView(CreateViewWithInsets(
185 details_button_, views::kPanelVertMargin, views::kButtonHEdgeMarginNew,
186 views::kPanelVertMargin, views::kButtonHEdgeMarginNew));
187 AddChildView(new views::Separator());
188 }
189
190 SRTPromptDialog::~SRTPromptDialog() {
191 // Make sure the controller is correctly notified in case the dialog widget is
192 // closed by some other means than the dialog buttons.
193 if (controller_)
194 controller_->Cancel();
195 }
196
197 void SRTPromptDialog::Show(Browser* browser) {
198 DCHECK(browser);
199 DCHECK(!browser_);
200
201 browser_ = browser;
202 constrained_window::CreateBrowserModalDialogViews(
203 this, browser_->window()->GetNativeWindow())
204 ->Show();
205 controller_->DialogShown();
206 }
207
208 // DialogModel overrides.
209
210 bool SRTPromptDialog::ShouldDefaultButtonBeBlue() const {
211 return true;
212 }
213
214 // WidgetDelegate overrides.
215
216 ui::ModalType SRTPromptDialog::GetModalType() const {
217 return ui::MODAL_TYPE_WINDOW;
218 }
219
220 base::string16 SRTPromptDialog::GetWindowTitle() const {
221 return controller_->GetWindowTitle();
222 }
223
224 // DialogDelegate overrides.
225
226 base::string16 SRTPromptDialog::GetDialogButtonLabel(
227 ui::DialogButton button) const {
228 DCHECK(button == ui::DIALOG_BUTTON_OK || button == ui::DIALOG_BUTTON_CANCEL);
229
230 if (button == ui::DIALOG_BUTTON_OK)
231 return controller_->GetAcceptButtonLabel();
232 return DialogDelegate::GetDialogButtonLabel(button);
233 }
234
235 bool SRTPromptDialog::Accept() {
236 if (controller_) {
237 controller_->Accept();
238 controller_ = nullptr;
239 }
240 return true;
241 }
242
243 bool SRTPromptDialog::Cancel() {
244 if (controller_) {
245 controller_->Cancel();
246 controller_ = nullptr;
247 }
248 return true;
249 }
250
251 // View overrides.
252
253 gfx::Size SRTPromptDialog::GetPreferredSize() const {
254 return gfx::Size(kDialogWidth, GetHeightForWidth(kDialogWidth));
255 }
256
257 // views::ButtonListener overrides.
258
259 void SRTPromptDialog::ButtonPressed(views::Button* sender,
260 const ui::Event& event) {
261 DCHECK_EQ(sender, details_button_);
262 DCHECK(browser_);
263
264 details_view_->SetVisible(!details_view_->visible());
265 if (details_view_->visible()) {
266 // Populate the details view adding the main message view inside a scroll
267 // view.
268 views::View* label_view =
269 CreateLabelView(views::kUnrelatedControlLargeHorizontalSpacing,
270 controller_->GetDetailsText());
271 views::ScrollView* scroll_view = new views::ScrollView();
272 scroll_view->ClipHeightTo(/*min_height=*/0, kDetailsSectionMaxHeight);
273 scroll_view->SetContents(label_view);
274 details_view_->AddChildView(scroll_view);
275 } else {
276 details_view_->RemoveAllChildViews(/*delete_children=*/true);
277 }
278
279 UpdateDetailsButton();
280
281 ChromeWebModalDialogManagerDelegate* manager = browser_;
282 constrained_window::UpdateWidgetModalDialogPosition(
283 GetWidget(), manager->GetWebContentsModalDialogHost());
284 }
285
286 SkColor SRTPromptDialog::GetDetailsButtonColor() {
287 return GetNativeTheme()->GetSystemColor(
288 ui::NativeTheme::kColorId_LinkEnabled);
289 }
290
291 void SRTPromptDialog::UpdateDetailsButton() {
292 details_button_->SetText(details_view_->visible()
293 ? controller_->GetHideDetailsLabel()
294 : controller_->GetShowDetailsLabel());
295 details_button_->SetImage(
296 views::Button::STATE_NORMAL,
297 details_view_->visible()
298 ? gfx::CreateVectorIcon(kCaretUpIcon, GetDetailsButtonColor())
299 : gfx::CreateVectorIcon(kCaretDownIcon, GetDetailsButtonColor()));
300 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/srt_prompt_dialog.h ('k') | chrome/browser/ui/views/srt_prompt_dialog_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698