Files.app: Use core-icon for the dropdown button of combobutton.
[chromium-blink-merge.git] / ui / app_list / views / search_box_view.cc
blob5f2ec39baf7d33ca09fd276aaf0f481ca45a0568
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"
7 #include <algorithm>
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"
35 namespace app_list {
37 namespace {
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 {
54 public:
55 ExperimentalSearchBoxBackground() {}
56 ~ExperimentalSearchBoxBackground() override {}
58 private:
59 // views::Background overrides:
60 void Paint(gfx::Canvas* canvas, views::View* view) const override {
61 gfx::Rect bounds = view->GetContentsBounds();
63 SkPaint paint;
64 paint.setFlags(SkPaint::kAntiAlias_Flag);
65 paint.setColor(kSearchBoxBackground);
66 canvas->DrawRoundRect(bounds, kBackgroundBorderCornerRadius, paint);
69 DISALLOW_COPY_AND_ASSIGN(ExperimentalSearchBoxBackground);
72 } // namespace
74 // To paint grey background on mic and back buttons
75 class SearchBoxImageButton : public views::ImageButton {
76 public:
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)
84 return;
86 selected_ = selected;
87 SchedulePaint();
88 if (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
96 // state.
97 if (event.key_code() == ui::VKEY_SPACE)
98 return false;
100 return CustomButton::OnKeyPressed(event);
103 private:
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);
110 bool selected_;
112 DISALLOW_COPY_AND_ASSIGN(SearchBoxImageButton);
115 SearchBoxView::SearchBoxView(SearchBoxViewDelegate* delegate,
116 AppListViewDelegate* view_delegate)
117 : delegate_(delegate),
118 view_delegate_(view_delegate),
119 model_(NULL),
120 content_container_(new views::View),
121 icon_view_(NULL),
122 back_button_(NULL),
123 speech_button_(NULL),
124 menu_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());
146 } else {
147 set_background(
148 views::Background::CreateSolidBackground(kSearchBoxBackground));
149 SetBorder(
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_);
183 #endif
185 view_delegate_->GetSpeechUI()->AddObserver(this);
186 ModelChanged();
189 SearchBoxView::~SearchBoxView() {
190 view_delegate_->GetSpeechUI()->RemoveObserver(this);
191 model_->search_box()->RemoveObserver(this);
194 void SearchBoxView::ModelChanged() {
195 if (model_)
196 model_->search_box()->RemoveObserver(this);
198 model_ = view_delegate_->GetModel();
199 DCHECK(model_);
200 model_->search_box()->AddObserver(this);
201 IconChanged();
202 SpeechRecognitionButtonPropChanged();
203 HintTextChanged();
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.
215 UpdateModel();
216 NotifyQueryChanged();
219 void SearchBoxView::InvalidateMenu() {
220 menu_.reset();
223 void SearchBoxView::SetShadow(const gfx::ShadowValue& shadow) {
224 SetBorder(make_scoped_ptr(new views::ShadowBorder(shadow)));
225 Layout();
228 gfx::Rect SearchBoxView::GetViewBoundsForSearchBoxContentsBounds(
229 const gfx::Rect& rect) const {
230 gfx::Rect view_bounds = rect;
231 view_bounds.Inset(-GetInsets());
232 return view_bounds;
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) {
243 if (back_button_)
244 back_button_->SetSelected(false);
245 if (speech_button_)
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;
251 break;
252 case FOCUS_SEARCH_BOX:
253 if (move_backwards) {
254 focused_view_ = back_button_ && back_button_->visible()
255 ? FOCUS_BACK_BUTTON : FOCUS_SEARCH_BOX;
256 } else {
257 focused_view_ = speech_button_ && speech_button_->visible()
258 ? FOCUS_MIC_BUTTON : FOCUS_CONTENTS_VIEW;
260 break;
261 case FOCUS_MIC_BUTTON:
262 focused_view_ = move_backwards ? FOCUS_SEARCH_BOX : FOCUS_CONTENTS_VIEW;
263 break;
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;
269 break;
270 default:
271 DCHECK(false);
274 switch (focused_view_) {
275 case FOCUS_BACK_BUTTON:
276 if (back_button_)
277 back_button_->SetSelected(true);
278 break;
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
284 // well.
285 if (search_box_->text().empty())
286 search_box_->NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
287 break;
288 case FOCUS_MIC_BUTTON:
289 if (speech_button_)
290 speech_button_->SetSelected(true);
291 break;
292 default:
293 break;
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) {
303 if (back_button_)
304 back_button_->SetSelected(false);
305 if (speech_button_)
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) {
315 if (contents_view_)
316 return contents_view_->OnMouseWheel(event);
318 return false;
321 void SearchBoxView::OnEnabledChanged() {
322 search_box_->SetEnabled(enabled());
323 if (menu_button_)
324 menu_button_->SetEnabled(enabled());
325 if (speech_button_)
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() {
338 DCHECK(delegate_);
339 delegate_->QueryChanged(this);
342 void SearchBoxView::ContentsChanged(views::Textfield* sender,
343 const base::string16& new_contents) {
344 UpdateModel();
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()))
355 return true;
358 if (focused_view_ == FOCUS_BACK_BUTTON && back_button_ &&
359 back_button_->OnKeyPressed(key_event))
360 return true;
362 if (focused_view_ == FOCUS_MIC_BUTTON && speech_button_ &&
363 speech_button_->OnKeyPressed(key_event))
364 return true;
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)) {
377 if (!handled)
378 delegate_->SetSearchResultSelection(true);
379 ResetTabFocus(handled);
382 return 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();
391 else
392 NOTREACHED();
395 void SearchBoxView::OnMenuButtonClicked(View* source, const gfx::Point& point) {
396 if (!menu_)
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() {
406 if (icon_view_)
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);
425 } else {
426 speech_button_->SetImage(
427 views::Button::STATE_NORMAL, &speech_button_prop->off_icon);
428 speech_button_->SetTooltipText(speech_button_prop->off_tooltip);
430 } else {
431 if (speech_button_) {
432 // Deleting a view will detach it from its parent.
433 delete speech_button_;
434 speech_button_ = NULL;
437 Layout();
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();
458 SchedulePaint();
461 } // namespace app_list