Chromium Code Reviews| 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" | |
| 29 | |
| 21 | 30 |
| 22 namespace payments { | 31 namespace payments { |
| 23 | 32 |
| 24 namespace { | 33 namespace { |
| 25 | 34 |
| 26 constexpr int kFirstTagValue = static_cast<int>( | 35 constexpr int kFirstTagValue = static_cast<int>( |
| 27 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); | 36 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); |
| 28 | 37 |
| 29 enum class PaymentMethodViewControllerTags : int { | 38 enum class PaymentMethodViewControllerTags : int { |
| 30 // The tag for the button that triggers the "add card" flow. Starts at | 39 // The tag for the button that triggers the "add card" flow. Starts at |
| 31 // |kFirstTagValue| not to conflict with tags common to all views. | 40 // |kFirstTagValue| not to conflict with tags common to all views. |
| 32 ADD_CREDIT_CARD_BUTTON = kFirstTagValue, | 41 ADD_CREDIT_CARD_BUTTON = kFirstTagValue, |
| 33 }; | 42 }; |
| 34 | 43 |
| 44 class PaymentMethodListItem : public payments::PaymentRequestItemList::Item, | |
| 45 public views::ButtonListener { | |
| 46 public: | |
| 47 explicit PaymentMethodListItem(autofill::CreditCard* card) : card_(card) {} | |
|
please use gerrit instead
2017/02/14 00:29:14
// Does not take ownership of |card|, which shoul
anthonyvd
2017/02/22 20:15:21
Yep, done.
| |
| 48 ~PaymentMethodListItem() override {} | |
| 49 | |
| 50 private: | |
| 51 std::unique_ptr<views::View> CreateItemView() override { | |
|
please use gerrit instead
2017/02/14 00:29:14
// payments::PaymentRequestItemList::Item implemen
anthonyvd
2017/02/22 20:15:21
Done.
| |
| 52 std::unique_ptr<PaymentRequestRowView> row = | |
| 53 base::MakeUnique<PaymentRequestRowView>(this); | |
| 54 views::GridLayout* layout = new views::GridLayout(row.get()); | |
| 55 layout->SetInsets(kPaymentRequestRowVerticalInsets, | |
| 56 kPaymentRequestRowHorizontalInsets, | |
| 57 kPaymentRequestRowVerticalInsets, | |
| 58 kPaymentRequestRowHorizontalInsets); | |
| 59 row->SetLayoutManager(layout); | |
| 60 views::ColumnSet* columns = layout->AddColumnSet(0); | |
| 61 | |
| 62 // A column for the masked number and name on card | |
| 63 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, | |
| 64 0, views::GridLayout::USE_PREF, 0, 0); | |
| 65 | |
| 66 // A padding column that resizes to take up the empty space between the | |
| 67 // leading and trailing parts. | |
| 68 columns->AddPaddingColumn(1, 0); | |
| 69 | |
| 70 // A column for the checkmark when the row is selected. | |
| 71 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, | |
| 72 0, views::GridLayout::USE_PREF, 0, 0); | |
| 73 | |
| 74 // A column for the card icon | |
| 75 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, | |
| 76 0, views::GridLayout::USE_PREF, 0, 0); | |
| 77 | |
| 78 // A column for the edit button | |
| 79 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, | |
| 80 0, views::GridLayout::USE_PREF, 0, 0); | |
| 81 | |
| 82 layout->StartRow(0, 0); | |
| 83 std::unique_ptr<views::View> card_info_container = | |
| 84 base::MakeUnique<views::View>(); | |
| 85 card_info_container->set_can_process_events_within_subtree(false); | |
| 86 | |
| 87 std::unique_ptr<views::BoxLayout> box_layout = | |
| 88 base::MakeUnique<views::BoxLayout>(views::BoxLayout::kVertical, | |
| 89 0, | |
| 90 kPaymentRequestRowVerticalInsets, | |
| 91 0); | |
| 92 box_layout->set_cross_axis_alignment( | |
| 93 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); | |
| 94 card_info_container->SetLayoutManager(box_layout.release()); | |
| 95 | |
| 96 card_info_container->AddChildView( | |
| 97 new views::Label(card_->TypeAndLastFourDigits())); | |
| 98 card_info_container->AddChildView(new views::Label( | |
| 99 card_->GetInfo(autofill::AutofillType(autofill::CREDIT_CARD_NAME_FULL), | |
| 100 g_browser_process->GetApplicationLocale()))); | |
| 101 layout->AddView(card_info_container.release()); | |
| 102 | |
| 103 std::unique_ptr<views::ImageView> checkmark = | |
| 104 base::MakeUnique<views::ImageView>(); | |
| 105 checkmark->set_interactive(false); | |
| 106 checkmark->SetImage( | |
| 107 gfx::CreateVectorIcon(views::kMenuCheckIcon, 0xFF609265)); | |
| 108 layout->AddView(checkmark.release()); | |
| 109 | |
| 110 layout->AddView(CreateCreditCardIconView(card_).release()); | |
|
please use gerrit instead
2017/02/14 00:29:14
Great to see use of std::uniqiue_ptr and .release(
anthonyvd
2017/02/22 20:15:21
Yeah I'm glad we agreed on just using unique_ptr e
| |
| 111 | |
| 112 return std::move(row); | |
| 113 } | |
| 114 | |
| 115 // views::ButtonListener: | |
| 116 void ButtonPressed(views::Button* sender, const ui::Event& event) override { | |
| 117 | |
|
please use gerrit instead
2017/02/14 00:29:14
Remove extra line.
anthonyvd
2017/02/22 20:15:21
Done.
| |
| 118 } | |
| 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 std::vector<autofill::CreditCard*> available_cards = request->credit_cards(); | |
|
please use gerrit instead
2017/02/14 00:29:14
This makes a copy of the vector, which is expensiv
anthonyvd
2017/02/22 20:15:21
Good catch, done! (Although I would hope that the
| |
| 132 | |
| 133 for (autofill::CreditCard* card : available_cards) { | |
| 134 payment_method_list_.AddItem(base::MakeUnique<PaymentMethodListItem>(card)); | |
| 135 } | |
| 136 } | |
| 41 | 137 |
| 42 PaymentMethodViewController::~PaymentMethodViewController() {} | 138 PaymentMethodViewController::~PaymentMethodViewController() {} |
| 43 | 139 |
| 44 std::unique_ptr<views::View> PaymentMethodViewController::CreateView() { | 140 std::unique_ptr<views::View> PaymentMethodViewController::CreateView() { |
| 45 std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>(); | 141 return CreatePaymentView(CreateSheetHeaderView( |
| 142 true, | |
| 143 l10n_util::GetStringUTF16( | |
| 144 IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME), | |
| 145 this), | |
| 146 payment_method_list_.CreateListView()); | |
| 147 } | |
| 46 | 148 |
| 47 views::FillLayout* layout = new views::FillLayout(); | 149 std::unique_ptr<views::View> PaymentMethodViewController::CreateExtraView() { |
| 48 content_view->SetLayoutManager(layout); | 150 std::unique_ptr<views::View> extra_view = base::MakeUnique<views::View>(); |
| 49 | 151 |
| 50 // Create the "Add a card" button. | 152 extra_view->SetLayoutManager(new views::BoxLayout( |
| 153 views::BoxLayout::kHorizontal, | |
| 154 kPaymentRequestRowHorizontalInsets, | |
| 155 kPaymentRequestRowVerticalInsets, | |
| 156 kPaymentRequestButtonSpacing)); | |
| 157 | |
| 51 views::LabelButton* button = views::MdTextButton::CreateSecondaryUiButton( | 158 views::LabelButton* button = views::MdTextButton::CreateSecondaryUiButton( |
| 52 this, l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_CAPTION)); | 159 this, l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_CAPTION)); |
| 53 button->set_tag(static_cast<int>( | 160 button->set_tag(static_cast<int>( |
| 54 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON)); | 161 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON)); |
| 55 button->set_id( | 162 button->set_id( |
| 56 static_cast<int>(DialogViewID::PAYMENT_METHOD_ADD_CARD_BUTTON)); | 163 static_cast<int>(DialogViewID::PAYMENT_METHOD_ADD_CARD_BUTTON)); |
| 57 content_view->AddChildView(button); | 164 extra_view->AddChildView(button); |
| 58 | 165 |
| 59 return CreatePaymentView(CreateSheetHeaderView( | 166 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 } | 167 } |
| 66 | 168 |
| 67 void PaymentMethodViewController::ButtonPressed(views::Button* sender, | 169 void PaymentMethodViewController::ButtonPressed(views::Button* sender, |
| 68 const ui::Event& event) { | 170 const ui::Event& event) { |
| 69 switch (sender->tag()) { | 171 switch (sender->tag()) { |
| 70 case static_cast<int>( | 172 case static_cast<int>( |
| 71 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON): | 173 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON): |
| 72 dialog()->ShowCreditCardEditor(); | 174 dialog()->ShowCreditCardEditor(); |
| 73 break; | 175 break; |
| 74 default: | 176 default: |
| 75 PaymentRequestSheetController::ButtonPressed(sender, event); | 177 PaymentRequestSheetController::ButtonPressed(sender, event); |
| 76 break; | 178 break; |
| 77 } | 179 } |
| 78 } | 180 } |
| 79 | 181 |
| 80 } // namespace payments | 182 } // namespace payments |
| OLD | NEW |