Chromium Code Reviews| 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 <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" | 17 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
| 18 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" | 18 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" |
| 19 #include "chrome/browser/ui/views/payments/payment_request_row_view.h" | 19 #include "chrome/browser/ui/views/payments/payment_request_row_view.h" |
| 20 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" | 20 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" |
| 21 #include "chrome/grit/generated_resources.h" | 21 #include "chrome/grit/generated_resources.h" |
| 22 #include "components/autofill/core/browser/autofill_type.h" | 22 #include "components/autofill/core/browser/autofill_type.h" |
| 23 #include "components/autofill/core/browser/credit_card.h" | 23 #include "components/autofill/core/browser/credit_card.h" |
| 24 #include "components/autofill/core/browser/field_types.h" | 24 #include "components/autofill/core/browser/field_types.h" |
| 25 #include "components/autofill/core/browser/personal_data_manager.h" | 25 #include "components/autofill/core/browser/personal_data_manager.h" |
| 26 #include "components/payments/content/payment_request.h" | |
| 27 #include "components/payments/core/currency_formatter.h" | 26 #include "components/payments/core/currency_formatter.h" |
| 28 #include "components/strings/grit/components_strings.h" | 27 #include "components/strings/grit/components_strings.h" |
| 29 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 30 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 31 #include "ui/gfx/color_utils.h" | 30 #include "ui/gfx/color_utils.h" |
| 32 #include "ui/gfx/font.h" | 31 #include "ui/gfx/font.h" |
| 33 #include "ui/gfx/paint_vector_icon.h" | 32 #include "ui/gfx/paint_vector_icon.h" |
| 34 #include "ui/gfx/range/range.h" | 33 #include "ui/gfx/range/range.h" |
| 35 #include "ui/views/controls/button/label_button.h" | |
| 36 #include "ui/views/controls/button/md_text_button.h" | 34 #include "ui/views/controls/button/md_text_button.h" |
| 37 #include "ui/views/controls/image_view.h" | 35 #include "ui/views/controls/image_view.h" |
| 38 #include "ui/views/controls/label.h" | 36 #include "ui/views/controls/label.h" |
| 39 #include "ui/views/controls/styled_label.h" | 37 #include "ui/views/controls/styled_label.h" |
| 40 #include "ui/views/layout/fill_layout.h" | 38 #include "ui/views/layout/fill_layout.h" |
| 41 #include "ui/views/layout/grid_layout.h" | 39 #include "ui/views/layout/grid_layout.h" |
| 42 #include "ui/views/vector_icons.h" | 40 #include "ui/views/vector_icons.h" |
| 43 #include "ui/views/view.h" | 41 #include "ui/views/view.h" |
| 44 | 42 |
| 45 namespace payments { | 43 namespace payments { |
| 46 namespace { | 44 namespace { |
| 47 | 45 |
| 48 constexpr int kFirstTagValue = static_cast<int>( | 46 constexpr int kFirstTagValue = static_cast<int>( |
| 49 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); | 47 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); |
| 50 | 48 |
| 51 enum class PaymentSheetViewControllerTags { | 49 enum class PaymentSheetViewControllerTags { |
| 52 // 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. |
| 53 SHOW_ORDER_SUMMARY_BUTTON = kFirstTagValue, | 51 SHOW_ORDER_SUMMARY_BUTTON = kFirstTagValue, |
| 54 SHOW_SHIPPING_BUTTON, | 52 SHOW_SHIPPING_BUTTON, |
| 55 SHOW_PAYMENT_METHOD_BUTTON, | 53 SHOW_PAYMENT_METHOD_BUTTON, |
| 56 SHOW_CONTACT_INFO_BUTTON, | 54 SHOW_CONTACT_INFO_BUTTON, |
| 55 PAY_BUTTON | |
| 57 }; | 56 }; |
| 58 | 57 |
| 59 int ComputeWidestNameColumnViewWidth() { | 58 int ComputeWidestNameColumnViewWidth() { |
| 60 // The name colums in each row should all have the same width, large enough to | 59 // The name colums in each row should all have the same width, large enough to |
| 61 // accomodate the longest piece of text they contain. Because of this, each | 60 // accomodate the longest piece of text they contain. Because of this, each |
| 62 // row's GridLayout requires its first column to have a fixed width of the | 61 // row's GridLayout requires its first column to have a fixed width of the |
| 63 // correct size. To measure the required size, layout a label with each | 62 // correct size. To measure the required size, layout a label with each |
| 64 // section name, measure its width, then initialize |widest_column_width| | 63 // section name, measure its width, then initialize |widest_column_width| |
| 65 // with the largest value. | 64 // with the largest value. |
| 66 std::vector<int> section_names{ | 65 std::vector<int> section_names{ |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 | 159 |
| 161 return std::move(row); | 160 return std::move(row); |
| 162 } | 161 } |
| 163 | 162 |
| 164 } // namespace | 163 } // namespace |
| 165 | 164 |
| 166 PaymentSheetViewController::PaymentSheetViewController( | 165 PaymentSheetViewController::PaymentSheetViewController( |
| 167 PaymentRequest* request, | 166 PaymentRequest* request, |
| 168 PaymentRequestDialogView* dialog) | 167 PaymentRequestDialogView* dialog) |
| 169 : PaymentRequestSheetController(request, dialog), | 168 : PaymentRequestSheetController(request, dialog), |
| 170 widest_name_column_view_width_(ComputeWidestNameColumnViewWidth()) {} | 169 pay_button_(nullptr), |
| 170 widest_name_column_view_width_(ComputeWidestNameColumnViewWidth()) { | |
| 171 request->AddObserver(this); | |
| 172 } | |
| 171 | 173 |
| 172 PaymentSheetViewController::~PaymentSheetViewController() {} | 174 PaymentSheetViewController::~PaymentSheetViewController() { |
| 175 request()->RemoveObserver(this); | |
| 176 } | |
| 173 | 177 |
| 174 std::unique_ptr<views::View> PaymentSheetViewController::CreateView() { | 178 std::unique_ptr<views::View> PaymentSheetViewController::CreateView() { |
| 175 std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>(); | 179 std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>(); |
| 176 | 180 |
| 177 views::GridLayout* layout = new views::GridLayout(content_view.get()); | 181 views::GridLayout* layout = new views::GridLayout(content_view.get()); |
| 178 content_view->SetLayoutManager(layout); | 182 content_view->SetLayoutManager(layout); |
| 179 views::ColumnSet* columns = layout->AddColumnSet(0); | 183 views::ColumnSet* columns = layout->AddColumnSet(0); |
| 180 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, | 184 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, |
| 181 1, views::GridLayout::USE_PREF, 0, 0); | 185 1, views::GridLayout::USE_PREF, 0, 0); |
| 182 | 186 |
| 183 layout->StartRow(0, 0); | 187 layout->StartRow(0, 0); |
| 184 layout->AddView(CreatePaymentSheetSummaryRow().release()); | 188 layout->AddView(CreatePaymentSheetSummaryRow().release()); |
| 185 layout->StartRow(1, 0); | 189 layout->StartRow(1, 0); |
| 186 layout->AddView(CreateShippingRow().release()); | 190 layout->AddView(CreateShippingRow().release()); |
| 187 layout->StartRow(0, 0); | 191 layout->StartRow(0, 0); |
| 188 layout->AddView(CreatePaymentMethodRow().release()); | 192 layout->AddView(CreatePaymentMethodRow().release()); |
| 189 layout->StartRow(1, 0); | 193 layout->StartRow(1, 0); |
| 190 layout->AddView(CreateContactInfoRow().release()); | 194 layout->AddView(CreateContactInfoRow().release()); |
| 191 | 195 |
| 192 return CreatePaymentView( | 196 return CreatePaymentView( |
| 193 CreateSheetHeaderView( | 197 CreateSheetHeaderView( |
| 194 false, | 198 false, |
| 195 l10n_util::GetStringUTF16(IDS_PAYMENT_REQUEST_PAYMENT_SHEET_TITLE), | 199 l10n_util::GetStringUTF16(IDS_PAYMENT_REQUEST_PAYMENT_SHEET_TITLE), |
| 196 this), | 200 this), |
| 197 std::move(content_view)); | 201 std::move(content_view)); |
| 198 } | 202 } |
| 199 | 203 |
| 204 std::unique_ptr<views::Button> | |
| 205 PaymentSheetViewController::CreatePrimaryButton() { | |
| 206 std::unique_ptr<views::Button> button( | |
| 207 views::MdTextButton::CreateSecondaryUiBlueButton( | |
| 208 this, l10n_util::GetStringUTF16(IDS_PAYMENTS_PAY_BUTTON))); | |
| 209 button->set_tag(static_cast<int>(PaymentSheetViewControllerTags::PAY_BUTTON)); | |
| 210 button->set_id(static_cast<int>(DialogViewID::PAYMENT_SHEET_PAY_BUTTON)); | |
| 211 pay_button_ = button.get(); | |
| 212 UpdatePayButtonState(request()->is_ready_to_pay()); | |
| 213 return button; | |
| 214 } | |
| 215 | |
| 216 void PaymentSheetViewController::OnSelectedInformationChanged() { | |
| 217 UpdatePayButtonState(request()->is_ready_to_pay()); | |
| 218 } | |
| 219 | |
| 200 void PaymentSheetViewController::ButtonPressed( | 220 void PaymentSheetViewController::ButtonPressed( |
| 201 views::Button* sender, const ui::Event& event) { | 221 views::Button* sender, const ui::Event& event) { |
| 202 switch (sender->tag()) { | 222 switch (sender->tag()) { |
| 203 case static_cast<int>( | 223 case static_cast<int>( |
| 204 PaymentSheetViewControllerTags::SHOW_ORDER_SUMMARY_BUTTON): | 224 PaymentSheetViewControllerTags::SHOW_ORDER_SUMMARY_BUTTON): |
| 205 dialog()->ShowOrderSummary(); | 225 dialog()->ShowOrderSummary(); |
| 206 break; | 226 break; |
| 207 | 227 |
| 208 case static_cast<int>(PaymentSheetViewControllerTags::SHOW_SHIPPING_BUTTON): | 228 case static_cast<int>(PaymentSheetViewControllerTags::SHOW_SHIPPING_BUTTON): |
| 209 dialog()->ShowShippingListSheet(); | 229 dialog()->ShowShippingListSheet(); |
| 210 break; | 230 break; |
| 211 | 231 |
| 212 case static_cast<int>( | 232 case static_cast<int>( |
| 213 PaymentSheetViewControllerTags::SHOW_PAYMENT_METHOD_BUTTON): | 233 PaymentSheetViewControllerTags::SHOW_PAYMENT_METHOD_BUTTON): |
| 214 dialog()->ShowPaymentMethodSheet(); | 234 dialog()->ShowPaymentMethodSheet(); |
| 215 break; | 235 break; |
| 216 | 236 |
| 217 case static_cast<int>( | 237 case static_cast<int>( |
| 218 PaymentSheetViewControllerTags::SHOW_CONTACT_INFO_BUTTON): | 238 PaymentSheetViewControllerTags::SHOW_CONTACT_INFO_BUTTON): |
| 219 // TODO(tmartino): Transition to contact info page once it exists. | 239 // TODO(tmartino): Transition to contact info page once it exists. |
| 220 break; | 240 break; |
| 221 | 241 |
| 242 case static_cast<int>(PaymentSheetViewControllerTags::PAY_BUTTON): | |
|
anthonyvd
2017/02/28 21:07:18
You can handle this in the base PaymentRequestShee
Mathieu
2017/02/28 21:43:27
Done.
| |
| 243 request()->Pay(); | |
| 244 break; | |
| 245 | |
| 222 default: | 246 default: |
| 223 PaymentRequestSheetController::ButtonPressed(sender, event); | 247 PaymentRequestSheetController::ButtonPressed(sender, event); |
| 224 break; | 248 break; |
| 225 } | 249 } |
| 226 } | 250 } |
| 227 | 251 |
| 252 void PaymentSheetViewController::UpdatePayButtonState(bool enabled) { | |
| 253 pay_button_->SetEnabled(enabled); | |
| 254 } | |
| 255 | |
| 228 std::unique_ptr<views::View> | 256 std::unique_ptr<views::View> |
| 229 PaymentSheetViewController::CreateOrderSummarySectionContent() { | 257 PaymentSheetViewController::CreateOrderSummarySectionContent() { |
| 230 CurrencyFormatter* formatter = request()->GetOrCreateCurrencyFormatter( | 258 CurrencyFormatter* formatter = request()->GetOrCreateCurrencyFormatter( |
| 231 request()->details()->total->amount->currency, | 259 request()->details()->total->amount->currency, |
| 232 request()->details()->total->amount->currency_system, | 260 request()->details()->total->amount->currency_system, |
| 233 g_browser_process->GetApplicationLocale()); | 261 g_browser_process->GetApplicationLocale()); |
| 234 base::string16 label_value = l10n_util::GetStringFUTF16( | 262 base::string16 label_value = l10n_util::GetStringFUTF16( |
| 235 IDS_PAYMENT_REQUEST_ORDER_SUMMARY_SECTION_TOTAL_FORMAT, | 263 IDS_PAYMENT_REQUEST_ORDER_SUMMARY_SECTION_TOTAL_FORMAT, |
| 236 base::UTF8ToUTF16(request()->details()->total->label), | 264 base::UTF8ToUTF16(request()->details()->total->label), |
| 237 base::UTF8ToUTF16(formatter->formatted_currency_code()), | 265 base::UTF8ToUTF16(formatter->formatted_currency_code()), |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 CreateContactInfoSectionContent(), std::unique_ptr<views::View>(nullptr), | 392 CreateContactInfoSectionContent(), std::unique_ptr<views::View>(nullptr), |
| 365 widest_name_column_view_width_); | 393 widest_name_column_view_width_); |
| 366 section->set_tag(static_cast<int>( | 394 section->set_tag(static_cast<int>( |
| 367 PaymentSheetViewControllerTags::SHOW_CONTACT_INFO_BUTTON)); | 395 PaymentSheetViewControllerTags::SHOW_CONTACT_INFO_BUTTON)); |
| 368 section->set_id( | 396 section->set_id( |
| 369 static_cast<int>(DialogViewID::PAYMENT_SHEET_CONTACT_INFO_SECTION)); | 397 static_cast<int>(DialogViewID::PAYMENT_SHEET_CONTACT_INFO_SECTION)); |
| 370 return section; | 398 return section; |
| 371 } | 399 } |
| 372 | 400 |
| 373 } // namespace payments | 401 } // namespace payments |
| OLD | NEW |