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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/ui/autofill/autofill_popup_delegate.h" | 9 #include "chrome/browser/ui/autofill/autofill_popup_delegate.h" |
10 #include "chrome/browser/ui/autofill/autofill_popup_view.h" | 10 #include "chrome/browser/ui/autofill/autofill_popup_view.h" |
11 #include "content/public/browser/native_web_keyboard_event.h" | 11 #include "content/public/browser/native_web_keyboard_event.h" |
12 #include "grit/webkit_resources.h" | 12 #include "grit/webkit_resources.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
14 #include "ui/base/events/event.h" | 14 #include "ui/base/events/event.h" |
15 | 15 |
16 using WebKit::WebAutofillClient; | 16 using WebKit::WebAutofillClient; |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // Used to indicate that no line is currently selected by the user. | 20 // Used to indicate that no line is currently selected by the user. |
21 const int kNoSelection = -1; | 21 const int kNoSelection = -1; |
22 | 22 |
23 // Size difference between value text and label text in pixels. | 23 // Size difference between name and subtext in pixels. |
24 const int kLabelFontSizeDelta = -2; | 24 const int kLabelFontSizeDelta = -2; |
25 | 25 |
26 // The vertical height of each row in pixels. | 26 // The vertical height of each row in pixels. |
27 const size_t kRowHeight = 24; | 27 const size_t kRowHeight = 24; |
28 | 28 |
29 // The vertical height of a separator in pixels. | 29 // The vertical height of a separator in pixels. |
30 const size_t kSeparatorHeight = 1; | 30 const size_t kSeparatorHeight = 1; |
31 | 31 |
32 // The amount of minimum padding between the Autofill value and label in pixels. | 32 // The amount of minimum padding between the Autofill name and subtext in |
33 const size_t kLabelPadding = 15; | 33 // pixels. |
| 34 const size_t kNamePadding = 15; |
34 | 35 |
35 // The maximum amount of characters to display from either the label or value. | 36 // The maximum amount of characters to display from either the name or subtext. |
36 const size_t kMaxTextLength = 15; | 37 const size_t kMaxTextLength = 15; |
37 | 38 |
38 #if !defined(OS_ANDROID) | 39 #if !defined(OS_ANDROID) |
39 const size_t kIconPadding = AutofillPopupView::kIconPadding; | 40 const size_t kIconPadding = AutofillPopupView::kIconPadding; |
40 const size_t kEndPadding = AutofillPopupView::kEndPadding; | 41 const size_t kEndPadding = AutofillPopupView::kEndPadding; |
41 const size_t kDeleteIconHeight = AutofillPopupView::kDeleteIconHeight; | 42 const size_t kDeleteIconHeight = AutofillPopupView::kDeleteIconHeight; |
42 const size_t kDeleteIconWidth = AutofillPopupView::kDeleteIconWidth; | 43 const size_t kDeleteIconWidth = AutofillPopupView::kDeleteIconWidth; |
43 const size_t kAutofillIconWidth = AutofillPopupView::kAutofillIconWidth; | 44 const size_t kAutofillIconWidth = AutofillPopupView::kAutofillIconWidth; |
44 #endif | 45 #endif |
45 | 46 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 gfx::NativeView container_view, | 88 gfx::NativeView container_view, |
88 const gfx::Rect& element_bounds) | 89 const gfx::Rect& element_bounds) |
89 : view_(NULL), | 90 : view_(NULL), |
90 delegate_(delegate), | 91 delegate_(delegate), |
91 container_view_(container_view), | 92 container_view_(container_view), |
92 element_bounds_(element_bounds), | 93 element_bounds_(element_bounds), |
93 selected_line_(kNoSelection), | 94 selected_line_(kNoSelection), |
94 delete_icon_hovered_(false), | 95 delete_icon_hovered_(false), |
95 is_hiding_(false) { | 96 is_hiding_(false) { |
96 #if !defined(OS_ANDROID) | 97 #if !defined(OS_ANDROID) |
97 label_font_ = value_font_.DeriveFont(kLabelFontSizeDelta); | 98 subtext_font_ = name_font_.DeriveFont(kLabelFontSizeDelta); |
98 #endif | 99 #endif |
99 } | 100 } |
100 | 101 |
101 AutofillPopupControllerImpl::~AutofillPopupControllerImpl() { | 102 AutofillPopupControllerImpl::~AutofillPopupControllerImpl() { |
102 if (delegate_) | 103 if (delegate_) |
103 delegate_->ControllerDestroyed(); | 104 delegate_->ControllerDestroyed(); |
104 } | 105 } |
105 | 106 |
106 void AutofillPopupControllerImpl::Show( | 107 void AutofillPopupControllerImpl::Show( |
107 const std::vector<string16>& autofill_values, | 108 const std::vector<string16>& names, |
108 const std::vector<string16>& autofill_labels, | 109 const std::vector<string16>& subtexts, |
109 const std::vector<string16>& autofill_icons, | 110 const std::vector<string16>& icons, |
110 const std::vector<int>& autofill_unique_ids) { | 111 const std::vector<int>& identifiers) { |
111 autofill_values_ = autofill_values; | 112 names_ = names; |
112 autofill_labels_ = autofill_labels; | 113 subtexts_ = subtexts; |
113 autofill_icons_ = autofill_icons; | 114 icons_ = icons; |
114 autofill_unique_ids_ = autofill_unique_ids; | 115 identifiers_ = identifiers; |
115 | 116 |
116 #if !defined(OS_ANDROID) | 117 #if !defined(OS_ANDROID) |
117 // Android displays the long text with ellipsis using the view attributes. | 118 // Android displays the long text with ellipsis using the view attributes. |
118 | 119 |
119 // TODO(csharp): Fix crbug.com/156163 and use better logic when clipping. | 120 // TODO(csharp): Fix crbug.com/156163 and use better logic when clipping. |
120 for (size_t i = 0; i < autofill_values_.size(); ++i) { | 121 for (size_t i = 0; i < names_.size(); ++i) { |
121 if (autofill_values_[i].length() > 15) | 122 if (names_[i].length() > 15) |
122 autofill_values_[i].erase(15); | 123 names_[i].erase(15); |
123 if (autofill_labels[i].length() > 15) | 124 if (subtexts[i].length() > 15) |
124 autofill_labels_[i].erase(15); | 125 subtexts_[i].erase(15); |
125 } | 126 } |
126 #endif | 127 #endif |
127 | 128 |
128 if (!view_) { | 129 if (!view_) { |
129 view_ = AutofillPopupView::Create(this); | 130 view_ = AutofillPopupView::Create(this); |
130 ShowView(); | 131 ShowView(); |
131 } else { | 132 } else { |
132 UpdateBoundsAndRedrawPopup(); | 133 UpdateBoundsAndRedrawPopup(); |
133 } | 134 } |
134 } | 135 } |
135 | 136 |
136 void AutofillPopupControllerImpl::Hide() { | 137 void AutofillPopupControllerImpl::Hide() { |
137 delegate_ = NULL; | 138 delegate_ = NULL; |
138 HideInternal(); | 139 HideInternal(); |
139 } | 140 } |
140 | 141 |
141 void AutofillPopupControllerImpl::ViewDestroyed() { | 142 void AutofillPopupControllerImpl::ViewDestroyed() { |
142 delete this; | 143 delete this; |
143 } | 144 } |
144 | 145 |
145 void AutofillPopupControllerImpl::UpdateBoundsAndRedrawPopup() { | 146 void AutofillPopupControllerImpl::UpdateBoundsAndRedrawPopup() { |
146 #if !defined(OS_ANDROID) | 147 #if !defined(OS_ANDROID) |
147 popup_bounds_.set_width(GetPopupRequiredWidth()); | 148 popup_bounds_.set_width(GetPopupRequiredWidth()); |
148 popup_bounds_.set_height(GetPopupRequiredHeight()); | 149 popup_bounds_.set_height(GetPopupRequiredHeight()); |
149 #endif | 150 #endif |
150 | 151 |
151 view_->UpdateBoundsAndRedrawPopup(); | 152 view_->UpdateBoundsAndRedrawPopup(); |
152 } | 153 } |
153 | 154 |
154 void AutofillPopupControllerImpl::SetSelectedPosition(int x, int y) { | 155 void AutofillPopupControllerImpl::MouseHovered(int x, int y) { |
155 int line = LineFromY(y); | 156 SetSelectedLine(LineFromY(y)); |
156 | |
157 SetSelectedLine(line); | |
158 | 157 |
159 bool delete_icon_hovered = DeleteIconIsUnder(x, y); | 158 bool delete_icon_hovered = DeleteIconIsUnder(x, y); |
160 if (delete_icon_hovered != delete_icon_hovered_) { | 159 if (delete_icon_hovered != delete_icon_hovered_) { |
161 delete_icon_hovered_ = delete_icon_hovered; | 160 delete_icon_hovered_ = delete_icon_hovered; |
162 InvalidateRow(selected_line()); | 161 InvalidateRow(selected_line()); |
163 } | 162 } |
164 } | 163 } |
165 | 164 |
166 bool AutofillPopupControllerImpl::AcceptAutofillSuggestion( | 165 void AutofillPopupControllerImpl::MouseClicked(int x, int y) { |
167 const string16& value, | 166 MouseHovered(x, y); |
168 int unique_id, | |
169 unsigned index) { | |
170 return delegate_->DidAcceptAutofillSuggestion(value, unique_id, index); | |
171 } | |
172 | 167 |
173 void AutofillPopupControllerImpl::AcceptSelectedPosition(int x, int y) { | 168 if (delete_icon_hovered_) |
174 DCHECK_EQ(selected_line(), LineFromY(y)); | |
175 | |
176 if (DeleteIconIsUnder(x, y)) | |
177 RemoveSelectedLine(); | 169 RemoveSelectedLine(); |
178 else | 170 else |
179 AcceptSelectedLine(); | 171 AcceptSelectedLine(); |
180 } | 172 } |
181 | 173 |
182 void AutofillPopupControllerImpl::ClearSelectedLine() { | 174 void AutofillPopupControllerImpl::MouseExitedPopup() { |
183 SetSelectedLine(kNoSelection); | 175 SetSelectedLine(kNoSelection); |
184 } | 176 } |
185 | 177 |
| 178 void AutofillPopupControllerImpl::AcceptSuggestion(size_t index) { |
| 179 delegate_->DidAcceptSuggestion(names_[index], identifiers_[index]); |
| 180 } |
| 181 |
186 int AutofillPopupControllerImpl::GetIconResourceID( | 182 int AutofillPopupControllerImpl::GetIconResourceID( |
187 const string16& resource_name) { | 183 const string16& resource_name) { |
188 for (size_t i = 0; i < arraysize(kDataResources); ++i) { | 184 for (size_t i = 0; i < arraysize(kDataResources); ++i) { |
189 if (resource_name == ASCIIToUTF16(kDataResources[i].name)) | 185 if (resource_name == ASCIIToUTF16(kDataResources[i].name)) |
190 return kDataResources[i].id; | 186 return kDataResources[i].id; |
191 } | 187 } |
192 | 188 |
193 return -1; | 189 return -1; |
194 } | 190 } |
195 | 191 |
196 bool AutofillPopupControllerImpl::CanDelete(int id) { | 192 bool AutofillPopupControllerImpl::CanDelete(size_t index) { |
| 193 int id = identifiers_[index]; |
197 return id > 0 || | 194 return id > 0 || |
198 id == WebAutofillClient::MenuItemIDAutocompleteEntry || | 195 id == WebAutofillClient::MenuItemIDAutocompleteEntry || |
199 id == WebAutofillClient::MenuItemIDPasswordEntry; | 196 id == WebAutofillClient::MenuItemIDPasswordEntry; |
200 } | 197 } |
201 | 198 |
202 #if !defined(OS_ANDROID) | 199 #if !defined(OS_ANDROID) |
203 int AutofillPopupControllerImpl::GetPopupRequiredWidth() { | 200 int AutofillPopupControllerImpl::GetPopupRequiredWidth() { |
204 if (value_font_.platform_font() == NULL || | 201 if (name_font_.platform_font() == NULL || |
205 label_font_.platform_font() == NULL) { | 202 subtext_font_.platform_font() == NULL) { |
206 // We can't calculate the size of the popup if the fonts | 203 // We can't calculate the size of the popup if the fonts |
207 // aren't present. | 204 // aren't present. |
208 return 0; | 205 return 0; |
209 } | 206 } |
210 | 207 |
211 int popup_width = element_bounds().width(); | 208 int popup_width = element_bounds().width(); |
212 DCHECK_EQ(autofill_values().size(), autofill_labels().size()); | 209 DCHECK_EQ(names().size(), subtexts().size()); |
213 for (size_t i = 0; i < autofill_values().size(); ++i) { | 210 for (size_t i = 0; i < names().size(); ++i) { |
214 int row_size = kEndPadding + | 211 int row_size = kEndPadding + |
215 value_font_.GetStringWidth(autofill_values()[i]) + | 212 name_font_.GetStringWidth(names()[i]) + |
216 kLabelPadding + | 213 kNamePadding + |
217 label_font_.GetStringWidth(autofill_labels()[i]); | 214 subtext_font_.GetStringWidth(subtexts()[i]); |
218 | 215 |
219 // Add the Autofill icon size, if required. | 216 // Add the Autofill icon size, if required. |
220 if (!autofill_icons()[i].empty()) | 217 if (!icons()[i].empty()) |
221 row_size += kAutofillIconWidth + kIconPadding; | 218 row_size += kAutofillIconWidth + kIconPadding; |
222 | 219 |
223 // Add delete icon, if required. | 220 // Add delete icon, if required. |
224 if (CanDelete(autofill_unique_ids()[i])) | 221 if (CanDelete(i)) |
225 row_size += kDeleteIconWidth + kIconPadding; | 222 row_size += kDeleteIconWidth + kIconPadding; |
226 | 223 |
227 // Add the padding at the end | 224 // Add the padding at the end |
228 row_size += kEndPadding; | 225 row_size += kEndPadding; |
229 | 226 |
230 popup_width = std::max(popup_width, row_size); | 227 popup_width = std::max(popup_width, row_size); |
231 } | 228 } |
232 | 229 |
233 return popup_width; | 230 return popup_width; |
234 } | 231 } |
235 | 232 |
236 int AutofillPopupControllerImpl::GetPopupRequiredHeight() { | 233 int AutofillPopupControllerImpl::GetPopupRequiredHeight() { |
237 int popup_height = 0; | 234 int popup_height = 0; |
238 | 235 |
239 for (size_t i = 0; i < autofill_unique_ids().size(); ++i) { | 236 for (size_t i = 0; i < identifiers().size(); ++i) { |
240 popup_height += GetRowHeightFromId(autofill_unique_ids()[i]); | 237 popup_height += GetRowHeightFromId(identifiers()[i]); |
241 } | 238 } |
242 | 239 |
243 return popup_height; | 240 return popup_height; |
244 } | 241 } |
245 #endif // !defined(OS_ANDROID) | 242 #endif // !defined(OS_ANDROID) |
246 | 243 |
247 int AutofillPopupControllerImpl::GetRowHeightFromId(int unique_id) { | 244 gfx::Rect AutofillPopupControllerImpl::GetRowBounds(size_t index) { |
248 if (unique_id == WebAutofillClient::MenuItemIDSeparator) | |
249 return kSeparatorHeight; | |
250 | |
251 return kRowHeight; | |
252 } | |
253 | |
254 gfx::Rect AutofillPopupControllerImpl::GetRectForRow(size_t row, int width) { | |
255 int top = 0; | 245 int top = 0; |
256 for (size_t i = 0; i < row; ++i) { | 246 for (size_t i = 0; i < index; ++i) { |
257 top += GetRowHeightFromId(autofill_unique_ids()[i]); | 247 top += GetRowHeightFromId(identifiers()[i]); |
258 } | 248 } |
259 | 249 |
260 return gfx::Rect( | 250 return gfx::Rect( |
261 0, top, width, GetRowHeightFromId(autofill_unique_ids()[row])); | 251 0, |
| 252 top, |
| 253 popup_bounds_.width(), |
| 254 GetRowHeightFromId(identifiers()[index])); |
262 } | 255 } |
263 | 256 |
264 void AutofillPopupControllerImpl::SetPopupBounds(const gfx::Rect& bounds) { | 257 void AutofillPopupControllerImpl::SetPopupBounds(const gfx::Rect& bounds) { |
265 popup_bounds_ = bounds; | 258 popup_bounds_ = bounds; |
266 UpdateBoundsAndRedrawPopup(); | 259 UpdateBoundsAndRedrawPopup(); |
267 } | 260 } |
268 | 261 |
269 const gfx::Rect& AutofillPopupControllerImpl::popup_bounds() const { | 262 const gfx::Rect& AutofillPopupControllerImpl::popup_bounds() const { |
270 return popup_bounds_; | 263 return popup_bounds_; |
271 } | 264 } |
272 | 265 |
273 gfx::NativeView AutofillPopupControllerImpl::container_view() const { | 266 gfx::NativeView AutofillPopupControllerImpl::container_view() const { |
274 return container_view_; | 267 return container_view_; |
275 } | 268 } |
276 | 269 |
277 const gfx::Rect& AutofillPopupControllerImpl::element_bounds() const { | 270 const gfx::Rect& AutofillPopupControllerImpl::element_bounds() const { |
278 return element_bounds_; | 271 return element_bounds_; |
279 } | 272 } |
280 | 273 |
281 const std::vector<string16>& AutofillPopupControllerImpl:: | 274 const std::vector<string16>& AutofillPopupControllerImpl::names() const { |
282 autofill_values() const { | 275 return names_; |
283 return autofill_values_; | |
284 } | 276 } |
285 | 277 |
286 const std::vector<string16>& AutofillPopupControllerImpl:: | 278 const std::vector<string16>& AutofillPopupControllerImpl::subtexts() const { |
287 autofill_labels() const { | 279 return subtexts_; |
288 return autofill_labels_; | |
289 } | 280 } |
290 | 281 |
291 const std::vector<string16>& AutofillPopupControllerImpl:: | 282 const std::vector<string16>& AutofillPopupControllerImpl::icons() const { |
292 autofill_icons() const { | 283 return icons_; |
293 return autofill_icons_; | |
294 } | 284 } |
295 | 285 |
296 const std::vector<int>& AutofillPopupControllerImpl:: | 286 const std::vector<int>& AutofillPopupControllerImpl::identifiers() const { |
297 autofill_unique_ids() const { | 287 return identifiers_; |
298 return autofill_unique_ids_; | |
299 } | 288 } |
300 | 289 |
301 #if !defined(OS_ANDROID) | 290 #if !defined(OS_ANDROID) |
302 const gfx::Font& AutofillPopupControllerImpl::label_font() const { | 291 const gfx::Font& AutofillPopupControllerImpl::name_font() const { |
303 return label_font_; | 292 return name_font_; |
304 } | 293 } |
305 | 294 |
306 const gfx::Font& AutofillPopupControllerImpl::value_font() const { | 295 const gfx::Font& AutofillPopupControllerImpl::subtext_font() const { |
307 return value_font_; | 296 return subtext_font_; |
308 } | 297 } |
309 #endif | 298 #endif |
310 | 299 |
311 int AutofillPopupControllerImpl::selected_line() const { | 300 int AutofillPopupControllerImpl::selected_line() const { |
312 return selected_line_; | 301 return selected_line_; |
313 } | 302 } |
314 | 303 |
315 bool AutofillPopupControllerImpl::delete_icon_hovered() const { | 304 bool AutofillPopupControllerImpl::delete_icon_hovered() const { |
316 return delete_icon_hovered_; | 305 return delete_icon_hovered_; |
317 } | 306 } |
318 | 307 |
319 bool AutofillPopupControllerImpl::HandleKeyPressEvent( | 308 bool AutofillPopupControllerImpl::HandleKeyPressEvent( |
320 const content::NativeWebKeyboardEvent& event) { | 309 const content::NativeWebKeyboardEvent& event) { |
321 switch (event.windowsKeyCode) { | 310 switch (event.windowsKeyCode) { |
322 case ui::VKEY_UP: | 311 case ui::VKEY_UP: |
323 SelectPreviousLine(); | 312 SelectPreviousLine(); |
324 return true; | 313 return true; |
325 case ui::VKEY_DOWN: | 314 case ui::VKEY_DOWN: |
326 SelectNextLine(); | 315 SelectNextLine(); |
327 return true; | 316 return true; |
328 case ui::VKEY_PRIOR: // Page up. | 317 case ui::VKEY_PRIOR: // Page up. |
329 SetSelectedLine(0); | 318 SetSelectedLine(0); |
330 return true; | 319 return true; |
331 case ui::VKEY_NEXT: // Page down. | 320 case ui::VKEY_NEXT: // Page down. |
332 SetSelectedLine(autofill_values().size() - 1); | 321 SetSelectedLine(names().size() - 1); |
333 return true; | 322 return true; |
334 case ui::VKEY_ESCAPE: | 323 case ui::VKEY_ESCAPE: |
335 HideInternal(); | 324 HideInternal(); |
336 return true; | 325 return true; |
337 case ui::VKEY_DELETE: | 326 case ui::VKEY_DELETE: |
338 return (event.modifiers & content::NativeWebKeyboardEvent::ShiftKey) && | 327 return (event.modifiers & content::NativeWebKeyboardEvent::ShiftKey) && |
339 RemoveSelectedLine(); | 328 RemoveSelectedLine(); |
340 case ui::VKEY_RETURN: | 329 case ui::VKEY_RETURN: |
341 return AcceptSelectedLine(); | 330 return AcceptSelectedLine(); |
342 default: | 331 default: |
(...skipping 17 matching lines...) Expand all Loading... |
360 return; | 349 return; |
361 | 350 |
362 if (selected_line_ != kNoSelection) | 351 if (selected_line_ != kNoSelection) |
363 InvalidateRow(selected_line_); | 352 InvalidateRow(selected_line_); |
364 | 353 |
365 if (selected_line != kNoSelection) | 354 if (selected_line != kNoSelection) |
366 InvalidateRow(selected_line); | 355 InvalidateRow(selected_line); |
367 | 356 |
368 selected_line_ = selected_line; | 357 selected_line_ = selected_line; |
369 | 358 |
370 if (selected_line_ != kNoSelection) { | 359 if (selected_line_ != kNoSelection) |
371 delegate_->SelectAutofillSuggestion( | 360 delegate_->DidSelectSuggestion(identifiers_[selected_line_]); |
372 autofill_unique_ids_[selected_line_]); | |
373 } | |
374 } | 361 } |
375 | 362 |
376 void AutofillPopupControllerImpl::SelectNextLine() { | 363 void AutofillPopupControllerImpl::SelectNextLine() { |
377 int new_selected_line = selected_line_ + 1; | 364 int new_selected_line = selected_line_ + 1; |
378 | 365 |
379 // Skip over any lines that can't be selected. | 366 // Skip over any lines that can't be selected. |
380 while (static_cast<size_t>(new_selected_line) < autofill_values_.size() && | 367 while (static_cast<size_t>(new_selected_line) < names_.size() && |
381 !CanAccept(autofill_unique_ids()[new_selected_line])) { | 368 !CanAccept(identifiers()[new_selected_line])) { |
382 ++new_selected_line; | 369 ++new_selected_line; |
383 } | 370 } |
384 | 371 |
385 if (new_selected_line == static_cast<int>(autofill_values_.size())) | 372 if (new_selected_line == static_cast<int>(names_.size())) |
386 new_selected_line = 0; | 373 new_selected_line = 0; |
387 | 374 |
388 SetSelectedLine(new_selected_line); | 375 SetSelectedLine(new_selected_line); |
389 } | 376 } |
390 | 377 |
391 void AutofillPopupControllerImpl::SelectPreviousLine() { | 378 void AutofillPopupControllerImpl::SelectPreviousLine() { |
392 int new_selected_line = selected_line_ - 1; | 379 int new_selected_line = selected_line_ - 1; |
393 | 380 |
394 // Skip over any lines that can't be selected. | 381 // Skip over any lines that can't be selected. |
395 while (new_selected_line > kNoSelection && | 382 while (new_selected_line > kNoSelection && |
396 !CanAccept(autofill_unique_ids()[new_selected_line])) { | 383 !CanAccept(identifiers()[new_selected_line])) { |
397 --new_selected_line; | 384 --new_selected_line; |
398 } | 385 } |
399 | 386 |
400 if (new_selected_line <= kNoSelection) | 387 if (new_selected_line <= kNoSelection) |
401 new_selected_line = autofill_values_.size() - 1; | 388 new_selected_line = names_.size() - 1; |
402 | 389 |
403 SetSelectedLine(new_selected_line); | 390 SetSelectedLine(new_selected_line); |
404 } | 391 } |
405 | 392 |
406 bool AutofillPopupControllerImpl::AcceptSelectedLine() { | 393 bool AutofillPopupControllerImpl::AcceptSelectedLine() { |
407 if (selected_line_ == kNoSelection) | 394 if (selected_line_ == kNoSelection) |
408 return false; | 395 return false; |
409 | 396 |
410 DCHECK_GE(selected_line_, 0); | 397 DCHECK_GE(selected_line_, 0); |
411 DCHECK_LT(selected_line_, static_cast<int>(autofill_values_.size())); | 398 DCHECK_LT(selected_line_, static_cast<int>(names_.size())); |
412 | 399 |
413 if (!CanAccept(autofill_unique_ids_[selected_line_])) | 400 if (!CanAccept(identifiers_[selected_line_])) |
414 return false; | 401 return false; |
415 | 402 |
416 return AcceptAutofillSuggestion( | 403 AcceptSuggestion(selected_line_); |
417 autofill_values_[selected_line_], | 404 return true; |
418 autofill_unique_ids_[selected_line_], | |
419 selected_line_); | |
420 } | 405 } |
421 | 406 |
422 bool AutofillPopupControllerImpl::RemoveSelectedLine() { | 407 bool AutofillPopupControllerImpl::RemoveSelectedLine() { |
423 if (selected_line_ == kNoSelection) | 408 if (selected_line_ == kNoSelection) |
424 return false; | 409 return false; |
425 | 410 |
426 DCHECK_GE(selected_line_, 0); | 411 DCHECK_GE(selected_line_, 0); |
427 DCHECK_LT(selected_line_, static_cast<int>(autofill_values_.size())); | 412 DCHECK_LT(selected_line_, static_cast<int>(names_.size())); |
428 | 413 |
429 if (!CanDelete(autofill_unique_ids_[selected_line_])) | 414 if (!CanDelete(selected_line_)) |
430 return false; | 415 return false; |
431 | 416 |
432 if (autofill_unique_ids_[selected_line_] > 0) { | 417 delegate_->RemoveSuggestion(names_[selected_line_], |
433 delegate_->RemoveAutofillProfileOrCreditCard( | 418 identifiers_[selected_line_]); |
434 autofill_unique_ids_[selected_line_]); | |
435 } else { | |
436 delegate_->RemoveAutocompleteEntry( | |
437 autofill_values_[selected_line_]); | |
438 } | |
439 | 419 |
440 // Remove the deleted element. | 420 // Remove the deleted element. |
441 autofill_values_.erase(autofill_values_.begin() + selected_line_); | 421 names_.erase(names_.begin() + selected_line_); |
442 autofill_labels_.erase(autofill_labels_.begin() + selected_line_); | 422 subtexts_.erase(subtexts_.begin() + selected_line_); |
443 autofill_icons_.erase(autofill_icons_.begin() + selected_line_); | 423 icons_.erase(icons_.begin() + selected_line_); |
444 autofill_unique_ids_.erase(autofill_unique_ids_.begin() + selected_line_); | 424 identifiers_.erase(identifiers_.begin() + selected_line_); |
445 | 425 |
446 SetSelectedLine(kNoSelection); | 426 SetSelectedLine(kNoSelection); |
447 | 427 |
448 if (HasAutofillEntries()) { | 428 if (HasAutofillEntries()) { |
449 delegate_->ClearPreviewedForm(); | 429 delegate_->ClearPreviewedForm(); |
450 UpdateBoundsAndRedrawPopup(); | 430 UpdateBoundsAndRedrawPopup(); |
451 } else { | 431 } else { |
452 HideInternal(); | 432 HideInternal(); |
453 } | 433 } |
454 | 434 |
455 return true; | 435 return true; |
456 } | 436 } |
457 | 437 |
458 int AutofillPopupControllerImpl::LineFromY(int y) { | 438 int AutofillPopupControllerImpl::LineFromY(int y) { |
459 int current_height = 0; | 439 int current_height = 0; |
460 | 440 |
461 for (size_t i = 0; i < autofill_unique_ids().size(); ++i) { | 441 for (size_t i = 0; i < identifiers().size(); ++i) { |
462 current_height += GetRowHeightFromId(autofill_unique_ids()[i]); | 442 current_height += GetRowHeightFromId(identifiers()[i]); |
463 | 443 |
464 if (y <= current_height) | 444 if (y <= current_height) |
465 return i; | 445 return i; |
466 } | 446 } |
467 | 447 |
468 // The y value goes beyond the popup so stop the selection at the last line. | 448 // The y value goes beyond the popup so stop the selection at the last line. |
469 return autofill_unique_ids().size() - 1; | 449 return identifiers().size() - 1; |
| 450 } |
| 451 |
| 452 int AutofillPopupControllerImpl::GetRowHeightFromId(int identifier) { |
| 453 if (identifier == WebAutofillClient::MenuItemIDSeparator) |
| 454 return kSeparatorHeight; |
| 455 |
| 456 return kRowHeight; |
470 } | 457 } |
471 | 458 |
472 bool AutofillPopupControllerImpl::DeleteIconIsUnder(int x, int y) { | 459 bool AutofillPopupControllerImpl::DeleteIconIsUnder(int x, int y) { |
473 #if defined(OS_ANDROID) | 460 #if defined(OS_ANDROID) |
474 return false; | 461 return false; |
475 #else | 462 #else |
476 if (!CanDelete(selected_line())) | 463 if (!CanDelete(selected_line())) |
477 return false; | 464 return false; |
478 | 465 |
479 int row_start_y = 0; | 466 int row_start_y = 0; |
480 for (int i = 0; i < selected_line(); ++i) { | 467 for (int i = 0; i < selected_line(); ++i) { |
481 row_start_y += GetRowHeightFromId(autofill_unique_ids()[i]); | 468 row_start_y += GetRowHeightFromId(identifiers()[i]); |
482 } | 469 } |
483 | 470 |
484 gfx::Rect delete_icon_bounds = gfx::Rect( | 471 gfx::Rect delete_icon_bounds = gfx::Rect( |
485 GetPopupRequiredWidth() - kDeleteIconWidth - kIconPadding, | 472 GetPopupRequiredWidth() - kDeleteIconWidth - kIconPadding, |
486 row_start_y + ((kRowHeight - kDeleteIconHeight) / 2), | 473 row_start_y + ((kRowHeight - kDeleteIconHeight) / 2), |
487 kDeleteIconWidth, | 474 kDeleteIconWidth, |
488 kDeleteIconHeight); | 475 kDeleteIconHeight); |
489 | 476 |
490 return delete_icon_bounds.Contains(x, y); | 477 return delete_icon_bounds.Contains(x, y); |
491 #endif | 478 #endif |
492 } | 479 } |
493 | 480 |
494 bool AutofillPopupControllerImpl::CanAccept(int id) { | 481 bool AutofillPopupControllerImpl::CanAccept(int id) { |
495 return id != WebAutofillClient::MenuItemIDSeparator; | 482 return id != WebAutofillClient::MenuItemIDSeparator && |
| 483 id != WebAutofillClient::MenuItemIDWarningMessage; |
496 } | 484 } |
497 | 485 |
498 bool AutofillPopupControllerImpl::HasAutofillEntries() { | 486 bool AutofillPopupControllerImpl::HasAutofillEntries() { |
499 return autofill_values_.size() != 0 && | 487 return names_.size() != 0 && |
500 (autofill_unique_ids_[0] > 0 || | 488 (identifiers_[0] > 0 || |
501 autofill_unique_ids_[0] == | 489 identifiers_[0] == |
502 WebAutofillClient::MenuItemIDAutocompleteEntry || | 490 WebAutofillClient::MenuItemIDAutocompleteEntry || |
503 autofill_unique_ids_[0] == WebAutofillClient::MenuItemIDPasswordEntry || | 491 identifiers_[0] == WebAutofillClient::MenuItemIDPasswordEntry || |
504 autofill_unique_ids_[0] == WebAutofillClient::MenuItemIDDataListEntry); | 492 identifiers_[0] == WebAutofillClient::MenuItemIDDataListEntry); |
505 } | 493 } |
506 | 494 |
507 void AutofillPopupControllerImpl::ShowView() { | 495 void AutofillPopupControllerImpl::ShowView() { |
508 view_->Show(); | 496 view_->Show(); |
509 } | 497 } |
510 | 498 |
511 void AutofillPopupControllerImpl::InvalidateRow(size_t row) { | 499 void AutofillPopupControllerImpl::InvalidateRow(size_t row) { |
512 view_->InvalidateRow(row); | 500 view_->InvalidateRow(row); |
513 } | 501 } |
OLD | NEW |