| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/views/payments/payment_sheet_view_controller.h" | 5 #include "chrome/browser/ui/views/payments/payment_sheet_view_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 7 #include <memory> | 8 #include <memory> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/ui/views/payments/payment_request_dialog.h" | 14 #include "chrome/browser/ui/views/payments/payment_request_dialog.h" |
| 14 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" | 15 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" |
| 15 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "components/autofill/core/browser/autofill_data_util.h" |
| 18 #include "components/autofill/core/browser/autofill_type.h" |
| 19 #include "components/autofill/core/browser/credit_card.h" |
| 20 #include "components/autofill/core/browser/field_types.h" |
| 21 #include "components/autofill/core/browser/personal_data_manager.h" |
| 16 #include "components/payments/currency_formatter.h" | 22 #include "components/payments/currency_formatter.h" |
| 17 #include "components/payments/payment_request.h" | 23 #include "components/payments/payment_request.h" |
| 18 #include "components/strings/grit/components_strings.h" | 24 #include "components/strings/grit/components_strings.h" |
| 25 #include "content/public/browser/web_contents.h" |
| 19 #include "third_party/skia/include/core/SkColor.h" | 26 #include "third_party/skia/include/core/SkColor.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
| 28 #include "ui/base/resource/resource_bundle.h" |
| 21 #include "ui/gfx/color_utils.h" | 29 #include "ui/gfx/color_utils.h" |
| 22 #include "ui/gfx/font.h" | 30 #include "ui/gfx/font.h" |
| 23 #include "ui/gfx/geometry/insets.h" | 31 #include "ui/gfx/geometry/insets.h" |
| 24 #include "ui/gfx/paint_vector_icon.h" | 32 #include "ui/gfx/paint_vector_icon.h" |
| 25 #include "ui/gfx/range/range.h" | 33 #include "ui/gfx/range/range.h" |
| 26 #include "ui/views/border.h" | 34 #include "ui/views/border.h" |
| 27 #include "ui/views/controls/button/custom_button.h" | 35 #include "ui/views/controls/button/custom_button.h" |
| 28 #include "ui/views/controls/button/label_button.h" | 36 #include "ui/views/controls/button/label_button.h" |
| 29 #include "ui/views/controls/button/md_text_button.h" | 37 #include "ui/views/controls/button/md_text_button.h" |
| 30 #include "ui/views/controls/image_view.h" | 38 #include "ui/views/controls/image_view.h" |
| 31 #include "ui/views/controls/label.h" | 39 #include "ui/views/controls/label.h" |
| 32 #include "ui/views/controls/styled_label.h" | 40 #include "ui/views/controls/styled_label.h" |
| 41 #include "ui/views/layout/fill_layout.h" |
| 33 #include "ui/views/layout/grid_layout.h" | 42 #include "ui/views/layout/grid_layout.h" |
| 34 #include "ui/views/resources/vector_icons/vector_icons.h" | 43 #include "ui/views/resources/vector_icons/vector_icons.h" |
| 35 #include "ui/views/view.h" | 44 #include "ui/views/view.h" |
| 36 | 45 |
| 37 namespace payments { | 46 namespace payments { |
| 38 namespace { | 47 namespace { |
| 39 | 48 |
| 40 enum class PaymentSheetViewControllerTags { | 49 enum class PaymentSheetViewControllerTags { |
| 41 // The tag for the button that navigates to the Order Summary sheet. | 50 // The tag for the button that navigates to the Order Summary sheet. |
| 42 SHOW_ORDER_SUMMARY_BUTTON = static_cast<int>( | 51 SHOW_ORDER_SUMMARY_BUTTON = static_cast<int>( |
| 43 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX), | 52 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX), |
| 53 SHOW_PAYMENT_METHOD_BUTTON, |
| 44 }; | 54 }; |
| 45 | 55 |
| 46 // Creates a clickable row to be displayed in the Payment Sheet. It contains | 56 // Creates a clickable row to be displayed in the Payment Sheet. It contains |
| 47 // a section name and some content, followed by a chevron as a clickability | 57 // a section name and some content, followed by a chevron as a clickability |
| 48 // affordance. Both, either, or none of |content_view| and |extra_content_view| | 58 // affordance. Both, either, or none of |content_view| and |extra_content_view| |
| 49 // may be present, the difference between the two being that content is pinned | 59 // may be present, the difference between the two being that content is pinned |
| 50 // to the left and extra_content is pinned to the right. | 60 // to the left and extra_content is pinned to the right. |
| 51 // The row also displays a light gray horizontal ruler on its lower boundary. | 61 // The row also displays a light gray horizontal ruler on its lower boundary. |
| 62 // The name column has a fixed width equal to |name_column_width|. |
| 52 // +----------------------------+ | 63 // +----------------------------+ |
| 53 // | Name | Content | Extra | > | | 64 // | Name | Content | Extra | > | |
| 54 // +~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ <-- ruler | 65 // +~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ <-- ruler |
| 55 class PaymentSheetRow : public views::CustomButton { | 66 class PaymentSheetRow : public views::CustomButton { |
| 56 public: | 67 public: |
| 57 PaymentSheetRow(views::ButtonListener* listener, | 68 PaymentSheetRow(views::ButtonListener* listener, |
| 58 const base::string16& section_name, | 69 const base::string16& section_name, |
| 59 std::unique_ptr<views::View> content_view, | 70 std::unique_ptr<views::View> content_view, |
| 60 std::unique_ptr<views::View> extra_content_view) | 71 std::unique_ptr<views::View> extra_content_view, |
| 72 int name_column_width) |
| 61 : views::CustomButton(listener) { | 73 : views::CustomButton(listener) { |
| 62 SetBorder(views::CreateSolidSidedBorder(0, 0, 1, 0, SK_ColorLTGRAY)); | 74 SetBorder(views::CreateSolidSidedBorder(0, 0, 1, 0, SK_ColorLTGRAY)); |
| 63 views::GridLayout* layout = new views::GridLayout(this); | 75 views::GridLayout* layout = new views::GridLayout(this); |
| 64 | 76 |
| 65 constexpr int kRowVerticalInset = 18; | 77 constexpr int kRowVerticalInset = 8; |
| 66 // The rows have extra inset compared to the header so that their right edge | 78 // The rows have extra inset compared to the header so that their right edge |
| 67 // lines up with the close button's X rather than its invisible right edge. | 79 // lines up with the close button's X rather than its invisible right edge. |
| 68 constexpr int kRowExtraRightInset = 8; | 80 constexpr int kRowExtraRightInset = 8; |
| 69 layout->SetInsets( | 81 layout->SetInsets( |
| 70 kRowVerticalInset, 0, kRowVerticalInset, kRowExtraRightInset); | 82 kRowVerticalInset, 0, kRowVerticalInset, kRowExtraRightInset); |
| 71 SetLayoutManager(layout); | 83 SetLayoutManager(layout); |
| 72 | 84 |
| 73 views::ColumnSet* columns = layout->AddColumnSet(0); | 85 views::ColumnSet* columns = layout->AddColumnSet(0); |
| 74 // A column for the section name. | 86 // A column for the section name. |
| 75 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, | 87 columns->AddColumn(views::GridLayout::LEADING, |
| 76 0, views::GridLayout::USE_PREF, 0, 0); | 88 views::GridLayout::LEADING, |
| 89 0, |
| 90 views::GridLayout::FIXED, |
| 91 name_column_width, |
| 92 0); |
| 93 |
| 94 constexpr int kPaddingColumnsWidth = 25; |
| 95 columns->AddPaddingColumn(0, kPaddingColumnsWidth); |
| 96 |
| 77 // A column for the content. | 97 // A column for the content. |
| 78 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, | 98 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::LEADING, |
| 79 1, views::GridLayout::USE_PREF, 0, 0); | 99 1, views::GridLayout::USE_PREF, 0, 0); |
| 80 // A column for the extra content. | 100 // A column for the extra content. |
| 81 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, | 101 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, |
| 82 0, views::GridLayout::USE_PREF, 0, 0); | 102 0, views::GridLayout::USE_PREF, 0, 0); |
| 83 | 103 |
| 84 constexpr int kPaddingColumnsWidth = 25; | |
| 85 columns->AddPaddingColumn(0, kPaddingColumnsWidth); | 104 columns->AddPaddingColumn(0, kPaddingColumnsWidth); |
| 86 // A column for the chevron. | 105 // A column for the chevron. |
| 87 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, | 106 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, |
| 88 0, views::GridLayout::USE_PREF, 0, 0); | 107 0, views::GridLayout::USE_PREF, 0, 0); |
| 89 | 108 |
| 90 layout->StartRow(0, 0); | 109 layout->StartRow(0, 0); |
| 91 views::Label* name_label = new views::Label(section_name); | 110 views::Label* name_label = new views::Label(section_name); |
| 92 layout->AddView(name_label); | 111 layout->AddView(name_label); |
| 93 | 112 |
| 94 if (content_view) { | 113 if (content_view) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 106 views::ImageView* chevron = new views::ImageView(); | 125 views::ImageView* chevron = new views::ImageView(); |
| 107 chevron->SetImage(gfx::CreateVectorIcon( | 126 chevron->SetImage(gfx::CreateVectorIcon( |
| 108 views::kSubmenuArrowIcon, | 127 views::kSubmenuArrowIcon, |
| 109 color_utils::DeriveDefaultIconColor(name_label->enabled_color()))); | 128 color_utils::DeriveDefaultIconColor(name_label->enabled_color()))); |
| 110 layout->AddView(chevron); | 129 layout->AddView(chevron); |
| 111 } | 130 } |
| 112 | 131 |
| 113 DISALLOW_COPY_AND_ASSIGN(PaymentSheetRow); | 132 DISALLOW_COPY_AND_ASSIGN(PaymentSheetRow); |
| 114 }; | 133 }; |
| 115 | 134 |
| 135 int ComputeWidestNameColumnViewWidth() { |
| 136 // The name colums in each row should all have the same width, large enough to |
| 137 // accomodate the longest piece of text they contain. Because of this, each |
| 138 // row's GridLayout requires its first column to have a fixed width of the |
| 139 // correct size. To measure the required size, layout a label with each |
| 140 // section name, measure its width, then initialize |widest_column_width| |
| 141 // with the largest value. |
| 142 std::vector<int> section_names { |
| 143 IDS_PAYMENT_REQUEST_ORDER_SUMMARY_SECTION_NAME, |
| 144 IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME, |
| 145 }; |
| 146 |
| 147 int widest_column_width = 0; |
| 148 |
| 149 views::Label label(base::ASCIIToUTF16("")); |
| 150 for (int name_id : section_names) { |
| 151 label.SetText(l10n_util::GetStringUTF16(name_id)); |
| 152 widest_column_width = std::max( |
| 153 label.GetPreferredSize().width(), |
| 154 widest_column_width); |
| 155 } |
| 156 |
| 157 return widest_column_width; |
| 158 } |
| 159 |
| 116 } // namespace | 160 } // namespace |
| 117 | 161 |
| 118 PaymentSheetViewController::PaymentSheetViewController( | 162 PaymentSheetViewController::PaymentSheetViewController( |
| 119 PaymentRequest* request, | 163 PaymentRequest* request, |
| 120 PaymentRequestDialog* dialog) | 164 PaymentRequestDialog* dialog) |
| 121 : PaymentRequestSheetController(request, dialog) {} | 165 : PaymentRequestSheetController(request, dialog), |
| 166 widest_name_column_view_width_(ComputeWidestNameColumnViewWidth()) {} |
| 122 | 167 |
| 123 PaymentSheetViewController::~PaymentSheetViewController() {} | 168 PaymentSheetViewController::~PaymentSheetViewController() {} |
| 124 | 169 |
| 125 std::unique_ptr<views::View> PaymentSheetViewController::CreateView() { | 170 std::unique_ptr<views::View> PaymentSheetViewController::CreateView() { |
| 126 std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>(); | 171 std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>(); |
| 127 | 172 |
| 128 views::GridLayout* layout = new views::GridLayout(content_view.get()); | 173 views::GridLayout* layout = new views::GridLayout(content_view.get()); |
| 129 content_view->SetLayoutManager(layout); | 174 content_view->SetLayoutManager(layout); |
| 130 views::ColumnSet* columns = layout->AddColumnSet(0); | 175 views::ColumnSet* columns = layout->AddColumnSet(0); |
| 131 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, | 176 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, |
| 132 1, views::GridLayout::USE_PREF, 0, 0); | 177 1, views::GridLayout::USE_PREF, 0, 0); |
| 133 | 178 |
| 134 layout->StartRow(0, 0); | 179 layout->StartRow(0, 0); |
| 135 layout->AddView(CreatePaymentSheetSummaryRow().release()); | 180 layout->AddView(CreatePaymentSheetSummaryRow().release()); |
| 181 layout->StartRow(0, 0); |
| 182 layout->AddView(CreatePaymentMethodRow().release()); |
| 136 | 183 |
| 137 return CreatePaymentView( | 184 return CreatePaymentView( |
| 138 CreateSheetHeaderView( | 185 CreateSheetHeaderView( |
| 139 false, | 186 false, |
| 140 l10n_util::GetStringUTF16(IDS_PAYMENT_REQUEST_PAYMENT_SHEET_TITLE), | 187 l10n_util::GetStringUTF16(IDS_PAYMENT_REQUEST_PAYMENT_SHEET_TITLE), |
| 141 this), | 188 this), |
| 142 std::move(content_view)); | 189 std::move(content_view)); |
| 143 } | 190 } |
| 144 | 191 |
| 145 void PaymentSheetViewController::ButtonPressed( | 192 void PaymentSheetViewController::ButtonPressed( |
| 146 views::Button* sender, const ui::Event& event) { | 193 views::Button* sender, const ui::Event& event) { |
| 147 switch (sender->tag()) { | 194 switch (sender->tag()) { |
| 148 case static_cast<int>(PaymentRequestCommonTags::CLOSE_BUTTON_TAG): | 195 case static_cast<int>(PaymentRequestCommonTags::CLOSE_BUTTON_TAG): |
| 149 dialog()->CloseDialog(); | 196 dialog()->CloseDialog(); |
| 150 break; | 197 break; |
| 151 case static_cast<int>( | 198 case static_cast<int>( |
| 152 PaymentSheetViewControllerTags::SHOW_ORDER_SUMMARY_BUTTON): | 199 PaymentSheetViewControllerTags::SHOW_ORDER_SUMMARY_BUTTON): |
| 153 dialog()->ShowOrderSummary(); | 200 dialog()->ShowOrderSummary(); |
| 154 break; | 201 break; |
| 202 case static_cast<int>( |
| 203 PaymentSheetViewControllerTags::SHOW_PAYMENT_METHOD_BUTTON): |
| 204 dialog()->ShowPaymentMethodSheet(); |
| 205 break; |
| 155 default: | 206 default: |
| 156 NOTREACHED(); | 207 NOTREACHED(); |
| 157 } | 208 } |
| 158 } | 209 } |
| 159 | 210 |
| 160 std::unique_ptr<views::View> | 211 std::unique_ptr<views::View> |
| 161 PaymentSheetViewController::CreateOrderSummarySectionContent() { | 212 PaymentSheetViewController::CreateOrderSummarySectionContent() { |
| 162 CurrencyFormatter* formatter = request()->GetOrCreateCurrencyFormatter( | 213 CurrencyFormatter* formatter = request()->GetOrCreateCurrencyFormatter( |
| 163 request()->details()->total->amount->currency, | 214 request()->details()->total->amount->currency, |
| 164 request()->details()->total->amount->currency_system, | 215 request()->details()->total->amount->currency_system, |
| 165 g_browser_process->GetApplicationLocale()); | 216 g_browser_process->GetApplicationLocale()); |
| 166 base::string16 label_value = l10n_util::GetStringFUTF16( | 217 base::string16 label_value = l10n_util::GetStringFUTF16( |
| 167 IDS_PAYMENT_REQUEST_ORDER_SUMMARY_SECTION_TOTAL_FORMAT, | 218 IDS_PAYMENT_REQUEST_ORDER_SUMMARY_SECTION_TOTAL_FORMAT, |
| 168 base::UTF8ToUTF16(request()->details()->total->label), | 219 base::UTF8ToUTF16(request()->details()->total->label), |
| 169 base::UTF8ToUTF16(formatter->formatted_currency_code()), | 220 base::UTF8ToUTF16(formatter->formatted_currency_code()), |
| 170 formatter->Format(request()->details()->total->amount->value)); | 221 formatter->Format(request()->details()->total->amount->value)); |
| 171 | 222 |
| 172 return base::MakeUnique<views::Label>(label_value); | 223 return base::MakeUnique<views::Label>(label_value); |
| 173 } | 224 } |
| 174 | 225 |
| 226 // Creates the Order Summary row, which contains an "Order Summary" label, |
| 227 // a Total Amount label, and a Chevron. |
| 228 // +----------------------------------------------+ |
| 229 // | Order Summary Total USD $12.34 > | |
| 230 // +----------------------------------------------+ |
| 175 std::unique_ptr<views::Button> | 231 std::unique_ptr<views::Button> |
| 176 PaymentSheetViewController::CreatePaymentSheetSummaryRow() { | 232 PaymentSheetViewController::CreatePaymentSheetSummaryRow() { |
| 177 std::unique_ptr<views::Button> section = base::MakeUnique<PaymentSheetRow>( | 233 std::unique_ptr<views::Button> section = base::MakeUnique<PaymentSheetRow>( |
| 178 this, | 234 this, |
| 179 l10n_util::GetStringUTF16(IDS_PAYMENT_REQUEST_ORDER_SUMMARY_SECTION_NAME), | 235 l10n_util::GetStringUTF16(IDS_PAYMENT_REQUEST_ORDER_SUMMARY_SECTION_NAME), |
| 180 std::unique_ptr<views::View>(nullptr), | 236 std::unique_ptr<views::View>(nullptr), |
| 181 CreateOrderSummarySectionContent()); | 237 CreateOrderSummarySectionContent(), |
| 238 widest_name_column_view_width_); |
| 182 section->set_tag(static_cast<int>( | 239 section->set_tag(static_cast<int>( |
| 183 PaymentSheetViewControllerTags::SHOW_ORDER_SUMMARY_BUTTON)); | 240 PaymentSheetViewControllerTags::SHOW_ORDER_SUMMARY_BUTTON)); |
| 184 return section; | 241 return section; |
| 185 } | 242 } |
| 186 | 243 |
| 244 // Creates the Payment Method row, which contains a "Payment" label, the user's |
| 245 // masked Credit Card details, the icon for the selected card, and a chevron. |
| 246 // +----------------------------------------------+ |
| 247 // | Payment Visa ****0000 | |
| 248 // | John Smith | VISA | > | |
| 249 // | | |
| 250 // +----------------------------------------------+ |
| 251 std::unique_ptr<views::Button> |
| 252 PaymentSheetViewController::CreatePaymentMethodRow() { |
| 253 autofill::CreditCard* selected_card = |
| 254 request()->GetCurrentlySelectedCreditCard(); |
| 255 |
| 256 std::unique_ptr<views::View> content_view; |
| 257 std::unique_ptr<views::ImageView> card_icon_view; |
| 258 if (selected_card) { |
| 259 content_view = base::MakeUnique<views::View>(); |
| 260 |
| 261 views::GridLayout* layout = new views::GridLayout(content_view.get()); |
| 262 content_view->SetLayoutManager(layout); |
| 263 views::ColumnSet* columns = layout->AddColumnSet(0); |
| 264 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, |
| 265 1, views::GridLayout::USE_PREF, 0, 0); |
| 266 |
| 267 layout->StartRow(0, 0); |
| 268 layout->AddView(new views::Label(selected_card->TypeAndLastFourDigits())); |
| 269 layout->StartRow(0, 0); |
| 270 layout->AddView(new views::Label( |
| 271 selected_card->GetInfo( |
| 272 autofill::AutofillType(autofill::CREDIT_CARD_NAME_FULL), |
| 273 g_browser_process->GetApplicationLocale()))); |
| 274 |
| 275 card_icon_view = base::MakeUnique<views::ImageView>(); |
| 276 card_icon_view->SetImage( |
| 277 ResourceBundle::GetSharedInstance() |
| 278 .GetImageNamed(autofill::data_util::GetPaymentRequestData( |
| 279 selected_card->type()).icon_resource_id) |
| 280 .AsImageSkia()); |
| 281 card_icon_view->SetBorder( |
| 282 views::CreateRoundedRectBorder(1, 3, SK_ColorLTGRAY)); |
| 283 |
| 284 constexpr gfx::Size kCardIconSize = gfx::Size(32, 20); |
| 285 card_icon_view->SetImageSize(kCardIconSize); |
| 286 } |
| 287 |
| 288 std::unique_ptr<views::Button> section = base::MakeUnique<PaymentSheetRow>( |
| 289 this, |
| 290 l10n_util::GetStringUTF16( |
| 291 IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME), |
| 292 std::move(content_view), |
| 293 std::move(card_icon_view), |
| 294 widest_name_column_view_width_); |
| 295 section->set_tag(static_cast<int>( |
| 296 PaymentSheetViewControllerTags::SHOW_PAYMENT_METHOD_BUTTON)); |
| 297 return section; |
| 298 } |
| 299 |
| 187 } // namespace payments | 300 } // namespace payments |
| OLD | NEW |