OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_method_view_controller.h" | 5 #include "chrome/browser/ui/views/payments/payment_method_view_controller.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" | 12 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
12 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" | 13 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" |
| 14 #include "chrome/browser/ui/views/payments/payment_request_row_view.h" |
13 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" | 15 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" |
14 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "components/autofill/core/browser/autofill_type.h" |
| 18 #include "components/autofill/core/browser/credit_card.h" |
15 #include "components/payments/payment_request.h" | 19 #include "components/payments/payment_request.h" |
16 #include "components/strings/grit/components_strings.h" | 20 #include "components/strings/grit/components_strings.h" |
| 21 #include "third_party/skia/include/core/SkColor.h" |
17 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/gfx/paint_vector_icon.h" |
18 #include "ui/views/controls/button/label_button.h" | 24 #include "ui/views/controls/button/label_button.h" |
19 #include "ui/views/controls/button/md_text_button.h" | 25 #include "ui/views/controls/button/md_text_button.h" |
20 #include "ui/views/layout/fill_layout.h" | 26 #include "ui/views/layout/box_layout.h" |
| 27 #include "ui/views/layout/grid_layout.h" |
| 28 #include "ui/views/vector_icons.h" |
21 | 29 |
22 namespace payments { | 30 namespace payments { |
23 | 31 |
24 namespace { | 32 namespace { |
25 | 33 |
26 constexpr int kFirstTagValue = static_cast<int>( | 34 constexpr int kFirstTagValue = static_cast<int>( |
27 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); | 35 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); |
28 | 36 |
29 enum class PaymentMethodViewControllerTags : int { | 37 enum class PaymentMethodViewControllerTags : int { |
30 // The tag for the button that triggers the "add card" flow. Starts at | 38 // The tag for the button that triggers the "add card" flow. Starts at |
31 // |kFirstTagValue| not to conflict with tags common to all views. | 39 // |kFirstTagValue| not to conflict with tags common to all views. |
32 ADD_CREDIT_CARD_BUTTON = kFirstTagValue, | 40 ADD_CREDIT_CARD_BUTTON = kFirstTagValue, |
33 }; | 41 }; |
34 | 42 |
| 43 class PaymentMethodListItem : public payments::PaymentRequestItemList::Item, |
| 44 public views::ButtonListener { |
| 45 public: |
| 46 // Does not take ownership of |card|, which should not be null and should |
| 47 // outlive this object. |
| 48 explicit PaymentMethodListItem(autofill::CreditCard* card) : card_(card) {} |
| 49 ~PaymentMethodListItem() override {} |
| 50 |
| 51 private: |
| 52 // payments::PaymentRequestItemList::Item: |
| 53 std::unique_ptr<views::View> CreateItemView() override { |
| 54 std::unique_ptr<PaymentRequestRowView> row = |
| 55 base::MakeUnique<PaymentRequestRowView>(this); |
| 56 views::GridLayout* layout = new views::GridLayout(row.get()); |
| 57 layout->SetInsets( |
| 58 kPaymentRequestRowVerticalInsets, kPaymentRequestRowHorizontalInsets, |
| 59 kPaymentRequestRowVerticalInsets, kPaymentRequestRowHorizontalInsets); |
| 60 row->SetLayoutManager(layout); |
| 61 views::ColumnSet* columns = layout->AddColumnSet(0); |
| 62 |
| 63 // A column for the masked number and name on card |
| 64 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
| 65 views::GridLayout::USE_PREF, 0, 0); |
| 66 |
| 67 // A padding column that resizes to take up the empty space between the |
| 68 // leading and trailing parts. |
| 69 columns->AddPaddingColumn(1, 0); |
| 70 |
| 71 // A column for the checkmark when the row is selected. |
| 72 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, |
| 73 0, views::GridLayout::USE_PREF, 0, 0); |
| 74 |
| 75 // A column for the card icon |
| 76 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, |
| 77 0, views::GridLayout::USE_PREF, 0, 0); |
| 78 |
| 79 // A column for the edit button |
| 80 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, |
| 81 0, views::GridLayout::USE_PREF, 0, 0); |
| 82 |
| 83 layout->StartRow(0, 0); |
| 84 std::unique_ptr<views::View> card_info_container = |
| 85 base::MakeUnique<views::View>(); |
| 86 card_info_container->set_can_process_events_within_subtree(false); |
| 87 |
| 88 std::unique_ptr<views::BoxLayout> box_layout = |
| 89 base::MakeUnique<views::BoxLayout>(views::BoxLayout::kVertical, 0, |
| 90 kPaymentRequestRowVerticalInsets, 0); |
| 91 box_layout->set_cross_axis_alignment( |
| 92 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); |
| 93 card_info_container->SetLayoutManager(box_layout.release()); |
| 94 |
| 95 card_info_container->AddChildView( |
| 96 new views::Label(card_->TypeAndLastFourDigits())); |
| 97 card_info_container->AddChildView(new views::Label( |
| 98 card_->GetInfo(autofill::AutofillType(autofill::CREDIT_CARD_NAME_FULL), |
| 99 g_browser_process->GetApplicationLocale()))); |
| 100 layout->AddView(card_info_container.release()); |
| 101 |
| 102 std::unique_ptr<views::ImageView> checkmark = |
| 103 base::MakeUnique<views::ImageView>(); |
| 104 checkmark->set_interactive(false); |
| 105 checkmark->SetImage( |
| 106 gfx::CreateVectorIcon(views::kMenuCheckIcon, 0xFF609265)); |
| 107 layout->AddView(checkmark.release()); |
| 108 |
| 109 std::unique_ptr<views::ImageView> card_icon_view = |
| 110 CreateCardIconView(card_->type()); |
| 111 card_icon_view->SetImageSize(gfx::Size(32, 20)); |
| 112 layout->AddView(card_icon_view.release()); |
| 113 |
| 114 return std::move(row); |
| 115 } |
| 116 |
| 117 // views::ButtonListener: |
| 118 void ButtonPressed(views::Button* sender, const ui::Event& event) override {} |
| 119 |
| 120 autofill::CreditCard* card_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(PaymentMethodListItem); |
| 123 }; |
| 124 |
35 } // namespace | 125 } // namespace |
36 | 126 |
37 PaymentMethodViewController::PaymentMethodViewController( | 127 PaymentMethodViewController::PaymentMethodViewController( |
38 PaymentRequest* request, | 128 PaymentRequest* request, |
39 PaymentRequestDialogView* dialog) | 129 PaymentRequestDialogView* dialog) |
40 : PaymentRequestSheetController(request, dialog) {} | 130 : PaymentRequestSheetController(request, dialog) { |
| 131 const std::vector<autofill::CreditCard*>& available_cards = |
| 132 request->credit_cards(); |
| 133 |
| 134 for (autofill::CreditCard* card : available_cards) { |
| 135 payment_method_list_.AddItem(base::MakeUnique<PaymentMethodListItem>(card)); |
| 136 } |
| 137 } |
41 | 138 |
42 PaymentMethodViewController::~PaymentMethodViewController() {} | 139 PaymentMethodViewController::~PaymentMethodViewController() {} |
43 | 140 |
44 std::unique_ptr<views::View> PaymentMethodViewController::CreateView() { | 141 std::unique_ptr<views::View> PaymentMethodViewController::CreateView() { |
45 std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>(); | 142 return CreatePaymentView( |
| 143 CreateSheetHeaderView( |
| 144 true, l10n_util::GetStringUTF16( |
| 145 IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME), |
| 146 this), |
| 147 payment_method_list_.CreateListView()); |
| 148 } |
46 | 149 |
47 views::FillLayout* layout = new views::FillLayout(); | 150 std::unique_ptr<views::View> PaymentMethodViewController::CreateExtraView() { |
48 content_view->SetLayoutManager(layout); | 151 std::unique_ptr<views::View> extra_view = base::MakeUnique<views::View>(); |
49 | 152 |
50 // Create the "Add a card" button. | 153 extra_view->SetLayoutManager(new views::BoxLayout( |
| 154 views::BoxLayout::kHorizontal, kPaymentRequestRowHorizontalInsets, |
| 155 kPaymentRequestRowVerticalInsets, kPaymentRequestButtonSpacing)); |
| 156 |
51 views::LabelButton* button = views::MdTextButton::CreateSecondaryUiButton( | 157 views::LabelButton* button = views::MdTextButton::CreateSecondaryUiButton( |
52 this, l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_CAPTION)); | 158 this, l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_CAPTION)); |
53 button->set_tag(static_cast<int>( | 159 button->set_tag(static_cast<int>( |
54 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON)); | 160 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON)); |
55 button->set_id( | 161 button->set_id( |
56 static_cast<int>(DialogViewID::PAYMENT_METHOD_ADD_CARD_BUTTON)); | 162 static_cast<int>(DialogViewID::PAYMENT_METHOD_ADD_CARD_BUTTON)); |
57 content_view->AddChildView(button); | 163 extra_view->AddChildView(button); |
58 | 164 |
59 return CreatePaymentView(CreateSheetHeaderView( | 165 return extra_view; |
60 true, | |
61 l10n_util::GetStringUTF16( | |
62 IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME), | |
63 this), | |
64 std::move(content_view)); | |
65 } | 166 } |
66 | 167 |
67 void PaymentMethodViewController::ButtonPressed(views::Button* sender, | 168 void PaymentMethodViewController::ButtonPressed(views::Button* sender, |
68 const ui::Event& event) { | 169 const ui::Event& event) { |
69 switch (sender->tag()) { | 170 switch (sender->tag()) { |
70 case static_cast<int>( | 171 case static_cast<int>( |
71 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON): | 172 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON): |
72 dialog()->ShowCreditCardEditor(); | 173 dialog()->ShowCreditCardEditor(); |
73 break; | 174 break; |
74 default: | 175 default: |
75 PaymentRequestSheetController::ButtonPressed(sender, event); | 176 PaymentRequestSheetController::ButtonPressed(sender, event); |
76 break; | 177 break; |
77 } | 178 } |
78 } | 179 } |
79 | 180 |
80 } // namespace payments | 181 } // namespace payments |
OLD | NEW |