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_OMNIBOX_OMNIBOX_POPUP_MODEL_H_
6 #define CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_POPUP_MODEL_H_
8 #include "base/basictypes.h"
9 #include "base/observer_list.h"
10 #include "chrome/browser/autocomplete/autocomplete_controller.h"
11 #include "chrome/browser/autocomplete/autocomplete_result.h"
12 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
14 class OmniboxPopupModelObserver
;
15 class OmniboxPopupView
;
21 class OmniboxPopupModel
{
23 // See selected_line_state_ for details.
29 OmniboxPopupModel(OmniboxPopupView
* popup_view
, OmniboxEditModel
* edit_model
);
32 // Returns true if the popup is currently open.
35 OmniboxPopupView
* view() const { return view_
; }
37 // Returns the AutocompleteController used by this popup.
38 AutocompleteController
* autocomplete_controller() const {
39 return edit_model_
->autocomplete_controller();
42 const AutocompleteResult
& result() const {
43 return autocomplete_controller()->result();
46 size_t hovered_line() const { return hovered_line_
; }
48 // Call to change the hovered line. |line| should be within the range of
49 // valid lines (to enable hover) or kNoMatch (to disable hover).
50 void SetHoveredLine(size_t line
);
52 size_t selected_line() const { return selected_line_
; }
54 LineState
selected_line_state() const { return selected_line_state_
; }
56 // Call to change the selected line. This will update all state and repaint
57 // the necessary parts of the window, as well as updating the edit with the
58 // new temporary text. |line| will be clamped to the range of valid lines.
59 // |reset_to_default| is true when the selection is being reset back to the
60 // default match, and thus there is no temporary text (and no
61 // |manually_selected_match_|). If |force| is true then the selected line will
62 // be updated forcibly even if the |line| is same as the current selected
64 // NOTE: This assumes the popup is open, and thus both old and new values for
65 // the selected line should not be kNoMatch.
66 void SetSelectedLine(size_t line
, bool reset_to_default
, bool force
);
68 // Called when the user hits escape after arrowing around the popup. This
69 // will change the selected line back to the default match and redraw.
70 void ResetToDefaultMatch();
72 // Immediately updates and opens the popup if necessary, then moves the
73 // current selection down (|count| > 0) or up (|count| < 0), clamping to the
74 // first or last result if necessary. If |count| == 0, the selection will be
75 // unchanged, but the popup will still redraw and modify the text in the
79 // If the selected line has both a normal match and a keyword match, this can
80 // be used to choose which to select. It is an error to call this when the
81 // selected line does not have both matches (or there is no selection).
82 void SetSelectedLineState(LineState state
);
84 // 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.
86 void TryDeletingCurrentItem();
88 // If |match| is from an extension, returns the extension icon; otherwise
89 // returns an empty Image.
90 gfx::Image
GetIconIfExtensionMatch(const AutocompleteMatch
& match
) const;
92 // The match the user has manually chosen, if any.
93 const AutocompleteResult::Selection
& manually_selected_match() const {
94 return manually_selected_match_
;
97 // Invoked from the edit model any time the result set of the controller
99 void OnResultChanged();
101 // Add and remove observers.
102 void AddObserver(OmniboxPopupModelObserver
* observer
);
103 void RemoveObserver(OmniboxPopupModelObserver
* observer
);
105 // The token value for selected_line_, hover_line_ and functions dealing with
106 // a "line number" that indicates "no line".
107 static const size_t kNoMatch
;
110 OmniboxPopupView
* view_
;
112 OmniboxEditModel
* edit_model_
;
114 // The line that's currently hovered. If we're not drawing a hover rect,
115 // this will be kNoMatch, even if the cursor is over the popup contents.
116 size_t hovered_line_
;
118 // The currently selected line. This is kNoMatch when nothing is selected,
119 // which should only be true when the popup is closed.
120 size_t selected_line_
;
122 // If the selected line has both a normal match and a keyword match, this
123 // determines whether the normal match (if NORMAL) or the keyword match
124 // (if KEYWORD) is selected.
125 LineState selected_line_state_
;
127 // The match the user has manually chosen, if any.
128 AutocompleteResult::Selection manually_selected_match_
;
131 ObserverList
<OmniboxPopupModelObserver
> observers_
;
133 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupModel
);
136 #endif // CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_POPUP_MODEL_H_