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