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 // Calls back to the OmniboxResultView when the requested image is downloaded. |
| 44 // This is a separate class instead of being implemented on OmniboxResultView |
| 45 // because BitmapFetcherService currently takes ownership of this object. |
| 46 // TODO(dschuyler): Make BitmapFetcherService use the more typical non-owning |
| 47 // ObserverList pattern and have OmniboxResultView implement the Observer call |
| 48 // directly. |
| 49 class AnswerImageObserver : public BitmapFetcherService::Observer { |
| 50 public: |
| 51 explicit AnswerImageObserver( |
| 52 const base::WeakPtr<OmniboxResultView>& view) |
| 53 : view_(view) {} |
| 54 |
| 55 void OnImageChanged(BitmapFetcherService::RequestId request_id, |
| 56 const SkBitmap& image) override; |
| 57 |
| 58 private: |
| 59 const base::WeakPtr<OmniboxResultView> view_; |
| 60 DISALLOW_COPY_AND_ASSIGN(AnswerImageObserver); |
| 61 }; |
| 62 |
| 63 void AnswerImageObserver::OnImageChanged( |
| 64 BitmapFetcherService::RequestId request_id, |
| 65 const SkBitmap& image) { |
| 66 DCHECK(!image.empty()); |
| 67 if (view_) |
| 68 view_->SetAnswerImage(gfx::ImageSkia::CreateFrom1xBitmap(image)); |
| 69 } |
| 70 |
41 // The minimum distance between the top and bottom of the {icon|text} and the | 71 // The minimum distance between the top and bottom of the {icon|text} and the |
42 // top or bottom of the row. | 72 // top or bottom of the row. |
43 const int kMinimumIconVerticalPadding = 2; | 73 const int kMinimumIconVerticalPadding = 2; |
44 const int kMinimumTextVerticalPadding = 3; | 74 const int kMinimumTextVerticalPadding = 3; |
45 | 75 |
46 // A mapping from OmniboxResultView's ResultViewState/ColorKind types to | 76 // A mapping from OmniboxResultView's ResultViewState/ColorKind types to |
47 // NativeTheme colors. | 77 // NativeTheme colors. |
48 struct TranslationTable { | 78 struct TranslationTable { |
49 ui::NativeTheme::ColorId id; | 79 ui::NativeTheme::ColorId id; |
50 OmniboxResultView::ResultViewState state; | 80 OmniboxResultView::ResultViewState state; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 OmniboxResultView::OmniboxResultView(OmniboxPopupContentsView* model, | 156 OmniboxResultView::OmniboxResultView(OmniboxPopupContentsView* model, |
127 int model_index, | 157 int model_index, |
128 LocationBarView* location_bar_view, | 158 LocationBarView* location_bar_view, |
129 const gfx::FontList& font_list) | 159 const gfx::FontList& font_list) |
130 : edge_item_padding_(LocationBarView::kItemPadding), | 160 : edge_item_padding_(LocationBarView::kItemPadding), |
131 item_padding_(LocationBarView::kItemPadding), | 161 item_padding_(LocationBarView::kItemPadding), |
132 minimum_text_vertical_padding_(kMinimumTextVerticalPadding), | 162 minimum_text_vertical_padding_(kMinimumTextVerticalPadding), |
133 model_(model), | 163 model_(model), |
134 model_index_(model_index), | 164 model_index_(model_index), |
135 location_bar_view_(location_bar_view), | 165 location_bar_view_(location_bar_view), |
| 166 image_service_(BitmapFetcherServiceFactory::GetForBrowserContext( |
| 167 location_bar_view_->profile())), |
136 font_list_(font_list), | 168 font_list_(font_list), |
137 font_height_( | 169 font_height_( |
138 std::max(font_list.GetHeight(), | 170 std::max(font_list.GetHeight(), |
139 font_list.DeriveWithStyle(gfx::Font::BOLD).GetHeight())), | 171 font_list.DeriveWithStyle(gfx::Font::BOLD).GetHeight())), |
140 mirroring_context_(new MirroringContext()), | 172 mirroring_context_(new MirroringContext()), |
141 keyword_icon_(new views::ImageView()), | 173 keyword_icon_(new views::ImageView()), |
142 animation_(new gfx::SlideAnimation(this)) { | 174 animation_(new gfx::SlideAnimation(this)), |
| 175 request_id_(BitmapFetcherService::REQUEST_ID_INVALID), |
| 176 weak_ptr_factory_(this) { |
143 CHECK_GE(model_index, 0); | 177 CHECK_GE(model_index, 0); |
144 if (default_icon_size_ == 0) { | 178 if (default_icon_size_ == 0) { |
145 default_icon_size_ = | 179 default_icon_size_ = |
146 location_bar_view_->GetThemeProvider()->GetImageSkiaNamed( | 180 location_bar_view_->GetThemeProvider()->GetImageSkiaNamed( |
147 AutocompleteMatch::TypeToIcon( | 181 AutocompleteMatch::TypeToIcon( |
148 AutocompleteMatchType::URL_WHAT_YOU_TYPED))->width(); | 182 AutocompleteMatchType::URL_WHAT_YOU_TYPED))->width(); |
149 } | 183 } |
150 keyword_icon_->set_owned_by_client(); | 184 keyword_icon_->set_owned_by_client(); |
151 keyword_icon_->EnableCanvasFlippingForRTLUI(true); | 185 keyword_icon_->EnableCanvasFlippingForRTLUI(true); |
152 keyword_icon_->SetImage(GetKeywordIcon()); | 186 keyword_icon_->SetImage(GetKeywordIcon()); |
153 keyword_icon_->SizeToPreferredSize(); | 187 keyword_icon_->SizeToPreferredSize(); |
154 } | 188 } |
155 | 189 |
156 OmniboxResultView::~OmniboxResultView() { | 190 OmniboxResultView::~OmniboxResultView() { |
| 191 if (image_service_) |
| 192 image_service_->CancelRequest(request_id_); |
157 } | 193 } |
158 | 194 |
159 SkColor OmniboxResultView::GetColor( | 195 SkColor OmniboxResultView::GetColor( |
160 ResultViewState state, | 196 ResultViewState state, |
161 ColorKind kind) const { | 197 ColorKind kind) const { |
162 for (size_t i = 0; i < arraysize(kTranslationTable); ++i) { | 198 for (size_t i = 0; i < arraysize(kTranslationTable); ++i) { |
163 if (kTranslationTable[i].state == state && | 199 if (kTranslationTable[i].state == state && |
164 kTranslationTable[i].kind == kind) { | 200 kTranslationTable[i].kind == kind) { |
165 return GetNativeTheme()->GetSystemColor(kTranslationTable[i].id); | 201 return GetNativeTheme()->GetSystemColor(kTranslationTable[i].id); |
166 } | 202 } |
167 } | 203 } |
168 | 204 |
169 NOTREACHED(); | 205 NOTREACHED(); |
170 return SK_ColorRED; | 206 return SK_ColorRED; |
171 } | 207 } |
172 | 208 |
173 void OmniboxResultView::SetMatch(const AutocompleteMatch& match) { | 209 void OmniboxResultView::SetMatch(const AutocompleteMatch& match) { |
174 match_ = match; | 210 match_ = match; |
175 ResetRenderTexts(); | 211 ResetRenderTexts(); |
176 animation_->Reset(); | 212 animation_->Reset(); |
177 | 213 |
| 214 answer_image_ = gfx::ImageSkia(); |
| 215 if (image_service_) { |
| 216 image_service_->CancelRequest(request_id_); |
| 217 if (match_.answer) { |
| 218 request_id_ = image_service_->RequestImage( |
| 219 match_.answer->second_line().image_url(), |
| 220 new AnswerImageObserver(weak_ptr_factory_.GetWeakPtr())); |
| 221 } |
| 222 } |
| 223 |
178 AutocompleteMatch* associated_keyword_match = match_.associated_keyword.get(); | 224 AutocompleteMatch* associated_keyword_match = match_.associated_keyword.get(); |
179 if (associated_keyword_match) { | 225 if (associated_keyword_match) { |
180 keyword_icon_->SetImage(GetKeywordIcon()); | 226 keyword_icon_->SetImage(GetKeywordIcon()); |
181 if (!keyword_icon_->parent()) | 227 if (!keyword_icon_->parent()) |
182 AddChildView(keyword_icon_.get()); | 228 AddChildView(keyword_icon_.get()); |
183 } else if (keyword_icon_->parent()) { | 229 } else if (keyword_icon_->parent()) { |
184 RemoveChildView(keyword_icon_.get()); | 230 RemoveChildView(keyword_icon_.get()); |
185 } | 231 } |
186 | 232 |
187 Layout(); | 233 Layout(); |
(...skipping 27 matching lines...) Expand all Loading... |
215 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const { | 261 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const { |
216 if (model_->IsSelectedIndex(model_index_)) | 262 if (model_->IsSelectedIndex(model_index_)) |
217 return SELECTED; | 263 return SELECTED; |
218 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; | 264 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; |
219 } | 265 } |
220 | 266 |
221 int OmniboxResultView::GetTextHeight() const { | 267 int OmniboxResultView::GetTextHeight() const { |
222 return font_height_; | 268 return font_height_; |
223 } | 269 } |
224 | 270 |
225 void OmniboxResultView::PaintMatch( | 271 void OmniboxResultView::PaintMatch(const AutocompleteMatch& match, |
226 const AutocompleteMatch& match, | 272 gfx::RenderText* contents, |
227 gfx::RenderText* contents, | 273 gfx::RenderText* description, |
228 gfx::RenderText* description, | 274 gfx::Canvas* canvas, |
229 gfx::Canvas* canvas, | 275 int x) const { |
230 int x) const { | |
231 int y = text_bounds_.y(); | 276 int y = text_bounds_.y(); |
232 | 277 |
233 if (!separator_rendertext_) { | 278 if (!separator_rendertext_) { |
234 const base::string16& separator = | 279 const base::string16& separator = |
235 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); | 280 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); |
236 separator_rendertext_.reset(CreateRenderText(separator).release()); | 281 separator_rendertext_.reset(CreateRenderText(separator).release()); |
237 separator_rendertext_->SetColor(GetColor(GetState(), DIMMED_TEXT)); | 282 separator_rendertext_->SetColor(GetColor(GetState(), DIMMED_TEXT)); |
238 separator_width_ = separator_rendertext_->GetContentWidth(); | 283 separator_width_ = separator_rendertext_->GetContentWidth(); |
239 } | 284 } |
240 | 285 |
241 int contents_max_width, description_max_width; | 286 int contents_max_width, description_max_width; |
242 OmniboxPopupModel::ComputeMatchMaxWidths( | 287 OmniboxPopupModel::ComputeMatchMaxWidths( |
243 contents->GetContentWidth(), | 288 contents->GetContentWidth(), |
244 separator_width_, | 289 separator_width_, |
245 description ? description->GetContentWidth() : 0, | 290 description ? description->GetContentWidth() : 0, |
246 mirroring_context_->remaining_width(x), | 291 mirroring_context_->remaining_width(x), |
247 !AutocompleteMatch::IsSearchType(match.type), | 292 !AutocompleteMatch::IsSearchType(match.type), |
248 &contents_max_width, | 293 &contents_max_width, |
249 &description_max_width); | 294 &description_max_width); |
250 | 295 |
251 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width); | 296 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width); |
252 | 297 |
253 if (description_max_width != 0) { | 298 if (description_max_width != 0) { |
254 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y, | 299 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y, |
255 separator_width_); | 300 separator_width_); |
| 301 |
| 302 if (!answer_image_.isNull()) { |
| 303 canvas->DrawImageInt(answer_image_, |
| 304 // Source x, y, w, h. |
| 305 0, 0, answer_image_.width(), answer_image_.height(), |
| 306 // Destination x, y, w, h. |
| 307 GetMirroredXInView(x), |
| 308 y + kMinimumIconVerticalPadding, default_icon_size_, |
| 309 default_icon_size_, true); |
| 310 x += default_icon_size_ + LocationBarView::kIconInternalPadding; |
| 311 } |
| 312 |
256 DrawRenderText(match, description, false, canvas, x, y, | 313 DrawRenderText(match, description, false, canvas, x, y, |
257 description_max_width); | 314 description_max_width); |
258 } | 315 } |
259 } | 316 } |
260 | 317 |
261 int OmniboxResultView::DrawRenderText( | 318 int OmniboxResultView::DrawRenderText( |
262 const AutocompleteMatch& match, | 319 const AutocompleteMatch& match, |
263 gfx::RenderText* render_text, | 320 gfx::RenderText* render_text, |
264 bool contents, | 321 bool contents, |
265 gfx::Canvas* canvas, | 322 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); | 446 render_text->ApplyColor(GetColor(GetState(), color_kind), current_range); |
390 } | 447 } |
391 return render_text.Pass(); | 448 return render_text.Pass(); |
392 } | 449 } |
393 | 450 |
394 int OmniboxResultView::GetMatchContentsWidth() const { | 451 int OmniboxResultView::GetMatchContentsWidth() const { |
395 InitContentsRenderTextIfNecessary(); | 452 InitContentsRenderTextIfNecessary(); |
396 return contents_rendertext_ ? contents_rendertext_->GetContentWidth() : 0; | 453 return contents_rendertext_ ? contents_rendertext_->GetContentWidth() : 0; |
397 } | 454 } |
398 | 455 |
| 456 void OmniboxResultView::SetAnswerImage(const gfx::ImageSkia& image) { |
| 457 answer_image_ = image; |
| 458 SchedulePaint(); |
| 459 } |
| 460 |
399 // TODO(skanuj): This is probably identical across all OmniboxResultView rows in | 461 // TODO(skanuj): This is probably identical across all OmniboxResultView rows in |
400 // the omnibox dropdown. Consider sharing the result. | 462 // the omnibox dropdown. Consider sharing the result. |
401 int OmniboxResultView::GetDisplayOffset( | 463 int OmniboxResultView::GetDisplayOffset( |
402 const AutocompleteMatch& match, | 464 const AutocompleteMatch& match, |
403 bool is_ui_rtl, | 465 bool is_ui_rtl, |
404 bool is_match_contents_rtl) const { | 466 bool is_match_contents_rtl) const { |
405 if (match.type != AutocompleteMatchType::SEARCH_SUGGEST_TAIL) | 467 if (match.type != AutocompleteMatchType::SEARCH_SUGGEST_TAIL) |
406 return 0; | 468 return 0; |
407 | 469 |
408 const base::string16& input_text = | 470 const base::string16& input_text = |
(...skipping 159 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 |