Home | History | Annotate | Download | only in location_bar
      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/keyword_hint_view.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/logging.h"
     10 #include "base/strings/utf_string_conversions.h"
     11 #include "chrome/app/chrome_command_ids.h"
     12 #include "chrome/browser/profiles/profile.h"
     13 #include "chrome/browser/search_engines/template_url_service_factory.h"
     14 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
     15 #include "chrome/grit/generated_resources.h"
     16 #include "components/search_engines/template_url_service.h"
     17 #include "grit/theme_resources.h"
     18 #include "ui/base/l10n/l10n_util.h"
     19 #include "ui/base/resource/resource_bundle.h"
     20 #include "ui/gfx/canvas.h"
     21 #include "ui/views/controls/image_view.h"
     22 #include "ui/views/controls/label.h"
     23 
     24 
     25 KeywordHintView::KeywordHintView(Profile* profile,
     26                                  const gfx::FontList& font_list,
     27                                  SkColor text_color,
     28                                  SkColor background_color)
     29     : profile_(profile),
     30       tab_image_(new views::ImageView()) {
     31   leading_label_ =
     32       CreateLabel(font_list, text_color, background_color);
     33   tab_image_->SetImage(
     34       ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
     35           IDR_OMNIBOX_KEYWORD_HINT_TAB));
     36   AddChildView(tab_image_);
     37   trailing_label_ =
     38       CreateLabel(font_list, text_color, background_color);
     39 }
     40 
     41 KeywordHintView::~KeywordHintView() {
     42 }
     43 
     44 void KeywordHintView::SetKeyword(const base::string16& keyword) {
     45   keyword_ = keyword;
     46   if (keyword_.empty())
     47     return;
     48   DCHECK(profile_);
     49   TemplateURLService* url_service =
     50       TemplateURLServiceFactory::GetForProfile(profile_);
     51   if (!url_service)
     52     return;
     53 
     54   std::vector<size_t> content_param_offsets;
     55   bool is_extension_keyword;
     56   base::string16 short_name(
     57       url_service->GetKeywordShortName(keyword, &is_extension_keyword));
     58   int message_id = is_extension_keyword ?
     59       IDS_OMNIBOX_EXTENSION_KEYWORD_HINT : IDS_OMNIBOX_KEYWORD_HINT;
     60   const base::string16 keyword_hint = l10n_util::GetStringFUTF16(
     61       message_id, base::string16(), short_name, &content_param_offsets);
     62   DCHECK_EQ(2U, content_param_offsets.size());
     63   leading_label_->SetText(
     64       keyword_hint.substr(0, content_param_offsets.front()));
     65   trailing_label_->SetText(keyword_hint.substr(content_param_offsets.front()));
     66 }
     67 
     68 gfx::Size KeywordHintView::GetPreferredSize() const {
     69   // Height will be ignored by the LocationBarView.
     70   return gfx::Size(leading_label_->GetPreferredSize().width() +
     71                        tab_image_->GetPreferredSize().width() +
     72                        trailing_label_->GetPreferredSize().width(),
     73                    0);
     74 }
     75 
     76 gfx::Size KeywordHintView::GetMinimumSize() const {
     77   // Height will be ignored by the LocationBarView.
     78   return tab_image_->GetPreferredSize();
     79 }
     80 
     81 void KeywordHintView::Layout() {
     82   int tab_width = tab_image_->GetPreferredSize().width();
     83   bool show_labels = (width() != tab_width);
     84   gfx::Size leading_size(leading_label_->GetPreferredSize());
     85   leading_label_->SetBounds(0, 0, show_labels ? leading_size.width() : 0,
     86                             height());
     87   tab_image_->SetBounds(leading_label_->bounds().right(), 0, tab_width,
     88                         height());
     89   gfx::Size trailing_size(trailing_label_->GetPreferredSize());
     90   trailing_label_->SetBounds(tab_image_->bounds().right(), 0,
     91                              show_labels ? trailing_size.width() : 0,
     92                              height());
     93 }
     94 
     95 views::Label* KeywordHintView::CreateLabel(const gfx::FontList& font_list,
     96                                            SkColor text_color,
     97                                            SkColor background_color) {
     98   views::Label* label = new views::Label(base::string16(), font_list);
     99   label->SetEnabledColor(text_color);
    100   label->SetBackgroundColor(background_color);
    101   AddChildView(label);
    102   return label;
    103 }
    104