Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / omnibox / browser / omnibox_popup_model.h
bloba878849ed40b2598d10399be6d9b8fd04297ae94
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 COMPONENTS_OMNIBOX_BROWSER_OMNIBOX_POPUP_MODEL_H_
6 #define COMPONENTS_OMNIBOX_BROWSER_OMNIBOX_POPUP_MODEL_H_
8 #include "base/basictypes.h"
9 #include "base/observer_list.h"
10 #include "components/omnibox/browser/autocomplete_controller.h"
11 #include "components/omnibox/browser/autocomplete_result.h"
12 #include "components/omnibox/browser/omnibox_edit_model.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
15 class OmniboxPopupModelObserver;
16 class OmniboxPopupView;
18 namespace gfx {
19 class Image;
22 class OmniboxPopupModel {
23 public:
24 // See selected_line_state_ for details.
25 enum LineState {
26 NORMAL = 0,
27 KEYWORD
30 OmniboxPopupModel(OmniboxPopupView* popup_view, OmniboxEditModel* edit_model);
31 ~OmniboxPopupModel();
33 // Computes the maximum width, in pixels, that can be allocated for the two
34 // parts of an autocomplete result, i.e. the contents and the description.
35 static void ComputeMatchMaxWidths(int contents_width,
36 int separator_width,
37 int description_width,
38 int available_width,
39 bool allow_shrinking_contents,
40 int* contents_max_width,
41 int* description_max_width);
43 // Returns true if the popup is currently open.
44 bool IsOpen() const;
46 OmniboxPopupView* view() const { return view_; }
48 // Returns the AutocompleteController used by this popup.
49 AutocompleteController* autocomplete_controller() const {
50 return edit_model_->autocomplete_controller();
53 const AutocompleteResult& result() const {
54 return autocomplete_controller()->result();
57 size_t hovered_line() const { return hovered_line_; }
59 // Call to change the hovered line. |line| should be within the range of
60 // valid lines (to enable hover) or kNoMatch (to disable hover).
61 void SetHoveredLine(size_t line);
63 size_t selected_line() const { return selected_line_; }
65 LineState selected_line_state() const { return selected_line_state_; }
67 // Call to change the selected line. This will update all state and repaint
68 // the necessary parts of the window, as well as updating the edit with the
69 // new temporary text. |line| will be clamped to the range of valid lines.
70 // |reset_to_default| is true when the selection is being reset back to the
71 // default match, and thus there is no temporary text (and no
72 // |manually_selected_match_|). If |force| is true then the selected line will
73 // be updated forcibly even if the |line| is same as the current selected
74 // line.
75 // NOTE: This assumes the popup is open, and thus both old and new values for
76 // the selected line should not be kNoMatch.
77 void SetSelectedLine(size_t line, bool reset_to_default, bool force);
79 // Called when the user hits escape after arrowing around the popup. This
80 // will change the selected line back to the default match and redraw.
81 void ResetToDefaultMatch();
83 // Immediately updates and opens the popup if necessary, then moves the
84 // current selection down (|count| > 0) or up (|count| < 0), clamping to the
85 // first or last result if necessary. If |count| == 0, the selection will be
86 // unchanged, but the popup will still redraw and modify the text in the
87 // OmniboxEditModel.
88 void Move(int count);
90 // If the selected line has both a normal match and a keyword match, this can
91 // be used to choose which to select. This allows the user to toggle between
92 // normal and keyword mode with tab/shift-tab without rerunning autocomplete
93 // or disturbing other popup state, which in turn is an important part of
94 // supporting the use of tab to do both tab-to-search and
95 // tab-to-traverse-dropdown.
97 // It is an error to call this when the selected line does not have both
98 // matches (or there is no selection).
99 void SetSelectedLineState(LineState state);
101 // Called when the user hits shift-delete. This should determine if the item
102 // can be removed from history, and if so, remove it and update the popup.
103 void TryDeletingCurrentItem();
105 // If |match| is from an extension, returns the extension icon; otherwise
106 // returns an empty Image.
107 gfx::Image GetIconIfExtensionMatch(const AutocompleteMatch& match) const;
109 // Returns true if the destination URL of the match is bookmarked.
110 bool IsStarredMatch(const AutocompleteMatch& match) const;
112 // The match the user has manually chosen, if any.
113 const AutocompleteResult::Selection& manually_selected_match() const {
114 return manually_selected_match_;
117 // Invoked from the edit model any time the result set of the controller
118 // changes.
119 void OnResultChanged();
121 // Add and remove observers.
122 void AddObserver(OmniboxPopupModelObserver* observer);
123 void RemoveObserver(OmniboxPopupModelObserver* observer);
125 // Stores the image in a local data member and schedules a repaint.
126 void SetAnswerBitmap(const SkBitmap& bitmap);
127 const SkBitmap& answer_bitmap() const { return answer_bitmap_; }
129 // The token value for selected_line_, hover_line_ and functions dealing with
130 // a "line number" that indicates "no line".
131 static const size_t kNoMatch;
133 private:
134 SkBitmap answer_bitmap_;
136 OmniboxPopupView* view_;
138 OmniboxEditModel* edit_model_;
140 // The line that's currently hovered. If we're not drawing a hover rect,
141 // this will be kNoMatch, even if the cursor is over the popup contents.
142 size_t hovered_line_;
144 // The currently selected line. This is kNoMatch when nothing is selected,
145 // which should only be true when the popup is closed.
146 size_t selected_line_;
148 // If the selected line has both a normal match and a keyword match, this
149 // determines whether the normal match (if NORMAL) or the keyword match
150 // (if KEYWORD) is selected.
151 LineState selected_line_state_;
153 // The match the user has manually chosen, if any.
154 AutocompleteResult::Selection manually_selected_match_;
156 // Observers.
157 base::ObserverList<OmniboxPopupModelObserver> observers_;
159 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupModel);
162 #endif // COMPONENTS_OMNIBOX_BROWSER_OMNIBOX_POPUP_MODEL_H_