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

Side by Side Diff: chrome/browser/ui/autofill/autofill_popup_controller_impl.cc

Issue 1570783003: [Autofill] Move functions from the AutofillPopupController to AutofillPopupLayoutModel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed nit Created 4 years, 11 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 "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"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "chrome/browser/ui/autofill/autofill_popup_view.h" 15 #include "chrome/browser/ui/autofill/autofill_popup_view.h"
16 #include "chrome/browser/ui/autofill/popup_constants.h"
17 #include "components/autofill/core/browser/autofill_popup_delegate.h" 16 #include "components/autofill/core/browser/autofill_popup_delegate.h"
18 #include "components/autofill/core/browser/popup_item_ids.h" 17 #include "components/autofill/core/browser/popup_item_ids.h"
19 #include "components/autofill/core/browser/suggestion.h" 18 #include "components/autofill/core/browser/suggestion.h"
20 #include "components/autofill/core/common/autofill_util.h"
21 #include "content/public/browser/native_web_keyboard_event.h" 19 #include "content/public/browser/native_web_keyboard_event.h"
22 #include "grit/components_scaled_resources.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/events/event.h" 20 #include "ui/events/event.h"
25 #include "ui/gfx/geometry/rect_conversions.h"
26 #include "ui/gfx/geometry/vector2d.h"
27 #include "ui/gfx/screen.h"
28 #include "ui/gfx/text_elider.h" 21 #include "ui/gfx/text_elider.h"
29 #include "ui/gfx/text_utils.h" 22 #include "ui/gfx/text_utils.h"
30 23
31 using base::WeakPtr; 24 using base::WeakPtr;
32 25
33 namespace autofill { 26 namespace autofill {
34 namespace { 27 namespace {
35 28
36 // Used to indicate that no line is currently selected by the user. 29 // Used to indicate that no line is currently selected by the user.
37 const int kNoSelection = -1; 30 const int kNoSelection = -1;
38 31
39 // The vertical height of each row in pixels.
40 const size_t kRowHeight = 24;
41
42 // The vertical height of a separator in pixels.
43 const size_t kSeparatorHeight = 1;
44 32
45 #if !defined(OS_ANDROID) 33 #if !defined(OS_ANDROID)
46 // Size difference between name and label in pixels. 34 // Size difference between name and label in pixels.
47 const int kLabelFontSizeDelta = -2; 35 const int kLabelFontSizeDelta = -2;
48
49 const size_t kNamePadding = AutofillPopupView::kNamePadding;
50 const size_t kIconPadding = AutofillPopupView::kIconPadding;
51 const size_t kEndPadding = AutofillPopupView::kEndPadding;
52 #endif 36 #endif
53 37
54 struct DataResource {
55 const char* name;
56 int id;
57 };
58
59 const DataResource kDataResources[] = {
60 { "americanExpressCC", IDR_AUTOFILL_CC_AMEX },
61 { "dinersCC", IDR_AUTOFILL_CC_GENERIC },
62 { "discoverCC", IDR_AUTOFILL_CC_DISCOVER },
63 { "genericCC", IDR_AUTOFILL_CC_GENERIC },
64 { "jcbCC", IDR_AUTOFILL_CC_GENERIC },
65 { "masterCardCC", IDR_AUTOFILL_CC_MASTERCARD },
66 { "visaCC", IDR_AUTOFILL_CC_VISA },
67 #if defined(OS_ANDROID)
68 { "scanCreditCardIcon", IDR_AUTOFILL_CC_SCAN_NEW },
69 { "settings", IDR_AUTOFILL_SETTINGS },
70 #endif
71 };
72
73 } // namespace 38 } // namespace
74 39
75 // static 40 // static
76 WeakPtr<AutofillPopupControllerImpl> AutofillPopupControllerImpl::GetOrCreate( 41 WeakPtr<AutofillPopupControllerImpl> AutofillPopupControllerImpl::GetOrCreate(
77 WeakPtr<AutofillPopupControllerImpl> previous, 42 WeakPtr<AutofillPopupControllerImpl> previous,
78 WeakPtr<AutofillPopupDelegate> delegate, 43 WeakPtr<AutofillPopupDelegate> delegate,
79 content::WebContents* web_contents, 44 content::WebContents* web_contents,
80 gfx::NativeView container_view, 45 gfx::NativeView container_view,
81 const gfx::RectF& element_bounds, 46 const gfx::RectF& element_bounds,
82 base::i18n::TextDirection text_direction) { 47 base::i18n::TextDirection text_direction) {
(...skipping 19 matching lines...) Expand all
102 base::WeakPtr<AutofillPopupDelegate> delegate, 67 base::WeakPtr<AutofillPopupDelegate> delegate,
103 content::WebContents* web_contents, 68 content::WebContents* web_contents,
104 gfx::NativeView container_view, 69 gfx::NativeView container_view,
105 const gfx::RectF& element_bounds, 70 const gfx::RectF& element_bounds,
106 base::i18n::TextDirection text_direction) 71 base::i18n::TextDirection text_direction)
107 : controller_common_(new PopupControllerCommon(element_bounds, 72 : controller_common_(new PopupControllerCommon(element_bounds,
108 text_direction, 73 text_direction,
109 container_view, 74 container_view,
110 web_contents)), 75 web_contents)),
111 view_(NULL), 76 view_(NULL),
77 layout_model_(this),
112 delegate_(delegate), 78 delegate_(delegate),
113 weak_ptr_factory_(this) { 79 weak_ptr_factory_(this) {
114 ClearState(); 80 ClearState();
115 controller_common_->SetKeyPressCallback( 81 controller_common_->SetKeyPressCallback(
116 base::Bind(&AutofillPopupControllerImpl::HandleKeyPressEvent, 82 base::Bind(&AutofillPopupControllerImpl::HandleKeyPressEvent,
117 base::Unretained(this))); 83 base::Unretained(this)));
118 #if !defined(OS_ANDROID) 84 #if !defined(OS_ANDROID)
119 label_font_list_ = value_font_list_.DeriveWithSizeDelta(kLabelFontSizeDelta); 85 label_font_list_ = value_font_list_.DeriveWithSizeDelta(kLabelFontSizeDelta);
120 title_font_list_ = value_font_list_.DeriveWithStyle(gfx::Font::BOLD); 86 title_font_list_ = value_font_list_.DeriveWithStyle(gfx::Font::BOLD);
121 #if defined(OS_MACOSX) 87 #if defined(OS_MACOSX)
122 // There is no italic version of the system font. 88 // There is no italic version of the system font.
123 warning_font_list_ = value_font_list_; 89 warning_font_list_ = value_font_list_;
124 #else 90 #else
125 warning_font_list_ = value_font_list_.DeriveWithStyle(gfx::Font::ITALIC); 91 warning_font_list_ = value_font_list_.DeriveWithStyle(gfx::Font::ITALIC);
126 #endif 92 #endif
127 #endif 93 #endif
128 } 94 }
129 95
130 AutofillPopupControllerImpl::~AutofillPopupControllerImpl() {} 96 AutofillPopupControllerImpl::~AutofillPopupControllerImpl() {}
131 97
132 void AutofillPopupControllerImpl::Show( 98 void AutofillPopupControllerImpl::Show(
133 const std::vector<autofill::Suggestion>& suggestions) { 99 const std::vector<autofill::Suggestion>& suggestions) {
134 SetValues(suggestions); 100 SetValues(suggestions);
135 DCHECK_EQ(suggestions_.size(), elided_values_.size()); 101 DCHECK_EQ(suggestions_.size(), elided_values_.size());
136 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); 102 DCHECK_EQ(suggestions_.size(), elided_labels_.size());
137 103
138 #if !defined(OS_ANDROID) 104 bool just_created = false;
139 // Android displays the long text with ellipsis using the view attributes.
140
141 UpdatePopupBounds();
142 int popup_width = popup_bounds().width();
143
144 // Elide the name and label strings so that the popup fits in the available
145 // space.
146 for (size_t i = 0; i < suggestions_.size(); ++i) {
147 int value_width =
148 gfx::GetStringWidth(suggestions_[i].value, GetValueFontListForRow(i));
149 int label_width =
150 gfx::GetStringWidth(suggestions_[i].label, GetLabelFontList());
151 int total_text_length = value_width + label_width;
152
153 // The line can have no strings if it represents a UI element, such as
154 // a separator line.
155 if (total_text_length == 0)
156 continue;
157
158 int available_width = popup_width - RowWidthWithoutText(i);
159
160 // Each field receives space in proportion to its length.
161 int value_size = available_width * value_width / total_text_length;
162 elided_values_[i] = gfx::ElideText(suggestions_[i].value,
163 GetValueFontListForRow(i),
164 value_size, gfx::ELIDE_TAIL);
165
166 int label_size = available_width * label_width / total_text_length;
167 elided_labels_[i] = gfx::ElideText(suggestions_[i].label,
168 GetLabelFontList(),
169 label_size, gfx::ELIDE_TAIL);
170 }
171 #endif
172
173 if (!view_) { 105 if (!view_) {
174 view_ = AutofillPopupView::Create(this); 106 view_ = AutofillPopupView::Create(this);
175 107
176 // It is possible to fail to create the popup, in this case 108 // It is possible to fail to create the popup, in this case
177 // treat the popup as hiding right away. 109 // treat the popup as hiding right away.
178 if (!view_) { 110 if (!view_) {
179 Hide(); 111 Hide();
180 return; 112 return;
181 } 113 }
114 just_created = true;
115 }
182 116
117 #if !defined(OS_ANDROID)
118 // Android displays the long text with ellipsis using the view attributes.
119
120 layout_model_.UpdatePopupBounds();
121
122 // Elide the name and label strings so that the popup fits in the available
123 // space.
124 for (size_t i = 0; i < suggestions_.size(); ++i) {
125 bool has_label = !suggestions_[i].label.empty();
126 ElideValueAndLabelForRow(
127 i, layout_model_.GetAvailableWidthForRow(i, has_label));
128 }
129 #endif
130
131 if (just_created) {
183 ShowView(); 132 ShowView();
184 } else { 133 } else {
185 UpdateBoundsAndRedrawPopup(); 134 UpdateBoundsAndRedrawPopup();
186 } 135 }
187 136
188 controller_common_->RegisterKeyPressCallback(); 137 controller_common_->RegisterKeyPressCallback();
189 delegate_->OnPopupShown(); 138 delegate_->OnPopupShown();
190 139
191 DCHECK_EQ(suggestions_.size(), elided_values_.size()); 140 DCHECK_EQ(suggestions_.size(), elided_values_.size());
192 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); 141 DCHECK_EQ(suggestions_.size(), elided_labels_.size());
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 return false; 259 return false;
311 } 260 }
312 } 261 }
313 262
314 void AutofillPopupControllerImpl::UpdateBoundsAndRedrawPopup() { 263 void AutofillPopupControllerImpl::UpdateBoundsAndRedrawPopup() {
315 #if !defined(OS_ANDROID) 264 #if !defined(OS_ANDROID)
316 // TODO(csharp): Since UpdatePopupBounds can change the position of the popup, 265 // TODO(csharp): Since UpdatePopupBounds can change the position of the popup,
317 // the popup could end up jumping from above the element to below it. 266 // the popup could end up jumping from above the element to below it.
318 // It is unclear if it is better to keep the popup where it was, or if it 267 // It is unclear if it is better to keep the popup where it was, or if it
319 // should try and move to its desired position. 268 // should try and move to its desired position.
320 UpdatePopupBounds(); 269 layout_model_.UpdatePopupBounds();
321 #endif 270 #endif
322 271
272 // Platform-specific draw call.
323 view_->UpdateBoundsAndRedrawPopup(); 273 view_->UpdateBoundsAndRedrawPopup();
324 } 274 }
325 275
326 void AutofillPopupControllerImpl::SetSelectionAtPoint(const gfx::Point& point) { 276 void AutofillPopupControllerImpl::SetSelectionAtPoint(const gfx::Point& point) {
327 SetSelectedLine(LineFromY(point.y())); 277 SetSelectedLine(layout_model_.LineFromY(point.y()));
328 } 278 }
329 279
330 bool AutofillPopupControllerImpl::AcceptSelectedLine() { 280 bool AutofillPopupControllerImpl::AcceptSelectedLine() {
331 if (selected_line_ == kNoSelection) 281 if (selected_line_ == kNoSelection)
332 return false; 282 return false;
333 283
334 DCHECK_GE(selected_line_, 0); 284 DCHECK_GE(selected_line_, 0);
335 DCHECK_LT(selected_line_, static_cast<int>(GetLineCount())); 285 DCHECK_LT(selected_line_, static_cast<int>(GetLineCount()));
336 286
337 if (!CanAccept(suggestions_[selected_line_].frontend_id)) 287 if (!CanAccept(suggestions_[selected_line_].frontend_id))
338 return false; 288 return false;
339 289
340 AcceptSuggestion(selected_line_); 290 AcceptSuggestion(selected_line_);
341 return true; 291 return true;
342 } 292 }
343 293
344 void AutofillPopupControllerImpl::SelectionCleared() { 294 void AutofillPopupControllerImpl::SelectionCleared() {
345 SetSelectedLine(kNoSelection); 295 SetSelectedLine(kNoSelection);
346 } 296 }
347 297
348 void AutofillPopupControllerImpl::AcceptSuggestion(size_t index) { 298 void AutofillPopupControllerImpl::AcceptSuggestion(size_t index) {
349 const autofill::Suggestion& suggestion = suggestions_[index]; 299 const autofill::Suggestion& suggestion = suggestions_[index];
350 delegate_->DidAcceptSuggestion(suggestion.value, suggestion.frontend_id, 300 delegate_->DidAcceptSuggestion(suggestion.value, suggestion.frontend_id,
351 index); 301 index);
352 } 302 }
353 303
354 int AutofillPopupControllerImpl::GetIconResourceID(
355 const base::string16& resource_name) const {
356 int result = -1;
357 for (size_t i = 0; i < arraysize(kDataResources); ++i) {
358 if (resource_name == base::ASCIIToUTF16(kDataResources[i].name)) {
359 result = kDataResources[i].id;
360 break;
361 }
362 }
363
364 #if defined(OS_ANDROID)
365 if (result == IDR_AUTOFILL_CC_SCAN_NEW && IsKeyboardAccessoryEnabled())
366 result = IDR_AUTOFILL_CC_SCAN_NEW_KEYBOARD_ACCESSORY;
367 #endif // OS_ANDROID
368
369 return result;
370 }
371
372 bool AutofillPopupControllerImpl::IsWarning(size_t index) const { 304 bool AutofillPopupControllerImpl::IsWarning(size_t index) const {
373 return suggestions_[index].frontend_id == POPUP_ITEM_ID_WARNING_MESSAGE; 305 return suggestions_[index].frontend_id == POPUP_ITEM_ID_WARNING_MESSAGE;
374 } 306 }
375 307
376 gfx::Rect AutofillPopupControllerImpl::GetRowBounds(size_t index) { 308 gfx::Rect AutofillPopupControllerImpl::popup_bounds() const {
377 int top = kPopupBorderThickness; 309 return layout_model_.popup_bounds();
378 for (size_t i = 0; i < index; ++i) {
379 top += GetRowHeightFromId(suggestions_[i].frontend_id);
380 }
381
382 return gfx::Rect(
383 kPopupBorderThickness,
384 top,
385 popup_bounds_.width() - 2 * kPopupBorderThickness,
386 GetRowHeightFromId(suggestions_[index].frontend_id));
387 }
388
389 const gfx::Rect& AutofillPopupControllerImpl::popup_bounds() const {
390 return popup_bounds_;
391 } 310 }
392 311
393 content::WebContents* AutofillPopupControllerImpl::web_contents() { 312 content::WebContents* AutofillPopupControllerImpl::web_contents() {
394 return controller_common_->web_contents(); 313 return controller_common_->web_contents();
395 } 314 }
396 315
397 gfx::NativeView AutofillPopupControllerImpl::container_view() { 316 gfx::NativeView AutofillPopupControllerImpl::container_view() {
398 return controller_common_->container_view(); 317 return controller_common_->container_view();
399 } 318 }
400 319
401 const gfx::RectF& AutofillPopupControllerImpl::element_bounds() const { 320 const gfx::RectF& AutofillPopupControllerImpl::element_bounds() const {
402 return controller_common_->element_bounds(); 321 return controller_common_->element_bounds();
403 } 322 }
404 323
405 bool AutofillPopupControllerImpl::IsRTL() const { 324 bool AutofillPopupControllerImpl::IsRTL() const {
406 return controller_common_->is_rtl(); 325 return controller_common_->is_rtl();
407 } 326 }
408 327
328 const std::vector<autofill::Suggestion>
329 AutofillPopupControllerImpl::GetSuggestions() {
330 return suggestions_;
331 }
332
333 #if !defined(OS_ANDROID)
334 int AutofillPopupControllerImpl::GetElidedValueWidthForRow(size_t row) {
335 return gfx::GetStringWidth(GetElidedValueAt(row), value_font_list_);
336 }
337
338 int AutofillPopupControllerImpl::GetElidedLabelWidthForRow(size_t row) {
339 return gfx::GetStringWidth(GetElidedValueAt(row), label_font_list_);
340 }
341 #endif
342
409 size_t AutofillPopupControllerImpl::GetLineCount() const { 343 size_t AutofillPopupControllerImpl::GetLineCount() const {
410 return suggestions_.size(); 344 return suggestions_.size();
411 } 345 }
412 346
413 const autofill::Suggestion& AutofillPopupControllerImpl::GetSuggestionAt( 347 const autofill::Suggestion& AutofillPopupControllerImpl::GetSuggestionAt(
414 size_t row) const { 348 size_t row) const {
415 return suggestions_[row]; 349 return suggestions_[row];
416 } 350 }
417 351
418 const base::string16& AutofillPopupControllerImpl::GetElidedValueAt( 352 const base::string16& AutofillPopupControllerImpl::GetElidedValueAt(
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 405
472 const gfx::FontList& AutofillPopupControllerImpl::GetLabelFontList() const { 406 const gfx::FontList& AutofillPopupControllerImpl::GetLabelFontList() const {
473 return label_font_list_; 407 return label_font_list_;
474 } 408 }
475 #endif 409 #endif
476 410
477 int AutofillPopupControllerImpl::selected_line() const { 411 int AutofillPopupControllerImpl::selected_line() const {
478 return selected_line_; 412 return selected_line_;
479 } 413 }
480 414
415 const AutofillPopupLayoutModel& AutofillPopupControllerImpl::layout_model()
416 const {
417 return layout_model_;
418 }
419
481 void AutofillPopupControllerImpl::SetSelectedLine(int selected_line) { 420 void AutofillPopupControllerImpl::SetSelectedLine(int selected_line) {
482 if (selected_line_ == selected_line) 421 if (selected_line_ == selected_line)
483 return; 422 return;
484 423
485 if (selected_line_ != kNoSelection && 424 if (selected_line_ != kNoSelection &&
486 static_cast<size_t>(selected_line_) < suggestions_.size()) 425 static_cast<size_t>(selected_line_) < suggestions_.size())
487 InvalidateRow(selected_line_); 426 InvalidateRow(selected_line_);
488 427
489 if (selected_line != kNoSelection) { 428 if (selected_line != kNoSelection) {
490 InvalidateRow(selected_line); 429 InvalidateRow(selected_line);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 474
536 bool AutofillPopupControllerImpl::RemoveSelectedLine() { 475 bool AutofillPopupControllerImpl::RemoveSelectedLine() {
537 if (selected_line_ == kNoSelection) 476 if (selected_line_ == kNoSelection)
538 return false; 477 return false;
539 478
540 DCHECK_GE(selected_line_, 0); 479 DCHECK_GE(selected_line_, 0);
541 DCHECK_LT(selected_line_, static_cast<int>(GetLineCount())); 480 DCHECK_LT(selected_line_, static_cast<int>(GetLineCount()));
542 return RemoveSuggestion(selected_line_); 481 return RemoveSuggestion(selected_line_);
543 } 482 }
544 483
545 int AutofillPopupControllerImpl::LineFromY(int y) {
546 int current_height = kPopupBorderThickness;
547
548 for (size_t i = 0; i < suggestions_.size(); ++i) {
549 current_height += GetRowHeightFromId(suggestions_[i].frontend_id);
550
551 if (y <= current_height)
552 return i;
553 }
554
555 // The y value goes beyond the popup so stop the selection at the last line.
556 return GetLineCount() - 1;
557 }
558
559 int AutofillPopupControllerImpl::GetRowHeightFromId(int identifier) const {
560 if (identifier == POPUP_ITEM_ID_SEPARATOR)
561 return kSeparatorHeight;
562
563 return kRowHeight;
564 }
565
566 bool AutofillPopupControllerImpl::CanAccept(int id) { 484 bool AutofillPopupControllerImpl::CanAccept(int id) {
567 return id != POPUP_ITEM_ID_SEPARATOR && id != POPUP_ITEM_ID_WARNING_MESSAGE && 485 return id != POPUP_ITEM_ID_SEPARATOR && id != POPUP_ITEM_ID_WARNING_MESSAGE &&
568 id != POPUP_ITEM_ID_TITLE; 486 id != POPUP_ITEM_ID_TITLE;
569 } 487 }
570 488
571 bool AutofillPopupControllerImpl::HasSuggestions() { 489 bool AutofillPopupControllerImpl::HasSuggestions() {
572 if (suggestions_.empty()) 490 if (suggestions_.empty())
573 return false; 491 return false;
574 int id = suggestions_[0].frontend_id; 492 int id = suggestions_[0].frontend_id;
575 return id > 0 || 493 return id > 0 ||
(...skipping 17 matching lines...) Expand all
593 void AutofillPopupControllerImpl::ShowView() { 511 void AutofillPopupControllerImpl::ShowView() {
594 view_->Show(); 512 view_->Show();
595 } 513 }
596 514
597 void AutofillPopupControllerImpl::InvalidateRow(size_t row) { 515 void AutofillPopupControllerImpl::InvalidateRow(size_t row) {
598 DCHECK(0 <= row); 516 DCHECK(0 <= row);
599 DCHECK(row < suggestions_.size()); 517 DCHECK(row < suggestions_.size());
600 view_->InvalidateRow(row); 518 view_->InvalidateRow(row);
601 } 519 }
602 520
603 #if !defined(OS_ANDROID)
604 int AutofillPopupControllerImpl::GetDesiredPopupWidth() const {
605 int popup_width = controller_common_->RoundedElementBounds().width();
606 for (size_t i = 0; i < GetLineCount(); ++i) {
607 int row_size =
608 gfx::GetStringWidth(GetElidedValueAt(i), value_font_list_) +
609 gfx::GetStringWidth(GetElidedLabelAt(i), label_font_list_) +
610 RowWidthWithoutText(i);
611
612 popup_width = std::max(popup_width, row_size);
613 }
614
615 return popup_width;
616 }
617
618 int AutofillPopupControllerImpl::GetDesiredPopupHeight() const {
619 int popup_height = 2 * kPopupBorderThickness;
620
621 for (size_t i = 0; i < suggestions_.size(); ++i) {
622 popup_height += GetRowHeightFromId(suggestions_[i].frontend_id);
623 }
624
625 return popup_height;
626 }
627
628 int AutofillPopupControllerImpl::RowWidthWithoutText(int row) const {
629 int row_size = kEndPadding;
630
631 if (!elided_labels_[row].empty())
632 row_size += kNamePadding;
633
634 // Add the Autofill icon size, if required.
635 const base::string16& icon = suggestions_[row].icon;
636 if (!icon.empty()) {
637 int icon_width = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
638 GetIconResourceID(icon)).Width();
639 row_size += icon_width + kIconPadding;
640 }
641
642 // Add the padding at the end.
643 row_size += kEndPadding;
644
645 // Add room for the popup border.
646 row_size += 2 * kPopupBorderThickness;
647
648 return row_size;
649 }
650
651 void AutofillPopupControllerImpl::UpdatePopupBounds() {
652 int popup_width = GetDesiredPopupWidth();
653 int popup_height = GetDesiredPopupHeight();
654
655 popup_bounds_ = controller_common_->GetPopupBounds(popup_width, popup_height);
656 }
657 #endif // !defined(OS_ANDROID)
658
659 WeakPtr<AutofillPopupControllerImpl> AutofillPopupControllerImpl::GetWeakPtr() { 521 WeakPtr<AutofillPopupControllerImpl> AutofillPopupControllerImpl::GetWeakPtr() {
660 return weak_ptr_factory_.GetWeakPtr(); 522 return weak_ptr_factory_.GetWeakPtr();
661 } 523 }
662 524
525 #if !defined(OS_ANDROID)
526 void AutofillPopupControllerImpl::ElideValueAndLabelForRow(
527 size_t row,
528 int available_width) {
529 int value_width =
530 gfx::GetStringWidth(suggestions_[row].value, GetValueFontListForRow(row));
531 int label_width =
532 gfx::GetStringWidth(suggestions_[row].label, GetLabelFontList());
533 int total_text_length = value_width + label_width;
534
535 // The line can have no strings if it represents a UI element, such as
536 // a separator line.
537 if (total_text_length == 0)
538 return;
539
540 // Each field receives space in proportion to its length.
541 int value_size = available_width * value_width / total_text_length;
542 elided_values_[row] =
543 gfx::ElideText(suggestions_[row].value, GetValueFontListForRow(row),
544 value_size, gfx::ELIDE_TAIL);
545
546 int label_size = available_width * label_width / total_text_length;
547 elided_labels_[row] = gfx::ElideText(
548 suggestions_[row].label, GetLabelFontList(), label_size, gfx::ELIDE_TAIL);
549 }
550 #endif
551
663 void AutofillPopupControllerImpl::ClearState() { 552 void AutofillPopupControllerImpl::ClearState() {
664 // Don't clear view_, because otherwise the popup will have to get regenerated 553 // Don't clear view_, because otherwise the popup will have to get regenerated
665 // and this will cause flickering. 554 // and this will cause flickering.
666
667 popup_bounds_ = gfx::Rect();
668
669 suggestions_.clear(); 555 suggestions_.clear();
670 elided_values_.clear(); 556 elided_values_.clear();
671 elided_labels_.clear(); 557 elided_labels_.clear();
672 558
673 selected_line_ = kNoSelection; 559 selected_line_ = kNoSelection;
674 } 560 }
675 561
676 } // namespace autofill 562 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698