OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_ | 5 #ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_ |
6 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_ | 6 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/string16.h" | 9 #include "base/string16.h" |
10 #include "chrome/browser/autocomplete/autocomplete_match.h" | 10 #include "chrome/browser/autocomplete/autocomplete_match.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 | 95 |
96 // Returns the title of the current page. | 96 // Returns the title of the current page. |
97 virtual std::wstring GetTitle() const = 0; | 97 virtual std::wstring GetTitle() const = 0; |
98 | 98 |
99 protected: | 99 protected: |
100 virtual ~AutocompleteEditController(); | 100 virtual ~AutocompleteEditController(); |
101 }; | 101 }; |
102 | 102 |
103 class AutocompleteEditModel : public NotificationObserver { | 103 class AutocompleteEditModel : public NotificationObserver { |
104 public: | 104 public: |
105 enum KeywordUIState { | |
106 // The user is typing normally. | |
107 NORMAL, | |
108 // The user is editing in the middle of the input string. Even if the | |
109 // input looks like a keyword, don't display the keyword UI, as to not | |
110 // interfere with the user's editing. | |
111 NO_KEYWORD, | |
112 // The user has triggered the keyword UI. Until it disappears, bias | |
113 // autocomplete results so that input strings of the keyword alone default | |
114 // to the keyword provider, not a normal navigation or search. | |
115 KEYWORD, | |
116 }; | |
117 | |
118 struct State { | 105 struct State { |
119 State(bool user_input_in_progress, | 106 State(bool user_input_in_progress, |
120 const std::wstring& user_text, | 107 const std::wstring& user_text, |
121 const std::wstring& keyword, | 108 const std::wstring& keyword, |
122 bool is_keyword_hint, | 109 bool is_keyword_hint); |
123 KeywordUIState keyword_ui_state); | |
124 ~State(); | 110 ~State(); |
125 | 111 |
126 bool user_input_in_progress; | 112 bool user_input_in_progress; |
127 const std::wstring user_text; | 113 const std::wstring user_text; |
128 const std::wstring keyword; | 114 const std::wstring keyword; |
129 const bool is_keyword_hint; | 115 const bool is_keyword_hint; |
130 const KeywordUIState keyword_ui_state; | |
131 }; | 116 }; |
132 | 117 |
133 AutocompleteEditModel(AutocompleteEditView* view, | 118 AutocompleteEditModel(AutocompleteEditView* view, |
134 AutocompleteEditController* controller, | 119 AutocompleteEditController* controller, |
135 Profile* profile); | 120 Profile* profile); |
136 ~AutocompleteEditModel(); | 121 ~AutocompleteEditModel(); |
137 | 122 |
138 void SetPopupModel(AutocompletePopupModel* popup_model); | 123 void SetPopupModel(AutocompletePopupModel* popup_model); |
139 | 124 |
140 // TODO: The edit and popup should be siblings owned by the LocationBarView, | 125 // TODO: The edit and popup should be siblings owned by the LocationBarView, |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 WindowOpenDisposition disposition, | 233 WindowOpenDisposition disposition, |
249 PageTransition::Type transition, | 234 PageTransition::Type transition, |
250 const GURL& alternate_nav_url, | 235 const GURL& alternate_nav_url, |
251 size_t index, | 236 size_t index, |
252 const std::wstring& keyword); | 237 const std::wstring& keyword); |
253 | 238 |
254 bool has_focus() const { return has_focus_; } | 239 bool has_focus() const { return has_focus_; } |
255 | 240 |
256 // Accessors for keyword-related state (see comments on keyword_ and | 241 // Accessors for keyword-related state (see comments on keyword_ and |
257 // is_keyword_hint_). | 242 // is_keyword_hint_). |
258 std::wstring keyword() const { | 243 const std::wstring& keyword() const { return keyword_; } |
259 return (is_keyword_hint_ || (keyword_ui_state_ != NO_KEYWORD)) ? | |
260 keyword_ : std::wstring(); | |
261 } | |
262 bool is_keyword_hint() const { return is_keyword_hint_; } | 244 bool is_keyword_hint() const { return is_keyword_hint_; } |
263 | 245 |
264 // Accepts the current keyword hint as a keyword. | 246 // Accepts the current keyword hint as a keyword. It always returns true for |
265 void AcceptKeyword(); | 247 // caller convenience. |
| 248 bool AcceptKeyword(); |
266 | 249 |
267 // Clears the current keyword. |visible_text| is the (non-keyword) text | 250 // Clears the current keyword. |visible_text| is the (non-keyword) text |
268 // currently visible in the edit. | 251 // currently visible in the edit. |
269 void ClearKeyword(const std::wstring& visible_text); | 252 void ClearKeyword(const std::wstring& visible_text); |
270 | 253 |
271 // Returns true if a query to an autocomplete provider is currently | 254 // Returns true if a query to an autocomplete provider is currently |
272 // in progress. This logic should in the future live in | 255 // in progress. This logic should in the future live in |
273 // AutocompleteController but resides here for now. This method is used by | 256 // AutocompleteController but resides here for now. This method is used by |
274 // AutomationProvider::AutocompleteEditIsQueryInProgress. | 257 // AutomationProvider::AutocompleteEditIsQueryInProgress. |
275 bool query_in_progress() const; | 258 bool query_in_progress() const; |
(...skipping 11 matching lines...) Expand all Loading... |
287 void OnKillFocus(); | 270 void OnKillFocus(); |
288 | 271 |
289 // Called when the user presses the escape key. Decides what, if anything, to | 272 // Called when the user presses the escape key. Decides what, if anything, to |
290 // revert about any current edits. Returns whether the key was handled. | 273 // revert about any current edits. Returns whether the key was handled. |
291 bool OnEscapeKeyPressed(); | 274 bool OnEscapeKeyPressed(); |
292 | 275 |
293 // Called when the user presses or releases the control key. Changes state as | 276 // Called when the user presses or releases the control key. Changes state as |
294 // necessary. | 277 // necessary. |
295 void OnControlKeyChanged(bool pressed); | 278 void OnControlKeyChanged(bool pressed); |
296 | 279 |
297 // Called when the user pastes in text that replaces the entire edit contents. | 280 // Called when the user pastes in text. |
298 void on_paste_replacing_all() { paste_state_ = REPLACING_ALL; } | 281 void on_paste() { paste_state_ = PASTING; } |
299 | 282 |
300 // Called when the user presses up or down. |count| is a repeat count, | 283 // Called when the user presses up or down. |count| is a repeat count, |
301 // negative for moving up, positive for moving down. | 284 // negative for moving up, positive for moving down. |
302 void OnUpOrDownKeyPressed(int count); | 285 void OnUpOrDownKeyPressed(int count); |
303 | 286 |
304 // Called when any relevant data changes. This rolls together several | 287 // Called when any relevant data changes. This rolls together several |
305 // separate pieces of data into one call so we can update all the UI | 288 // separate pieces of data into one call so we can update all the UI |
306 // efficiently: | 289 // efficiently: |
307 // |text| is either the new temporary text from the user manually selecting | 290 // |text| is either the new temporary text from the user manually selecting |
308 // a different match, or the inline autocomplete text. We distinguish by | 291 // a different match, or the inline autocomplete text. We distinguish by |
(...skipping 22 matching lines...) Expand all Loading... |
331 bool selection_differs, | 314 bool selection_differs, |
332 bool text_differs, | 315 bool text_differs, |
333 bool just_deleted_text, | 316 bool just_deleted_text, |
334 bool allow_keyword_ui_change); | 317 bool allow_keyword_ui_change); |
335 | 318 |
336 // Invoked when the popup is going to change its bounds to |bounds|. | 319 // Invoked when the popup is going to change its bounds to |bounds|. |
337 void PopupBoundsChangedTo(const gfx::Rect& bounds); | 320 void PopupBoundsChangedTo(const gfx::Rect& bounds); |
338 | 321 |
339 private: | 322 private: |
340 enum PasteState { | 323 enum PasteState { |
341 NONE, // Most recent edit was not a paste that replaced all text. | 324 NONE, // Most recent edit was not a paste. |
342 REPLACED_ALL, // Most recent edit was a paste that replaced all text. | 325 PASTING, // In the middle of doing a paste. We need this intermediate |
343 REPLACING_ALL, // In the middle of doing a paste that replaces all | 326 // state because OnPaste() does the actual detection of |
344 // text. We need this intermediate state because OnPaste() | 327 // paste, but OnAfterPossibleChange() has to update the |
345 // does the actual detection of such pastes, but | 328 // paste state for every edit. If OnPaste() set the state |
346 // OnAfterPossibleChange() has to update the paste state | 329 // directly to PASTED, OnAfterPossibleChange() wouldn't know |
347 // for every edit. If OnPaste() set the state directly to | |
348 // REPLACED_ALL, OnAfterPossibleChange() wouldn't know | |
349 // whether that represented the current edit or a past one. | 330 // whether that represented the current edit or a past one. |
| 331 PASTED, // Most recent edit was a paste. |
350 }; | 332 }; |
351 | 333 |
352 enum ControlKeyState { | 334 enum ControlKeyState { |
353 UP, // The control key is not depressed. | 335 UP, // The control key is not depressed. |
354 DOWN_WITHOUT_CHANGE, // The control key is depressed, and the edit's | 336 DOWN_WITHOUT_CHANGE, // The control key is depressed, and the edit's |
355 // contents/selection have not changed since it was | 337 // contents/selection have not changed since it was |
356 // depressed. This is the only state in which we | 338 // depressed. This is the only state in which we |
357 // do the "ctrl-enter" behavior when the user hits | 339 // do the "ctrl-enter" behavior when the user hits |
358 // enter. | 340 // enter. |
359 DOWN_WITH_CHANGE, // The control key is depressed, and the edit's | 341 DOWN_WITH_CHANGE, // The control key is depressed, and the edit's |
(...skipping 26 matching lines...) Expand all Loading... |
386 GURL* alternate_nav_url) const; | 368 GURL* alternate_nav_url) const; |
387 | 369 |
388 // Returns true if |text| (which is display text in the current context) | 370 // Returns true if |text| (which is display text in the current context) |
389 // parses as a URL, and in that case sets |url| to the calculated URL. | 371 // parses as a URL, and in that case sets |url| to the calculated URL. |
390 // Subtle note: This ignores the desired_tld_ (unlike GetDataForURLExport() | 372 // Subtle note: This ignores the desired_tld_ (unlike GetDataForURLExport() |
391 // and CurrentTextIsURL()). The view needs this because it calls this | 373 // and CurrentTextIsURL()). The view needs this because it calls this |
392 // function during copy handling, when the control key is down to trigger the | 374 // function during copy handling, when the control key is down to trigger the |
393 // copy. | 375 // copy. |
394 bool GetURLForText(const std::wstring& text, GURL* url) const; | 376 bool GetURLForText(const std::wstring& text, GURL* url) const; |
395 | 377 |
| 378 // Accepts current keyword if the user only typed a space at the end of |
| 379 // |new_user_text|. Returns true if the current keyword is accepted. |
| 380 bool MaybeAcceptKeywordBySpace(const std::wstring& new_user_text); |
| 381 |
| 382 // Checks if a given character is a valid space character for accepting |
| 383 // keyword. |
| 384 static bool IsSpaceCharForAcceptingKeyword(wchar_t c); |
| 385 |
396 AutocompleteEditView* view_; | 386 AutocompleteEditView* view_; |
397 | 387 |
398 AutocompletePopupModel* popup_; | 388 AutocompletePopupModel* popup_; |
399 | 389 |
400 AutocompleteEditController* controller_; | 390 AutocompleteEditController* controller_; |
401 | 391 |
402 NotificationRegistrar registrar_; | 392 NotificationRegistrar registrar_; |
403 | 393 |
404 // Whether the edit has focus. | 394 // Whether the edit has focus. |
405 bool has_focus_; | 395 bool has_focus_; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 // When the user hits <esc>, the edit box reverts to "goog". Hit <esc> again | 444 // When the user hits <esc>, the edit box reverts to "goog". Hit <esc> again |
455 // and the popup is closed and "goog" is replaced by the permanent_text_, | 445 // and the popup is closed and "goog" is replaced by the permanent_text_, |
456 // which is the URL of the current page. | 446 // which is the URL of the current page. |
457 // | 447 // |
458 // original_url_ is only valid when there is temporary text, and is used as | 448 // original_url_ is only valid when there is temporary text, and is used as |
459 // the unique identifier of the originally selected item. Thus, if the user | 449 // the unique identifier of the originally selected item. Thus, if the user |
460 // arrows to a different item with the same text, we can still distinguish | 450 // arrows to a different item with the same text, we can still distinguish |
461 // them and not revert all the way to the permanent_text_. | 451 // them and not revert all the way to the permanent_text_. |
462 bool has_temporary_text_; | 452 bool has_temporary_text_; |
463 GURL original_url_; | 453 GURL original_url_; |
464 KeywordUIState original_keyword_ui_state_; | |
465 | 454 |
466 // When the user's last action was to paste and replace all the text, we | 455 // When the user's last action was to paste, we disallow inline autocomplete |
467 // disallow inline autocomplete (on the theory that the user is trying to | 456 // (on the theory that the user is trying to paste in a new URL or part of |
468 // paste in a new URL or part of one, and in either case inline autocomplete | 457 // one, and in either case inline autocomplete would get in the way). |
469 // would get in the way). | |
470 PasteState paste_state_; | 458 PasteState paste_state_; |
471 | 459 |
472 // Whether the control key is depressed. We track this to avoid calling | 460 // Whether the control key is depressed. We track this to avoid calling |
473 // UpdatePopup() repeatedly if the user holds down the key, and to know | 461 // UpdatePopup() repeatedly if the user holds down the key, and to know |
474 // whether to trigger "ctrl-enter" behavior. | 462 // whether to trigger "ctrl-enter" behavior. |
475 ControlKeyState control_key_state_; | 463 ControlKeyState control_key_state_; |
476 | 464 |
477 // The keyword associated with the current match. The user may have an actual | 465 // The keyword associated with the current match. The user may have an actual |
478 // selected keyword, or just some input text that looks like a keyword (so we | 466 // selected keyword, or just some input text that looks like a keyword (so we |
479 // can show a hint to press <tab>). This is the keyword in either case; | 467 // can show a hint to press <tab>). This is the keyword in either case; |
480 // is_keyword_hint_ (below) distinguishes the two cases. | 468 // is_keyword_hint_ (below) distinguishes the two cases. |
481 std::wstring keyword_; | 469 std::wstring keyword_; |
482 | 470 |
483 // True if the keyword associated with this match is merely a hint, i.e. the | 471 // True if the keyword associated with this match is merely a hint, i.e. the |
484 // user hasn't actually selected a keyword yet. When this is true, we can use | 472 // user hasn't actually selected a keyword yet. When this is true, we can use |
485 // keyword_ to show a "Press <tab> to search" sort of hint. | 473 // keyword_ to show a "Press <tab> to search" sort of hint. |
486 bool is_keyword_hint_; | 474 bool is_keyword_hint_; |
487 | 475 |
488 // See KeywordUIState enum. | |
489 KeywordUIState keyword_ui_state_; | |
490 | |
491 // Paste And Go-related state. See CanPasteAndGo(). | 476 // Paste And Go-related state. See CanPasteAndGo(). |
492 mutable GURL paste_and_go_url_; | 477 mutable GURL paste_and_go_url_; |
493 mutable PageTransition::Type paste_and_go_transition_; | 478 mutable PageTransition::Type paste_and_go_transition_; |
494 mutable GURL paste_and_go_alternate_nav_url_; | 479 mutable GURL paste_and_go_alternate_nav_url_; |
495 | 480 |
496 Profile* profile_; | 481 Profile* profile_; |
497 | 482 |
498 DISALLOW_COPY_AND_ASSIGN(AutocompleteEditModel); | 483 DISALLOW_COPY_AND_ASSIGN(AutocompleteEditModel); |
499 }; | 484 }; |
500 | 485 |
501 #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_ | 486 #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_ |
OLD | NEW |