OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/autofill/autofill_popup_view.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/macros.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/ui/autofill/popup_constants.h" |
| 12 #include "components/autofill/core/browser/popup_item_ids.h" |
| 13 #include "components/autofill/core/browser/suggestion.h" |
| 14 #include "components/autofill/core/common/autofill_util.h" |
| 15 #include "grit/components_scaled_resources.h" |
| 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/gfx/geometry/rect_conversions.h" |
| 18 |
| 19 namespace autofill { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // The vertical height of each row in pixels. |
| 24 const size_t kRowHeight = 24; |
| 25 |
| 26 // The vertical height of a separator in pixels. |
| 27 const size_t kSeparatorHeight = 1; |
| 28 |
| 29 const struct { |
| 30 const char* name; |
| 31 int id; |
| 32 } kDataResources[] = { |
| 33 {"americanExpressCC", IDR_AUTOFILL_CC_AMEX}, |
| 34 {"dinersCC", IDR_AUTOFILL_CC_GENERIC}, |
| 35 {"discoverCC", IDR_AUTOFILL_CC_DISCOVER}, |
| 36 {"genericCC", IDR_AUTOFILL_CC_GENERIC}, |
| 37 {"jcbCC", IDR_AUTOFILL_CC_GENERIC}, |
| 38 {"masterCardCC", IDR_AUTOFILL_CC_MASTERCARD}, |
| 39 {"visaCC", IDR_AUTOFILL_CC_VISA}, |
| 40 #if defined(OS_ANDROID) |
| 41 {"scanCreditCardIcon", IDR_AUTOFILL_CC_SCAN_NEW}, |
| 42 {"settings", IDR_AUTOFILL_SETTINGS}, |
| 43 #endif |
| 44 }; |
| 45 |
| 46 int GetRowHeightFromId(int identifier) { |
| 47 if (identifier == POPUP_ITEM_ID_SEPARATOR) |
| 48 return kSeparatorHeight; |
| 49 |
| 50 return kRowHeight; |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
| 55 AutofillPopupView::AutofillPopupView(AutofillPopupViewDelegate* delegate) |
| 56 : delegate_(delegate) {} |
| 57 |
| 58 #if !defined(OS_ANDROID) |
| 59 int AutofillPopupView::GetDesiredPopupHeight() const { |
| 60 std::vector<autofill::Suggestion> suggestions = delegate_->GetSuggestions(); |
| 61 int popup_height = 2 * kPopupBorderThickness; |
| 62 |
| 63 for (size_t i = 0; i < suggestions.size(); ++i) { |
| 64 popup_height += GetRowHeightFromId(suggestions[i].frontend_id); |
| 65 } |
| 66 |
| 67 return popup_height; |
| 68 } |
| 69 |
| 70 int AutofillPopupView::GetDesiredPopupWidth() const { |
| 71 std::vector<autofill::Suggestion> suggestions = delegate_->GetSuggestions(); |
| 72 |
| 73 int popup_width = RoundedElementBounds().width(); |
| 74 |
| 75 for (size_t i = 0; i < suggestions.size(); ++i) { |
| 76 int label_size = delegate_->GetElidedLabelWidthForRow(i); |
| 77 int row_size = delegate_->GetElidedValueWidthForRow(i) + label_size + |
| 78 RowWidthWithoutText(i, /* with_label= */ label_size > 0); |
| 79 |
| 80 popup_width = std::max(popup_width, row_size); |
| 81 } |
| 82 |
| 83 return popup_width; |
| 84 } |
| 85 |
| 86 int AutofillPopupView::RowWidthWithoutText(int row, bool with_label) const { |
| 87 std::vector<autofill::Suggestion> suggestions = delegate_->GetSuggestions(); |
| 88 |
| 89 int row_size = kEndPadding; |
| 90 |
| 91 if (with_label) |
| 92 row_size += kNamePadding; |
| 93 |
| 94 // Add the Autofill icon size, if required. |
| 95 const base::string16& icon = suggestions[row].icon; |
| 96 if (!icon.empty()) { |
| 97 int icon_width = ui::ResourceBundle::GetSharedInstance() |
| 98 .GetImageNamed(GetIconResourceID(icon)) |
| 99 .Width(); |
| 100 row_size += icon_width + kIconPadding; |
| 101 } |
| 102 |
| 103 // Add the padding at the end. |
| 104 row_size += kEndPadding; |
| 105 |
| 106 // Add room for the popup border. |
| 107 row_size += 2 * kPopupBorderThickness; |
| 108 |
| 109 return row_size; |
| 110 } |
| 111 |
| 112 int AutofillPopupView::GetAvailableWidthForRow(int row, bool with_label) const { |
| 113 return popup_bounds_.width() - RowWidthWithoutText(row, with_label); |
| 114 } |
| 115 |
| 116 void AutofillPopupView::UpdatePopupBounds() { |
| 117 int popup_width = GetDesiredPopupWidth(); |
| 118 int popup_height = GetDesiredPopupHeight(); |
| 119 |
| 120 popup_bounds_ = view_utils_.CalculatePopupBounds( |
| 121 popup_width, popup_height, RoundedElementBounds(), |
| 122 delegate_->container_view(), delegate_->IsRTL()); |
| 123 } |
| 124 #endif |
| 125 |
| 126 int AutofillPopupView::LineFromY(int y) { |
| 127 std::vector<autofill::Suggestion> suggestions = delegate_->GetSuggestions(); |
| 128 int current_height = kPopupBorderThickness; |
| 129 |
| 130 for (size_t i = 0; i < suggestions.size(); ++i) { |
| 131 current_height += GetRowHeightFromId(suggestions[i].frontend_id); |
| 132 |
| 133 if (y <= current_height) |
| 134 return i; |
| 135 } |
| 136 |
| 137 // The y value goes beyond the popup so stop the selection at the last line. |
| 138 return suggestions.size() - 1; |
| 139 } |
| 140 |
| 141 gfx::Rect AutofillPopupView::GetRowBounds(size_t index) { |
| 142 std::vector<autofill::Suggestion> suggestions = delegate_->GetSuggestions(); |
| 143 |
| 144 int top = kPopupBorderThickness; |
| 145 for (size_t i = 0; i < index; ++i) { |
| 146 top += GetRowHeightFromId(suggestions[i].frontend_id); |
| 147 } |
| 148 |
| 149 return gfx::Rect(kPopupBorderThickness, top, |
| 150 popup_bounds_.width() - 2 * kPopupBorderThickness, |
| 151 GetRowHeightFromId(suggestions[index].frontend_id)); |
| 152 } |
| 153 |
| 154 int AutofillPopupView::GetIconResourceID( |
| 155 const base::string16& resource_name) const { |
| 156 int result = -1; |
| 157 for (size_t i = 0; i < arraysize(kDataResources); ++i) { |
| 158 if (resource_name == base::ASCIIToUTF16(kDataResources[i].name)) { |
| 159 result = kDataResources[i].id; |
| 160 break; |
| 161 } |
| 162 } |
| 163 |
| 164 #if defined(OS_ANDROID) && !defined(USE_AURA) |
| 165 if (result == IDR_AUTOFILL_CC_SCAN_NEW && IsKeyboardAccessoryEnabled()) |
| 166 result = IDR_AUTOFILL_CC_SCAN_NEW_KEYBOARD_ACCESSORY; |
| 167 #endif |
| 168 |
| 169 return result; |
| 170 } |
| 171 |
| 172 const gfx::Rect AutofillPopupView::RoundedElementBounds() const { |
| 173 return gfx::ToEnclosingRect(delegate_->element_bounds()); |
| 174 } |
| 175 |
| 176 } // namespace autofill |
OLD | NEW |