| OLD | NEW |
| (Empty) |
| 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 "chrome/browser/ui/views/try_chrome_dialog_view.h" | |
| 6 | |
| 7 #include <shellapi.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/run_loop.h" | |
| 13 #include "base/strings/string16.h" | |
| 14 #include "chrome/browser/process_singleton.h" | |
| 15 #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h" | |
| 16 #include "chrome/grit/chromium_strings.h" | |
| 17 #include "chrome/grit/generated_resources.h" | |
| 18 #include "chrome/grit/theme_resources.h" | |
| 19 #include "chrome/install_static/install_util.h" | |
| 20 #include "chrome/installer/util/user_experiment.h" | |
| 21 #include "components/strings/grit/components_strings.h" | |
| 22 #include "ui/aura/window.h" | |
| 23 #include "ui/aura/window_tree_host.h" | |
| 24 #include "ui/base/l10n/l10n_util.h" | |
| 25 #include "ui/base/resource/resource_bundle.h" | |
| 26 #include "ui/display/screen.h" | |
| 27 #include "ui/gfx/image/image.h" | |
| 28 #include "ui/resources/grit/ui_resources.h" | |
| 29 #include "ui/views/background.h" | |
| 30 #include "ui/views/controls/button/checkbox.h" | |
| 31 #include "ui/views/controls/button/image_button.h" | |
| 32 #include "ui/views/controls/button/md_text_button.h" | |
| 33 #include "ui/views/controls/button/radio_button.h" | |
| 34 #include "ui/views/controls/image_view.h" | |
| 35 #include "ui/views/controls/link.h" | |
| 36 #include "ui/views/controls/separator.h" | |
| 37 #include "ui/views/layout/grid_layout.h" | |
| 38 #include "ui/views/layout/layout_provider.h" | |
| 39 #include "ui/views/widget/widget.h" | |
| 40 | |
| 41 namespace { | |
| 42 | |
| 43 const wchar_t kHelpCenterUrl[] = | |
| 44 L"https://support.google.com/chrome/answer/150752"; | |
| 45 | |
| 46 enum ButtonTags { | |
| 47 BT_NONE, | |
| 48 BT_CLOSE_BUTTON, | |
| 49 BT_OK_BUTTON, | |
| 50 BT_TRY_IT_RADIO, | |
| 51 BT_DONT_BUG_RADIO | |
| 52 }; | |
| 53 | |
| 54 const int kRadioGroupID = 1; | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 // static | |
| 59 TryChromeDialogView::Result TryChromeDialogView::Show( | |
| 60 size_t flavor, | |
| 61 const ActiveModalDialogListener& listener) { | |
| 62 if (flavor > 10000) { | |
| 63 // This is a test value. We want to make sure we exercise | |
| 64 // returning this early. See TryChromeDialogBrowserTest test. | |
| 65 return NOT_NOW; | |
| 66 } | |
| 67 TryChromeDialogView dialog(flavor); | |
| 68 TryChromeDialogView::Result result = dialog.ShowDialog( | |
| 69 listener, kDialogType::MODAL, kUsageType::FOR_CHROME); | |
| 70 return result; | |
| 71 } | |
| 72 | |
| 73 TryChromeDialogView::TryChromeDialogView(size_t flavor) | |
| 74 : flavor_(flavor), | |
| 75 popup_(NULL), | |
| 76 try_chrome_(NULL), | |
| 77 kill_chrome_(NULL), | |
| 78 dont_try_chrome_(NULL), | |
| 79 make_default_(NULL), | |
| 80 result_(COUNT) {} | |
| 81 | |
| 82 TryChromeDialogView::~TryChromeDialogView() {} | |
| 83 | |
| 84 TryChromeDialogView::Result TryChromeDialogView::ShowDialog( | |
| 85 const ActiveModalDialogListener& listener, | |
| 86 kDialogType dialog_type, | |
| 87 kUsageType usage_type) { | |
| 88 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 89 | |
| 90 views::ImageView* icon = new views::ImageView(); | |
| 91 icon->SetImage(rb.GetNativeImageNamed(IDR_PRODUCT_LOGO_32).ToImageSkia()); | |
| 92 gfx::Size icon_size = icon->GetPreferredSize(); | |
| 93 | |
| 94 // An approximate window size. After Layout() we'll get better bounds. | |
| 95 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); | |
| 96 params.activatable = views::Widget::InitParams::ACTIVATABLE_YES; | |
| 97 params.bounds = gfx::Rect(310, 200); | |
| 98 popup_ = new views::Widget; | |
| 99 popup_->Init(params); | |
| 100 | |
| 101 views::View* root_view = popup_->GetRootView(); | |
| 102 // The window color is a tiny bit off-white. | |
| 103 root_view->set_background( | |
| 104 views::Background::CreateSolidBackground(0xfc, 0xfc, 0xfc)); | |
| 105 | |
| 106 views::GridLayout* layout = views::GridLayout::CreatePanel(root_view); | |
| 107 views::ColumnSet* columns; | |
| 108 | |
| 109 ChromeLayoutProvider* provider = ChromeLayoutProvider::Get(); | |
| 110 const int label_spacing = | |
| 111 provider->GetDistanceMetric(DISTANCE_RELATED_LABEL_HORIZONTAL); | |
| 112 const int unrelated_space_horiz = | |
| 113 provider->GetDistanceMetric(DISTANCE_UNRELATED_CONTROL_HORIZONTAL); | |
| 114 const int unrelated_space_vert = | |
| 115 provider->GetDistanceMetric(DISTANCE_UNRELATED_CONTROL_VERTICAL); | |
| 116 const int button_spacing_horiz = | |
| 117 provider->GetDistanceMetric(views::DISTANCE_RELATED_BUTTON_HORIZONTAL); | |
| 118 | |
| 119 // First row: [icon][pad][text][pad][button]. | |
| 120 columns = layout->AddColumnSet(0); | |
| 121 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::LEADING, 0, | |
| 122 views::GridLayout::FIXED, icon_size.width(), | |
| 123 icon_size.height()); | |
| 124 columns->AddPaddingColumn(0, label_spacing); | |
| 125 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, | |
| 126 views::GridLayout::USE_PREF, 0, 0); | |
| 127 columns->AddPaddingColumn(0, unrelated_space_horiz); | |
| 128 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL, 1, | |
| 129 views::GridLayout::USE_PREF, 0, 0); | |
| 130 | |
| 131 int icon_padding = icon_size.width() + label_spacing; | |
| 132 // Optional second row: [pad][radio 1]. | |
| 133 columns = layout->AddColumnSet(1); | |
| 134 columns->AddPaddingColumn(0, icon_padding); | |
| 135 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, | |
| 136 views::GridLayout::USE_PREF, 0, 0); | |
| 137 | |
| 138 // Third row: [pad][radio 2]. | |
| 139 columns = layout->AddColumnSet(2); | |
| 140 columns->AddPaddingColumn(0, icon_padding); | |
| 141 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, | |
| 142 views::GridLayout::USE_PREF, 0, 0); | |
| 143 | |
| 144 // Fourth row: [pad][button][pad][button]. | |
| 145 columns = layout->AddColumnSet(3); | |
| 146 columns->AddPaddingColumn(0, icon_padding); | |
| 147 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 0, | |
| 148 views::GridLayout::USE_PREF, 0, 0); | |
| 149 columns->AddPaddingColumn(0, button_spacing_horiz); | |
| 150 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 0, | |
| 151 views::GridLayout::USE_PREF, 0, 0); | |
| 152 | |
| 153 // Fifth row: [pad][link]. | |
| 154 columns = layout->AddColumnSet(4); | |
| 155 columns->AddPaddingColumn(0, icon_padding); | |
| 156 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, | |
| 157 views::GridLayout::USE_PREF, 0, 0); | |
| 158 | |
| 159 // Optional fourth row: [button]. | |
| 160 columns = layout->AddColumnSet(5); | |
| 161 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::FILL, 1, | |
| 162 views::GridLayout::USE_PREF, 0, 0); | |
| 163 | |
| 164 // Optional fourth row: [divider] | |
| 165 columns = layout->AddColumnSet(6); | |
| 166 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::FILL, 1, | |
| 167 views::GridLayout::USE_PREF, 0, 0); | |
| 168 | |
| 169 // Optional fifth row [checkbox][pad][button] | |
| 170 columns = layout->AddColumnSet(7); | |
| 171 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 0, | |
| 172 views::GridLayout::USE_PREF, 0, 0); | |
| 173 columns->AddPaddingColumn(0, unrelated_space_horiz); | |
| 174 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL, 1, | |
| 175 views::GridLayout::USE_PREF, 0, 0); | |
| 176 | |
| 177 // First row. | |
| 178 layout->StartRow(0, 0); | |
| 179 layout->AddView(icon); | |
| 180 | |
| 181 // Find out what experiment we are conducting. | |
| 182 installer::ExperimentDetails experiment; | |
| 183 if ((usage_type != kUsageType::FOR_TESTING && | |
| 184 !install_static::SupportsRetentionExperiments()) || | |
| 185 !installer::CreateExperimentDetails(flavor_, &experiment) || | |
| 186 !experiment.heading) { | |
| 187 NOTREACHED() << "Cannot determine which headline to show."; | |
| 188 return DIALOG_ERROR; | |
| 189 } | |
| 190 views::Label* label = | |
| 191 new views::Label(l10n_util::GetStringUTF16(experiment.heading), | |
| 192 views::style::CONTEXT_DIALOG_TITLE); | |
| 193 label->SetMultiLine(true); | |
| 194 label->SizeToFit(200); | |
| 195 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 196 layout->AddView(label); | |
| 197 // The close button is custom. | |
| 198 views::ImageButton* close_button = new views::ImageButton(this); | |
| 199 close_button->SetImage(views::CustomButton::STATE_NORMAL, | |
| 200 rb.GetNativeImageNamed(IDR_CLOSE_2).ToImageSkia()); | |
| 201 close_button->SetImage(views::CustomButton::STATE_HOVERED, | |
| 202 rb.GetNativeImageNamed(IDR_CLOSE_2_H).ToImageSkia()); | |
| 203 close_button->SetImage(views::CustomButton::STATE_PRESSED, | |
| 204 rb.GetNativeImageNamed(IDR_CLOSE_2_P).ToImageSkia()); | |
| 205 close_button->set_tag(BT_CLOSE_BUTTON); | |
| 206 layout->AddView(close_button); | |
| 207 | |
| 208 // Second row. | |
| 209 layout->StartRowWithPadding(0, 1, 0, 10); | |
| 210 try_chrome_ = new views::RadioButton( | |
| 211 l10n_util::GetStringUTF16(IDS_TRY_TOAST_TRY_OPT), kRadioGroupID); | |
| 212 try_chrome_->SetChecked(true); | |
| 213 try_chrome_->set_tag(BT_TRY_IT_RADIO); | |
| 214 try_chrome_->set_listener(this); | |
| 215 layout->AddView(try_chrome_); | |
| 216 | |
| 217 // Decide if the don't bug me is a button or a radio button. | |
| 218 bool dont_bug_me_button = | |
| 219 !!(experiment.flags & installer::kToastUiDontBugMeAsButton); | |
| 220 | |
| 221 // Optional third and fourth row. | |
| 222 if (!dont_bug_me_button) { | |
| 223 layout->StartRow(0, 1); | |
| 224 dont_try_chrome_ = new views::RadioButton( | |
| 225 l10n_util::GetStringUTF16(IDS_TRY_TOAST_CANCEL), kRadioGroupID); | |
| 226 dont_try_chrome_->set_tag(BT_DONT_BUG_RADIO); | |
| 227 dont_try_chrome_->set_listener(this); | |
| 228 layout->AddView(dont_try_chrome_); | |
| 229 } | |
| 230 if (experiment.flags & installer::kToastUiUninstall) { | |
| 231 layout->StartRow(0, 2); | |
| 232 kill_chrome_ = new views::RadioButton( | |
| 233 l10n_util::GetStringUTF16(IDS_UNINSTALL_CHROME), kRadioGroupID); | |
| 234 layout->AddView(kill_chrome_); | |
| 235 } | |
| 236 | |
| 237 views::LabelButton* accept_button = | |
| 238 views::MdTextButton::CreateSecondaryUiButton( | |
| 239 this, l10n_util::GetStringUTF16(IDS_OK)); | |
| 240 accept_button->set_tag(BT_OK_BUTTON); | |
| 241 | |
| 242 views::Separator* separator = NULL; | |
| 243 if (experiment.flags & installer::kToastUiMakeDefault) { | |
| 244 // In this flavor we have some vertical space, then a separator line | |
| 245 // and the 'make default' checkbox and the OK button on the same row. | |
| 246 layout->AddPaddingRow(0, unrelated_space_vert); | |
| 247 layout->StartRow(0, 6); | |
| 248 separator = new views::Separator(); | |
| 249 layout->AddView(separator); | |
| 250 layout->AddPaddingRow(0, unrelated_space_vert); | |
| 251 | |
| 252 layout->StartRow(0, 7); | |
| 253 make_default_ = new views::Checkbox( | |
| 254 l10n_util::GetStringUTF16(IDS_TRY_TOAST_SET_DEFAULT)); | |
| 255 make_default_->SetChecked(true); | |
| 256 layout->AddView(make_default_); | |
| 257 layout->AddView(accept_button); | |
| 258 } else { | |
| 259 // On this other flavor there is no checkbox, the OK button and possibly | |
| 260 // the cancel button are in the same row. | |
| 261 layout->StartRowWithPadding(0, dont_bug_me_button ? 3 : 5, 0, 10); | |
| 262 layout->AddView(accept_button); | |
| 263 if (dont_bug_me_button) { | |
| 264 // The dialog needs a "Don't bug me" as a button or as a radio button, | |
| 265 // this the button case. | |
| 266 views::LabelButton* cancel_button = | |
| 267 views::MdTextButton::CreateSecondaryUiButton( | |
| 268 this, l10n_util::GetStringUTF16(IDS_TRY_TOAST_CANCEL)); | |
| 269 cancel_button->set_tag(BT_CLOSE_BUTTON); | |
| 270 layout->AddView(cancel_button); | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 if (experiment.flags & installer::kToastUiWhyLink) { | |
| 275 layout->StartRowWithPadding(0, 4, 0, 10); | |
| 276 views::Link* link = | |
| 277 new views::Link(l10n_util::GetStringUTF16(IDS_TRY_TOAST_WHY)); | |
| 278 link->set_listener(this); | |
| 279 layout->AddView(link); | |
| 280 } | |
| 281 | |
| 282 // We resize the window according to the layout manager. This takes into | |
| 283 // account the differences between XP and Vista fonts and buttons. | |
| 284 layout->Layout(root_view); | |
| 285 gfx::Size preferred = layout->GetPreferredSize(root_view); | |
| 286 if (separator) { | |
| 287 int separator_height = separator->GetPreferredSize().height(); | |
| 288 separator->SetSize(gfx::Size(preferred.width(), separator_height)); | |
| 289 } | |
| 290 | |
| 291 gfx::Rect pos = ComputeWindowPosition(preferred, base::i18n::IsRTL()); | |
| 292 popup_->SetBounds(pos); | |
| 293 | |
| 294 // Carve the toast shape into the window. | |
| 295 HWND toast_window; | |
| 296 toast_window = popup_->GetNativeView()->GetHost()->GetAcceleratedWidget(); | |
| 297 SetToastRegion(toast_window, preferred.width(), preferred.height()); | |
| 298 | |
| 299 // Time to show the window in a modal loop. | |
| 300 popup_->Show(); | |
| 301 | |
| 302 if (!listener.is_null()) | |
| 303 listener.Run(popup_->GetNativeView()); | |
| 304 | |
| 305 // If the dialog is not modal, we don't control when it is going to be | |
| 306 // dismissed and hence we cannot inform the listener about the dialog going | |
| 307 // away. | |
| 308 if (dialog_type == kDialogType::MODAL) { | |
| 309 base::RunLoop().Run(); | |
| 310 if (!listener.is_null()) | |
| 311 listener.Run(nullptr); | |
| 312 } | |
| 313 return result_; | |
| 314 } | |
| 315 | |
| 316 gfx::Rect TryChromeDialogView::ComputeWindowPosition(const gfx::Size& size, | |
| 317 bool is_RTL) { | |
| 318 gfx::Point origin; | |
| 319 | |
| 320 gfx::Rect work_area = popup_->GetWorkAreaBoundsInScreen(); | |
| 321 origin.set_x(is_RTL ? work_area.x() : work_area.right() - size.width()); | |
| 322 origin.set_y(work_area.bottom()- size.height()); | |
| 323 | |
| 324 return display::Screen::GetScreen()->ScreenToDIPRectInWindow( | |
| 325 popup_->GetNativeView(), gfx::Rect(origin, size)); | |
| 326 } | |
| 327 | |
| 328 void TryChromeDialogView::SetToastRegion(HWND window, int w, int h) { | |
| 329 static const POINT polygon[] = { | |
| 330 {0, 4}, {1, 2}, {2, 1}, {4, 0}, // Left side. | |
| 331 {w - 4, 0}, {w - 2, 1}, {w - 1, 2}, {w, 4}, // Right side. | |
| 332 {w, h}, {0, h}}; | |
| 333 HRGN region = ::CreatePolygonRgn(polygon, arraysize(polygon), WINDING); | |
| 334 ::SetWindowRgn(window, region, FALSE); | |
| 335 } | |
| 336 | |
| 337 void TryChromeDialogView::ButtonPressed(views::Button* sender, | |
| 338 const ui::Event& event) { | |
| 339 if (sender->tag() == BT_DONT_BUG_RADIO) { | |
| 340 if (make_default_) { | |
| 341 make_default_->SetChecked(false); | |
| 342 make_default_->SetVisible(false); | |
| 343 } | |
| 344 return; | |
| 345 } else if (sender->tag() == BT_TRY_IT_RADIO) { | |
| 346 if (make_default_) { | |
| 347 make_default_->SetVisible(true); | |
| 348 make_default_->SetChecked(true); | |
| 349 } | |
| 350 return; | |
| 351 } else if (sender->tag() == BT_CLOSE_BUTTON) { | |
| 352 // The user pressed cancel or the [x] button. | |
| 353 result_ = NOT_NOW; | |
| 354 } else if (!try_chrome_) { | |
| 355 // We don't have radio buttons, the user pressed ok. | |
| 356 result_ = TRY_CHROME; | |
| 357 } else { | |
| 358 // The outcome is according to the selected radio button. | |
| 359 if (try_chrome_->checked()) | |
| 360 result_ = TRY_CHROME; | |
| 361 else if (dont_try_chrome_ && dont_try_chrome_->checked()) | |
| 362 result_ = NOT_NOW; | |
| 363 else if (kill_chrome_ && kill_chrome_->checked()) | |
| 364 result_ = UNINSTALL_CHROME; | |
| 365 else | |
| 366 NOTREACHED() << "Unknown radio button selected"; | |
| 367 } | |
| 368 | |
| 369 if (make_default_) { | |
| 370 if ((result_ == TRY_CHROME) && make_default_->checked()) | |
| 371 result_ = TRY_CHROME_AS_DEFAULT; | |
| 372 } | |
| 373 | |
| 374 popup_->Close(); | |
| 375 base::MessageLoop::current()->QuitWhenIdle(); | |
| 376 } | |
| 377 | |
| 378 void TryChromeDialogView::LinkClicked(views::Link* source, int event_flags) { | |
| 379 ::ShellExecuteW(NULL, L"open", kHelpCenterUrl, NULL, NULL, SW_SHOW); | |
| 380 } | |
| OLD | NEW |