Chromium Code Reviews| 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" | 20 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" | 22 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
| 23 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 23 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 24 #include "chrome/browser/ui/views/omnibox/omnibox_popup_contents_view.h" | 24 #include "chrome/browser/ui/views/omnibox/omnibox_popup_contents_view.h" |
| 25 #include "chrome/grit/generated_resources.h" | 25 #include "chrome/grit/generated_resources.h" |
| 26 #include "components/omnibox/suggestion_answer.h" | 26 #include "components/omnibox/suggestion_answer.h" |
| 27 #include "grit/components_scaled_resources.h" | 27 #include "grit/components_scaled_resources.h" |
| 28 #include "grit/theme_resources.h" | 28 #include "grit/theme_resources.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 30 #include "ui/base/resource/resource_bundle.h" | |
| 30 #include "ui/base/theme_provider.h" | 31 #include "ui/base/theme_provider.h" |
| 31 #include "ui/gfx/canvas.h" | 32 #include "ui/gfx/canvas.h" |
| 32 #include "ui/gfx/color_utils.h" | 33 #include "ui/gfx/color_utils.h" |
| 33 #include "ui/gfx/image/image.h" | 34 #include "ui/gfx/image/image.h" |
| 34 #include "ui/gfx/range/range.h" | 35 #include "ui/gfx/range/range.h" |
| 35 #include "ui/gfx/render_text.h" | 36 #include "ui/gfx/render_text.h" |
| 36 #include "ui/gfx/text_utils.h" | 37 #include "ui/gfx/text_utils.h" |
| 37 #include "ui/native_theme/native_theme.h" | |
| 38 | 38 |
| 39 using ui::NativeTheme; | 39 using ui::NativeTheme; |
| 40 | 40 |
| 41 namespace { | 41 namespace { |
| 42 | 42 |
| 43 // The minimum distance between the top and bottom of the icon and the | 43 // The minimum distance between the top and bottom of the icon and the |
| 44 // top or bottom of the row. | 44 // top or bottom of the row. |
| 45 const int kMinimumIconVerticalPadding = 2; | 45 const int kMinimumIconVerticalPadding = 2; |
| 46 | 46 |
| 47 // Calls back to the OmniboxResultView when the requested image is downloaded. | 47 // Calls back to the OmniboxResultView when the requested image is downloaded. |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 241 void OmniboxResultView::Invalidate() { | 241 void OmniboxResultView::Invalidate() { |
| 242 keyword_icon_->SetImage(GetKeywordIcon()); | 242 keyword_icon_->SetImage(GetKeywordIcon()); |
| 243 // While the text in the RenderTexts may not have changed, the styling | 243 // While the text in the RenderTexts may not have changed, the styling |
| 244 // (color/bold) may need to change. So we reset them to cause them to be | 244 // (color/bold) may need to change. So we reset them to cause them to be |
| 245 // recomputed in OnPaint(). | 245 // recomputed in OnPaint(). |
| 246 ResetRenderTexts(); | 246 ResetRenderTexts(); |
| 247 SchedulePaint(); | 247 SchedulePaint(); |
| 248 } | 248 } |
| 249 | 249 |
| 250 gfx::Size OmniboxResultView::GetPreferredSize() const { | 250 gfx::Size OmniboxResultView::GetPreferredSize() const { |
| 251 return gfx::Size(0, std::max( | 251 if (!match_.answer) |
| 252 default_icon_size_ + (kMinimumIconVerticalPadding * 2), | 252 return gfx::Size(0, GetContentLineHeight()); |
| 253 GetTextHeight() + (kMinimumTextVerticalPadding * 2))); | 253 // An answer implies a match and a description in a large font. |
| 254 return gfx::Size(0, GetContentLineHeight() + GetAnswerLineHeight()); | |
| 254 } | 255 } |
| 255 | 256 |
| 256 //////////////////////////////////////////////////////////////////////////////// | 257 //////////////////////////////////////////////////////////////////////////////// |
| 257 // OmniboxResultView, protected: | 258 // OmniboxResultView, protected: |
| 258 | 259 |
| 259 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const { | 260 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const { |
| 260 if (model_->IsSelectedIndex(model_index_)) | 261 if (model_->IsSelectedIndex(model_index_)) |
| 261 return SELECTED; | 262 return SELECTED; |
| 262 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; | 263 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; |
| 263 } | 264 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 284 int contents_max_width, description_max_width; | 285 int contents_max_width, description_max_width; |
| 285 OmniboxPopupModel::ComputeMatchMaxWidths( | 286 OmniboxPopupModel::ComputeMatchMaxWidths( |
| 286 contents->GetContentWidth(), | 287 contents->GetContentWidth(), |
| 287 separator_width_, | 288 separator_width_, |
| 288 description ? description->GetContentWidth() : 0, | 289 description ? description->GetContentWidth() : 0, |
| 289 mirroring_context_->remaining_width(x), | 290 mirroring_context_->remaining_width(x), |
| 290 !AutocompleteMatch::IsSearchType(match.type), | 291 !AutocompleteMatch::IsSearchType(match.type), |
| 291 &contents_max_width, | 292 &contents_max_width, |
| 292 &description_max_width); | 293 &description_max_width); |
| 293 | 294 |
| 294 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width); | 295 int after_contents_x = |
| 296 DrawRenderText(match, contents, true, canvas, x, y, contents_max_width); | |
| 295 | 297 |
| 296 if (description_max_width != 0) { | 298 if (description_max_width != 0) { |
| 297 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y, | 299 if (match.answer) { |
| 298 separator_width_); | 300 y += GetContentLineHeight(); |
| 299 | 301 if (!answer_image_.isNull()) { |
| 300 if (!answer_image_.isNull()) { | 302 int answer_icon_size = GetAnswerLineHeight(); |
| 301 canvas->DrawImageInt(answer_image_, | 303 canvas->DrawImageInt( |
| 302 // Source x, y, w, h. | 304 answer_image_, |
| 303 0, 0, answer_image_.width(), answer_image_.height(), | 305 // Source x, y, w, h. |
|
Peter Kasting
2015/03/19 00:54:57
Nit: I'd probably omit these two comments, but up
dschuyler
2015/03/19 01:14:55
I think you'd previously made a suggestion that th
dschuyler
2015/03/19 01:14:55
Done.
| |
| 304 // Destination x, y, w, h. | 306 0, 0, answer_image_.width(), answer_image_.height(), |
| 305 GetMirroredXInView(x), | 307 // Destination x, y, w, h. |
| 306 y + kMinimumIconVerticalPadding, default_icon_size_, | 308 GetMirroredXInView(x), y, answer_icon_size, answer_icon_size, true); |
| 307 default_icon_size_, true); | 309 x += answer_icon_size + LocationBarView::kIconInternalPadding; |
| 308 x += default_icon_size_ + LocationBarView::kIconInternalPadding; | 310 } |
| 311 } else { | |
| 312 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, | |
| 313 after_contents_x, y, separator_width_); | |
| 309 } | 314 } |
| 310 | 315 |
| 311 DrawRenderText(match, description, false, canvas, x, y, | 316 DrawRenderText(match, description, false, canvas, x, y, |
| 312 description_max_width); | 317 description_max_width); |
| 313 } | 318 } |
| 314 } | 319 } |
| 315 | 320 |
| 316 int OmniboxResultView::DrawRenderText( | 321 int OmniboxResultView::DrawRenderText( |
| 317 const AutocompleteMatch& match, | 322 const AutocompleteMatch& match, |
| 318 gfx::RenderText* render_text, | 323 gfx::RenderText* render_text, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 369 const int start_offset = std::max(prefix_width, | 374 const int start_offset = std::max(prefix_width, |
| 370 std::min(remaining_width - max_match_contents_width, offset)); | 375 std::min(remaining_width - max_match_contents_width, offset)); |
| 371 right_x = x + std::min(remaining_width, start_offset + max_width); | 376 right_x = x + std::min(remaining_width, start_offset + max_width); |
| 372 x += start_offset; | 377 x += start_offset; |
| 373 prefix_x = x - prefix_width; | 378 prefix_x = x - prefix_width; |
| 374 } | 379 } |
| 375 prefix_render_text->SetDirectionalityMode(is_match_contents_rtl ? | 380 prefix_render_text->SetDirectionalityMode(is_match_contents_rtl ? |
| 376 gfx::DIRECTIONALITY_FORCE_RTL : gfx::DIRECTIONALITY_FORCE_LTR); | 381 gfx::DIRECTIONALITY_FORCE_RTL : gfx::DIRECTIONALITY_FORCE_LTR); |
| 377 prefix_render_text->SetHorizontalAlignment( | 382 prefix_render_text->SetHorizontalAlignment( |
| 378 is_match_contents_rtl ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT); | 383 is_match_contents_rtl ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT); |
| 379 prefix_render_text->SetDisplayRect(gfx::Rect( | 384 prefix_render_text->SetDisplayRect( |
| 380 mirroring_context_->mirrored_left_coord( | 385 gfx::Rect(mirroring_context_->mirrored_left_coord( |
| 381 prefix_x, prefix_x + prefix_width), y, | 386 prefix_x, prefix_x + prefix_width), |
| 382 prefix_width, height())); | 387 y, prefix_width, GetContentLineHeight())); |
| 383 prefix_render_text->Draw(canvas); | 388 prefix_render_text->Draw(canvas); |
| 384 } | 389 } |
| 385 | 390 |
| 386 // Set the display rect to trigger eliding. | 391 // Set the display rect to trigger eliding. |
| 387 render_text->SetDisplayRect(gfx::Rect( | 392 render_text->SetDisplayRect( |
| 388 mirroring_context_->mirrored_left_coord(x, right_x), y, | 393 gfx::Rect(mirroring_context_->mirrored_left_coord(x, right_x), y, |
| 389 right_x - x, height())); | 394 right_x - x, GetContentLineHeight())); |
| 390 render_text->Draw(canvas); | 395 render_text->Draw(canvas); |
| 391 return right_x; | 396 return right_x; |
| 392 } | 397 } |
| 393 | 398 |
| 394 scoped_ptr<gfx::RenderText> OmniboxResultView::CreateRenderText( | 399 scoped_ptr<gfx::RenderText> OmniboxResultView::CreateRenderText( |
| 395 const base::string16& text) const { | 400 const base::string16& text) const { |
| 396 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance()); | 401 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance()); |
| 397 render_text->SetDisplayRect(gfx::Rect(gfx::Size(INT_MAX, 0))); | 402 render_text->SetDisplayRect(gfx::Rect(gfx::Size(INT_MAX, 0))); |
| 398 render_text->SetCursorEnabled(false); | 403 render_text->SetCursorEnabled(false); |
| 399 render_text->SetElideBehavior(gfx::ELIDE_TAIL); | 404 render_text->SetElideBehavior(gfx::ELIDE_TAIL); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 545 if (!contents_rendertext_) { | 550 if (!contents_rendertext_) { |
| 546 contents_rendertext_.reset( | 551 contents_rendertext_.reset( |
| 547 CreateClassifiedRenderText( | 552 CreateClassifiedRenderText( |
| 548 match_.contents, match_.contents_class, false).release()); | 553 match_.contents, match_.contents_class, false).release()); |
| 549 } | 554 } |
| 550 } | 555 } |
| 551 | 556 |
| 552 void OmniboxResultView::Layout() { | 557 void OmniboxResultView::Layout() { |
| 553 const gfx::ImageSkia icon = GetIcon(); | 558 const gfx::ImageSkia icon = GetIcon(); |
| 554 | 559 |
| 555 icon_bounds_.SetRect(edge_item_padding_ + | 560 icon_bounds_.SetRect( |
| 556 ((icon.width() == default_icon_size_) ? | 561 edge_item_padding_ + ((icon.width() == default_icon_size_) |
| 557 0 : LocationBarView::kIconInternalPadding), | 562 ? 0 |
| 558 (height() - icon.height()) / 2, icon.width(), icon.height()); | 563 : LocationBarView::kIconInternalPadding), |
| 564 (GetContentLineHeight() - icon.height()) / 2, icon.width(), | |
| 565 icon.height()); | |
| 559 | 566 |
| 560 int text_x = edge_item_padding_ + default_icon_size_ + item_padding_; | 567 int text_x = edge_item_padding_ + default_icon_size_ + item_padding_; |
| 561 int text_width = width() - text_x - edge_item_padding_; | 568 int text_width = width() - text_x - edge_item_padding_; |
| 562 | 569 |
| 563 if (match_.associated_keyword.get()) { | 570 if (match_.associated_keyword.get()) { |
| 564 const int kw_collapsed_size = | 571 const int kw_collapsed_size = |
| 565 keyword_icon_->width() + edge_item_padding_; | 572 keyword_icon_->width() + edge_item_padding_; |
| 566 const int max_kw_x = width() - kw_collapsed_size; | 573 const int max_kw_x = width() - kw_collapsed_size; |
| 567 const int kw_x = | 574 const int kw_x = |
| 568 animation_->CurrentValueBetween(max_kw_x, edge_item_padding_); | 575 animation_->CurrentValueBetween(max_kw_x, edge_item_padding_); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 596 int x = GetMirroredXForRect(text_bounds_); | 603 int x = GetMirroredXForRect(text_bounds_); |
| 597 mirroring_context_->Initialize(x, text_bounds_.width()); | 604 mirroring_context_->Initialize(x, text_bounds_.width()); |
| 598 InitContentsRenderTextIfNecessary(); | 605 InitContentsRenderTextIfNecessary(); |
| 599 | 606 |
| 600 if (!description_rendertext_) { | 607 if (!description_rendertext_) { |
| 601 if (match_.answer) { | 608 if (match_.answer) { |
| 602 base::string16 text; | 609 base::string16 text; |
| 603 for (const auto& textfield : match_.answer->second_line().text_fields()) | 610 for (const auto& textfield : match_.answer->second_line().text_fields()) |
| 604 text += textfield.text(); | 611 text += textfield.text(); |
| 605 description_rendertext_ = CreateRenderText(text); | 612 description_rendertext_ = CreateRenderText(text); |
| 613 description_rendertext_->SetFontList( | |
| 614 ui::ResourceBundle::GetSharedInstance().GetFontList( | |
| 615 ui::ResourceBundle::LargeFont)); | |
|
Peter Kasting
2015/03/19 00:54:57
This doesn't seem right. Why are we changing the
dschuyler
2015/03/19 01:14:55
In my testing, this only happens for the AiS resul
Peter Kasting
2015/03/19 01:17:56
Oops, I didn't read the code properly.
I guess th
| |
| 606 } else if (!match_.description.empty()) { | 616 } else if (!match_.description.empty()) { |
| 607 description_rendertext_ = CreateClassifiedRenderText( | 617 description_rendertext_ = CreateClassifiedRenderText( |
| 608 match_.description, match_.description_class, true); | 618 match_.description, match_.description_class, true); |
| 609 } | 619 } |
| 610 } | 620 } |
| 611 PaintMatch(match_, contents_rendertext_.get(), | 621 PaintMatch(match_, contents_rendertext_.get(), |
| 612 description_rendertext_.get(), canvas, x); | 622 description_rendertext_.get(), canvas, x); |
| 613 } | 623 } |
| 614 | 624 |
| 615 AutocompleteMatch* keyword_match = match_.associated_keyword.get(); | 625 AutocompleteMatch* keyword_match = match_.associated_keyword.get(); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 631 } | 641 } |
| 632 PaintMatch(*keyword_match, keyword_contents_rendertext_.get(), | 642 PaintMatch(*keyword_match, keyword_contents_rendertext_.get(), |
| 633 keyword_description_rendertext_.get(), canvas, x); | 643 keyword_description_rendertext_.get(), canvas, x); |
| 634 } | 644 } |
| 635 } | 645 } |
| 636 | 646 |
| 637 void OmniboxResultView::AnimationProgressed(const gfx::Animation* animation) { | 647 void OmniboxResultView::AnimationProgressed(const gfx::Animation* animation) { |
| 638 Layout(); | 648 Layout(); |
| 639 SchedulePaint(); | 649 SchedulePaint(); |
| 640 } | 650 } |
| 651 | |
| 652 int OmniboxResultView::GetAnswerLineHeight() const { | |
| 653 // LargeFont is the largest font used and so defines the boundary that | |
| 654 // all the other answer styles fit within. | |
| 655 return ui::ResourceBundle::GetSharedInstance() | |
| 656 .GetFontList(ui::ResourceBundle::LargeFont) | |
| 657 .GetHeight(); | |
| 658 } | |
| 659 | |
| 660 int OmniboxResultView::GetContentLineHeight() const { | |
| 661 return std::max(default_icon_size_ + (kMinimumIconVerticalPadding * 2), | |
| 662 GetTextHeight() + (kMinimumTextVerticalPadding * 2)); | |
| 663 } | |
| OLD | NEW |