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