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/first_run_search_engine_view.h" | |
6 | |
7 #include <algorithm> | |
8 #include <map> | |
9 #include <vector> | |
10 | |
11 #include "base/i18n/rtl.h" | |
12 #include "base/rand_util.h" | |
13 #include "base/time.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "chrome/browser/first_run/first_run.h" | |
16 #include "chrome/browser/first_run/first_run_dialog.h" | |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/browser/search_engines/search_engine_type.h" | |
19 #include "chrome/browser/search_engines/template_url.h" | |
20 #include "chrome/browser/search_engines/template_url_service.h" | |
21 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
22 #include "chrome/browser/themes/theme_service.h" | |
23 #include "grit/chromium_strings.h" | |
24 #include "grit/generated_resources.h" | |
25 #include "grit/google_chrome_strings.h" | |
26 #include "grit/locale_settings.h" | |
27 #include "grit/theme_resources.h" | |
28 #include "ui/base/accessibility/accessible_view_state.h" | |
29 #include "ui/base/l10n/l10n_util.h" | |
30 #include "ui/base/resource/resource_bundle.h" | |
31 #include "ui/gfx/canvas.h" | |
32 #include "ui/gfx/font.h" | |
33 #include "ui/views/controls/button/button.h" | |
34 #include "ui/views/controls/image_view.h" | |
35 #include "ui/views/controls/label.h" | |
36 #include "ui/views/focus/accelerator_handler.h" | |
37 #include "ui/views/layout/layout_constants.h" | |
38 #include "ui/views/view_text_utils.h" | |
39 #include "ui/views/widget/widget.h" | |
40 | |
41 namespace { | |
42 | |
43 // Size to scale logos down to if showing 4 instead of 3 choices. Logo images | |
44 // are all originally sized at 180 x 120 pixels, with the logo text baseline | |
45 // located 74 pixels beneath the top of the image. | |
46 const int kSmallLogoWidth = 132; | |
47 const int kSmallLogoHeight = 88; | |
48 | |
49 // Used to pad text label height so it fits nicely in view. | |
50 const int kLabelPadding = 25; | |
51 | |
52 } // namespace | |
53 | |
54 namespace first_run { | |
55 | |
56 void ShowFirstRunDialog(Profile* profile, | |
57 bool randomize_search_engine_experiment) { | |
58 // If the default search is managed via policy, we don't ask the user to | |
59 // choose. | |
60 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile(profile); | |
61 if (!first_run::ShouldShowSearchEngineSelector(model)) | |
62 return; | |
63 | |
64 views::Widget* window = views::Widget::CreateWindow( | |
65 new FirstRunSearchEngineView( | |
66 profile, randomize_search_engine_experiment)); | |
67 window->SetAlwaysOnTop(true); | |
68 window->Show(); | |
69 views::AcceleratorHandler accelerator_handler; | |
70 MessageLoopForUI::current()->RunWithDispatcher(&accelerator_handler); | |
71 } | |
72 | |
73 } // namespace first_run | |
74 | |
75 SearchEngineChoice::SearchEngineChoice(views::ButtonListener* listener, | |
76 const TemplateURL* search_engine, | |
77 bool use_small_logos) | |
78 : NativeTextButton( | |
79 listener, | |
80 UTF16ToWide(l10n_util::GetStringUTF16(IDS_FR_SEARCH_CHOOSE))), | |
81 is_image_label_(false), | |
82 search_engine_(search_engine), | |
83 slot_(0) { | |
84 bool use_images = false; | |
85 #if defined(GOOGLE_CHROME_BUILD) | |
86 use_images = true; | |
87 #endif | |
88 int logo_id = search_engine_->logo_id(); | |
89 if (use_images && logo_id != kNoSearchEngineLogo) { | |
90 is_image_label_ = true; | |
91 views::ImageView* logo_image = new views::ImageView(); | |
92 SkBitmap* logo_bmp = | |
93 ResourceBundle::GetSharedInstance().GetBitmapNamed(logo_id); | |
94 logo_image->SetImage(logo_bmp); | |
95 if (use_small_logos) | |
96 logo_image->SetImageSize(gfx::Size(kSmallLogoWidth, kSmallLogoHeight)); | |
97 // Tooltip text provides accessibility for low-vision users. | |
98 logo_image->SetTooltipText(search_engine_->short_name()); | |
99 choice_view_ = logo_image; | |
100 } else { | |
101 // No logo -- we must show a text label. | |
102 views::Label* logo_label = new views::Label(search_engine_->short_name()); | |
103 logo_label->SetBackgroundColor(SK_ColorWHITE); | |
104 logo_label->SetEnabledColor(SK_ColorDKGRAY); | |
105 logo_label->SetFont(logo_label->font().DeriveFont(3, gfx::Font::BOLD)); | |
106 logo_label->SetHorizontalAlignment(views::Label::ALIGN_CENTER); | |
107 logo_label->SetTooltipText(search_engine_->short_name()); | |
108 logo_label->SetMultiLine(true); | |
109 logo_label->SizeToFit(kSmallLogoWidth); | |
110 choice_view_ = logo_label; | |
111 } | |
112 | |
113 // The accessible name of the button provides accessibility for | |
114 // screenreaders. It uses the browser name rather than the text of the | |
115 // button "Choose", since it's not obvious to a screenreader user which | |
116 // browser each button corresponds to. | |
117 SetAccessibleName(search_engine_->short_name()); | |
118 } | |
119 | |
120 int SearchEngineChoice::GetChoiceViewWidth() { | |
121 if (is_image_label_) | |
122 return choice_view_->GetPreferredSize().width(); | |
123 else | |
124 return kSmallLogoWidth; | |
125 } | |
126 | |
127 int SearchEngineChoice::GetChoiceViewHeight() { | |
128 if (!is_image_label_) { | |
129 // Labels need to be padded to look nicer. | |
130 return choice_view_->GetPreferredSize().height() + kLabelPadding; | |
131 } else { | |
132 return choice_view_->GetPreferredSize().height(); | |
133 } | |
134 } | |
135 | |
136 void SearchEngineChoice::SetChoiceViewBounds(int x, int y, int width, | |
137 int height) { | |
138 choice_view_->SetBounds(x, y, width, height); | |
139 } | |
140 | |
141 FirstRunSearchEngineView::FirstRunSearchEngineView(Profile* profile, | |
142 bool randomize) | |
143 : views::ClientView(NULL, NULL), | |
144 background_image_(NULL), | |
145 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), | |
146 text_direction_is_rtl_(base::i18n::IsRTL()), | |
147 added_to_view_hierarchy_(false), | |
148 randomize_(randomize), | |
149 user_chosen_engine_(false), | |
150 fallback_choice_(NULL), | |
151 quit_on_closing_(true) { | |
152 // Don't show ourselves until all the search engines have loaded from | |
153 // the profile -- otherwise we have nothing to show. | |
154 SetVisible(false); | |
155 | |
156 // Start loading the search engines for the given profile. The service is | |
157 // already loaded in tests. | |
158 DCHECK(template_url_service_); | |
159 if (!template_url_service_->loaded()) { | |
160 template_url_service_->AddObserver(this); | |
161 template_url_service_->Load(); | |
162 } | |
163 } | |
164 | |
165 FirstRunSearchEngineView::~FirstRunSearchEngineView() { | |
166 template_url_service_->RemoveObserver(this); | |
167 } | |
168 | |
169 string16 FirstRunSearchEngineView::GetWindowTitle() const { | |
170 return l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_TITLE); | |
171 } | |
172 | |
173 views::View* FirstRunSearchEngineView::GetContentsView() { | |
174 return this; | |
175 } | |
176 | |
177 views::ClientView* FirstRunSearchEngineView::CreateClientView( | |
178 views::Widget* widget) { | |
179 return this; | |
180 } | |
181 | |
182 void FirstRunSearchEngineView::WindowClosing() { | |
183 // If the window is closed by clicking the close button, we default to the | |
184 // engine in the first slot. | |
185 if (!user_chosen_engine_) | |
186 ChooseSearchEngine(fallback_choice_); | |
187 if (quit_on_closing_) | |
188 MessageLoop::current()->Quit(); | |
189 } | |
190 | |
191 views::Widget* FirstRunSearchEngineView::GetWidget() { | |
192 return View::GetWidget(); | |
193 } | |
194 | |
195 const views::Widget* FirstRunSearchEngineView::GetWidget() const { | |
196 return View::GetWidget(); | |
197 } | |
198 | |
199 bool FirstRunSearchEngineView::CanClose() { | |
200 // We need a valid search engine to set as default, so if the user tries to | |
201 // close the window before the template URL service is loaded, we must prevent | |
202 // this from happening. | |
203 return fallback_choice_ != NULL; | |
204 } | |
205 | |
206 void FirstRunSearchEngineView::ButtonPressed(views::Button* sender, | |
207 const views::Event& event) { | |
208 ChooseSearchEngine(static_cast<SearchEngineChoice*>(sender)); | |
209 GetWidget()->Close(); | |
210 // This will call through to WindowClosing() above and will quit the message | |
211 // loop. | |
212 } | |
213 | |
214 void FirstRunSearchEngineView::OnPaint(gfx::Canvas* canvas) { | |
215 // Fill in behind the background image with the standard gray toolbar color. | |
216 canvas->FillRect(GetThemeProvider()->GetColor(ThemeService::COLOR_TOOLBAR), | |
217 gfx::Rect(0, 0, width(), background_image_->height())); | |
218 // The rest of the dialog background should be white. | |
219 DCHECK(height() > background_image_->height()); | |
220 canvas->FillRect(SK_ColorWHITE, | |
221 gfx::Rect(0, background_image_->height(), width(), | |
222 height() - background_image_->height())); | |
223 } | |
224 | |
225 void FirstRunSearchEngineView::OnTemplateURLServiceChanged() { | |
226 // We only watch the search engine model change once, on load. Remove | |
227 // observer so we don't try to redraw if engines change under us. | |
228 template_url_service_->RemoveObserver(this); | |
229 AddSearchEnginesIfPossible(); | |
230 } | |
231 | |
232 gfx::Size FirstRunSearchEngineView::GetPreferredSize() { | |
233 return views::Widget::GetLocalizedContentsSize( | |
234 IDS_FIRSTRUN_SEARCH_ENGINE_SELECTION_WIDTH_CHARS, | |
235 IDS_FIRSTRUN_SEARCH_ENGINE_SELECTION_HEIGHT_LINES); | |
236 } | |
237 | |
238 void FirstRunSearchEngineView::Layout() { | |
239 gfx::Size pref_size = background_image_->GetPreferredSize(); | |
240 background_image_->SetBounds(0, 0, GetPreferredSize().width(), | |
241 pref_size.height()); | |
242 | |
243 // General vertical spacing between elements: | |
244 const int kVertSpacing = 8; | |
245 // Percentage of vertical space around logos to use for upper padding. | |
246 const double kUpperPaddingPercent = 0.4; | |
247 | |
248 int num_choices = search_engine_choices_.size(); | |
249 int label_width = GetPreferredSize().width() - 2 * views::kPanelHorizMargin; | |
250 int label_height = GetPreferredSize().height() - 2 * views::kPanelVertMargin; | |
251 | |
252 // Set title. | |
253 title_label_->SetBounds( | |
254 views::kPanelHorizMargin, | |
255 pref_size.height() / 2 - title_label_->GetPreferredSize().height() / 2, | |
256 label_width, | |
257 title_label_->GetPreferredSize().height()); | |
258 | |
259 int next_v_space = background_image_->height() + kVertSpacing * 2; | |
260 | |
261 // Set text describing search engine hooked into omnibox. | |
262 text_label_->SetBounds(views::kPanelHorizMargin, | |
263 next_v_space, | |
264 label_width, | |
265 text_label_->GetPreferredSize().height()); | |
266 next_v_space = text_label_->y() + | |
267 text_label_->height() + kVertSpacing; | |
268 | |
269 // Logos and buttons | |
270 if (num_choices > 0) { | |
271 // All search engine logos are sized the same, so the size of the first is | |
272 // generally valid as the size of all. | |
273 int logo_width = search_engine_choices_[0]->GetChoiceViewWidth(); | |
274 int logo_height = search_engine_choices_[0]->GetChoiceViewHeight(); | |
275 int button_width = search_engine_choices_[0]->GetPreferredSize().width(); | |
276 int button_height = search_engine_choices_[0]->GetPreferredSize().height(); | |
277 | |
278 int logo_section_height = logo_height + kVertSpacing + button_height; | |
279 // Upper logo margin gives the amount of whitespace between the text label | |
280 // and the logo field. The total amount of whitespace available is equal | |
281 // to the height of the whole label subtracting the heights of the logo | |
282 // section itself, the top image, the text label, and vertical spacing | |
283 // between those elements. | |
284 int upper_logo_margin = | |
285 static_cast<int>((label_height - logo_section_height - | |
286 background_image_->height() - text_label_->height() | |
287 - kVertSpacing + views::kPanelVertMargin) * kUpperPaddingPercent); | |
288 | |
289 next_v_space = text_label_->y() + text_label_->height() + | |
290 upper_logo_margin; | |
291 | |
292 // The search engine logos (which all have equal size): | |
293 int logo_padding = | |
294 (label_width - (num_choices * logo_width)) / (num_choices + 1); | |
295 | |
296 search_engine_choices_[0]->SetChoiceViewBounds( | |
297 views::kPanelHorizMargin + logo_padding, next_v_space, logo_width, | |
298 logo_height); | |
299 | |
300 int next_h_space = search_engine_choices_[0]->GetView()->x() + | |
301 logo_width + logo_padding; | |
302 search_engine_choices_[1]->SetChoiceViewBounds( | |
303 next_h_space, next_v_space, logo_width, logo_height); | |
304 | |
305 next_h_space = search_engine_choices_[1]->GetView()->x() + logo_width + | |
306 logo_padding; | |
307 if (num_choices > 2) { | |
308 search_engine_choices_[2]->SetChoiceViewBounds( | |
309 next_h_space, next_v_space, logo_width, logo_height); | |
310 } | |
311 | |
312 if (num_choices > 3) { | |
313 next_h_space = search_engine_choices_[2]->GetView()->x() + logo_width + | |
314 logo_padding; | |
315 search_engine_choices_[3]->SetChoiceViewBounds( | |
316 next_h_space, next_v_space, logo_width, logo_height); | |
317 } | |
318 | |
319 next_v_space = search_engine_choices_[0]->GetView()->y() + logo_height + | |
320 kVertSpacing; | |
321 | |
322 // The buttons for search engine selection: | |
323 int button_padding = logo_padding + logo_width / 2 - button_width / 2; | |
324 | |
325 search_engine_choices_[0]->SetBounds( | |
326 views::kPanelHorizMargin + button_padding, next_v_space, | |
327 button_width, button_height); | |
328 | |
329 next_h_space = search_engine_choices_[0]->x() + logo_width + logo_padding; | |
330 search_engine_choices_[1]->SetBounds(next_h_space, next_v_space, | |
331 button_width, button_height); | |
332 next_h_space = search_engine_choices_[1]->x() + logo_width + logo_padding; | |
333 if (num_choices > 2) { | |
334 search_engine_choices_[2]->SetBounds(next_h_space, next_v_space, | |
335 button_width, button_height); | |
336 } | |
337 | |
338 if (num_choices > 3) { | |
339 next_h_space = search_engine_choices_[2]->x() + logo_width + | |
340 logo_padding; | |
341 search_engine_choices_[3]->SetBounds(next_h_space, next_v_space, | |
342 button_width, button_height); | |
343 } | |
344 } // if (search_engine_choices.size() > 0) | |
345 } | |
346 | |
347 void FirstRunSearchEngineView::ViewHierarchyChanged(bool is_add, | |
348 View* parent, | |
349 View* child) { | |
350 View::ViewHierarchyChanged(is_add, parent, child); | |
351 | |
352 if (is_add && (child == this)) { | |
353 background_image_ = new views::ImageView(); | |
354 background_image_->SetImage( | |
355 ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
356 IDR_SEARCH_ENGINE_DIALOG_TOP)); | |
357 background_image_->EnableCanvasFlippingForRTLUI(true); | |
358 background_image_->SetHorizontalAlignment(text_direction_is_rtl_ ? | |
359 views::ImageView::LEADING : views::ImageView::TRAILING); | |
360 | |
361 AddChildView(background_image_); | |
362 | |
363 int label_width = GetPreferredSize().width() - 2 * views::kPanelHorizMargin; | |
364 | |
365 // Add title and text asking the user to choose a search engine: | |
366 title_label_ = new views::Label(l10n_util::GetStringUTF16( | |
367 IDS_FR_SEARCH_MAIN_LABEL)); | |
368 title_label_->SetBackgroundColor( | |
369 GetThemeProvider()->GetColor(ThemeService::COLOR_TOOLBAR)); | |
370 title_label_->SetEnabledColor(SK_ColorBLACK); | |
371 title_label_->SetFont(title_label_->font().DeriveFont(3, gfx::Font::BOLD)); | |
372 title_label_->SetMultiLine(true); | |
373 title_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
374 title_label_->SizeToFit(label_width); | |
375 AddChildView(title_label_); | |
376 | |
377 text_label_ = new views::Label(l10n_util::GetStringFUTF16( | |
378 IDS_FR_SEARCH_TEXT, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); | |
379 text_label_->SetBackgroundColor(SK_ColorWHITE); | |
380 text_label_->SetEnabledColor(SK_ColorBLACK); | |
381 text_label_->SetFont(text_label_->font().DeriveFont(1, gfx::Font::NORMAL)); | |
382 text_label_->SetMultiLine(true); | |
383 text_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
384 text_label_->SizeToFit(label_width); | |
385 AddChildView(text_label_); | |
386 | |
387 added_to_view_hierarchy_ = true; | |
388 AddSearchEnginesIfPossible(); | |
389 } | |
390 } | |
391 | |
392 void FirstRunSearchEngineView::GetAccessibleState( | |
393 ui::AccessibleViewState* state) { | |
394 state->role = ui::AccessibilityTypes::ROLE_ALERT; | |
395 } | |
396 | |
397 void FirstRunSearchEngineView::AddSearchEnginesIfPossible() { | |
398 if (!template_url_service_->loaded() || !added_to_view_hierarchy_) | |
399 return; | |
400 | |
401 // Add search engines in template_url_service_ to buttons list. The | |
402 // first three will always be from prepopulated data. | |
403 std::vector<const TemplateURL*> template_urls = | |
404 template_url_service_->GetTemplateURLs(); | |
405 | |
406 // If we have fewer than two search engines, end search engine dialog | |
407 // immediately, leaving imported default search engine setting intact. | |
408 if (template_urls.size() < 2) { | |
409 MessageLoop::current()->Quit(); | |
410 return; | |
411 } | |
412 | |
413 std::vector<const TemplateURL*>::iterator search_engine_iter; | |
414 | |
415 // Is user's default search engine included in first three prepopulated | |
416 // set? If not, we need to expand the dialog to include a fourth engine. | |
417 const TemplateURL* default_search_engine = | |
418 template_url_service_->GetDefaultSearchProvider(); | |
419 // If the user's default choice is not in the first three search engines | |
420 // in template_urls, store it in |default_choice| and provide it as a | |
421 // fourth option. | |
422 SearchEngineChoice* default_choice = NULL; | |
423 | |
424 // First, see if we have 4 logos to show (in which case we use small logos). | |
425 // We show 4 logos when the default search engine the user has chosen is | |
426 // not one of the first three prepopulated engines. | |
427 if (template_urls.size() > 3) { | |
428 for (search_engine_iter = template_urls.begin() + 3; | |
429 search_engine_iter != template_urls.end(); | |
430 ++search_engine_iter) { | |
431 if (default_search_engine == *search_engine_iter) { | |
432 default_choice = new SearchEngineChoice(this, *search_engine_iter, | |
433 true); | |
434 } | |
435 } | |
436 } | |
437 | |
438 // Now that we know what size the logos should be, create new search engine | |
439 // choices for the view. If there are 2 search engines, only show 2 | |
440 // choices; for 3 or more, show 3 (unless the default is not one of the | |
441 // top 3, in which case show 4). | |
442 for (search_engine_iter = template_urls.begin(); | |
443 search_engine_iter < template_urls.begin() + | |
444 (template_urls.size() < 3 ? 2 : 3); | |
445 ++search_engine_iter) { | |
446 // Push first three engines into buttons: | |
447 SearchEngineChoice* choice = new SearchEngineChoice(this, | |
448 *search_engine_iter, default_choice != NULL); | |
449 search_engine_choices_.push_back(choice); | |
450 AddChildView(choice->GetView()); // The logo or text view. | |
451 AddChildView(choice); // The button associated with the choice. | |
452 } | |
453 // Push the default choice to the fourth position. | |
454 if (default_choice) { | |
455 search_engine_choices_.push_back(default_choice); | |
456 AddChildView(default_choice->GetView()); // The logo or text view. | |
457 AddChildView(default_choice); // The button associated with the choice. | |
458 } | |
459 | |
460 // It is critically important that this line happens before randomization is | |
461 // done below. | |
462 fallback_choice_ = search_engine_choices_.front(); | |
463 | |
464 // Randomize order of logos if option has been set. | |
465 if (randomize_) { | |
466 std::random_shuffle(search_engine_choices_.begin(), | |
467 search_engine_choices_.end(), | |
468 base::RandGenerator); | |
469 // Assign to each choice the position in which it is shown on the screen. | |
470 std::vector<SearchEngineChoice*>::iterator it; | |
471 int slot = 0; | |
472 for (it = search_engine_choices_.begin(); | |
473 it != search_engine_choices_.end(); | |
474 it++) { | |
475 (*it)->set_slot(slot++); | |
476 } | |
477 } | |
478 | |
479 // Now that we know how many logos to show, lay out and become visible. | |
480 SetVisible(true); | |
481 Layout(); | |
482 SchedulePaint(); | |
483 | |
484 // If the widget has detected that a screenreader is running, change the | |
485 // button names from "Choose" to the name of the search engine. This works | |
486 // around a bug that JAWS ignores the accessible name of a native button. | |
487 if (GetWidget() && GetWidget()->IsAccessibleWidget()) { | |
488 std::vector<SearchEngineChoice*>::iterator it; | |
489 for (it = search_engine_choices_.begin(); | |
490 it != search_engine_choices_.end(); | |
491 it++) { | |
492 (*it)->SetText((*it)->GetSearchEngine()->short_name()); | |
493 } | |
494 } | |
495 | |
496 // This will tell screenreaders that they should read the full text | |
497 // of this dialog to the user now (rather than waiting for the user | |
498 // to explore it). | |
499 GetWidget()->NotifyAccessibilityEvent( | |
500 this, ui::AccessibilityTypes::EVENT_ALERT, true); | |
501 } | |
502 | |
503 void FirstRunSearchEngineView::ChooseSearchEngine(SearchEngineChoice* choice) { | |
504 user_chosen_engine_ = true; | |
505 DCHECK(choice && template_url_service_); | |
506 template_url_service_->SetSearchEngineDialogSlot(choice->slot()); | |
507 const TemplateURL* default_search = choice->GetSearchEngine(); | |
508 if (default_search) | |
509 template_url_service_->SetDefaultSearchProvider(default_search); | |
510 } | |
OLD | NEW |