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_id( |
104 checkmark->set_can_process_events_within_subtree(false); | 114 static_cast<int>(DialogViewID::PAYMENT_METHOD_ITEM_CHECKMARK_VIEW)); |
105 checkmark->SetImage( | 115 checkmark_->set_owned_by_client(); |
| 116 checkmark_->set_can_process_events_within_subtree(false); |
| 117 checkmark_->SetImage( |
106 gfx::CreateVectorIcon(views::kMenuCheckIcon, 0xFF609265)); | 118 gfx::CreateVectorIcon(views::kMenuCheckIcon, 0xFF609265)); |
107 layout->AddView(checkmark.release()); | 119 layout->AddView(checkmark_.get()); |
| 120 if (!selected()) |
| 121 checkmark_->SetVisible(false); |
108 | 122 |
109 std::unique_ptr<views::ImageView> card_icon_view = | 123 std::unique_ptr<views::ImageView> card_icon_view = |
110 CreateCardIconView(card_->type()); | 124 CreateCardIconView(card_->type()); |
111 card_icon_view->SetImageSize(gfx::Size(32, 20)); | 125 card_icon_view->SetImageSize(gfx::Size(32, 20)); |
112 layout->AddView(card_icon_view.release()); | 126 layout->AddView(card_icon_view.release()); |
113 | 127 |
114 return std::move(row); | 128 return std::move(row); |
115 } | 129 } |
116 | 130 |
| 131 void SelectedStateChanged() override { |
| 132 // This could be called before CreateItemView, so before |checkmark_| is |
| 133 // instantiated. |
| 134 if (checkmark_) |
| 135 checkmark_->SetVisible(selected()); |
| 136 |
| 137 request()->set_selected_credit_card(card_); |
| 138 } |
| 139 |
117 // views::ButtonListener: | 140 // views::ButtonListener: |
118 void ButtonPressed(views::Button* sender, const ui::Event& event) override {} | 141 void ButtonPressed(views::Button* sender, const ui::Event& event) override { |
| 142 if (IsComplete()) { |
| 143 list()->SelectItem(this); |
| 144 } else { |
| 145 // TODO(anthonyvd): Display the editor, pre-populated with the data that |
| 146 // already exists in |card|. |
| 147 } |
| 148 } |
| 149 |
| 150 bool IsComplete() const { |
| 151 // TODO(anthonyvd): Hook this up to the card completedness logic when it's |
| 152 // implemented in PaymentRequest. |
| 153 return true; |
| 154 } |
119 | 155 |
120 autofill::CreditCard* card_; | 156 autofill::CreditCard* card_; |
| 157 std::unique_ptr<views::ImageView> checkmark_; |
121 | 158 |
122 DISALLOW_COPY_AND_ASSIGN(PaymentMethodListItem); | 159 DISALLOW_COPY_AND_ASSIGN(PaymentMethodListItem); |
123 }; | 160 }; |
124 | 161 |
125 } // namespace | 162 } // namespace |
126 | 163 |
127 PaymentMethodViewController::PaymentMethodViewController( | 164 PaymentMethodViewController::PaymentMethodViewController( |
128 PaymentRequest* request, | 165 PaymentRequest* request, |
129 PaymentRequestDialogView* dialog) | 166 PaymentRequestDialogView* dialog) |
130 : PaymentRequestSheetController(request, dialog) { | 167 : PaymentRequestSheetController(request, dialog) { |
131 const std::vector<autofill::CreditCard*>& available_cards = | 168 const std::vector<autofill::CreditCard*>& available_cards = |
132 request->credit_cards(); | 169 request->credit_cards(); |
133 | 170 |
134 for (autofill::CreditCard* card : available_cards) { | 171 for (autofill::CreditCard* card : available_cards) { |
135 payment_method_list_.AddItem(base::MakeUnique<PaymentMethodListItem>(card)); | 172 std::unique_ptr<PaymentMethodListItem> item = |
| 173 base::MakeUnique<PaymentMethodListItem>( |
| 174 card, request, &payment_method_list_, |
| 175 card == request->selected_credit_card()); |
| 176 payment_method_list_.AddItem(std::move(item)); |
136 } | 177 } |
137 } | 178 } |
138 | 179 |
139 PaymentMethodViewController::~PaymentMethodViewController() {} | 180 PaymentMethodViewController::~PaymentMethodViewController() {} |
140 | 181 |
141 std::unique_ptr<views::View> PaymentMethodViewController::CreateView() { | 182 std::unique_ptr<views::View> PaymentMethodViewController::CreateView() { |
| 183 std::unique_ptr<views::View> list_view = |
| 184 payment_method_list_.CreateListView(); |
| 185 list_view->set_id( |
| 186 static_cast<int>(DialogViewID::PAYMENT_METHOD_SHEET_LIST_VIEW)); |
142 return CreatePaymentView( | 187 return CreatePaymentView( |
143 CreateSheetHeaderView( | 188 CreateSheetHeaderView( |
144 true, l10n_util::GetStringUTF16( | 189 true, |
145 IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME), | 190 l10n_util::GetStringUTF16( |
| 191 IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME), |
146 this), | 192 this), |
147 payment_method_list_.CreateListView()); | 193 std::move(list_view)); |
148 } | 194 } |
149 | 195 |
150 std::unique_ptr<views::View> | 196 std::unique_ptr<views::View> |
151 PaymentMethodViewController::CreateExtraFooterView() { | 197 PaymentMethodViewController::CreateExtraFooterView() { |
152 std::unique_ptr<views::View> extra_view = base::MakeUnique<views::View>(); | 198 std::unique_ptr<views::View> extra_view = base::MakeUnique<views::View>(); |
153 | 199 |
154 extra_view->SetLayoutManager(new views::BoxLayout( | 200 extra_view->SetLayoutManager(new views::BoxLayout( |
155 views::BoxLayout::kHorizontal, kPaymentRequestRowHorizontalInsets, | 201 views::BoxLayout::kHorizontal, kPaymentRequestRowHorizontalInsets, |
156 kPaymentRequestRowVerticalInsets, kPaymentRequestButtonSpacing)); | 202 kPaymentRequestRowVerticalInsets, kPaymentRequestButtonSpacing)); |
157 | 203 |
(...skipping 15 matching lines...) Expand all Loading... |
173 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON): | 219 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON): |
174 dialog()->ShowCreditCardEditor(); | 220 dialog()->ShowCreditCardEditor(); |
175 break; | 221 break; |
176 default: | 222 default: |
177 PaymentRequestSheetController::ButtonPressed(sender, event); | 223 PaymentRequestSheetController::ButtonPressed(sender, event); |
178 break; | 224 break; |
179 } | 225 } |
180 } | 226 } |
181 | 227 |
182 } // namespace payments | 228 } // namespace payments |
OLD | NEW |