OLD | NEW |
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 "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 | 46 |
47 const SkColor kHintTextColor = SkColorSetRGB(0xA0, 0xA0, 0xA0); | 47 const SkColor kHintTextColor = SkColorSetRGB(0xA0, 0xA0, 0xA0); |
48 | 48 |
49 // Menu offset relative to the bottom-right corner of the menu button. | 49 // Menu offset relative to the bottom-right corner of the menu button. |
50 const int kMenuYOffsetFromButton = -4; | 50 const int kMenuYOffsetFromButton = -4; |
51 const int kMenuXOffsetFromButton = -7; | 51 const int kMenuXOffsetFromButton = -7; |
52 | 52 |
53 const int kBackgroundBorderCornerRadius = 2; | 53 const int kBackgroundBorderCornerRadius = 2; |
54 | 54 |
55 // A background that paints a solid white rounded rect with a thin grey border. | 55 // A background that paints a solid white rounded rect with a thin grey border. |
56 class ExperimentalSearchBoxBackground : public views::Background { | 56 class SearchBoxBackground : public views::Background { |
57 public: | 57 public: |
58 ExperimentalSearchBoxBackground() {} | 58 SearchBoxBackground() {} |
59 ~ExperimentalSearchBoxBackground() override {} | 59 ~SearchBoxBackground() override {} |
60 | 60 |
61 private: | 61 private: |
62 // views::Background overrides: | 62 // views::Background overrides: |
63 void Paint(gfx::Canvas* canvas, views::View* view) const override { | 63 void Paint(gfx::Canvas* canvas, views::View* view) const override { |
64 gfx::Rect bounds = view->GetContentsBounds(); | 64 gfx::Rect bounds = view->GetContentsBounds(); |
65 | 65 |
66 SkPaint paint; | 66 SkPaint paint; |
67 paint.setFlags(SkPaint::kAntiAlias_Flag); | 67 paint.setFlags(SkPaint::kAntiAlias_Flag); |
68 paint.setColor(kSearchBoxBackground); | 68 paint.setColor(kSearchBoxBackground); |
69 canvas->DrawRoundRect(bounds, kBackgroundBorderCornerRadius, paint); | 69 canvas->DrawRoundRect(bounds, kBackgroundBorderCornerRadius, paint); |
70 } | 70 } |
71 | 71 |
72 DISALLOW_COPY_AND_ASSIGN(ExperimentalSearchBoxBackground); | 72 DISALLOW_COPY_AND_ASSIGN(SearchBoxBackground); |
73 }; | 73 }; |
74 | 74 |
75 } // namespace | 75 } // namespace |
76 | 76 |
77 // To paint grey background on mic and back buttons | 77 // To paint grey background on mic and back buttons |
78 class SearchBoxImageButton : public views::ImageButton { | 78 class SearchBoxImageButton : public views::ImageButton { |
79 public: | 79 public: |
80 explicit SearchBoxImageButton(views::ButtonListener* listener) | 80 explicit SearchBoxImageButton(views::ButtonListener* listener) |
81 : ImageButton(listener), selected_(false) {} | 81 : ImageButton(listener), selected_(false) {} |
82 ~SearchBoxImageButton() override {} | 82 ~SearchBoxImageButton() override {} |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 114 |
115 DISALLOW_COPY_AND_ASSIGN(SearchBoxImageButton); | 115 DISALLOW_COPY_AND_ASSIGN(SearchBoxImageButton); |
116 }; | 116 }; |
117 | 117 |
118 SearchBoxView::SearchBoxView(SearchBoxViewDelegate* delegate, | 118 SearchBoxView::SearchBoxView(SearchBoxViewDelegate* delegate, |
119 AppListViewDelegate* view_delegate) | 119 AppListViewDelegate* view_delegate) |
120 : delegate_(delegate), | 120 : delegate_(delegate), |
121 view_delegate_(view_delegate), | 121 view_delegate_(view_delegate), |
122 model_(NULL), | 122 model_(NULL), |
123 content_container_(new views::View), | 123 content_container_(new views::View), |
124 icon_view_(NULL), | |
125 back_button_(NULL), | 124 back_button_(NULL), |
126 speech_button_(NULL), | 125 speech_button_(NULL), |
127 menu_button_(NULL), | 126 menu_button_(NULL), |
128 search_box_(new views::Textfield), | 127 search_box_(new views::Textfield), |
129 contents_view_(NULL), | 128 contents_view_(NULL), |
130 focused_view_(FOCUS_SEARCH_BOX) { | 129 focused_view_(FOCUS_SEARCH_BOX) { |
131 SetLayoutManager(new views::FillLayout); | 130 SetLayoutManager(new views::FillLayout); |
132 AddChildView(content_container_); | 131 AddChildView(content_container_); |
133 | 132 |
134 if (switches::IsExperimentalAppListEnabled()) { | 133 SetShadow(GetShadowForZHeight(2)); |
135 SetShadow(GetShadowForZHeight(2)); | 134 back_button_ = new SearchBoxImageButton(this); |
136 back_button_ = new SearchBoxImageButton(this); | 135 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
137 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 136 back_button_->SetImage(views::ImageButton::STATE_NORMAL, |
138 back_button_->SetImage( | 137 rb.GetImageSkiaNamed(IDR_APP_LIST_FOLDER_BACK_NORMAL)); |
139 views::ImageButton::STATE_NORMAL, | 138 back_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, |
140 rb.GetImageSkiaNamed(IDR_APP_LIST_FOLDER_BACK_NORMAL)); | 139 views::ImageButton::ALIGN_MIDDLE); |
141 back_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, | 140 SetBackButtonLabel(false); |
142 views::ImageButton::ALIGN_MIDDLE); | 141 content_container_->AddChildView(back_button_); |
143 SetBackButtonLabel(false); | 142 content_container_->set_background(new SearchBoxBackground()); |
144 content_container_->AddChildView(back_button_); | |
145 | |
146 content_container_->set_background(new ExperimentalSearchBoxBackground()); | |
147 } else { | |
148 set_background( | |
149 views::Background::CreateSolidBackground(kSearchBoxBackground)); | |
150 SetBorder( | |
151 views::Border::CreateSolidSidedBorder(0, 0, 1, 0, kTopSeparatorColor)); | |
152 icon_view_ = new views::ImageView; | |
153 content_container_->AddChildView(icon_view_); | |
154 } | |
155 | 143 |
156 views::BoxLayout* layout = | 144 views::BoxLayout* layout = |
157 new views::BoxLayout(views::BoxLayout::kHorizontal, kPadding, 0, | 145 new views::BoxLayout(views::BoxLayout::kHorizontal, kPadding, 0, |
158 kInnerPadding - views::Textfield::kTextPadding); | 146 kInnerPadding - views::Textfield::kTextPadding); |
159 content_container_->SetLayoutManager(layout); | 147 content_container_->SetLayoutManager(layout); |
160 layout->set_cross_axis_alignment( | 148 layout->set_cross_axis_alignment( |
161 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); | 149 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); |
162 layout->set_minimum_cross_axis_size(kPreferredHeight); | 150 layout->set_minimum_cross_axis_size(kPreferredHeight); |
163 | 151 |
164 search_box_->SetBorder(views::Border::NullBorder()); | 152 search_box_->SetBorder(views::Border::NullBorder()); |
165 search_box_->SetTextColor(kSearchTextColor); | 153 search_box_->SetTextColor(kSearchTextColor); |
166 search_box_->SetBackgroundColor(kSearchBoxBackground); | 154 search_box_->SetBackgroundColor(kSearchBoxBackground); |
167 search_box_->set_placeholder_text_color(kHintTextColor); | 155 search_box_->set_placeholder_text_color(kHintTextColor); |
168 search_box_->set_controller(this); | 156 search_box_->set_controller(this); |
169 search_box_->SetTextInputType(ui::TEXT_INPUT_TYPE_SEARCH); | 157 search_box_->SetTextInputType(ui::TEXT_INPUT_TYPE_SEARCH); |
170 search_box_->SetTextInputFlags(ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF); | 158 search_box_->SetTextInputFlags(ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF); |
171 content_container_->AddChildView(search_box_); | 159 content_container_->AddChildView(search_box_); |
172 layout->SetFlexForView(search_box_, 1); | 160 layout->SetFlexForView(search_box_, 1); |
173 | 161 |
174 #if !defined(OS_CHROMEOS) | 162 #if !defined(OS_CHROMEOS) |
175 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 163 // TODO(mgiuca): Remove the menu (this code doesn't run on non-Chrome-OS so |
| 164 // the menu will never be created). https://crbug.com/600915. |
176 menu_button_ = new views::MenuButton(base::string16(), this, false); | 165 menu_button_ = new views::MenuButton(base::string16(), this, false); |
177 menu_button_->SetBorder(views::Border::NullBorder()); | 166 menu_button_->SetBorder(views::Border::NullBorder()); |
178 menu_button_->SetImage(views::Button::STATE_NORMAL, | 167 menu_button_->SetImage(views::Button::STATE_NORMAL, |
179 *rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_NORMAL)); | 168 *rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_NORMAL)); |
180 menu_button_->SetImage(views::Button::STATE_HOVERED, | 169 menu_button_->SetImage(views::Button::STATE_HOVERED, |
181 *rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_HOVER)); | 170 *rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_HOVER)); |
182 menu_button_->SetImage(views::Button::STATE_PRESSED, | 171 menu_button_->SetImage(views::Button::STATE_PRESSED, |
183 *rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_PRESSED)); | 172 *rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_PRESSED)); |
184 content_container_->AddChildView(menu_button_); | 173 content_container_->AddChildView(menu_button_); |
185 #endif | 174 #endif |
186 | 175 |
187 view_delegate_->GetSpeechUI()->AddObserver(this); | 176 view_delegate_->GetSpeechUI()->AddObserver(this); |
188 ModelChanged(); | 177 ModelChanged(); |
189 } | 178 } |
190 | 179 |
191 SearchBoxView::~SearchBoxView() { | 180 SearchBoxView::~SearchBoxView() { |
192 view_delegate_->GetSpeechUI()->RemoveObserver(this); | 181 view_delegate_->GetSpeechUI()->RemoveObserver(this); |
193 model_->search_box()->RemoveObserver(this); | 182 model_->search_box()->RemoveObserver(this); |
194 } | 183 } |
195 | 184 |
196 void SearchBoxView::ModelChanged() { | 185 void SearchBoxView::ModelChanged() { |
197 if (model_) | 186 if (model_) |
198 model_->search_box()->RemoveObserver(this); | 187 model_->search_box()->RemoveObserver(this); |
199 | 188 |
200 model_ = view_delegate_->GetModel(); | 189 model_ = view_delegate_->GetModel(); |
201 DCHECK(model_); | 190 DCHECK(model_); |
202 model_->search_box()->AddObserver(this); | 191 model_->search_box()->AddObserver(this); |
203 IconChanged(); | |
204 SpeechRecognitionButtonPropChanged(); | 192 SpeechRecognitionButtonPropChanged(); |
205 HintTextChanged(); | 193 HintTextChanged(); |
206 } | 194 } |
207 | 195 |
208 bool SearchBoxView::HasSearch() const { | 196 bool SearchBoxView::HasSearch() const { |
209 return !search_box_->text().empty(); | 197 return !search_box_->text().empty(); |
210 } | 198 } |
211 | 199 |
212 void SearchBoxView::ClearSearch() { | 200 void SearchBoxView::ClearSearch() { |
213 search_box_->SetText(base::string16()); | 201 search_box_->SetText(base::string16()); |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 const ui::Event* event) { | 413 const ui::Event* event) { |
426 if (!menu_) | 414 if (!menu_) |
427 menu_.reset(new AppListMenuViews(view_delegate_)); | 415 menu_.reset(new AppListMenuViews(view_delegate_)); |
428 | 416 |
429 const gfx::Point menu_location = | 417 const gfx::Point menu_location = |
430 menu_button_->GetBoundsInScreen().bottom_right() + | 418 menu_button_->GetBoundsInScreen().bottom_right() + |
431 gfx::Vector2d(kMenuXOffsetFromButton, kMenuYOffsetFromButton); | 419 gfx::Vector2d(kMenuXOffsetFromButton, kMenuYOffsetFromButton); |
432 menu_->RunMenuAt(menu_button_, menu_location); | 420 menu_->RunMenuAt(menu_button_, menu_location); |
433 } | 421 } |
434 | 422 |
435 void SearchBoxView::IconChanged() { | |
436 if (icon_view_) | |
437 icon_view_->SetImage(model_->search_box()->icon()); | |
438 } | |
439 | |
440 void SearchBoxView::SpeechRecognitionButtonPropChanged() { | 423 void SearchBoxView::SpeechRecognitionButtonPropChanged() { |
441 const SearchBoxModel::SpeechButtonProperty* speech_button_prop = | 424 const SearchBoxModel::SpeechButtonProperty* speech_button_prop = |
442 model_->search_box()->speech_button(); | 425 model_->search_box()->speech_button(); |
443 if (speech_button_prop) { | 426 if (speech_button_prop) { |
444 if (!speech_button_) { | 427 if (!speech_button_) { |
445 speech_button_ = new SearchBoxImageButton(this); | 428 speech_button_ = new SearchBoxImageButton(this); |
446 content_container_->AddChildView(speech_button_); | 429 content_container_->AddChildView(speech_button_); |
447 } | 430 } |
448 | 431 |
449 speech_button_->SetAccessibleName(speech_button_prop->accessible_name); | 432 speech_button_->SetAccessibleName(speech_button_prop->accessible_name); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 NotifyQueryChanged(); | 465 NotifyQueryChanged(); |
483 } | 466 } |
484 | 467 |
485 void SearchBoxView::OnSpeechRecognitionStateChanged( | 468 void SearchBoxView::OnSpeechRecognitionStateChanged( |
486 SpeechRecognitionState new_state) { | 469 SpeechRecognitionState new_state) { |
487 SpeechRecognitionButtonPropChanged(); | 470 SpeechRecognitionButtonPropChanged(); |
488 SchedulePaint(); | 471 SchedulePaint(); |
489 } | 472 } |
490 | 473 |
491 } // namespace app_list | 474 } // namespace app_list |
OLD | NEW |