| 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/editor_view_controller.h" | 5 #include "chrome/browser/ui/views/payments/editor_view_controller.h" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include <utility> | 8 #include <utility> |
| 8 | 9 |
| 10 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" | 12 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
| 11 #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" |
| 12 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" | 14 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" |
| 13 #include "chrome/grit/generated_resources.h" | 15 #include "chrome/grit/generated_resources.h" |
| 14 #include "components/payments/payment_request.h" | 16 #include "components/payments/payment_request.h" |
| 15 #include "components/strings/grit/components_strings.h" | 17 #include "components/strings/grit/components_strings.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/views/border.h" | 19 #include "ui/views/border.h" |
| 18 #include "ui/views/controls/button/label_button.h" | 20 #include "ui/views/controls/button/label_button.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 29 | 31 |
| 30 constexpr int kFirstTagValue = static_cast<int>( | 32 constexpr int kFirstTagValue = static_cast<int>( |
| 31 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); | 33 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); |
| 32 | 34 |
| 33 enum class EditorViewControllerTags : int { | 35 enum class EditorViewControllerTags : int { |
| 34 // The tag for the button that saves the model being edited. Starts at | 36 // The tag for the button that saves the model being edited. Starts at |
| 35 // |kFirstTagValue| not to conflict with tags common to all views. | 37 // |kFirstTagValue| not to conflict with tags common to all views. |
| 36 SAVE_BUTTON = kFirstTagValue, | 38 SAVE_BUTTON = kFirstTagValue, |
| 37 }; | 39 }; |
| 38 | 40 |
| 39 constexpr int kNumCharactersInShortField = 6; | 41 constexpr int kNumCharactersInShortField = 8; |
| 40 constexpr int kNumCharactersInLongField = 20; | 42 constexpr int kNumCharactersInLongField = 20; |
| 41 | 43 |
| 42 } // namespace | 44 } // namespace |
| 43 | 45 |
| 44 EditorViewController::EditorViewController(PaymentRequest* request, | 46 EditorViewController::EditorViewController(PaymentRequest* request, |
| 45 PaymentRequestDialogView* dialog) | 47 PaymentRequestDialogView* dialog) |
| 46 : PaymentRequestSheetController(request, dialog) {} | 48 : PaymentRequestSheetController(request, dialog) {} |
| 47 | 49 |
| 48 EditorViewController::~EditorViewController() {} | 50 EditorViewController::~EditorViewController() {} |
| 49 | 51 |
| 50 std::unique_ptr<views::View> EditorViewController::CreateView() { | 52 std::unique_ptr<views::View> EditorViewController::CreateView() { |
| 51 std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>(); | 53 std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>(); |
| 52 | 54 |
| 53 views::BoxLayout* layout = | 55 views::BoxLayout* layout = |
| 54 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); | 56 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); |
| 55 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); | 57 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
| 56 layout->set_cross_axis_alignment( | 58 layout->set_cross_axis_alignment( |
| 57 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); | 59 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); |
| 58 content_view->SetLayoutManager(layout); | 60 content_view->SetLayoutManager(layout); |
| 59 | 61 |
| 60 // Create an input label/textfield for each field definition. | 62 // Create an input label/textfield for each field definition. |
| 61 std::vector<EditorField> fields = GetFieldDefinitions(); | 63 std::vector<EditorField> fields = GetFieldDefinitions(); |
| 62 for (const auto& field : fields) { | 64 for (const auto& field : fields) { |
| 63 views::Textfield* text_field = nullptr; | 65 content_view->AddChildView(CreateInputField(field).release()); |
| 64 content_view->AddChildView(CreateInputField(field, &text_field).release()); | |
| 65 // |field| is moved out of the |fields| structure and should not be | |
| 66 // referenced after the following line. | |
| 67 text_fields_.insert(std::make_pair(text_field, std::move(field))); | |
| 68 } | 66 } |
| 69 | 67 |
| 70 // TODO(mathp): Use the save button in the footer once it's built. | |
| 71 views::LabelButton* button = views::MdTextButton::CreateSecondaryUiButton( | |
| 72 this, l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SAVE_BUTTON)); | |
| 73 button->set_tag(static_cast<int>(EditorViewControllerTags::SAVE_BUTTON)); | |
| 74 button->set_id(static_cast<int>(DialogViewID::EDITOR_SAVE_BUTTON)); | |
| 75 content_view->AddChildView(button); | |
| 76 | |
| 77 return CreatePaymentView( | 68 return CreatePaymentView( |
| 78 CreateSheetHeaderView( | 69 CreateSheetHeaderView( |
| 79 true, l10n_util::GetStringUTF16( | 70 true, l10n_util::GetStringUTF16( |
| 80 IDS_PAYMENT_REQUEST_CREDIT_CARD_EDITOR_ADD_TITLE), | 71 IDS_PAYMENT_REQUEST_CREDIT_CARD_EDITOR_ADD_TITLE), |
| 81 this), | 72 this), |
| 82 std::move(content_view)); | 73 std::move(content_view)); |
| 83 } | 74 } |
| 84 | 75 |
| 76 std::unique_ptr<views::Button> EditorViewController::CreatePrimaryButton() { |
| 77 std::unique_ptr<views::Button> button( |
| 78 views::MdTextButton::CreateSecondaryUiBlueButton( |
| 79 this, l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SAVE_BUTTON))); |
| 80 button->set_tag(static_cast<int>(EditorViewControllerTags::SAVE_BUTTON)); |
| 81 button->set_id(static_cast<int>(DialogViewID::EDITOR_SAVE_BUTTON)); |
| 82 return button; |
| 83 } |
| 84 |
| 85 void EditorViewController::ButtonPressed(views::Button* sender, | 85 void EditorViewController::ButtonPressed(views::Button* sender, |
| 86 const ui::Event& event) { | 86 const ui::Event& event) { |
| 87 switch (sender->tag()) { | 87 switch (sender->tag()) { |
| 88 case static_cast<int>(EditorViewControllerTags::SAVE_BUTTON): | 88 case static_cast<int>(EditorViewControllerTags::SAVE_BUTTON): |
| 89 if (ValidateModelAndSave()) | 89 if (ValidateModelAndSave()) |
| 90 dialog()->GoBack(); | 90 dialog()->GoBack(); |
| 91 break; | 91 break; |
| 92 default: | 92 default: |
| 93 PaymentRequestSheetController::ButtonPressed(sender, event); | 93 PaymentRequestSheetController::ButtonPressed(sender, event); |
| 94 break; | 94 break; |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 | 97 |
| 98 void EditorViewController::ContentsChanged(views::Textfield* sender, | 98 void EditorViewController::ContentsChanged(views::Textfield* sender, |
| 99 const base::string16& new_contents) { | 99 const base::string16& new_contents) { |
| 100 // TODO(mathp): Validate the |sender| textfield and display errors. | 100 static_cast<ValidatingTextfield*>(sender)->OnContentsChanged(); |
| 101 } | 101 } |
| 102 | 102 |
| 103 std::unique_ptr<views::View> EditorViewController::CreateInputField( | 103 std::unique_ptr<views::View> EditorViewController::CreateInputField( |
| 104 const EditorField& field, | 104 const EditorField& field) { |
| 105 views::Textfield** text_field) { | |
| 106 std::unique_ptr<views::View> row = base::MakeUnique<views::View>(); | 105 std::unique_ptr<views::View> row = base::MakeUnique<views::View>(); |
| 107 | 106 |
| 108 row->SetBorder(payments::CreatePaymentRequestRowBorder()); | 107 row->SetBorder(payments::CreatePaymentRequestRowBorder()); |
| 109 | 108 |
| 110 views::GridLayout* layout = new views::GridLayout(row.get()); | 109 views::GridLayout* layout = new views::GridLayout(row.get()); |
| 111 | 110 |
| 112 // The vertical spacing for these rows is slightly different than the spacing | 111 // The vertical spacing for these rows is slightly different than the spacing |
| 113 // spacing for clickable rows, so don't use kPaymentRequestRowVerticalInsets. | 112 // spacing for clickable rows, so don't use kPaymentRequestRowVerticalInsets. |
| 114 constexpr int kRowVerticalInset = 12; | 113 constexpr int kRowVerticalInset = 12; |
| 115 layout->SetInsets( | 114 layout->SetInsets( |
| 116 kRowVerticalInset, payments::kPaymentRequestRowHorizontalInsets, | 115 kRowVerticalInset, payments::kPaymentRequestRowHorizontalInsets, |
| 117 kRowVerticalInset, payments::kPaymentRequestRowHorizontalInsets); | 116 kRowVerticalInset, payments::kPaymentRequestRowHorizontalInsets); |
| 118 | 117 |
| 119 row->SetLayoutManager(layout); | 118 row->SetLayoutManager(layout); |
| 120 views::ColumnSet* columns = layout->AddColumnSet(0); | 119 views::ColumnSet* columns = layout->AddColumnSet(0); |
| 121 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, | 120 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
| 122 views::GridLayout::USE_PREF, 0, 0); | 121 views::GridLayout::USE_PREF, 0, 0); |
| 123 columns->AddPaddingColumn(1, 0); | 122 columns->AddPaddingColumn(1, 0); |
| 124 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 0, | 123 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 0, |
| 125 views::GridLayout::USE_PREF, 0, 0); | 124 views::GridLayout::USE_PREF, 0, 0); |
| 126 | 125 |
| 127 layout->StartRow(0, 0); | 126 layout->StartRow(0, 0); |
| 128 layout->AddView(new views::Label(field.label)); | 127 layout->AddView(new views::Label(field.label)); |
| 129 | 128 |
| 130 *text_field = new views::Textfield(); | 129 ValidatingTextfield* text_field = |
| 131 (*text_field)->set_controller(this); | 130 new ValidatingTextfield(CreateValidationDelegate(field)); |
| 132 (*text_field) | 131 text_field->set_controller(this); |
| 133 ->set_default_width_in_chars(field.length_hint == | 132 // Using autofill field type as a view ID (for testing). |
| 134 EditorField::LengthHint::HINT_SHORT | 133 text_field->set_id(static_cast<int>(field.type)); |
| 135 ? kNumCharactersInShortField | 134 text_field->set_default_width_in_chars( |
| 136 : kNumCharactersInLongField); | 135 field.length_hint == EditorField::LengthHint::HINT_SHORT |
| 137 // |text_field| will now be owned by the layout, but the caller kept a | 136 ? kNumCharactersInShortField |
| 138 // reference. | 137 : kNumCharactersInLongField); |
| 139 layout->AddView(*text_field); | 138 |
| 139 text_fields_.insert(std::make_pair(text_field, field)); |
| 140 // |text_field| will now be owned by the layout. |
| 141 layout->AddView(text_field); |
| 140 | 142 |
| 141 return row; | 143 return row; |
| 142 } | 144 } |
| 143 | 145 |
| 144 } // namespace payments | 146 } // namespace payments |
| OLD | NEW |