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 "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" | 5 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/browser/ui/autofill/autofill_popup_view.h" | 12 #include "chrome/browser/ui/autofill/autofill_popup_view.h" |
13 #include "components/autofill/core/browser/autofill_popup_delegate.h" | 13 #include "components/autofill/core/browser/autofill_popup_delegate.h" |
14 #include "content/public/browser/native_web_keyboard_event.h" | 14 #include "content/public/browser/native_web_keyboard_event.h" |
15 #include "grit/webkit_resources.h" | 15 #include "grit/webkit_resources.h" |
16 #include "third_party/WebKit/public/web/WebAutofillClient.h" | 16 #include "third_party/WebKit/public/web/WebAutofillClient.h" |
17 #include "ui/events/event.h" | 17 #include "ui/events/event.h" |
18 #include "ui/gfx/display.h" | 18 #include "ui/gfx/display.h" |
19 #include "ui/gfx/rect_conversions.h" | 19 #include "ui/gfx/rect_conversions.h" |
20 #include "ui/gfx/screen.h" | 20 #include "ui/gfx/screen.h" |
21 #include "ui/gfx/text_elider.h" | 21 #include "ui/gfx/text_elider.h" |
22 #include "ui/gfx/vector2d.h" | 22 #include "ui/gfx/vector2d.h" |
23 | 23 |
24 using base::WeakPtr; | 24 using base::WeakPtr; |
25 using WebKit::WebAutofillClient; | 25 using WebKit::WebAutofillClient; |
26 | 26 |
27 namespace autofill { | 27 namespace autofill { |
28 namespace { | 28 namespace { |
29 | 29 |
30 // Used to indicate that no line is currently selected by the user. | 30 // Used to indicate that no line is currently preselected by the user. |
31 const int kNoSelection = -1; | 31 const int kNoPreselection = -1; |
32 | 32 |
33 // The vertical height of each row in pixels. | 33 // The vertical height of each row in pixels. |
34 const size_t kRowHeight = 24; | 34 const size_t kRowHeight = 24; |
35 | 35 |
36 // The vertical height of a separator in pixels. | 36 // The vertical height of a separator in pixels. |
37 const size_t kSeparatorHeight = 1; | 37 const size_t kSeparatorHeight = 1; |
38 | 38 |
39 #if !defined(OS_ANDROID) | 39 #if !defined(OS_ANDROID) |
40 // Size difference between name and subtext in pixels. | 40 // Size difference between name and subtext in pixels. |
41 const int kLabelFontSizeDelta = -2; | 41 const int kLabelFontSizeDelta = -2; |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 // The view has already been destroyed so clear the reference to it. | 241 // The view has already been destroyed so clear the reference to it. |
242 view_ = NULL; | 242 view_ = NULL; |
243 | 243 |
244 Hide(); | 244 Hide(); |
245 } | 245 } |
246 | 246 |
247 bool AutofillPopupControllerImpl::HandleKeyPressEvent( | 247 bool AutofillPopupControllerImpl::HandleKeyPressEvent( |
248 const content::NativeWebKeyboardEvent& event) { | 248 const content::NativeWebKeyboardEvent& event) { |
249 switch (event.windowsKeyCode) { | 249 switch (event.windowsKeyCode) { |
250 case ui::VKEY_UP: | 250 case ui::VKEY_UP: |
251 SelectPreviousLine(); | 251 PreselectPreviousLine(); |
252 return true; | 252 return true; |
253 case ui::VKEY_DOWN: | 253 case ui::VKEY_DOWN: |
254 SelectNextLine(); | 254 PreselectNextLine(); |
255 return true; | 255 return true; |
256 case ui::VKEY_PRIOR: // Page up. | 256 case ui::VKEY_PRIOR: // Page up. |
257 SetSelectedLine(0); | 257 SetPreselectedLine(0); |
258 return true; | 258 return true; |
259 case ui::VKEY_NEXT: // Page down. | 259 case ui::VKEY_NEXT: // Page down. |
260 SetSelectedLine(names().size() - 1); | 260 SetPreselectedLine(names().size() - 1); |
261 return true; | 261 return true; |
262 case ui::VKEY_ESCAPE: | 262 case ui::VKEY_ESCAPE: |
263 Hide(); | 263 Hide(); |
264 return true; | 264 return true; |
265 case ui::VKEY_DELETE: | 265 case ui::VKEY_DELETE: |
266 return (event.modifiers & content::NativeWebKeyboardEvent::ShiftKey) && | 266 return (event.modifiers & content::NativeWebKeyboardEvent::ShiftKey) && |
267 RemoveSelectedLine(); | 267 RemovePreselectedLine(); |
268 case ui::VKEY_TAB: | 268 case ui::VKEY_TAB: |
269 // A tab press should cause the highlighted line to be selected, but still | 269 // A tab press should cause the preselected line to be selected, but still |
270 // return false so the tab key press propagates and changes the cursor | 270 // return false so the tab key press propagates and changes the cursor |
271 // location. | 271 // location. |
272 AcceptSelectedLine(); | 272 AcceptPreselectedLine(); |
273 return false; | 273 return false; |
274 case ui::VKEY_RETURN: | 274 case ui::VKEY_RETURN: |
275 return AcceptSelectedLine(); | 275 return AcceptPreselectedLine(); |
276 default: | 276 default: |
277 return false; | 277 return false; |
278 } | 278 } |
279 } | 279 } |
280 | 280 |
281 void AutofillPopupControllerImpl::UpdateBoundsAndRedrawPopup() { | 281 void AutofillPopupControllerImpl::UpdateBoundsAndRedrawPopup() { |
282 #if !defined(OS_ANDROID) | 282 #if !defined(OS_ANDROID) |
283 // TODO(csharp): Since UpdatePopupBounds can change the position of the popup, | 283 // TODO(csharp): Since UpdatePopupBounds can change the position of the popup, |
284 // the popup could end up jumping from above the element to below it. | 284 // the popup could end up jumping from above the element to below it. |
285 // It is unclear if it is better to keep the popup where it was, or if it | 285 // It is unclear if it is better to keep the popup where it was, or if it |
286 // should try and move to its desired position. | 286 // should try and move to its desired position. |
287 UpdatePopupBounds(); | 287 UpdatePopupBounds(); |
288 #endif | 288 #endif |
289 | 289 |
290 view_->UpdateBoundsAndRedrawPopup(); | 290 view_->UpdateBoundsAndRedrawPopup(); |
291 } | 291 } |
292 | 292 |
293 void AutofillPopupControllerImpl::MouseHovered(int x, int y) { | 293 void AutofillPopupControllerImpl::PointPreselected(int x, int y) { |
294 SetSelectedLine(LineFromY(y)); | 294 SetPreselectedLine(LineFromY(y)); |
295 } | 295 } |
296 | 296 |
297 void AutofillPopupControllerImpl::MouseClicked(int x, int y) { | 297 void AutofillPopupControllerImpl::PointSelected(int x, int y) { |
298 MouseHovered(x, y); | 298 PointPreselected(x, y); |
299 AcceptSelectedLine(); | 299 AcceptPreselectedLine(); |
300 } | 300 } |
301 | 301 |
302 void AutofillPopupControllerImpl::MouseExitedPopup() { | 302 void AutofillPopupControllerImpl::PreselectionCleared() { |
303 SetSelectedLine(kNoSelection); | 303 SetPreselectedLine(kNoPreselection); |
304 } | 304 } |
305 | 305 |
306 bool AutofillPopupControllerImpl::ShouldRepostEvent( | 306 bool AutofillPopupControllerImpl::ShouldRepostEvent( |
307 const ui::MouseEvent& event) { | 307 const ui::MouseEvent& event) { |
308 return delegate_->ShouldRepostEvent(event); | 308 return delegate_->ShouldRepostEvent(event); |
309 } | 309 } |
310 | 310 |
311 void AutofillPopupControllerImpl::AcceptSuggestion(size_t index) { | 311 void AutofillPopupControllerImpl::AcceptSuggestion(size_t index) { |
312 delegate_->DidAcceptSuggestion(full_names_[index], identifiers_[index]); | 312 delegate_->DidAcceptSuggestion(full_names_[index], identifiers_[index]); |
313 } | 313 } |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 return warning_font_; | 396 return warning_font_; |
397 | 397 |
398 return name_font_; | 398 return name_font_; |
399 } | 399 } |
400 | 400 |
401 const gfx::Font& AutofillPopupControllerImpl::subtext_font() const { | 401 const gfx::Font& AutofillPopupControllerImpl::subtext_font() const { |
402 return subtext_font_; | 402 return subtext_font_; |
403 } | 403 } |
404 #endif | 404 #endif |
405 | 405 |
406 int AutofillPopupControllerImpl::selected_line() const { | 406 int AutofillPopupControllerImpl::preselected_line() const { |
407 return selected_line_; | 407 return preselected_line_; |
408 } | 408 } |
409 | 409 |
410 void AutofillPopupControllerImpl::set_hide_on_outside_click( | 410 void AutofillPopupControllerImpl::set_hide_on_outside_click( |
411 bool hide_on_outside_click) { | 411 bool hide_on_outside_click) { |
412 hide_on_outside_click_ = hide_on_outside_click; | 412 hide_on_outside_click_ = hide_on_outside_click; |
413 } | 413 } |
414 | 414 |
415 void AutofillPopupControllerImpl::SetSelectedLine(int selected_line) { | 415 void AutofillPopupControllerImpl::SetPreselectedLine(int preselected_line) { |
416 if (selected_line_ == selected_line) | 416 if (preselected_line_ == preselected_line) |
417 return; | 417 return; |
418 | 418 |
419 if (selected_line_ != kNoSelection && | 419 if (preselected_line_ != kNoPreselection && |
420 static_cast<size_t>(selected_line_) < identifiers_.size()) | 420 static_cast<size_t>(preselected_line_) < identifiers_.size()) |
421 InvalidateRow(selected_line_); | 421 InvalidateRow(preselected_line_); |
422 | 422 |
423 if (selected_line != kNoSelection) | 423 if (preselected_line != kNoPreselection) |
424 InvalidateRow(selected_line); | 424 InvalidateRow(preselected_line); |
425 | 425 |
426 selected_line_ = selected_line; | 426 preselected_line_ = preselected_line; |
427 | 427 |
428 if (selected_line_ != kNoSelection) | 428 if (preselected_line_ != kNoPreselection) |
429 delegate_->DidSelectSuggestion(identifiers_[selected_line_]); | 429 delegate_->DidPreselectSuggestion(identifiers_[preselected_line_]); |
430 else | 430 else |
431 delegate_->ClearPreviewedForm(); | 431 delegate_->ClearPreviewedForm(); |
432 } | 432 } |
433 | 433 |
434 void AutofillPopupControllerImpl::SelectNextLine() { | 434 void AutofillPopupControllerImpl::PreselectNextLine() { |
435 int new_selected_line = selected_line_ + 1; | 435 int new_preselected_line = preselected_line_ + 1; |
436 | 436 |
437 // Skip over any lines that can't be selected. | 437 // Skip over any lines that can't be selected. |
438 while (static_cast<size_t>(new_selected_line) < names_.size() && | 438 while (static_cast<size_t>(new_preselected_line) < names_.size() && |
439 !CanAccept(identifiers()[new_selected_line])) { | 439 !CanAccept(identifiers()[new_preselected_line])) { |
440 ++new_selected_line; | 440 ++new_preselected_line; |
441 } | 441 } |
442 | 442 |
443 if (new_selected_line >= static_cast<int>(names_.size())) | 443 if (new_preselected_line >= static_cast<int>(names_.size())) |
444 new_selected_line = 0; | 444 new_preselected_line = 0; |
445 | 445 |
446 SetSelectedLine(new_selected_line); | 446 SetPreselectedLine(new_preselected_line); |
447 } | 447 } |
448 | 448 |
449 void AutofillPopupControllerImpl::SelectPreviousLine() { | 449 void AutofillPopupControllerImpl::PreselectPreviousLine() { |
450 int new_selected_line = selected_line_ - 1; | 450 int new_preselected_line = preselected_line_ - 1; |
451 | 451 |
452 // Skip over any lines that can't be selected. | 452 // Skip over any lines that can't be selected. |
453 while (new_selected_line > kNoSelection && | 453 while (new_preselected_line > kNoPreselection && |
454 !CanAccept(identifiers()[new_selected_line])) { | 454 !CanAccept(identifiers()[new_preselected_line])) { |
455 --new_selected_line; | 455 --new_preselected_line; |
456 } | 456 } |
457 | 457 |
458 if (new_selected_line <= kNoSelection) | 458 if (new_preselected_line <= kNoPreselection) |
459 new_selected_line = names_.size() - 1; | 459 new_preselected_line = names_.size() - 1; |
460 | 460 |
461 SetSelectedLine(new_selected_line); | 461 SetPreselectedLine(new_preselected_line); |
462 } | 462 } |
463 | 463 |
464 bool AutofillPopupControllerImpl::AcceptSelectedLine() { | 464 bool AutofillPopupControllerImpl::AcceptPreselectedLine() { |
465 if (selected_line_ == kNoSelection) | 465 if (preselected_line_ == kNoPreselection) |
466 return false; | 466 return false; |
467 | 467 |
468 DCHECK_GE(selected_line_, 0); | 468 DCHECK_GE(preselected_line_, 0); |
469 DCHECK_LT(selected_line_, static_cast<int>(names_.size())); | 469 DCHECK_LT(preselected_line_, static_cast<int>(names_.size())); |
470 | 470 |
471 if (!CanAccept(identifiers_[selected_line_])) | 471 if (!CanAccept(identifiers_[preselected_line_])) |
472 return false; | 472 return false; |
473 | 473 |
474 AcceptSuggestion(selected_line_); | 474 AcceptSuggestion(preselected_line_); |
475 return true; | 475 return true; |
476 } | 476 } |
477 | 477 |
478 bool AutofillPopupControllerImpl::RemoveSelectedLine() { | 478 bool AutofillPopupControllerImpl::RemovePreselectedLine() { |
479 if (selected_line_ == kNoSelection) | 479 if (preselected_line_ == kNoPreselection) |
480 return false; | 480 return false; |
481 | 481 |
482 DCHECK_GE(selected_line_, 0); | 482 DCHECK_GE(preselected_line_, 0); |
483 DCHECK_LT(selected_line_, static_cast<int>(names_.size())); | 483 DCHECK_LT(preselected_line_, static_cast<int>(names_.size())); |
484 | 484 |
485 if (!CanDelete(selected_line_)) | 485 if (!CanDelete(preselected_line_)) |
486 return false; | 486 return false; |
487 | 487 |
488 delegate_->RemoveSuggestion(full_names_[selected_line_], | 488 delegate_->RemoveSuggestion(full_names_[preselected_line_], |
489 identifiers_[selected_line_]); | 489 identifiers_[preselected_line_]); |
490 | 490 |
491 // Remove the deleted element. | 491 // Remove the deleted element. |
492 names_.erase(names_.begin() + selected_line_); | 492 names_.erase(names_.begin() + preselected_line_); |
493 full_names_.erase(full_names_.begin() + selected_line_); | 493 full_names_.erase(full_names_.begin() + preselected_line_); |
494 subtexts_.erase(subtexts_.begin() + selected_line_); | 494 subtexts_.erase(subtexts_.begin() + preselected_line_); |
495 icons_.erase(icons_.begin() + selected_line_); | 495 icons_.erase(icons_.begin() + preselected_line_); |
496 identifiers_.erase(identifiers_.begin() + selected_line_); | 496 identifiers_.erase(identifiers_.begin() + preselected_line_); |
497 | 497 |
498 SetSelectedLine(kNoSelection); | 498 SetPreselectedLine(kNoPreselection); |
499 | 499 |
500 if (HasSuggestions()) { | 500 if (HasSuggestions()) { |
501 delegate_->ClearPreviewedForm(); | 501 delegate_->ClearPreviewedForm(); |
502 UpdateBoundsAndRedrawPopup(); | 502 UpdateBoundsAndRedrawPopup(); |
503 } else { | 503 } else { |
504 Hide(); | 504 Hide(); |
505 } | 505 } |
506 | 506 |
507 return true; | 507 return true; |
508 } | 508 } |
509 | 509 |
510 int AutofillPopupControllerImpl::LineFromY(int y) { | 510 int AutofillPopupControllerImpl::LineFromY(int y) { |
511 int current_height = AutofillPopupView::kBorderThickness; | 511 int current_height = AutofillPopupView::kBorderThickness; |
512 | 512 |
513 for (size_t i = 0; i < identifiers().size(); ++i) { | 513 for (size_t i = 0; i < identifiers().size(); ++i) { |
514 current_height += GetRowHeightFromId(identifiers()[i]); | 514 current_height += GetRowHeightFromId(identifiers()[i]); |
515 | 515 |
516 if (y <= current_height) | 516 if (y <= current_height) |
517 return i; | 517 return i; |
518 } | 518 } |
519 | 519 |
520 // The y value goes beyond the popup so stop the selection at the last line. | 520 // The y value goes beyond the popup so stop the preselection at the last |
| 521 // line. |
521 return identifiers().size() - 1; | 522 return identifiers().size() - 1; |
522 } | 523 } |
523 | 524 |
524 int AutofillPopupControllerImpl::GetRowHeightFromId(int identifier) const { | 525 int AutofillPopupControllerImpl::GetRowHeightFromId(int identifier) const { |
525 if (identifier == WebAutofillClient::MenuItemIDSeparator) | 526 if (identifier == WebAutofillClient::MenuItemIDSeparator) |
526 return kSeparatorHeight; | 527 return kSeparatorHeight; |
527 | 528 |
528 return kRowHeight; | 529 return kRowHeight; |
529 } | 530 } |
530 | 531 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
657 // and this will cause flickering. | 658 // and this will cause flickering. |
658 | 659 |
659 popup_bounds_ = gfx::Rect(); | 660 popup_bounds_ = gfx::Rect(); |
660 | 661 |
661 names_.clear(); | 662 names_.clear(); |
662 subtexts_.clear(); | 663 subtexts_.clear(); |
663 icons_.clear(); | 664 icons_.clear(); |
664 identifiers_.clear(); | 665 identifiers_.clear(); |
665 full_names_.clear(); | 666 full_names_.clear(); |
666 | 667 |
667 selected_line_ = kNoSelection; | 668 preselected_line_ = kNoPreselection; |
668 } | 669 } |
669 | 670 |
670 const gfx::Rect AutofillPopupControllerImpl::RoundedElementBounds() const { | 671 const gfx::Rect AutofillPopupControllerImpl::RoundedElementBounds() const { |
671 return gfx::ToEnclosingRect(element_bounds_); | 672 return gfx::ToEnclosingRect(element_bounds_); |
672 } | 673 } |
673 | 674 |
674 gfx::Display AutofillPopupControllerImpl::GetDisplayNearestPoint( | 675 gfx::Display AutofillPopupControllerImpl::GetDisplayNearestPoint( |
675 const gfx::Point& point) const { | 676 const gfx::Point& point) const { |
676 return gfx::Screen::GetScreenFor(container_view())->GetDisplayNearestPoint( | 677 return gfx::Screen::GetScreenFor(container_view())->GetDisplayNearestPoint( |
677 point); | 678 point); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
733 // The popup can appear below the field. | 734 // The popup can appear below the field. |
734 return std::make_pair(bottom_growth_start, popup_required_height); | 735 return std::make_pair(bottom_growth_start, popup_required_height); |
735 } else { | 736 } else { |
736 // The popup must appear above the field. | 737 // The popup must appear above the field. |
737 return std::make_pair(top_growth_end - popup_required_height, | 738 return std::make_pair(top_growth_end - popup_required_height, |
738 popup_required_height); | 739 popup_required_height); |
739 } | 740 } |
740 } | 741 } |
741 | 742 |
742 } // namespace autofill | 743 } // namespace autofill |
OLD | NEW |