Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(505)

Side by Side Diff: chrome/browser/ui/views/payments/editor_view_controller.cc

Issue 2676663002: [Payments] Implements the credit card editor for Desktop UI (Closed)
Patch Set: Initial Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <memory>
please use gerrit instead 2017/02/02 20:19:02 Already included in the corresponding header file,
Mathieu 2017/02/03 02:18:57 Done.
8 #include <utility>
9
10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h"
please use gerrit instead 2017/02/02 20:19:02 Ditto
Mathieu 2017/02/03 02:18:57 Done.
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h"
14 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h"
15 #include "chrome/browser/ui/views/payments/payment_request_views_util.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "components/payments/payment_request.h"
18 #include "components/strings/grit/components_strings.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/views/border.h"
21 #include "ui/views/controls/button/label_button.h"
22 #include "ui/views/controls/button/md_text_button.h"
23 #include "ui/views/controls/label.h"
24 #include "ui/views/controls/styled_label.h"
25 #include "ui/views/controls/textfield/textfield.h"
26 #include "ui/views/layout/box_layout.h"
27 #include "ui/views/layout/grid_layout.h"
28 #include "ui/views/view.h"
29
30 namespace payments {
31 namespace {
32
33 constexpr int kFirstTagValue = static_cast<int>(
34 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX);
35
36 enum class EditorViewControllerTags : int {
37 // The tag for the button that saves the model being edited.
38 SAVE_BUTTON = kFirstTagValue,
39 };
40
41 constexpr int kNumCharactersInShortField = 6;
42 constexpr int kNumCharactersInLongField = 20;
43
44 } // namespace
45
46 EditorViewController::EditorViewController(PaymentRequest* request,
47 PaymentRequestDialogView* dialog)
48 : PaymentRequestSheetController(request, dialog) {}
49
50 EditorViewController::~EditorViewController() {}
51
52 std::unique_ptr<views::View> EditorViewController::CreateView() {
53 std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>();
54
55 views::BoxLayout* layout =
56 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0);
57 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START);
58 layout->set_cross_axis_alignment(
59 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH);
60 content_view->SetLayoutManager(layout);
61
62 // Create an input label/textfield for each field definition.
63 std::vector<std::unique_ptr<EditorField>> fields = GetFieldDefinitions();
64 for (auto& field : fields) {
65 views::Textfield* text_field = nullptr;
66 content_view->AddChildView(
67 CreateInputField(field.get(), &text_field).release());
68 // |field| is moved out of the |fields| structure and should not be
69 // referenced after the following line.
70 text_fields_.insert(std::make_pair(text_field, std::move(field)));
71 }
72
73 // TODO(mathp): Use the save button in the footer once it's built.
74 views::LabelButton* button = views::MdTextButton::CreateSecondaryUiButton(
75 this, l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SAVE_BUTTON));
76 button->set_tag(static_cast<int>(EditorViewControllerTags::SAVE_BUTTON));
77 button->set_id(static_cast<int>(DialogViewID::EDITOR_SAVE_BUTTON));
78 content_view->AddChildView(button);
79
80 return payments::CreatePaymentView(
81 CreateSheetHeaderView(
82 true, l10n_util::GetStringUTF16(
83 IDS_PAYMENT_REQUEST_CREDIT_CARD_EDITOR_ADD_TITLE),
84 this),
85 std::move(content_view));
86 }
87
88 void EditorViewController::ButtonPressed(views::Button* sender,
89 const ui::Event& event) {
90 switch (sender->tag()) {
91 case static_cast<int>(EditorViewControllerTags::SAVE_BUTTON):
92 if (ValidateModelAndSave())
93 dialog()->GoBack();
94 break;
95 case static_cast<int>(PaymentRequestCommonTags::CLOSE_BUTTON_TAG):
96 dialog()->CloseDialog();
97 break;
98 case static_cast<int>(PaymentRequestCommonTags::BACK_BUTTON_TAG):
99 dialog()->GoBack();
100 break;
101 default:
102 NOTREACHED();
103 }
104 }
105
106 void EditorViewController::ContentsChanged(views::Textfield* sender,
107 const base::string16& new_contents) {
108 // TODO(mathp): Validate the |sender| textfield and display errors.
109 }
110
111 std::unique_ptr<views::View> EditorViewController::CreateInputField(
112 EditorField* field,
113 views::Textfield** text_field) {
114 std::unique_ptr<views::View> row = base::MakeUnique<views::View>();
115
116 row->SetBorder(payments::CreatePaymentRequestRowBorder());
117
118 views::GridLayout* layout = new views::GridLayout(row.get());
119
120 // The vertical spacing for these rows is slightly different than the spacing
121 // spacing for clickable rows, so don't use kPaymentRequestRowVerticalInsets.
122 constexpr int kRowVerticalInset = 12;
123 layout->SetInsets(
124 kRowVerticalInset, payments::kPaymentRequestRowHorizontalInsets,
125 kRowVerticalInset, payments::kPaymentRequestRowHorizontalInsets);
126
127 row->SetLayoutManager(layout);
128 views::ColumnSet* columns = layout->AddColumnSet(0);
129 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0,
130 views::GridLayout::USE_PREF, 0, 0);
131 columns->AddPaddingColumn(1, 0);
132 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 0,
133 views::GridLayout::USE_PREF, 0, 0);
134
135 layout->StartRow(0, 0);
136 layout->AddView(new views::Label(field->label));
137
138 *text_field = new views::Textfield();
139 (*text_field)->set_controller(this);
140 (*text_field)
141 ->set_default_width_in_chars(field->length_hint ==
142 EditorField::LengthHint::HINT_SHORT
143 ? kNumCharactersInShortField
144 : kNumCharactersInLongField);
145 // |text_field| will now be owned by the layout, but the caller kept a
146 // reference.
147 layout->AddView(*text_field);
148
149 return row;
150 }
151
152 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698