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/omnibox/omnibox_view_views.h" | 5 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/property_bag.h" | 8 #include "base/property_bag.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 | 217 |
218 // static | 218 // static |
219 const char OmniboxViewViews::kViewClassName[] = "BrowserOmniboxViewViews"; | 219 const char OmniboxViewViews::kViewClassName[] = "BrowserOmniboxViewViews"; |
220 | 220 |
221 OmniboxViewViews::OmniboxViewViews(OmniboxEditController* controller, | 221 OmniboxViewViews::OmniboxViewViews(OmniboxEditController* controller, |
222 ToolbarModel* toolbar_model, | 222 ToolbarModel* toolbar_model, |
223 Profile* profile, | 223 Profile* profile, |
224 CommandUpdater* command_updater, | 224 CommandUpdater* command_updater, |
225 bool popup_window_mode, | 225 bool popup_window_mode, |
226 LocationBarView* location_bar) | 226 LocationBarView* location_bar) |
227 : textfield_(NULL), | 227 : OmniboxView(profile, controller, toolbar_model, command_updater), |
228 textfield_(NULL), | |
228 popup_window_mode_(popup_window_mode), | 229 popup_window_mode_(popup_window_mode), |
229 model_(new OmniboxEditModel(this, controller, profile)), | |
230 controller_(controller), | |
231 toolbar_model_(toolbar_model), | |
232 command_updater_(command_updater), | |
233 security_level_(ToolbarModel::NONE), | 230 security_level_(ToolbarModel::NONE), |
234 ime_composing_before_change_(false), | 231 ime_composing_before_change_(false), |
235 delete_at_end_pressed_(false), | 232 delete_at_end_pressed_(false), |
236 location_bar_view_(location_bar), | 233 location_bar_view_(location_bar), |
237 ime_candidate_window_open_(false), | 234 ime_candidate_window_open_(false), |
238 select_all_on_mouse_release_(false) { | 235 select_all_on_mouse_release_(false) { |
239 if (chrome::search::IsInstantExtendedAPIEnabled( | 236 if (chrome::search::IsInstantExtendedAPIEnabled( |
240 location_bar_view_->profile())) { | 237 location_bar_view_->profile())) { |
241 set_background(views::Background::CreateSolidBackground( | 238 set_background(views::Background::CreateSolidBackground( |
242 chrome::search::kOmniboxBackgroundColor)); | 239 chrome::search::kOmniboxBackgroundColor)); |
243 } | 240 } |
244 } | 241 } |
245 | 242 |
246 OmniboxViewViews::~OmniboxViewViews() { | 243 OmniboxViewViews::~OmniboxViewViews() { |
247 #if defined(OS_CHROMEOS) | 244 #if defined(OS_CHROMEOS) |
248 chromeos::input_method::InputMethodManager::GetInstance()-> | 245 chromeos::input_method::InputMethodManager::GetInstance()-> |
249 RemoveCandidateWindowObserver(this); | 246 RemoveCandidateWindowObserver(this); |
250 #endif | 247 #endif |
251 | 248 |
252 // Explicitly teardown members which have a reference to us. Just to be safe | 249 // Explicitly teardown members which have a reference to us. Just to be safe |
253 // we want them to be destroyed before destroying any other internal state. | 250 // we want them to be destroyed before destroying any other internal state. |
254 popup_view_.reset(); | 251 popup_view_.reset(); |
255 model_.reset(); | 252 model_.reset(); |
Peter Kasting
2012/07/26 23:03:24
Sort of worries me that we had this code in the fi
dominich
2012/07/30 16:42:59
What are we no longer doing? The model_.reset() ca
Peter Kasting
2012/07/30 17:06:03
What I mean is that the bit about "reset these bef
| |
256 } | 253 } |
257 | 254 |
258 //////////////////////////////////////////////////////////////////////////////// | 255 //////////////////////////////////////////////////////////////////////////////// |
259 // OmniboxViewViews public: | 256 // OmniboxViewViews public: |
260 | 257 |
261 void OmniboxViewViews::Init(views::View* popup_parent_view) { | 258 void OmniboxViewViews::Init(views::View* popup_parent_view) { |
262 // The height of the text view is going to change based on the font used. We | 259 // The height of the text view is going to change based on the font used. We |
263 // don't want to stretch the height, and we want it vertically centered. | 260 // don't want to stretch the height, and we want it vertically centered. |
264 // TODO(oshima): make sure the above happens with views. | 261 // TODO(oshima): make sure the above happens with views. |
265 textfield_ = new AutocompleteTextfield(this, location_bar_view_); | 262 textfield_ = new AutocompleteTextfield(this, location_bar_view_); |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
461 } | 458 } |
462 | 459 |
463 void OmniboxViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 460 void OmniboxViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
464 if (popup_view_->IsOpen()) | 461 if (popup_view_->IsOpen()) |
465 popup_view_->UpdatePopupAppearance(); | 462 popup_view_->UpdatePopupAppearance(); |
466 } | 463 } |
467 | 464 |
468 //////////////////////////////////////////////////////////////////////////////// | 465 //////////////////////////////////////////////////////////////////////////////// |
469 // OmniboxViewViews, AutocopmleteEditView implementation: | 466 // OmniboxViewViews, AutocopmleteEditView implementation: |
470 | 467 |
471 OmniboxEditModel* OmniboxViewViews::model() { | |
472 return model_.get(); | |
473 } | |
474 | |
475 const OmniboxEditModel* OmniboxViewViews::model() const { | |
476 return model_.get(); | |
477 } | |
478 | |
479 void OmniboxViewViews::SaveStateToTab(WebContents* tab) { | 468 void OmniboxViewViews::SaveStateToTab(WebContents* tab) { |
480 DCHECK(tab); | 469 DCHECK(tab); |
481 | 470 |
482 // We don't want to keep the IME status, so force quit the current | 471 // We don't want to keep the IME status, so force quit the current |
483 // session here. It may affect the selection status, so order is | 472 // session here. It may affect the selection status, so order is |
484 // also important. | 473 // also important. |
485 if (textfield_->IsIMEComposing()) { | 474 if (textfield_->IsIMEComposing()) { |
486 textfield_->GetTextInputClient()->ConfirmCompositionText(); | 475 textfield_->GetTextInputClient()->ConfirmCompositionText(); |
487 textfield_->GetInputMethod()->CancelComposition(textfield_); | 476 textfield_->GetInputMethod()->CancelComposition(textfield_); |
488 } | 477 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
522 // TODO(oshima): consider saving/restoring edit history. | 511 // TODO(oshima): consider saving/restoring edit history. |
523 textfield_->ClearEditHistory(); | 512 textfield_->ClearEditHistory(); |
524 } | 513 } |
525 } else if (visibly_changed_permanent_text) { | 514 } else if (visibly_changed_permanent_text) { |
526 RevertAll(); | 515 RevertAll(); |
527 } else if (changed_security_level) { | 516 } else if (changed_security_level) { |
528 EmphasizeURLComponents(); | 517 EmphasizeURLComponents(); |
529 } | 518 } |
530 } | 519 } |
531 | 520 |
532 void OmniboxViewViews::OpenMatch(const AutocompleteMatch& match, | |
533 WindowOpenDisposition disposition, | |
534 const GURL& alternate_nav_url, | |
535 size_t selected_line) { | |
536 if (!match.destination_url.is_valid()) | |
537 return; | |
538 | |
539 model_->OpenMatch(match, disposition, alternate_nav_url, selected_line); | |
540 } | |
541 | |
542 string16 OmniboxViewViews::GetText() const { | 521 string16 OmniboxViewViews::GetText() const { |
543 // TODO(oshima): IME support | 522 // TODO(oshima): IME support |
544 return textfield_->text(); | 523 return textfield_->text(); |
545 } | 524 } |
546 | 525 |
547 bool OmniboxViewViews::IsEditingOrEmpty() const { | |
548 return model_->user_input_in_progress() || (GetTextLength() == 0); | |
549 } | |
550 | |
551 int OmniboxViewViews::GetIcon() const { | |
552 return IsEditingOrEmpty() ? | |
553 AutocompleteMatch::TypeToIcon(model_->CurrentTextType()) : | |
554 toolbar_model_->GetIcon(); | |
555 } | |
556 | |
557 void OmniboxViewViews::SetUserText(const string16& text) { | |
558 SetUserText(text, text, true); | |
559 } | |
560 | |
561 void OmniboxViewViews::SetUserText(const string16& text, | |
562 const string16& display_text, | |
563 bool update_popup) { | |
564 model_->SetUserText(text); | |
565 SetWindowTextAndCaretPos(display_text, display_text.length(), update_popup, | |
566 true); | |
567 } | |
568 | |
569 void OmniboxViewViews::SetWindowTextAndCaretPos(const string16& text, | 526 void OmniboxViewViews::SetWindowTextAndCaretPos(const string16& text, |
570 size_t caret_pos, | 527 size_t caret_pos, |
571 bool update_popup, | 528 bool update_popup, |
572 bool notify_text_changed) { | 529 bool notify_text_changed) { |
573 const ui::Range range(caret_pos, caret_pos); | 530 const ui::Range range(caret_pos, caret_pos); |
574 SetTextAndSelectedRange(text, range); | 531 SetTextAndSelectedRange(text, range); |
575 | 532 |
576 if (update_popup) | 533 if (update_popup) |
577 UpdatePopup(); | 534 UpdatePopup(); |
578 | 535 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
610 } else { | 567 } else { |
611 *start = static_cast<size_t>(range.end()); | 568 *start = static_cast<size_t>(range.end()); |
612 *end = static_cast<size_t>(range.start()); | 569 *end = static_cast<size_t>(range.start()); |
613 } | 570 } |
614 } | 571 } |
615 | 572 |
616 void OmniboxViewViews::SelectAll(bool reversed) { | 573 void OmniboxViewViews::SelectAll(bool reversed) { |
617 textfield_->SelectAll(reversed); | 574 textfield_->SelectAll(reversed); |
618 } | 575 } |
619 | 576 |
620 void OmniboxViewViews::RevertAll() { | |
621 ClosePopup(); | |
622 model_->Revert(); | |
623 TextChanged(); | |
624 } | |
625 | |
626 void OmniboxViewViews::UpdatePopup() { | 577 void OmniboxViewViews::UpdatePopup() { |
627 model_->SetInputInProgress(true); | 578 model_->SetInputInProgress(true); |
628 if (ime_candidate_window_open_) | 579 if (ime_candidate_window_open_) |
629 return; | 580 return; |
630 if (!model_->has_focus()) | 581 if (!model_->has_focus()) |
631 return; | 582 return; |
632 | 583 |
633 // Don't inline autocomplete when the caret/selection isn't at the end of | 584 // Don't inline autocomplete when the caret/selection isn't at the end of |
634 // the text, or in the middle of composition. | 585 // the text, or in the middle of composition. |
635 ui::Range sel; | 586 ui::Range sel; |
636 textfield_->GetSelectedRange(&sel); | 587 textfield_->GetSelectedRange(&sel); |
637 bool no_inline_autocomplete = | 588 bool no_inline_autocomplete = |
638 sel.GetMax() < GetTextLength() || textfield_->IsIMEComposing(); | 589 sel.GetMax() < GetTextLength() || textfield_->IsIMEComposing(); |
639 | 590 |
640 model_->StartAutocomplete(!sel.is_empty(), no_inline_autocomplete); | 591 model_->StartAutocomplete(!sel.is_empty(), no_inline_autocomplete); |
641 } | 592 } |
642 | 593 |
643 void OmniboxViewViews::ClosePopup() { | |
644 model_->StopAutocomplete(); | |
645 } | |
646 | |
647 void OmniboxViewViews::SetFocus() { | 594 void OmniboxViewViews::SetFocus() { |
648 // In views-implementation, the focus is on textfield rather than OmniboxView. | 595 // In views-implementation, the focus is on textfield rather than OmniboxView. |
649 textfield_->RequestFocus(); | 596 textfield_->RequestFocus(); |
650 } | 597 } |
651 | 598 |
652 void OmniboxViewViews::OnTemporaryTextMaybeChanged( | 599 void OmniboxViewViews::OnTemporaryTextMaybeChanged( |
653 const string16& display_text, | 600 const string16& display_text, |
654 bool save_original_selection) { | 601 bool save_original_selection) { |
655 if (save_original_selection) | 602 if (save_original_selection) |
656 textfield_->GetSelectedRange(&saved_temporary_selection_); | 603 textfield_->GetSelectedRange(&saved_temporary_selection_); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
722 } | 669 } |
723 | 670 |
724 gfx::NativeView OmniboxViewViews::GetNativeView() const { | 671 gfx::NativeView OmniboxViewViews::GetNativeView() const { |
725 return GetWidget()->GetNativeView(); | 672 return GetWidget()->GetNativeView(); |
726 } | 673 } |
727 | 674 |
728 gfx::NativeView OmniboxViewViews::GetRelativeWindowForPopup() const { | 675 gfx::NativeView OmniboxViewViews::GetRelativeWindowForPopup() const { |
729 return GetWidget()->GetTopLevelWidget()->GetNativeView(); | 676 return GetWidget()->GetTopLevelWidget()->GetNativeView(); |
730 } | 677 } |
731 | 678 |
732 CommandUpdater* OmniboxViewViews::GetCommandUpdater() { | |
733 return command_updater_; | |
734 } | |
735 | |
736 void OmniboxViewViews::SetInstantSuggestion(const string16& input, | 679 void OmniboxViewViews::SetInstantSuggestion(const string16& input, |
737 bool animate_to_complete) { | 680 bool animate_to_complete) { |
738 #if defined(OS_WIN) || defined(USE_AURA) | 681 #if defined(OS_WIN) || defined(USE_AURA) |
739 location_bar_view_->SetInstantSuggestion(input, animate_to_complete); | 682 location_bar_view_->SetInstantSuggestion(input, animate_to_complete); |
740 #endif | 683 #endif |
741 } | 684 } |
742 | 685 |
743 string16 OmniboxViewViews::GetInstantSuggestion() const { | 686 string16 OmniboxViewViews::GetInstantSuggestion() const { |
744 #if defined(OS_WIN) || defined(USE_AURA) | 687 #if defined(OS_WIN) || defined(USE_AURA) |
745 return location_bar_view_->GetInstantSuggestion(); | 688 return location_bar_view_->GetInstantSuggestion(); |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
905 #endif | 848 #endif |
906 | 849 |
907 //////////////////////////////////////////////////////////////////////////////// | 850 //////////////////////////////////////////////////////////////////////////////// |
908 // OmniboxViewViews, private: | 851 // OmniboxViewViews, private: |
909 | 852 |
910 size_t OmniboxViewViews::GetTextLength() const { | 853 size_t OmniboxViewViews::GetTextLength() const { |
911 // TODO(oshima): Support instant, IME. | 854 // TODO(oshima): Support instant, IME. |
912 return textfield_->text().length(); | 855 return textfield_->text().length(); |
913 } | 856 } |
914 | 857 |
858 int OmniboxViewViews::GetOmniboxTextLength() const { | |
859 return static_cast<int>(GetTextLength()); | |
860 } | |
861 | |
915 void OmniboxViewViews::EmphasizeURLComponents() { | 862 void OmniboxViewViews::EmphasizeURLComponents() { |
916 // See whether the contents are a URL with a non-empty host portion, which we | 863 // See whether the contents are a URL with a non-empty host portion, which we |
917 // should emphasize. To check for a URL, rather than using the type returned | 864 // should emphasize. To check for a URL, rather than using the type returned |
918 // by Parse(), ask the model, which will check the desired page transition for | 865 // by Parse(), ask the model, which will check the desired page transition for |
919 // this input. This can tell us whether an UNKNOWN input string is going to | 866 // this input. This can tell us whether an UNKNOWN input string is going to |
920 // be treated as a search or a navigation, and is the same method the Paste | 867 // be treated as a search or a navigation, and is the same method the Paste |
921 // And Go system uses. | 868 // And Go system uses. |
922 string16 text = GetText(); | 869 string16 text = GetText(); |
923 url_parse::Component scheme, host; | 870 url_parse::Component scheme, host; |
924 AutocompleteInput::ParseForEmphasizeComponents(text, model_->GetDesiredTLD(), | 871 AutocompleteInput::ParseForEmphasizeComponents(text, model_->GetDesiredTLD(), |
(...skipping 15 matching lines...) Expand all Loading... | |
940 if (!model_->user_input_in_progress() && scheme.is_nonempty() && | 887 if (!model_->user_input_in_progress() && scheme.is_nonempty() && |
941 (security_level_ != ToolbarModel::NONE)) { | 888 (security_level_ != ToolbarModel::NONE)) { |
942 SkColor security_color = LocationBarView::GetColor( | 889 SkColor security_color = LocationBarView::GetColor( |
943 security_level_, LocationBarView::SECURITY_TEXT); | 890 security_level_, LocationBarView::SECURITY_TEXT); |
944 bool use_strikethrough = (security_level_ == ToolbarModel::SECURITY_ERROR); | 891 bool use_strikethrough = (security_level_ == ToolbarModel::SECURITY_ERROR); |
945 ApplyURLStyle(textfield_, scheme.begin, scheme.end(), | 892 ApplyURLStyle(textfield_, scheme.begin, scheme.end(), |
946 security_color, use_strikethrough); | 893 security_color, use_strikethrough); |
947 } | 894 } |
948 } | 895 } |
949 | 896 |
950 void OmniboxViewViews::TextChanged() { | |
951 EmphasizeURLComponents(); | |
952 model_->OnChanged(); | |
953 } | |
954 | |
955 void OmniboxViewViews::SetTextAndSelectedRange(const string16& text, | 897 void OmniboxViewViews::SetTextAndSelectedRange(const string16& text, |
956 const ui::Range& range) { | 898 const ui::Range& range) { |
957 if (text != GetText()) | 899 if (text != GetText()) |
958 textfield_->SetText(text); | 900 textfield_->SetText(text); |
959 textfield_->SelectRange(range); | 901 textfield_->SelectRange(range); |
960 } | 902 } |
961 | 903 |
962 string16 OmniboxViewViews::GetSelectedText() const { | 904 string16 OmniboxViewViews::GetSelectedText() const { |
963 // TODO(oshima): Support instant, IME. | 905 // TODO(oshima): Support instant, IME. |
964 return textfield_->GetSelectedText(); | 906 return textfield_->GetSelectedText(); |
965 } | 907 } |
OLD | NEW |