Fix //third_party/sqlite BUILD.gn on iOS.
[chromium-blink-merge.git] / ui / app_list / search_result.cc
blobf1abf58818d004e2bc8c385c7d490edfce460888
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"
7 #include <map>
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"
14 namespace app_list {
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()
33 : relevance_(0),
34 display_type_(DISPLAY_LIST),
35 distance_from_origin_(-1),
36 voice_result_(false),
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) {
46 icon_ = icon;
47 FOR_EACH_OBSERVER(SearchResultObserver,
48 observers_,
49 OnIconChanged());
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) {
58 actions_ = sets;
59 FOR_EACH_OBSERVER(SearchResultObserver,
60 observers_,
61 OnActionsChanged());
64 void SearchResult::SetIsInstalling(bool is_installing) {
65 if (is_installing_ == is_installing)
66 return;
68 is_installing_ = is_installing;
69 FOR_EACH_OBSERVER(SearchResultObserver,
70 observers_,
71 OnIsInstallingChanged());
74 void SearchResult::SetPercentDownloaded(int percent_downloaded) {
75 if (percent_downloaded_ == percent_downloaded)
76 return;
78 percent_downloaded_ = percent_downloaded;
79 FOR_EACH_OBSERVER(SearchResultObserver,
80 observers_,
81 OnPercentDownloadedChanged());
84 int SearchResult::GetPreferredIconDimension() const {
85 switch (display_type_) {
86 case DISPLAY_RECOMMENDATION: // Falls through.
87 case DISPLAY_TILE:
88 return kTileIconSize;
89 case DISPLAY_LIST:
90 return kListIconSize;
91 case DISPLAY_NONE:
92 return 0;
93 case DISPLAY_TYPE_LAST:
94 break;
96 NOTREACHED();
97 return 0;
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();
116 Tags tags;
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() {
133 return NULL;
136 // static
137 std::string SearchResult::TagsDebugString(const std::string& text,
138 const Tags& tags) {
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);
162 return result;
165 } // namespace app_list