| 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 "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/search_engines/template_url_service_factory.h" | 9 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 10 #include "chrome/browser/ui/location_bar/location_bar_util.h" | |
| 11 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 10 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 12 #include "chrome/grit/generated_resources.h" | 11 #include "chrome/grit/generated_resources.h" |
| 13 #include "components/search_engines/template_url_service.h" | 12 #include "components/search_engines/template_url_service.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/base/theme_provider.h" | 14 #include "ui/base/theme_provider.h" |
| 16 #include "ui/gfx/color_palette.h" | 15 #include "ui/gfx/color_palette.h" |
| 17 #include "ui/gfx/color_utils.h" | 16 #include "ui/gfx/color_utils.h" |
| 18 #include "ui/gfx/paint_vector_icon.h" | 17 #include "ui/gfx/paint_vector_icon.h" |
| 19 #include "ui/native_theme/native_theme.h" | 18 #include "ui/native_theme/native_theme.h" |
| 20 #include "ui/vector_icons/vector_icons.h" | 19 #include "ui/vector_icons/vector_icons.h" |
| 21 | 20 |
| 22 SelectedKeywordView::SelectedKeywordView(const gfx::FontList& font_list, | 21 SelectedKeywordView::SelectedKeywordView(const gfx::FontList& font_list, |
| 23 Profile* profile) | 22 Profile* profile) |
| 24 : IconLabelBubbleView(font_list, false), profile_(profile) { | 23 : IconLabelBubbleView(font_list, false), profile_(profile) { |
| 25 full_label_.SetFontList(font_list); | 24 full_label_.SetFontList(font_list); |
| 26 full_label_.SetVisible(false); | 25 full_label_.SetVisible(false); |
| 27 partial_label_.SetFontList(font_list); | 26 partial_label_.SetFontList(font_list); |
| 28 partial_label_.SetVisible(false); | 27 partial_label_.SetVisible(false); |
| 28 label()->SetElideBehavior(gfx::FADE_TAIL); |
| 29 } | 29 } |
| 30 | 30 |
| 31 SelectedKeywordView::~SelectedKeywordView() { | 31 SelectedKeywordView::~SelectedKeywordView() { |
| 32 } | 32 } |
| 33 | 33 |
| 34 void SelectedKeywordView::ResetImage() { | 34 void SelectedKeywordView::ResetImage() { |
| 35 SetImage(gfx::CreateVectorIcon(ui::kSearchIcon, LocationBarView::kIconWidth, | 35 SetImage(gfx::CreateVectorIcon(ui::kSearchIcon, LocationBarView::kIconWidth, |
| 36 GetTextColor())); | 36 GetTextColor())); |
| 37 } | 37 } |
| 38 | 38 |
| 39 SkColor SelectedKeywordView::GetTextColor() const { | 39 SkColor SelectedKeywordView::GetTextColor() const { |
| 40 return GetNativeTheme()->GetSystemColor( | 40 return GetNativeTheme()->GetSystemColor( |
| 41 color_utils::IsDark(GetParentBackgroundColor()) | 41 color_utils::IsDark(GetParentBackgroundColor()) |
| 42 ? ui::NativeTheme::kColorId_TextfieldDefaultColor | 42 ? ui::NativeTheme::kColorId_TextfieldDefaultColor |
| 43 : ui::NativeTheme::kColorId_LinkEnabled); | 43 : ui::NativeTheme::kColorId_LinkEnabled); |
| 44 } | 44 } |
| 45 | 45 |
| 46 gfx::Size SelectedKeywordView::GetPreferredSize() const { | 46 gfx::Size SelectedKeywordView::GetPreferredSize() const { |
| 47 // Height will be ignored by the LocationBarView. | 47 // Height will be ignored by the LocationBarView. |
| 48 return GetSizeForLabelWidth(full_label_.GetPreferredSize().width()); | 48 return GetSizeForLabelWidth(full_label_.GetPreferredSize().width()); |
| 49 } | 49 } |
| 50 | 50 |
| 51 gfx::Size SelectedKeywordView::GetMinimumSize() const { | 51 gfx::Size SelectedKeywordView::GetMinimumSize() const { |
| 52 // Height will be ignored by the LocationBarView. | 52 // Height will be ignored by the LocationBarView. |
| 53 return GetSizeForLabelWidth(partial_label_.GetMinimumSize().width()); | 53 return GetSizeForLabelWidth(0); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void SelectedKeywordView::Layout() { | 56 void SelectedKeywordView::Layout() { |
| 57 SetLabel(((width() == GetPreferredSize().width()) ? | 57 // Keep showing the full label as long as there's more than enough width for |
| 58 full_label_ : partial_label_).text()); | 58 // the partial label. Otherwise there will be empty space displayed next to |
| 59 // the partial label. |
| 60 bool use_full_label = |
| 61 width() > |
| 62 GetSizeForLabelWidth(partial_label_.GetPreferredSize().width()).width(); |
| 63 SetLabel(use_full_label ? full_label_.text() : partial_label_.text()); |
| 64 |
| 59 IconLabelBubbleView::Layout(); | 65 IconLabelBubbleView::Layout(); |
| 60 } | 66 } |
| 61 | 67 |
| 62 void SelectedKeywordView::SetKeyword(const base::string16& keyword) { | 68 void SelectedKeywordView::SetKeyword(const base::string16& keyword) { |
| 63 keyword_ = keyword; | 69 keyword_ = keyword; |
| 64 if (keyword.empty()) | 70 if (keyword.empty()) |
| 65 return; | 71 return; |
| 66 DCHECK(profile_); | 72 DCHECK(profile_); |
| 67 TemplateURLService* model = | 73 TemplateURLService* model = |
| 68 TemplateURLServiceFactory::GetForProfile(profile_); | 74 TemplateURLServiceFactory::GetForProfile(profile_); |
| 69 if (!model) | 75 if (!model) |
| 70 return; | 76 return; |
| 71 | 77 |
| 72 bool is_extension_keyword; | 78 bool is_extension_keyword; |
| 73 const base::string16 short_name = | 79 const base::string16 short_name = |
| 74 model->GetKeywordShortName(keyword, &is_extension_keyword); | 80 model->GetKeywordShortName(keyword, &is_extension_keyword); |
| 75 const base::string16 full_name = | 81 const base::string16 full_name = |
| 76 is_extension_keyword | 82 is_extension_keyword |
| 77 ? short_name | 83 ? short_name |
| 78 : l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT_MD, short_name); | 84 : l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT_MD, short_name); |
| 79 full_label_.SetText(full_name); | 85 full_label_.SetText(full_name); |
| 80 | 86 |
| 81 const base::string16 min_string( | 87 partial_label_.SetText(short_name); |
| 82 location_bar_util::CalculateMinString(short_name)); | |
| 83 const base::string16 partial_name = | |
| 84 is_extension_keyword | |
| 85 ? min_string | |
| 86 : l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT_MD, min_string); | |
| 87 partial_label_.SetText(min_string.empty() ? | |
| 88 full_label_.text() : partial_name); | |
| 89 | 88 |
| 90 // Update the label now so ShouldShowLabel() works correctly when the parent | 89 // Update the label now so ShouldShowLabel() works correctly when the parent |
| 91 // class is calculating the preferred size. It will be updated again in | 90 // class is calculating the preferred size. It will be updated again in |
| 92 // Layout(), taking into account how much space has actually been allotted. | 91 // Layout(), taking into account how much space has actually been allotted. |
| 93 SetLabel(full_name); | 92 SetLabel(full_name); |
| 94 } | 93 } |
| 95 | 94 |
| 96 const char* SelectedKeywordView::GetClassName() const { | 95 const char* SelectedKeywordView::GetClassName() const { |
| 97 return "SelectedKeywordView"; | 96 return "SelectedKeywordView"; |
| 98 } | 97 } |
| OLD | NEW |