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