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( | |
47 const base::WeakPtr<OmniboxResultView>& view) | |
48 : view_(view) {} | |
49 | |
50 void OnImageChanged(BitmapFetcherService::RequestId request_id, | |
51 const SkBitmap& image) override; | |
52 | |
53 private: | |
54 const base::WeakPtr<OmniboxResultView> view_; | |
55 DISALLOW_COPY_AND_ASSIGN(AnswersImageObserverDesktop); | |
56 }; | |
57 | |
58 void AnswersImageObserverDesktop::OnImageChanged( | |
59 BitmapFetcherService::RequestId request_id, | |
60 const SkBitmap& image) { | |
61 DCHECK(!image.empty()); | |
62 if (view_) | |
63 view_.get()->SetAnswerImage(gfx::ImageSkia::CreateFrom1xBitmap(image)); | |
64 } | |
65 | |
41 // The minimum distance between the top and bottom of the {icon|text} and the | 66 // The minimum distance between the top and bottom of the {icon|text} and the |
42 // top or bottom of the row. | 67 // top or bottom of the row. |
43 const int kMinimumIconVerticalPadding = 2; | 68 const int kMinimumIconVerticalPadding = 2; |
44 const int kMinimumTextVerticalPadding = 3; | 69 const int kMinimumTextVerticalPadding = 3; |
45 | 70 |
46 // A mapping from OmniboxResultView's ResultViewState/ColorKind types to | 71 // A mapping from OmniboxResultView's ResultViewState/ColorKind types to |
47 // NativeTheme colors. | 72 // NativeTheme colors. |
48 struct TranslationTable { | 73 struct TranslationTable { |
49 ui::NativeTheme::ColorId id; | 74 ui::NativeTheme::ColorId id; |
50 OmniboxResultView::ResultViewState state; | 75 OmniboxResultView::ResultViewState state; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 int center_; | 145 int center_; |
121 int right_; | 146 int right_; |
122 | 147 |
123 DISALLOW_COPY_AND_ASSIGN(MirroringContext); | 148 DISALLOW_COPY_AND_ASSIGN(MirroringContext); |
124 }; | 149 }; |
125 | 150 |
126 OmniboxResultView::OmniboxResultView(OmniboxPopupContentsView* model, | 151 OmniboxResultView::OmniboxResultView(OmniboxPopupContentsView* model, |
127 int model_index, | 152 int model_index, |
128 LocationBarView* location_bar_view, | 153 LocationBarView* location_bar_view, |
129 const gfx::FontList& font_list) | 154 const gfx::FontList& font_list) |
130 : edge_item_padding_(LocationBarView::kItemPadding), | 155 : request_id_(BitmapFetcherService::REQUEST_ID_INVALID), |
156 edge_item_padding_(LocationBarView::kItemPadding), | |
131 item_padding_(LocationBarView::kItemPadding), | 157 item_padding_(LocationBarView::kItemPadding), |
132 minimum_text_vertical_padding_(kMinimumTextVerticalPadding), | 158 minimum_text_vertical_padding_(kMinimumTextVerticalPadding), |
133 model_(model), | 159 model_(model), |
134 model_index_(model_index), | 160 model_index_(model_index), |
135 location_bar_view_(location_bar_view), | 161 location_bar_view_(location_bar_view), |
136 font_list_(font_list), | 162 font_list_(font_list), |
137 font_height_( | 163 font_height_( |
138 std::max(font_list.GetHeight(), | 164 std::max(font_list.GetHeight(), |
139 font_list.DeriveWithStyle(gfx::Font::BOLD).GetHeight())), | 165 font_list.DeriveWithStyle(gfx::Font::BOLD).GetHeight())), |
140 mirroring_context_(new MirroringContext()), | 166 mirroring_context_(new MirroringContext()), |
141 keyword_icon_(new views::ImageView()), | 167 keyword_icon_(new views::ImageView()), |
142 animation_(new gfx::SlideAnimation(this)) { | 168 animation_(new gfx::SlideAnimation(this)), |
169 weak_ptr_factory_(this) { | |
143 CHECK_GE(model_index, 0); | 170 CHECK_GE(model_index, 0); |
144 if (default_icon_size_ == 0) { | 171 if (default_icon_size_ == 0) { |
145 default_icon_size_ = | 172 default_icon_size_ = |
146 location_bar_view_->GetThemeProvider()->GetImageSkiaNamed( | 173 location_bar_view_->GetThemeProvider()->GetImageSkiaNamed( |
147 AutocompleteMatch::TypeToIcon( | 174 AutocompleteMatch::TypeToIcon( |
148 AutocompleteMatchType::URL_WHAT_YOU_TYPED))->width(); | 175 AutocompleteMatchType::URL_WHAT_YOU_TYPED))->width(); |
149 } | 176 } |
150 keyword_icon_->set_owned_by_client(); | 177 keyword_icon_->set_owned_by_client(); |
151 keyword_icon_->EnableCanvasFlippingForRTLUI(true); | 178 keyword_icon_->EnableCanvasFlippingForRTLUI(true); |
152 keyword_icon_->SetImage(GetKeywordIcon()); | 179 keyword_icon_->SetImage(GetKeywordIcon()); |
153 keyword_icon_->SizeToPreferredSize(); | 180 keyword_icon_->SizeToPreferredSize(); |
181 image_service_ = BitmapFetcherServiceFactory::GetForBrowserContext( | |
182 location_bar_view_->profile()); | |
Peter Kasting
2015/03/03 01:32:36
Nit: Init this in the initializer list
dschuyler
2015/03/03 21:13:56
Done.
| |
154 } | 183 } |
155 | 184 |
156 OmniboxResultView::~OmniboxResultView() { | 185 OmniboxResultView::~OmniboxResultView() { |
186 if (image_service_) | |
187 image_service_->CancelRequest(request_id_); | |
157 } | 188 } |
158 | 189 |
159 SkColor OmniboxResultView::GetColor( | 190 SkColor OmniboxResultView::GetColor( |
160 ResultViewState state, | 191 ResultViewState state, |
161 ColorKind kind) const { | 192 ColorKind kind) const { |
162 for (size_t i = 0; i < arraysize(kTranslationTable); ++i) { | 193 for (size_t i = 0; i < arraysize(kTranslationTable); ++i) { |
163 if (kTranslationTable[i].state == state && | 194 if (kTranslationTable[i].state == state && |
164 kTranslationTable[i].kind == kind) { | 195 kTranslationTable[i].kind == kind) { |
165 return GetNativeTheme()->GetSystemColor(kTranslationTable[i].id); | 196 return GetNativeTheme()->GetSystemColor(kTranslationTable[i].id); |
166 } | 197 } |
167 } | 198 } |
168 | 199 |
169 NOTREACHED(); | 200 NOTREACHED(); |
170 return SK_ColorRED; | 201 return SK_ColorRED; |
171 } | 202 } |
172 | 203 |
173 void OmniboxResultView::SetMatch(const AutocompleteMatch& match) { | 204 void OmniboxResultView::SetMatch(const AutocompleteMatch& match) { |
174 match_ = match; | 205 match_ = match; |
175 ResetRenderTexts(); | 206 ResetRenderTexts(); |
176 animation_->Reset(); | 207 animation_->Reset(); |
177 | 208 |
209 answer_image_ = gfx::ImageSkia(); | |
210 if (match_.answer && match_.answer->second_line().image_url().is_valid()) | |
211 GetAnswerIcon(); | |
Peter Kasting
2015/03/03 01:32:36
It seems sort of weird to have caller code that co
dschuyler
2015/03/03 21:13:56
Done.
| |
212 | |
178 AutocompleteMatch* associated_keyword_match = match_.associated_keyword.get(); | 213 AutocompleteMatch* associated_keyword_match = match_.associated_keyword.get(); |
179 if (associated_keyword_match) { | 214 if (associated_keyword_match) { |
180 keyword_icon_->SetImage(GetKeywordIcon()); | 215 keyword_icon_->SetImage(GetKeywordIcon()); |
181 if (!keyword_icon_->parent()) | 216 if (!keyword_icon_->parent()) |
182 AddChildView(keyword_icon_.get()); | 217 AddChildView(keyword_icon_.get()); |
183 } else if (keyword_icon_->parent()) { | 218 } else if (keyword_icon_->parent()) { |
184 RemoveChildView(keyword_icon_.get()); | 219 RemoveChildView(keyword_icon_.get()); |
185 } | 220 } |
186 | 221 |
187 Layout(); | 222 Layout(); |
(...skipping 27 matching lines...) Expand all Loading... | |
215 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const { | 250 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const { |
216 if (model_->IsSelectedIndex(model_index_)) | 251 if (model_->IsSelectedIndex(model_index_)) |
217 return SELECTED; | 252 return SELECTED; |
218 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; | 253 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; |
219 } | 254 } |
220 | 255 |
221 int OmniboxResultView::GetTextHeight() const { | 256 int OmniboxResultView::GetTextHeight() const { |
222 return font_height_; | 257 return font_height_; |
223 } | 258 } |
224 | 259 |
225 void OmniboxResultView::PaintMatch( | 260 void OmniboxResultView::PaintMatch(const AutocompleteMatch& match, |
226 const AutocompleteMatch& match, | 261 gfx::RenderText* contents, |
227 gfx::RenderText* contents, | 262 gfx::RenderText* description, |
228 gfx::RenderText* description, | 263 gfx::Canvas* canvas, |
229 gfx::Canvas* canvas, | 264 int x) const { |
230 int x) const { | |
231 int y = text_bounds_.y(); | 265 int y = text_bounds_.y(); |
232 | 266 |
233 if (!separator_rendertext_) { | 267 if (!separator_rendertext_) { |
234 const base::string16& separator = | 268 const base::string16& separator = |
235 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); | 269 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); |
236 separator_rendertext_.reset(CreateRenderText(separator).release()); | 270 separator_rendertext_.reset(CreateRenderText(separator).release()); |
237 separator_rendertext_->SetColor(GetColor(GetState(), DIMMED_TEXT)); | 271 separator_rendertext_->SetColor(GetColor(GetState(), DIMMED_TEXT)); |
238 separator_width_ = separator_rendertext_->GetContentWidth(); | 272 separator_width_ = separator_rendertext_->GetContentWidth(); |
239 } | 273 } |
240 | 274 |
241 int contents_max_width, description_max_width; | 275 int contents_max_width, description_max_width; |
242 OmniboxPopupModel::ComputeMatchMaxWidths( | 276 OmniboxPopupModel::ComputeMatchMaxWidths( |
243 contents->GetContentWidth(), | 277 contents->GetContentWidth(), |
244 separator_width_, | 278 separator_width_, |
245 description ? description->GetContentWidth() : 0, | 279 description ? description->GetContentWidth() : 0, |
246 mirroring_context_->remaining_width(x), | 280 mirroring_context_->remaining_width(x), |
247 !AutocompleteMatch::IsSearchType(match.type), | 281 !AutocompleteMatch::IsSearchType(match.type), |
248 &contents_max_width, | 282 &contents_max_width, |
249 &description_max_width); | 283 &description_max_width); |
250 | 284 |
251 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width); | 285 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width); |
252 | 286 |
253 if (description_max_width != 0) { | 287 if (description_max_width != 0) { |
254 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y, | 288 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y, |
255 separator_width_); | 289 separator_width_); |
290 | |
291 if (!answer_image_.size().IsEmpty()) { | |
292 canvas->DrawImageInt(answer_image_, | |
293 // Source x, y, w, h. | |
294 0, 0, answer_image_.width(), answer_image_.height(), | |
295 // Destination x, y, w, h. | |
296 GetMirroredXInView(x), | |
297 y + kMinimumIconVerticalPadding, default_icon_size_, | |
298 default_icon_size_, true); | |
299 x += default_icon_size_ + LocationBarView::kIconInternalPadding; | |
300 } | |
301 | |
256 DrawRenderText(match, description, false, canvas, x, y, | 302 DrawRenderText(match, description, false, canvas, x, y, |
257 description_max_width); | 303 description_max_width); |
258 } | 304 } |
259 } | 305 } |
260 | 306 |
261 int OmniboxResultView::DrawRenderText( | 307 int OmniboxResultView::DrawRenderText( |
262 const AutocompleteMatch& match, | 308 const AutocompleteMatch& match, |
263 gfx::RenderText* render_text, | 309 gfx::RenderText* render_text, |
264 bool contents, | 310 bool contents, |
265 gfx::Canvas* canvas, | 311 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); | 435 render_text->ApplyColor(GetColor(GetState(), color_kind), current_range); |
390 } | 436 } |
391 return render_text.Pass(); | 437 return render_text.Pass(); |
392 } | 438 } |
393 | 439 |
394 int OmniboxResultView::GetMatchContentsWidth() const { | 440 int OmniboxResultView::GetMatchContentsWidth() const { |
395 InitContentsRenderTextIfNecessary(); | 441 InitContentsRenderTextIfNecessary(); |
396 return contents_rendertext_ ? contents_rendertext_->GetContentWidth() : 0; | 442 return contents_rendertext_ ? contents_rendertext_->GetContentWidth() : 0; |
397 } | 443 } |
398 | 444 |
445 void OmniboxResultView::SetAnswerImage(gfx::ImageSkia image) { | |
446 answer_image_ = image; | |
447 SchedulePaint(); | |
448 } | |
449 | |
399 // TODO(skanuj): This is probably identical across all OmniboxResultView rows in | 450 // TODO(skanuj): This is probably identical across all OmniboxResultView rows in |
400 // the omnibox dropdown. Consider sharing the result. | 451 // the omnibox dropdown. Consider sharing the result. |
401 int OmniboxResultView::GetDisplayOffset( | 452 int OmniboxResultView::GetDisplayOffset( |
402 const AutocompleteMatch& match, | 453 const AutocompleteMatch& match, |
403 bool is_ui_rtl, | 454 bool is_ui_rtl, |
404 bool is_match_contents_rtl) const { | 455 bool is_match_contents_rtl) const { |
405 if (match.type != AutocompleteMatchType::SEARCH_SUGGEST_TAIL) | 456 if (match.type != AutocompleteMatchType::SEARCH_SUGGEST_TAIL) |
406 return 0; | 457 return 0; |
407 | 458 |
408 const base::string16& input_text = | 459 const base::string16& input_text = |
409 base::UTF8ToUTF16(match.GetAdditionalInfo(kACMatchPropertyInputText)); | 460 base::UTF8ToUTF16(match.GetAdditionalInfo(kACMatchPropertyInputText)); |
410 int contents_start_index = 0; | 461 int contents_start_index = 0; |
411 base::StringToInt(match.GetAdditionalInfo(kACMatchPropertyContentsStartIndex), | 462 base::StringToInt(match.GetAdditionalInfo(kACMatchPropertyContentsStartIndex), |
412 &contents_start_index); | 463 &contents_start_index); |
413 | 464 |
414 scoped_ptr<gfx::RenderText> input_render_text(CreateRenderText(input_text)); | 465 scoped_ptr<gfx::RenderText> input_render_text(CreateRenderText(input_text)); |
415 const gfx::Range& glyph_bounds = | 466 const gfx::Range& glyph_bounds = |
416 input_render_text->GetGlyphBounds(contents_start_index); | 467 input_render_text->GetGlyphBounds(contents_start_index); |
417 const int start_padding = is_match_contents_rtl ? | 468 const int start_padding = is_match_contents_rtl ? |
418 std::max(glyph_bounds.start(), glyph_bounds.end()) : | 469 std::max(glyph_bounds.start(), glyph_bounds.end()) : |
419 std::min(glyph_bounds.start(), glyph_bounds.end()); | 470 std::min(glyph_bounds.start(), glyph_bounds.end()); |
420 | 471 |
421 return is_ui_rtl ? | 472 return is_ui_rtl ? |
422 (input_render_text->GetContentWidth() - start_padding) : start_padding; | 473 (input_render_text->GetContentWidth() - start_padding) : start_padding; |
423 } | 474 } |
424 | 475 |
425 // static | 476 // static |
426 int OmniboxResultView::default_icon_size_ = 0; | 477 int OmniboxResultView::default_icon_size_ = 0; |
427 | 478 |
479 void OmniboxResultView::GetAnswerIcon() { | |
480 // The answer images are not bundled with Chrome, so we need to download | |
Peter Kasting
2015/03/03 01:32:36
Is there a reason the reader would have expected t
dschuyler
2015/03/03 21:13:56
Done.
| |
481 // them. | |
482 if (image_service_) { | |
483 image_service_->CancelRequest(request_id_); | |
484 request_id_ = image_service_->RequestImage( | |
485 match_.answer->second_line().image_url(), | |
486 new AnswersImageObserverDesktop(GetWeakPtr())); | |
487 } | |
488 } | |
489 | |
428 const char* OmniboxResultView::GetClassName() const { | 490 const char* OmniboxResultView::GetClassName() const { |
429 return "OmniboxResultView"; | 491 return "OmniboxResultView"; |
430 } | 492 } |
431 | 493 |
432 gfx::ImageSkia OmniboxResultView::GetIcon() const { | 494 gfx::ImageSkia OmniboxResultView::GetIcon() const { |
433 const gfx::Image image = model_->GetIconIfExtensionMatch(model_index_); | 495 const gfx::Image image = model_->GetIconIfExtensionMatch(model_index_); |
434 if (!image.IsEmpty()) | 496 if (!image.IsEmpty()) |
435 return image.AsImageSkia(); | 497 return image.AsImageSkia(); |
436 | 498 |
437 int icon = model_->IsStarredMatch(match_) ? | 499 int icon = model_->IsStarredMatch(match_) ? |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
568 } | 630 } |
569 PaintMatch(*keyword_match, keyword_contents_rendertext_.get(), | 631 PaintMatch(*keyword_match, keyword_contents_rendertext_.get(), |
570 keyword_description_rendertext_.get(), canvas, x); | 632 keyword_description_rendertext_.get(), canvas, x); |
571 } | 633 } |
572 } | 634 } |
573 | 635 |
574 void OmniboxResultView::AnimationProgressed(const gfx::Animation* animation) { | 636 void OmniboxResultView::AnimationProgressed(const gfx::Animation* animation) { |
575 Layout(); | 637 Layout(); |
576 SchedulePaint(); | 638 SchedulePaint(); |
577 } | 639 } |
OLD | NEW |