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

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

Issue 2952763002: SearchBoxView now enables/disables cursor based on user interaction. (Closed)
Patch Set: SearchBoxView now enables/disables cursor based on user interaction. Created 3 years, 6 months 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
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 "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 constexpr int kBackgroundBorderCornerRadius = 2; 55 constexpr int kBackgroundBorderCornerRadius = 2;
56 constexpr int kBackgroundBorderCornerRadiusFullscreen = 24; 56 constexpr int kBackgroundBorderCornerRadiusFullscreen = 24;
57 constexpr int kGoogleIconSize = 24; 57 constexpr int kGoogleIconSize = 24;
58 constexpr int kMicIconSize = 24; 58 constexpr int kMicIconSize = 24;
59 59
60 // Default color used when wallpaper customized color is not available for 60 // Default color used when wallpaper customized color is not available for
61 // searchbox, #000 at 87% opacity. 61 // searchbox, #000 at 87% opacity.
62 constexpr SkColor kDefaultSearchboxColor = 62 constexpr SkColor kDefaultSearchboxColor =
63 SkColorSetARGBMacro(0xDE, 0x00, 0x00, 0x00); 63 SkColorSetARGBMacro(0xDE, 0x00, 0x00, 0x00);
64 64
65 // Color used for placeholder text in zero query state.
66 constexpr SkColor kZeroQuerySearchboxColor =
67 SkColorSetARGBMacro(0x8A, 0x00, 0x00, 0x00);
68
65 // A background that paints a solid white rounded rect with a thin grey border. 69 // A background that paints a solid white rounded rect with a thin grey border.
66 class SearchBoxBackground : public views::Background { 70 class SearchBoxBackground : public views::Background {
67 public: 71 public:
68 SearchBoxBackground() 72 SearchBoxBackground()
69 : background_border_corner_radius_( 73 : background_border_corner_radius_(
70 features::IsFullscreenAppListEnabled() 74 features::IsFullscreenAppListEnabled()
71 ? kBackgroundBorderCornerRadiusFullscreen 75 ? kBackgroundBorderCornerRadiusFullscreen
72 : kBackgroundBorderCornerRadius) {} 76 : kBackgroundBorderCornerRadius) {}
73 ~SearchBoxBackground() override {} 77 ~SearchBoxBackground() override {}
74 78
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 view_delegate_(view_delegate), 144 view_delegate_(view_delegate),
141 model_(nullptr), 145 model_(nullptr),
142 content_container_(new views::View), 146 content_container_(new views::View),
143 google_icon_(nullptr), 147 google_icon_(nullptr),
144 back_button_(nullptr), 148 back_button_(nullptr),
145 speech_button_(nullptr), 149 speech_button_(nullptr),
146 search_box_(new views::Textfield), 150 search_box_(new views::Textfield),
147 contents_view_(nullptr), 151 contents_view_(nullptr),
148 app_list_view_(app_list_view), 152 app_list_view_(app_list_view),
149 focused_view_(FOCUS_SEARCH_BOX), 153 focused_view_(FOCUS_SEARCH_BOX),
150 is_fullscreen_app_list_enabled_(features::IsFullscreenAppListEnabled()) { 154 is_fullscreen_app_list_enabled_(features::IsFullscreenAppListEnabled()),
155 is_cursor_enabled_(false),
156 unit_tests_running_(false) {
151 SetLayoutManager(new views::FillLayout); 157 SetLayoutManager(new views::FillLayout);
152 SetPreferredSize(gfx::Size(is_fullscreen_app_list_enabled_ 158 SetPreferredSize(gfx::Size(is_fullscreen_app_list_enabled_
153 ? kPreferredWidthFullscreen 159 ? kPreferredWidthFullscreen
154 : kPreferredWidth, 160 : kPreferredWidth,
155 kPreferredHeight)); 161 kPreferredHeight));
156 AddChildView(content_container_); 162 AddChildView(content_container_);
157 163
158 SetShadow(GetShadowForZHeight(2)); 164 SetShadow(GetShadowForZHeight(2));
159 content_container_->SetBackground(base::MakeUnique<SearchBoxBackground>()); 165 content_container_->SetBackground(base::MakeUnique<SearchBoxBackground>());
160 166
(...skipping 14 matching lines...) Expand all
175 if (is_fullscreen_app_list_enabled_) { 181 if (is_fullscreen_app_list_enabled_) {
176 google_icon_ = new views::ImageView(); 182 google_icon_ = new views::ImageView();
177 google_icon_->SetImage(gfx::CreateVectorIcon( 183 google_icon_->SetImage(gfx::CreateVectorIcon(
178 kIcGoogleBlackIcon, kGoogleIconSize, kDefaultSearchboxColor)); 184 kIcGoogleBlackIcon, kGoogleIconSize, kDefaultSearchboxColor));
179 content_container_->AddChildView(google_icon_); 185 content_container_->AddChildView(google_icon_);
180 186
181 search_box_->set_placeholder_text_color(kDefaultSearchboxColor); 187 search_box_->set_placeholder_text_color(kDefaultSearchboxColor);
182 search_box_->set_placeholder_text_draw_flags( 188 search_box_->set_placeholder_text_draw_flags(
183 gfx::Canvas::TEXT_ALIGN_CENTER); 189 gfx::Canvas::TEXT_ALIGN_CENTER);
184 search_box_->SetFontList(search_box_->GetFontList().DeriveWithSizeDelta(2)); 190 search_box_->SetFontList(search_box_->GetFontList().DeriveWithSizeDelta(2));
191 search_box_->SetCursorEnabled(is_cursor_enabled_);
185 } else { 192 } else {
186 back_button_ = new SearchBoxImageButton(this); 193 back_button_ = new SearchBoxImageButton(this);
187 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 194 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
188 back_button_->SetImage( 195 back_button_->SetImage(
189 views::ImageButton::STATE_NORMAL, 196 views::ImageButton::STATE_NORMAL,
190 rb.GetImageSkiaNamed(IDR_APP_LIST_FOLDER_BACK_NORMAL)); 197 rb.GetImageSkiaNamed(IDR_APP_LIST_FOLDER_BACK_NORMAL));
191 back_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, 198 back_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
192 views::ImageButton::ALIGN_MIDDLE); 199 views::ImageButton::ALIGN_MIDDLE);
193 SetBackButtonLabel(false); 200 SetBackButtonLabel(false);
194 content_container_->AddChildView(back_button_); 201 content_container_->AddChildView(back_button_);
(...skipping 27 matching lines...) Expand all
222 return !search_box_->text().empty(); 229 return !search_box_->text().empty();
223 } 230 }
224 231
225 void SearchBoxView::ClearSearch() { 232 void SearchBoxView::ClearSearch() {
226 search_box_->SetText(base::string16()); 233 search_box_->SetText(base::string16());
227 view_delegate_->AutoLaunchCanceled(); 234 view_delegate_->AutoLaunchCanceled();
228 // Updates model and fires query changed manually because SetText() above 235 // Updates model and fires query changed manually because SetText() above
229 // does not generate ContentsChanged() notification. 236 // does not generate ContentsChanged() notification.
230 UpdateModel(); 237 UpdateModel();
231 NotifyQueryChanged(); 238 NotifyQueryChanged();
239 if (is_fullscreen_app_list_enabled_) {
240 SetPlaceholderTextAndEnableCursor(!search_box_->text().empty());
241 if (!unit_tests_running_)
242 app_list_view_->SetStateFromSearchBoxView(search_box_->text().empty());
243 }
232 } 244 }
233 245
234 void SearchBoxView::SetShadow(const gfx::ShadowValue& shadow) { 246 void SearchBoxView::SetShadow(const gfx::ShadowValue& shadow) {
235 if (is_fullscreen_app_list_enabled_) 247 if (is_fullscreen_app_list_enabled_)
236 return; 248 return;
237 249
238 SetBorder(base::MakeUnique<views::ShadowBorder>(shadow)); 250 SetBorder(base::MakeUnique<views::ShadowBorder>(shadow));
239 Layout(); 251 Layout();
240 } 252 }
241 253
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 if (!back_button_) 337 if (!back_button_)
326 return; 338 return;
327 339
328 base::string16 back_button_label(l10n_util::GetStringUTF16( 340 base::string16 back_button_label(l10n_util::GetStringUTF16(
329 folder ? IDS_APP_LIST_FOLDER_CLOSE_FOLDER_ACCESSIBILE_NAME 341 folder ? IDS_APP_LIST_FOLDER_CLOSE_FOLDER_ACCESSIBILE_NAME
330 : IDS_APP_LIST_BACK)); 342 : IDS_APP_LIST_BACK));
331 back_button_->SetAccessibleName(back_button_label); 343 back_button_->SetAccessibleName(back_button_label);
332 back_button_->SetTooltipText(back_button_label); 344 back_button_->SetTooltipText(back_button_label);
333 } 345 }
334 346
347 void SearchBoxView::SetPlaceholderTextAndEnableCursor(bool enable) {
348 search_box_->set_placeholder_text_draw_flags(
349 enable ? gfx::Canvas::TEXT_ALIGN_LEFT : gfx::Canvas::TEXT_ALIGN_CENTER);
350 search_box_->set_placeholder_text_color(enable ? kZeroQuerySearchboxColor
351 : kDefaultSearchboxColor);
352 is_cursor_enabled_ = enable;
353 search_box_->SetCursorEnabled(enable);
354 search_box_->SchedulePaint();
355 }
356
357 bool SearchBoxView::PassMouseEventForTesting(ui::MouseEvent& mouse_event) {
vadimt 2017/06/22 00:53:05 I assume you tried to find a way to inject a mouse
newcomer 2017/06/22 16:17:21 I couldn't find a way to do this without completel
358 return HandleMouseEvent(search_box_, mouse_event);
359 }
360
335 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent& event) { 361 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent& event) {
336 if (contents_view_) 362 if (contents_view_)
337 return contents_view_->OnMouseWheel(event); 363 return contents_view_->OnMouseWheel(event);
338 364
339 return false; 365 return false;
340 } 366 }
341 367
342 void SearchBoxView::OnEnabledChanged() { 368 void SearchBoxView::OnEnabledChanged() {
343 search_box_->SetEnabled(enabled()); 369 search_box_->SetEnabled(enabled());
344 if (speech_button_) 370 if (speech_button_)
345 speech_button_->SetEnabled(enabled()); 371 speech_button_->SetEnabled(enabled());
346 } 372 }
347 373
348 const char* SearchBoxView::GetClassName() const { 374 const char* SearchBoxView::GetClassName() const {
349 return "SearchBoxView"; 375 return "SearchBoxView";
350 } 376 }
351 377
378 void SearchBoxView::OnGestureEvent(ui::GestureEvent* event) {
379 if (!is_fullscreen_app_list_enabled_ || event->type() != ui::ET_GESTURE_TAP)
380 return;
381
382 // If the search box is empty and the cursor is not enabled, enable the cursor
383 // and shift the placeholder text left.
384 if (search_box_->text().empty() && !is_cursor_enabled_)
385 SetPlaceholderTextAndEnableCursor(true);
386 }
387
388 void SearchBoxView::OnMouseEvent(ui::MouseEvent* event) {
389 if (!is_fullscreen_app_list_enabled_ || event->type() != ui::ET_MOUSE_PRESSED)
390 return;
391
392 // If the search box is empty and the cursor is not enabled, enable the cursor
393 // and shift the placeholder text left.
394 if (search_box_->text().empty() && !is_cursor_enabled_)
395 SetPlaceholderTextAndEnableCursor(true);
396 }
397
352 void SearchBoxView::UpdateModel() { 398 void SearchBoxView::UpdateModel() {
353 // Temporarily remove from observer to ignore notifications caused by us. 399 // Temporarily remove from observer to ignore notifications caused by us.
354 model_->search_box()->RemoveObserver(this); 400 model_->search_box()->RemoveObserver(this);
355 model_->search_box()->Update(search_box_->text(), false); 401 model_->search_box()->Update(search_box_->text(), false);
356 model_->search_box()->SetSelectionModel(search_box_->GetSelectionModel()); 402 model_->search_box()->SetSelectionModel(search_box_->GetSelectionModel());
357 model_->search_box()->AddObserver(this); 403 model_->search_box()->AddObserver(this);
358 } 404 }
359 405
360 void SearchBoxView::NotifyQueryChanged() { 406 void SearchBoxView::NotifyQueryChanged() {
361 DCHECK(delegate_); 407 DCHECK(delegate_);
362 delegate_->QueryChanged(this); 408 delegate_->QueryChanged(this);
363 } 409 }
364 410
365 void SearchBoxView::ContentsChanged(views::Textfield* sender, 411 void SearchBoxView::ContentsChanged(views::Textfield* sender,
366 const base::string16& new_contents) { 412 const base::string16& new_contents) {
367 UpdateModel(); 413 UpdateModel();
368 view_delegate_->AutoLaunchCanceled(); 414 view_delegate_->AutoLaunchCanceled();
369 NotifyQueryChanged(); 415 NotifyQueryChanged();
370 if (is_fullscreen_app_list_enabled_) 416 if (is_fullscreen_app_list_enabled_) {
371 app_list_view_->SetStateFromSearchBoxView(search_box_->text().empty()); 417 SetPlaceholderTextAndEnableCursor(!search_box_->text().empty());
418 if (!unit_tests_running_)
419 app_list_view_->SetStateFromSearchBoxView(search_box_->text().empty());
420 }
372 } 421 }
373 422
374 bool SearchBoxView::HandleKeyEvent(views::Textfield* sender, 423 bool SearchBoxView::HandleKeyEvent(views::Textfield* sender,
375 const ui::KeyEvent& key_event) { 424 const ui::KeyEvent& key_event) {
376 if (key_event.type() == ui::ET_KEY_PRESSED) { 425 if (key_event.type() == ui::ET_KEY_PRESSED) {
377 if (key_event.key_code() == ui::VKEY_TAB && 426 if (key_event.key_code() == ui::VKEY_TAB &&
378 focused_view_ != FOCUS_CONTENTS_VIEW && 427 focused_view_ != FOCUS_CONTENTS_VIEW &&
379 MoveTabFocus(key_event.IsShiftDown())) 428 MoveTabFocus(key_event.IsShiftDown()))
380 return true; 429 return true;
381 430
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 speech_button_->OnKeyReleased(key_event)) 464 speech_button_->OnKeyReleased(key_event))
416 return true; 465 return true;
417 466
418 return contents_view_ && contents_view_->visible() && 467 return contents_view_ && contents_view_->visible() &&
419 contents_view_->OnKeyReleased(key_event); 468 contents_view_->OnKeyReleased(key_event);
420 } 469 }
421 470
422 return false; 471 return false;
423 } 472 }
424 473
474 bool SearchBoxView::HandleMouseEvent(views::Textfield* sender,
475 const ui::MouseEvent& mouse_event) {
476 if (!is_fullscreen_app_list_enabled_)
477 return false;
478
479 if (search_box_->text().empty() && !is_cursor_enabled_)
480 SetPlaceholderTextAndEnableCursor(true);
481
482 return true;
483 }
484
485 void SearchBoxView::NotifyOfGestureEvent() {
486 if (!is_fullscreen_app_list_enabled_)
487 return;
488
489 if (search_box_->text().empty() && !is_cursor_enabled_)
490 SetPlaceholderTextAndEnableCursor(true);
491 }
492
425 void SearchBoxView::ButtonPressed(views::Button* sender, 493 void SearchBoxView::ButtonPressed(views::Button* sender,
426 const ui::Event& event) { 494 const ui::Event& event) {
427 if (back_button_ && sender == back_button_) 495 if (back_button_ && sender == back_button_)
428 delegate_->BackButtonPressed(); 496 delegate_->BackButtonPressed();
429 else if (speech_button_ && sender == speech_button_) 497 else if (speech_button_ && sender == speech_button_)
430 view_delegate_->StartSpeechRecognition(); 498 view_delegate_->StartSpeechRecognition();
431 else 499 else
432 NOTREACHED(); 500 NOTREACHED();
433 } 501 }
434 502
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 NotifyQueryChanged(); 557 NotifyQueryChanged();
490 } 558 }
491 559
492 void SearchBoxView::OnSpeechRecognitionStateChanged( 560 void SearchBoxView::OnSpeechRecognitionStateChanged(
493 SpeechRecognitionState new_state) { 561 SpeechRecognitionState new_state) {
494 SpeechRecognitionButtonPropChanged(); 562 SpeechRecognitionButtonPropChanged();
495 SchedulePaint(); 563 SchedulePaint();
496 } 564 }
497 565
498 } // namespace app_list 566 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698