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