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/settings_reset_prompt_dialog.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom
pt_controller.h" |
| 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h" |
| 12 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 13 #include "chrome/browser/ui/views/toolbar/app_menu_button.h" |
| 14 #include "chrome/browser/ui/views/toolbar/toolbar_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/gfx/color_palette.h" |
| 18 #include "ui/gfx/image/image.h" |
| 19 #include "ui/gfx/paint_vector_icon.h" |
| 20 #include "ui/gfx/text_constants.h" |
| 21 #include "ui/gfx/vector_icons_public.h" |
| 22 #include "ui/views/bubble/bubble_border.h" |
| 23 #include "ui/views/controls/label.h" |
| 24 #include "ui/views/controls/link.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/view.h" |
| 31 #include "ui/views/widget/widget.h" |
| 32 |
| 33 namespace safe_browsing { |
| 34 |
| 35 // static |
| 36 void SettingsResetPromptController::ShowSettingsResetPrompt( |
| 37 Browser* browser, |
| 38 SettingsResetPromptController* controller) { |
| 39 SettingsResetPromptDialog* dialog = new SettingsResetPromptDialog(controller); |
| 40 // The dialog will delete itself, as implemented in |
| 41 // |DialogDelegateView::DeleteDelegate()|, when its widget is closed. |
| 42 dialog->Show(browser); |
| 43 } |
| 44 |
| 45 } // namespace safe_browsing |
| 46 |
| 47 namespace { |
| 48 |
| 49 using LabelInfo = safe_browsing::SettingsResetPromptController::LabelInfo; |
| 50 |
| 51 constexpr int kDialogWidth = 512; |
| 52 constexpr int kDetailsSectionMaxHeight = 150; |
| 53 constexpr int kBulletColumnWidth = 5; |
| 54 // Constants used for the layout of the label views. |
| 55 static constexpr int kMainColumSetId = 0; |
| 56 static constexpr int kBulletColumnSetId = 1; |
| 57 |
| 58 // Helper function used by |CreateLabelView()| below that adds the labels as |
| 59 // specified by |labels| to |label_view|. |
| 60 void AddLabelsToLabelView(views::View* label_view, |
| 61 views::GridLayout* layout, |
| 62 const std::vector<LabelInfo>& labels) { |
| 63 static const base::char16 kBulletPoint[] = {0x2022, 0}; |
| 64 |
| 65 bool first_label = true; |
| 66 bool last_label_was_bullet = false; |
| 67 for (const LabelInfo& label_info : labels) { |
| 68 const bool is_bullet = label_info.type == LabelInfo::BULLET_ITEM; |
| 69 views::Label* label = new views::Label(label_info.text); |
| 70 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 71 if (is_bullet) { |
| 72 label->SetElideBehavior(gfx::ELIDE_TAIL); |
| 73 } else { |
| 74 label->SetMultiLine(true); |
| 75 } |
| 76 |
| 77 // Do not add a padding row if |
| 78 // - this is the first label being added, or |
| 79 // - a bullet item is being added and the last label was also a bullet item. |
| 80 if (!(first_label || (is_bullet && last_label_was_bullet))) |
| 81 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| 82 |
| 83 layout->StartRow(0, is_bullet ? kBulletColumnSetId : kMainColumSetId); |
| 84 if (is_bullet) { |
| 85 views::Label* bullet = new views::Label(base::string16(kBulletPoint)); |
| 86 layout->AddView(bullet); |
| 87 } |
| 88 layout->AddView(label); |
| 89 |
| 90 last_label_was_bullet = is_bullet; |
| 91 first_label = false; |
| 92 } |
| 93 } |
| 94 |
| 95 // Creates a view that displays two types of labels: multiline labels or |
| 96 // indented single-line labels that are indented and start with a bullet |
| 97 // point. The indented text is intended to be used to display setting values |
| 98 // (URLs) and is elided if the text is too wide to fit in its entirety. |
| 99 views::View* CreateLabelView(int top_vertical_spacing, |
| 100 const std::vector<LabelInfo>& labels) { |
| 101 views::View* label_view = new views::View(); |
| 102 views::GridLayout* layout = new views::GridLayout(label_view); |
| 103 label_view->SetLayoutManager(layout); |
| 104 |
| 105 views::ColumnSet* main_column_set = layout->AddColumnSet(kMainColumSetId); |
| 106 main_column_set->AddColumn(views::GridLayout::FILL, |
| 107 views::GridLayout::LEADING, |
| 108 /*resize_percent=*/1, views::GridLayout::USE_PREF, |
| 109 /*fixed_width=*/0, |
| 110 /*min_width=*/0); |
| 111 |
| 112 views::ColumnSet* bullet_column_set_ = |
| 113 layout->AddColumnSet(kBulletColumnSetId); |
| 114 bullet_column_set_->AddPaddingColumn( |
| 115 /*resize_percent=*/0, views::kUnrelatedControlLargeHorizontalSpacing); |
| 116 bullet_column_set_->AddColumn( |
| 117 views::GridLayout::FILL, views::GridLayout::LEADING, |
| 118 /*resize_percent=*/0, views::GridLayout::USE_PREF, |
| 119 /*fixed_width=*/0, |
| 120 /*min_width=*/0); |
| 121 bullet_column_set_->AddPaddingColumn(/*resize_percent=*/0, |
| 122 kBulletColumnWidth); |
| 123 bullet_column_set_->AddColumn( |
| 124 views::GridLayout::FILL, views::GridLayout::LEADING, |
| 125 /*resize_percent=*/1, views::GridLayout::USE_PREF, |
| 126 /*fixed_width=*/0, |
| 127 /*min_width=*/0); |
| 128 |
| 129 if (top_vertical_spacing > 0) |
| 130 layout->AddPaddingRow(/*vertical_resize=*/0, top_vertical_spacing); |
| 131 |
| 132 AddLabelsToLabelView(label_view, layout, labels); |
| 133 return label_view; |
| 134 } |
| 135 |
| 136 } // namespace |
| 137 |
| 138 //////////////////////////////////////////////////////////////////////////////// |
| 139 // SettingsResetPromptDialog::ExpandableMessageView |
| 140 // |
| 141 // A view, whose visibilty can be toggled, and will be used for the details |
| 142 // section the main dialog. |
| 143 class SettingsResetPromptDialog::ExpandableMessageView : public views::View { |
| 144 public: |
| 145 explicit ExpandableMessageView(const std::vector<LabelInfo>& labels); |
| 146 ~ExpandableMessageView() override; |
| 147 |
| 148 void ToggleShowView(); |
| 149 |
| 150 // views::View overrides. |
| 151 gfx::Size GetPreferredSize() const override; |
| 152 int GetHeightForWidth(int width) const override; |
| 153 |
| 154 private: |
| 155 DISALLOW_COPY_AND_ASSIGN(ExpandableMessageView); |
| 156 }; |
| 157 |
| 158 SettingsResetPromptDialog::ExpandableMessageView::ExpandableMessageView( |
| 159 const std::vector<LabelInfo>& labels) { |
| 160 views::GridLayout* layout = new views::GridLayout(this); |
| 161 SetLayoutManager(layout); |
| 162 |
| 163 SetVisible(false); |
| 164 |
| 165 constexpr int kColumnId = 0; |
| 166 views::ColumnSet* column_set = layout->AddColumnSet(kColumnId); |
| 167 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::LEADING, |
| 168 /*resize_percent=*/1, views::GridLayout::USE_PREF, |
| 169 /*fixed_width=*/0, |
| 170 /*min_width=*/0); |
| 171 |
| 172 // Add a horizontal line separator. |
| 173 layout->StartRowWithPadding( |
| 174 /*vertical_resize=*/0, kColumnId, |
| 175 /*padding_resize=*/0, views::kUnrelatedControlVerticalSpacing); |
| 176 layout->AddView(new views::Separator()); |
| 177 |
| 178 // Add the main message view inside a scroll view. |
| 179 views::View* label_view = |
| 180 CreateLabelView(views::kUnrelatedControlVerticalSpacing, labels); |
| 181 views::ScrollView* scroll_view = new views::ScrollView(); |
| 182 scroll_view->ClipHeightTo(/*min_height=*/0, kDetailsSectionMaxHeight); |
| 183 scroll_view->SetContents(label_view); |
| 184 layout->StartRow(0, kColumnId); |
| 185 layout->AddView(scroll_view); |
| 186 } |
| 187 |
| 188 SettingsResetPromptDialog::ExpandableMessageView::~ExpandableMessageView() {} |
| 189 |
| 190 void SettingsResetPromptDialog::ExpandableMessageView::ToggleShowView() { |
| 191 SetVisible(!visible()); |
| 192 PreferredSizeChanged(); |
| 193 } |
| 194 |
| 195 gfx::Size SettingsResetPromptDialog::ExpandableMessageView::GetPreferredSize() |
| 196 const { |
| 197 gfx::Size size = views::View::GetPreferredSize(); |
| 198 return gfx::Size(size.width(), visible() ? size.height() : 0); |
| 199 } |
| 200 |
| 201 int SettingsResetPromptDialog::ExpandableMessageView::GetHeightForWidth( |
| 202 int width) const { |
| 203 return GetPreferredSize().height(); |
| 204 } |
| 205 |
| 206 //////////////////////////////////////////////////////////////////////////////// |
| 207 // SettingsResetPromptDialog |
| 208 |
| 209 SettingsResetPromptDialog::SettingsResetPromptDialog( |
| 210 safe_browsing::SettingsResetPromptController* controller) |
| 211 : browser_(nullptr), |
| 212 controller_(controller), |
| 213 interaction_done_(false), |
| 214 details_link_(nullptr) { |
| 215 DCHECK(controller_); |
| 216 |
| 217 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, |
| 218 views::kButtonHEdgeMarginNew, |
| 219 views::kPanelVertMargin, 0)); |
| 220 |
| 221 // Add the dialog's main label view. |
| 222 AddChildView(CreateLabelView(0, controller_->GetMainText())); |
| 223 |
| 224 // Add the main details view that starts off not being visible. |
| 225 details_view_ = new ExpandableMessageView(controller_->GetDetailsText()); |
| 226 AddChildView(details_view_); |
| 227 } |
| 228 |
| 229 SettingsResetPromptDialog::~SettingsResetPromptDialog() { |
| 230 // Make sure the controller is correctly notified in case the dialog widget is |
| 231 // closed by some other means than the dialog buttons. |
| 232 Close(); |
| 233 } |
| 234 |
| 235 void SettingsResetPromptDialog::Show(Browser* browser) { |
| 236 DCHECK(browser); |
| 237 browser_ = browser; |
| 238 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); |
| 239 constrained_window::CreateBrowserModalDialogViews( |
| 240 this, browser_view->GetNativeWindow()) |
| 241 ->Show(); |
| 242 } |
| 243 |
| 244 // WidgetDelegate overrides. |
| 245 |
| 246 ui::ModalType SettingsResetPromptDialog::GetModalType() const { |
| 247 return ui::MODAL_TYPE_WINDOW; |
| 248 } |
| 249 |
| 250 bool SettingsResetPromptDialog::ShouldShowWindowIcon() const { |
| 251 return true; |
| 252 } |
| 253 |
| 254 gfx::ImageSkia SettingsResetPromptDialog::GetWindowIcon() { |
| 255 return gfx::CreateVectorIcon(gfx::VectorIconId::WARNING, 32 /* Icon size. */, |
| 256 gfx::kGoogleRed700); |
| 257 } |
| 258 |
| 259 base::string16 SettingsResetPromptDialog::GetWindowTitle() const { |
| 260 return controller_->GetWindowTitle(); |
| 261 } |
| 262 |
| 263 // DialogModel overrides. |
| 264 |
| 265 bool SettingsResetPromptDialog::ShouldDefaultButtonBeBlue() const { |
| 266 return true; |
| 267 } |
| 268 |
| 269 // DialogDelegate overrides. |
| 270 |
| 271 base::string16 SettingsResetPromptDialog::GetDialogButtonLabel( |
| 272 ui::DialogButton button) const { |
| 273 DCHECK(button == ui::DIALOG_BUTTON_OK || button == ui::DIALOG_BUTTON_CANCEL); |
| 274 |
| 275 if (button == ui::DIALOG_BUTTON_OK) |
| 276 return controller_->GetButtonLabel(); |
| 277 return DialogDelegate::GetDialogButtonLabel(button); |
| 278 } |
| 279 |
| 280 views::View* SettingsResetPromptDialog::CreateExtraView() { |
| 281 // Add a "show details" link that toggles visibility of the details view. |
| 282 details_link_ = new views::Link(controller_->GetShowDetailsLabel()); |
| 283 details_link_->SetUnderline(false); |
| 284 details_link_->set_listener(this); |
| 285 return details_link_; |
| 286 } |
| 287 |
| 288 bool SettingsResetPromptDialog::Accept() { |
| 289 if (!interaction_done_) { |
| 290 interaction_done_ = true; |
| 291 controller_->Accept(); |
| 292 } |
| 293 return true; |
| 294 } |
| 295 |
| 296 bool SettingsResetPromptDialog::Cancel() { |
| 297 if (!interaction_done_) { |
| 298 interaction_done_ = true; |
| 299 controller_->Cancel(); |
| 300 } |
| 301 return true; |
| 302 } |
| 303 |
| 304 // View overrides. |
| 305 |
| 306 gfx::Size SettingsResetPromptDialog::GetPreferredSize() const { |
| 307 return gfx::Size(kDialogWidth, GetHeightForWidth(kDialogWidth)); |
| 308 } |
| 309 |
| 310 // LinkListener overrides. |
| 311 |
| 312 void SettingsResetPromptDialog::LinkClicked(views::Link* source, |
| 313 int event_flags) { |
| 314 DCHECK_EQ(source, details_link_); |
| 315 DCHECK(browser_); |
| 316 |
| 317 details_view_->ToggleShowView(); |
| 318 details_link_->SetText(details_view_->visible() |
| 319 ? controller_->GetHideDetailsLabel() |
| 320 : controller_->GetShowDetailsLabel()); |
| 321 |
| 322 ChromeWebModalDialogManagerDelegate* manager = browser_; |
| 323 constrained_window::UpdateWidgetModalDialogPosition( |
| 324 GetWidget(), manager->GetWebContentsModalDialogHost()); |
| 325 } |
OLD | NEW |