OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/find_bar_view.h" | 5 #include "chrome/browser/ui/views/find_bar_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/i18n/number_formatting.h" | 9 #include "base/i18n/number_formatting.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 104 |
105 DISALLOW_COPY_AND_ASSIGN(FocusForwarderView); | 105 DISALLOW_COPY_AND_ASSIGN(FocusForwarderView); |
106 }; | 106 }; |
107 | 107 |
108 } // namespace | 108 } // namespace |
109 | 109 |
110 //////////////////////////////////////////////////////////////////////////////// | 110 //////////////////////////////////////////////////////////////////////////////// |
111 // FindBarView, public: | 111 // FindBarView, public: |
112 | 112 |
113 FindBarView::FindBarView(FindBarHost* host) | 113 FindBarView::FindBarView(FindBarHost* host) |
114 : DropdownBarView(host), | 114 : find_bar_host_(host), |
115 find_text_(new views::Textfield), | 115 find_text_(new views::Textfield), |
116 match_count_text_(new MatchCountLabel()), | 116 match_count_text_(new MatchCountLabel()), |
117 focus_forwarder_view_(new FocusForwarderView(find_text_)), | 117 focus_forwarder_view_(new FocusForwarderView(find_text_)), |
118 separator_(new views::Separator(views::Separator::VERTICAL)), | 118 separator_(new views::Separator(views::Separator::VERTICAL)), |
119 find_previous_button_(new views::VectorIconButton(this)), | 119 find_previous_button_(new views::VectorIconButton(this)), |
120 find_next_button_(new views::VectorIconButton(this)), | 120 find_next_button_(new views::VectorIconButton(this)), |
121 close_button_(new views::VectorIconButton(this)) { | 121 close_button_(new views::VectorIconButton(this)) { |
122 find_text_->set_id(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); | 122 find_text_->set_id(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); |
123 find_text_->set_default_width_in_chars(kDefaultCharWidth); | 123 find_text_->set_default_width_in_chars(kDefaultCharWidth); |
124 find_text_->set_controller(this); | 124 find_text_->set_controller(this); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 //////////////////////////////////////////////////////////////////////////////// | 278 //////////////////////////////////////////////////////////////////////////////// |
279 // FindBarView, views::VectorIconButtonDelegate implementation: | 279 // FindBarView, views::VectorIconButtonDelegate implementation: |
280 | 280 |
281 void FindBarView::ButtonPressed( | 281 void FindBarView::ButtonPressed( |
282 views::Button* sender, const ui::Event& event) { | 282 views::Button* sender, const ui::Event& event) { |
283 switch (sender->id()) { | 283 switch (sender->id()) { |
284 case VIEW_ID_FIND_IN_PAGE_PREVIOUS_BUTTON: | 284 case VIEW_ID_FIND_IN_PAGE_PREVIOUS_BUTTON: |
285 case VIEW_ID_FIND_IN_PAGE_NEXT_BUTTON: | 285 case VIEW_ID_FIND_IN_PAGE_NEXT_BUTTON: |
286 if (!find_text_->text().empty()) { | 286 if (!find_text_->text().empty()) { |
287 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents( | 287 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents( |
288 find_bar_host()->GetFindBarController()->web_contents()); | 288 find_bar_host_->GetFindBarController()->web_contents()); |
289 find_tab_helper->StartFinding( | 289 find_tab_helper->StartFinding( |
290 find_text_->text(), | 290 find_text_->text(), |
291 sender->id() == VIEW_ID_FIND_IN_PAGE_NEXT_BUTTON, | 291 sender->id() == VIEW_ID_FIND_IN_PAGE_NEXT_BUTTON, |
292 false); // Not case sensitive. | 292 false); // Not case sensitive. |
293 } | 293 } |
294 break; | 294 break; |
295 case VIEW_ID_FIND_IN_PAGE_CLOSE_BUTTON: | 295 case VIEW_ID_FIND_IN_PAGE_CLOSE_BUTTON: |
296 find_bar_host()->GetFindBarController()->EndFindSession( | 296 find_bar_host_->GetFindBarController()->EndFindSession( |
297 FindBarController::kKeepSelectionOnPage, | 297 FindBarController::kKeepSelectionOnPage, |
298 FindBarController::kKeepResultsInFindBox); | 298 FindBarController::kKeepResultsInFindBox); |
299 break; | 299 break; |
300 default: | 300 default: |
301 NOTREACHED() << "Unknown button"; | 301 NOTREACHED() << "Unknown button"; |
302 break; | 302 break; |
303 } | 303 } |
304 } | 304 } |
305 | 305 |
306 SkColor FindBarView::GetVectorIconBaseColor() const { | 306 SkColor FindBarView::GetVectorIconBaseColor() const { |
307 return GetNativeTheme()->GetSystemColor( | 307 return GetNativeTheme()->GetSystemColor( |
308 ui::NativeTheme::kColorId_TextfieldDefaultColor); | 308 ui::NativeTheme::kColorId_TextfieldDefaultColor); |
309 } | 309 } |
310 | 310 |
311 //////////////////////////////////////////////////////////////////////////////// | 311 //////////////////////////////////////////////////////////////////////////////// |
312 // FindBarView, views::TextfieldController implementation: | 312 // FindBarView, views::TextfieldController implementation: |
313 | 313 |
314 bool FindBarView::HandleKeyEvent(views::Textfield* sender, | 314 bool FindBarView::HandleKeyEvent(views::Textfield* sender, |
315 const ui::KeyEvent& key_event) { | 315 const ui::KeyEvent& key_event) { |
316 // If the dialog is not visible, there is no reason to process keyboard input. | 316 // If the dialog is not visible, there is no reason to process keyboard input. |
317 if (!host()->IsVisible()) | 317 if (!find_bar_host_->IsVisible()) |
318 return false; | 318 return false; |
319 | 319 |
320 if (find_bar_host()->MaybeForwardKeyEventToWebpage(key_event)) | 320 if (find_bar_host_->MaybeForwardKeyEventToWebpage(key_event)) |
321 return true; // Handled, we are done! | 321 return true; // Handled, we are done! |
322 | 322 |
323 if (key_event.key_code() == ui::VKEY_RETURN && | 323 if (key_event.key_code() == ui::VKEY_RETURN && |
324 key_event.type() == ui::ET_KEY_PRESSED) { | 324 key_event.type() == ui::ET_KEY_PRESSED) { |
325 // Pressing Return/Enter starts the search (unless text box is empty). | 325 // Pressing Return/Enter starts the search (unless text box is empty). |
326 base::string16 find_string = find_text_->text(); | 326 base::string16 find_string = find_text_->text(); |
327 if (!find_string.empty()) { | 327 if (!find_string.empty()) { |
328 FindBarController* controller = find_bar_host()->GetFindBarController(); | 328 FindBarController* controller = find_bar_host_->GetFindBarController(); |
329 FindTabHelper* find_tab_helper = | 329 FindTabHelper* find_tab_helper = |
330 FindTabHelper::FromWebContents(controller->web_contents()); | 330 FindTabHelper::FromWebContents(controller->web_contents()); |
331 // Search forwards for enter, backwards for shift-enter. | 331 // Search forwards for enter, backwards for shift-enter. |
332 find_tab_helper->StartFinding(find_string, | 332 find_tab_helper->StartFinding(find_string, |
333 !key_event.IsShiftDown(), | 333 !key_event.IsShiftDown(), |
334 false); // Not case sensitive. | 334 false); // Not case sensitive. |
335 } | 335 } |
336 return true; | 336 return true; |
337 } | 337 } |
338 | 338 |
(...skipping 13 matching lines...) Expand all Loading... |
352 // See http://crbug.com/79002 | 352 // See http://crbug.com/79002 |
353 last_searched_text_.clear(); | 353 last_searched_text_.clear(); |
354 } | 354 } |
355 | 355 |
356 views::View* FindBarView::TargetForRect(View* root, const gfx::Rect& rect) { | 356 views::View* FindBarView::TargetForRect(View* root, const gfx::Rect& rect) { |
357 DCHECK_EQ(match_count_text_, root); | 357 DCHECK_EQ(match_count_text_, root); |
358 return find_text_; | 358 return find_text_; |
359 } | 359 } |
360 | 360 |
361 void FindBarView::Find(const base::string16& search_text) { | 361 void FindBarView::Find(const base::string16& search_text) { |
362 FindBarController* controller = find_bar_host()->GetFindBarController(); | 362 FindBarController* controller = find_bar_host_->GetFindBarController(); |
363 DCHECK(controller); | 363 DCHECK(controller); |
364 content::WebContents* web_contents = controller->web_contents(); | 364 content::WebContents* web_contents = controller->web_contents(); |
365 // We must guard against a NULL web_contents, which can happen if the text | 365 // We must guard against a NULL web_contents, which can happen if the text |
366 // in the Find box is changed right after the tab is destroyed. Otherwise, it | 366 // in the Find box is changed right after the tab is destroyed. Otherwise, it |
367 // can lead to crashes, as exposed by automation testing in issue 8048. | 367 // can lead to crashes, as exposed by automation testing in issue 8048. |
368 if (!web_contents) | 368 if (!web_contents) |
369 return; | 369 return; |
370 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents); | 370 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents); |
371 | 371 |
372 last_searched_text_ = search_text; | 372 last_searched_text_ = search_text; |
373 | 373 |
374 // When the user changes something in the text box we check the contents and | 374 // When the user changes something in the text box we check the contents and |
375 // if the textbox contains something we set it as the new search string and | 375 // if the textbox contains something we set it as the new search string and |
376 // initiate search (even though old searches might be in progress). | 376 // initiate search (even though old searches might be in progress). |
377 if (!search_text.empty()) { | 377 if (!search_text.empty()) { |
378 // The last two params here are forward (true) and case sensitive (false). | 378 // The last two params here are forward (true) and case sensitive (false). |
379 find_tab_helper->StartFinding(search_text, true, false); | 379 find_tab_helper->StartFinding(search_text, true, false); |
380 } else { | 380 } else { |
381 find_tab_helper->StopFinding(FindBarController::kClearSelectionOnPage); | 381 find_tab_helper->StopFinding(FindBarController::kClearSelectionOnPage); |
382 UpdateForResult(find_tab_helper->find_result(), base::string16()); | 382 UpdateForResult(find_tab_helper->find_result(), base::string16()); |
383 find_bar_host()->MoveWindowIfNecessary(gfx::Rect()); | 383 find_bar_host_->MoveWindowIfNecessary(gfx::Rect()); |
384 | 384 |
385 // Clearing the text box should clear the prepopulate state so that when | 385 // Clearing the text box should clear the prepopulate state so that when |
386 // we close and reopen the Find box it doesn't show the search we just | 386 // we close and reopen the Find box it doesn't show the search we just |
387 // deleted. We can't do this on ChromeOS yet because we get ContentsChanged | 387 // deleted. We can't do this on ChromeOS yet because we get ContentsChanged |
388 // sent for a lot more things than just the user nulling out the search | 388 // sent for a lot more things than just the user nulling out the search |
389 // terms. See http://crbug.com/45372. | 389 // terms. See http://crbug.com/45372. |
390 Profile* profile = | 390 Profile* profile = |
391 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 391 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
392 FindBarState* find_bar_state = FindBarStateFactory::GetForProfile(profile); | 392 FindBarState* find_bar_state = FindBarStateFactory::GetForProfile(profile); |
393 find_bar_state->set_last_prepopulate_text(base::string16()); | 393 find_bar_state->set_last_prepopulate_text(base::string16()); |
394 } | 394 } |
395 } | 395 } |
396 | 396 |
397 void FindBarView::UpdateMatchCountAppearance(bool no_match) { | 397 void FindBarView::UpdateMatchCountAppearance(bool no_match) { |
398 bool enable_buttons = !find_text_->text().empty() && !no_match; | 398 bool enable_buttons = !find_text_->text().empty() && !no_match; |
399 find_previous_button_->SetEnabled(enable_buttons); | 399 find_previous_button_->SetEnabled(enable_buttons); |
400 find_next_button_->SetEnabled(enable_buttons); | 400 find_next_button_->SetEnabled(enable_buttons); |
401 } | 401 } |
402 | 402 |
403 FindBarHost* FindBarView::find_bar_host() const { | |
404 return static_cast<FindBarHost*>(host()); | |
405 } | |
406 | |
407 const char* FindBarView::GetClassName() const { | 403 const char* FindBarView::GetClassName() const { |
408 return "FindBarView"; | 404 return "FindBarView"; |
409 } | 405 } |
410 | 406 |
411 void FindBarView::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 407 void FindBarView::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
412 SkColor bg_color = theme->GetSystemColor( | 408 SkColor bg_color = theme->GetSystemColor( |
413 ui::NativeTheme::kColorId_TextfieldDefaultBackground); | 409 ui::NativeTheme::kColorId_TextfieldDefaultBackground); |
414 auto border = base::MakeUnique<views::BubbleBorder>( | 410 auto border = base::MakeUnique<views::BubbleBorder>( |
415 views::BubbleBorder::NONE, views::BubbleBorder::SMALL_SHADOW, | 411 views::BubbleBorder::NONE, views::BubbleBorder::SMALL_SHADOW, |
416 bg_color); | 412 bg_color); |
417 set_background(new views::BubbleBackground(border.get())); | 413 set_background(new views::BubbleBackground(border.get())); |
418 SetBorder(std::move(border)); | 414 SetBorder(std::move(border)); |
419 | 415 |
420 match_count_text_->SetBackgroundColor(bg_color); | 416 match_count_text_->SetBackgroundColor(bg_color); |
421 SkColor text_color = | 417 SkColor text_color = |
422 theme->GetSystemColor(ui::NativeTheme::kColorId_TextfieldDefaultColor); | 418 theme->GetSystemColor(ui::NativeTheme::kColorId_TextfieldDefaultColor); |
423 match_count_text_->SetEnabledColor(SkColorSetA(text_color, 0x69)); | 419 match_count_text_->SetEnabledColor(SkColorSetA(text_color, 0x69)); |
424 separator_->SetColor(SkColorSetA(text_color, 0x26)); | 420 separator_->SetColor(SkColorSetA(text_color, 0x26)); |
425 } | 421 } |
426 | 422 |
OLD | NEW |