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/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/views/painter.h" 18 19 20 namespace { 21 const int kBackgroundImages[] = IMAGE_GRID(IDR_OMNIBOX_SELECTED_KEYWORD_BUBBLE); 22 } 23 24 25 SelectedKeywordView::SelectedKeywordView(const gfx::FontList& font_list, 26 SkColor text_color, 27 SkColor parent_background_color, 28 Profile* profile) 29 : IconLabelBubbleView(kBackgroundImages, NULL, IDR_KEYWORD_SEARCH_MAGNIFIER, 30 font_list, text_color, parent_background_color, 31 false), 32 profile_(profile) { 33 full_label_.SetFontList(font_list); 34 full_label_.SetVisible(false); 35 partial_label_.SetFontList(font_list); 36 partial_label_.SetVisible(false); 37 } 38 39 SelectedKeywordView::~SelectedKeywordView() { 40 } 41 42 gfx::Size SelectedKeywordView::GetPreferredSize() const { 43 // Height will be ignored by the LocationBarView. 44 return GetSizeForLabelWidth(full_label_.GetPreferredSize().width()); 45 } 46 47 gfx::Size SelectedKeywordView::GetMinimumSize() const { 48 // Height will be ignored by the LocationBarView. 49 return GetSizeForLabelWidth(partial_label_.GetMinimumSize().width()); 50 } 51 52 void SelectedKeywordView::Layout() { 53 SetLabel(((width() == GetPreferredSize().width()) ? 54 full_label_ : partial_label_).text()); 55 IconLabelBubbleView::Layout(); 56 } 57 58 void SelectedKeywordView::SetKeyword(const base::string16& keyword) { 59 keyword_ = keyword; 60 if (keyword.empty()) 61 return; 62 DCHECK(profile_); 63 TemplateURLService* model = 64 TemplateURLServiceFactory::GetForProfile(profile_); 65 if (!model) 66 return; 67 68 bool is_extension_keyword; 69 const base::string16 short_name = 70 model->GetKeywordShortName(keyword, &is_extension_keyword); 71 const base::string16 full_name = is_extension_keyword ? 72 short_name : 73 l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT, short_name); 74 full_label_.SetText(full_name); 75 76 const base::string16 min_string( 77 location_bar_util::CalculateMinString(short_name)); 78 const base::string16 partial_name = is_extension_keyword ? 79 min_string : 80 l10n_util::GetStringFUTF16(IDS_OMNIBOX_KEYWORD_TEXT, min_string); 81 partial_label_.SetText(min_string.empty() ? 82 full_label_.text() : partial_name); 83 } 84