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 #include "ui/app_list/search_result.h"
9 #include "ui/app_list/app_list_constants.h"
10 #include "ui/app_list/search/tokenized_string.h"
11 #include "ui/app_list/search/tokenized_string_match.h"
12 #include "ui/app_list/search_result_observer.h"
16 SearchResult::Action::Action(const gfx::ImageSkia
& base_image
,
17 const gfx::ImageSkia
& hover_image
,
18 const gfx::ImageSkia
& pressed_image
,
19 const base::string16
& tooltip_text
)
20 : base_image(base_image
),
21 hover_image(hover_image
),
22 pressed_image(pressed_image
),
23 tooltip_text(tooltip_text
) {}
25 SearchResult::Action::Action(const base::string16
& label_text
,
26 const base::string16
& tooltip_text
)
27 : tooltip_text(tooltip_text
),
28 label_text(label_text
) {}
30 SearchResult::Action::~Action() {}
32 SearchResult::SearchResult()
34 display_type_(DISPLAY_LIST
),
35 distance_from_origin_(-1),
37 is_installing_(false),
38 percent_downloaded_(0) {
41 SearchResult::~SearchResult() {
42 FOR_EACH_OBSERVER(SearchResultObserver
, observers_
, OnResultDestroying());
45 void SearchResult::SetIcon(const gfx::ImageSkia
& icon
) {
47 FOR_EACH_OBSERVER(SearchResultObserver
,
52 void SearchResult::SetBadgeIcon(const gfx::ImageSkia
& badge_icon
) {
53 badge_icon_
= badge_icon
;
54 FOR_EACH_OBSERVER(SearchResultObserver
, observers_
, OnBadgeIconChanged());
57 void SearchResult::SetActions(const Actions
& sets
) {
59 FOR_EACH_OBSERVER(SearchResultObserver
,
64 void SearchResult::SetIsInstalling(bool is_installing
) {
65 if (is_installing_
== is_installing
)
68 is_installing_
= is_installing
;
69 FOR_EACH_OBSERVER(SearchResultObserver
,
71 OnIsInstallingChanged());
74 void SearchResult::SetPercentDownloaded(int percent_downloaded
) {
75 if (percent_downloaded_
== percent_downloaded
)
78 percent_downloaded_
= percent_downloaded
;
79 FOR_EACH_OBSERVER(SearchResultObserver
,
81 OnPercentDownloadedChanged());
84 int SearchResult::GetPreferredIconDimension() const {
85 switch (display_type_
) {
86 case DISPLAY_RECOMMENDATION
: // Falls through.
93 case DISPLAY_TYPE_LAST
:
100 void SearchResult::NotifyItemInstalled() {
101 FOR_EACH_OBSERVER(SearchResultObserver
, observers_
, OnItemInstalled());
104 void SearchResult::AddObserver(SearchResultObserver
* observer
) {
105 observers_
.AddObserver(observer
);
108 void SearchResult::RemoveObserver(SearchResultObserver
* observer
) {
109 observers_
.RemoveObserver(observer
);
112 void SearchResult::UpdateFromMatch(const TokenizedString
& title
,
113 const TokenizedStringMatch
& match
) {
114 const TokenizedStringMatch::Hits
& hits
= match
.hits();
117 tags
.reserve(hits
.size());
118 for (size_t i
= 0; i
< hits
.size(); ++i
)
119 tags
.push_back(Tag(Tag::MATCH
, hits
[i
].start(), hits
[i
].end()));
121 set_title(title
.text());
122 set_title_tags(tags
);
123 set_relevance(match
.relevance());
126 void SearchResult::Open(int event_flags
) {
129 void SearchResult::InvokeAction(int action_index
, int event_flags
) {
132 ui::MenuModel
* SearchResult::GetContextMenuModel() {
137 std::string
SearchResult::TagsDebugString(const std::string
& text
,
139 std::string result
= text
;
141 // Build a table of delimiters to insert.
142 std::map
<size_t, std::string
> inserts
;
143 for (const auto& tag
: tags
) {
144 if (tag
.styles
& Tag::URL
)
145 inserts
[tag
.range
.start()].push_back('{');
146 if (tag
.styles
& Tag::MATCH
)
147 inserts
[tag
.range
.start()].push_back('[');
148 if (tag
.styles
& Tag::DIM
) {
149 inserts
[tag
.range
.start()].push_back('<');
150 inserts
[tag
.range
.end()].push_back('>');
152 if (tag
.styles
& Tag::MATCH
)
153 inserts
[tag
.range
.end()].push_back(']');
154 if (tag
.styles
& Tag::URL
)
155 inserts
[tag
.range
.end()].push_back('}');
158 // Insert the delimiters (in reverse order, to preserve indices).
159 for (auto it
= inserts
.rbegin(); it
!= inserts
.rend(); ++it
)
160 result
.insert(it
->first
, it
->second
);
165 } // namespace app_list