| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/payments/editor_view_controller.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
| 11 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" |
| 12 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" |
| 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "components/payments/payment_request.h" |
| 15 #include "components/strings/grit/components_strings.h" |
| 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/views/border.h" |
| 18 #include "ui/views/controls/button/label_button.h" |
| 19 #include "ui/views/controls/button/md_text_button.h" |
| 20 #include "ui/views/controls/label.h" |
| 21 #include "ui/views/controls/styled_label.h" |
| 22 #include "ui/views/controls/textfield/textfield.h" |
| 23 #include "ui/views/layout/box_layout.h" |
| 24 #include "ui/views/layout/grid_layout.h" |
| 25 #include "ui/views/view.h" |
| 26 |
| 27 namespace payments { |
| 28 namespace { |
| 29 |
| 30 constexpr int kFirstTagValue = static_cast<int>( |
| 31 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); |
| 32 |
| 33 enum class EditorViewControllerTags : int { |
| 34 // The tag for the button that saves the model being edited. Starts at |
| 35 // |kFirstTagValue| not to conflict with tags common to all views. |
| 36 SAVE_BUTTON = kFirstTagValue, |
| 37 }; |
| 38 |
| 39 constexpr int kNumCharactersInShortField = 6; |
| 40 constexpr int kNumCharactersInLongField = 20; |
| 41 |
| 42 } // namespace |
| 43 |
| 44 EditorViewController::EditorViewController(PaymentRequest* request, |
| 45 PaymentRequestDialogView* dialog) |
| 46 : PaymentRequestSheetController(request, dialog) {} |
| 47 |
| 48 EditorViewController::~EditorViewController() {} |
| 49 |
| 50 std::unique_ptr<views::View> EditorViewController::CreateView() { |
| 51 std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>(); |
| 52 |
| 53 views::BoxLayout* layout = |
| 54 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); |
| 55 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
| 56 layout->set_cross_axis_alignment( |
| 57 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); |
| 58 content_view->SetLayoutManager(layout); |
| 59 |
| 60 // Create an input label/textfield for each field definition. |
| 61 std::vector<EditorField> fields = GetFieldDefinitions(); |
| 62 for (const auto& field : fields) { |
| 63 views::Textfield* text_field = nullptr; |
| 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 } |
| 69 |
| 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( |
| 78 CreateSheetHeaderView( |
| 79 true, l10n_util::GetStringUTF16( |
| 80 IDS_PAYMENT_REQUEST_CREDIT_CARD_EDITOR_ADD_TITLE), |
| 81 this), |
| 82 std::move(content_view)); |
| 83 } |
| 84 |
| 85 void EditorViewController::ButtonPressed(views::Button* sender, |
| 86 const ui::Event& event) { |
| 87 switch (sender->tag()) { |
| 88 case static_cast<int>(EditorViewControllerTags::SAVE_BUTTON): |
| 89 if (ValidateModelAndSave()) |
| 90 dialog()->GoBack(); |
| 91 break; |
| 92 default: |
| 93 PaymentRequestSheetController::ButtonPressed(sender, event); |
| 94 break; |
| 95 } |
| 96 } |
| 97 |
| 98 void EditorViewController::ContentsChanged(views::Textfield* sender, |
| 99 const base::string16& new_contents) { |
| 100 // TODO(mathp): Validate the |sender| textfield and display errors. |
| 101 } |
| 102 |
| 103 std::unique_ptr<views::View> EditorViewController::CreateInputField( |
| 104 const EditorField& field, |
| 105 views::Textfield** text_field) { |
| 106 std::unique_ptr<views::View> row = base::MakeUnique<views::View>(); |
| 107 |
| 108 row->SetBorder(payments::CreatePaymentRequestRowBorder()); |
| 109 |
| 110 views::GridLayout* layout = new views::GridLayout(row.get()); |
| 111 |
| 112 // The vertical spacing for these rows is slightly different than the spacing |
| 113 // spacing for clickable rows, so don't use kPaymentRequestRowVerticalInsets. |
| 114 constexpr int kRowVerticalInset = 12; |
| 115 layout->SetInsets( |
| 116 kRowVerticalInset, payments::kPaymentRequestRowHorizontalInsets, |
| 117 kRowVerticalInset, payments::kPaymentRequestRowHorizontalInsets); |
| 118 |
| 119 row->SetLayoutManager(layout); |
| 120 views::ColumnSet* columns = layout->AddColumnSet(0); |
| 121 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
| 122 views::GridLayout::USE_PREF, 0, 0); |
| 123 columns->AddPaddingColumn(1, 0); |
| 124 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 0, |
| 125 views::GridLayout::USE_PREF, 0, 0); |
| 126 |
| 127 layout->StartRow(0, 0); |
| 128 layout->AddView(new views::Label(field.label)); |
| 129 |
| 130 *text_field = new views::Textfield(); |
| 131 (*text_field)->set_controller(this); |
| 132 (*text_field) |
| 133 ->set_default_width_in_chars(field.length_hint == |
| 134 EditorField::LengthHint::HINT_SHORT |
| 135 ? kNumCharactersInShortField |
| 136 : kNumCharactersInLongField); |
| 137 // |text_field| will now be owned by the layout, but the caller kept a |
| 138 // reference. |
| 139 layout->AddView(*text_field); |
| 140 |
| 141 return row; |
| 142 } |
| 143 |
| 144 } // namespace payments |
| OLD | NEW |