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 UI_APP_LIST_SEARCH_RESULT_H_
6 #define UI_APP_LIST_SEARCH_RESULT_H_
11 #include "base/basictypes.h"
12 #include "base/observer_list.h"
13 #include "base/strings/string16.h"
14 #include "ui/app_list/app_list_export.h"
15 #include "ui/gfx/image/image_skia.h"
16 #include "ui/gfx/range/range.h"
24 class SearchResultObserver
;
25 class TokenizedString
;
26 class TokenizedStringMatch
;
28 // SearchResult consists of an icon, title text and details text. Title and
29 // details text can have tagged ranges that are displayed differently from
31 class APP_LIST_EXPORT SearchResult
{
33 // How the result should be displayed. Do not change the order of these as
34 // they are used for metrics.
39 DISPLAY_RECOMMENDATION
,
40 // Add new values here.
45 // A tagged range in search result text.
46 struct APP_LIST_EXPORT Tag
{
47 // Similar to ACMatchClassification::Style, the style values are not
48 // mutually exclusive.
56 Tag(int styles
, size_t start
, size_t end
)
64 typedef std::vector
<Tag
> Tags
;
66 // Data representing an action that can be performed on this search result.
67 // An action could be represented as an icon set or as a blue button with
68 // a label. Icon set is chosen if label text is empty. Otherwise, a blue
69 // button with the label text will be used.
70 struct APP_LIST_EXPORT Action
{
71 Action(const gfx::ImageSkia
& base_image
,
72 const gfx::ImageSkia
& hover_image
,
73 const gfx::ImageSkia
& pressed_image
,
74 const base::string16
& tooltip_text
);
75 Action(const base::string16
& label_text
,
76 const base::string16
& tooltip_text
);
79 gfx::ImageSkia base_image
;
80 gfx::ImageSkia hover_image
;
81 gfx::ImageSkia pressed_image
;
83 base::string16 tooltip_text
;
84 base::string16 label_text
;
86 typedef std::vector
<Action
> Actions
;
89 virtual ~SearchResult();
91 const gfx::ImageSkia
& icon() const { return icon_
; }
92 void SetIcon(const gfx::ImageSkia
& icon
);
94 const base::string16
& title() const { return title_
; }
95 void set_title(const base::string16
& title
) { title_
= title
;}
97 const Tags
& title_tags() const { return title_tags_
; }
98 void set_title_tags(const Tags
& tags
) { title_tags_
= tags
; }
100 const base::string16
& details() const { return details_
; }
101 void set_details(const base::string16
& details
) { details_
= details
; }
103 const Tags
& details_tags() const { return details_tags_
; }
104 void set_details_tags(const Tags
& tags
) { details_tags_
= tags
; }
106 const std::string
& id() const { return id_
; }
108 double relevance() const { return relevance_
; }
109 void set_relevance(double relevance
) { relevance_
= relevance
; }
111 DisplayType
display_type() const { return display_type_
; }
112 void set_display_type(DisplayType display_type
) {
113 display_type_
= display_type
;
116 int distance_from_origin() { return distance_from_origin_
; }
117 void set_distance_from_origin(int distance
) {
118 distance_from_origin_
= distance
;
121 const Actions
& actions() const {
124 void SetActions(const Actions
& sets
);
126 // Whether the result can be automatically selected by a voice query.
127 // (Non-voice results can still appear in the results list to be manually
129 bool voice_result() const { return voice_result_
; }
131 bool is_installing() const { return is_installing_
; }
132 void SetIsInstalling(bool is_installing
);
134 int percent_downloaded() const { return percent_downloaded_
; }
135 void SetPercentDownloaded(int percent_downloaded
);
137 // Returns the dimension at which this result's icon should be displayed.
138 int GetPreferredIconDimension() const;
140 void NotifyItemInstalled();
142 void AddObserver(SearchResultObserver
* observer
);
143 void RemoveObserver(SearchResultObserver
* observer
);
145 // Updates the result's relevance score, and sets its title and title tags,
146 // based on a string match result.
147 void UpdateFromMatch(const TokenizedString
& title
,
148 const TokenizedStringMatch
& match
);
150 // TODO(mukai): Remove this method and really simplify the ownership of
151 // SearchResult. Ideally, SearchResult will be copyable.
152 virtual scoped_ptr
<SearchResult
> Duplicate() const = 0;
154 // Invokes a custom action on the result. It does nothing by default.
155 virtual void InvokeAction(int action_index
, int event_flags
);
157 // Returns the context menu model for this item, or NULL if there is currently
158 // no menu for the item (e.g. during install).
159 // Note the returned menu model is owned by this item.
160 virtual ui::MenuModel
* GetContextMenuModel();
162 // Returns a string showing |text| marked up with brackets indicating the
163 // tag positions in |tags|. Useful for debugging and testing.
164 static std::string
TagsDebugString(const std::string
& text
, const Tags
& tags
);
167 void set_id(const std::string
& id
) { id_
= id
; }
168 void set_voice_result(bool voice_result
) { voice_result_
= voice_result
; }
171 friend class SearchController
;
173 // Opens the result. Clients should use AppListViewDelegate::OpenSearchResult.
174 virtual void Open(int event_flags
);
176 gfx::ImageSkia icon_
;
178 base::string16 title_
;
181 base::string16 details_
;
186 DisplayType display_type_
;
188 // The Manhattan distance from the origin of all search results to this
189 // result. This is logged for UMA.
190 int distance_from_origin_
;
196 int percent_downloaded_
;
198 base::ObserverList
<SearchResultObserver
> observers_
;
200 DISALLOW_COPY_AND_ASSIGN(SearchResult
);
203 } // namespace app_list
205 #endif // UI_APP_LIST_SEARCH_RESULT_H_