Chromium Code Reviews| 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. | |
| 35 SAVE_BUTTON = kFirstTagValue, | |
|
sky
2017/02/06 17:05:11
It's worth a comment why this enum starts at kFirs
Mathieu
2017/02/06 17:46:24
Done.
| |
| 36 }; | |
| 37 | |
| 38 constexpr int kNumCharactersInShortField = 6; | |
| 39 constexpr int kNumCharactersInLongField = 20; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 EditorViewController::EditorViewController(PaymentRequest* request, | |
| 44 PaymentRequestDialogView* dialog) | |
| 45 : PaymentRequestSheetController(request, dialog) {} | |
| 46 | |
| 47 EditorViewController::~EditorViewController() {} | |
| 48 | |
| 49 std::unique_ptr<views::View> EditorViewController::CreateView() { | |
| 50 std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>(); | |
| 51 | |
| 52 views::BoxLayout* layout = | |
| 53 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); | |
| 54 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); | |
| 55 layout->set_cross_axis_alignment( | |
| 56 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); | |
| 57 content_view->SetLayoutManager(layout); | |
| 58 | |
| 59 // Create an input label/textfield for each field definition. | |
| 60 std::vector<std::unique_ptr<EditorField>> fields = GetFieldDefinitions(); | |
| 61 for (auto& field : fields) { | |
| 62 views::Textfield* text_field = nullptr; | |
| 63 content_view->AddChildView( | |
| 64 CreateInputField(field.get(), &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 payments::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 case static_cast<int>(PaymentRequestCommonTags::CLOSE_BUTTON_TAG): | |
| 93 dialog()->CloseDialog(); | |
| 94 break; | |
| 95 case static_cast<int>(PaymentRequestCommonTags::BACK_BUTTON_TAG): | |
| 96 dialog()->GoBack(); | |
| 97 break; | |
| 98 default: | |
| 99 NOTREACHED(); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 void EditorViewController::ContentsChanged(views::Textfield* sender, | |
| 104 const base::string16& new_contents) { | |
| 105 // TODO(mathp): Validate the |sender| textfield and display errors. | |
| 106 } | |
| 107 | |
| 108 std::unique_ptr<views::View> EditorViewController::CreateInputField( | |
| 109 EditorField* field, | |
|
sky
2017/02/06 17:05:11
const EditorField& ?
Mathieu
2017/02/06 17:46:24
Done.
| |
| 110 views::Textfield** text_field) { | |
| 111 std::unique_ptr<views::View> row = base::MakeUnique<views::View>(); | |
| 112 | |
| 113 row->SetBorder(payments::CreatePaymentRequestRowBorder()); | |
| 114 | |
| 115 views::GridLayout* layout = new views::GridLayout(row.get()); | |
| 116 | |
| 117 // The vertical spacing for these rows is slightly different than the spacing | |
| 118 // spacing for clickable rows, so don't use kPaymentRequestRowVerticalInsets. | |
| 119 constexpr int kRowVerticalInset = 12; | |
| 120 layout->SetInsets( | |
| 121 kRowVerticalInset, payments::kPaymentRequestRowHorizontalInsets, | |
| 122 kRowVerticalInset, payments::kPaymentRequestRowHorizontalInsets); | |
| 123 | |
| 124 row->SetLayoutManager(layout); | |
| 125 views::ColumnSet* columns = layout->AddColumnSet(0); | |
| 126 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, | |
| 127 views::GridLayout::USE_PREF, 0, 0); | |
| 128 columns->AddPaddingColumn(1, 0); | |
| 129 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 0, | |
| 130 views::GridLayout::USE_PREF, 0, 0); | |
| 131 | |
| 132 layout->StartRow(0, 0); | |
| 133 layout->AddView(new views::Label(field->label)); | |
| 134 | |
| 135 *text_field = new views::Textfield(); | |
| 136 (*text_field)->set_controller(this); | |
| 137 (*text_field) | |
| 138 ->set_default_width_in_chars(field->length_hint == | |
| 139 EditorField::LengthHint::HINT_SHORT | |
| 140 ? kNumCharactersInShortField | |
| 141 : kNumCharactersInLongField); | |
| 142 // |text_field| will now be owned by the layout, but the caller kept a | |
| 143 // reference. | |
| 144 layout->AddView(*text_field); | |
| 145 | |
| 146 return row; | |
| 147 } | |
| 148 | |
| 149 } // namespace payments | |
| OLD | NEW |