| 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" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 enum class PaymentMethodViewControllerTags : int { | 37 enum class PaymentMethodViewControllerTags : int { |
| 38 // 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 |
| 39 // |kFirstTagValue| not to conflict with tags common to all views. | 39 // |kFirstTagValue| not to conflict with tags common to all views. |
| 40 ADD_CREDIT_CARD_BUTTON = kFirstTagValue, | 40 ADD_CREDIT_CARD_BUTTON = kFirstTagValue, |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 class PaymentMethodListItem : public payments::PaymentRequestItemList::Item, | 43 class PaymentMethodListItem : public payments::PaymentRequestItemList::Item, |
| 44 public views::ButtonListener { | 44 public views::ButtonListener { |
| 45 public: | 45 public: |
| 46 // Does not take ownership of |card|, which should not be null and should | 46 // Does not take ownership of |card|, which should not be null and should |
| 47 // outlive this object. | 47 // outlive this object. |list| is the PaymentRequestItemList object that will |
| 48 explicit PaymentMethodListItem(autofill::CreditCard* card) : card_(card) {} | 48 // own this. |
| 49 PaymentMethodListItem(autofill::CreditCard* card, |
| 50 PaymentRequest* request, |
| 51 PaymentRequestItemList* list, |
| 52 bool selected) |
| 53 : payments::PaymentRequestItemList::Item(request, list, selected), |
| 54 card_(card) {} |
| 49 ~PaymentMethodListItem() override {} | 55 ~PaymentMethodListItem() override {} |
| 50 | 56 |
| 51 private: | 57 private: |
| 52 // payments::PaymentRequestItemList::Item: | 58 // payments::PaymentRequestItemList::Item: |
| 53 std::unique_ptr<views::View> CreateItemView() override { | 59 std::unique_ptr<views::View> CreateItemView() override { |
| 54 std::unique_ptr<PaymentRequestRowView> row = | 60 std::unique_ptr<PaymentRequestRowView> row = |
| 55 base::MakeUnique<PaymentRequestRowView>(this); | 61 base::MakeUnique<PaymentRequestRowView>(this); |
| 56 views::GridLayout* layout = new views::GridLayout(row.get()); | 62 views::GridLayout* layout = new views::GridLayout(row.get()); |
| 57 layout->SetInsets( | 63 layout->SetInsets( |
| 58 kPaymentRequestRowVerticalInsets, kPaymentRequestRowHorizontalInsets, | 64 kPaymentRequestRowVerticalInsets, kPaymentRequestRowHorizontalInsets, |
| 59 kPaymentRequestRowVerticalInsets, kPaymentRequestRowHorizontalInsets); | 65 kPaymentRequestRowVerticalInsets, kPaymentRequestRowHorizontalInsets); |
| 60 row->SetLayoutManager(layout); | 66 row->SetLayoutManager(layout); |
| 61 views::ColumnSet* columns = layout->AddColumnSet(0); | 67 views::ColumnSet* columns = layout->AddColumnSet(0); |
| 62 | 68 |
| 63 // A column for the masked number and name on card | 69 // A column for the masked number and name on card |
| 64 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, | 70 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
| 65 views::GridLayout::USE_PREF, 0, 0); | 71 views::GridLayout::USE_PREF, 0, 0); |
| 66 | 72 |
| 67 // A padding column that resizes to take up the empty space between the | 73 // A padding column that resizes to take up the empty space between the |
| 68 // leading and trailing parts. | 74 // leading and trailing parts. |
| 69 columns->AddPaddingColumn(1, 0); | 75 columns->AddPaddingColumn(1, 0); |
| 70 | 76 |
| 71 // A column for the checkmark when the row is selected. | 77 // A column for the checkmark when the row is selected. |
| 72 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, | 78 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, |
| 73 0, views::GridLayout::USE_PREF, 0, 0); | 79 0, views::GridLayout::USE_PREF, 0, 0); |
| 74 | 80 |
| 81 columns->AddPaddingColumn(0, kPaymentRequestButtonSpacing); |
| 82 |
| 75 // A column for the card icon | 83 // A column for the card icon |
| 76 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, | 84 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, |
| 77 0, views::GridLayout::USE_PREF, 0, 0); | 85 0, views::GridLayout::USE_PREF, 0, 0); |
| 78 | 86 |
| 79 // A column for the edit button | 87 // A column for the edit button |
| 80 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, | 88 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, |
| 81 0, views::GridLayout::USE_PREF, 0, 0); | 89 0, views::GridLayout::USE_PREF, 0, 0); |
| 82 | 90 |
| 83 layout->StartRow(0, 0); | 91 layout->StartRow(0, 0); |
| 84 std::unique_ptr<views::View> card_info_container = | 92 std::unique_ptr<views::View> card_info_container = |
| 85 base::MakeUnique<views::View>(); | 93 base::MakeUnique<views::View>(); |
| 86 card_info_container->set_can_process_events_within_subtree(false); | 94 card_info_container->set_can_process_events_within_subtree(false); |
| 87 | 95 |
| 88 std::unique_ptr<views::BoxLayout> box_layout = | 96 std::unique_ptr<views::BoxLayout> box_layout = |
| 89 base::MakeUnique<views::BoxLayout>(views::BoxLayout::kVertical, 0, | 97 base::MakeUnique<views::BoxLayout>(views::BoxLayout::kVertical, 0, |
| 90 kPaymentRequestRowVerticalInsets, 0); | 98 kPaymentRequestRowVerticalInsets, 0); |
| 91 box_layout->set_cross_axis_alignment( | 99 box_layout->set_cross_axis_alignment( |
| 92 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); | 100 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); |
| 93 card_info_container->SetLayoutManager(box_layout.release()); | 101 card_info_container->SetLayoutManager(box_layout.release()); |
| 94 | 102 |
| 95 card_info_container->AddChildView( | 103 card_info_container->AddChildView( |
| 96 new views::Label(card_->TypeAndLastFourDigits())); | 104 new views::Label(card_->TypeAndLastFourDigits())); |
| 97 card_info_container->AddChildView(new views::Label( | 105 card_info_container->AddChildView(new views::Label( |
| 98 card_->GetInfo(autofill::AutofillType(autofill::CREDIT_CARD_NAME_FULL), | 106 card_->GetInfo(autofill::AutofillType(autofill::CREDIT_CARD_NAME_FULL), |
| 99 g_browser_process->GetApplicationLocale()))); | 107 g_browser_process->GetApplicationLocale()))); |
| 108 // TODO(anthonyvd): Add the "card is incomplete" label once the |
| 109 // completedness logic is implemented. |
| 100 layout->AddView(card_info_container.release()); | 110 layout->AddView(card_info_container.release()); |
| 101 | 111 |
| 102 std::unique_ptr<views::ImageView> checkmark = | 112 checkmark_ = base::MakeUnique<views::ImageView>(); |
| 103 base::MakeUnique<views::ImageView>(); | 113 checkmark_->set_owned_by_client(); |
| 104 checkmark->set_interactive(false); | 114 checkmark_->set_interactive(false); |
| 105 checkmark->SetImage( | 115 checkmark_->SetImage( |
| 106 gfx::CreateVectorIcon(views::kMenuCheckIcon, 0xFF609265)); | 116 gfx::CreateVectorIcon(views::kMenuCheckIcon, 0xFF609265)); |
| 107 layout->AddView(checkmark.release()); | 117 layout->AddView(checkmark_.get()); |
| 118 if (!selected()) |
| 119 checkmark_->SetVisible(false); |
| 108 | 120 |
| 109 std::unique_ptr<views::ImageView> card_icon_view = | 121 std::unique_ptr<views::ImageView> card_icon_view = |
| 110 CreateCardIconView(card_->type()); | 122 CreateCardIconView(card_->type()); |
| 111 card_icon_view->SetImageSize(gfx::Size(32, 20)); | 123 card_icon_view->SetImageSize(gfx::Size(32, 20)); |
| 112 layout->AddView(card_icon_view.release()); | 124 layout->AddView(card_icon_view.release()); |
| 113 | 125 |
| 114 return std::move(row); | 126 return std::move(row); |
| 115 } | 127 } |
| 116 | 128 |
| 129 void SelectedStateChanged() override { |
| 130 // This could be called before CreateItemView, so before |checkmark_| is |
| 131 // instantiated. |
| 132 if (checkmark_) |
| 133 checkmark_->SetVisible(selected()); |
| 134 |
| 135 request()->set_selected_credit_card(card_); |
| 136 } |
| 137 |
| 117 // views::ButtonListener: | 138 // views::ButtonListener: |
| 118 void ButtonPressed(views::Button* sender, const ui::Event& event) override {} | 139 void ButtonPressed(views::Button* sender, const ui::Event& event) override { |
| 140 if (IsComplete()) { |
| 141 list()->SelectItem(this); |
| 142 } else { |
| 143 // TODO(anthonyvd): Display the editor, pre-populated with the data that |
| 144 // already exists in |card|. |
| 145 } |
| 146 } |
| 147 |
| 148 bool IsComplete() const { |
| 149 // TODO(anthonyvd): Hook this up to the card completedness logic when it's |
| 150 // implemented in PaymentRequest. |
| 151 return true; |
| 152 } |
| 119 | 153 |
| 120 autofill::CreditCard* card_; | 154 autofill::CreditCard* card_; |
| 155 std::unique_ptr<views::ImageView> checkmark_; |
| 121 | 156 |
| 122 DISALLOW_COPY_AND_ASSIGN(PaymentMethodListItem); | 157 DISALLOW_COPY_AND_ASSIGN(PaymentMethodListItem); |
| 123 }; | 158 }; |
| 124 | 159 |
| 125 } // namespace | 160 } // namespace |
| 126 | 161 |
| 127 PaymentMethodViewController::PaymentMethodViewController( | 162 PaymentMethodViewController::PaymentMethodViewController( |
| 128 PaymentRequest* request, | 163 PaymentRequest* request, |
| 129 PaymentRequestDialogView* dialog) | 164 PaymentRequestDialogView* dialog) |
| 130 : PaymentRequestSheetController(request, dialog) { | 165 : PaymentRequestSheetController(request, dialog) { |
| 131 const std::vector<autofill::CreditCard*>& available_cards = | 166 const std::vector<autofill::CreditCard*>& available_cards = |
| 132 request->credit_cards(); | 167 request->credit_cards(); |
| 133 | 168 |
| 134 for (autofill::CreditCard* card : available_cards) { | 169 for (autofill::CreditCard* card : available_cards) { |
| 135 payment_method_list_.AddItem(base::MakeUnique<PaymentMethodListItem>(card)); | 170 std::unique_ptr<PaymentMethodListItem> item = |
| 171 base::MakeUnique<PaymentMethodListItem>( |
| 172 card, request, &payment_method_list_, |
| 173 card == request->selected_credit_card()); |
| 174 payment_method_list_.AddItem(std::move(item)); |
| 136 } | 175 } |
| 137 } | 176 } |
| 138 | 177 |
| 139 PaymentMethodViewController::~PaymentMethodViewController() {} | 178 PaymentMethodViewController::~PaymentMethodViewController() {} |
| 140 | 179 |
| 141 std::unique_ptr<views::View> PaymentMethodViewController::CreateView() { | 180 std::unique_ptr<views::View> PaymentMethodViewController::CreateView() { |
| 142 return CreatePaymentView( | 181 return CreatePaymentView( |
| 143 CreateSheetHeaderView( | 182 CreateSheetHeaderView( |
| 144 true, l10n_util::GetStringUTF16( | 183 true, l10n_util::GetStringUTF16( |
| 145 IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME), | 184 IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME), |
| (...skipping 27 matching lines...) Expand all Loading... |
| 173 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON): | 212 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON): |
| 174 dialog()->ShowCreditCardEditor(); | 213 dialog()->ShowCreditCardEditor(); |
| 175 break; | 214 break; |
| 176 default: | 215 default: |
| 177 PaymentRequestSheetController::ButtonPressed(sender, event); | 216 PaymentRequestSheetController::ButtonPressed(sender, event); |
| 178 break; | 217 break; |
| 179 } | 218 } |
| 180 } | 219 } |
| 181 | 220 |
| 182 } // namespace payments | 221 } // namespace payments |
| OLD | NEW |