| 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 0, 0, answer_image_.width(), answer_image_.height(), |
| 304 // Destination x, y, w, h. | 306 GetMirroredXInView(x), y, answer_icon_size, answer_icon_size, true); |
| 305 GetMirroredXInView(x), | 307 x += answer_icon_size + LocationBarView::kIconInternalPadding; |
| 306 y + kMinimumIconVerticalPadding, default_icon_size_, | 308 } |
| 307 default_icon_size_, true); | 309 } else { |
| 308 x += default_icon_size_ + LocationBarView::kIconInternalPadding; | 310 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, |
| 311 after_contents_x, y, separator_width_); |
| 309 } | 312 } |
| 310 | 313 |
| 311 DrawRenderText(match, description, false, canvas, x, y, | 314 DrawRenderText(match, description, false, canvas, x, y, |
| 312 description_max_width); | 315 description_max_width); |
| 313 } | 316 } |
| 314 } | 317 } |
| 315 | 318 |
| 316 int OmniboxResultView::DrawRenderText( | 319 int OmniboxResultView::DrawRenderText( |
| 317 const AutocompleteMatch& match, | 320 const AutocompleteMatch& match, |
| 318 gfx::RenderText* render_text, | 321 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, | 372 const int start_offset = std::max(prefix_width, |
| 370 std::min(remaining_width - max_match_contents_width, offset)); | 373 std::min(remaining_width - max_match_contents_width, offset)); |
| 371 right_x = x + std::min(remaining_width, start_offset + max_width); | 374 right_x = x + std::min(remaining_width, start_offset + max_width); |
| 372 x += start_offset; | 375 x += start_offset; |
| 373 prefix_x = x - prefix_width; | 376 prefix_x = x - prefix_width; |
| 374 } | 377 } |
| 375 prefix_render_text->SetDirectionalityMode(is_match_contents_rtl ? | 378 prefix_render_text->SetDirectionalityMode(is_match_contents_rtl ? |
| 376 gfx::DIRECTIONALITY_FORCE_RTL : gfx::DIRECTIONALITY_FORCE_LTR); | 379 gfx::DIRECTIONALITY_FORCE_RTL : gfx::DIRECTIONALITY_FORCE_LTR); |
| 377 prefix_render_text->SetHorizontalAlignment( | 380 prefix_render_text->SetHorizontalAlignment( |
| 378 is_match_contents_rtl ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT); | 381 is_match_contents_rtl ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT); |
| 379 prefix_render_text->SetDisplayRect(gfx::Rect( | 382 prefix_render_text->SetDisplayRect( |
| 380 mirroring_context_->mirrored_left_coord( | 383 gfx::Rect(mirroring_context_->mirrored_left_coord( |
| 381 prefix_x, prefix_x + prefix_width), y, | 384 prefix_x, prefix_x + prefix_width), |
| 382 prefix_width, height())); | 385 y, prefix_width, GetContentLineHeight())); |
| 383 prefix_render_text->Draw(canvas); | 386 prefix_render_text->Draw(canvas); |
| 384 } | 387 } |
| 385 | 388 |
| 386 // Set the display rect to trigger eliding. | 389 // Set the display rect to trigger eliding. |
| 387 render_text->SetDisplayRect(gfx::Rect( | 390 render_text->SetDisplayRect( |
| 388 mirroring_context_->mirrored_left_coord(x, right_x), y, | 391 gfx::Rect(mirroring_context_->mirrored_left_coord(x, right_x), y, |
| 389 right_x - x, height())); | 392 right_x - x, GetContentLineHeight())); |
| 390 render_text->Draw(canvas); | 393 render_text->Draw(canvas); |
| 391 return right_x; | 394 return right_x; |
| 392 } | 395 } |
| 393 | 396 |
| 394 scoped_ptr<gfx::RenderText> OmniboxResultView::CreateRenderText( | 397 scoped_ptr<gfx::RenderText> OmniboxResultView::CreateRenderText( |
| 395 const base::string16& text) const { | 398 const base::string16& text) const { |
| 396 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance()); | 399 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance()); |
| 397 render_text->SetDisplayRect(gfx::Rect(gfx::Size(INT_MAX, 0))); | 400 render_text->SetDisplayRect(gfx::Rect(gfx::Size(INT_MAX, 0))); |
| 398 render_text->SetCursorEnabled(false); | 401 render_text->SetCursorEnabled(false); |
| 399 render_text->SetElideBehavior(gfx::ELIDE_TAIL); | 402 render_text->SetElideBehavior(gfx::ELIDE_TAIL); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 if (!contents_rendertext_) { | 548 if (!contents_rendertext_) { |
| 546 contents_rendertext_.reset( | 549 contents_rendertext_.reset( |
| 547 CreateClassifiedRenderText( | 550 CreateClassifiedRenderText( |
| 548 match_.contents, match_.contents_class, false).release()); | 551 match_.contents, match_.contents_class, false).release()); |
| 549 } | 552 } |
| 550 } | 553 } |
| 551 | 554 |
| 552 void OmniboxResultView::Layout() { | 555 void OmniboxResultView::Layout() { |
| 553 const gfx::ImageSkia icon = GetIcon(); | 556 const gfx::ImageSkia icon = GetIcon(); |
| 554 | 557 |
| 555 icon_bounds_.SetRect(edge_item_padding_ + | 558 icon_bounds_.SetRect( |
| 556 ((icon.width() == default_icon_size_) ? | 559 edge_item_padding_ + ((icon.width() == default_icon_size_) |
| 557 0 : LocationBarView::kIconInternalPadding), | 560 ? 0 |
| 558 (height() - icon.height()) / 2, icon.width(), icon.height()); | 561 : LocationBarView::kIconInternalPadding), |
| 562 (GetContentLineHeight() - icon.height()) / 2, icon.width(), |
| 563 icon.height()); |
| 559 | 564 |
| 560 int text_x = edge_item_padding_ + default_icon_size_ + item_padding_; | 565 int text_x = edge_item_padding_ + default_icon_size_ + item_padding_; |
| 561 int text_width = width() - text_x - edge_item_padding_; | 566 int text_width = width() - text_x - edge_item_padding_; |
| 562 | 567 |
| 563 if (match_.associated_keyword.get()) { | 568 if (match_.associated_keyword.get()) { |
| 564 const int kw_collapsed_size = | 569 const int kw_collapsed_size = |
| 565 keyword_icon_->width() + edge_item_padding_; | 570 keyword_icon_->width() + edge_item_padding_; |
| 566 const int max_kw_x = width() - kw_collapsed_size; | 571 const int max_kw_x = width() - kw_collapsed_size; |
| 567 const int kw_x = | 572 const int kw_x = |
| 568 animation_->CurrentValueBetween(max_kw_x, edge_item_padding_); | 573 animation_->CurrentValueBetween(max_kw_x, edge_item_padding_); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 596 int x = GetMirroredXForRect(text_bounds_); | 601 int x = GetMirroredXForRect(text_bounds_); |
| 597 mirroring_context_->Initialize(x, text_bounds_.width()); | 602 mirroring_context_->Initialize(x, text_bounds_.width()); |
| 598 InitContentsRenderTextIfNecessary(); | 603 InitContentsRenderTextIfNecessary(); |
| 599 | 604 |
| 600 if (!description_rendertext_) { | 605 if (!description_rendertext_) { |
| 601 if (match_.answer) { | 606 if (match_.answer) { |
| 602 base::string16 text; | 607 base::string16 text; |
| 603 for (const auto& textfield : match_.answer->second_line().text_fields()) | 608 for (const auto& textfield : match_.answer->second_line().text_fields()) |
| 604 text += textfield.text(); | 609 text += textfield.text(); |
| 605 description_rendertext_ = CreateRenderText(text); | 610 description_rendertext_ = CreateRenderText(text); |
| 611 description_rendertext_->SetFontList( |
| 612 ui::ResourceBundle::GetSharedInstance().GetFontList( |
| 613 ui::ResourceBundle::LargeFont)); |
| 606 } else if (!match_.description.empty()) { | 614 } else if (!match_.description.empty()) { |
| 607 description_rendertext_ = CreateClassifiedRenderText( | 615 description_rendertext_ = CreateClassifiedRenderText( |
| 608 match_.description, match_.description_class, true); | 616 match_.description, match_.description_class, true); |
| 609 } | 617 } |
| 610 } | 618 } |
| 611 PaintMatch(match_, contents_rendertext_.get(), | 619 PaintMatch(match_, contents_rendertext_.get(), |
| 612 description_rendertext_.get(), canvas, x); | 620 description_rendertext_.get(), canvas, x); |
| 613 } | 621 } |
| 614 | 622 |
| 615 AutocompleteMatch* keyword_match = match_.associated_keyword.get(); | 623 AutocompleteMatch* keyword_match = match_.associated_keyword.get(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 631 } | 639 } |
| 632 PaintMatch(*keyword_match, keyword_contents_rendertext_.get(), | 640 PaintMatch(*keyword_match, keyword_contents_rendertext_.get(), |
| 633 keyword_description_rendertext_.get(), canvas, x); | 641 keyword_description_rendertext_.get(), canvas, x); |
| 634 } | 642 } |
| 635 } | 643 } |
| 636 | 644 |
| 637 void OmniboxResultView::AnimationProgressed(const gfx::Animation* animation) { | 645 void OmniboxResultView::AnimationProgressed(const gfx::Animation* animation) { |
| 638 Layout(); | 646 Layout(); |
| 639 SchedulePaint(); | 647 SchedulePaint(); |
| 640 } | 648 } |
| 649 |
| 650 int OmniboxResultView::GetAnswerLineHeight() const { |
| 651 // LargeFont is the largest font used and so defines the boundary that |
| 652 // all the other answer styles fit within. |
| 653 return ui::ResourceBundle::GetSharedInstance() |
| 654 .GetFontList(ui::ResourceBundle::LargeFont) |
| 655 .GetHeight(); |
| 656 } |
| 657 |
| 658 int OmniboxResultView::GetContentLineHeight() const { |
| 659 return std::max(default_icon_size_ + (kMinimumIconVerticalPadding * 2), |
| 660 GetTextHeight() + (kMinimumTextVerticalPadding * 2)); |
| 661 } |
| OLD | NEW |