Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / location_bar / selected_keyword_view.cc
blobc5e302a90fc47942e97d657aad54c32bbcf19261
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.
5 #include "chrome/browser/ui/views/location_bar/selected_keyword_view.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/search_engines/template_url_service_factory.h"
11 #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"
14 #include "components/search_engines/template_url_service.h"
15 #include "grit/theme_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/resource/material_design/material_design_controller.h"
18 #include "ui/views/painter.h"
20 SelectedKeywordView::SelectedKeywordView(const gfx::FontList& font_list,
21 SkColor text_color,
22 SkColor parent_background_color,
23 Profile* profile)
24 : IconLabelBubbleView(IDR_KEYWORD_SEARCH_MAGNIFIER,
25 font_list,
26 text_color,
27 parent_background_color,
28 false),
29 profile_(profile) {
30 if (ui::MaterialDesignController::IsModeMaterial()) {
31 // The inset for IDR_OMNIBOX_SELECTED_KEYWORD_BUBBLE for which to perfom
32 // nine-slicing.
33 static const int kImageInset = 4;
34 gfx::Insets insets(kImageInset, kImageInset, kImageInset, kImageInset);
35 SetBackgroundImageWithInsets(IDR_OMNIBOX_SELECTED_KEYWORD_BUBBLE, insets);
36 } else {
37 static const int kBackgroundImages[] =
38 IMAGE_GRID(IDR_OMNIBOX_SELECTED_KEYWORD_BUBBLE);
39 SetBackgroundImageGrid(kBackgroundImages);
41 full_label_.SetFontList(font_list);
42 full_label_.SetVisible(false);
43 partial_label_.SetFontList(font_list);
44 partial_label_.SetVisible(false);
47 SelectedKeywordView::~SelectedKeywordView() {
50 gfx::Size SelectedKeywordView::GetPreferredSize() const {
51 // Height will be ignored by the LocationBarView.
52 return GetSizeForLabelWidth(full_label_.GetPreferredSize().width());
55 gfx::Size SelectedKeywordView::GetMinimumSize() const {
56 // Height will be ignored by the LocationBarView.
57 return GetSizeForLabelWidth(partial_label_.GetMinimumSize().width());
60 void SelectedKeywordView::Layout() {
61 SetLabel(((width() == GetPreferredSize().width()) ?
62 full_label_ : partial_label_).text());
63 IconLabelBubbleView::Layout();
66 void SelectedKeywordView::SetKeyword(const base::string16& keyword) {
67 keyword_ = keyword;
68 if (keyword.empty())
69 return;
70 DCHECK(profile_);
71 TemplateURLService* model =
72 TemplateURLServiceFactory::GetForProfile(profile_);
73 if (!model)
74 return;
76 bool is_extension_keyword;
77 const base::string16 short_name =
78 model->GetKeywordShortName(keyword, &is_extension_keyword);
79 const base::string16 full_name = is_extension_keyword ?
80 short_name :
81 l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT, short_name);
82 full_label_.SetText(full_name);
84 const base::string16 min_string(
85 location_bar_util::CalculateMinString(short_name));
86 const base::string16 partial_name = is_extension_keyword ?
87 min_string :
88 l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT, min_string);
89 partial_label_.SetText(min_string.empty() ?
90 full_label_.text() : partial_name);
93 const char* SelectedKeywordView::GetClassName() const {
94 return "SelectedKeywordView";