Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/chrome_web_modal_dialog_manager_delegate.h" | |
| 14 #include "chrome/browser/ui/views/frame/browser_view.h" | |
| 15 #include "components/constrained_window/constrained_window_views.h" | |
| 16 #include "components/web_modal/web_contents_modal_dialog_host.h" | |
| 17 #include "ui/base/ui_base_types.h" | |
| 18 #include "ui/events/event.h" | |
| 19 #include "ui/gfx/geometry/size.h" | |
| 20 #include "ui/gfx/native_widget_types.h" | |
| 21 #include "ui/gfx/paint_vector_icon.h" | |
| 22 #include "ui/gfx/text_constants.h" | |
| 23 #include "ui/native_theme/native_theme.h" | |
| 24 #include "ui/views/controls/label.h" | |
| 25 #include "ui/views/controls/scroll_view.h" | |
| 26 #include "ui/views/controls/separator.h" | |
| 27 #include "ui/views/layout/box_layout.h" | |
| 28 #include "ui/views/layout/grid_layout.h" | |
| 29 #include "ui/views/layout/layout_constants.h" | |
| 30 #include "ui/views/widget/widget.h" | |
| 31 | |
| 32 namespace safe_browsing { | |
| 33 | |
| 34 // static | |
| 35 void SRTPromptController::ShowSRTPrompt(Browser* browser, | |
| 36 SRTPromptController* controller) { | |
| 37 SRTPromptDialog* dialog = new SRTPromptDialog(controller); | |
| 38 dialog->Show(browser); | |
| 39 } | |
| 40 | |
| 41 } // namespace safe_browsing | |
| 42 | |
| 43 namespace { | |
| 44 | |
| 45 using LabelInfo = safe_browsing::SRTPromptController::LabelInfo; | |
| 46 | |
| 47 constexpr int kDialogWidth = 448; | |
| 48 constexpr int kDetailsSectionMaxHeight = 150; | |
| 49 constexpr int kBulletColumnWidth = 10; | |
| 50 // Constants used for the layout of the label views. | |
| 51 static constexpr int kMainColumSetId = 0; | |
| 52 static constexpr int kBulletColumnSetId = 1; | |
| 53 | |
| 54 // Returns a view containing |item| with insets defined by |top|, |left|, | |
| 55 // |bottom|, and |right|. | |
| 56 views::View* CreateViewWithInsets(views::View* item, | |
| 57 int top, | |
| 58 int left, | |
| 59 int bottom, | |
| 60 int right) { | |
| 61 views::View* view = new views::View(); | |
| 62 views::GridLayout* layout = new views::GridLayout(view); | |
| 63 view->SetLayoutManager(layout); | |
| 64 layout->SetInsets(top, left, bottom, right); | |
| 65 layout->AddColumnSet(0)->AddColumn( | |
| 66 views::GridLayout::LEADING, views::GridLayout::LEADING, | |
| 67 /*resize_percent=*/1, views::GridLayout::USE_PREF, | |
| 68 /*fixed_width=*/0, | |
| 69 /*min_width=*/0); | |
| 70 layout->StartRow(0, 0); | |
| 71 layout->AddView(item); | |
| 72 | |
| 73 return view; | |
| 74 } | |
| 75 | |
| 76 // Helper function used by |CreateLabelView()| below that adds |labels| to | |
| 77 // |label_view|. | |
| 78 void AddLabelsToLabelView(views::View* label_view, | |
| 79 views::GridLayout* layout, | |
| 80 const std::vector<LabelInfo>& labels) { | |
| 81 static constexpr base::char16 kBulletPoint[] = {0x2022, 0}; | |
| 82 | |
| 83 bool first_label = true; | |
| 84 bool last_label_was_bullet = false; | |
| 85 for (const LabelInfo& label_info : labels) { | |
| 86 const bool is_bullet = label_info.type == LabelInfo::BULLET_ITEM; | |
| 87 views::Label* label = new views::Label(label_info.text); | |
| 88 label->SetMultiLine(true); | |
| 89 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 90 | |
| 91 // Do not add a padding row if | |
| 92 // - this is the first label being added, or | |
| 93 // - a bullet item is being added and the last label was also a bullet item. | |
| 94 if (!(first_label || (is_bullet && last_label_was_bullet))) | |
|
ftirelo
2017/04/05 18:03:40
Please rewrite as:
!first_label && (!is_bullet |
alito
2017/04/05 19:23:49
After much consternation and discussion, we have a
| |
| 95 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 96 | |
| 97 layout->StartRow(0, is_bullet ? kBulletColumnSetId : kMainColumSetId); | |
| 98 if (is_bullet) { | |
| 99 views::Label* bullet = new views::Label(base::string16(kBulletPoint)); | |
| 100 layout->AddView(bullet); | |
| 101 } | |
| 102 layout->AddView(label); | |
| 103 | |
| 104 last_label_was_bullet = is_bullet; | |
| 105 first_label = false; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 // Creates a view that displays two types of labels: multiline labels | |
| 110 // representing whole paragraphs or indented labels that are start with a bullet | |
| 111 // point. | |
| 112 // | |
| 113 // A vertical padding of size |top_vertical_space| is added before the labels. | |
| 114 views::View* CreateLabelView(int top_vertical_space, | |
| 115 const std::vector<LabelInfo>& labels) { | |
| 116 views::View* label_view = new views::View(); | |
| 117 views::GridLayout* layout = new views::GridLayout(label_view); | |
| 118 layout->SetInsets(0, views::kButtonHEdgeMarginNew, 0, | |
| 119 views::kButtonHEdgeMarginNew); | |
| 120 | |
| 121 label_view->SetLayoutManager(layout); | |
| 122 | |
| 123 views::ColumnSet* main_column_set = layout->AddColumnSet(kMainColumSetId); | |
| 124 main_column_set->AddColumn(views::GridLayout::FILL, | |
| 125 views::GridLayout::LEADING, | |
| 126 /*resize_percent=*/1, views::GridLayout::USE_PREF, | |
| 127 /*fixed_width=*/0, | |
| 128 /*min_width=*/0); | |
| 129 | |
| 130 views::ColumnSet* bullet_column_set_ = | |
| 131 layout->AddColumnSet(kBulletColumnSetId); | |
| 132 bullet_column_set_->AddPaddingColumn( | |
| 133 /*resize_percent=*/0, views::kUnrelatedControlLargeHorizontalSpacing); | |
| 134 bullet_column_set_->AddColumn( | |
| 135 views::GridLayout::FILL, views::GridLayout::LEADING, | |
| 136 /*resize_percent=*/0, views::GridLayout::USE_PREF, | |
| 137 /*fixed_width=*/0, | |
| 138 /*min_width=*/0); | |
| 139 bullet_column_set_->AddPaddingColumn(/*resize_percent=*/0, | |
| 140 kBulletColumnWidth); | |
| 141 bullet_column_set_->AddColumn( | |
| 142 views::GridLayout::FILL, views::GridLayout::LEADING, | |
| 143 /*resize_percent=*/1, views::GridLayout::USE_PREF, | |
| 144 /*fixed_width=*/0, | |
| 145 /*min_width=*/0); | |
| 146 | |
| 147 if (top_vertical_space > 0) | |
| 148 layout->AddPaddingRow(/*vertical_resize=*/0, top_vertical_space); | |
| 149 | |
| 150 AddLabelsToLabelView(label_view, layout, labels); | |
| 151 | |
| 152 layout->AddPaddingRow(/*vertical_resize=*/0, | |
| 153 views::kUnrelatedControlLargeHorizontalSpacing); | |
| 154 return label_view; | |
| 155 } | |
| 156 | |
| 157 } // namespace | |
| 158 | |
| 159 //////////////////////////////////////////////////////////////////////////////// | |
| 160 // SRTPromptDialog::ExpandableMessageView | |
| 161 // | |
| 162 // A view, whose visibilty can be toggled, and will be used for the details | |
| 163 // section the main dialog. | |
| 164 class SRTPromptDialog::ExpandableMessageView : public views::View { | |
| 165 public: | |
| 166 explicit ExpandableMessageView(const std::vector<LabelInfo>& labels); | |
| 167 ~ExpandableMessageView() override; | |
| 168 | |
| 169 void ToggleShowView(); | |
| 170 | |
| 171 // views::View overrides. | |
| 172 gfx::Size GetPreferredSize() const override; | |
| 173 int GetHeightForWidth(int width) const override; | |
| 174 | |
| 175 private: | |
| 176 DISALLOW_COPY_AND_ASSIGN(ExpandableMessageView); | |
| 177 }; | |
| 178 | |
| 179 SRTPromptDialog::ExpandableMessageView::ExpandableMessageView( | |
| 180 const std::vector<LabelInfo>& labels) { | |
| 181 views::GridLayout* layout = new views::GridLayout(this); | |
| 182 SetLayoutManager(layout); | |
| 183 | |
| 184 SetVisible(false); | |
| 185 | |
| 186 constexpr int kColumnId = 0; | |
| 187 views::ColumnSet* column_set = layout->AddColumnSet(kColumnId); | |
| 188 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::LEADING, | |
| 189 /*resize_percent=*/1, views::GridLayout::USE_PREF, | |
| 190 /*fixed_width=*/0, | |
| 191 /*min_width=*/0); | |
| 192 | |
| 193 // Add the main message view inside a scroll view. | |
| 194 views::View* label_view = | |
| 195 CreateLabelView(views::kUnrelatedControlLargeHorizontalSpacing, labels); | |
| 196 views::ScrollView* scroll_view = new views::ScrollView(); | |
| 197 scroll_view->ClipHeightTo(/*min_height=*/0, kDetailsSectionMaxHeight); | |
| 198 scroll_view->SetContents(label_view); | |
| 199 layout->StartRow(0, kColumnId); | |
| 200 layout->AddView(scroll_view); | |
| 201 } | |
| 202 | |
| 203 SRTPromptDialog::ExpandableMessageView::~ExpandableMessageView() {} | |
| 204 | |
| 205 void SRTPromptDialog::ExpandableMessageView::ToggleShowView() { | |
| 206 SetVisible(!visible()); | |
| 207 PreferredSizeChanged(); | |
| 208 } | |
| 209 | |
| 210 gfx::Size SRTPromptDialog::ExpandableMessageView::GetPreferredSize() const { | |
| 211 gfx::Size size = views::View::GetPreferredSize(); | |
| 212 return gfx::Size(size.width(), visible() ? size.height() : 0); | |
| 213 } | |
| 214 | |
| 215 int SRTPromptDialog::ExpandableMessageView::GetHeightForWidth(int width) const { | |
| 216 return GetPreferredSize().height(); | |
| 217 } | |
| 218 | |
| 219 //////////////////////////////////////////////////////////////////////////////// | |
| 220 // SRTPromptDialog | |
| 221 | |
| 222 SRTPromptDialog::SRTPromptDialog(safe_browsing::SRTPromptController* controller) | |
| 223 : browser_(nullptr), | |
| 224 controller_(controller), | |
| 225 interaction_done_(false), | |
| 226 details_view_(nullptr), | |
| 227 expand_details_button_(nullptr), | |
| 228 expand_details_button_color_(GetNativeTheme()->GetSystemColor( | |
| 229 ui::NativeTheme::kColorId_LinkEnabled)), | |
| 230 expand_icon_( | |
| 231 gfx::CreateVectorIcon(kCaretDownIcon, expand_details_button_color_)), | |
| 232 fold_icon_( | |
| 233 gfx::CreateVectorIcon(kCaretUpIcon, expand_details_button_color_)) { | |
| 234 DCHECK(controller_); | |
| 235 | |
| 236 SetLayoutManager(new views::BoxLayout( | |
| 237 /*orientation=*/views::BoxLayout::kVertical, | |
| 238 /*inside_border_horizontal_spacing=*/0, | |
| 239 /*inside_border_vertical_spacing=*/views::kPanelVertMargin, | |
| 240 /*between_child_spacing=*/0)); | |
| 241 | |
| 242 AddChildView(CreateLabelView(0, controller_->GetMainText())); | |
| 243 AddChildView(new views::Separator()); | |
| 244 | |
| 245 details_view_ = new ExpandableMessageView(controller_->GetDetailsText()); | |
| 246 AddChildView(details_view_); | |
| 247 | |
| 248 expand_details_button_ = | |
| 249 new views::LabelButton(this, controller_->GetShowDetailsLabel()); | |
| 250 expand_details_button_->SetEnabledTextColors(expand_details_button_color_); | |
| 251 expand_details_button_->SetImage( | |
| 252 views::Button::STATE_NORMAL, | |
| 253 details_view_->visible() ? fold_icon_ : expand_icon_); | |
| 254 AddChildView(CreateViewWithInsets( | |
| 255 expand_details_button_, views::kPanelVertMargin, | |
| 256 views::kButtonHEdgeMarginNew, views::kPanelVertMargin, | |
| 257 views::kButtonHEdgeMarginNew)); | |
| 258 AddChildView(new views::Separator()); | |
| 259 } | |
| 260 | |
| 261 SRTPromptDialog::~SRTPromptDialog() { | |
| 262 // Make sure the controller is correctly notified in case the dialog widget is | |
| 263 // closed by some other means than the dialog buttons. | |
| 264 Close(); | |
| 265 } | |
| 266 | |
| 267 void SRTPromptDialog::Show(Browser* browser) { | |
| 268 DCHECK(browser); | |
| 269 | |
| 270 browser_ = browser; | |
| 271 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser_); | |
| 272 constrained_window::CreateBrowserModalDialogViews( | |
| 273 this, browser_view->GetNativeWindow()) | |
| 274 ->Show(); | |
| 275 controller_->DialogShown(); | |
| 276 } | |
| 277 | |
| 278 // DialogModel overrides. | |
| 279 | |
| 280 bool SRTPromptDialog::ShouldDefaultButtonBeBlue() const { | |
| 281 return true; | |
| 282 } | |
| 283 | |
| 284 // WidgetDelegate overrides. | |
| 285 | |
| 286 ui::ModalType SRTPromptDialog::GetModalType() const { | |
| 287 return ui::MODAL_TYPE_WINDOW; | |
| 288 } | |
| 289 | |
| 290 base::string16 SRTPromptDialog::GetWindowTitle() const { | |
| 291 return controller_->GetWindowTitle(); | |
| 292 } | |
| 293 | |
| 294 // DialogDelegate overrides. | |
| 295 | |
| 296 base::string16 SRTPromptDialog::GetDialogButtonLabel( | |
| 297 ui::DialogButton button) const { | |
| 298 DCHECK(button == ui::DIALOG_BUTTON_OK || button == ui::DIALOG_BUTTON_CANCEL); | |
| 299 | |
| 300 if (button == ui::DIALOG_BUTTON_OK) | |
| 301 return controller_->GetAcceptButtonLabel(); | |
| 302 return DialogDelegate::GetDialogButtonLabel(button); | |
| 303 } | |
| 304 | |
| 305 bool SRTPromptDialog::Accept() { | |
| 306 if (!interaction_done_) { | |
| 307 interaction_done_ = true; | |
| 308 controller_->Accept(); | |
| 309 } | |
| 310 return true; | |
| 311 } | |
| 312 | |
| 313 bool SRTPromptDialog::Cancel() { | |
| 314 if (!interaction_done_) { | |
| 315 interaction_done_ = true; | |
| 316 controller_->Cancel(); | |
| 317 } | |
| 318 return true; | |
| 319 } | |
| 320 | |
| 321 // View overrides. | |
| 322 | |
| 323 gfx::Size SRTPromptDialog::GetPreferredSize() const { | |
| 324 return gfx::Size(kDialogWidth, GetHeightForWidth(kDialogWidth)); | |
| 325 } | |
| 326 | |
| 327 // views::ButtonListener overrides. | |
| 328 | |
| 329 void SRTPromptDialog::ButtonPressed(views::Button* sender, | |
| 330 const ui::Event& event) { | |
| 331 DCHECK_EQ(sender, expand_details_button_); | |
| 332 DCHECK(browser_); | |
| 333 | |
| 334 details_view_->ToggleShowView(); | |
| 335 expand_details_button_->SetText(details_view_->visible() | |
| 336 ? controller_->GetHideDetailsLabel() | |
| 337 : controller_->GetShowDetailsLabel()); | |
| 338 expand_details_button_->SetImage( | |
| 339 views::Button::STATE_NORMAL, | |
| 340 details_view_->visible() ? fold_icon_ : expand_icon_); | |
| 341 | |
| 342 ChromeWebModalDialogManagerDelegate* manager = browser_; | |
| 343 constrained_window::UpdateWidgetModalDialogPosition( | |
| 344 GetWidget(), manager->GetWebContentsModalDialogHost()); | |
| 345 } | |
| OLD | NEW |