| 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 #include "chrome/browser/ui/views/location_bar/selected_keyword_view.h" | 5 #include "chrome/browser/ui/views/location_bar/selected_keyword_view.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/search_engines/template_url_service_factory.h" | 9 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 11 #include "chrome/browser/ui/location_bar/location_bar_util.h" | 10 #include "chrome/browser/ui/location_bar/location_bar_util.h" |
| 12 #include "chrome/browser/ui/views/location_bar/keyword_hint_view.h" | |
| 13 #include "chrome/grit/generated_resources.h" | 11 #include "chrome/grit/generated_resources.h" |
| 14 #include "chrome/grit/theme_resources.h" | |
| 15 #include "components/grit/components_scaled_resources.h" | |
| 16 #include "components/search_engines/template_url_service.h" | 12 #include "components/search_engines/template_url_service.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/material_design/material_design_controller.h" | |
| 19 #include "ui/base/theme_provider.h" | 14 #include "ui/base/theme_provider.h" |
| 20 #include "ui/gfx/color_palette.h" | 15 #include "ui/gfx/color_palette.h" |
| 21 #include "ui/gfx/color_utils.h" | 16 #include "ui/gfx/color_utils.h" |
| 22 #include "ui/gfx/paint_vector_icon.h" | 17 #include "ui/gfx/paint_vector_icon.h" |
| 23 #include "ui/gfx/vector_icons_public.h" | 18 #include "ui/gfx/vector_icons_public.h" |
| 24 #include "ui/native_theme/native_theme.h" | 19 #include "ui/native_theme/native_theme.h" |
| 25 #include "ui/views/painter.h" | |
| 26 | 20 |
| 27 SelectedKeywordView::SelectedKeywordView(const gfx::FontList& font_list, | 21 SelectedKeywordView::SelectedKeywordView(const gfx::FontList& font_list, |
| 28 SkColor text_color, | |
| 29 SkColor parent_background_color, | |
| 30 Profile* profile) | 22 Profile* profile) |
| 31 : IconLabelBubbleView(0, font_list, parent_background_color, false), | 23 : IconLabelBubbleView(font_list, false), profile_(profile) { |
| 32 text_color_(text_color), | |
| 33 profile_(profile) { | |
| 34 static const int kBackgroundImages[] = | |
| 35 IMAGE_GRID(IDR_OMNIBOX_SELECTED_KEYWORD_BUBBLE); | |
| 36 SetBackgroundImageGrid(kBackgroundImages); | |
| 37 full_label_.SetFontList(font_list); | 24 full_label_.SetFontList(font_list); |
| 38 full_label_.SetVisible(false); | 25 full_label_.SetVisible(false); |
| 39 partial_label_.SetFontList(font_list); | 26 partial_label_.SetFontList(font_list); |
| 40 partial_label_.SetVisible(false); | 27 partial_label_.SetVisible(false); |
| 41 } | 28 } |
| 42 | 29 |
| 43 SelectedKeywordView::~SelectedKeywordView() { | 30 SelectedKeywordView::~SelectedKeywordView() { |
| 44 } | 31 } |
| 45 | 32 |
| 46 void SelectedKeywordView::ResetImage() { | 33 void SelectedKeywordView::ResetImage() { |
| 47 if (ui::MaterialDesignController::IsModeMaterial()) { | 34 SetImage( |
| 48 SetImage(gfx::CreateVectorIcon(gfx::VectorIconId::OMNIBOX_SEARCH, | 35 gfx::CreateVectorIcon(gfx::VectorIconId::OMNIBOX_SEARCH, GetTextColor())); |
| 49 16, | |
| 50 GetTextColor())); | |
| 51 } else { | |
| 52 SetImage(*GetThemeProvider()->GetImageSkiaNamed(IDR_OMNIBOX_SEARCH)); | |
| 53 } | |
| 54 } | 36 } |
| 55 | 37 |
| 56 SkColor SelectedKeywordView::GetTextColor() const { | 38 SkColor SelectedKeywordView::GetTextColor() const { |
| 57 if (!ui::MaterialDesignController::IsModeMaterial()) | |
| 58 return text_color_; | |
| 59 | |
| 60 return GetNativeTheme()->GetSystemColor( | 39 return GetNativeTheme()->GetSystemColor( |
| 61 color_utils::IsDark(GetParentBackgroundColor()) | 40 color_utils::IsDark(GetParentBackgroundColor()) |
| 62 ? ui::NativeTheme::kColorId_TextfieldDefaultColor | 41 ? ui::NativeTheme::kColorId_TextfieldDefaultColor |
| 63 : ui::NativeTheme::kColorId_LinkEnabled); | 42 : ui::NativeTheme::kColorId_LinkEnabled); |
| 64 } | 43 } |
| 65 | 44 |
| 66 SkColor SelectedKeywordView::GetBorderColor() const { | |
| 67 DCHECK(ui::MaterialDesignController::IsModeMaterial()); | |
| 68 return GetTextColor(); | |
| 69 } | |
| 70 | |
| 71 gfx::Size SelectedKeywordView::GetPreferredSize() const { | 45 gfx::Size SelectedKeywordView::GetPreferredSize() const { |
| 72 // Height will be ignored by the LocationBarView. | 46 // Height will be ignored by the LocationBarView. |
| 73 return GetSizeForLabelWidth(full_label_.GetPreferredSize().width()); | 47 return GetSizeForLabelWidth(full_label_.GetPreferredSize().width()); |
| 74 } | 48 } |
| 75 | 49 |
| 76 gfx::Size SelectedKeywordView::GetMinimumSize() const { | 50 gfx::Size SelectedKeywordView::GetMinimumSize() const { |
| 77 // Height will be ignored by the LocationBarView. | 51 // Height will be ignored by the LocationBarView. |
| 78 return GetSizeForLabelWidth(partial_label_.GetMinimumSize().width()); | 52 return GetSizeForLabelWidth(partial_label_.GetMinimumSize().width()); |
| 79 } | 53 } |
| 80 | 54 |
| 81 void SelectedKeywordView::Layout() { | 55 void SelectedKeywordView::Layout() { |
| 82 SetLabel(((width() == GetPreferredSize().width()) ? | 56 SetLabel(((width() == GetPreferredSize().width()) ? |
| 83 full_label_ : partial_label_).text()); | 57 full_label_ : partial_label_).text()); |
| 84 IconLabelBubbleView::Layout(); | 58 IconLabelBubbleView::Layout(); |
| 85 } | 59 } |
| 86 | 60 |
| 87 void SelectedKeywordView::SetKeyword(const base::string16& keyword) { | 61 void SelectedKeywordView::SetKeyword(const base::string16& keyword) { |
| 88 keyword_ = keyword; | 62 keyword_ = keyword; |
| 89 if (keyword.empty()) | 63 if (keyword.empty()) |
| 90 return; | 64 return; |
| 91 DCHECK(profile_); | 65 DCHECK(profile_); |
| 92 TemplateURLService* model = | 66 TemplateURLService* model = |
| 93 TemplateURLServiceFactory::GetForProfile(profile_); | 67 TemplateURLServiceFactory::GetForProfile(profile_); |
| 94 if (!model) | 68 if (!model) |
| 95 return; | 69 return; |
| 96 | 70 |
| 97 bool is_extension_keyword; | 71 bool is_extension_keyword; |
| 98 const base::string16 short_name = | 72 const base::string16 short_name = |
| 99 model->GetKeywordShortName(keyword, &is_extension_keyword); | 73 model->GetKeywordShortName(keyword, &is_extension_keyword); |
| 100 int keyword_text_id = ui::MaterialDesignController::IsModeMaterial() | |
| 101 ? IDS_OMNIBOX_KEYWORD_TEXT_MD | |
| 102 : IDS_OMNIBOX_KEYWORD_TEXT; | |
| 103 const base::string16 full_name = | 74 const base::string16 full_name = |
| 104 is_extension_keyword ? short_name : l10n_util::GetStringFUTF16( | 75 is_extension_keyword |
| 105 keyword_text_id, short_name); | 76 ? short_name |
| 77 : l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT_MD, short_name); |
| 106 full_label_.SetText(full_name); | 78 full_label_.SetText(full_name); |
| 107 | 79 |
| 108 const base::string16 min_string( | 80 const base::string16 min_string( |
| 109 location_bar_util::CalculateMinString(short_name)); | 81 location_bar_util::CalculateMinString(short_name)); |
| 110 const base::string16 partial_name = | 82 const base::string16 partial_name = |
| 111 is_extension_keyword ? min_string : l10n_util::GetStringFUTF16( | 83 is_extension_keyword |
| 112 keyword_text_id, min_string); | 84 ? min_string |
| 85 : l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT_MD, min_string); |
| 113 partial_label_.SetText(min_string.empty() ? | 86 partial_label_.SetText(min_string.empty() ? |
| 114 full_label_.text() : partial_name); | 87 full_label_.text() : partial_name); |
| 88 |
| 89 // Update the label now so ShouldShowLabel() works correctly when the parent |
| 90 // class is calculating the preferred size. It will be updated again in |
| 91 // Layout(), taking into account how much space has actually been allotted. |
| 92 SetLabel(full_name); |
| 115 } | 93 } |
| 116 | 94 |
| 117 const char* SelectedKeywordView::GetClassName() const { | 95 const char* SelectedKeywordView::GetClassName() const { |
| 118 return "SelectedKeywordView"; | 96 return "SelectedKeywordView"; |
| 119 } | 97 } |
| OLD | NEW |