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