| 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/keyword_hint_view.h" | 5 #include "chrome/browser/ui/views/location_bar/keyword_hint_view.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/search_engines/template_url_service_factory.h" | 13 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 14 #include "chrome/browser/ui/views/location_bar/background_with_1_px_border.h" | 14 #include "chrome/browser/ui/views/location_bar/background_with_1_px_border.h" |
| 15 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 15 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 16 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "components/search_engines/template_url_service.h" | 17 #include "components/search_engines/template_url_service.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/gfx/color_utils.h" | 19 #include "ui/gfx/color_utils.h" |
| 20 #include "ui/keyboard/keyboard_util.h" |
| 20 #include "ui/strings/grit/ui_strings.h" | 21 #include "ui/strings/grit/ui_strings.h" |
| 21 #include "ui/views/border.h" | 22 #include "ui/views/border.h" |
| 22 #include "ui/views/controls/label.h" | 23 #include "ui/views/controls/label.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // This view sort of looks like a tab key. | 27 // This view looks somewhat like a keyboard key. |
| 27 class TabKeyBubbleView : public views::Label { | 28 class KeyboardKeyView : public views::Label { |
| 28 public: | 29 public: |
| 29 explicit TabKeyBubbleView(const gfx::FontList& font_list); | 30 explicit KeyboardKeyView(const gfx::FontList& font_list); |
| 30 ~TabKeyBubbleView() override; | 31 ~KeyboardKeyView() override; |
| 31 | 32 |
| 32 private: | 33 private: |
| 33 // views::Label: | 34 // views::Label: |
| 34 gfx::Size GetPreferredSize() const override; | 35 gfx::Size GetPreferredSize() const override; |
| 35 | 36 |
| 36 DISALLOW_COPY_AND_ASSIGN(TabKeyBubbleView); | 37 DISALLOW_COPY_AND_ASSIGN(KeyboardKeyView); |
| 37 }; | 38 }; |
| 38 | 39 |
| 39 TabKeyBubbleView::TabKeyBubbleView(const gfx::FontList& font_list) | 40 KeyboardKeyView::KeyboardKeyView(const gfx::FontList& font_list) |
| 40 : views::Label(l10n_util::GetStringUTF16(IDS_APP_TAB_KEY), {font_list}) { | 41 : views::Label(base::string16(), {font_list}) { |
| 41 SetBorder(views::CreateEmptyBorder( | 42 SetBorder(views::CreateEmptyBorder( |
| 42 gfx::Insets(LocationBarView::kBubbleVerticalPadding, 0))); | 43 gfx::Insets(LocationBarView::kBubbleVerticalPadding, 0))); |
| 43 } | 44 } |
| 44 | 45 |
| 45 TabKeyBubbleView::~TabKeyBubbleView() {} | 46 KeyboardKeyView::~KeyboardKeyView() {} |
| 46 | 47 |
| 47 gfx::Size TabKeyBubbleView::GetPreferredSize() const { | 48 gfx::Size KeyboardKeyView::GetPreferredSize() const { |
| 48 gfx::Size size = views::Label::GetPreferredSize(); | 49 gfx::Size size = views::Label::GetPreferredSize(); |
| 49 constexpr int kPaddingInsideBorder = 5; | 50 constexpr int kPaddingInsideBorder = 5; |
| 50 // Even though the border is 1 px thick visibly, it takes 1 DIP logically. | 51 // Even though the border is 1 px thick visibly, it takes 1 DIP logically. |
| 51 size.Enlarge(2 * (kPaddingInsideBorder + 1), 0); | 52 size.Enlarge(2 * (kPaddingInsideBorder + 1), 0); |
| 52 return size; | 53 return size; |
| 53 } | 54 } |
| 54 | 55 |
| 55 } // namespace | 56 } // namespace |
| 56 | 57 |
| 57 KeywordHintView::KeywordHintView(views::ButtonListener* listener, | 58 KeywordHintView::KeywordHintView(views::ButtonListener* listener, |
| 58 Profile* profile, | 59 Profile* profile, |
| 59 const gfx::FontList& font_list, | 60 const gfx::FontList& font_list, |
| 60 const gfx::FontList& bubble_font_list, | 61 const gfx::FontList& bubble_font_list, |
| 61 int bubble_height, | 62 int chip_height, |
| 62 SkColor text_color, | 63 SkColor text_color, |
| 63 SkColor background_color) | 64 SkColor background_color) |
| 64 : CustomButton(listener), | 65 : CustomButton(listener), |
| 65 profile_(profile), | 66 profile_(profile), |
| 66 leading_label_(nullptr), | 67 leading_label_(nullptr), |
| 67 tab_key_view_(nullptr), | 68 chip_view_(new KeyboardKeyView(bubble_font_list)), |
| 68 trailing_label_(nullptr), | 69 trailing_label_(nullptr), |
| 69 tab_key_height_(bubble_height) { | 70 chip_view_height_(chip_height) { |
| 70 leading_label_ = | 71 leading_label_ = |
| 71 CreateLabel(font_list, text_color, background_color); | 72 CreateLabel(font_list, text_color, background_color); |
| 72 TabKeyBubbleView* tab_key = new TabKeyBubbleView(bubble_font_list); | 73 chip_view_->SetEnabledColor(text_color); |
| 73 tab_key->SetEnabledColor(text_color); | |
| 74 bool inverted = color_utils::IsDark(background_color); | 74 bool inverted = color_utils::IsDark(background_color); |
| 75 SkColor tab_bg_color = | 75 SkColor tab_bg_color = |
| 76 inverted ? SK_ColorWHITE : SkColorSetA(text_color, 0x13); | 76 inverted ? SK_ColorWHITE : SkColorSetA(text_color, 0x13); |
| 77 SkColor tab_border_color = inverted ? SK_ColorWHITE : text_color; | 77 SkColor tab_border_color = inverted ? SK_ColorWHITE : text_color; |
| 78 tab_key->SetBackgroundColor(tab_bg_color); | 78 chip_view_->SetBackgroundColor(tab_bg_color); |
| 79 tab_key->set_background( | 79 chip_view_->set_background( |
| 80 new BackgroundWith1PxBorder(tab_bg_color, tab_border_color)); | 80 new BackgroundWith1PxBorder(tab_bg_color, tab_border_color)); |
| 81 tab_key_view_ = tab_key; | 81 AddChildView(chip_view_); |
| 82 AddChildView(tab_key_view_); | |
| 83 trailing_label_ = | 82 trailing_label_ = |
| 84 CreateLabel(font_list, text_color, background_color); | 83 CreateLabel(font_list, text_color, background_color); |
| 85 } | 84 } |
| 86 | 85 |
| 87 KeywordHintView::~KeywordHintView() { | 86 KeywordHintView::~KeywordHintView() {} |
| 88 } | |
| 89 | 87 |
| 90 void KeywordHintView::SetKeyword(const base::string16& keyword) { | 88 void KeywordHintView::SetKeyword(const base::string16& keyword) { |
| 91 keyword_ = keyword; | 89 keyword_ = keyword; |
| 92 if (keyword_.empty()) | 90 if (keyword_.empty()) |
| 93 return; | 91 return; |
| 94 DCHECK(profile_); | 92 DCHECK(profile_); |
| 95 TemplateURLService* url_service = | 93 TemplateURLService* url_service = |
| 96 TemplateURLServiceFactory::GetForProfile(profile_); | 94 TemplateURLServiceFactory::GetForProfile(profile_); |
| 97 if (!url_service) | 95 if (!url_service) |
| 98 return; | 96 return; |
| 99 | 97 |
| 100 std::vector<size_t> content_param_offsets; | |
| 101 bool is_extension_keyword; | 98 bool is_extension_keyword; |
| 102 base::string16 short_name( | 99 base::string16 short_name( |
| 103 url_service->GetKeywordShortName(keyword, &is_extension_keyword)); | 100 url_service->GetKeywordShortName(keyword, &is_extension_keyword)); |
| 104 int message_id = is_extension_keyword ? | 101 |
| 105 IDS_OMNIBOX_EXTENSION_KEYWORD_HINT : IDS_OMNIBOX_KEYWORD_HINT; | 102 if (keyboard::IsKeyboardVisible()) { |
| 106 const base::string16 keyword_hint = l10n_util::GetStringFUTF16( | 103 int message_id = is_extension_keyword |
| 107 message_id, base::string16(), short_name, &content_param_offsets); | 104 ? IDS_OMNIBOX_EXTENSION_KEYWORD_HINT_TOUCH |
| 108 DCHECK_EQ(2U, content_param_offsets.size()); | 105 : IDS_OMNIBOX_KEYWORD_HINT_TOUCH; |
| 109 leading_label_->SetText( | 106 chip_view_->SetText(l10n_util::GetStringFUTF16(message_id, short_name)); |
| 110 keyword_hint.substr(0, content_param_offsets.front())); | 107 |
| 111 trailing_label_->SetText(keyword_hint.substr(content_param_offsets.front())); | 108 leading_label_->SetText(base::string16()); |
| 109 trailing_label_->SetText(base::string16()); |
| 110 } else { |
| 111 chip_view_->SetText(l10n_util::GetStringUTF16(IDS_APP_TAB_KEY)); |
| 112 |
| 113 std::vector<size_t> content_param_offsets; |
| 114 int message_id = is_extension_keyword ? IDS_OMNIBOX_EXTENSION_KEYWORD_HINT |
| 115 : IDS_OMNIBOX_KEYWORD_HINT; |
| 116 const base::string16 keyword_hint = l10n_util::GetStringFUTF16( |
| 117 message_id, base::string16(), short_name, &content_param_offsets); |
| 118 DCHECK_EQ(2U, content_param_offsets.size()); |
| 119 leading_label_->SetText( |
| 120 keyword_hint.substr(0, content_param_offsets.front())); |
| 121 trailing_label_->SetText( |
| 122 keyword_hint.substr(content_param_offsets.front())); |
| 123 } |
| 112 } | 124 } |
| 113 | 125 |
| 114 gfx::Size KeywordHintView::GetPreferredSize() const { | 126 gfx::Size KeywordHintView::GetPreferredSize() const { |
| 115 // Height will be ignored by the LocationBarView. | 127 // Height will be ignored by the LocationBarView. |
| 116 return gfx::Size(leading_label_->GetPreferredSize().width() + | 128 return gfx::Size(leading_label_->GetPreferredSize().width() + |
| 117 tab_key_view_->GetPreferredSize().width() + | 129 chip_view_->GetPreferredSize().width() + |
| 118 trailing_label_->GetPreferredSize().width() + | 130 trailing_label_->GetPreferredSize().width() + |
| 119 LocationBarView::kIconInteriorPadding, | 131 LocationBarView::kIconInteriorPadding, |
| 120 0); | 132 0); |
| 121 } | 133 } |
| 122 | 134 |
| 123 gfx::Size KeywordHintView::GetMinimumSize() const { | 135 gfx::Size KeywordHintView::GetMinimumSize() const { |
| 124 // Height will be ignored by the LocationBarView. | 136 // Height will be ignored by the LocationBarView. |
| 125 return tab_key_view_->GetPreferredSize(); | 137 return chip_view_->GetPreferredSize(); |
| 126 } | 138 } |
| 127 | 139 |
| 128 void KeywordHintView::Layout() { | 140 void KeywordHintView::Layout() { |
| 129 int tab_width = tab_key_view_->GetPreferredSize().width(); | 141 int chip_width = chip_view_->GetPreferredSize().width(); |
| 130 bool show_labels = (width() != tab_width); | 142 bool show_labels = (width() != chip_width); |
| 131 gfx::Size leading_size(leading_label_->GetPreferredSize()); | 143 gfx::Size leading_size(leading_label_->GetPreferredSize()); |
| 132 leading_label_->SetBounds(0, 0, show_labels ? leading_size.width() : 0, | 144 leading_label_->SetBounds(0, 0, show_labels ? leading_size.width() : 0, |
| 133 height()); | 145 height()); |
| 134 tab_key_view_->SetBounds(leading_label_->bounds().right(), | 146 chip_view_->SetBounds(leading_label_->bounds().right(), |
| 135 (height() - tab_key_height_) / 2, tab_width, | 147 (height() - chip_view_height_) / 2, chip_width, |
| 136 tab_key_height_); | 148 chip_view_height_); |
| 137 gfx::Size trailing_size(trailing_label_->GetPreferredSize()); | 149 gfx::Size trailing_size(trailing_label_->GetPreferredSize()); |
| 138 trailing_label_->SetBounds(tab_key_view_->bounds().right(), 0, | 150 trailing_label_->SetBounds(chip_view_->bounds().right(), 0, |
| 139 show_labels ? trailing_size.width() : 0, height()); | 151 show_labels ? trailing_size.width() : 0, height()); |
| 140 } | 152 } |
| 141 | 153 |
| 142 const char* KeywordHintView::GetClassName() const { | 154 const char* KeywordHintView::GetClassName() const { |
| 143 return "KeywordHintView"; | 155 return "KeywordHintView"; |
| 144 } | 156 } |
| 145 | 157 |
| 146 views::Label* KeywordHintView::CreateLabel(const gfx::FontList& font_list, | 158 views::Label* KeywordHintView::CreateLabel(const gfx::FontList& font_list, |
| 147 SkColor text_color, | 159 SkColor text_color, |
| 148 SkColor background_color) { | 160 SkColor background_color) { |
| 149 views::Label* label = new views::Label(base::string16(), {font_list}); | 161 views::Label* label = new views::Label(base::string16(), {font_list}); |
| 150 label->SetEnabledColor(text_color); | 162 label->SetEnabledColor(text_color); |
| 151 label->SetBackgroundColor(background_color); | 163 label->SetBackgroundColor(background_color); |
| 152 AddChildView(label); | 164 AddChildView(label); |
| 153 return label; | 165 return label; |
| 154 } | 166 } |
| OLD | NEW |