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.
5 #include "ui/app_list/views/search_box_view.h"
9 #include "ui/app_list/app_list_constants.h"
10 #include "ui/app_list/app_list_model.h"
11 #include "ui/app_list/app_list_switches.h"
12 #include "ui/app_list/app_list_view_delegate.h"
13 #include "ui/app_list/search_box_model.h"
14 #include "ui/app_list/speech_ui_model.h"
15 #include "ui/app_list/views/app_list_menu_views.h"
16 #include "ui/app_list/views/contents_view.h"
17 #include "ui/app_list/views/search_box_view_delegate.h"
18 #include "ui/base/ime/text_input_flags.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/events/event.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/shadow_value.h"
24 #include "ui/resources/grit/ui_resources.h"
25 #include "ui/strings/grit/ui_strings.h"
26 #include "ui/views/border.h"
27 #include "ui/views/controls/button/image_button.h"
28 #include "ui/views/controls/button/menu_button.h"
29 #include "ui/views/controls/image_view.h"
30 #include "ui/views/controls/textfield/textfield.h"
31 #include "ui/views/layout/box_layout.h"
32 #include "ui/views/layout/fill_layout.h"
33 #include "ui/views/shadow_border.h"
39 const int kPadding
= 16;
40 const int kInnerPadding
= 24;
41 const int kPreferredWidth
= 360;
42 const int kPreferredHeight
= 48;
44 const SkColor kHintTextColor
= SkColorSetRGB(0xA0, 0xA0, 0xA0);
46 // Menu offset relative to the bottom-right corner of the menu button.
47 const int kMenuYOffsetFromButton
= -4;
48 const int kMenuXOffsetFromButton
= -7;
50 const int kBackgroundBorderCornerRadius
= 2;
52 // A background that paints a solid white rounded rect with a thin grey border.
53 class ExperimentalSearchBoxBackground
: public views::Background
{
55 ExperimentalSearchBoxBackground() {}
56 ~ExperimentalSearchBoxBackground() override
{}
59 // views::Background overrides:
60 void Paint(gfx::Canvas
* canvas
, views::View
* view
) const override
{
61 gfx::Rect bounds
= view
->GetContentsBounds();
64 paint
.setFlags(SkPaint::kAntiAlias_Flag
);
65 paint
.setColor(kSearchBoxBackground
);
66 canvas
->DrawRoundRect(bounds
, kBackgroundBorderCornerRadius
, paint
);
69 DISALLOW_COPY_AND_ASSIGN(ExperimentalSearchBoxBackground
);
74 // To paint grey background on mic and back buttons
75 class SearchBoxImageButton
: public views::ImageButton
{
77 explicit SearchBoxImageButton(views::ButtonListener
* listener
)
78 : ImageButton(listener
), selected_(false) {}
79 ~SearchBoxImageButton() override
{}
81 bool selected() { return selected_
; }
82 void SetSelected(bool selected
) {
83 if (selected_
== selected
)
89 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS
, true);
92 bool OnKeyPressed(const ui::KeyEvent
& event
) override
{
93 // Disable space key to press the button. The keyboard events received
94 // by this view are forwarded from a Textfield (SearchBoxView) and key
95 // released events are not forwarded. This leaves the button in pressed
97 if (event
.key_code() == ui::VKEY_SPACE
)
100 return CustomButton::OnKeyPressed(event
);
104 // views::View overrides:
105 void OnPaintBackground(gfx::Canvas
* canvas
) override
{
106 if (state_
== STATE_HOVERED
|| state_
== STATE_PRESSED
|| selected_
)
107 canvas
->FillRect(gfx::Rect(size()), kSelectedColor
);
112 DISALLOW_COPY_AND_ASSIGN(SearchBoxImageButton
);
115 SearchBoxView::SearchBoxView(SearchBoxViewDelegate
* delegate
,
116 AppListViewDelegate
* view_delegate
)
117 : delegate_(delegate
),
118 view_delegate_(view_delegate
),
120 content_container_(new views::View
),
123 speech_button_(NULL
),
125 search_box_(new views::Textfield
),
126 contents_view_(NULL
),
127 focused_view_(FOCUS_SEARCH_BOX
) {
128 SetLayoutManager(new views::FillLayout
);
129 AddChildView(content_container_
);
131 if (switches::IsExperimentalAppListEnabled()) {
132 SetShadow(GetShadowForZHeight(2));
133 back_button_
= new SearchBoxImageButton(this);
134 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
135 back_button_
->SetImage(
136 views::ImageButton::STATE_NORMAL
,
137 rb
.GetImageSkiaNamed(IDR_APP_LIST_FOLDER_BACK_NORMAL
));
138 back_button_
->SetImageAlignment(views::ImageButton::ALIGN_CENTER
,
139 views::ImageButton::ALIGN_MIDDLE
);
140 base::string16
back_title(l10n_util::GetStringUTF16(IDS_APP_LIST_BACK
));
141 back_button_
->SetAccessibleName(back_title
);
142 back_button_
->SetTooltipText(back_title
);
143 content_container_
->AddChildView(back_button_
);
145 content_container_
->set_background(new ExperimentalSearchBoxBackground());
148 views::Background::CreateSolidBackground(kSearchBoxBackground
));
150 views::Border::CreateSolidSidedBorder(0, 0, 1, 0, kTopSeparatorColor
));
151 icon_view_
= new views::ImageView
;
152 content_container_
->AddChildView(icon_view_
);
155 views::BoxLayout
* layout
=
156 new views::BoxLayout(views::BoxLayout::kHorizontal
, kPadding
, 0,
157 kInnerPadding
- views::Textfield::kTextPadding
);
158 content_container_
->SetLayoutManager(layout
);
159 layout
->set_cross_axis_alignment(
160 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER
);
161 layout
->set_minimum_cross_axis_size(kPreferredHeight
);
163 search_box_
->SetBorder(views::Border::NullBorder());
164 search_box_
->SetTextColor(kSearchTextColor
);
165 search_box_
->set_placeholder_text_color(kHintTextColor
);
166 search_box_
->set_controller(this);
167 search_box_
->SetTextInputType(ui::TEXT_INPUT_TYPE_SEARCH
);
168 search_box_
->SetTextInputFlags(ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF
);
169 content_container_
->AddChildView(search_box_
);
170 layout
->SetFlexForView(search_box_
, 1);
172 #if !defined(OS_CHROMEOS)
173 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
174 menu_button_
= new views::MenuButton(NULL
, base::string16(), this, false);
175 menu_button_
->SetBorder(views::Border::NullBorder());
176 menu_button_
->SetImage(views::Button::STATE_NORMAL
,
177 *rb
.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_NORMAL
));
178 menu_button_
->SetImage(views::Button::STATE_HOVERED
,
179 *rb
.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_HOVER
));
180 menu_button_
->SetImage(views::Button::STATE_PRESSED
,
181 *rb
.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_PRESSED
));
182 content_container_
->AddChildView(menu_button_
);
185 view_delegate_
->GetSpeechUI()->AddObserver(this);
189 SearchBoxView::~SearchBoxView() {
190 view_delegate_
->GetSpeechUI()->RemoveObserver(this);
191 model_
->search_box()->RemoveObserver(this);
194 void SearchBoxView::ModelChanged() {
196 model_
->search_box()->RemoveObserver(this);
198 model_
= view_delegate_
->GetModel();
200 model_
->search_box()->AddObserver(this);
202 SpeechRecognitionButtonPropChanged();
206 bool SearchBoxView::HasSearch() const {
207 return !search_box_
->text().empty();
210 void SearchBoxView::ClearSearch() {
211 search_box_
->SetText(base::string16());
212 view_delegate_
->AutoLaunchCanceled();
213 // Updates model and fires query changed manually because SetText() above
214 // does not generate ContentsChanged() notification.
216 NotifyQueryChanged();
219 void SearchBoxView::InvalidateMenu() {
223 void SearchBoxView::SetShadow(const gfx::ShadowValue
& shadow
) {
224 SetBorder(make_scoped_ptr(new views::ShadowBorder(shadow
)));
228 gfx::Rect
SearchBoxView::GetViewBoundsForSearchBoxContentsBounds(
229 const gfx::Rect
& rect
) const {
230 gfx::Rect view_bounds
= rect
;
231 view_bounds
.Inset(-GetInsets());
235 views::ImageButton
* SearchBoxView::back_button() {
236 return static_cast<views::ImageButton
*>(back_button_
);
239 // Returns true if set internally, i.e. if focused_view_ != CONTENTS_VIEW.
240 // Note: because we always want to be able to type in the edit box, this is only
241 // a faux-focus so that buttons can respond to the ENTER key.
242 bool SearchBoxView::MoveTabFocus(bool move_backwards
) {
244 back_button_
->SetSelected(false);
246 speech_button_
->SetSelected(false);
248 switch (focused_view_
) {
249 case FOCUS_BACK_BUTTON
:
250 focused_view_
= move_backwards
? FOCUS_BACK_BUTTON
: FOCUS_SEARCH_BOX
;
252 case FOCUS_SEARCH_BOX
:
253 if (move_backwards
) {
254 focused_view_
= back_button_
&& back_button_
->visible()
255 ? FOCUS_BACK_BUTTON
: FOCUS_SEARCH_BOX
;
257 focused_view_
= speech_button_
&& speech_button_
->visible()
258 ? FOCUS_MIC_BUTTON
: FOCUS_CONTENTS_VIEW
;
261 case FOCUS_MIC_BUTTON
:
262 focused_view_
= move_backwards
? FOCUS_SEARCH_BOX
: FOCUS_CONTENTS_VIEW
;
264 case FOCUS_CONTENTS_VIEW
:
265 focused_view_
= move_backwards
266 ? (speech_button_
&& speech_button_
->visible() ?
267 FOCUS_MIC_BUTTON
: FOCUS_SEARCH_BOX
)
268 : FOCUS_CONTENTS_VIEW
;
274 switch (focused_view_
) {
275 case FOCUS_BACK_BUTTON
:
277 back_button_
->SetSelected(true);
279 case FOCUS_SEARCH_BOX
:
280 // Set the ChromeVox focus to the search box. However, DO NOT do this if
281 // we are in the search results state (i.e., if the search box has text in
282 // it), because the focus is about to be shifted to the first search
283 // result and we do not want to read out the name of the search box as
285 if (search_box_
->text().empty())
286 search_box_
->NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS
, true);
288 case FOCUS_MIC_BUTTON
:
290 speech_button_
->SetSelected(true);
296 if (focused_view_
< FOCUS_CONTENTS_VIEW
)
297 delegate_
->SetSearchResultSelection(focused_view_
== FOCUS_SEARCH_BOX
);
299 return (focused_view_
< FOCUS_CONTENTS_VIEW
);
302 void SearchBoxView::ResetTabFocus(bool on_contents
) {
304 back_button_
->SetSelected(false);
306 speech_button_
->SetSelected(false);
307 focused_view_
= on_contents
? FOCUS_CONTENTS_VIEW
: FOCUS_SEARCH_BOX
;
310 gfx::Size
SearchBoxView::GetPreferredSize() const {
311 return gfx::Size(kPreferredWidth
, kPreferredHeight
);
314 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent
& event
) {
316 return contents_view_
->OnMouseWheel(event
);
321 void SearchBoxView::OnEnabledChanged() {
322 search_box_
->SetEnabled(enabled());
324 menu_button_
->SetEnabled(enabled());
326 speech_button_
->SetEnabled(enabled());
329 void SearchBoxView::UpdateModel() {
330 // Temporarily remove from observer to ignore notifications caused by us.
331 model_
->search_box()->RemoveObserver(this);
332 model_
->search_box()->SetText(search_box_
->text());
333 model_
->search_box()->SetSelectionModel(search_box_
->GetSelectionModel());
334 model_
->search_box()->AddObserver(this);
337 void SearchBoxView::NotifyQueryChanged() {
339 delegate_
->QueryChanged(this);
342 void SearchBoxView::ContentsChanged(views::Textfield
* sender
,
343 const base::string16
& new_contents
) {
345 view_delegate_
->AutoLaunchCanceled();
346 NotifyQueryChanged();
349 bool SearchBoxView::HandleKeyEvent(views::Textfield
* sender
,
350 const ui::KeyEvent
& key_event
) {
351 bool handled
= false;
352 if (key_event
.key_code() == ui::VKEY_TAB
) {
353 if (focused_view_
!= FOCUS_CONTENTS_VIEW
&&
354 MoveTabFocus(key_event
.IsShiftDown()))
358 if (focused_view_
== FOCUS_BACK_BUTTON
&& back_button_
&&
359 back_button_
->OnKeyPressed(key_event
))
362 if (focused_view_
== FOCUS_MIC_BUTTON
&& speech_button_
&&
363 speech_button_
->OnKeyPressed(key_event
))
366 if (contents_view_
&& contents_view_
->visible())
367 handled
= contents_view_
->OnKeyPressed(key_event
);
369 // Arrow keys may have selected an item. If they did, move focus off buttons.
370 // If they didn't, we still select the first search item, in case they're
371 // moving the caret through typed search text. The UP arrow never moves
372 // focus from text/buttons to app list/results, so ignore it.
373 if (focused_view_
< FOCUS_CONTENTS_VIEW
&&
374 (key_event
.key_code() == ui::VKEY_LEFT
||
375 key_event
.key_code() == ui::VKEY_RIGHT
||
376 key_event
.key_code() == ui::VKEY_DOWN
)) {
378 delegate_
->SetSearchResultSelection(true);
379 ResetTabFocus(handled
);
385 void SearchBoxView::ButtonPressed(views::Button
* sender
,
386 const ui::Event
& event
) {
387 if (back_button_
&& sender
== back_button_
)
388 delegate_
->BackButtonPressed();
389 else if (speech_button_
&& sender
== speech_button_
)
390 view_delegate_
->ToggleSpeechRecognition();
395 void SearchBoxView::OnMenuButtonClicked(View
* source
, const gfx::Point
& point
) {
397 menu_
.reset(new AppListMenuViews(view_delegate_
));
399 const gfx::Point menu_location
=
400 menu_button_
->GetBoundsInScreen().bottom_right() +
401 gfx::Vector2d(kMenuXOffsetFromButton
, kMenuYOffsetFromButton
);
402 menu_
->RunMenuAt(menu_button_
, menu_location
);
405 void SearchBoxView::IconChanged() {
407 icon_view_
->SetImage(model_
->search_box()->icon());
410 void SearchBoxView::SpeechRecognitionButtonPropChanged() {
411 const SearchBoxModel::SpeechButtonProperty
* speech_button_prop
=
412 model_
->search_box()->speech_button();
413 if (speech_button_prop
) {
414 if (!speech_button_
) {
415 speech_button_
= new SearchBoxImageButton(this);
416 content_container_
->AddChildView(speech_button_
);
419 speech_button_
->SetAccessibleName(speech_button_prop
->accessible_name
);
420 if (view_delegate_
->GetSpeechUI()->state() ==
421 SPEECH_RECOGNITION_HOTWORD_LISTENING
) {
422 speech_button_
->SetImage(
423 views::Button::STATE_NORMAL
, &speech_button_prop
->on_icon
);
424 speech_button_
->SetTooltipText(speech_button_prop
->on_tooltip
);
426 speech_button_
->SetImage(
427 views::Button::STATE_NORMAL
, &speech_button_prop
->off_icon
);
428 speech_button_
->SetTooltipText(speech_button_prop
->off_tooltip
);
431 if (speech_button_
) {
432 // Deleting a view will detach it from its parent.
433 delete speech_button_
;
434 speech_button_
= NULL
;
440 void SearchBoxView::HintTextChanged() {
441 const app_list::SearchBoxModel
* search_box
= model_
->search_box();
442 search_box_
->set_placeholder_text(search_box
->hint_text());
443 search_box_
->SetAccessibleName(search_box
->accessible_name());
446 void SearchBoxView::SelectionModelChanged() {
447 search_box_
->SelectSelectionModel(model_
->search_box()->selection_model());
450 void SearchBoxView::TextChanged() {
451 search_box_
->SetText(model_
->search_box()->text());
452 NotifyQueryChanged();
455 void SearchBoxView::OnSpeechRecognitionStateChanged(
456 SpeechRecognitionState new_state
) {
457 SpeechRecognitionButtonPropChanged();
461 } // namespace app_list