Chromium Code Reviews| Index: chrome/browser/ui/autofill/autofill_popup_view_helper.cc |
| diff --git a/chrome/browser/ui/autofill/autofill_popup_view_helper.cc b/chrome/browser/ui/autofill/autofill_popup_view_helper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fdd5dfeaf116a147e1ad7ea94e1e2506f589e97d |
| --- /dev/null |
| +++ b/chrome/browser/ui/autofill/autofill_popup_view_helper.cc |
| @@ -0,0 +1,180 @@ |
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
|
Evan Stade
2016/01/11 23:09:38
when using menus, most or all of the code in this
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/autofill/autofill_popup_view_helper.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/macros.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/ui/autofill/autofill_popup_view.h" |
| +#include "chrome/browser/ui/autofill/popup_constants.h" |
| +#include "components/autofill/core/browser/popup_item_ids.h" |
| +#include "components/autofill/core/browser/suggestion.h" |
| +#include "components/autofill/core/common/autofill_util.h" |
| +#include "grit/components_scaled_resources.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| +#include "ui/gfx/geometry/rect_conversions.h" |
| + |
| +namespace autofill { |
| + |
| +namespace { |
| + |
| +// The vertical height of each row in pixels. |
| +const size_t kRowHeight = 24; |
| + |
| +// The vertical height of a separator in pixels. |
| +const size_t kSeparatorHeight = 1; |
| + |
| +const struct { |
| + const char* name; |
| + int id; |
| +} kDataResources[] = { |
| + {"americanExpressCC", IDR_AUTOFILL_CC_AMEX}, |
| + {"dinersCC", IDR_AUTOFILL_CC_GENERIC}, |
| + {"discoverCC", IDR_AUTOFILL_CC_DISCOVER}, |
| + {"genericCC", IDR_AUTOFILL_CC_GENERIC}, |
| + {"jcbCC", IDR_AUTOFILL_CC_GENERIC}, |
| + {"masterCardCC", IDR_AUTOFILL_CC_MASTERCARD}, |
| + {"visaCC", IDR_AUTOFILL_CC_VISA}, |
| +#if defined(OS_ANDROID) |
| + {"scanCreditCardIcon", IDR_AUTOFILL_CC_SCAN_NEW}, |
| + {"settings", IDR_AUTOFILL_SETTINGS}, |
| +#endif |
| +}; |
| + |
| +int GetRowHeightFromId(int identifier) { |
| + if (identifier == POPUP_ITEM_ID_SEPARATOR) |
| + return kSeparatorHeight; |
| + |
| + return kRowHeight; |
| +} |
| + |
| +} // namespace |
| + |
| +AutofillPopupViewHelper::AutofillPopupViewHelper( |
| + AutofillPopupViewDelegate* delegate) |
| + : delegate_(delegate) {} |
| + |
| +#if !defined(OS_ANDROID) |
| +int AutofillPopupViewHelper::GetDesiredPopupHeight() const { |
| + std::vector<autofill::Suggestion> suggestions = delegate_->GetSuggestions(); |
| + int popup_height = 2 * kPopupBorderThickness; |
| + |
| + for (size_t i = 0; i < suggestions.size(); ++i) { |
| + popup_height += GetRowHeightFromId(suggestions[i].frontend_id); |
| + } |
| + |
| + return popup_height; |
| +} |
| + |
| +int AutofillPopupViewHelper::GetDesiredPopupWidth() const { |
| + std::vector<autofill::Suggestion> suggestions = delegate_->GetSuggestions(); |
| + |
| + int popup_width = RoundedElementBounds().width(); |
| + |
| + for (size_t i = 0; i < suggestions.size(); ++i) { |
| + int label_size = delegate_->GetElidedLabelWidthForRow(i); |
| + int row_size = delegate_->GetElidedValueWidthForRow(i) + label_size + |
| + RowWidthWithoutText(i, /* with_label= */ label_size > 0); |
| + |
| + popup_width = std::max(popup_width, row_size); |
| + } |
| + |
| + return popup_width; |
| +} |
| + |
| +int AutofillPopupViewHelper::RowWidthWithoutText(int row, |
| + bool with_label) const { |
| + std::vector<autofill::Suggestion> suggestions = delegate_->GetSuggestions(); |
| + |
| + int row_size = AutofillPopupView::kEndPadding; |
| + |
| + if (with_label) |
| + row_size += AutofillPopupView::kNamePadding; |
| + |
| + // Add the Autofill icon size, if required. |
| + const base::string16& icon = suggestions[row].icon; |
| + if (!icon.empty()) { |
| + int icon_width = ui::ResourceBundle::GetSharedInstance() |
| + .GetImageNamed(GetIconResourceID(icon)) |
| + .Width(); |
| + row_size += icon_width + AutofillPopupView::kIconPadding; |
| + } |
| + |
| + // Add the padding at the end. |
| + row_size += AutofillPopupView::kEndPadding; |
| + |
| + // Add room for the popup border. |
| + row_size += 2 * kPopupBorderThickness; |
| + |
| + return row_size; |
| +} |
| + |
| +int AutofillPopupViewHelper::GetAvailableWidthForRow(int row, |
| + bool with_label) const { |
| + return popup_bounds_.width() - RowWidthWithoutText(row, with_label); |
| +} |
| + |
| +void AutofillPopupViewHelper::UpdatePopupBounds() { |
| + int popup_width = GetDesiredPopupWidth(); |
| + int popup_height = GetDesiredPopupHeight(); |
| + |
| + popup_bounds_ = view_utils_.CalculatePopupBounds( |
| + popup_width, popup_height, RoundedElementBounds(), |
| + delegate_->container_view(), delegate_->IsRTL()); |
| +} |
| +#endif |
| + |
| +int AutofillPopupViewHelper::LineFromY(int y) { |
| + std::vector<autofill::Suggestion> suggestions = delegate_->GetSuggestions(); |
| + int current_height = kPopupBorderThickness; |
| + |
| + for (size_t i = 0; i < suggestions.size(); ++i) { |
| + current_height += GetRowHeightFromId(suggestions[i].frontend_id); |
| + |
| + if (y <= current_height) |
| + return i; |
| + } |
| + |
| + // The y value goes beyond the popup so stop the selection at the last line. |
| + return suggestions.size() - 1; |
| +} |
| + |
| +gfx::Rect AutofillPopupViewHelper::GetRowBounds(size_t index) { |
| + std::vector<autofill::Suggestion> suggestions = delegate_->GetSuggestions(); |
| + |
| + int top = kPopupBorderThickness; |
| + for (size_t i = 0; i < index; ++i) { |
| + top += GetRowHeightFromId(suggestions[i].frontend_id); |
| + } |
| + |
| + return gfx::Rect(kPopupBorderThickness, top, |
| + popup_bounds_.width() - 2 * kPopupBorderThickness, |
| + GetRowHeightFromId(suggestions[index].frontend_id)); |
| +} |
| + |
| +int AutofillPopupViewHelper::GetIconResourceID( |
| + const base::string16& resource_name) const { |
| + int result = -1; |
| + for (size_t i = 0; i < arraysize(kDataResources); ++i) { |
| + if (resource_name == base::ASCIIToUTF16(kDataResources[i].name)) { |
| + result = kDataResources[i].id; |
| + break; |
| + } |
| + } |
| + |
| +#if defined(OS_ANDROID) && !defined(USE_AURA) |
| + if (result == IDR_AUTOFILL_CC_SCAN_NEW && IsKeyboardAccessoryEnabled()) |
| + result = IDR_AUTOFILL_CC_SCAN_NEW_KEYBOARD_ACCESSORY; |
| +#endif |
| + |
| + return result; |
| +} |
| + |
| +const gfx::Rect AutofillPopupViewHelper::RoundedElementBounds() const { |
| + return gfx::ToEnclosingRect(delegate_->element_bounds()); |
| +} |
| + |
| +} // namespace autofill |