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/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // Elide the name and label strings so that the popup fits in the available | 107 // Elide the name and label strings so that the popup fits in the available |
108 // space. | 108 // space. |
109 for (size_t i = 0; i < suggestions_.size(); ++i) { | 109 for (size_t i = 0; i < suggestions_.size(); ++i) { |
110 bool has_label = !suggestions_[i].label.empty(); | 110 bool has_label = !suggestions_[i].label.empty(); |
111 ElideValueAndLabelForRow( | 111 ElideValueAndLabelForRow( |
112 i, layout_model_.GetAvailableWidthForRow(i, has_label)); | 112 i, layout_model_.GetAvailableWidthForRow(i, has_label)); |
113 } | 113 } |
114 #endif | 114 #endif |
115 | 115 |
116 if (just_created) { | 116 if (just_created) { |
117 ShowView(); | 117 view_->Show(); |
118 } else { | 118 } else { |
119 UpdateBoundsAndRedrawPopup(); | 119 OnSuggestionsChanged(); |
120 } | 120 } |
121 | 121 |
122 controller_common_->RegisterKeyPressCallback(); | 122 controller_common_->RegisterKeyPressCallback(); |
123 delegate_->OnPopupShown(); | 123 delegate_->OnPopupShown(); |
124 | 124 |
125 DCHECK_EQ(suggestions_.size(), elided_values_.size()); | 125 DCHECK_EQ(suggestions_.size(), elided_values_.size()); |
126 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); | 126 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); |
127 } | 127 } |
128 | 128 |
129 void AutofillPopupControllerImpl::UpdateDataListValues( | 129 void AutofillPopupControllerImpl::UpdateDataListValues( |
(...skipping 16 matching lines...) Expand all Loading... |
146 if (values.empty()) { | 146 if (values.empty()) { |
147 if (!suggestions_.empty() && | 147 if (!suggestions_.empty() && |
148 suggestions_[0].frontend_id == POPUP_ITEM_ID_SEPARATOR) { | 148 suggestions_[0].frontend_id == POPUP_ITEM_ID_SEPARATOR) { |
149 suggestions_.erase(suggestions_.begin()); | 149 suggestions_.erase(suggestions_.begin()); |
150 elided_values_.erase(elided_values_.begin()); | 150 elided_values_.erase(elided_values_.begin()); |
151 elided_labels_.erase(elided_labels_.begin()); | 151 elided_labels_.erase(elided_labels_.begin()); |
152 } | 152 } |
153 | 153 |
154 // The popup contents have changed, so either update the bounds or hide it. | 154 // The popup contents have changed, so either update the bounds or hide it. |
155 if (HasSuggestions()) | 155 if (HasSuggestions()) |
156 UpdateBoundsAndRedrawPopup(); | 156 OnSuggestionsChanged(); |
157 else | 157 else |
158 Hide(); | 158 Hide(); |
159 | 159 |
160 return; | 160 return; |
161 } | 161 } |
162 | 162 |
163 // Add a separator if there are any other values. | 163 // Add a separator if there are any other values. |
164 if (!suggestions_.empty() && | 164 if (!suggestions_.empty() && |
165 suggestions_[0].frontend_id != POPUP_ITEM_ID_SEPARATOR) { | 165 suggestions_[0].frontend_id != POPUP_ITEM_ID_SEPARATOR) { |
166 suggestions_.insert(suggestions_.begin(), autofill::Suggestion()); | 166 suggestions_.insert(suggestions_.begin(), autofill::Suggestion()); |
(...skipping 11 matching lines...) Expand all Loading... |
178 for (size_t i = 0; i < values.size(); i++) { | 178 for (size_t i = 0; i < values.size(); i++) { |
179 suggestions_[i].value = values[i]; | 179 suggestions_[i].value = values[i]; |
180 suggestions_[i].label = labels[i]; | 180 suggestions_[i].label = labels[i]; |
181 suggestions_[i].frontend_id = POPUP_ITEM_ID_DATALIST_ENTRY; | 181 suggestions_[i].frontend_id = POPUP_ITEM_ID_DATALIST_ENTRY; |
182 | 182 |
183 // TODO(brettw) it looks like these should be elided. | 183 // TODO(brettw) it looks like these should be elided. |
184 elided_values_[i] = values[i]; | 184 elided_values_[i] = values[i]; |
185 elided_labels_[i] = labels[i]; | 185 elided_labels_[i] = labels[i]; |
186 } | 186 } |
187 | 187 |
188 UpdateBoundsAndRedrawPopup(); | 188 OnSuggestionsChanged(); |
189 DCHECK_EQ(suggestions_.size(), elided_values_.size()); | 189 DCHECK_EQ(suggestions_.size(), elided_values_.size()); |
190 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); | 190 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); |
191 } | 191 } |
192 | 192 |
193 void AutofillPopupControllerImpl::Hide() { | 193 void AutofillPopupControllerImpl::Hide() { |
194 controller_common_->RemoveKeyPressCallback(); | 194 controller_common_->RemoveKeyPressCallback(); |
195 if (delegate_) | 195 if (delegate_) |
196 delegate_->OnPopupHidden(); | 196 delegate_->OnPopupHidden(); |
197 | 197 |
198 if (view_) | 198 if (view_) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 // location. | 238 // location. |
239 AcceptSelectedLine(); | 239 AcceptSelectedLine(); |
240 return false; | 240 return false; |
241 case ui::VKEY_RETURN: | 241 case ui::VKEY_RETURN: |
242 return AcceptSelectedLine(); | 242 return AcceptSelectedLine(); |
243 default: | 243 default: |
244 return false; | 244 return false; |
245 } | 245 } |
246 } | 246 } |
247 | 247 |
248 void AutofillPopupControllerImpl::UpdateBoundsAndRedrawPopup() { | 248 void AutofillPopupControllerImpl::OnSuggestionsChanged() { |
249 #if !defined(OS_ANDROID) | 249 #if !defined(OS_ANDROID) |
250 // TODO(csharp): Since UpdatePopupBounds can change the position of the popup, | 250 // TODO(csharp): Since UpdatePopupBounds can change the position of the popup, |
251 // the popup could end up jumping from above the element to below it. | 251 // the popup could end up jumping from above the element to below it. |
252 // It is unclear if it is better to keep the popup where it was, or if it | 252 // It is unclear if it is better to keep the popup where it was, or if it |
253 // should try and move to its desired position. | 253 // should try and move to its desired position. |
254 layout_model_.UpdatePopupBounds(); | 254 layout_model_.UpdatePopupBounds(); |
255 #endif | 255 #endif |
256 | 256 |
257 // Platform-specific draw call. | 257 // Platform-specific draw call. |
258 view_->UpdateBoundsAndRedrawPopup(); | 258 view_->OnSuggestionsChanged(); |
259 } | 259 } |
260 | 260 |
261 void AutofillPopupControllerImpl::SetSelectionAtPoint(const gfx::Point& point) { | 261 void AutofillPopupControllerImpl::SetSelectionAtPoint(const gfx::Point& point) { |
262 SetSelectedLine(layout_model_.LineFromY(point.y())); | 262 SetSelectedLine(layout_model_.LineFromY(point.y())); |
263 } | 263 } |
264 | 264 |
265 bool AutofillPopupControllerImpl::AcceptSelectedLine() { | 265 bool AutofillPopupControllerImpl::AcceptSelectedLine() { |
266 if (selected_line_ == kNoSelection) | 266 if (selected_line_ == kNoSelection) |
267 return false; | 267 return false; |
268 | 268 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 | 359 |
360 // Remove the deleted element. | 360 // Remove the deleted element. |
361 suggestions_.erase(suggestions_.begin() + list_index); | 361 suggestions_.erase(suggestions_.begin() + list_index); |
362 elided_values_.erase(elided_values_.begin() + list_index); | 362 elided_values_.erase(elided_values_.begin() + list_index); |
363 elided_labels_.erase(elided_labels_.begin() + list_index); | 363 elided_labels_.erase(elided_labels_.begin() + list_index); |
364 | 364 |
365 SetSelectedLine(kNoSelection); | 365 SetSelectedLine(kNoSelection); |
366 | 366 |
367 if (HasSuggestions()) { | 367 if (HasSuggestions()) { |
368 delegate_->ClearPreviewedForm(); | 368 delegate_->ClearPreviewedForm(); |
369 UpdateBoundsAndRedrawPopup(); | 369 OnSuggestionsChanged(); |
370 } else { | 370 } else { |
371 Hide(); | 371 Hide(); |
372 } | 372 } |
373 | 373 |
374 return true; | 374 return true; |
375 } | 375 } |
376 | 376 |
377 ui::NativeTheme::ColorId | 377 ui::NativeTheme::ColorId |
378 AutofillPopupControllerImpl::GetBackgroundColorIDForRow(int index) const { | 378 AutofillPopupControllerImpl::GetBackgroundColorIDForRow(int index) const { |
379 return index == selected_line_ ? | 379 return index == selected_line_ ? |
380 ui::NativeTheme::kColorId_ResultsTableHoveredBackground : | 380 ui::NativeTheme::kColorId_ResultsTableHoveredBackground : |
381 ui::NativeTheme::kColorId_ResultsTableNormalBackground; | 381 ui::NativeTheme::kColorId_ResultsTableNormalBackground; |
382 } | 382 } |
383 | 383 |
384 int AutofillPopupControllerImpl::selected_line() const { | 384 int AutofillPopupControllerImpl::selected_line() const { |
385 return selected_line_; | 385 return selected_line_; |
386 } | 386 } |
387 | 387 |
388 const AutofillPopupLayoutModel& AutofillPopupControllerImpl::layout_model() | 388 const AutofillPopupLayoutModel& AutofillPopupControllerImpl::layout_model() |
389 const { | 389 const { |
390 return layout_model_; | 390 return layout_model_; |
391 } | 391 } |
392 | 392 |
393 void AutofillPopupControllerImpl::SetSelectedLine(int selected_line) { | 393 void AutofillPopupControllerImpl::SetSelectedLine(int selected_line) { |
394 if (selected_line_ == selected_line) | 394 if (selected_line_ == selected_line) |
395 return; | 395 return; |
396 | 396 |
397 if (selected_line_ != kNoSelection && | 397 if (selected_line_ != kNoSelection && |
398 static_cast<size_t>(selected_line_) < suggestions_.size()) | 398 static_cast<size_t>(selected_line_) < suggestions_.size()) { |
399 InvalidateRow(selected_line_); | 399 DCHECK_GE(selected_line_, 0); |
| 400 view_->InvalidateRow(selected_line_, false /* is_selected */); |
| 401 } |
400 | 402 |
401 if (selected_line != kNoSelection) { | 403 if (selected_line != kNoSelection) { |
402 InvalidateRow(selected_line); | 404 DCHECK_GE(selected_line, 0); |
| 405 DCHECK_LT(static_cast<size_t>(selected_line), suggestions_.size()); |
| 406 const bool is_selected = CanAccept(suggestions_[selected_line].frontend_id); |
| 407 view_->InvalidateRow(selected_line, is_selected); |
403 | 408 |
404 if (!CanAccept(suggestions_[selected_line].frontend_id)) | 409 if (!is_selected) |
405 selected_line = kNoSelection; | 410 selected_line = kNoSelection; |
406 } | 411 } |
407 | 412 |
408 selected_line_ = selected_line; | 413 selected_line_ = selected_line; |
409 | 414 |
410 if (selected_line_ != kNoSelection) { | 415 if (selected_line_ != kNoSelection) { |
411 delegate_->DidSelectSuggestion(suggestions_[selected_line_].value, | 416 delegate_->DidSelectSuggestion(suggestions_[selected_line_].value, |
412 suggestions_[selected_line_].frontend_id); | 417 suggestions_[selected_line_].frontend_id); |
413 } else { | 418 } else { |
414 delegate_->ClearPreviewedForm(); | 419 delegate_->ClearPreviewedForm(); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 const std::vector<autofill::Suggestion>& suggestions) { | 480 const std::vector<autofill::Suggestion>& suggestions) { |
476 suggestions_ = suggestions; | 481 suggestions_ = suggestions; |
477 elided_values_.resize(suggestions.size()); | 482 elided_values_.resize(suggestions.size()); |
478 elided_labels_.resize(suggestions.size()); | 483 elided_labels_.resize(suggestions.size()); |
479 for (size_t i = 0; i < suggestions.size(); i++) { | 484 for (size_t i = 0; i < suggestions.size(); i++) { |
480 elided_values_[i] = suggestions[i].value; | 485 elided_values_[i] = suggestions[i].value; |
481 elided_labels_[i] = suggestions[i].label; | 486 elided_labels_[i] = suggestions[i].label; |
482 } | 487 } |
483 } | 488 } |
484 | 489 |
485 void AutofillPopupControllerImpl::ShowView() { | |
486 view_->Show(); | |
487 } | |
488 | |
489 void AutofillPopupControllerImpl::InvalidateRow(size_t row) { | |
490 DCHECK(0 <= row); | |
491 DCHECK(row < suggestions_.size()); | |
492 view_->InvalidateRow(row); | |
493 } | |
494 | |
495 WeakPtr<AutofillPopupControllerImpl> AutofillPopupControllerImpl::GetWeakPtr() { | 490 WeakPtr<AutofillPopupControllerImpl> AutofillPopupControllerImpl::GetWeakPtr() { |
496 return weak_ptr_factory_.GetWeakPtr(); | 491 return weak_ptr_factory_.GetWeakPtr(); |
497 } | 492 } |
498 | 493 |
499 #if !defined(OS_ANDROID) | 494 #if !defined(OS_ANDROID) |
500 void AutofillPopupControllerImpl::ElideValueAndLabelForRow( | 495 void AutofillPopupControllerImpl::ElideValueAndLabelForRow( |
501 size_t row, | 496 size_t row, |
502 int available_width) { | 497 int available_width) { |
503 int value_width = gfx::GetStringWidth( | 498 int value_width = gfx::GetStringWidth( |
504 suggestions_[row].value, layout_model_.GetValueFontListForRow(row)); | 499 suggestions_[row].value, layout_model_.GetValueFontListForRow(row)); |
(...skipping 23 matching lines...) Expand all Loading... |
528 // Don't clear view_, because otherwise the popup will have to get regenerated | 523 // Don't clear view_, because otherwise the popup will have to get regenerated |
529 // and this will cause flickering. | 524 // and this will cause flickering. |
530 suggestions_.clear(); | 525 suggestions_.clear(); |
531 elided_values_.clear(); | 526 elided_values_.clear(); |
532 elided_labels_.clear(); | 527 elided_labels_.clear(); |
533 | 528 |
534 selected_line_ = kNoSelection; | 529 selected_line_ = kNoSelection; |
535 } | 530 } |
536 | 531 |
537 } // namespace autofill | 532 } // namespace autofill |
OLD | NEW |