Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / app_list / views / search_box_view.cc
blobea973ae67f34d37f04607991229001c40dcfe0d0
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_->SetBackgroundColor(kSearchBoxBackground);
166 search_box_->set_placeholder_text_color(kHintTextColor);
167 search_box_->set_controller(this);
168 search_box_->SetTextInputType(ui::TEXT_INPUT_TYPE_SEARCH);
169 search_box_->SetTextInputFlags(ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF);
170 content_container_->AddChildView(search_box_);
171 layout->SetFlexForView(search_box_, 1);
173 #if !defined(OS_CHROMEOS)
174 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
175 menu_button_ = new views::MenuButton(NULL, base::string16(), this, false);
176 menu_button_->SetBorder(views::Border::NullBorder());
177 menu_button_->SetImage(views::Button::STATE_NORMAL,
178 *rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_NORMAL));
179 menu_button_->SetImage(views::Button::STATE_HOVERED,
180 *rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_HOVER));
181 menu_button_->SetImage(views::Button::STATE_PRESSED,
182 *rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_PRESSED));
183 content_container_->AddChildView(menu_button_);
184 #endif
186 view_delegate_->GetSpeechUI()->AddObserver(this);
187 ModelChanged();
190 SearchBoxView::~SearchBoxView() {
191 view_delegate_->GetSpeechUI()->RemoveObserver(this);
192 model_->search_box()->RemoveObserver(this);
195 void SearchBoxView::ModelChanged() {
196 if (model_)
197 model_->search_box()->RemoveObserver(this);
199 model_ = view_delegate_->GetModel();
200 DCHECK(model_);
201 model_->search_box()->AddObserver(this);
202 IconChanged();
203 SpeechRecognitionButtonPropChanged();
204 HintTextChanged();
207 bool SearchBoxView::HasSearch() const {
208 return !search_box_->text().empty();
211 void SearchBoxView::ClearSearch() {
212 search_box_->SetText(base::string16());
213 view_delegate_->AutoLaunchCanceled();
214 // Updates model and fires query changed manually because SetText() above
215 // does not generate ContentsChanged() notification.
216 UpdateModel();
217 NotifyQueryChanged();
220 void SearchBoxView::InvalidateMenu() {
221 menu_.reset();
224 void SearchBoxView::SetShadow(const gfx::ShadowValue& shadow) {
225 SetBorder(make_scoped_ptr(new views::ShadowBorder(shadow)));
226 Layout();
229 gfx::Rect SearchBoxView::GetViewBoundsForSearchBoxContentsBounds(
230 const gfx::Rect& rect) const {
231 gfx::Rect view_bounds = rect;
232 view_bounds.Inset(-GetInsets());
233 return view_bounds;
236 views::ImageButton* SearchBoxView::back_button() {
237 return static_cast<views::ImageButton*>(back_button_);
240 // Returns true if set internally, i.e. if focused_view_ != CONTENTS_VIEW.
241 // Note: because we always want to be able to type in the edit box, this is only
242 // a faux-focus so that buttons can respond to the ENTER key.
243 bool SearchBoxView::MoveTabFocus(bool move_backwards) {
244 if (back_button_)
245 back_button_->SetSelected(false);
246 if (speech_button_)
247 speech_button_->SetSelected(false);
249 switch (focused_view_) {
250 case FOCUS_BACK_BUTTON:
251 focused_view_ = move_backwards ? FOCUS_BACK_BUTTON : FOCUS_SEARCH_BOX;
252 break;
253 case FOCUS_SEARCH_BOX:
254 if (move_backwards) {
255 focused_view_ = back_button_ && back_button_->visible()
256 ? FOCUS_BACK_BUTTON : FOCUS_SEARCH_BOX;
257 } else {
258 focused_view_ = speech_button_ && speech_button_->visible()
259 ? FOCUS_MIC_BUTTON : FOCUS_CONTENTS_VIEW;
261 break;
262 case FOCUS_MIC_BUTTON:
263 focused_view_ = move_backwards ? FOCUS_SEARCH_BOX : FOCUS_CONTENTS_VIEW;
264 break;
265 case FOCUS_CONTENTS_VIEW:
266 focused_view_ = move_backwards
267 ? (speech_button_ && speech_button_->visible() ?
268 FOCUS_MIC_BUTTON : FOCUS_SEARCH_BOX)
269 : FOCUS_CONTENTS_VIEW;
270 break;
271 default:
272 DCHECK(false);
275 switch (focused_view_) {
276 case FOCUS_BACK_BUTTON:
277 if (back_button_)
278 back_button_->SetSelected(true);
279 break;
280 case FOCUS_SEARCH_BOX:
281 // Set the ChromeVox focus to the search box. However, DO NOT do this if
282 // we are in the search results state (i.e., if the search box has text in
283 // it), because the focus is about to be shifted to the first search
284 // result and we do not want to read out the name of the search box as
285 // well.
286 if (search_box_->text().empty())
287 search_box_->NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
288 break;
289 case FOCUS_MIC_BUTTON:
290 if (speech_button_)
291 speech_button_->SetSelected(true);
292 break;
293 default:
294 break;
297 if (focused_view_ < FOCUS_CONTENTS_VIEW)
298 delegate_->SetSearchResultSelection(focused_view_ == FOCUS_SEARCH_BOX);
300 return (focused_view_ < FOCUS_CONTENTS_VIEW);
303 void SearchBoxView::ResetTabFocus(bool on_contents) {
304 if (back_button_)
305 back_button_->SetSelected(false);
306 if (speech_button_)
307 speech_button_->SetSelected(false);
308 focused_view_ = on_contents ? FOCUS_CONTENTS_VIEW : FOCUS_SEARCH_BOX;
311 gfx::Size SearchBoxView::GetPreferredSize() const {
312 return gfx::Size(kPreferredWidth, kPreferredHeight);
315 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent& event) {
316 if (contents_view_)
317 return contents_view_->OnMouseWheel(event);
319 return false;
322 void SearchBoxView::OnEnabledChanged() {
323 search_box_->SetEnabled(enabled());
324 if (menu_button_)
325 menu_button_->SetEnabled(enabled());
326 if (speech_button_)
327 speech_button_->SetEnabled(enabled());
330 void SearchBoxView::UpdateModel() {
331 // Temporarily remove from observer to ignore notifications caused by us.
332 model_->search_box()->RemoveObserver(this);
333 model_->search_box()->SetText(search_box_->text());
334 model_->search_box()->SetSelectionModel(search_box_->GetSelectionModel());
335 model_->search_box()->AddObserver(this);
338 void SearchBoxView::NotifyQueryChanged() {
339 DCHECK(delegate_);
340 delegate_->QueryChanged(this);
343 void SearchBoxView::ContentsChanged(views::Textfield* sender,
344 const base::string16& new_contents) {
345 UpdateModel();
346 view_delegate_->AutoLaunchCanceled();
347 NotifyQueryChanged();
350 bool SearchBoxView::HandleKeyEvent(views::Textfield* sender,
351 const ui::KeyEvent& key_event) {
352 bool handled = false;
353 if (key_event.key_code() == ui::VKEY_TAB) {
354 if (focused_view_ != FOCUS_CONTENTS_VIEW &&
355 MoveTabFocus(key_event.IsShiftDown()))
356 return true;
359 if (focused_view_ == FOCUS_BACK_BUTTON && back_button_ &&
360 back_button_->OnKeyPressed(key_event))
361 return true;
363 if (focused_view_ == FOCUS_MIC_BUTTON && speech_button_ &&
364 speech_button_->OnKeyPressed(key_event))
365 return true;
367 if (contents_view_ && contents_view_->visible())
368 handled = contents_view_->OnKeyPressed(key_event);
370 // Arrow keys may have selected an item. If they did, move focus off buttons.
371 // If they didn't, we still select the first search item, in case they're
372 // moving the caret through typed search text. The UP arrow never moves
373 // focus from text/buttons to app list/results, so ignore it.
374 if (focused_view_ < FOCUS_CONTENTS_VIEW &&
375 (key_event.key_code() == ui::VKEY_LEFT ||
376 key_event.key_code() == ui::VKEY_RIGHT ||
377 key_event.key_code() == ui::VKEY_DOWN)) {
378 if (!handled)
379 delegate_->SetSearchResultSelection(true);
380 ResetTabFocus(handled);
383 return handled;
386 void SearchBoxView::ButtonPressed(views::Button* sender,
387 const ui::Event& event) {
388 if (back_button_ && sender == back_button_)
389 delegate_->BackButtonPressed();
390 else if (speech_button_ && sender == speech_button_)
391 view_delegate_->ToggleSpeechRecognition();
392 else
393 NOTREACHED();
396 void SearchBoxView::OnMenuButtonClicked(View* source, const gfx::Point& point) {
397 if (!menu_)
398 menu_.reset(new AppListMenuViews(view_delegate_));
400 const gfx::Point menu_location =
401 menu_button_->GetBoundsInScreen().bottom_right() +
402 gfx::Vector2d(kMenuXOffsetFromButton, kMenuYOffsetFromButton);
403 menu_->RunMenuAt(menu_button_, menu_location);
406 void SearchBoxView::IconChanged() {
407 if (icon_view_)
408 icon_view_->SetImage(model_->search_box()->icon());
411 void SearchBoxView::SpeechRecognitionButtonPropChanged() {
412 const SearchBoxModel::SpeechButtonProperty* speech_button_prop =
413 model_->search_box()->speech_button();
414 if (speech_button_prop) {
415 if (!speech_button_) {
416 speech_button_ = new SearchBoxImageButton(this);
417 content_container_->AddChildView(speech_button_);
420 speech_button_->SetAccessibleName(speech_button_prop->accessible_name);
421 if (view_delegate_->GetSpeechUI()->state() ==
422 SPEECH_RECOGNITION_HOTWORD_LISTENING) {
423 speech_button_->SetImage(
424 views::Button::STATE_NORMAL, &speech_button_prop->on_icon);
425 speech_button_->SetTooltipText(speech_button_prop->on_tooltip);
426 } else {
427 speech_button_->SetImage(
428 views::Button::STATE_NORMAL, &speech_button_prop->off_icon);
429 speech_button_->SetTooltipText(speech_button_prop->off_tooltip);
431 } else {
432 if (speech_button_) {
433 // Deleting a view will detach it from its parent.
434 delete speech_button_;
435 speech_button_ = NULL;
438 Layout();
441 void SearchBoxView::HintTextChanged() {
442 const app_list::SearchBoxModel* search_box = model_->search_box();
443 search_box_->set_placeholder_text(search_box->hint_text());
444 search_box_->SetAccessibleName(search_box->accessible_name());
447 void SearchBoxView::SelectionModelChanged() {
448 search_box_->SelectSelectionModel(model_->search_box()->selection_model());
451 void SearchBoxView::TextChanged() {
452 search_box_->SetText(model_->search_box()->text());
453 NotifyQueryChanged();
456 void SearchBoxView::OnSpeechRecognitionStateChanged(
457 SpeechRecognitionState new_state) {
458 SpeechRecognitionButtonPropChanged();
459 SchedulePaint();
462 } // namespace app_list