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.
5 #ifndef CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_
6 #define CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "chrome/browser/ui/omnibox/omnibox_view.h"
15 #include "chrome/browser/ui/toolbar/toolbar_model.h"
16 #include "ui/base/window_open_disposition.h"
17 #include "ui/gfx/range/range.h"
18 #include "ui/views/controls/textfield/textfield.h"
19 #include "ui/views/controls/textfield/textfield_controller.h"
21 #if defined(OS_CHROMEOS)
22 #include "ui/base/ime/chromeos/input_method_manager.h"
25 class LocationBarView
;
26 class OmniboxPopupView
;
37 // Views-implementation of OmniboxView.
38 class OmniboxViewViews
40 public views::Textfield
,
41 #if defined(OS_CHROMEOS)
43 chromeos::input_method::InputMethodManager::CandidateWindowObserver
,
45 public views::TextfieldController
{
47 // The internal view class name.
48 static const char kViewClassName
[];
50 OmniboxViewViews(OmniboxEditController
* controller
,
52 CommandUpdater
* command_updater
,
53 bool popup_window_mode
,
54 LocationBarView
* location_bar
,
55 const gfx::FontList
& font_list
);
56 ~OmniboxViewViews() override
;
58 // Initialize, create the underlying views, etc.
61 // Exposes the RenderText for tests.
62 #if defined(UNIT_TEST)
63 gfx::RenderText
* GetRenderText() {
64 return views::Textfield::GetRenderText();
68 // For use when switching tabs, this saves the current state onto the tab so
69 // that it can be restored during a later call to Update().
70 void SaveStateToTab(content::WebContents
* tab
);
72 // Called when the window's active tab changes.
73 void OnTabChanged(const content::WebContents
* web_contents
);
75 // Called to clear the saved state for |web_contents|.
76 void ResetTabState(content::WebContents
* web_contents
);
79 void Update() override
;
80 base::string16
GetText() const override
;
81 void SetUserText(const base::string16
& text
,
82 const base::string16
& display_text
,
83 bool update_popup
) override
;
84 void SetForcedQuery() override
;
85 void GetSelectionBounds(base::string16::size_type
* start
,
86 base::string16::size_type
* end
) const override
;
87 void SelectAll(bool reversed
) override
;
88 void RevertAll() override
;
89 void SetFocus() override
;
90 int GetTextWidth() const override
;
91 bool IsImeComposing() const override
;
94 gfx::Size
GetMinimumSize() const override
;
95 void OnPaint(gfx::Canvas
* canvas
) override
;
96 void OnNativeThemeChanged(const ui::NativeTheme
* theme
) override
;
97 void ExecuteCommand(int command_id
, int event_flags
) override
;
100 FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest
, CloseOmniboxPopupOnTextDrag
);
102 // Update the field with |text| and set the selection.
103 void SetTextAndSelectedRange(const base::string16
& text
,
104 const gfx::Range
& range
);
106 // Returns the selected text.
107 base::string16
GetSelectedText() const;
109 // Paste text from the clipboard into the omnibox.
110 // Textfields implementation of Paste() pastes the contents of the clipboard
111 // as is. We want to strip whitespace and other things (see GetClipboardText()
112 // for details). The function invokes OnBefore/AfterPossibleChange() as
116 // Handle keyword hint tab-to-search and tabbing through dropdown results.
117 bool HandleEarlyTabActions(const ui::KeyEvent
& event
);
119 // Handles a request to change the value of this text field from software
120 // using an accessibility API (typically automation software, screen readers
121 // don't normally use this). Sets the value and clears the selection.
122 void AccessibilitySetValue(const base::string16
& new_value
);
125 void SetWindowTextAndCaretPos(const base::string16
& text
,
128 bool notify_text_changed
) override
;
129 bool IsSelectAll() const override
;
130 bool DeleteAtEndPressed() override
;
131 void UpdatePopup() override
;
132 void ApplyCaretVisibility() override
;
133 void OnTemporaryTextMaybeChanged(const base::string16
& display_text
,
134 bool save_original_selection
,
135 bool notify_text_changed
) override
;
136 bool OnInlineAutocompleteTextMaybeChanged(const base::string16
& display_text
,
137 size_t user_text_length
) override
;
138 void OnInlineAutocompleteTextCleared() override
;
139 void OnRevertTemporaryText() override
;
140 void OnBeforePossibleChange() override
;
141 bool OnAfterPossibleChange() override
;
142 gfx::NativeView
GetNativeView() const override
;
143 gfx::NativeView
GetRelativeWindowForPopup() const override
;
144 void SetGrayTextAutocompletion(const base::string16
& input
) override
;
145 base::string16
GetGrayTextAutocompletion() const override
;
146 int GetWidth() const override
;
147 bool IsImeShowingPopup() const override
;
148 void ShowImeIfNeeded() override
;
149 void OnMatchOpened(const AutocompleteMatch
& match
) override
;
150 int GetOmniboxTextLength() const override
;
151 void EmphasizeURLComponents() override
;
154 bool OnKeyReleased(const ui::KeyEvent
& event
) override
;
155 bool IsItemForCommandIdDynamic(int command_id
) const override
;
156 base::string16
GetLabelForCommandId(int command_id
) const override
;
157 const char* GetClassName() const override
;
158 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
159 bool OnMouseDragged(const ui::MouseEvent
& event
) override
;
160 void OnMouseReleased(const ui::MouseEvent
& event
) override
;
161 bool OnKeyPressed(const ui::KeyEvent
& event
) override
;
162 void OnGestureEvent(ui::GestureEvent
* event
) override
;
163 void AboutToRequestFocusFromTabTraversal(bool reverse
) override
;
164 bool SkipDefaultKeyEventProcessing(const ui::KeyEvent
& event
) override
;
165 void GetAccessibleState(ui::AXViewState
* state
) override
;
166 void OnFocus() override
;
167 void OnBlur() override
;
168 bool IsCommandIdEnabled(int command_id
) const override
;
169 base::string16
GetSelectionClipboardText() const override
;
170 void DoInsertChar(base::char16 ch
) override
;
172 // chromeos::input_method::InputMethodManager::CandidateWindowObserver:
173 #if defined(OS_CHROMEOS)
174 void CandidateWindowOpened(
175 chromeos::input_method::InputMethodManager
* manager
) override
;
176 void CandidateWindowClosed(
177 chromeos::input_method::InputMethodManager
* manager
) override
;
180 // views::TextfieldController:
181 void ContentsChanged(views::Textfield
* sender
,
182 const base::string16
& new_contents
) override
;
183 bool HandleKeyEvent(views::Textfield
* sender
,
184 const ui::KeyEvent
& key_event
) override
;
185 void OnBeforeUserAction(views::Textfield
* sender
) override
;
186 void OnAfterUserAction(views::Textfield
* sender
) override
;
187 void OnAfterCutOrCopy(ui::ClipboardType clipboard_type
) override
;
188 void OnWriteDragData(ui::OSExchangeData
* data
) override
;
189 void OnGetDragOperationsForTextfield(int* drag_operations
) override
;
190 void AppendDropFormats(
192 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) override
;
193 int OnDrop(const ui::OSExchangeData
& data
) override
;
194 void UpdateContextMenu(ui::SimpleMenuModel
* menu_contents
) override
;
196 // When true, the location bar view is read only and also is has a slightly
197 // different presentation (smaller font size). This is used for popups.
198 bool popup_window_mode_
;
200 scoped_ptr
<OmniboxPopupView
> popup_view_
;
202 connection_security::SecurityLevel security_level_
;
204 // Selection persisted across temporary text changes, like popup suggestions.
205 gfx::Range saved_temporary_selection_
;
207 // Holds the user's selection across focus changes. There is only a saved
208 // selection if this range IsValid().
209 gfx::Range saved_selection_for_focus_change_
;
211 // Tracking state before and after a possible change.
212 base::string16 text_before_change_
;
213 gfx::Range sel_before_change_
;
214 bool ime_composing_before_change_
;
216 // Was the delete key pressed with an empty selection at the end of the edit?
217 bool delete_at_end_pressed_
;
219 // |location_bar_view_| can be NULL in tests.
220 LocationBarView
* location_bar_view_
;
222 // True if the IME candidate window is open. When this is true, we want to
223 // avoid showing the popup. So far, the candidate window is detected only
225 bool ime_candidate_window_open_
;
227 // Should we select all the text when we see the mouse button get released?
228 // We select in response to a click that focuses the omnibox, but we defer
229 // until release, setting this variable back to false if we saw a drag, to
230 // allow the user to select just a portion of the text.
231 bool select_all_on_mouse_release_
;
233 // Indicates if we want to select all text in the omnibox when we get a
234 // GESTURE_TAP. We want to select all only when the textfield is not in focus
235 // and gets a tap. So we use this variable to remember focus state before tap.
236 bool select_all_on_gesture_tap_
;
238 // The time of the first character insert operation that has not yet been
239 // painted. Used to measure omnibox responsiveness with a histogram.
240 base::TimeTicks insert_char_time_
;
242 // Used to bind callback functions to this object.
243 base::WeakPtrFactory
<OmniboxViewViews
> weak_ptr_factory_
;
245 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViews
);
248 #endif // CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_