Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: chrome/browser/ui/views/omnibox/omnibox_result_view.cc

Issue 917333004: Answers in Suggest icon downloading (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: changes for nits in review Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.
Peter Kasting 2015/03/05 00:33:43 Nit: How about something like: Calls back to the
dschuyler 2015/03/05 18:31:03 Done.
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_;
groby-ooo-7-16 2015/03/05 15:44:54 I'm still not entirely convinced this needs to be
dschuyler 2015/03/05 22:19:03 Let's look at that with the change about who owns
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());
Peter Kasting 2015/03/05 00:33:43 Just to make sure, if the received image was corru
groby-ooo-7-16 2015/03/05 15:44:54 Nice catch - it actually will always call. See the
dschuyler 2015/03/05 18:31:04 I missed the comment, I looked at the code. which
dschuyler 2015/03/05 18:31:04 The caller currently does check the image. So the
62 if (view_)
63 view_->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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 : edge_item_padding_(LocationBarView::kItemPadding),
131 item_padding_(LocationBarView::kItemPadding), 156 item_padding_(LocationBarView::kItemPadding),
132 minimum_text_vertical_padding_(kMinimumTextVerticalPadding), 157 minimum_text_vertical_padding_(kMinimumTextVerticalPadding),
133 model_(model), 158 model_(model),
134 model_index_(model_index), 159 model_index_(model_index),
135 location_bar_view_(location_bar_view), 160 location_bar_view_(location_bar_view),
161 image_service_(BitmapFetcherServiceFactory::GetForBrowserContext(
162 location_bar_view_->profile())),
136 font_list_(font_list), 163 font_list_(font_list),
137 font_height_( 164 font_height_(
138 std::max(font_list.GetHeight(), 165 std::max(font_list.GetHeight(),
139 font_list.DeriveWithStyle(gfx::Font::BOLD).GetHeight())), 166 font_list.DeriveWithStyle(gfx::Font::BOLD).GetHeight())),
140 mirroring_context_(new MirroringContext()), 167 mirroring_context_(new MirroringContext()),
141 keyword_icon_(new views::ImageView()), 168 keyword_icon_(new views::ImageView()),
142 animation_(new gfx::SlideAnimation(this)) { 169 animation_(new gfx::SlideAnimation(this)),
170 request_id_(BitmapFetcherService::REQUEST_ID_INVALID),
171 weak_ptr_factory_(this) {
143 CHECK_GE(model_index, 0); 172 CHECK_GE(model_index, 0);
144 if (default_icon_size_ == 0) { 173 if (default_icon_size_ == 0) {
145 default_icon_size_ = 174 default_icon_size_ =
146 location_bar_view_->GetThemeProvider()->GetImageSkiaNamed( 175 location_bar_view_->GetThemeProvider()->GetImageSkiaNamed(
147 AutocompleteMatch::TypeToIcon( 176 AutocompleteMatch::TypeToIcon(
148 AutocompleteMatchType::URL_WHAT_YOU_TYPED))->width(); 177 AutocompleteMatchType::URL_WHAT_YOU_TYPED))->width();
149 } 178 }
150 keyword_icon_->set_owned_by_client(); 179 keyword_icon_->set_owned_by_client();
151 keyword_icon_->EnableCanvasFlippingForRTLUI(true); 180 keyword_icon_->EnableCanvasFlippingForRTLUI(true);
152 keyword_icon_->SetImage(GetKeywordIcon()); 181 keyword_icon_->SetImage(GetKeywordIcon());
153 keyword_icon_->SizeToPreferredSize(); 182 keyword_icon_->SizeToPreferredSize();
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() &&
groby-ooo-7-16 2015/03/05 15:44:54 There's no need to check URL validity - the fetche
dschuyler 2015/03/05 22:19:03 The accessor for the image url sounds cool, though
211 image_service_) {
212 image_service_->CancelRequest(request_id_);
groby-ooo-7-16 2015/03/05 15:44:54 I'd assume you'd want to cancel a pending request
dschuyler 2015/03/05 22:19:03 Ah, good one :) Done.
213 request_id_ = image_service_->RequestImage(
214 match_.answer->second_line().image_url(),
215 new AnswersImageObserverDesktop(weak_ptr_factory_.GetWeakPtr()));
216 }
217
178 AutocompleteMatch* associated_keyword_match = match_.associated_keyword.get(); 218 AutocompleteMatch* associated_keyword_match = match_.associated_keyword.get();
179 if (associated_keyword_match) { 219 if (associated_keyword_match) {
180 keyword_icon_->SetImage(GetKeywordIcon()); 220 keyword_icon_->SetImage(GetKeywordIcon());
181 if (!keyword_icon_->parent()) 221 if (!keyword_icon_->parent())
182 AddChildView(keyword_icon_.get()); 222 AddChildView(keyword_icon_.get());
183 } else if (keyword_icon_->parent()) { 223 } else if (keyword_icon_->parent()) {
184 RemoveChildView(keyword_icon_.get()); 224 RemoveChildView(keyword_icon_.get());
185 } 225 }
186 226
187 Layout(); 227 Layout();
(...skipping 27 matching lines...) Expand all
215 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const { 255 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const {
216 if (model_->IsSelectedIndex(model_index_)) 256 if (model_->IsSelectedIndex(model_index_))
217 return SELECTED; 257 return SELECTED;
218 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; 258 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL;
219 } 259 }
220 260
221 int OmniboxResultView::GetTextHeight() const { 261 int OmniboxResultView::GetTextHeight() const {
222 return font_height_; 262 return font_height_;
223 } 263 }
224 264
225 void OmniboxResultView::PaintMatch( 265 void OmniboxResultView::PaintMatch(const AutocompleteMatch& match,
226 const AutocompleteMatch& match, 266 gfx::RenderText* contents,
227 gfx::RenderText* contents, 267 gfx::RenderText* description,
228 gfx::RenderText* description, 268 gfx::Canvas* canvas,
229 gfx::Canvas* canvas, 269 int x) const {
230 int x) const {
231 int y = text_bounds_.y(); 270 int y = text_bounds_.y();
232 271
233 if (!separator_rendertext_) { 272 if (!separator_rendertext_) {
234 const base::string16& separator = 273 const base::string16& separator =
235 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); 274 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR);
236 separator_rendertext_.reset(CreateRenderText(separator).release()); 275 separator_rendertext_.reset(CreateRenderText(separator).release());
237 separator_rendertext_->SetColor(GetColor(GetState(), DIMMED_TEXT)); 276 separator_rendertext_->SetColor(GetColor(GetState(), DIMMED_TEXT));
238 separator_width_ = separator_rendertext_->GetContentWidth(); 277 separator_width_ = separator_rendertext_->GetContentWidth();
239 } 278 }
240 279
241 int contents_max_width, description_max_width; 280 int contents_max_width, description_max_width;
242 OmniboxPopupModel::ComputeMatchMaxWidths( 281 OmniboxPopupModel::ComputeMatchMaxWidths(
243 contents->GetContentWidth(), 282 contents->GetContentWidth(),
244 separator_width_, 283 separator_width_,
245 description ? description->GetContentWidth() : 0, 284 description ? description->GetContentWidth() : 0,
246 mirroring_context_->remaining_width(x), 285 mirroring_context_->remaining_width(x),
247 !AutocompleteMatch::IsSearchType(match.type), 286 !AutocompleteMatch::IsSearchType(match.type),
248 &contents_max_width, 287 &contents_max_width,
249 &description_max_width); 288 &description_max_width);
250 289
251 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width); 290 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width);
252 291
253 if (description_max_width != 0) { 292 if (description_max_width != 0) {
254 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y, 293 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y,
255 separator_width_); 294 separator_width_);
295
296 if (!answer_image_.size().IsEmpty()) {
groby-ooo-7-16 2015/03/05 15:44:54 Isn't that covered by answer_image_.IsNull()?
Peter Kasting 2015/03/05 18:28:36 I don't think so, though I'm not sure. It depends
dschuyler 2015/03/05 22:19:03 Is it ok to leave in the empty check for now?
297 canvas->DrawImageInt(answer_image_,
298 // Source x, y, w, h.
299 0, 0, answer_image_.width(), answer_image_.height(),
300 // Destination x, y, w, h.
301 GetMirroredXInView(x),
302 y + kMinimumIconVerticalPadding, default_icon_size_,
303 default_icon_size_, true);
304 x += default_icon_size_ + LocationBarView::kIconInternalPadding;
305 }
306
256 DrawRenderText(match, description, false, canvas, x, y, 307 DrawRenderText(match, description, false, canvas, x, y,
257 description_max_width); 308 description_max_width);
258 } 309 }
259 } 310 }
260 311
261 int OmniboxResultView::DrawRenderText( 312 int OmniboxResultView::DrawRenderText(
262 const AutocompleteMatch& match, 313 const AutocompleteMatch& match,
263 gfx::RenderText* render_text, 314 gfx::RenderText* render_text,
264 bool contents, 315 bool contents,
265 gfx::Canvas* canvas, 316 gfx::Canvas* canvas,
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 render_text->ApplyColor(GetColor(GetState(), color_kind), current_range); 440 render_text->ApplyColor(GetColor(GetState(), color_kind), current_range);
390 } 441 }
391 return render_text.Pass(); 442 return render_text.Pass();
392 } 443 }
393 444
394 int OmniboxResultView::GetMatchContentsWidth() const { 445 int OmniboxResultView::GetMatchContentsWidth() const {
395 InitContentsRenderTextIfNecessary(); 446 InitContentsRenderTextIfNecessary();
396 return contents_rendertext_ ? contents_rendertext_->GetContentWidth() : 0; 447 return contents_rendertext_ ? contents_rendertext_->GetContentWidth() : 0;
397 } 448 }
398 449
450 void OmniboxResultView::SetAnswerImage(gfx::ImageSkia image) {
451 answer_image_ = image;
452 SchedulePaint();
453 }
454
399 // TODO(skanuj): This is probably identical across all OmniboxResultView rows in 455 // TODO(skanuj): This is probably identical across all OmniboxResultView rows in
400 // the omnibox dropdown. Consider sharing the result. 456 // the omnibox dropdown. Consider sharing the result.
401 int OmniboxResultView::GetDisplayOffset( 457 int OmniboxResultView::GetDisplayOffset(
402 const AutocompleteMatch& match, 458 const AutocompleteMatch& match,
403 bool is_ui_rtl, 459 bool is_ui_rtl,
404 bool is_match_contents_rtl) const { 460 bool is_match_contents_rtl) const {
405 if (match.type != AutocompleteMatchType::SEARCH_SUGGEST_TAIL) 461 if (match.type != AutocompleteMatchType::SEARCH_SUGGEST_TAIL)
406 return 0; 462 return 0;
407 463
408 const base::string16& input_text = 464 const base::string16& input_text =
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698