| 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/resource/resource_bundle.h" |
| 12 #include "ui/views/controls/image_view.h" |
| 13 #include "ui/views/controls/textfield/textfield.h" |
| 14 |
| 15 namespace app_list { |
| 16 |
| 17 namespace { |
| 18 |
| 19 const int kPadding = 9; |
| 20 const int kIconDimension = 32; |
| 21 const int kPreferredWidth = 360; |
| 22 const int kPreferredHeight = 48; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 SearchBoxView::SearchBoxView(SearchBoxViewDelegate* delegate) |
| 27 : delegate_(delegate), |
| 28 model_(NULL), |
| 29 icon_view_(NULL), |
| 30 search_box_(NULL), |
| 31 grid_view_(NULL), |
| 32 results_view_(NULL) { |
| 33 icon_view_ = new views::ImageView; |
| 34 AddChildView(icon_view_); |
| 35 |
| 36 search_box_ = new views::Textfield; |
| 37 search_box_->RemoveBorder(); |
| 38 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 39 search_box_->SetFont(rb.GetFont(ResourceBundle::BaseFont).DeriveFont( |
| 40 2, gfx::Font::BOLD)); |
| 41 search_box_->SetController(this); |
| 42 AddChildView(search_box_); |
| 43 } |
| 44 |
| 45 SearchBoxView::~SearchBoxView() { |
| 46 if (model_) |
| 47 model_->RemoveObserver(this); |
| 48 } |
| 49 |
| 50 void SearchBoxView::SetModel(SearchBoxModel* model) { |
| 51 if (model_ == model) |
| 52 return; |
| 53 |
| 54 if (model_) |
| 55 model_->RemoveObserver(this); |
| 56 |
| 57 model_ = model; |
| 58 if (model_) { |
| 59 model_->AddObserver(this); |
| 60 IconChanged(); |
| 61 HintTextChanged(); |
| 62 } |
| 63 } |
| 64 |
| 65 gfx::Size SearchBoxView::GetPreferredSize() { |
| 66 return gfx::Size(kPreferredWidth, kPreferredHeight); |
| 67 } |
| 68 |
| 69 void SearchBoxView::Layout() { |
| 70 gfx::Rect rect(GetContentsBounds()); |
| 71 if (rect.IsEmpty()) |
| 72 return; |
| 73 |
| 74 gfx::Size icon_size(kIconDimension, kIconDimension); |
| 75 gfx::Rect icon_frame(rect); |
| 76 icon_frame.set_width(icon_size.width() + 2 * kPadding); |
| 77 icon_view_->SetBoundsRect(icon_frame); |
| 78 |
| 79 gfx::Rect edit_frame(rect); |
| 80 edit_frame.set_x(icon_frame.right()); |
| 81 edit_frame.set_width(rect.width() - icon_frame.width() - kPadding); |
| 82 search_box_->SetBoundsRect(edit_frame); |
| 83 } |
| 84 |
| 85 void SearchBoxView::UpdateModel() { |
| 86 // Temporarily remove from observer to ignore notifications caused by us. |
| 87 model_->RemoveObserver(this); |
| 88 model_->SetText(search_box_->text()); |
| 89 |
| 90 gfx::SelectionModel sel; |
| 91 search_box_->GetSelectionModel(&sel); |
| 92 model_->SetSelectionModel(sel); |
| 93 model_->AddObserver(this); |
| 94 } |
| 95 |
| 96 void SearchBoxView::NotifyQueryChanged() { |
| 97 DCHECK(delegate_); |
| 98 delegate_->QueryChanged(this); |
| 99 } |
| 100 |
| 101 void SearchBoxView::ContentsChanged(views::Textfield* sender, |
| 102 const string16& new_contents) { |
| 103 UpdateModel(); |
| 104 NotifyQueryChanged(); |
| 105 } |
| 106 |
| 107 bool SearchBoxView::HandleKeyEvent(views::Textfield* sender, |
| 108 const views::KeyEvent& key_event) { |
| 109 bool has_query = !search_box_->text().empty(); |
| 110 |
| 111 // Escape with non-empty query text clears the search box. |
| 112 if (has_query && key_event.key_code() == ui::VKEY_ESCAPE) { |
| 113 search_box_->SetText(string16()); |
| 114 // Updates model and fires query changed manually because SetText above |
| 115 // does not generate ContentsChanged notification. |
| 116 UpdateModel(); |
| 117 NotifyQueryChanged(); |
| 118 return true; |
| 119 } |
| 120 |
| 121 bool handled = false; |
| 122 if (has_query) { |
| 123 if (results_view_ && results_view_->visible()) |
| 124 handled = results_view_->OnKeyPressed(key_event); |
| 125 } else { |
| 126 if (grid_view_ && grid_view_->visible()) |
| 127 handled = grid_view_->OnKeyPressed(key_event); |
| 128 } |
| 129 |
| 130 return handled; |
| 131 } |
| 132 |
| 133 void SearchBoxView::IconChanged() { |
| 134 icon_view_->SetImage(model_->icon()); |
| 135 } |
| 136 |
| 137 void SearchBoxView::HintTextChanged() { |
| 138 search_box_->set_placeholder_text(model_->hint_text()); |
| 139 } |
| 140 |
| 141 void SearchBoxView::SelectionModelChanged() { |
| 142 search_box_->SelectSelectionModel(model_->selection_model()); |
| 143 } |
| 144 |
| 145 void SearchBoxView::TextChanged() { |
| 146 search_box_->SetText(model_->text()); |
| 147 } |
| 148 |
| 149 } // namespace app_list |
| 150 |
| OLD | NEW |