OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // For WinDDK ATL compatibility, these ATL headers must come first. | 5 // For WinDDK ATL compatibility, these ATL headers must come first. |
6 #include "build/build_config.h" | 6 #include "build/build_config.h" |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <atlbase.h> // NOLINT | 8 #include <atlbase.h> // NOLINT |
9 #include <atlwin.h> // NOLINT | 9 #include <atlwin.h> // NOLINT |
10 #endif | 10 #endif |
11 | 11 |
12 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h" | 12 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h" |
13 | 13 |
14 #include <algorithm> // NOLINT | 14 #include <algorithm> // NOLINT |
15 | 15 |
16 #include "base/i18n/bidi_line_iterator.h" | 16 #include "base/i18n/bidi_line_iterator.h" |
17 #include "base/memory/scoped_vector.h" | 17 #include "base/memory/scoped_vector.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
20 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.h" | |
21 #include "chrome/browser/profiles/profile.h" | |
20 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" | 22 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
21 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 23 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
22 #include "chrome/browser/ui/views/omnibox/omnibox_popup_contents_view.h" | 24 #include "chrome/browser/ui/views/omnibox/omnibox_popup_contents_view.h" |
23 #include "chrome/grit/generated_resources.h" | 25 #include "chrome/grit/generated_resources.h" |
24 #include "components/omnibox/suggestion_answer.h" | 26 #include "components/omnibox/suggestion_answer.h" |
25 #include "grit/components_scaled_resources.h" | 27 #include "grit/components_scaled_resources.h" |
26 #include "grit/theme_resources.h" | 28 #include "grit/theme_resources.h" |
27 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
28 #include "ui/base/theme_provider.h" | 30 #include "ui/base/theme_provider.h" |
29 #include "ui/gfx/canvas.h" | 31 #include "ui/gfx/canvas.h" |
30 #include "ui/gfx/color_utils.h" | 32 #include "ui/gfx/color_utils.h" |
31 #include "ui/gfx/image/image.h" | 33 #include "ui/gfx/image/image.h" |
32 #include "ui/gfx/range/range.h" | 34 #include "ui/gfx/range/range.h" |
33 #include "ui/gfx/render_text.h" | 35 #include "ui/gfx/render_text.h" |
34 #include "ui/gfx/text_utils.h" | 36 #include "ui/gfx/text_utils.h" |
35 #include "ui/native_theme/native_theme.h" | 37 #include "ui/native_theme/native_theme.h" |
36 | 38 |
37 using ui::NativeTheme; | 39 using ui::NativeTheme; |
38 | 40 |
39 namespace { | 41 namespace { |
40 | 42 |
43 // The observer watches for changes in the image being downloaded. | |
44 class AnswersImageObserverDesktop : public BitmapFetcherService::Observer { | |
45 public: | |
46 explicit AnswersImageObserverDesktop(OmniboxResultView* view) : view_(view) {} | |
47 | |
48 void OnImageChanged(BitmapFetcherService::RequestId request_id, | |
49 const SkBitmap& image) override; | |
50 | |
51 private: | |
52 OmniboxResultView* view_; | |
53 DISALLOW_COPY_AND_ASSIGN(AnswersImageObserverDesktop); | |
54 }; | |
55 | |
56 void AnswersImageObserverDesktop::OnImageChanged( | |
57 BitmapFetcherService::RequestId request_id, const SkBitmap& image) { | |
58 DCHECK(!image.empty()); | |
59 view_->SetAnswerImage(gfx::ImageSkia::CreateFrom1xBitmap(image)); | |
60 } | |
61 | |
41 // The minimum distance between the top and bottom of the {icon|text} and the | 62 // The minimum distance between the top and bottom of the {icon|text} and the |
42 // top or bottom of the row. | 63 // top or bottom of the row. |
43 const int kMinimumIconVerticalPadding = 2; | 64 const int kMinimumIconVerticalPadding = 2; |
44 const int kMinimumTextVerticalPadding = 3; | 65 const int kMinimumTextVerticalPadding = 3; |
45 | 66 |
46 // A mapping from OmniboxResultView's ResultViewState/ColorKind types to | 67 // A mapping from OmniboxResultView's ResultViewState/ColorKind types to |
47 // NativeTheme colors. | 68 // NativeTheme colors. |
48 struct TranslationTable { | 69 struct TranslationTable { |
49 ui::NativeTheme::ColorId id; | 70 ui::NativeTheme::ColorId id; |
50 OmniboxResultView::ResultViewState state; | 71 OmniboxResultView::ResultViewState state; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 int center_; | 141 int center_; |
121 int right_; | 142 int right_; |
122 | 143 |
123 DISALLOW_COPY_AND_ASSIGN(MirroringContext); | 144 DISALLOW_COPY_AND_ASSIGN(MirroringContext); |
124 }; | 145 }; |
125 | 146 |
126 OmniboxResultView::OmniboxResultView(OmniboxPopupContentsView* model, | 147 OmniboxResultView::OmniboxResultView(OmniboxPopupContentsView* model, |
127 int model_index, | 148 int model_index, |
128 LocationBarView* location_bar_view, | 149 LocationBarView* location_bar_view, |
129 const gfx::FontList& font_list) | 150 const gfx::FontList& font_list) |
130 : edge_item_padding_(LocationBarView::kItemPadding), | 151 : request_id_(BitmapFetcherService::REQUEST_ID_INVALID), |
152 edge_item_padding_(LocationBarView::kItemPadding), | |
131 item_padding_(LocationBarView::kItemPadding), | 153 item_padding_(LocationBarView::kItemPadding), |
132 minimum_text_vertical_padding_(kMinimumTextVerticalPadding), | 154 minimum_text_vertical_padding_(kMinimumTextVerticalPadding), |
133 model_(model), | 155 model_(model), |
134 model_index_(model_index), | 156 model_index_(model_index), |
135 location_bar_view_(location_bar_view), | 157 location_bar_view_(location_bar_view), |
136 font_list_(font_list), | 158 font_list_(font_list), |
137 font_height_( | 159 font_height_( |
138 std::max(font_list.GetHeight(), | 160 std::max(font_list.GetHeight(), |
139 font_list.DeriveWithStyle(gfx::Font::BOLD).GetHeight())), | 161 font_list.DeriveWithStyle(gfx::Font::BOLD).GetHeight())), |
140 mirroring_context_(new MirroringContext()), | 162 mirroring_context_(new MirroringContext()), |
141 keyword_icon_(new views::ImageView()), | 163 keyword_icon_(new views::ImageView()), |
142 animation_(new gfx::SlideAnimation(this)) { | 164 animation_(new gfx::SlideAnimation(this)) { |
143 CHECK_GE(model_index, 0); | 165 CHECK_GE(model_index, 0); |
144 if (default_icon_size_ == 0) { | 166 if (default_icon_size_ == 0) { |
145 default_icon_size_ = | 167 default_icon_size_ = |
146 location_bar_view_->GetThemeProvider()->GetImageSkiaNamed( | 168 location_bar_view_->GetThemeProvider()->GetImageSkiaNamed( |
147 AutocompleteMatch::TypeToIcon( | 169 AutocompleteMatch::TypeToIcon( |
148 AutocompleteMatchType::URL_WHAT_YOU_TYPED))->width(); | 170 AutocompleteMatchType::URL_WHAT_YOU_TYPED))->width(); |
149 } | 171 } |
150 keyword_icon_->set_owned_by_client(); | 172 keyword_icon_->set_owned_by_client(); |
151 keyword_icon_->EnableCanvasFlippingForRTLUI(true); | 173 keyword_icon_->EnableCanvasFlippingForRTLUI(true); |
152 keyword_icon_->SetImage(GetKeywordIcon()); | 174 keyword_icon_->SetImage(GetKeywordIcon()); |
153 keyword_icon_->SizeToPreferredSize(); | 175 keyword_icon_->SizeToPreferredSize(); |
154 } | 176 } |
155 | 177 |
156 OmniboxResultView::~OmniboxResultView() { | 178 OmniboxResultView::~OmniboxResultView() { |
179 BitmapFetcherServiceFactory::GetForBrowserContext( | |
180 location_bar_view_->profile())->CancelRequest(request_id_); | |
157 } | 181 } |
158 | 182 |
159 SkColor OmniboxResultView::GetColor( | 183 SkColor OmniboxResultView::GetColor( |
160 ResultViewState state, | 184 ResultViewState state, |
161 ColorKind kind) const { | 185 ColorKind kind) const { |
162 for (size_t i = 0; i < arraysize(kTranslationTable); ++i) { | 186 for (size_t i = 0; i < arraysize(kTranslationTable); ++i) { |
163 if (kTranslationTable[i].state == state && | 187 if (kTranslationTable[i].state == state && |
164 kTranslationTable[i].kind == kind) { | 188 kTranslationTable[i].kind == kind) { |
165 return GetNativeTheme()->GetSystemColor(kTranslationTable[i].id); | 189 return GetNativeTheme()->GetSystemColor(kTranslationTable[i].id); |
166 } | 190 } |
167 } | 191 } |
168 | 192 |
169 NOTREACHED(); | 193 NOTREACHED(); |
170 return SK_ColorRED; | 194 return SK_ColorRED; |
171 } | 195 } |
172 | 196 |
173 void OmniboxResultView::SetMatch(const AutocompleteMatch& match) { | 197 void OmniboxResultView::SetMatch(const AutocompleteMatch& match) { |
174 match_ = match; | 198 match_ = match; |
175 ResetRenderTexts(); | 199 ResetRenderTexts(); |
176 animation_->Reset(); | 200 animation_->Reset(); |
177 | 201 |
202 answer_image_ = gfx::ImageSkia(); | |
203 if (match_.answer && match_.answer->second_line().image_url().is_valid()) | |
204 GetAnswerIcon(); | |
205 | |
178 AutocompleteMatch* associated_keyword_match = match_.associated_keyword.get(); | 206 AutocompleteMatch* associated_keyword_match = match_.associated_keyword.get(); |
179 if (associated_keyword_match) { | 207 if (associated_keyword_match) { |
180 keyword_icon_->SetImage(GetKeywordIcon()); | 208 keyword_icon_->SetImage(GetKeywordIcon()); |
181 if (!keyword_icon_->parent()) | 209 if (!keyword_icon_->parent()) |
182 AddChildView(keyword_icon_.get()); | 210 AddChildView(keyword_icon_.get()); |
183 } else if (keyword_icon_->parent()) { | 211 } else if (keyword_icon_->parent()) { |
184 RemoveChildView(keyword_icon_.get()); | 212 RemoveChildView(keyword_icon_.get()); |
185 } | 213 } |
186 | 214 |
187 Layout(); | 215 Layout(); |
(...skipping 27 matching lines...) Expand all Loading... | |
215 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const { | 243 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const { |
216 if (model_->IsSelectedIndex(model_index_)) | 244 if (model_->IsSelectedIndex(model_index_)) |
217 return SELECTED; | 245 return SELECTED; |
218 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; | 246 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; |
219 } | 247 } |
220 | 248 |
221 int OmniboxResultView::GetTextHeight() const { | 249 int OmniboxResultView::GetTextHeight() const { |
222 return font_height_; | 250 return font_height_; |
223 } | 251 } |
224 | 252 |
225 void OmniboxResultView::PaintMatch( | 253 void OmniboxResultView::PaintMatch(const AutocompleteMatch& match, |
226 const AutocompleteMatch& match, | 254 gfx::RenderText* contents, |
227 gfx::RenderText* contents, | 255 gfx::RenderText* description, |
228 gfx::RenderText* description, | 256 gfx::Canvas* canvas, int x) const { |
229 gfx::Canvas* canvas, | |
230 int x) const { | |
231 int y = text_bounds_.y(); | 257 int y = text_bounds_.y(); |
232 | 258 |
233 if (!separator_rendertext_) { | 259 if (!separator_rendertext_) { |
234 const base::string16& separator = | 260 const base::string16& separator = |
235 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); | 261 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); |
236 separator_rendertext_.reset(CreateRenderText(separator).release()); | 262 separator_rendertext_.reset(CreateRenderText(separator).release()); |
237 separator_rendertext_->SetColor(GetColor(GetState(), DIMMED_TEXT)); | 263 separator_rendertext_->SetColor(GetColor(GetState(), DIMMED_TEXT)); |
238 separator_width_ = separator_rendertext_->GetContentWidth(); | 264 separator_width_ = separator_rendertext_->GetContentWidth(); |
239 } | 265 } |
240 | 266 |
241 int contents_max_width, description_max_width; | 267 int contents_max_width, description_max_width; |
242 OmniboxPopupModel::ComputeMatchMaxWidths( | 268 OmniboxPopupModel::ComputeMatchMaxWidths( |
243 contents->GetContentWidth(), | 269 contents->GetContentWidth(), |
244 separator_width_, | 270 separator_width_, |
245 description ? description->GetContentWidth() : 0, | 271 description ? description->GetContentWidth() : 0, |
246 mirroring_context_->remaining_width(x), | 272 mirroring_context_->remaining_width(x), |
247 !AutocompleteMatch::IsSearchType(match.type), | 273 !AutocompleteMatch::IsSearchType(match.type), |
248 &contents_max_width, | 274 &contents_max_width, |
249 &description_max_width); | 275 &description_max_width); |
250 | 276 |
251 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width); | 277 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width); |
252 | 278 |
253 if (description_max_width != 0) { | 279 if (description_max_width != 0) { |
254 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y, | 280 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y, |
255 separator_width_); | 281 separator_width_); |
282 | |
283 if (!answer_image_.size().IsEmpty()) { | |
284 canvas->DrawImageInt(answer_image_, | |
285 // Source x, y, w, h. | |
286 0, 0, answer_image_.width(), answer_image_.height(), | |
287 // Destination x, y, w, h. | |
288 GetMirroredXInView(x), | |
289 y + kMinimumIconVerticalPadding, default_icon_size_, | |
290 default_icon_size_, true); | |
291 x += default_icon_size_ + LocationBarView::kIconInternalPadding; | |
292 } | |
293 | |
256 DrawRenderText(match, description, false, canvas, x, y, | 294 DrawRenderText(match, description, false, canvas, x, y, |
257 description_max_width); | 295 description_max_width); |
258 } | 296 } |
259 } | 297 } |
260 | 298 |
261 int OmniboxResultView::DrawRenderText( | 299 int OmniboxResultView::DrawRenderText( |
262 const AutocompleteMatch& match, | 300 const AutocompleteMatch& match, |
263 gfx::RenderText* render_text, | 301 gfx::RenderText* render_text, |
264 bool contents, | 302 bool contents, |
265 gfx::Canvas* canvas, | 303 gfx::Canvas* canvas, |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
389 render_text->ApplyColor(GetColor(GetState(), color_kind), current_range); | 427 render_text->ApplyColor(GetColor(GetState(), color_kind), current_range); |
390 } | 428 } |
391 return render_text.Pass(); | 429 return render_text.Pass(); |
392 } | 430 } |
393 | 431 |
394 int OmniboxResultView::GetMatchContentsWidth() const { | 432 int OmniboxResultView::GetMatchContentsWidth() const { |
395 InitContentsRenderTextIfNecessary(); | 433 InitContentsRenderTextIfNecessary(); |
396 return contents_rendertext_ ? contents_rendertext_->GetContentWidth() : 0; | 434 return contents_rendertext_ ? contents_rendertext_->GetContentWidth() : 0; |
397 } | 435 } |
398 | 436 |
437 void OmniboxResultView::SetAnswerImage(gfx::ImageSkia image) { | |
438 answer_image_ = image; | |
439 SchedulePaint(); | |
440 } | |
441 | |
399 // TODO(skanuj): This is probably identical across all OmniboxResultView rows in | 442 // TODO(skanuj): This is probably identical across all OmniboxResultView rows in |
400 // the omnibox dropdown. Consider sharing the result. | 443 // the omnibox dropdown. Consider sharing the result. |
401 int OmniboxResultView::GetDisplayOffset( | 444 int OmniboxResultView::GetDisplayOffset( |
402 const AutocompleteMatch& match, | 445 const AutocompleteMatch& match, |
403 bool is_ui_rtl, | 446 bool is_ui_rtl, |
404 bool is_match_contents_rtl) const { | 447 bool is_match_contents_rtl) const { |
405 if (match.type != AutocompleteMatchType::SEARCH_SUGGEST_TAIL) | 448 if (match.type != AutocompleteMatchType::SEARCH_SUGGEST_TAIL) |
406 return 0; | 449 return 0; |
407 | 450 |
408 const base::string16& input_text = | 451 const base::string16& input_text = |
409 base::UTF8ToUTF16(match.GetAdditionalInfo(kACMatchPropertyInputText)); | 452 base::UTF8ToUTF16(match.GetAdditionalInfo(kACMatchPropertyInputText)); |
410 int contents_start_index = 0; | 453 int contents_start_index = 0; |
411 base::StringToInt(match.GetAdditionalInfo(kACMatchPropertyContentsStartIndex), | 454 base::StringToInt(match.GetAdditionalInfo(kACMatchPropertyContentsStartIndex), |
412 &contents_start_index); | 455 &contents_start_index); |
413 | 456 |
414 scoped_ptr<gfx::RenderText> input_render_text(CreateRenderText(input_text)); | 457 scoped_ptr<gfx::RenderText> input_render_text(CreateRenderText(input_text)); |
415 const gfx::Range& glyph_bounds = | 458 const gfx::Range& glyph_bounds = |
416 input_render_text->GetGlyphBounds(contents_start_index); | 459 input_render_text->GetGlyphBounds(contents_start_index); |
417 const int start_padding = is_match_contents_rtl ? | 460 const int start_padding = is_match_contents_rtl ? |
418 std::max(glyph_bounds.start(), glyph_bounds.end()) : | 461 std::max(glyph_bounds.start(), glyph_bounds.end()) : |
419 std::min(glyph_bounds.start(), glyph_bounds.end()); | 462 std::min(glyph_bounds.start(), glyph_bounds.end()); |
420 | 463 |
421 return is_ui_rtl ? | 464 return is_ui_rtl ? |
422 (input_render_text->GetContentWidth() - start_padding) : start_padding; | 465 (input_render_text->GetContentWidth() - start_padding) : start_padding; |
423 } | 466 } |
424 | 467 |
425 // static | 468 // static |
426 int OmniboxResultView::default_icon_size_ = 0; | 469 int OmniboxResultView::default_icon_size_ = 0; |
427 | 470 |
471 void OmniboxResultView::GetAnswerIcon() { | |
472 // The answer images are not bundled with Chrome, so we need to download | |
473 // them. | |
groby-ooo-7-16
2015/02/19 00:23:43
Personal nit: The comment is probably not helpful.
| |
474 BitmapFetcherService* image_service = | |
475 BitmapFetcherServiceFactory::GetForBrowserContext( | |
476 location_bar_view_->profile()); | |
477 DCHECK(image_service); | |
478 image_service->CancelRequest(request_id_); | |
479 request_id_ = image_service->RequestImage( | |
480 match_.answer->second_line().image_url(), | |
481 new AnswersImageObserverDesktop(this)); | |
482 } | |
483 | |
428 const char* OmniboxResultView::GetClassName() const { | 484 const char* OmniboxResultView::GetClassName() const { |
429 return "OmniboxResultView"; | 485 return "OmniboxResultView"; |
430 } | 486 } |
431 | 487 |
432 gfx::ImageSkia OmniboxResultView::GetIcon() const { | 488 gfx::ImageSkia OmniboxResultView::GetIcon() const { |
433 const gfx::Image image = model_->GetIconIfExtensionMatch(model_index_); | 489 const gfx::Image image = model_->GetIconIfExtensionMatch(model_index_); |
434 if (!image.IsEmpty()) | 490 if (!image.IsEmpty()) |
435 return image.AsImageSkia(); | 491 return image.AsImageSkia(); |
436 | 492 |
437 int icon = model_->IsStarredMatch(match_) ? | 493 int icon = model_->IsStarredMatch(match_) ? |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
568 } | 624 } |
569 PaintMatch(*keyword_match, keyword_contents_rendertext_.get(), | 625 PaintMatch(*keyword_match, keyword_contents_rendertext_.get(), |
570 keyword_description_rendertext_.get(), canvas, x); | 626 keyword_description_rendertext_.get(), canvas, x); |
571 } | 627 } |
572 } | 628 } |
573 | 629 |
574 void OmniboxResultView::AnimationProgressed(const gfx::Animation* animation) { | 630 void OmniboxResultView::AnimationProgressed(const gfx::Animation* animation) { |
575 Layout(); | 631 Layout(); |
576 SchedulePaint(); | 632 SchedulePaint(); |
577 } | 633 } |
OLD | NEW |