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

Unified Diff: chrome/browser/ui/autofill/autofill_popup_controller_impl.cc

Issue 11636040: AutofillPopupController clarifications + simplifications. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ilya review Created 8 years 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/autofill/autofill_popup_controller_impl.cc
diff --git a/chrome/browser/ui/autofill/autofill_popup_controller_impl.cc b/chrome/browser/ui/autofill/autofill_popup_controller_impl.cc
index 2d74b8706f6ddd8ae60d00f27d3dd757684f63ac..ebc207a4501642ec3c97a8725b4504d10a626fc1 100644
--- a/chrome/browser/ui/autofill/autofill_popup_controller_impl.cc
+++ b/chrome/browser/ui/autofill/autofill_popup_controller_impl.cc
@@ -6,6 +6,7 @@
#include "base/logging.h"
#include "base/utf_string_conversions.h"
+#include "chrome/browser/ui/autofill/autofill_popup_delegate.h"
#include "chrome/browser/ui/autofill/autofill_popup_view.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "grit/webkit_resources.h"
@@ -93,7 +94,7 @@ AutofillPopupControllerImpl::AutofillPopupControllerImpl(
delete_icon_hovered_(false),
is_hiding_(false) {
#if !defined(OS_ANDROID)
- label_font_ = value_font_.DeriveFont(kLabelFontSizeDelta);
+ sub_label_font_ = label_font_.DeriveFont(kLabelFontSizeDelta);
#endif
}
@@ -103,24 +104,24 @@ AutofillPopupControllerImpl::~AutofillPopupControllerImpl() {
}
void AutofillPopupControllerImpl::Show(
- const std::vector<string16>& autofill_values,
- const std::vector<string16>& autofill_labels,
- const std::vector<string16>& autofill_icons,
- const std::vector<int>& autofill_unique_ids) {
- autofill_values_ = autofill_values;
- autofill_labels_ = autofill_labels;
- autofill_icons_ = autofill_icons;
- autofill_unique_ids_ = autofill_unique_ids;
+ const std::vector<string16>& labels,
+ const std::vector<string16>& sub_labels,
+ const std::vector<string16>& icons,
+ const std::vector<int>& identifiers) {
+ labels_ = labels;
+ sub_labels_ = sub_labels;
+ icons_ = icons;
+ identifiers_ = identifiers;
#if !defined(OS_ANDROID)
// Android displays the long text with ellipsis using the view attributes.
// TODO(csharp): Fix crbug.com/156163 and use better logic when clipping.
- for (size_t i = 0; i < autofill_values_.size(); ++i) {
- if (autofill_values_[i].length() > 15)
- autofill_values_[i].erase(15);
- if (autofill_labels[i].length() > 15)
- autofill_labels_[i].erase(15);
+ for (size_t i = 0; i < labels_.size(); ++i) {
+ if (labels_[i].length() > 15)
+ labels_[i].erase(15);
+ if (sub_labels[i].length() > 15)
+ sub_labels_[i].erase(15);
}
#endif
@@ -150,10 +151,8 @@ void AutofillPopupControllerImpl::UpdateBoundsAndRedrawPopup() {
view_->UpdateBoundsAndRedrawPopup();
}
-void AutofillPopupControllerImpl::SetSelectedPosition(int x, int y) {
- int line = LineFromY(y);
-
- SetSelectedLine(line);
+void AutofillPopupControllerImpl::MouseHovered(int x, int y) {
+ SetSelectedLine(LineFromY(y));
bool delete_icon_hovered = DeleteIconIsUnder(x, y);
if (delete_icon_hovered != delete_icon_hovered_) {
@@ -162,26 +161,23 @@ void AutofillPopupControllerImpl::SetSelectedPosition(int x, int y) {
}
}
-bool AutofillPopupControllerImpl::AcceptAutofillSuggestion(
- const string16& value,
- int unique_id,
- unsigned index) {
- return delegate_->DidAcceptAutofillSuggestion(value, unique_id, index);
-}
-
-void AutofillPopupControllerImpl::AcceptSelectedPosition(int x, int y) {
- DCHECK_EQ(selected_line(), LineFromY(y));
+void AutofillPopupControllerImpl::MouseClicked(int x, int y) {
+ MouseHovered(x, y);
- if (DeleteIconIsUnder(x, y))
+ if (delete_icon_hovered_)
RemoveSelectedLine();
else
AcceptSelectedLine();
}
-void AutofillPopupControllerImpl::ClearSelectedLine() {
+void AutofillPopupControllerImpl::MouseExitedPopup() {
SetSelectedLine(kNoSelection);
}
+void AutofillPopupControllerImpl::AcceptSuggestion(size_t index) {
+ delegate_->DidAcceptSuggestion(labels_[index], identifiers_[index]);
+}
+
int AutofillPopupControllerImpl::GetIconResourceID(
const string16& resource_name) {
for (size_t i = 0; i < arraysize(kDataResources); ++i) {
@@ -192,7 +188,8 @@ int AutofillPopupControllerImpl::GetIconResourceID(
return -1;
}
-bool AutofillPopupControllerImpl::CanDelete(int id) {
+bool AutofillPopupControllerImpl::CanDelete(size_t index) {
+ int id = identifiers_[index];
return id > 0 ||
id == WebAutofillClient::MenuItemIDAutocompleteEntry ||
id == WebAutofillClient::MenuItemIDPasswordEntry;
@@ -200,27 +197,27 @@ bool AutofillPopupControllerImpl::CanDelete(int id) {
#if !defined(OS_ANDROID)
int AutofillPopupControllerImpl::GetPopupRequiredWidth() {
- if (value_font_.platform_font() == NULL ||
- label_font_.platform_font() == NULL) {
+ if (label_font_.platform_font() == NULL ||
+ sub_label_font_.platform_font() == NULL) {
// We can't calculate the size of the popup if the fonts
// aren't present.
return 0;
}
int popup_width = element_bounds().width();
- DCHECK_EQ(autofill_values().size(), autofill_labels().size());
- for (size_t i = 0; i < autofill_values().size(); ++i) {
+ DCHECK_EQ(labels().size(), sub_labels().size());
+ for (size_t i = 0; i < labels().size(); ++i) {
int row_size = kEndPadding +
- value_font_.GetStringWidth(autofill_values()[i]) +
+ label_font_.GetStringWidth(labels()[i]) +
kLabelPadding +
- label_font_.GetStringWidth(autofill_labels()[i]);
+ sub_label_font_.GetStringWidth(sub_labels()[i]);
// Add the Autofill icon size, if required.
- if (!autofill_icons()[i].empty())
+ if (!icons()[i].empty())
row_size += kAutofillIconWidth + kIconPadding;
// Add delete icon, if required.
- if (CanDelete(autofill_unique_ids()[i]))
+ if (CanDelete(i))
row_size += kDeleteIconWidth + kIconPadding;
// Add the padding at the end
@@ -235,29 +232,25 @@ int AutofillPopupControllerImpl::GetPopupRequiredWidth() {
int AutofillPopupControllerImpl::GetPopupRequiredHeight() {
int popup_height = 0;
- for (size_t i = 0; i < autofill_unique_ids().size(); ++i) {
- popup_height += GetRowHeightFromId(autofill_unique_ids()[i]);
+ for (size_t i = 0; i < identifiers().size(); ++i) {
+ popup_height += GetRowHeightFromId(identifiers()[i]);
}
return popup_height;
}
#endif // !defined(OS_ANDROID)
-int AutofillPopupControllerImpl::GetRowHeightFromId(int unique_id) {
- if (unique_id == WebAutofillClient::MenuItemIDSeparator)
- return kSeparatorHeight;
-
- return kRowHeight;
-}
-
-gfx::Rect AutofillPopupControllerImpl::GetRectForRow(size_t row, int width) {
+gfx::Rect AutofillPopupControllerImpl::GetRowBounds(size_t index) {
int top = 0;
- for (size_t i = 0; i < row; ++i) {
- top += GetRowHeightFromId(autofill_unique_ids()[i]);
+ for (size_t i = 0; i < index; ++i) {
+ top += GetRowHeightFromId(identifiers()[i]);
}
return gfx::Rect(
- 0, top, width, GetRowHeightFromId(autofill_unique_ids()[row]));
+ 0,
+ top,
+ popup_bounds_.width(),
+ GetRowHeightFromId(identifiers()[index]));
}
void AutofillPopupControllerImpl::SetPopupBounds(const gfx::Rect& bounds) {
@@ -277,24 +270,20 @@ const gfx::Rect& AutofillPopupControllerImpl::element_bounds() const {
return element_bounds_;
}
-const std::vector<string16>& AutofillPopupControllerImpl::
- autofill_values() const {
- return autofill_values_;
+const std::vector<string16>& AutofillPopupControllerImpl::labels() const {
+ return labels_;
}
-const std::vector<string16>& AutofillPopupControllerImpl::
- autofill_labels() const {
- return autofill_labels_;
+const std::vector<string16>& AutofillPopupControllerImpl::sub_labels() const {
+ return sub_labels_;
}
-const std::vector<string16>& AutofillPopupControllerImpl::
- autofill_icons() const {
- return autofill_icons_;
+const std::vector<string16>& AutofillPopupControllerImpl::icons() const {
+ return icons_;
}
-const std::vector<int>& AutofillPopupControllerImpl::
- autofill_unique_ids() const {
- return autofill_unique_ids_;
+const std::vector<int>& AutofillPopupControllerImpl::identifiers() const {
+ return identifiers_;
}
#if !defined(OS_ANDROID)
@@ -302,8 +291,8 @@ const gfx::Font& AutofillPopupControllerImpl::label_font() const {
return label_font_;
}
-const gfx::Font& AutofillPopupControllerImpl::value_font() const {
- return value_font_;
+const gfx::Font& AutofillPopupControllerImpl::sub_label_font() const {
+ return sub_label_font_;
}
#endif
@@ -328,7 +317,7 @@ bool AutofillPopupControllerImpl::HandleKeyPressEvent(
SetSelectedLine(0);
return true;
case ui::VKEY_NEXT: // Page down.
- SetSelectedLine(autofill_values().size() - 1);
+ SetSelectedLine(labels().size() - 1);
return true;
case ui::VKEY_ESCAPE:
HideInternal();
@@ -367,8 +356,7 @@ void AutofillPopupControllerImpl::SetSelectedLine(int selected_line) {
selected_line_ = selected_line;
if (selected_line_ != kNoSelection) {
- delegate_->SelectAutofillSuggestionAtIndex(
- autofill_unique_ids_[selected_line_]);
+ delegate_->DidSelectSuggestion(identifiers_[selected_line_]);
}
}
@@ -376,12 +364,12 @@ void AutofillPopupControllerImpl::SelectNextLine() {
int new_selected_line = selected_line_ + 1;
// Skip over any lines that can't be selected.
- while (static_cast<size_t>(new_selected_line) < autofill_values_.size() &&
- !CanAccept(autofill_unique_ids()[new_selected_line])) {
+ while (static_cast<size_t>(new_selected_line) < labels_.size() &&
+ !CanAccept(identifiers()[new_selected_line])) {
++new_selected_line;
}
- if (new_selected_line == static_cast<int>(autofill_values_.size()))
+ if (new_selected_line == static_cast<int>(labels_.size()))
new_selected_line = 0;
SetSelectedLine(new_selected_line);
@@ -392,12 +380,12 @@ void AutofillPopupControllerImpl::SelectPreviousLine() {
// Skip over any lines that can't be selected.
while (new_selected_line > kNoSelection &&
- !CanAccept(autofill_unique_ids()[new_selected_line])) {
+ !CanAccept(identifiers()[new_selected_line])) {
--new_selected_line;
}
if (new_selected_line <= kNoSelection)
- new_selected_line = autofill_values_.size() - 1;
+ new_selected_line = labels_.size() - 1;
SetSelectedLine(new_selected_line);
}
@@ -407,15 +395,13 @@ bool AutofillPopupControllerImpl::AcceptSelectedLine() {
return false;
DCHECK_GE(selected_line_, 0);
- DCHECK_LT(selected_line_, static_cast<int>(autofill_values_.size()));
+ DCHECK_LT(selected_line_, static_cast<int>(labels_.size()));
- if (!CanAccept(autofill_unique_ids_[selected_line_]))
+ if (!CanAccept(identifiers_[selected_line_]))
return false;
- return AcceptAutofillSuggestion(
- autofill_values_[selected_line_],
- autofill_unique_ids_[selected_line_],
- selected_line_);
+ AcceptSuggestion(selected_line_);
+ return true;
}
bool AutofillPopupControllerImpl::RemoveSelectedLine() {
@@ -423,24 +409,22 @@ bool AutofillPopupControllerImpl::RemoveSelectedLine() {
return false;
DCHECK_GE(selected_line_, 0);
- DCHECK_LT(selected_line_, static_cast<int>(autofill_values_.size()));
+ DCHECK_LT(selected_line_, static_cast<int>(labels_.size()));
- if (!CanDelete(autofill_unique_ids_[selected_line_]))
+ if (!CanDelete(selected_line_))
return false;
- if (autofill_unique_ids_[selected_line_] > 0) {
- delegate_->RemoveAutofillProfileOrCreditCard(
- autofill_unique_ids_[selected_line_]);
+ if (identifiers_[selected_line_] > 0) {
+ delegate_->RemoveSuggestion(identifiers_[selected_line_]);
} else {
- delegate_->RemoveAutocompleteEntry(
- autofill_values_[selected_line_]);
+ delegate_->RemoveAutocompleteEntry(labels_[selected_line_]);
}
// Remove the deleted element.
- autofill_values_.erase(autofill_values_.begin() + selected_line_);
- autofill_labels_.erase(autofill_labels_.begin() + selected_line_);
- autofill_icons_.erase(autofill_icons_.begin() + selected_line_);
- autofill_unique_ids_.erase(autofill_unique_ids_.begin() + selected_line_);
+ labels_.erase(labels_.begin() + selected_line_);
+ sub_labels_.erase(sub_labels_.begin() + selected_line_);
+ icons_.erase(icons_.begin() + selected_line_);
+ identifiers_.erase(identifiers_.begin() + selected_line_);
SetSelectedLine(kNoSelection);
@@ -457,15 +441,22 @@ bool AutofillPopupControllerImpl::RemoveSelectedLine() {
int AutofillPopupControllerImpl::LineFromY(int y) {
int current_height = 0;
- for (size_t i = 0; i < autofill_unique_ids().size(); ++i) {
- current_height += GetRowHeightFromId(autofill_unique_ids()[i]);
+ for (size_t i = 0; i < identifiers().size(); ++i) {
+ current_height += GetRowHeightFromId(identifiers()[i]);
if (y <= current_height)
return i;
}
// The y value goes beyond the popup so stop the selection at the last line.
- return autofill_unique_ids().size() - 1;
+ return identifiers().size() - 1;
+}
+
+int AutofillPopupControllerImpl::GetRowHeightFromId(int identifier) {
+ if (identifier == WebAutofillClient::MenuItemIDSeparator)
+ return kSeparatorHeight;
+
+ return kRowHeight;
}
bool AutofillPopupControllerImpl::DeleteIconIsUnder(int x, int y) {
@@ -477,7 +468,7 @@ bool AutofillPopupControllerImpl::DeleteIconIsUnder(int x, int y) {
int row_start_y = 0;
for (int i = 0; i < selected_line(); ++i) {
- row_start_y += GetRowHeightFromId(autofill_unique_ids()[i]);
+ row_start_y += GetRowHeightFromId(identifiers()[i]);
}
gfx::Rect delete_icon_bounds = gfx::Rect(
@@ -491,16 +482,17 @@ bool AutofillPopupControllerImpl::DeleteIconIsUnder(int x, int y) {
}
bool AutofillPopupControllerImpl::CanAccept(int id) {
- return id != WebAutofillClient::MenuItemIDSeparator;
+ return id != WebAutofillClient::MenuItemIDSeparator &&
+ id != WebAutofillClient::MenuItemIDWarningMessage;
}
bool AutofillPopupControllerImpl::HasAutofillEntries() {
- return autofill_values_.size() != 0 &&
- (autofill_unique_ids_[0] > 0 ||
- autofill_unique_ids_[0] ==
+ return labels_.size() != 0 &&
+ (identifiers_[0] > 0 ||
+ identifiers_[0] ==
WebAutofillClient::MenuItemIDAutocompleteEntry ||
- autofill_unique_ids_[0] == WebAutofillClient::MenuItemIDPasswordEntry ||
- autofill_unique_ids_[0] == WebAutofillClient::MenuItemIDDataListEntry);
+ identifiers_[0] == WebAutofillClient::MenuItemIDPasswordEntry ||
+ identifiers_[0] == WebAutofillClient::MenuItemIDDataListEntry);
}
void AutofillPopupControllerImpl::ShowView() {

Powered by Google App Engine
This is Rietveld 408576698