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/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
390 NOTREACHED() << L"Unknown button"; | 390 NOTREACHED() << L"Unknown button"; |
391 break; | 391 break; |
392 } | 392 } |
393 } | 393 } |
394 | 394 |
395 //////////////////////////////////////////////////////////////////////////////// | 395 //////////////////////////////////////////////////////////////////////////////// |
396 // FindBarView, views::TextfieldController implementation: | 396 // FindBarView, views::TextfieldController implementation: |
397 | 397 |
398 void FindBarView::ContentsChanged(views::Textfield* sender, | 398 void FindBarView::ContentsChanged(views::Textfield* sender, |
399 const string16& new_contents) { | 399 const string16& new_contents) { |
400 FindBarController* controller = find_bar_host()->GetFindBarController(); | 400 // TextfieldController::OnAfterUserAction() is supported only by Views |
401 DCHECK(controller); | 401 // implementation, and NativeTextfieldWin doesn't call OnAfterUserAction(). |
402 content::WebContents* web_contents = controller->web_contents(); | 402 // Call DoFinding() here. |
403 // We must guard against a NULL web_contents, which can happen if the text | 403 // TODO(yukishiino): Remove this code after the migration to Views. |
404 // in the Find box is changed right after the tab is destroyed. Otherwise, it | 404 if (!views::Textfield::IsViewsTextfieldEnabled()) |
405 // can lead to crashes, as exposed by automation testing in issue 8048. | 405 DoFinding(sender); |
406 if (!web_contents) | |
407 return; | |
408 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents); | |
409 | |
410 // When the user changes something in the text box we check the contents and | |
411 // if the textbox contains something we set it as the new search string and | |
412 // initiate search (even though old searches might be in progress). | |
413 if (!new_contents.empty()) { | |
414 // The last two params here are forward (true) and case sensitive (false). | |
415 find_tab_helper->StartFinding(new_contents, true, false); | |
416 } else { | |
417 find_tab_helper->StopFinding(FindBarController::kClearSelectionOnPage); | |
418 UpdateForResult(find_tab_helper->find_result(), string16()); | |
419 find_bar_host()->MoveWindowIfNecessary(gfx::Rect(), false); | |
420 | |
421 // Clearing the text box should clear the prepopulate state so that when | |
422 // we close and reopen the Find box it doesn't show the search we just | |
423 // deleted. We can't do this on ChromeOS yet because we get ContentsChanged | |
424 // sent for a lot more things than just the user nulling out the search | |
425 // terms. See http://crbug.com/45372. | |
426 Profile* profile = | |
427 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
428 FindBarState* find_bar_state = FindBarStateFactory::GetForProfile(profile); | |
429 find_bar_state->set_last_prepopulate_text(string16()); | |
430 } | |
431 } | 406 } |
432 | 407 |
433 bool FindBarView::HandleKeyEvent(views::Textfield* sender, | 408 bool FindBarView::HandleKeyEvent(views::Textfield* sender, |
434 const ui::KeyEvent& key_event) { | 409 const ui::KeyEvent& key_event) { |
435 // If the dialog is not visible, there is no reason to process keyboard input. | 410 // If the dialog is not visible, there is no reason to process keyboard input. |
436 if (!host()->IsVisible()) | 411 if (!host()->IsVisible()) |
437 return false; | 412 return false; |
438 | 413 |
439 if (find_bar_host()->MaybeForwardKeyEventToWebpage(key_event)) | 414 if (find_bar_host()->MaybeForwardKeyEventToWebpage(key_event)) |
440 return true; // Handled, we are done! | 415 return true; // Handled, we are done! |
441 | 416 |
442 if (key_event.key_code() == ui::VKEY_RETURN) { | 417 if (key_event.key_code() == ui::VKEY_RETURN) { |
443 // Pressing Return/Enter starts the search (unless text box is empty). | 418 // Pressing Return/Enter starts the search (unless text box is empty). |
444 string16 find_string = find_text_->text(); | 419 string16 find_string = find_text_->text(); |
445 if (!find_string.empty()) { | 420 if (!find_string.empty()) { |
446 FindBarController* controller = find_bar_host()->GetFindBarController(); | 421 FindBarController* controller = find_bar_host()->GetFindBarController(); |
447 FindTabHelper* find_tab_helper = | 422 FindTabHelper* find_tab_helper = |
448 FindTabHelper::FromWebContents(controller->web_contents()); | 423 FindTabHelper::FromWebContents(controller->web_contents()); |
449 // Search forwards for enter, backwards for shift-enter. | 424 // Search forwards for enter, backwards for shift-enter. |
450 find_tab_helper->StartFinding(find_string, | 425 find_tab_helper->StartFinding(find_string, |
451 !key_event.IsShiftDown(), | 426 !key_event.IsShiftDown(), |
452 false); // Not case sensitive. | 427 false); // Not case sensitive. |
453 } | 428 } |
454 return true; | 429 return true; |
455 } | 430 } |
456 | 431 |
457 return false; | 432 return false; |
458 } | 433 } |
459 | 434 |
435 void FindBarView::OnAfterUserAction(views::Textfield* sender) { | |
436 // The composition text wouldn't be what the user is really looking for. | |
437 // We delay the search until the user commits the composition text. | |
438 if (sender->IsIMEComposing() || | |
439 sender->text() == last_searched_text_) | |
msw
2013/06/04 01:30:23
nit: this fits on the line above; also invert the
Yuki
2013/06/04 03:35:04
Done.
| |
440 return; | |
441 | |
442 DoFinding(sender); | |
443 } | |
444 | |
460 void FindBarView::OnAfterCutOrCopy() { | 445 void FindBarView::OnAfterCutOrCopy() { |
461 Profile* profile = host()->browser_view()->browser()->profile(); | 446 Profile* profile = host()->browser_view()->browser()->profile(); |
462 ui::SourceTag source_tag = | 447 ui::SourceTag source_tag = |
463 content::BrowserContext::GetMarkerForOffTheRecordContext(profile); | 448 content::BrowserContext::GetMarkerForOffTheRecordContext(profile); |
464 if (source_tag != ui::SourceTag()) { | 449 if (source_tag != ui::SourceTag()) { |
465 // Overwrite the clipboard with the correct SourceTag | 450 // Overwrite the clipboard with the correct SourceTag |
466 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); | 451 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); |
467 string16 text; | 452 string16 text; |
468 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &text); | 453 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &text); |
469 | 454 |
470 ui::ScopedClipboardWriter scw(clipboard, | 455 ui::ScopedClipboardWriter scw(clipboard, |
471 ui::Clipboard::BUFFER_STANDARD, | 456 ui::Clipboard::BUFFER_STANDARD, |
472 source_tag); | 457 source_tag); |
473 scw.WriteText(text); | 458 scw.WriteText(text); |
474 } | 459 } |
475 } | 460 } |
476 | 461 |
462 void FindBarView::OnAfterPaste() { | |
463 // Clear the last search text so we always search for the user input after | |
464 // a paste operation, even if the pasted text is the same as before. | |
465 // See http://crbug.com/79002 | |
466 last_searched_text_.clear(); | |
467 } | |
468 | |
469 void FindBarView::DoFinding(views::Textfield* sender) { | |
470 FindBarController* controller = find_bar_host()->GetFindBarController(); | |
471 DCHECK(controller); | |
472 content::WebContents* web_contents = controller->web_contents(); | |
473 // We must guard against a NULL web_contents, which can happen if the text | |
474 // in the Find box is changed right after the tab is destroyed. Otherwise, it | |
475 // can lead to crashes, as exposed by automation testing in issue 8048. | |
476 if (!web_contents) | |
477 return; | |
478 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents); | |
479 | |
480 const string16& search_text = sender->text(); | |
481 last_searched_text_ = search_text; | |
482 | |
483 // When the user changes something in the text box we check the contents and | |
484 // if the textbox contains something we set it as the new search string and | |
485 // initiate search (even though old searches might be in progress). | |
486 if (!search_text.empty()) { | |
487 // The last two params here are forward (true) and case sensitive (false). | |
488 find_tab_helper->StartFinding(search_text, true, false); | |
489 } else { | |
490 find_tab_helper->StopFinding(FindBarController::kClearSelectionOnPage); | |
491 UpdateForResult(find_tab_helper->find_result(), string16()); | |
492 find_bar_host()->MoveWindowIfNecessary(gfx::Rect(), false); | |
493 | |
494 // Clearing the text box should clear the prepopulate state so that when | |
495 // we close and reopen the Find box it doesn't show the search we just | |
496 // deleted. We can't do this on ChromeOS yet because we get ContentsChanged | |
497 // sent for a lot more things than just the user nulling out the search | |
498 // terms. See http://crbug.com/45372. | |
499 Profile* profile = | |
500 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
501 FindBarState* find_bar_state = FindBarStateFactory::GetForProfile(profile); | |
502 find_bar_state->set_last_prepopulate_text(string16()); | |
503 } | |
504 } | |
505 | |
477 void FindBarView::UpdateMatchCountAppearance(bool no_match) { | 506 void FindBarView::UpdateMatchCountAppearance(bool no_match) { |
478 if (no_match) { | 507 if (no_match) { |
479 match_count_text_->SetBackgroundColor(kBackgroundColorNoMatch); | 508 match_count_text_->SetBackgroundColor(kBackgroundColorNoMatch); |
480 match_count_text_->SetEnabledColor(kTextColorNoMatch); | 509 match_count_text_->SetEnabledColor(kTextColorNoMatch); |
481 } else { | 510 } else { |
482 match_count_text_->SetBackgroundColor(kBackgroundColorMatch); | 511 match_count_text_->SetBackgroundColor(kBackgroundColorMatch); |
483 match_count_text_->SetEnabledColor(kTextColorMatchCount); | 512 match_count_text_->SetEnabledColor(kTextColorMatchCount); |
484 } | 513 } |
485 } | 514 } |
486 | 515 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
522 | 551 |
523 void FindBarView::OnThemeChanged() { | 552 void FindBarView::OnThemeChanged() { |
524 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 553 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
525 if (GetThemeProvider()) { | 554 if (GetThemeProvider()) { |
526 close_button_->SetBackground( | 555 close_button_->SetBackground( |
527 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TAB_TEXT), | 556 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TAB_TEXT), |
528 rb.GetImageSkiaNamed(IDR_CLOSE_1), | 557 rb.GetImageSkiaNamed(IDR_CLOSE_1), |
529 rb.GetImageSkiaNamed(IDR_CLOSE_1_MASK)); | 558 rb.GetImageSkiaNamed(IDR_CLOSE_1_MASK)); |
530 } | 559 } |
531 } | 560 } |
OLD | NEW |