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 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const { | 239 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const { |
216 if (model_->IsSelectedIndex(model_index_)) | 240 if (model_->IsSelectedIndex(model_index_)) |
217 return SELECTED; | 241 return SELECTED; |
218 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; | 242 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; |
219 } | 243 } |
220 | 244 |
221 int OmniboxResultView::GetTextHeight() const { | 245 int OmniboxResultView::GetTextHeight() const { |
222 return font_height_; | 246 return font_height_; |
223 } | 247 } |
224 | 248 |
225 void OmniboxResultView::PaintMatch( | 249 void OmniboxResultView::PaintMatch(const AutocompleteMatch& match, |
dschuyler
2015/02/13 22:25:26
This reformatting was done by clang-format. Shoul
Peter Kasting
2015/02/13 22:36:36
I would just break "int x" to a new line. I know
groby-ooo-7-16
2015/02/13 22:47:22
While I'm technically fine with either formatting,
Peter Kasting
2015/02/13 22:49:39
All omnibox code, at least, should only be indenti
dschuyler
2015/02/14 00:24:31
Acknowledged.
dschuyler
2015/02/14 00:24:31
Acknowledged.
dschuyler
2015/02/14 00:24:32
If I'm following this correctly, the last thing sa
| |
226 const AutocompleteMatch& match, | 250 gfx::RenderText* contents, |
227 gfx::RenderText* contents, | 251 gfx::RenderText* description, |
228 gfx::RenderText* description, | 252 gfx::Canvas* canvas, int x) const { |
229 gfx::Canvas* canvas, | |
230 int x) const { | |
231 int y = text_bounds_.y(); | 253 int y = text_bounds_.y(); |
232 | 254 |
233 if (!separator_rendertext_) { | 255 if (!separator_rendertext_) { |
234 const base::string16& separator = | 256 const base::string16& separator = |
235 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); | 257 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); |
236 separator_rendertext_.reset(CreateRenderText(separator).release()); | 258 separator_rendertext_.reset(CreateRenderText(separator).release()); |
237 separator_rendertext_->SetColor(GetColor(GetState(), DIMMED_TEXT)); | 259 separator_rendertext_->SetColor(GetColor(GetState(), DIMMED_TEXT)); |
238 separator_width_ = separator_rendertext_->GetContentWidth(); | 260 separator_width_ = separator_rendertext_->GetContentWidth(); |
239 } | 261 } |
240 | 262 |
241 int contents_max_width, description_max_width; | 263 int contents_max_width, description_max_width; |
242 OmniboxPopupModel::ComputeMatchMaxWidths( | 264 OmniboxPopupModel::ComputeMatchMaxWidths( |
243 contents->GetContentWidth(), | 265 contents->GetContentWidth(), |
244 separator_width_, | 266 separator_width_, |
245 description ? description->GetContentWidth() : 0, | 267 description ? description->GetContentWidth() : 0, |
246 mirroring_context_->remaining_width(x), | 268 mirroring_context_->remaining_width(x), |
247 !AutocompleteMatch::IsSearchType(match.type), | 269 !AutocompleteMatch::IsSearchType(match.type), |
248 &contents_max_width, | 270 &contents_max_width, |
249 &description_max_width); | 271 &description_max_width); |
250 | 272 |
251 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width); | 273 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width); |
252 | 274 |
253 if (description_max_width != 0) { | 275 if (description_max_width != 0) { |
254 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y, | 276 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y, |
255 separator_width_); | 277 separator_width_); |
278 | |
279 if (!answer_image_.size().IsEmpty()) { | |
280 canvas->DrawImageInt(answer_image_, | |
281 // Source x, y, w, h. | |
Peter Kasting
2015/02/14 00:10:14
Nit: I'd normally omit in-the-middle-of-a-bunch-of
| |
282 0, 0, answer_image_.width(), answer_image_.height(), | |
283 // Destination x, y, w, h. | |
284 GetMirroredXInView(x), | |
285 y + kMinimumIconVerticalPadding, default_icon_size_, | |
286 default_icon_size_, true); | |
287 x += default_icon_size_ + LocationBarView::kIconInternalPadding; | |
288 } | |
289 | |
256 DrawRenderText(match, description, false, canvas, x, y, | 290 DrawRenderText(match, description, false, canvas, x, y, |
257 description_max_width); | 291 description_max_width); |
258 } | 292 } |
259 } | 293 } |
260 | 294 |
261 int OmniboxResultView::DrawRenderText( | 295 int OmniboxResultView::DrawRenderText( |
262 const AutocompleteMatch& match, | 296 const AutocompleteMatch& match, |
263 gfx::RenderText* render_text, | 297 gfx::RenderText* render_text, |
264 bool contents, | 298 bool contents, |
265 gfx::Canvas* canvas, | 299 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); | 423 render_text->ApplyColor(GetColor(GetState(), color_kind), current_range); |
390 } | 424 } |
391 return render_text.Pass(); | 425 return render_text.Pass(); |
392 } | 426 } |
393 | 427 |
394 int OmniboxResultView::GetMatchContentsWidth() const { | 428 int OmniboxResultView::GetMatchContentsWidth() const { |
395 InitContentsRenderTextIfNecessary(); | 429 InitContentsRenderTextIfNecessary(); |
396 return contents_rendertext_ ? contents_rendertext_->GetContentWidth() : 0; | 430 return contents_rendertext_ ? contents_rendertext_->GetContentWidth() : 0; |
397 } | 431 } |
398 | 432 |
433 void OmniboxResultView::SetAnswerImage(gfx::ImageSkia image) { | |
434 answer_image_ = image; | |
435 SchedulePaint(); | |
436 } | |
437 | |
399 // TODO(skanuj): This is probably identical across all OmniboxResultView rows in | 438 // TODO(skanuj): This is probably identical across all OmniboxResultView rows in |
400 // the omnibox dropdown. Consider sharing the result. | 439 // the omnibox dropdown. Consider sharing the result. |
401 int OmniboxResultView::GetDisplayOffset( | 440 int OmniboxResultView::GetDisplayOffset( |
402 const AutocompleteMatch& match, | 441 const AutocompleteMatch& match, |
403 bool is_ui_rtl, | 442 bool is_ui_rtl, |
404 bool is_match_contents_rtl) const { | 443 bool is_match_contents_rtl) const { |
405 if (match.type != AutocompleteMatchType::SEARCH_SUGGEST_TAIL) | 444 if (match.type != AutocompleteMatchType::SEARCH_SUGGEST_TAIL) |
406 return 0; | 445 return 0; |
407 | 446 |
408 const base::string16& input_text = | 447 const base::string16& input_text = |
409 base::UTF8ToUTF16(match.GetAdditionalInfo(kACMatchPropertyInputText)); | 448 base::UTF8ToUTF16(match.GetAdditionalInfo(kACMatchPropertyInputText)); |
410 int contents_start_index = 0; | 449 int contents_start_index = 0; |
411 base::StringToInt(match.GetAdditionalInfo(kACMatchPropertyContentsStartIndex), | 450 base::StringToInt(match.GetAdditionalInfo(kACMatchPropertyContentsStartIndex), |
412 &contents_start_index); | 451 &contents_start_index); |
413 | 452 |
414 scoped_ptr<gfx::RenderText> input_render_text(CreateRenderText(input_text)); | 453 scoped_ptr<gfx::RenderText> input_render_text(CreateRenderText(input_text)); |
415 const gfx::Range& glyph_bounds = | 454 const gfx::Range& glyph_bounds = |
416 input_render_text->GetGlyphBounds(contents_start_index); | 455 input_render_text->GetGlyphBounds(contents_start_index); |
417 const int start_padding = is_match_contents_rtl ? | 456 const int start_padding = is_match_contents_rtl ? |
418 std::max(glyph_bounds.start(), glyph_bounds.end()) : | 457 std::max(glyph_bounds.start(), glyph_bounds.end()) : |
419 std::min(glyph_bounds.start(), glyph_bounds.end()); | 458 std::min(glyph_bounds.start(), glyph_bounds.end()); |
420 | 459 |
421 return is_ui_rtl ? | 460 return is_ui_rtl ? |
422 (input_render_text->GetContentWidth() - start_padding) : start_padding; | 461 (input_render_text->GetContentWidth() - start_padding) : start_padding; |
423 } | 462 } |
424 | 463 |
425 // static | 464 // static |
426 int OmniboxResultView::default_icon_size_ = 0; | 465 int OmniboxResultView::default_icon_size_ = 0; |
427 | 466 |
467 void OmniboxResultView::GetAnswerIcon() { | |
468 // Check to see if we have already requested this image. | |
469 const GURL& image_url = match_.answer->second_line().image_url(); | |
470 if (image_url == image_url_) | |
471 // We have already requested this url. | |
Peter Kasting
2015/02/14 00:10:14
Nit: A comment here would necessitate {} on the co
dschuyler
2015/02/14 00:24:32
I removed the image_url_ entirely.
Done.
| |
472 return; | |
473 | |
474 // We don't have that image, so we will try to get it. | |
475 answer_image_ = gfx::ImageSkia(); | |
476 | |
477 // Make a new image download request. | |
Peter Kasting
2015/02/14 00:10:14
Nit: This block doesn't actually make a new downlo
dschuyler
2015/02/14 00:24:31
Done.
| |
478 BitmapFetcherService* image_service = | |
479 BitmapFetcherServiceFactory::GetForBrowserContext( | |
480 location_bar_view_->profile()); | |
481 DCHECK(image_service); | |
482 | |
483 // Cancel the prior request. | |
Peter Kasting
2015/02/14 00:10:15
Nit: Omit this comment as it merely restates the c
dschuyler
2015/02/14 00:24:31
Done.
| |
484 image_service->CancelRequest(request_id_); | |
485 | |
486 // Request the new image. | |
487 image_url_ = image_url; | |
488 request_id_ = image_service->RequestImage( | |
489 image_url, new AnswersImageObserverDesktop(this)); | |
490 } | |
491 | |
428 const char* OmniboxResultView::GetClassName() const { | 492 const char* OmniboxResultView::GetClassName() const { |
429 return "OmniboxResultView"; | 493 return "OmniboxResultView"; |
430 } | 494 } |
431 | 495 |
432 gfx::ImageSkia OmniboxResultView::GetIcon() const { | 496 gfx::ImageSkia OmniboxResultView::GetIcon() const { |
433 const gfx::Image image = model_->GetIconIfExtensionMatch(model_index_); | 497 const gfx::Image image = model_->GetIconIfExtensionMatch(model_index_); |
434 if (!image.IsEmpty()) | 498 if (!image.IsEmpty()) |
435 return image.AsImageSkia(); | 499 return image.AsImageSkia(); |
436 | 500 |
437 int icon = model_->IsStarredMatch(match_) ? | 501 int icon = model_->IsStarredMatch(match_) ? |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
533 int x = GetMirroredXForRect(text_bounds_); | 597 int x = GetMirroredXForRect(text_bounds_); |
534 mirroring_context_->Initialize(x, text_bounds_.width()); | 598 mirroring_context_->Initialize(x, text_bounds_.width()); |
535 InitContentsRenderTextIfNecessary(); | 599 InitContentsRenderTextIfNecessary(); |
536 | 600 |
537 if (!description_rendertext_) { | 601 if (!description_rendertext_) { |
538 if (match_.answer) { | 602 if (match_.answer) { |
539 base::string16 text; | 603 base::string16 text; |
540 for (const auto& textfield : match_.answer->second_line().text_fields()) | 604 for (const auto& textfield : match_.answer->second_line().text_fields()) |
541 text += textfield.text(); | 605 text += textfield.text(); |
542 description_rendertext_ = CreateRenderText(text); | 606 description_rendertext_ = CreateRenderText(text); |
607 if (match_.answer->second_line().image_url().is_valid()) { | |
Peter Kasting
2015/02/14 00:10:14
Nit: No {} (omnibox file style)
dschuyler
2015/02/14 00:24:32
Done.
| |
608 GetAnswerIcon(); | |
609 } | |
543 } else if (!match_.description.empty()) { | 610 } else if (!match_.description.empty()) { |
544 description_rendertext_ = CreateClassifiedRenderText( | 611 description_rendertext_ = CreateClassifiedRenderText( |
545 match_.description, match_.description_class, true); | 612 match_.description, match_.description_class, true); |
546 } | 613 } |
547 } | 614 } |
548 PaintMatch(match_, contents_rendertext_.get(), | 615 PaintMatch(match_, contents_rendertext_.get(), |
549 description_rendertext_.get(), canvas, x); | 616 description_rendertext_.get(), canvas, x); |
550 } | 617 } |
551 | 618 |
552 AutocompleteMatch* keyword_match = match_.associated_keyword.get(); | 619 AutocompleteMatch* keyword_match = match_.associated_keyword.get(); |
(...skipping 15 matching lines...) Expand all Loading... | |
568 } | 635 } |
569 PaintMatch(*keyword_match, keyword_contents_rendertext_.get(), | 636 PaintMatch(*keyword_match, keyword_contents_rendertext_.get(), |
570 keyword_description_rendertext_.get(), canvas, x); | 637 keyword_description_rendertext_.get(), canvas, x); |
571 } | 638 } |
572 } | 639 } |
573 | 640 |
574 void OmniboxResultView::AnimationProgressed(const gfx::Animation* animation) { | 641 void OmniboxResultView::AnimationProgressed(const gfx::Animation* animation) { |
575 Layout(); | 642 Layout(); |
576 SchedulePaint(); | 643 SchedulePaint(); |
577 } | 644 } |
OLD | NEW |