Mailbox support for texture layers.
[chromium-blink-merge.git] / ui / app_list / search_box_view.cc
blob95cd83a936fe90334a48160a5e81ed21617a9e9a
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"
7 #include <algorithm>
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"
16 namespace app_list {
18 namespace {
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);
28 } // namespace
30 SearchBoxView::SearchBoxView(SearchBoxViewDelegate* delegate)
31 : delegate_(delegate),
32 model_(NULL),
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() {
47 if (model_)
48 model_->RemoveObserver(this);
51 void SearchBoxView::SetModel(SearchBoxModel* model) {
52 if (model_ == model)
53 return;
55 if (model_)
56 model_->RemoveObserver(this);
58 model_ = model;
59 if (model_) {
60 model_->AddObserver(this);
61 IconChanged();
62 HintTextChanged();
66 gfx::Size SearchBoxView::GetPreferredSize() {
67 return gfx::Size(kPreferredWidth, kPreferredHeight);
70 void SearchBoxView::Layout() {
71 gfx::Rect rect(GetContentsBounds());
72 if (rect.IsEmpty())
73 return;
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) {
88 if (contents_view_)
89 return contents_view_->OnMouseWheel(event);
91 return false;
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() {
106 DCHECK(delegate_);
107 delegate_->QueryChanged(this);
110 void SearchBoxView::ContentsChanged(views::Textfield* sender,
111 const string16& new_contents) {
112 UpdateModel();
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.
125 UpdateModel();
126 NotifyQueryChanged();
127 return true;
130 bool handled = false;
131 if (contents_view_ && contents_view_->visible())
132 handled = contents_view_->OnKeyPressed(key_event);
134 return handled;
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