| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "ui/app_list/search_box_view.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 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" | |
| 15 | |
| 16 namespace app_list { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 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; | |
| 25 | |
| 26 const SkColor kHintTextColor = SkColorSetRGB(0xA0, 0xA0, 0xA0); | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 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_); | |
| 37 | |
| 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_); | |
| 44 } | |
| 45 | |
| 46 SearchBoxView::~SearchBoxView() { | |
| 47 if (model_) | |
| 48 model_->RemoveObserver(this); | |
| 49 } | |
| 50 | |
| 51 void SearchBoxView::SetModel(SearchBoxModel* model) { | |
| 52 if (model_ == model) | |
| 53 return; | |
| 54 | |
| 55 if (model_) | |
| 56 model_->RemoveObserver(this); | |
| 57 | |
| 58 model_ = model; | |
| 59 if (model_) { | |
| 60 model_->AddObserver(this); | |
| 61 IconChanged(); | |
| 62 HintTextChanged(); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 gfx::Size SearchBoxView::GetPreferredSize() { | |
| 67 return gfx::Size(kPreferredWidth, kPreferredHeight); | |
| 68 } | |
| 69 | |
| 70 void SearchBoxView::Layout() { | |
| 71 gfx::Rect rect(GetContentsBounds()); | |
| 72 if (rect.IsEmpty()) | |
| 73 return; | |
| 74 | |
| 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); | |
| 79 | |
| 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); | |
| 85 } | |
| 86 | |
| 87 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent& event) { | |
| 88 if (contents_view_) | |
| 89 return contents_view_->OnMouseWheel(event); | |
| 90 | |
| 91 return false; | |
| 92 } | |
| 93 | |
| 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()); | |
| 98 | |
| 99 gfx::SelectionModel sel; | |
| 100 search_box_->GetSelectionModel(&sel); | |
| 101 model_->SetSelectionModel(sel); | |
| 102 model_->AddObserver(this); | |
| 103 } | |
| 104 | |
| 105 void SearchBoxView::NotifyQueryChanged() { | |
| 106 DCHECK(delegate_); | |
| 107 delegate_->QueryChanged(this); | |
| 108 } | |
| 109 | |
| 110 void SearchBoxView::ContentsChanged(views::Textfield* sender, | |
| 111 const string16& new_contents) { | |
| 112 UpdateModel(); | |
| 113 NotifyQueryChanged(); | |
| 114 } | |
| 115 | |
| 116 bool SearchBoxView::HandleKeyEvent(views::Textfield* sender, | |
| 117 const ui::KeyEvent& key_event) { | |
| 118 bool has_query = !search_box_->text().empty(); | |
| 119 | |
| 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; | |
| 128 } | |
| 129 | |
| 130 bool handled = false; | |
| 131 if (contents_view_ && contents_view_->visible()) | |
| 132 handled = contents_view_->OnKeyPressed(key_event); | |
| 133 | |
| 134 return handled; | |
| 135 } | |
| 136 | |
| 137 void SearchBoxView::IconChanged() { | |
| 138 icon_view_->SetImage(model_->icon()); | |
| 139 } | |
| 140 | |
| 141 void SearchBoxView::HintTextChanged() { | |
| 142 search_box_->set_placeholder_text(model_->hint_text()); | |
| 143 } | |
| 144 | |
| 145 void SearchBoxView::SelectionModelChanged() { | |
| 146 search_box_->SelectSelectionModel(model_->selection_model()); | |
| 147 } | |
| 148 | |
| 149 void SearchBoxView::TextChanged() { | |
| 150 search_box_->SetText(model_->text()); | |
| 151 } | |
| 152 | |
| 153 } // namespace app_list | |
| 154 | |
| OLD | NEW |