Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(326)

Side by Side Diff: ui/app_list/views/search_box_view.cc

Issue 69813002: Adds the speech recognition button to the app-list searchbox. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/app_list/views/search_box_view.h" 5 #include "ui/app_list/views/search_box_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "grit/ui_resources.h" 9 #include "grit/ui_resources.h"
10 #include "ui/app_list/app_list_model.h" 10 #include "ui/app_list/app_list_model.h"
11 #include "ui/app_list/app_list_view_delegate.h"
11 #include "ui/app_list/search_box_model.h" 12 #include "ui/app_list/search_box_model.h"
12 #include "ui/app_list/views/app_list_menu_views.h" 13 #include "ui/app_list/views/app_list_menu_views.h"
13 #include "ui/app_list/views/search_box_view_delegate.h" 14 #include "ui/app_list/views/search_box_view_delegate.h"
15 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h" 16 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/events/event.h" 17 #include "ui/events/event.h"
18 #include "ui/views/controls/button/image_button.h"
16 #include "ui/views/controls/button/menu_button.h" 19 #include "ui/views/controls/button/menu_button.h"
17 #include "ui/views/controls/image_view.h" 20 #include "ui/views/controls/image_view.h"
18 #include "ui/views/controls/textfield/textfield.h" 21 #include "ui/views/controls/textfield/textfield.h"
19 22
20 namespace app_list { 23 namespace app_list {
21 24
22 namespace { 25 namespace {
23 26
24 const int kPadding = 14; 27 const int kPadding = 14;
25 const int kIconDimension = 32; 28 const int kIconDimension = 32;
(...skipping 11 matching lines...) Expand all
37 40
38 } // namespace 41 } // namespace
39 42
40 SearchBoxView::SearchBoxView(SearchBoxViewDelegate* delegate, 43 SearchBoxView::SearchBoxView(SearchBoxViewDelegate* delegate,
41 AppListViewDelegate* view_delegate, 44 AppListViewDelegate* view_delegate,
42 AppListModel* model) 45 AppListModel* model)
43 : delegate_(delegate), 46 : delegate_(delegate),
44 view_delegate_(view_delegate), 47 view_delegate_(view_delegate),
45 model_(model), 48 model_(model),
46 icon_view_(new views::ImageView), 49 icon_view_(new views::ImageView),
50 right_button_(NULL),
47 search_box_(new views::Textfield), 51 search_box_(new views::Textfield),
48 contents_view_(NULL) { 52 contents_view_(NULL) {
49 DCHECK(model_); 53 DCHECK(model_);
50 AddChildView(icon_view_); 54 AddChildView(icon_view_);
51 55
52 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 56 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
53 57
54 #if !defined(OS_CHROMEOS) 58 #if !defined(OS_CHROMEOS)
55 menu_button_ = new views::MenuButton(NULL, base::string16(), this, false); 59 menu_button_ = new views::MenuButton(NULL, base::string16(), this, false);
56 menu_button_->set_border(NULL); 60 menu_button_->set_border(NULL);
57 menu_button_->SetIcon(*rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_NORMAL)); 61 menu_button_->SetIcon(*rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_NORMAL));
58 menu_button_->SetHoverIcon(*rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_HOVER)); 62 menu_button_->SetHoverIcon(*rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_HOVER));
59 menu_button_->SetPushedIcon(*rb.GetImageSkiaNamed( 63 menu_button_->SetPushedIcon(*rb.GetImageSkiaNamed(
60 IDR_APP_LIST_TOOLS_PRESSED)); 64 IDR_APP_LIST_TOOLS_PRESSED));
61 AddChildView(menu_button_); 65 AddChildView(menu_button_);
62 #endif 66 #endif
63 67
64 search_box_->RemoveBorder(); 68 search_box_->RemoveBorder();
65 search_box_->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont)); 69 search_box_->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont));
66 search_box_->set_placeholder_text_color(kHintTextColor); 70 search_box_->set_placeholder_text_color(kHintTextColor);
67 search_box_->SetController(this); 71 search_box_->SetController(this);
68 AddChildView(search_box_); 72 AddChildView(search_box_);
69 73
70 model_->search_box()->AddObserver(this); 74 model_->search_box()->AddObserver(this);
71 IconChanged(); 75 IconChanged();
76 RightButtonPropChanged();
72 HintTextChanged(); 77 HintTextChanged();
73 } 78 }
74 79
75 SearchBoxView::~SearchBoxView() { 80 SearchBoxView::~SearchBoxView() {
76 model_->search_box()->RemoveObserver(this); 81 model_->search_box()->RemoveObserver(this);
77 } 82 }
78 83
79 bool SearchBoxView::HasSearch() const { 84 bool SearchBoxView::HasSearch() const {
80 return !search_box_->text().empty(); 85 return !search_box_->text().empty();
81 } 86 }
(...skipping 16 matching lines...) Expand all
98 103
99 void SearchBoxView::Layout() { 104 void SearchBoxView::Layout() {
100 gfx::Rect rect(GetContentsBounds()); 105 gfx::Rect rect(GetContentsBounds());
101 if (rect.IsEmpty()) 106 if (rect.IsEmpty())
102 return; 107 return;
103 108
104 gfx::Rect icon_frame(rect); 109 gfx::Rect icon_frame(rect);
105 icon_frame.set_width(kIconDimension + 2 * kPadding); 110 icon_frame.set_width(kIconDimension + 2 * kPadding);
106 icon_view_->SetBoundsRect(icon_frame); 111 icon_view_->SetBoundsRect(icon_frame);
107 112
113 // Empty if right icon doesn't exist.
114 gfx::Rect right_button_frame;
115 if (right_button_) {
116 right_button_frame = icon_frame;
117 right_button_frame.set_x(rect.right() - icon_frame.width());
118 gfx::Size button_size = right_button_->GetPreferredSize();
119 gfx::Point button_origin = right_button_frame.CenterPoint();
120 button_origin.Offset(-button_size.width() / 2, -button_size.height() / 2);
121 right_button_->SetBoundsRect(gfx::Rect(button_origin, button_size));
122 }
123
108 gfx::Rect menu_button_frame(rect); 124 gfx::Rect menu_button_frame(rect);
109 #if !defined(OS_CHROMEOS) 125 #if !defined(OS_CHROMEOS)
110 menu_button_frame.set_width(kMenuButtonDimension); 126 menu_button_frame.set_width(kMenuButtonDimension);
111 menu_button_frame.set_x(rect.right() - menu_button_frame.width() - kPadding); 127 menu_button_frame.set_x(rect.right() - menu_button_frame.width() - kPadding);
112 menu_button_frame.ClampToCenteredSize(gfx::Size(menu_button_frame.width(), 128 menu_button_frame.ClampToCenteredSize(gfx::Size(menu_button_frame.width(),
113 kMenuButtonDimension)); 129 kMenuButtonDimension));
114 menu_button_->SetBoundsRect(menu_button_frame); 130 menu_button_->SetBoundsRect(menu_button_frame);
115 #else 131 #else
116 menu_button_frame.set_width(0); 132 menu_button_frame.set_width(0);
117 #endif 133 #endif
118 134
119 gfx::Rect edit_frame(rect); 135 gfx::Rect edit_frame(rect);
120 edit_frame.set_x(icon_frame.right()); 136 edit_frame.set_x(icon_frame.right());
121 edit_frame.set_width( 137 int edit_frame_width =
122 rect.width() - icon_frame.width() - kPadding - menu_button_frame.width()); 138 rect.width() - icon_frame.width() - kPadding - menu_button_frame.width();
139 if (!right_button_frame.IsEmpty())
140 edit_frame_width -= right_button_frame.width() + kPadding;
141 edit_frame.set_width(edit_frame_width);
123 edit_frame.ClampToCenteredSize( 142 edit_frame.ClampToCenteredSize(
124 gfx::Size(edit_frame.width(), search_box_->GetPreferredSize().height())); 143 gfx::Size(edit_frame.width(), search_box_->GetPreferredSize().height()));
125 search_box_->SetBoundsRect(edit_frame); 144 search_box_->SetBoundsRect(edit_frame);
126 } 145 }
127 146
128 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent& event) { 147 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent& event) {
129 if (contents_view_) 148 if (contents_view_)
130 return contents_view_->OnMouseWheel(event); 149 return contents_view_->OnMouseWheel(event);
131 150
132 return false; 151 return false;
(...skipping 20 matching lines...) Expand all
153 172
154 bool SearchBoxView::HandleKeyEvent(views::Textfield* sender, 173 bool SearchBoxView::HandleKeyEvent(views::Textfield* sender,
155 const ui::KeyEvent& key_event) { 174 const ui::KeyEvent& key_event) {
156 bool handled = false; 175 bool handled = false;
157 if (contents_view_ && contents_view_->visible()) 176 if (contents_view_ && contents_view_->visible())
158 handled = contents_view_->OnKeyPressed(key_event); 177 handled = contents_view_->OnKeyPressed(key_event);
159 178
160 return handled; 179 return handled;
161 } 180 }
162 181
182 void SearchBoxView::ButtonPressed(views::Button* sender,
183 const ui::Event& event) {
184 DCHECK(!right_button_ && sender == right_button_);
185 view_delegate_->OnRightButtonClicked();
186 }
187
163 void SearchBoxView::OnMenuButtonClicked(View* source, const gfx::Point& point) { 188 void SearchBoxView::OnMenuButtonClicked(View* source, const gfx::Point& point) {
164 if (!menu_) 189 if (!menu_)
165 menu_.reset(new AppListMenuViews(view_delegate_)); 190 menu_.reset(new AppListMenuViews(view_delegate_));
166 191
167 const gfx::Point menu_location = 192 const gfx::Point menu_location =
168 menu_button_->GetBoundsInScreen().bottom_right() + 193 menu_button_->GetBoundsInScreen().bottom_right() +
169 gfx::Vector2d(kMenuXOffsetFromButton, kMenuYOffsetFromButton); 194 gfx::Vector2d(kMenuXOffsetFromButton, kMenuYOffsetFromButton);
170 menu_->RunMenuAt(menu_button_, menu_location); 195 menu_->RunMenuAt(menu_button_, menu_location);
171 } 196 }
172 197
173 void SearchBoxView::IconChanged() { 198 void SearchBoxView::IconChanged() {
174 icon_view_->SetImage(model_->search_box()->icon()); 199 icon_view_->SetImage(model_->search_box()->icon());
175 } 200 }
176 201
202 void SearchBoxView::RightButtonPropChanged() {
203 const SearchBoxModel::RightButtonProperty* right_button_prop =
204 model_->search_box()->right_button();
205 if (right_button_prop) {
206 if (!right_button_) {
207 right_button_ = new views::ToggleImageButton(this);
208 AddChildView(right_button_);
209 }
210 right_button_->SetImage(views::Button::STATE_NORMAL,
211 &right_button_prop->icon);
212 right_button_->SetToggledImage(views::Button::STATE_NORMAL,
213 &right_button_prop->toggled_icon);
214 right_button_->SetTooltipText(right_button_prop->tooltip);
215 right_button_->SetToggledTooltipText(right_button_prop->toggled_tooltip);
216 } else {
217 if (right_button_) {
218 // Deleting a view will detach it from its parent.
219 delete right_button_;
220 right_button_ = NULL;
221 }
222 }
223 }
224
225 void SearchBoxView::RightButtonStateChanged(bool toggled) {
226 if (right_button_)
227 right_button_->SetToggled(toggled);
228 }
229
177 void SearchBoxView::HintTextChanged() { 230 void SearchBoxView::HintTextChanged() {
178 search_box_->set_placeholder_text(model_->search_box()->hint_text()); 231 search_box_->set_placeholder_text(model_->search_box()->hint_text());
179 } 232 }
180 233
181 void SearchBoxView::SelectionModelChanged() { 234 void SearchBoxView::SelectionModelChanged() {
182 search_box_->SelectSelectionModel(model_->search_box()->selection_model()); 235 search_box_->SelectSelectionModel(model_->search_box()->selection_model());
183 } 236 }
184 237
185 void SearchBoxView::TextChanged() { 238 void SearchBoxView::TextChanged() {
186 search_box_->SetText(model_->search_box()->text()); 239 search_box_->SetText(model_->search_box()->text());
187 NotifyQueryChanged(); 240 NotifyQueryChanged();
188 } 241 }
189 242
190 } // namespace app_list 243 } // namespace app_list
OLDNEW
« ui/app_list/search_box_model.cc ('K') | « ui/app_list/views/search_box_view.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698