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::SetActions(const Actions
& sets
) {
54 FOR_EACH_OBSERVER(SearchResultObserver
,
59 void SearchResult::SetIsInstalling(bool is_installing
) {
60 if (is_installing_
== is_installing
)
63 is_installing_
= is_installing
;
64 FOR_EACH_OBSERVER(SearchResultObserver
,
66 OnIsInstallingChanged());
69 void SearchResult::SetPercentDownloaded(int percent_downloaded
) {
70 if (percent_downloaded_
== percent_downloaded
)
73 percent_downloaded_
= percent_downloaded
;
74 FOR_EACH_OBSERVER(SearchResultObserver
,
76 OnPercentDownloadedChanged());
79 int SearchResult::GetPreferredIconDimension() const {
80 switch (display_type_
) {
81 case DISPLAY_RECOMMENDATION
: // Falls through.
88 case DISPLAY_TYPE_LAST
:
95 void SearchResult::NotifyItemInstalled() {
96 FOR_EACH_OBSERVER(SearchResultObserver
, observers_
, OnItemInstalled());
99 void SearchResult::AddObserver(SearchResultObserver
* observer
) {
100 observers_
.AddObserver(observer
);
103 void SearchResult::RemoveObserver(SearchResultObserver
* observer
) {
104 observers_
.RemoveObserver(observer
);
107 void SearchResult::UpdateFromMatch(const TokenizedString
& title
,
108 const TokenizedStringMatch
& match
) {
109 const TokenizedStringMatch::Hits
& hits
= match
.hits();
112 tags
.reserve(hits
.size());
113 for (size_t i
= 0; i
< hits
.size(); ++i
)
114 tags
.push_back(Tag(Tag::MATCH
, hits
[i
].start(), hits
[i
].end()));
116 set_title(title
.text());
117 set_title_tags(tags
);
118 set_relevance(match
.relevance());
121 void SearchResult::Open(int event_flags
) {
124 void SearchResult::InvokeAction(int action_index
, int event_flags
) {
127 ui::MenuModel
* SearchResult::GetContextMenuModel() {
132 std::string
SearchResult::TagsDebugString(const std::string
& text
,
134 std::string result
= text
;
136 // Build a table of delimiters to insert.
137 std::map
<size_t, std::string
> inserts
;
138 for (const auto& tag
: tags
) {
139 if (tag
.styles
& Tag::URL
)
140 inserts
[tag
.range
.start()].push_back('{');
141 if (tag
.styles
& Tag::MATCH
)
142 inserts
[tag
.range
.start()].push_back('[');
143 if (tag
.styles
& Tag::DIM
) {
144 inserts
[tag
.range
.start()].push_back('<');
145 inserts
[tag
.range
.end()].push_back('>');
147 if (tag
.styles
& Tag::MATCH
)
148 inserts
[tag
.range
.end()].push_back(']');
149 if (tag
.styles
& Tag::URL
)
150 inserts
[tag
.range
.end()].push_back('}');
153 // Insert the delimiters (in reverse order, to preserve indices).
154 for (auto it
= inserts
.rbegin(); it
!= inserts
.rend(); ++it
)
155 result
.insert(it
->first
, it
->second
);
160 } // namespace app_list