| OLD | NEW |
| 1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_MODEL_H_ | 5 #ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_MODEL_H_ |
| 6 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_MODEL_H_ | 6 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_MODEL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete.h" | 10 #include "chrome/browser/autocomplete/autocomplete.h" |
| 11 #include "chrome/browser/autocomplete/autocomplete_edit.h" | 11 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
| 12 | 12 |
| 13 class AutocompletePopupView; | 13 class AutocompletePopupView; |
| 14 class SkBitmap; | 14 class SkBitmap; |
| 15 | 15 |
| 16 class AutocompletePopupModel { | 16 class AutocompletePopupModel { |
| 17 public: | 17 public: |
| 18 // See selected_line_state_ for details. |
| 19 enum LineState { |
| 20 NORMAL = 0, |
| 21 KEYWORD |
| 22 }; |
| 23 |
| 18 AutocompletePopupModel(AutocompletePopupView* popup_view, | 24 AutocompletePopupModel(AutocompletePopupView* popup_view, |
| 19 AutocompleteEditModel* edit_model); | 25 AutocompleteEditModel* edit_model); |
| 20 ~AutocompletePopupModel(); | 26 ~AutocompletePopupModel(); |
| 21 | 27 |
| 22 // Returns true if the popup is currently open. | 28 // Returns true if the popup is currently open. |
| 23 bool IsOpen() const; | 29 bool IsOpen() const; |
| 24 | 30 |
| 25 AutocompletePopupView* view() const { return view_; } | 31 AutocompletePopupView* view() const { return view_; } |
| 26 | 32 |
| 27 // Returns the AutocompleteController used by this popup. | 33 // Returns the AutocompleteController used by this popup. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 38 } | 44 } |
| 39 | 45 |
| 40 // Call to change the hovered line. |line| should be within the range of | 46 // Call to change the hovered line. |line| should be within the range of |
| 41 // valid lines (to enable hover) or kNoMatch (to disable hover). | 47 // valid lines (to enable hover) or kNoMatch (to disable hover). |
| 42 void SetHoveredLine(size_t line); | 48 void SetHoveredLine(size_t line); |
| 43 | 49 |
| 44 size_t selected_line() const { | 50 size_t selected_line() const { |
| 45 return selected_line_; | 51 return selected_line_; |
| 46 } | 52 } |
| 47 | 53 |
| 54 LineState selected_line_state() const { |
| 55 return selected_line_state_; |
| 56 } |
| 57 |
| 48 // Call to change the selected line. This will update all state and repaint | 58 // Call to change the selected line. This will update all state and repaint |
| 49 // the necessary parts of the window, as well as updating the edit with the | 59 // the necessary parts of the window, as well as updating the edit with the |
| 50 // new temporary text. |line| will be clamped to the range of valid lines. | 60 // new temporary text. |line| will be clamped to the range of valid lines. |
| 51 // |reset_to_default| is true when the selection is being reset back to the | 61 // |reset_to_default| is true when the selection is being reset back to the |
| 52 // default match, and thus there is no temporary text (and no | 62 // default match, and thus there is no temporary text (and no |
| 53 // |manually_selected_match_|). If |force| is true then the selected line will | 63 // |manually_selected_match_|). If |force| is true then the selected line will |
| 54 // be updated forcibly even if the |line| is same as the current selected | 64 // be updated forcibly even if the |line| is same as the current selected |
| 55 // line. | 65 // line. |
| 56 // NOTE: This assumes the popup is open, and thus both old and new values for | 66 // NOTE: This assumes the popup is open, and thus both old and new values for |
| 57 // the selected line should not be kNoMatch. | 67 // the selected line should not be kNoMatch. |
| 58 void SetSelectedLine(size_t line, bool reset_to_default, bool force); | 68 void SetSelectedLine(size_t line, bool reset_to_default, bool force); |
| 59 | 69 |
| 60 // Called when the user hits escape after arrowing around the popup. This | 70 // Called when the user hits escape after arrowing around the popup. This |
| 61 // will change the selected line back to the default match and redraw. | 71 // will change the selected line back to the default match and redraw. |
| 62 void ResetToDefaultMatch(); | 72 void ResetToDefaultMatch(); |
| 63 | 73 |
| 64 // Gets the selected keyword or keyword hint for the given match. If the match | |
| 65 // is already keyword, then the keyword will be returned directly. Otherwise, | |
| 66 // it returns GetKeywordForText(match.fill_into_edit, keyword). | |
| 67 bool GetKeywordForMatch(const AutocompleteMatch& match, | |
| 68 string16* keyword) const; | |
| 69 | |
| 70 // Gets the selected keyword or keyword hint for the given text. Returns | |
| 71 // true if |keyword| represents a keyword hint, or false if |keyword| | |
| 72 // represents a selected keyword. (|keyword| will always be set [though | |
| 73 // possibly to the empty string], and you cannot have both a selected keyword | |
| 74 // and a keyword hint simultaneously.) | |
| 75 bool GetKeywordForText(const string16& text, string16* keyword) const; | |
| 76 | |
| 77 // Immediately updates and opens the popup if necessary, then moves the | 74 // Immediately updates and opens the popup if necessary, then moves the |
| 78 // current selection down (|count| > 0) or up (|count| < 0), clamping to the | 75 // current selection down (|count| > 0) or up (|count| < 0), clamping to the |
| 79 // first or last result if necessary. If |count| == 0, the selection will be | 76 // first or last result if necessary. If |count| == 0, the selection will be |
| 80 // unchanged, but the popup will still redraw and modify the text in the | 77 // unchanged, but the popup will still redraw and modify the text in the |
| 81 // AutocompleteEditModel. | 78 // AutocompleteEditModel. |
| 82 void Move(int count); | 79 void Move(int count); |
| 83 | 80 |
| 81 // If the selected line has both a normal match and a keyword match, this can |
| 82 // be used to choose which to select. It is an error to call this when the |
| 83 // selected line does not have both matches (or there is no selection). |
| 84 void SetSelectedLineState(LineState state); |
| 85 |
| 84 // Called when the user hits shift-delete. This should determine if the item | 86 // Called when the user hits shift-delete. This should determine if the item |
| 85 // can be removed from history, and if so, remove it and update the popup. | 87 // can be removed from history, and if so, remove it and update the popup. |
| 86 void TryDeletingCurrentItem(); | 88 void TryDeletingCurrentItem(); |
| 87 | 89 |
| 88 // If |match| is from an extension, returns the extension icon; otherwise | 90 // If |match| is from an extension, returns the extension icon; otherwise |
| 89 // returns NULL. | 91 // returns NULL. |
| 90 const SkBitmap* GetIconIfExtensionMatch(const AutocompleteMatch& match) const; | 92 const SkBitmap* GetIconIfExtensionMatch(const AutocompleteMatch& match) const; |
| 91 | 93 |
| 92 // The match the user has manually chosen, if any. | 94 // The match the user has manually chosen, if any. |
| 93 const AutocompleteResult::Selection& manually_selected_match() const { | 95 const AutocompleteResult::Selection& manually_selected_match() const { |
| 94 return manually_selected_match_; | 96 return manually_selected_match_; |
| 95 } | 97 } |
| 96 | 98 |
| 97 // Invoked from the edit model any time the result set of the controller | 99 // Invoked from the edit model any time the result set of the controller |
| 98 // changes. | 100 // changes. |
| 99 void OnResultChanged(); | 101 void OnResultChanged(); |
| 100 | 102 |
| 101 // The token value for selected_line_, hover_line_ and functions dealing with | 103 // The token value for selected_line_, hover_line_ and functions dealing with |
| 102 // a "line number" that indicates "no line". | 104 // a "line number" that indicates "no line". |
| 103 static const size_t kNoMatch = -1; | 105 static const size_t kNoMatch; |
| 104 | 106 |
| 105 private: | 107 private: |
| 106 AutocompletePopupView* view_; | 108 AutocompletePopupView* view_; |
| 107 | 109 |
| 108 AutocompleteEditModel* edit_model_; | 110 AutocompleteEditModel* edit_model_; |
| 109 | 111 |
| 110 // The line that's currently hovered. If we're not drawing a hover rect, | 112 // The line that's currently hovered. If we're not drawing a hover rect, |
| 111 // this will be kNoMatch, even if the cursor is over the popup contents. | 113 // this will be kNoMatch, even if the cursor is over the popup contents. |
| 112 size_t hovered_line_; | 114 size_t hovered_line_; |
| 113 | 115 |
| 114 // The currently selected line. This is kNoMatch when nothing is selected, | 116 // The currently selected line. This is kNoMatch when nothing is selected, |
| 115 // which should only be true when the popup is closed. | 117 // which should only be true when the popup is closed. |
| 116 size_t selected_line_; | 118 size_t selected_line_; |
| 117 | 119 |
| 120 // If the selected line has both a normal match and a keyword match, this |
| 121 // determines whether the normal match (if NORMAL) or the keyword match |
| 122 // (if KEYWORD) is selected. |
| 123 LineState selected_line_state_; |
| 124 |
| 118 // The match the user has manually chosen, if any. | 125 // The match the user has manually chosen, if any. |
| 119 AutocompleteResult::Selection manually_selected_match_; | 126 AutocompleteResult::Selection manually_selected_match_; |
| 120 | 127 |
| 121 DISALLOW_COPY_AND_ASSIGN(AutocompletePopupModel); | 128 DISALLOW_COPY_AND_ASSIGN(AutocompletePopupModel); |
| 122 }; | 129 }; |
| 123 | 130 |
| 124 #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_MODEL_H_ | 131 #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_MODEL_H_ |
| OLD | NEW |