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/search_box_view.h"
9 #include "ui/app_list/search_box_model.h"
10 #include "ui/app_list/search_box_view_delegate.h"
11 #include "ui/base/events/event.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/views/controls/image_view.h"
14 #include "ui/views/controls/textfield/textfield.h"
20 const int kPadding
= 14;
21 const int kIconDimension
= 32;
22 const int kPreferredWidth
= 360;
23 const int kPreferredHeight
= 48;
24 const int kEditHeight
= 19;
26 const SkColor kHintTextColor
= SkColorSetRGB(0xA0, 0xA0, 0xA0);
30 SearchBoxView::SearchBoxView(SearchBoxViewDelegate
* delegate
)
31 : delegate_(delegate
),
33 icon_view_(new views::ImageView
),
34 search_box_(new views::Textfield
),
35 contents_view_(NULL
) {
36 AddChildView(icon_view_
);
38 search_box_
->RemoveBorder();
39 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
40 search_box_
->SetFont(rb
.GetFont(ui::ResourceBundle::MediumFont
));
41 search_box_
->set_placeholder_text_color(kHintTextColor
);
42 search_box_
->SetController(this);
43 AddChildView(search_box_
);
46 SearchBoxView::~SearchBoxView() {
48 model_
->RemoveObserver(this);
51 void SearchBoxView::SetModel(SearchBoxModel
* model
) {
56 model_
->RemoveObserver(this);
60 model_
->AddObserver(this);
66 gfx::Size
SearchBoxView::GetPreferredSize() {
67 return gfx::Size(kPreferredWidth
, kPreferredHeight
);
70 void SearchBoxView::Layout() {
71 gfx::Rect
rect(GetContentsBounds());
75 gfx::Size
icon_size(kIconDimension
, kIconDimension
);
76 gfx::Rect
icon_frame(rect
);
77 icon_frame
.set_width(icon_size
.width() + 2 * kPadding
);
78 icon_view_
->SetBoundsRect(icon_frame
);
80 gfx::Rect
edit_frame(rect
);
81 edit_frame
.set_x(icon_frame
.right());
82 edit_frame
.set_width(rect
.width() - icon_frame
.width() - kPadding
);
83 edit_frame
.ClampToCenteredSize(gfx::Size(edit_frame
.width(), kEditHeight
));
84 search_box_
->SetBoundsRect(edit_frame
);
87 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent
& event
) {
89 return contents_view_
->OnMouseWheel(event
);
94 void SearchBoxView::UpdateModel() {
95 // Temporarily remove from observer to ignore notifications caused by us.
96 model_
->RemoveObserver(this);
97 model_
->SetText(search_box_
->text());
99 gfx::SelectionModel sel
;
100 search_box_
->GetSelectionModel(&sel
);
101 model_
->SetSelectionModel(sel
);
102 model_
->AddObserver(this);
105 void SearchBoxView::NotifyQueryChanged() {
107 delegate_
->QueryChanged(this);
110 void SearchBoxView::ContentsChanged(views::Textfield
* sender
,
111 const string16
& new_contents
) {
113 NotifyQueryChanged();
116 bool SearchBoxView::HandleKeyEvent(views::Textfield
* sender
,
117 const ui::KeyEvent
& key_event
) {
118 bool has_query
= !search_box_
->text().empty();
120 // Escape with non-empty query text clears the search box.
121 if (has_query
&& key_event
.key_code() == ui::VKEY_ESCAPE
) {
122 search_box_
->SetText(string16());
123 // Updates model and fires query changed manually because SetText above
124 // does not generate ContentsChanged notification.
126 NotifyQueryChanged();
130 bool handled
= false;
131 if (contents_view_
&& contents_view_
->visible())
132 handled
= contents_view_
->OnKeyPressed(key_event
);
137 void SearchBoxView::IconChanged() {
138 icon_view_
->SetImage(model_
->icon());
141 void SearchBoxView::HintTextChanged() {
142 search_box_
->set_placeholder_text(model_
->hint_text());
145 void SearchBoxView::SelectionModelChanged() {
146 search_box_
->SelectSelectionModel(model_
->selection_model());
149 void SearchBoxView::TextChanged() {
150 search_box_
->SetText(model_
->text());
153 } // namespace app_list