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/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 #include "ui/aura/root_window.h" | 51 #include "ui/aura/root_window.h" |
52 #include "ui/compositor/layer.h" | 52 #include "ui/compositor/layer.h" |
53 #endif | 53 #endif |
54 | 54 |
55 using content::WebContents; | 55 using content::WebContents; |
56 | 56 |
57 namespace { | 57 namespace { |
58 | 58 |
59 // Stores omnibox state for each tab. | 59 // Stores omnibox state for each tab. |
60 struct ViewState { | 60 struct ViewState { |
61 explicit ViewState(const gfx::SelectionModel& selection_model) | 61 explicit ViewState(const gfx::SelectionModel& selection_model, |
62 : selection_model(selection_model) { | 62 bool is_focus_visible) |
| 63 : selection_model(selection_model), |
| 64 is_focus_visible(is_focus_visible) { |
63 } | 65 } |
64 | 66 |
65 // SelectionModel of selected text. | 67 // SelectionModel of selected text. |
66 gfx::SelectionModel selection_model; | 68 gfx::SelectionModel selection_model; |
| 69 // Is the focus visible? |
| 70 bool is_focus_visible; |
67 }; | 71 }; |
68 | 72 |
69 const char kAutocompleteEditStateKey[] = "AutocompleteEditState"; | 73 const char kAutocompleteEditStateKey[] = "AutocompleteEditState"; |
70 | 74 |
71 struct AutocompleteEditState : public base::SupportsUserData::Data { | 75 struct AutocompleteEditState : public base::SupportsUserData::Data { |
72 AutocompleteEditState(const OmniboxEditModel::State& model_state, | 76 AutocompleteEditState(const OmniboxEditModel::State& model_state, |
73 const ViewState& view_state) | 77 const ViewState& view_state) |
74 : model_state(model_state), | 78 : model_state(model_state), |
75 view_state(view_state) { | 79 view_state(view_state) { |
76 } | 80 } |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 std::string OmniboxViewViews::GetClassName() const { | 437 std::string OmniboxViewViews::GetClassName() const { |
434 return kViewClassName; | 438 return kViewClassName; |
435 } | 439 } |
436 | 440 |
437 void OmniboxViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 441 void OmniboxViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
438 if (popup_view_->IsOpen()) | 442 if (popup_view_->IsOpen()) |
439 popup_view_->UpdatePopupAppearance(); | 443 popup_view_->UpdatePopupAppearance(); |
440 } | 444 } |
441 | 445 |
442 //////////////////////////////////////////////////////////////////////////////// | 446 //////////////////////////////////////////////////////////////////////////////// |
443 // OmniboxViewViews, AutocopmleteEditView implementation: | 447 // OmniboxViewViews, AutocompleteEditView implementation: |
444 | 448 |
445 void OmniboxViewViews::SaveStateToTab(WebContents* tab) { | 449 void OmniboxViewViews::SaveStateToTab(WebContents* tab) { |
446 DCHECK(tab); | 450 DCHECK(tab); |
447 | 451 |
448 // We don't want to keep the IME status, so force quit the current | 452 // We don't want to keep the IME status, so force quit the current |
449 // session here. It may affect the selection status, so order is | 453 // session here. It may affect the selection status, so order is |
450 // also important. | 454 // also important. |
451 if (textfield_->IsIMEComposing()) { | 455 if (textfield_->IsIMEComposing()) { |
452 textfield_->GetTextInputClient()->ConfirmCompositionText(); | 456 textfield_->GetTextInputClient()->ConfirmCompositionText(); |
453 textfield_->GetInputMethod()->CancelComposition(textfield_); | 457 textfield_->GetInputMethod()->CancelComposition(textfield_); |
454 } | 458 } |
455 | 459 |
456 // NOTE: GetStateForTabSwitch may affect GetSelection, so order is important. | 460 // NOTE: GetStateForTabSwitch may affect GetSelection, so order is important. |
457 OmniboxEditModel::State model_state = model()->GetStateForTabSwitch(); | 461 OmniboxEditModel::State model_state = model()->GetStateForTabSwitch(); |
458 gfx::SelectionModel selection; | 462 gfx::SelectionModel selection; |
459 textfield_->GetSelectionModel(&selection); | 463 textfield_->GetSelectionModel(&selection); |
460 tab->SetUserData( | 464 tab->SetUserData( |
461 kAutocompleteEditStateKey, | 465 kAutocompleteEditStateKey, |
462 new AutocompleteEditState(model_state, ViewState(selection))); | 466 new AutocompleteEditState( |
| 467 model_state, ViewState(selection, model()->is_focus_visible()))); |
463 } | 468 } |
464 | 469 |
465 void OmniboxViewViews::Update(const WebContents* contents) { | 470 void OmniboxViewViews::Update(const WebContents* contents) { |
466 // NOTE: We're getting the URL text here from the ToolbarModel. | 471 // NOTE: We're getting the URL text here from the ToolbarModel. |
467 bool visibly_changed_permanent_text = | 472 bool visibly_changed_permanent_text = |
468 model()->UpdatePermanentText(toolbar_model()->GetText(true)); | 473 model()->UpdatePermanentText(toolbar_model()->GetText(true)); |
469 ToolbarModel::SecurityLevel security_level = | 474 ToolbarModel::SecurityLevel security_level = |
470 toolbar_model()->GetSecurityLevel(); | 475 toolbar_model()->GetSecurityLevel(); |
471 bool changed_security_level = (security_level != security_level_); | 476 bool changed_security_level = (security_level != security_level_); |
472 security_level_ = security_level; | 477 security_level_ = security_level; |
473 | 478 |
474 // TODO(oshima): Copied from gtk implementation which is | 479 // TODO(oshima): Copied from gtk implementation which is |
475 // slightly different from WIN impl. Find out the correct implementation | 480 // slightly different from WIN impl. Find out the correct implementation |
476 // for views-implementation. | 481 // for views-implementation. |
477 if (contents) { | 482 if (contents) { |
478 RevertAll(); | 483 RevertAll(); |
479 const AutocompleteEditState* state = static_cast<AutocompleteEditState*>( | 484 const AutocompleteEditState* state = static_cast<AutocompleteEditState*>( |
480 contents->GetUserData(&kAutocompleteEditStateKey)); | 485 contents->GetUserData(&kAutocompleteEditStateKey)); |
481 if (state) { | 486 if (state) { |
482 model()->RestoreState(state->model_state); | 487 model()->RestoreState(state->model_state); |
| 488 SetFocusVisibility(state->view_state.is_focus_visible); |
483 | 489 |
484 // Move the marks for the cursor and the other end of the selection to | 490 // Move the marks for the cursor and the other end of the selection to |
485 // the previously-saved offsets (but preserve PRIMARY). | 491 // the previously-saved offsets (but preserve PRIMARY). |
486 textfield_->SelectSelectionModel(state->view_state.selection_model); | 492 textfield_->SelectSelectionModel(state->view_state.selection_model); |
487 // We do not carry over the current edit history to another tab. | 493 // We do not carry over the current edit history to another tab. |
488 // TODO(oshima): consider saving/restoring edit history. | 494 // TODO(oshima): consider saving/restoring edit history. |
489 textfield_->ClearEditHistory(); | 495 textfield_->ClearEditHistory(); |
490 } | 496 } |
491 } else if (visibly_changed_permanent_text) { | 497 } else if (visibly_changed_permanent_text) { |
492 RevertAll(); | 498 RevertAll(); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 // the text, or in the middle of composition. | 568 // the text, or in the middle of composition. |
563 ui::Range sel; | 569 ui::Range sel; |
564 textfield_->GetSelectedRange(&sel); | 570 textfield_->GetSelectedRange(&sel); |
565 bool no_inline_autocomplete = | 571 bool no_inline_autocomplete = |
566 sel.GetMax() < GetTextLength() || textfield_->IsIMEComposing(); | 572 sel.GetMax() < GetTextLength() || textfield_->IsIMEComposing(); |
567 | 573 |
568 model()->StartAutocomplete(!sel.is_empty(), no_inline_autocomplete); | 574 model()->StartAutocomplete(!sel.is_empty(), no_inline_autocomplete); |
569 } | 575 } |
570 | 576 |
571 void OmniboxViewViews::SetFocus() { | 577 void OmniboxViewViews::SetFocus() { |
| 578 SetFocusVisibility(true); |
572 // In views-implementation, the focus is on textfield rather than OmniboxView. | 579 // In views-implementation, the focus is on textfield rather than OmniboxView. |
573 textfield_->RequestFocus(); | 580 textfield_->RequestFocus(); |
574 } | 581 } |
| 582 |
| 583 void OmniboxViewViews::SetInvisibleFocus() { |
| 584 SetFocusVisibility(false); |
| 585 // In views-implementation, the focus is on textfield rather than OmniboxView. |
| 586 textfield_->RequestFocus(); |
| 587 } |
575 | 588 |
576 void OmniboxViewViews::OnTemporaryTextMaybeChanged( | 589 void OmniboxViewViews::OnTemporaryTextMaybeChanged( |
577 const string16& display_text, | 590 const string16& display_text, |
578 bool save_original_selection) { | 591 bool save_original_selection) { |
579 if (save_original_selection) | 592 if (save_original_selection) |
580 textfield_->GetSelectedRange(&saved_temporary_selection_); | 593 textfield_->GetSelectedRange(&saved_temporary_selection_); |
581 | 594 |
582 SetWindowTextAndCaretPos(display_text, display_text.length(), false, true); | 595 SetWindowTextAndCaretPos(display_text, display_text.length(), false, true); |
583 } | 596 } |
584 | 597 |
585 bool OmniboxViewViews::OnInlineAutocompleteTextMaybeChanged( | 598 bool OmniboxViewViews::OnInlineAutocompleteTextMaybeChanged( |
586 const string16& display_text, | 599 const string16& display_text, |
587 size_t user_text_length) { | 600 size_t user_text_length) { |
588 if (display_text == GetText()) | 601 if (display_text == GetText()) |
589 return false; | 602 return false; |
590 ui::Range range(display_text.size(), user_text_length); | 603 ui::Range range(display_text.size(), user_text_length); |
591 SetTextAndSelectedRange(display_text, range); | 604 SetTextAndSelectedRange(display_text, range); |
592 TextChanged(); | 605 TextChanged(); |
593 return true; | 606 return true; |
594 } | 607 } |
595 | 608 |
596 void OmniboxViewViews::OnRevertTemporaryText() { | 609 void OmniboxViewViews::OnRevertTemporaryText() { |
597 textfield_->SelectRange(saved_temporary_selection_); | 610 textfield_->SelectRange(saved_temporary_selection_); |
598 TextChanged(); | 611 TextChanged(); |
599 } | 612 } |
600 | 613 |
601 void OmniboxViewViews::OnBeforePossibleChange() { | 614 void OmniboxViewViews::OnBeforePossibleChange() { |
| 615 SetFocusVisibility(true); |
602 // Record our state. | 616 // Record our state. |
603 text_before_change_ = GetText(); | 617 text_before_change_ = GetText(); |
604 textfield_->GetSelectedRange(&sel_before_change_); | 618 textfield_->GetSelectedRange(&sel_before_change_); |
605 ime_composing_before_change_ = textfield_->IsIMEComposing(); | 619 ime_composing_before_change_ = textfield_->IsIMEComposing(); |
606 } | 620 } |
607 | 621 |
608 bool OmniboxViewViews::OnAfterPossibleChange() { | 622 bool OmniboxViewViews::OnAfterPossibleChange() { |
609 ui::Range new_sel; | 623 ui::Range new_sel; |
610 textfield_->GetSelectedRange(&new_sel); | 624 textfield_->GetSelectedRange(&new_sel); |
611 | 625 |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
929 if (!text.empty()) { | 943 if (!text.empty()) { |
930 // Record this paste, so we can do different behavior. | 944 // Record this paste, so we can do different behavior. |
931 model()->on_paste(); | 945 model()->on_paste(); |
932 // Force a Paste operation to trigger the text_changed code in | 946 // Force a Paste operation to trigger the text_changed code in |
933 // OnAfterPossibleChange(), even if identical contents are pasted into the | 947 // OnAfterPossibleChange(), even if identical contents are pasted into the |
934 // text box. | 948 // text box. |
935 text_before_change_.clear(); | 949 text_before_change_.clear(); |
936 textfield_->ReplaceSelection(text); | 950 textfield_->ReplaceSelection(text); |
937 } | 951 } |
938 } | 952 } |
| 953 |
| 954 void OmniboxViewViews::SetFocusVisibility(bool is_focus_visible) { |
| 955 DVLOG(1) << "SetFocusVisibility(" << is_focus_visible << ");"; |
| 956 model()->OnFocusVisibilityChange(is_focus_visible); |
| 957 // TODO(mathp): actually make focus invisible |
| 958 } |
OLD | NEW |