OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/srt_prompt_dialog.h" | 5 #include "chrome/browser/ui/views/srt_prompt_dialog.h" |
6 | 6 |
7 #include <vector> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "base/strings/string16.h" | 7 #include "base/strings/string16.h" |
11 #include "chrome/app/vector_icons/vector_icons.h" | |
12 #include "chrome/browser/safe_browsing/srt_prompt_controller.h" | 8 #include "chrome/browser/safe_browsing/srt_prompt_controller.h" |
13 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/browser/ui/browser_dialogs.h" | 10 #include "chrome/browser/ui/browser_dialogs.h" |
15 #include "chrome/browser/ui/browser_window.h" | 11 #include "chrome/browser/ui/browser_window.h" |
16 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h" | |
17 #include "components/constrained_window/constrained_window_views.h" | 12 #include "components/constrained_window/constrained_window_views.h" |
18 #include "components/web_modal/web_contents_modal_dialog_host.h" | |
19 #include "ui/base/ui_base_types.h" | 13 #include "ui/base/ui_base_types.h" |
20 #include "ui/events/event.h" | 14 #include "ui/events/event.h" |
21 #include "ui/gfx/animation/slide_animation.h" | |
22 #include "ui/gfx/geometry/size.h" | |
23 #include "ui/gfx/native_widget_types.h" | 15 #include "ui/gfx/native_widget_types.h" |
24 #include "ui/gfx/paint_vector_icon.h" | |
25 #include "ui/gfx/text_constants.h" | 16 #include "ui/gfx/text_constants.h" |
26 #include "ui/native_theme/native_theme.h" | |
27 #include "ui/views/border.h" | |
28 #include "ui/views/controls/label.h" | 17 #include "ui/views/controls/label.h" |
29 #include "ui/views/controls/scroll_view.h" | |
30 #include "ui/views/controls/separator.h" | |
31 #include "ui/views/layout/box_layout.h" | 18 #include "ui/views/layout/box_layout.h" |
32 #include "ui/views/layout/fill_layout.h" | |
33 #include "ui/views/layout/grid_layout.h" | |
34 #include "ui/views/layout/layout_constants.h" | 19 #include "ui/views/layout/layout_constants.h" |
35 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
36 | 21 |
37 namespace chrome { | 22 namespace chrome { |
38 | 23 |
39 void ShowSRTPrompt(Browser* browser, | 24 void ShowSRTPrompt(Browser* browser, |
40 safe_browsing::SRTPromptController* controller) { | 25 safe_browsing::SRTPromptController* controller) { |
41 SRTPromptDialog* dialog = new SRTPromptDialog(controller); | 26 SRTPromptDialog* dialog = new SRTPromptDialog(controller); |
42 dialog->Show(browser); | 27 dialog->Show(browser); |
43 } | 28 } |
44 | 29 |
45 } // namespace chrome | 30 } // namespace chrome |
46 | 31 |
47 namespace { | 32 namespace { |
48 | |
49 using LabelInfo = safe_browsing::SRTPromptController::LabelInfo; | |
50 | |
51 constexpr int kDialogWidth = 448; | 33 constexpr int kDialogWidth = 448; |
52 constexpr int kDetailsSectionMaxHeight = 150; | |
53 constexpr int kBulletColumnWidth = 10; | |
54 // Constants used for the layout of the label views. | |
55 constexpr int kMainColumSetId = 0; | |
56 constexpr int kBulletColumnSetId = 1; | |
57 | |
58 // Returns a view containing |item| with insets defined by |top|, |left|, | |
59 // |bottom|, and |right|. | |
60 views::View* CreateViewWithInsets(views::View* item, | |
61 int top, | |
62 int left, | |
63 int bottom, | |
64 int right) { | |
65 views::View* view = new views::View(); | |
66 view->SetLayoutManager(new views::FillLayout()); | |
67 view->SetBorder(views::CreateEmptyBorder(top, left, bottom, right)); | |
68 view->AddChildView(item); | |
69 return view; | |
70 } | |
71 | |
72 // Helper function used by |CreateLabelView()| below that adds |labels| to | |
73 // |label_view|. | |
74 void AddLabelsToLabelView(views::View* label_view, | |
75 views::GridLayout* layout, | |
76 const std::vector<LabelInfo>& labels) { | |
77 static constexpr base::char16 kBulletPoint[] = {0x2022, 0}; | |
78 | |
79 bool first_label = true; | |
80 bool last_label_was_bullet = false; | |
81 for (const LabelInfo& label_info : labels) { | |
82 const bool is_bullet = label_info.type == LabelInfo::BULLET_ITEM; | |
83 views::Label* label = new views::Label(label_info.text); | |
84 label->SetMultiLine(true); | |
85 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
86 | |
87 // Do not add a padding row if | |
88 // - this is the first label being added, or | |
89 // - a bullet item is being added and the last label was also a bullet item. | |
90 bool skip_padding = first_label || (is_bullet && last_label_was_bullet); | |
91 if (!skip_padding) | |
92 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
93 | |
94 layout->StartRow(0, is_bullet ? kBulletColumnSetId : kMainColumSetId); | |
95 if (is_bullet) { | |
96 views::Label* bullet = new views::Label(base::string16(kBulletPoint)); | |
97 layout->AddView(bullet); | |
98 } | |
99 layout->AddView(label); | |
100 | |
101 last_label_was_bullet = is_bullet; | |
102 first_label = false; | |
103 } | |
104 } | |
105 | |
106 // Creates a view that displays two types of labels: multiline labels | |
107 // representing whole paragraphs or indented labels that are start with a bullet | |
108 // point. | |
109 // | |
110 // A vertical padding of size |top_vertical_space| is added before the labels. | |
111 views::View* CreateLabelView(int top_vertical_space, | |
112 const std::vector<LabelInfo>& labels) { | |
113 views::View* label_view = new views::View(); | |
114 views::GridLayout* layout = new views::GridLayout(label_view); | |
115 layout->SetInsets(0, views::kButtonHEdgeMarginNew, 0, | |
116 views::kButtonHEdgeMarginNew); | |
117 | |
118 label_view->SetLayoutManager(layout); | |
119 | |
120 views::ColumnSet* main_column_set = layout->AddColumnSet(kMainColumSetId); | |
121 main_column_set->AddColumn(views::GridLayout::FILL, | |
122 views::GridLayout::LEADING, | |
123 /*resize_percent=*/1, views::GridLayout::USE_PREF, | |
124 /*fixed_width=*/0, | |
125 /*min_width=*/0); | |
126 | |
127 views::ColumnSet* bullet_column_set_ = | |
128 layout->AddColumnSet(kBulletColumnSetId); | |
129 bullet_column_set_->AddPaddingColumn( | |
130 /*resize_percent=*/0, views::kUnrelatedControlLargeHorizontalSpacing); | |
131 bullet_column_set_->AddColumn( | |
132 views::GridLayout::FILL, views::GridLayout::LEADING, | |
133 /*resize_percent=*/0, views::GridLayout::USE_PREF, | |
134 /*fixed_width=*/0, | |
135 /*min_width=*/0); | |
136 bullet_column_set_->AddPaddingColumn(/*resize_percent=*/0, | |
137 kBulletColumnWidth); | |
138 bullet_column_set_->AddColumn( | |
139 views::GridLayout::FILL, views::GridLayout::LEADING, | |
140 /*resize_percent=*/1, views::GridLayout::USE_PREF, | |
141 /*fixed_width=*/0, | |
142 /*min_width=*/0); | |
143 | |
144 if (top_vertical_space > 0) | |
145 layout->AddPaddingRow(/*vertical_resize=*/0, top_vertical_space); | |
146 | |
147 AddLabelsToLabelView(label_view, layout, labels); | |
148 | |
149 layout->AddPaddingRow(/*vertical_resize=*/0, | |
150 views::kUnrelatedControlLargeHorizontalSpacing); | |
151 return label_view; | |
152 } | |
153 | |
154 } // namespace | 34 } // namespace |
155 | 35 |
156 //////////////////////////////////////////////////////////////////////////////// | 36 //////////////////////////////////////////////////////////////////////////////// |
157 // SRTPromptDialog::ExpandableMessageView | |
158 // | |
159 // A view, whose visibilty can be toggled, and will be used for the details | |
160 // section the main dialog. | |
161 class SRTPromptDialog::ExpandableMessageView : public views::View { | |
162 public: | |
163 explicit ExpandableMessageView(const std::vector<LabelInfo>& labels); | |
164 ~ExpandableMessageView() override; | |
165 | |
166 void AnimateToState(double state); | |
167 | |
168 // views::View overrides. | |
169 gfx::Size GetPreferredSize() const override; | |
170 int GetHeightForWidth(int width) const override; | |
171 | |
172 private: | |
173 // A number between 0 and 1 that determines how much of the view's preferred | |
174 // height should be visible. | |
175 double animation_state_; | |
176 | |
177 DISALLOW_COPY_AND_ASSIGN(ExpandableMessageView); | |
178 }; | |
179 | |
180 SRTPromptDialog::ExpandableMessageView::ExpandableMessageView( | |
181 const std::vector<LabelInfo>& labels) | |
182 : animation_state_(0.0) { | |
183 // Add the main message view inside a scroll view. | |
184 views::View* label_view = | |
185 CreateLabelView(views::kUnrelatedControlLargeHorizontalSpacing, labels); | |
186 views::ScrollView* scroll_view = new views::ScrollView(); | |
187 scroll_view->ClipHeightTo(kDetailsSectionMaxHeight, kDetailsSectionMaxHeight); | |
188 scroll_view->SetContents(label_view); | |
189 scroll_view->SetSize(gfx::Size(kDialogWidth, kDetailsSectionMaxHeight)); | |
190 AddChildView(scroll_view); | |
191 } | |
192 | |
193 SRTPromptDialog::ExpandableMessageView::~ExpandableMessageView() {} | |
194 | |
195 void SRTPromptDialog::ExpandableMessageView::AnimateToState(double state) { | |
196 DCHECK_LE(0.0, state); | |
197 DCHECK_GE(1.0, state); | |
198 | |
199 animation_state_ = state; | |
200 PreferredSizeChanged(); | |
201 } | |
202 | |
203 gfx::Size SRTPromptDialog::ExpandableMessageView::GetPreferredSize() const { | |
204 return gfx::Size(kDialogWidth, kDetailsSectionMaxHeight * animation_state_); | |
205 } | |
206 | |
207 int SRTPromptDialog::ExpandableMessageView::GetHeightForWidth(int width) const { | |
208 return GetPreferredSize().height(); | |
209 } | |
210 | |
211 //////////////////////////////////////////////////////////////////////////////// | |
212 // SRTPromptDialog | 37 // SRTPromptDialog |
213 | 38 |
214 SRTPromptDialog::SRTPromptDialog(safe_browsing::SRTPromptController* controller) | 39 SRTPromptDialog::SRTPromptDialog(safe_browsing::SRTPromptController* controller) |
215 : browser_(nullptr), | 40 : browser_(nullptr), |
216 controller_(controller), | 41 controller_(controller), |
217 slide_animation_(base::MakeUnique<gfx::SlideAnimation>(this)), | 42 advanced_button_( |
218 details_view_(new ExpandableMessageView(controller_->GetDetailsText())), | 43 new views::LabelButton(this, controller_->GetAdvancedButtonLabel())) { |
219 details_button_( | |
220 new views::LabelButton(this, controller_->GetShowDetailsLabel())) { | |
221 DCHECK(controller_); | 44 DCHECK(controller_); |
222 | 45 |
223 SetLayoutManager(new views::BoxLayout( | 46 SetLayoutManager(new views::BoxLayout( |
224 /*orientation=*/views::BoxLayout::kVertical, | 47 /*orientation=*/views::BoxLayout::kVertical, |
225 /*inside_border_horizontal_spacing=*/0, | 48 /*inside_border_horizontal_spacing=*/views::kButtonHEdgeMarginNew, |
226 /*inside_border_vertical_spacing=*/views::kPanelVertMargin, | 49 /*inside_border_vertical_spacing=*/views::kPanelVertMargin, |
227 /*between_child_spacing=*/0)); | 50 /*between_child_spacing=*/0)); |
| 51 views::Label* label = new views::Label(controller_->GetMainText()); |
| 52 label->SetMultiLine(true); |
| 53 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 54 AddChildView(label); |
228 | 55 |
229 AddChildView(CreateLabelView(0, controller_->GetMainText())); | 56 advanced_button_->SetStyle(views::Button::STYLE_BUTTON); |
230 AddChildView(new views::Separator()); | |
231 | |
232 AddChildView(details_view_); | |
233 | |
234 details_button_->SetEnabledTextColors(GetDetailsButtonColor()); | |
235 UpdateDetailsButton(); | |
236 AddChildView(CreateViewWithInsets( | |
237 details_button_, views::kPanelVertMargin, views::kButtonHEdgeMarginNew, | |
238 views::kPanelVertMargin, views::kButtonHEdgeMarginNew)); | |
239 AddChildView(new views::Separator()); | |
240 } | 57 } |
241 | 58 |
242 SRTPromptDialog::~SRTPromptDialog() { | 59 SRTPromptDialog::~SRTPromptDialog() { |
243 // Make sure the controller is correctly notified in case the dialog widget is | 60 // Make sure the controller is correctly notified in case the dialog widget is |
244 // closed by some other means than the dialog buttons. | 61 // closed by some other means than the dialog buttons. |
245 if (controller_) | 62 if (controller_) |
246 controller_->Cancel(); | 63 controller_->Cancel(); |
247 } | 64 } |
248 | 65 |
249 void SRTPromptDialog::Show(Browser* browser) { | 66 void SRTPromptDialog::Show(Browser* browser) { |
(...skipping 25 matching lines...) Expand all Loading... |
275 return controller_->GetWindowTitle(); | 92 return controller_->GetWindowTitle(); |
276 } | 93 } |
277 | 94 |
278 // DialogDelegate overrides. | 95 // DialogDelegate overrides. |
279 | 96 |
280 base::string16 SRTPromptDialog::GetDialogButtonLabel( | 97 base::string16 SRTPromptDialog::GetDialogButtonLabel( |
281 ui::DialogButton button) const { | 98 ui::DialogButton button) const { |
282 DCHECK(button == ui::DIALOG_BUTTON_OK || button == ui::DIALOG_BUTTON_CANCEL); | 99 DCHECK(button == ui::DIALOG_BUTTON_OK || button == ui::DIALOG_BUTTON_CANCEL); |
283 DCHECK(controller_); | 100 DCHECK(controller_); |
284 | 101 |
285 if (button == ui::DIALOG_BUTTON_OK) | 102 return button == ui::DIALOG_BUTTON_OK |
286 return controller_->GetAcceptButtonLabel(); | 103 ? controller_->GetAcceptButtonLabel() |
287 return DialogDelegate::GetDialogButtonLabel(button); | 104 : DialogDelegate::GetDialogButtonLabel(button); |
| 105 } |
| 106 |
| 107 views::View* SRTPromptDialog::CreateExtraView() { |
| 108 return advanced_button_; |
288 } | 109 } |
289 | 110 |
290 bool SRTPromptDialog::Accept() { | 111 bool SRTPromptDialog::Accept() { |
291 if (controller_) { | 112 if (controller_) { |
292 controller_->Accept(); | 113 controller_->Accept(); |
293 controller_ = nullptr; | 114 controller_ = nullptr; |
294 } | 115 } |
295 return true; | 116 return true; |
296 } | 117 } |
297 | 118 |
298 bool SRTPromptDialog::Cancel() { | 119 bool SRTPromptDialog::Cancel() { |
299 if (controller_) { | 120 if (controller_) { |
300 controller_->Cancel(); | 121 controller_->Cancel(); |
301 controller_ = nullptr; | 122 controller_ = nullptr; |
302 } | 123 } |
303 return true; | 124 return true; |
304 } | 125 } |
305 | 126 |
| 127 bool SRTPromptDialog::Close() { |
| 128 if (controller_) { |
| 129 controller_->Close(); |
| 130 controller_ = nullptr; |
| 131 } |
| 132 return true; |
| 133 } |
| 134 |
306 // View overrides. | 135 // View overrides. |
307 | 136 |
308 gfx::Size SRTPromptDialog::GetPreferredSize() const { | 137 gfx::Size SRTPromptDialog::GetPreferredSize() const { |
309 return gfx::Size(kDialogWidth, GetHeightForWidth(kDialogWidth)); | 138 return gfx::Size(kDialogWidth, GetHeightForWidth(kDialogWidth)); |
310 } | 139 } |
311 | 140 |
312 // views::ButtonListener overrides. | 141 // views::ButtonListener overrides. |
313 | 142 |
314 void SRTPromptDialog::ButtonPressed(views::Button* sender, | 143 void SRTPromptDialog::ButtonPressed(views::Button* sender, |
315 const ui::Event& event) { | 144 const ui::Event& event) { |
316 DCHECK_EQ(sender, details_button_); | 145 DCHECK_EQ(sender, advanced_button_); |
317 DCHECK(browser_); | 146 DCHECK(browser_); |
318 | 147 |
319 if (slide_animation_->IsShowing()) | 148 // TODO(alito): Navigate to the webui version of the Chrome Cleaner UI when |
320 slide_animation_->Hide(); | 149 // that is implemented. |
321 else | 150 if (controller_) { |
322 slide_animation_->Show(); | 151 controller_->AdvancedButtonClicked(); |
| 152 controller_ = nullptr; |
| 153 } |
| 154 GetWidget()->Close(); |
323 } | 155 } |
324 | |
325 void SRTPromptDialog::AnimationProgressed(const gfx::Animation* animation) { | |
326 DCHECK_EQ(slide_animation_.get(), animation); | |
327 | |
328 details_view_->AnimateToState(animation->GetCurrentValue()); | |
329 ChromeWebModalDialogManagerDelegate* manager = browser_; | |
330 constrained_window::UpdateWidgetModalDialogPosition( | |
331 GetWidget(), manager->GetWebContentsModalDialogHost()); | |
332 } | |
333 | |
334 void SRTPromptDialog::AnimationEnded(const gfx::Animation* animation) { | |
335 DCHECK_EQ(slide_animation_.get(), animation); | |
336 UpdateDetailsButton(); | |
337 } | |
338 | |
339 SkColor SRTPromptDialog::GetDetailsButtonColor() { | |
340 return GetNativeTheme()->GetSystemColor( | |
341 ui::NativeTheme::kColorId_LinkEnabled); | |
342 } | |
343 | |
344 void SRTPromptDialog::UpdateDetailsButton() { | |
345 DCHECK(controller_); | |
346 details_button_->SetText(slide_animation_->IsShowing() | |
347 ? controller_->GetHideDetailsLabel() | |
348 : controller_->GetShowDetailsLabel()); | |
349 details_button_->SetImage( | |
350 views::Button::STATE_NORMAL, | |
351 slide_animation_->IsShowing() | |
352 ? gfx::CreateVectorIcon(kCaretUpIcon, GetDetailsButtonColor()) | |
353 : gfx::CreateVectorIcon(kCaretDownIcon, GetDetailsButtonColor())); | |
354 } | |
OLD | NEW |