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 gfx::ImageSkia
& badge_icon() const { return badge_icon_
; }
95 void SetBadgeIcon(const gfx::ImageSkia
& badge_icon
);
97 const base::string16
& title() const { return title_
; }
98 void set_title(const base::string16
& title
) { title_
= title
; }
100 const Tags
& title_tags() const { return title_tags_
; }
101 void set_title_tags(const Tags
& tags
) { title_tags_
= tags
; }
103 const base::string16
& details() const { return details_
; }
104 void set_details(const base::string16
& details
) { details_
= details
; }
106 const Tags
& details_tags() const { return details_tags_
; }
107 void set_details_tags(const Tags
& tags
) { details_tags_
= tags
; }
109 const std::string
& id() const { return id_
; }
111 double relevance() const { return relevance_
; }
112 void set_relevance(double relevance
) { relevance_
= relevance
; }
114 DisplayType
display_type() const { return display_type_
; }
115 void set_display_type(DisplayType display_type
) {
116 display_type_
= display_type
;
119 int distance_from_origin() { return distance_from_origin_
; }
120 void set_distance_from_origin(int distance
) {
121 distance_from_origin_
= distance
;
124 const Actions
& actions() const {
127 void SetActions(const Actions
& sets
);
129 // Whether the result can be automatically selected by a voice query.
130 // (Non-voice results can still appear in the results list to be manually
132 bool voice_result() const { return voice_result_
; }
134 bool is_installing() const { return is_installing_
; }
135 void SetIsInstalling(bool is_installing
);
137 int percent_downloaded() const { return percent_downloaded_
; }
138 void SetPercentDownloaded(int percent_downloaded
);
140 // Returns the dimension at which this result's icon should be displayed.
141 int GetPreferredIconDimension() const;
143 void NotifyItemInstalled();
145 void AddObserver(SearchResultObserver
* observer
);
146 void RemoveObserver(SearchResultObserver
* observer
);
148 // Updates the result's relevance score, and sets its title and title tags,
149 // based on a string match result.
150 void UpdateFromMatch(const TokenizedString
& title
,
151 const TokenizedStringMatch
& match
);
153 // TODO(mukai): Remove this method and really simplify the ownership of
154 // SearchResult. Ideally, SearchResult will be copyable.
155 virtual scoped_ptr
<SearchResult
> Duplicate() const = 0;
157 // Invokes a custom action on the result. It does nothing by default.
158 virtual void InvokeAction(int action_index
, int event_flags
);
160 // Returns the context menu model for this item, or NULL if there is currently
161 // no menu for the item (e.g. during install).
162 // Note the returned menu model is owned by this item.
163 virtual ui::MenuModel
* GetContextMenuModel();
165 // Returns a string showing |text| marked up with brackets indicating the
166 // tag positions in |tags|. Useful for debugging and testing.
167 static std::string
TagsDebugString(const std::string
& text
, const Tags
& tags
);
170 void set_id(const std::string
& id
) { id_
= id
; }
171 void set_voice_result(bool voice_result
) { voice_result_
= voice_result
; }
174 friend class SearchController
;
176 // Opens the result. Clients should use AppListViewDelegate::OpenSearchResult.
177 virtual void Open(int event_flags
);
179 gfx::ImageSkia icon_
;
180 gfx::ImageSkia badge_icon_
;
182 base::string16 title_
;
185 base::string16 details_
;
190 DisplayType display_type_
;
192 // The Manhattan distance from the origin of all search results to this
193 // result. This is logged for UMA.
194 int distance_from_origin_
;
200 int percent_downloaded_
;
202 base::ObserverList
<SearchResultObserver
> observers_
;
204 DISALLOW_COPY_AND_ASSIGN(SearchResult
);
207 } // namespace app_list
209 #endif // UI_APP_LIST_SEARCH_RESULT_H_