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 "ui/app_list/views/search_box_view.h" | 5 #include "ui/app_list/views/search_box_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 #include "ui/app_list/app_list_constants.h" | 12 #include "ui/app_list/app_list_constants.h" |
13 #include "ui/app_list/app_list_model.h" | 13 #include "ui/app_list/app_list_model.h" |
14 #include "ui/app_list/app_list_switches.h" | 14 #include "ui/app_list/app_list_switches.h" |
15 #include "ui/app_list/app_list_view_delegate.h" | 15 #include "ui/app_list/app_list_view_delegate.h" |
16 #include "ui/app_list/resources/grit/app_list_resources.h" | 16 #include "ui/app_list/resources/grit/app_list_resources.h" |
17 #include "ui/app_list/search_box_model.h" | 17 #include "ui/app_list/search_box_model.h" |
18 #include "ui/app_list/speech_ui_model.h" | 18 #include "ui/app_list/speech_ui_model.h" |
19 #include "ui/app_list/views/app_list_menu_views.h" | |
20 #include "ui/app_list/views/contents_view.h" | 19 #include "ui/app_list/views/contents_view.h" |
21 #include "ui/app_list/views/search_box_view_delegate.h" | 20 #include "ui/app_list/views/search_box_view_delegate.h" |
22 #include "ui/base/ime/text_input_flags.h" | 21 #include "ui/base/ime/text_input_flags.h" |
23 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
24 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
25 #include "ui/events/event.h" | 24 #include "ui/events/event.h" |
26 #include "ui/gfx/canvas.h" | 25 #include "ui/gfx/canvas.h" |
27 #include "ui/gfx/shadow_value.h" | 26 #include "ui/gfx/shadow_value.h" |
28 #include "ui/strings/grit/ui_strings.h" | 27 #include "ui/strings/grit/ui_strings.h" |
29 #include "ui/views/border.h" | 28 #include "ui/views/border.h" |
30 #include "ui/views/controls/button/image_button.h" | 29 #include "ui/views/controls/button/image_button.h" |
31 #include "ui/views/controls/button/menu_button.h" | |
32 #include "ui/views/controls/image_view.h" | 30 #include "ui/views/controls/image_view.h" |
33 #include "ui/views/controls/textfield/textfield.h" | 31 #include "ui/views/controls/textfield/textfield.h" |
34 #include "ui/views/layout/box_layout.h" | 32 #include "ui/views/layout/box_layout.h" |
35 #include "ui/views/layout/fill_layout.h" | 33 #include "ui/views/layout/fill_layout.h" |
36 #include "ui/views/shadow_border.h" | 34 #include "ui/views/shadow_border.h" |
37 | 35 |
38 namespace app_list { | 36 namespace app_list { |
39 | 37 |
40 namespace { | 38 namespace { |
41 | 39 |
42 const int kPadding = 16; | 40 const int kPadding = 16; |
43 const int kInnerPadding = 24; | 41 const int kInnerPadding = 24; |
44 const int kPreferredWidth = 360; | 42 const int kPreferredWidth = 360; |
45 const int kPreferredHeight = 48; | 43 const int kPreferredHeight = 48; |
46 | 44 |
47 const SkColor kHintTextColor = SkColorSetRGB(0xA0, 0xA0, 0xA0); | 45 const SkColor kHintTextColor = SkColorSetRGB(0xA0, 0xA0, 0xA0); |
48 | 46 |
49 // Menu offset relative to the bottom-right corner of the menu button. | |
50 const int kMenuYOffsetFromButton = -4; | |
51 const int kMenuXOffsetFromButton = -7; | |
52 | |
53 const int kBackgroundBorderCornerRadius = 2; | 47 const int kBackgroundBorderCornerRadius = 2; |
54 | 48 |
55 // A background that paints a solid white rounded rect with a thin grey border. | 49 // A background that paints a solid white rounded rect with a thin grey border. |
56 class ExperimentalSearchBoxBackground : public views::Background { | 50 class ExperimentalSearchBoxBackground : public views::Background { |
57 public: | 51 public: |
58 ExperimentalSearchBoxBackground() {} | 52 ExperimentalSearchBoxBackground() {} |
59 ~ExperimentalSearchBoxBackground() override {} | 53 ~ExperimentalSearchBoxBackground() override {} |
60 | 54 |
61 private: | 55 private: |
62 // views::Background overrides: | 56 // views::Background overrides: |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 111 |
118 SearchBoxView::SearchBoxView(SearchBoxViewDelegate* delegate, | 112 SearchBoxView::SearchBoxView(SearchBoxViewDelegate* delegate, |
119 AppListViewDelegate* view_delegate) | 113 AppListViewDelegate* view_delegate) |
120 : delegate_(delegate), | 114 : delegate_(delegate), |
121 view_delegate_(view_delegate), | 115 view_delegate_(view_delegate), |
122 model_(NULL), | 116 model_(NULL), |
123 content_container_(new views::View), | 117 content_container_(new views::View), |
124 icon_view_(NULL), | 118 icon_view_(NULL), |
125 back_button_(NULL), | 119 back_button_(NULL), |
126 speech_button_(NULL), | 120 speech_button_(NULL), |
127 menu_button_(NULL), | |
128 search_box_(new views::Textfield), | 121 search_box_(new views::Textfield), |
129 contents_view_(NULL), | 122 contents_view_(NULL), |
130 focused_view_(FOCUS_SEARCH_BOX) { | 123 focused_view_(FOCUS_SEARCH_BOX) { |
131 SetLayoutManager(new views::FillLayout); | 124 SetLayoutManager(new views::FillLayout); |
132 AddChildView(content_container_); | 125 AddChildView(content_container_); |
133 | 126 |
134 if (switches::IsExperimentalAppListEnabled()) { | 127 if (switches::IsExperimentalAppListEnabled()) { |
135 SetShadow(GetShadowForZHeight(2)); | 128 SetShadow(GetShadowForZHeight(2)); |
136 back_button_ = new SearchBoxImageButton(this); | 129 back_button_ = new SearchBoxImageButton(this); |
137 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 130 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
(...skipping 26 matching lines...) Expand all Loading... |
164 search_box_->SetBorder(views::Border::NullBorder()); | 157 search_box_->SetBorder(views::Border::NullBorder()); |
165 search_box_->SetTextColor(kSearchTextColor); | 158 search_box_->SetTextColor(kSearchTextColor); |
166 search_box_->SetBackgroundColor(kSearchBoxBackground); | 159 search_box_->SetBackgroundColor(kSearchBoxBackground); |
167 search_box_->set_placeholder_text_color(kHintTextColor); | 160 search_box_->set_placeholder_text_color(kHintTextColor); |
168 search_box_->set_controller(this); | 161 search_box_->set_controller(this); |
169 search_box_->SetTextInputType(ui::TEXT_INPUT_TYPE_SEARCH); | 162 search_box_->SetTextInputType(ui::TEXT_INPUT_TYPE_SEARCH); |
170 search_box_->SetTextInputFlags(ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF); | 163 search_box_->SetTextInputFlags(ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF); |
171 content_container_->AddChildView(search_box_); | 164 content_container_->AddChildView(search_box_); |
172 layout->SetFlexForView(search_box_, 1); | 165 layout->SetFlexForView(search_box_, 1); |
173 | 166 |
174 #if !defined(OS_CHROMEOS) | |
175 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
176 menu_button_ = new views::MenuButton(base::string16(), this, false); | |
177 menu_button_->SetBorder(views::Border::NullBorder()); | |
178 menu_button_->SetImage(views::Button::STATE_NORMAL, | |
179 *rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_NORMAL)); | |
180 menu_button_->SetImage(views::Button::STATE_HOVERED, | |
181 *rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_HOVER)); | |
182 menu_button_->SetImage(views::Button::STATE_PRESSED, | |
183 *rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_PRESSED)); | |
184 content_container_->AddChildView(menu_button_); | |
185 #endif | |
186 | |
187 view_delegate_->GetSpeechUI()->AddObserver(this); | 167 view_delegate_->GetSpeechUI()->AddObserver(this); |
188 ModelChanged(); | 168 ModelChanged(); |
189 } | 169 } |
190 | 170 |
191 SearchBoxView::~SearchBoxView() { | 171 SearchBoxView::~SearchBoxView() { |
192 view_delegate_->GetSpeechUI()->RemoveObserver(this); | 172 view_delegate_->GetSpeechUI()->RemoveObserver(this); |
193 model_->search_box()->RemoveObserver(this); | 173 model_->search_box()->RemoveObserver(this); |
194 } | 174 } |
195 | 175 |
196 void SearchBoxView::ModelChanged() { | 176 void SearchBoxView::ModelChanged() { |
(...skipping 14 matching lines...) Expand all Loading... |
211 | 191 |
212 void SearchBoxView::ClearSearch() { | 192 void SearchBoxView::ClearSearch() { |
213 search_box_->SetText(base::string16()); | 193 search_box_->SetText(base::string16()); |
214 view_delegate_->AutoLaunchCanceled(); | 194 view_delegate_->AutoLaunchCanceled(); |
215 // Updates model and fires query changed manually because SetText() above | 195 // Updates model and fires query changed manually because SetText() above |
216 // does not generate ContentsChanged() notification. | 196 // does not generate ContentsChanged() notification. |
217 UpdateModel(); | 197 UpdateModel(); |
218 NotifyQueryChanged(); | 198 NotifyQueryChanged(); |
219 } | 199 } |
220 | 200 |
221 void SearchBoxView::InvalidateMenu() { | |
222 menu_.reset(); | |
223 } | |
224 | |
225 void SearchBoxView::SetShadow(const gfx::ShadowValue& shadow) { | 201 void SearchBoxView::SetShadow(const gfx::ShadowValue& shadow) { |
226 SetBorder(base::WrapUnique(new views::ShadowBorder(shadow))); | 202 SetBorder(base::WrapUnique(new views::ShadowBorder(shadow))); |
227 Layout(); | 203 Layout(); |
228 } | 204 } |
229 | 205 |
230 gfx::Rect SearchBoxView::GetViewBoundsForSearchBoxContentsBounds( | 206 gfx::Rect SearchBoxView::GetViewBoundsForSearchBoxContentsBounds( |
231 const gfx::Rect& rect) const { | 207 const gfx::Rect& rect) const { |
232 gfx::Rect view_bounds = rect; | 208 gfx::Rect view_bounds = rect; |
233 view_bounds.Inset(-GetInsets()); | 209 view_bounds.Inset(-GetInsets()); |
234 return view_bounds; | 210 return view_bounds; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 | 302 |
327 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent& event) { | 303 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent& event) { |
328 if (contents_view_) | 304 if (contents_view_) |
329 return contents_view_->OnMouseWheel(event); | 305 return contents_view_->OnMouseWheel(event); |
330 | 306 |
331 return false; | 307 return false; |
332 } | 308 } |
333 | 309 |
334 void SearchBoxView::OnEnabledChanged() { | 310 void SearchBoxView::OnEnabledChanged() { |
335 search_box_->SetEnabled(enabled()); | 311 search_box_->SetEnabled(enabled()); |
336 if (menu_button_) | |
337 menu_button_->SetEnabled(enabled()); | |
338 if (speech_button_) | 312 if (speech_button_) |
339 speech_button_->SetEnabled(enabled()); | 313 speech_button_->SetEnabled(enabled()); |
340 } | 314 } |
341 | 315 |
342 void SearchBoxView::UpdateModel() { | 316 void SearchBoxView::UpdateModel() { |
343 // Temporarily remove from observer to ignore notifications caused by us. | 317 // Temporarily remove from observer to ignore notifications caused by us. |
344 model_->search_box()->RemoveObserver(this); | 318 model_->search_box()->RemoveObserver(this); |
345 model_->search_box()->SetText(search_box_->text()); | 319 model_->search_box()->SetText(search_box_->text()); |
346 model_->search_box()->SetSelectionModel(search_box_->GetSelectionModel()); | 320 model_->search_box()->SetSelectionModel(search_box_->GetSelectionModel()); |
347 model_->search_box()->AddObserver(this); | 321 model_->search_box()->AddObserver(this); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 void SearchBoxView::ButtonPressed(views::Button* sender, | 387 void SearchBoxView::ButtonPressed(views::Button* sender, |
414 const ui::Event& event) { | 388 const ui::Event& event) { |
415 if (back_button_ && sender == back_button_) | 389 if (back_button_ && sender == back_button_) |
416 delegate_->BackButtonPressed(); | 390 delegate_->BackButtonPressed(); |
417 else if (speech_button_ && sender == speech_button_) | 391 else if (speech_button_ && sender == speech_button_) |
418 view_delegate_->StartSpeechRecognition(); | 392 view_delegate_->StartSpeechRecognition(); |
419 else | 393 else |
420 NOTREACHED(); | 394 NOTREACHED(); |
421 } | 395 } |
422 | 396 |
423 void SearchBoxView::OnMenuButtonClicked(views::MenuButton* source, | |
424 const gfx::Point& point, | |
425 const ui::Event* event) { | |
426 if (!menu_) | |
427 menu_.reset(new AppListMenuViews(view_delegate_)); | |
428 | |
429 const gfx::Point menu_location = | |
430 menu_button_->GetBoundsInScreen().bottom_right() + | |
431 gfx::Vector2d(kMenuXOffsetFromButton, kMenuYOffsetFromButton); | |
432 menu_->RunMenuAt(menu_button_, menu_location); | |
433 } | |
434 | |
435 void SearchBoxView::IconChanged() { | 397 void SearchBoxView::IconChanged() { |
436 if (icon_view_) | 398 if (icon_view_) |
437 icon_view_->SetImage(model_->search_box()->icon()); | 399 icon_view_->SetImage(model_->search_box()->icon()); |
438 } | 400 } |
439 | 401 |
440 void SearchBoxView::SpeechRecognitionButtonPropChanged() { | 402 void SearchBoxView::SpeechRecognitionButtonPropChanged() { |
441 const SearchBoxModel::SpeechButtonProperty* speech_button_prop = | 403 const SearchBoxModel::SpeechButtonProperty* speech_button_prop = |
442 model_->search_box()->speech_button(); | 404 model_->search_box()->speech_button(); |
443 if (speech_button_prop) { | 405 if (speech_button_prop) { |
444 if (!speech_button_) { | 406 if (!speech_button_) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 NotifyQueryChanged(); | 444 NotifyQueryChanged(); |
483 } | 445 } |
484 | 446 |
485 void SearchBoxView::OnSpeechRecognitionStateChanged( | 447 void SearchBoxView::OnSpeechRecognitionStateChanged( |
486 SpeechRecognitionState new_state) { | 448 SpeechRecognitionState new_state) { |
487 SpeechRecognitionButtonPropChanged(); | 449 SpeechRecognitionButtonPropChanged(); |
488 SchedulePaint(); | 450 SchedulePaint(); |
489 } | 451 } |
490 | 452 |
491 } // namespace app_list | 453 } // namespace app_list |
OLD | NEW |