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

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) {
151 SetLayoutManager(new views::FillLayout); 156 SetLayoutManager(new views::FillLayout);
152 SetPreferredSize(gfx::Size(is_fullscreen_app_list_enabled_ 157 SetPreferredSize(gfx::Size(is_fullscreen_app_list_enabled_
153 ? kPreferredWidthFullscreen 158 ? kPreferredWidthFullscreen
154 : kPreferredWidth, 159 : kPreferredWidth,
155 kPreferredHeight)); 160 kPreferredHeight));
156 AddChildView(content_container_); 161 AddChildView(content_container_);
157 162
158 SetShadow(GetShadowForZHeight(2)); 163 SetShadow(GetShadowForZHeight(2));
159 content_container_->SetBackground(base::MakeUnique<SearchBoxBackground>()); 164 content_container_->SetBackground(base::MakeUnique<SearchBoxBackground>());
160 165
(...skipping 14 matching lines...) Expand all
175 if (is_fullscreen_app_list_enabled_) { 180 if (is_fullscreen_app_list_enabled_) {
176 google_icon_ = new views::ImageView(); 181 google_icon_ = new views::ImageView();
177 google_icon_->SetImage(gfx::CreateVectorIcon( 182 google_icon_->SetImage(gfx::CreateVectorIcon(
178 kIcGoogleBlackIcon, kGoogleIconSize, kDefaultSearchboxColor)); 183 kIcGoogleBlackIcon, kGoogleIconSize, kDefaultSearchboxColor));
179 content_container_->AddChildView(google_icon_); 184 content_container_->AddChildView(google_icon_);
180 185
181 search_box_->set_placeholder_text_color(kDefaultSearchboxColor); 186 search_box_->set_placeholder_text_color(kDefaultSearchboxColor);
182 search_box_->set_placeholder_text_draw_flags( 187 search_box_->set_placeholder_text_draw_flags(
183 gfx::Canvas::TEXT_ALIGN_CENTER); 188 gfx::Canvas::TEXT_ALIGN_CENTER);
184 search_box_->SetFontList(search_box_->GetFontList().DeriveWithSizeDelta(2)); 189 search_box_->SetFontList(search_box_->GetFontList().DeriveWithSizeDelta(2));
190 search_box_->SetCursorEnabled(is_cursor_enabled_);
185 } else { 191 } else {
186 back_button_ = new SearchBoxImageButton(this); 192 back_button_ = new SearchBoxImageButton(this);
187 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 193 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
188 back_button_->SetImage( 194 back_button_->SetImage(
189 views::ImageButton::STATE_NORMAL, 195 views::ImageButton::STATE_NORMAL,
190 rb.GetImageSkiaNamed(IDR_APP_LIST_FOLDER_BACK_NORMAL)); 196 rb.GetImageSkiaNamed(IDR_APP_LIST_FOLDER_BACK_NORMAL));
191 back_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, 197 back_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
192 views::ImageButton::ALIGN_MIDDLE); 198 views::ImageButton::ALIGN_MIDDLE);
193 SetBackButtonLabel(false); 199 SetBackButtonLabel(false);
194 content_container_->AddChildView(back_button_); 200 content_container_->AddChildView(back_button_);
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 if (!back_button_) 331 if (!back_button_)
326 return; 332 return;
327 333
328 base::string16 back_button_label(l10n_util::GetStringUTF16( 334 base::string16 back_button_label(l10n_util::GetStringUTF16(
329 folder ? IDS_APP_LIST_FOLDER_CLOSE_FOLDER_ACCESSIBILE_NAME 335 folder ? IDS_APP_LIST_FOLDER_CLOSE_FOLDER_ACCESSIBILE_NAME
330 : IDS_APP_LIST_BACK)); 336 : IDS_APP_LIST_BACK));
331 back_button_->SetAccessibleName(back_button_label); 337 back_button_->SetAccessibleName(back_button_label);
332 back_button_->SetTooltipText(back_button_label); 338 back_button_->SetTooltipText(back_button_label);
333 } 339 }
334 340
341 void SearchBoxView::SetPlaceholderTextAndEnableCursor(bool enable) {
342 search_box_->set_placeholder_text_draw_flags(
343 enable ? gfx::Canvas::TEXT_ALIGN_LEFT : gfx::Canvas::TEXT_ALIGN_CENTER);
344 search_box_->set_placeholder_text_color(enable ? kZeroQuerySearchboxColor
345 : kDefaultSearchboxColor);
346 is_cursor_enabled_ = enable;
347 search_box_->SetCursorEnabled(enable);
348 search_box_->SchedulePaint();
349 }
350
351 bool SearchBoxView::PassMouseEventForTesting(ui::MouseEvent& mouse_event) {
352 return HandleMouseEvent(search_box_, mouse_event);
353 }
354
335 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent& event) { 355 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent& event) {
336 if (contents_view_) 356 if (contents_view_)
337 return contents_view_->OnMouseWheel(event); 357 return contents_view_->OnMouseWheel(event);
338 358
339 return false; 359 return false;
340 } 360 }
341 361
342 void SearchBoxView::OnEnabledChanged() { 362 void SearchBoxView::OnEnabledChanged() {
343 search_box_->SetEnabled(enabled()); 363 search_box_->SetEnabled(enabled());
344 if (speech_button_) 364 if (speech_button_)
345 speech_button_->SetEnabled(enabled()); 365 speech_button_->SetEnabled(enabled());
346 } 366 }
347 367
348 const char* SearchBoxView::GetClassName() const { 368 const char* SearchBoxView::GetClassName() const {
349 return "SearchBoxView"; 369 return "SearchBoxView";
350 } 370 }
351 371
372 void SearchBoxView::OnGestureEvent(ui::GestureEvent* event) {
373 if (!is_fullscreen_app_list_enabled_ || event->type() != ui::ET_GESTURE_TAP)
374 return;
375
376 // If the search box is empty and the cursor is not enabled, enable the cursor
377 // and shift the placeholder text left.
378 if (search_box_->text().empty() && !is_cursor_enabled_)
379 SetPlaceholderTextAndEnableCursor(true);
380 }
381
382 void SearchBoxView::OnMouseEvent(ui::MouseEvent* event) {
383 if (!is_fullscreen_app_list_enabled_ || event->type() != ui::ET_MOUSE_PRESSED)
384 return;
385
386 // If the search box is empty and the cursor is not enabled, enable the cursor
387 // and shift the placeholder text left.
388 if (search_box_->text().empty() && !is_cursor_enabled_)
389 SetPlaceholderTextAndEnableCursor(true);
390 }
391
352 void SearchBoxView::UpdateModel() { 392 void SearchBoxView::UpdateModel() {
353 // Temporarily remove from observer to ignore notifications caused by us. 393 // Temporarily remove from observer to ignore notifications caused by us.
354 model_->search_box()->RemoveObserver(this); 394 model_->search_box()->RemoveObserver(this);
355 model_->search_box()->Update(search_box_->text(), false); 395 model_->search_box()->Update(search_box_->text(), false);
356 model_->search_box()->SetSelectionModel(search_box_->GetSelectionModel()); 396 model_->search_box()->SetSelectionModel(search_box_->GetSelectionModel());
357 model_->search_box()->AddObserver(this); 397 model_->search_box()->AddObserver(this);
358 } 398 }
359 399
360 void SearchBoxView::NotifyQueryChanged() { 400 void SearchBoxView::NotifyQueryChanged() {
361 DCHECK(delegate_); 401 DCHECK(delegate_);
362 delegate_->QueryChanged(this); 402 delegate_->QueryChanged(this);
363 } 403 }
364 404
365 void SearchBoxView::ContentsChanged(views::Textfield* sender, 405 void SearchBoxView::ContentsChanged(views::Textfield* sender,
366 const base::string16& new_contents) { 406 const base::string16& new_contents) {
367 UpdateModel(); 407 UpdateModel();
368 view_delegate_->AutoLaunchCanceled(); 408 view_delegate_->AutoLaunchCanceled();
369 NotifyQueryChanged(); 409 NotifyQueryChanged();
370 if (is_fullscreen_app_list_enabled_) 410 if (is_fullscreen_app_list_enabled_) {
411 SetPlaceholderTextAndEnableCursor(!search_box_->text().empty());
371 app_list_view_->SetStateFromSearchBoxView(search_box_->text().empty()); 412 app_list_view_->SetStateFromSearchBoxView(search_box_->text().empty());
413 }
372 } 414 }
373 415
374 bool SearchBoxView::HandleKeyEvent(views::Textfield* sender, 416 bool SearchBoxView::HandleKeyEvent(views::Textfield* sender,
375 const ui::KeyEvent& key_event) { 417 const ui::KeyEvent& key_event) {
376 if (key_event.type() == ui::ET_KEY_PRESSED) { 418 if (key_event.type() == ui::ET_KEY_PRESSED) {
377 if (key_event.key_code() == ui::VKEY_TAB && 419 if (key_event.key_code() == ui::VKEY_TAB &&
378 focused_view_ != FOCUS_CONTENTS_VIEW && 420 focused_view_ != FOCUS_CONTENTS_VIEW &&
379 MoveTabFocus(key_event.IsShiftDown())) 421 MoveTabFocus(key_event.IsShiftDown()))
380 return true; 422 return true;
381 423
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 speech_button_->OnKeyReleased(key_event)) 457 speech_button_->OnKeyReleased(key_event))
416 return true; 458 return true;
417 459
418 return contents_view_ && contents_view_->visible() && 460 return contents_view_ && contents_view_->visible() &&
419 contents_view_->OnKeyReleased(key_event); 461 contents_view_->OnKeyReleased(key_event);
420 } 462 }
421 463
422 return false; 464 return false;
423 } 465 }
424 466
467 bool SearchBoxView::HandleMouseEvent(views::Textfield* sender,
468 const ui::MouseEvent& mouse_event) {
469 if (!is_fullscreen_app_list_enabled_)
470 return false;
471
472 if (search_box_->text().empty() && !is_cursor_enabled_)
473 SetPlaceholderTextAndEnableCursor(true);
474
475 return true;
476 }
477
478 void SearchBoxView::NotifyOfGestureEvent() {
479 if (!is_fullscreen_app_list_enabled_)
480 return;
481
482 if (search_box_->text().empty() && !is_cursor_enabled_)
483 SetPlaceholderTextAndEnableCursor(true);
484 }
485
425 void SearchBoxView::ButtonPressed(views::Button* sender, 486 void SearchBoxView::ButtonPressed(views::Button* sender,
426 const ui::Event& event) { 487 const ui::Event& event) {
427 if (back_button_ && sender == back_button_) 488 if (back_button_ && sender == back_button_)
428 delegate_->BackButtonPressed(); 489 delegate_->BackButtonPressed();
429 else if (speech_button_ && sender == speech_button_) 490 else if (speech_button_ && sender == speech_button_)
430 view_delegate_->StartSpeechRecognition(); 491 view_delegate_->StartSpeechRecognition();
431 else 492 else
432 NOTREACHED(); 493 NOTREACHED();
433 } 494 }
434 495
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 void SearchBoxView::Update() { 548 void SearchBoxView::Update() {
488 search_box_->SetText(model_->search_box()->text()); 549 search_box_->SetText(model_->search_box()->text());
489 NotifyQueryChanged(); 550 NotifyQueryChanged();
490 } 551 }
491 552
492 void SearchBoxView::OnSpeechRecognitionStateChanged( 553 void SearchBoxView::OnSpeechRecognitionStateChanged(
493 SpeechRecognitionState new_state) { 554 SpeechRecognitionState new_state) {
494 SpeechRecognitionButtonPropChanged(); 555 SpeechRecognitionButtonPropChanged();
495 SchedulePaint(); 556 SchedulePaint();
496 } 557 }
497
498 } // namespace app_list 558 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698