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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/views/location_bar/selected_keyword_view.h"
6
7 #include "base/check.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/search_engines/template_url_service_factory.h"
10 #include "chrome/browser/ui/layout_constants.h"
11 #include "chrome/browser/ui/omnibox/omnibox_theme.h"
12 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "components/search_engines/template_url_service.h"
15 #include "components/vector_icons/vector_icons.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/theme_provider.h"
18 #include "ui/gfx/color_palette.h"
19 #include "ui/gfx/color_utils.h"
20 #include "ui/gfx/paint_vector_icon.h"
21
22 // static
23 SelectedKeywordView::KeywordLabelNames
GetKeywordLabelNames(const base::string16 & keyword,TemplateURLService * service)24 SelectedKeywordView::GetKeywordLabelNames(const base::string16& keyword,
25 TemplateURLService* service) {
26 KeywordLabelNames names;
27 if (service) {
28 bool is_extension_keyword = false;
29 names.short_name =
30 service->GetKeywordShortName(keyword, &is_extension_keyword);
31 names.full_name = is_extension_keyword
32 ? names.short_name
33 : l10n_util::GetStringFUTF16(
34 IDS_OMNIBOX_KEYWORD_TEXT_MD, names.short_name);
35 }
36 return names;
37 }
38
SelectedKeywordView(LocationBarView * location_bar,const gfx::FontList & font_list)39 SelectedKeywordView::SelectedKeywordView(LocationBarView* location_bar,
40 const gfx::FontList& font_list)
41 : IconLabelBubbleView(font_list, location_bar),
42 location_bar_(location_bar) {
43 full_label_.SetFontList(font_list);
44 full_label_.SetVisible(false);
45 partial_label_.SetFontList(font_list);
46 partial_label_.SetVisible(false);
47 label()->SetElideBehavior(gfx::FADE_TAIL);
48 }
49
~SelectedKeywordView()50 SelectedKeywordView::~SelectedKeywordView() {}
51
SetCustomImage(const gfx::Image & image)52 void SelectedKeywordView::SetCustomImage(const gfx::Image& image) {
53 using_custom_image_ = !image.IsEmpty();
54 if (using_custom_image_) {
55 IconLabelBubbleView::SetImageModel(ui::ImageModel::FromImage(image));
56 } else {
57 IconLabelBubbleView::SetImageModel(ui::ImageModel::FromVectorIcon(
58 vector_icons::kSearchIcon, GetForegroundColor(),
59 GetLayoutConstant(LOCATION_BAR_ICON_SIZE)));
60 }
61 }
62
OnBoundsChanged(const gfx::Rect & previous_bounds)63 void SelectedKeywordView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
64 SetLabelForCurrentWidth();
65 }
66
GetForegroundColor() const67 SkColor SelectedKeywordView::GetForegroundColor() const {
68 return location_bar_->GetColor(OmniboxPart::LOCATION_BAR_SELECTED_KEYWORD);
69 }
70
CalculatePreferredSize() const71 gfx::Size SelectedKeywordView::CalculatePreferredSize() const {
72 // Height will be ignored by the LocationBarView.
73 return GetSizeForLabelWidth(full_label_.GetPreferredSize().width());
74 }
75
GetMinimumSize() const76 gfx::Size SelectedKeywordView::GetMinimumSize() const {
77 // Height will be ignored by the LocationBarView.
78 return GetSizeForLabelWidth(0);
79 }
80
OnThemeChanged()81 void SelectedKeywordView::OnThemeChanged() {
82 IconLabelBubbleView::OnThemeChanged();
83 if (!using_custom_image_)
84 SetCustomImage(gfx::Image());
85 }
86
SetKeyword(const base::string16 & keyword,Profile * profile)87 void SelectedKeywordView::SetKeyword(const base::string16& keyword,
88 Profile* profile) {
89 keyword_ = keyword;
90 if (keyword.empty())
91 return;
92
93 DCHECK(profile);
94 TemplateURLService* service =
95 TemplateURLServiceFactory::GetForProfile(profile);
96 if (!service)
97 return;
98
99 KeywordLabelNames names = GetKeywordLabelNames(keyword, service);
100 full_label_.SetText(names.full_name);
101 partial_label_.SetText(names.short_name);
102
103 // Update the label now so ShouldShowLabel() works correctly when the parent
104 // class is calculating the preferred size. It will be updated again in
105 // Layout(), taking into account how much space has actually been allotted.
106 SetLabelForCurrentWidth();
107 NotifyAccessibilityEvent(ax::mojom::Event::kLiveRegionChanged, true);
108 }
109
GetExtraInternalSpacing() const110 int SelectedKeywordView::GetExtraInternalSpacing() const {
111 // Align the label text with the suggestion text.
112 return 11;
113 }
114
GetClassName() const115 const char* SelectedKeywordView::GetClassName() const {
116 return "SelectedKeywordView";
117 }
118
SetLabelForCurrentWidth()119 void SelectedKeywordView::SetLabelForCurrentWidth() {
120 // Keep showing the full label as long as there's more than enough width for
121 // the partial label. Otherwise there will be empty space displayed next to
122 // the partial label.
123 bool use_full_label =
124 width() >
125 GetSizeForLabelWidth(partial_label_.GetPreferredSize().width()).width();
126 SetLabel(use_full_label ? full_label_.GetText() : partial_label_.GetText());
127 }
128