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 <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 constexpr int kFirstTagValue = static_cast<int>( | 37 constexpr int kFirstTagValue = static_cast<int>( |
38 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); | 38 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); |
39 | 39 |
40 enum class EditorViewControllerTags : int { | 40 enum class EditorViewControllerTags : int { |
41 // The tag for the button that saves the model being edited. Starts at | 41 // The tag for the button that saves the model being edited. Starts at |
42 // |kFirstTagValue| not to conflict with tags common to all views. | 42 // |kFirstTagValue| not to conflict with tags common to all views. |
43 SAVE_BUTTON = kFirstTagValue, | 43 SAVE_BUTTON = kFirstTagValue, |
44 }; | 44 }; |
45 | 45 |
46 std::unique_ptr<views::View> CreateErrorLabelView(const base::string16& error, | 46 std::unique_ptr<views::View> CreateErrorLabelView( |
47 const EditorField& field) { | 47 const base::string16& error, |
| 48 autofill::ServerFieldType type) { |
48 std::unique_ptr<views::View> view = base::MakeUnique<views::View>(); | 49 std::unique_ptr<views::View> view = base::MakeUnique<views::View>(); |
49 | 50 |
50 std::unique_ptr<views::BoxLayout> layout = | 51 std::unique_ptr<views::BoxLayout> layout = |
51 base::MakeUnique<views::BoxLayout>(views::BoxLayout::kVertical, 0, 0, 0); | 52 base::MakeUnique<views::BoxLayout>(views::BoxLayout::kVertical, 0, 0, 0); |
52 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); | 53 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
53 layout->set_cross_axis_alignment( | 54 layout->set_cross_axis_alignment( |
54 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); | 55 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); |
55 // This is the space between the input field and the error label. | 56 // This is the space between the input field and the error label. |
56 constexpr int kErrorLabelTopPadding = 6; | 57 constexpr int kErrorLabelTopPadding = 6; |
57 layout->set_inside_border_insets(gfx::Insets(kErrorLabelTopPadding, 0, 0, 0)); | 58 layout->set_inside_border_insets(gfx::Insets(kErrorLabelTopPadding, 0, 0, 0)); |
58 view->SetLayoutManager(layout.release()); | 59 view->SetLayoutManager(layout.release()); |
59 | 60 |
60 std::unique_ptr<views::Label> error_label = | 61 std::unique_ptr<views::Label> error_label = |
61 base::MakeUnique<views::Label>(error); | 62 base::MakeUnique<views::Label>(error); |
62 error_label->set_id(static_cast<int>(DialogViewID::ERROR_LABEL_OFFSET) + | 63 error_label->set_id(static_cast<int>(DialogViewID::ERROR_LABEL_OFFSET) + |
63 field.type); | 64 type); |
64 error_label->SetFontList( | 65 error_label->SetFontList( |
65 error_label->GetDefaultFontList().DeriveWithSizeDelta(-1)); | 66 error_label->GetDefaultFontList().DeriveWithSizeDelta(-1)); |
66 error_label->SetEnabledColor(error_label->GetNativeTheme()->GetSystemColor( | 67 error_label->SetEnabledColor(error_label->GetNativeTheme()->GetSystemColor( |
67 ui::NativeTheme::kColorId_AlertSeverityHigh)); | 68 ui::NativeTheme::kColorId_AlertSeverityHigh)); |
68 | 69 |
69 view->AddChildView(error_label.release()); | 70 view->AddChildView(error_label.release()); |
70 return view; | 71 return view; |
71 } | 72 } |
72 | 73 |
73 } // namespace | 74 } // namespace |
74 | 75 |
75 EditorViewController::EditorViewController( | 76 EditorViewController::EditorViewController( |
76 PaymentRequestSpec* spec, | 77 PaymentRequestSpec* spec, |
77 PaymentRequestState* state, | 78 PaymentRequestState* state, |
78 PaymentRequestDialogView* dialog, | 79 PaymentRequestDialogView* dialog, |
79 BackNavigationType back_navigation_type) | 80 BackNavigationType back_navigation_type) |
80 : PaymentRequestSheetController(spec, state, dialog), | 81 : PaymentRequestSheetController(spec, state, dialog), |
81 first_field_view_(nullptr), | 82 first_field_view_(nullptr), |
82 back_navigation_type_(back_navigation_type) {} | 83 back_navigation_type_(back_navigation_type) {} |
83 | 84 |
84 EditorViewController::~EditorViewController() {} | 85 EditorViewController::~EditorViewController() {} |
85 | 86 |
86 void EditorViewController::DisplayErrorMessageForField( | 87 void EditorViewController::DisplayErrorMessageForField( |
87 const EditorField& field, | 88 autofill::ServerFieldType type, |
88 const base::string16& error_message) { | 89 const base::string16& error_message) { |
89 const auto& label_view_it = error_labels_.find(field); | 90 const auto& label_view_it = error_labels_.find(type); |
90 DCHECK(label_view_it != error_labels_.end()); | 91 DCHECK(label_view_it != error_labels_.end()); |
91 | 92 |
92 label_view_it->second->RemoveAllChildViews(/*delete_children=*/true); | 93 label_view_it->second->RemoveAllChildViews(/*delete_children=*/true); |
93 if (!error_message.empty()) { | 94 if (!error_message.empty()) { |
94 label_view_it->second->AddChildView( | 95 label_view_it->second->AddChildView( |
95 CreateErrorLabelView(error_message, field).release()); | 96 CreateErrorLabelView(error_message, type).release()); |
96 } | 97 } |
97 RelayoutPane(); | 98 RelayoutPane(); |
98 } | 99 } |
99 | 100 |
| 101 // static |
| 102 int EditorViewController::GetInputFieldViewId(autofill::ServerFieldType type) { |
| 103 return static_cast<int>(DialogViewID::INPUT_FIELD_TYPE_OFFSET) + |
| 104 static_cast<int>(type); |
| 105 } |
| 106 |
100 std::unique_ptr<views::View> EditorViewController::CreateHeaderView() { | 107 std::unique_ptr<views::View> EditorViewController::CreateHeaderView() { |
101 return nullptr; | 108 return nullptr; |
102 } | 109 } |
103 | 110 |
104 std::unique_ptr<views::View> EditorViewController::CreateCustomFieldView( | 111 std::unique_ptr<views::View> EditorViewController::CreateCustomFieldView( |
105 autofill::ServerFieldType type) { | 112 autofill::ServerFieldType type) { |
106 return nullptr; | 113 return nullptr; |
107 } | 114 } |
108 | 115 |
109 std::unique_ptr<views::View> EditorViewController::CreateExtraViewForField( | 116 std::unique_ptr<views::View> EditorViewController::CreateExtraViewForField( |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 break; | 172 break; |
166 } | 173 } |
167 } | 174 } |
168 | 175 |
169 views::View* EditorViewController::GetFirstFocusedView() { | 176 views::View* EditorViewController::GetFirstFocusedView() { |
170 if (first_field_view_) | 177 if (first_field_view_) |
171 return first_field_view_; | 178 return first_field_view_; |
172 return PaymentRequestSheetController::GetFirstFocusedView(); | 179 return PaymentRequestSheetController::GetFirstFocusedView(); |
173 } | 180 } |
174 | 181 |
| 182 std::unique_ptr<ValidatingCombobox> |
| 183 EditorViewController::CreateComboboxForField(const EditorField& field) { |
| 184 std::unique_ptr<ValidatingCombobox> combobox = |
| 185 base::MakeUnique<ValidatingCombobox>(GetComboboxModelForType(field.type), |
| 186 CreateValidationDelegate(field)); |
| 187 base::string16 initial_value = GetInitialValueForType(field.type); |
| 188 if (!initial_value.empty()) |
| 189 combobox->SelectValue(initial_value); |
| 190 // Using autofill field type as a view ID. |
| 191 combobox->set_id(GetInputFieldViewId(field.type)); |
| 192 combobox->set_listener(this); |
| 193 comboboxes_.insert(std::make_pair(combobox.get(), field)); |
| 194 return combobox; |
| 195 } |
| 196 |
175 void EditorViewController::ContentsChanged(views::Textfield* sender, | 197 void EditorViewController::ContentsChanged(views::Textfield* sender, |
176 const base::string16& new_contents) { | 198 const base::string16& new_contents) { |
177 static_cast<ValidatingTextfield*>(sender)->OnContentsChanged(); | 199 static_cast<ValidatingTextfield*>(sender)->OnContentsChanged(); |
178 } | 200 } |
179 | 201 |
180 void EditorViewController::OnPerformAction(views::Combobox* sender) { | 202 void EditorViewController::OnPerformAction(views::Combobox* sender) { |
181 static_cast<ValidatingCombobox*>(sender)->OnContentsChanged(); | 203 static_cast<ValidatingCombobox*>(sender)->OnContentsChanged(); |
182 } | 204 } |
183 | 205 |
184 std::unique_ptr<views::View> EditorViewController::CreateEditorView() { | 206 std::unique_ptr<views::View> EditorViewController::CreateEditorView() { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 label->SetMultiLine(true); | 323 label->SetMultiLine(true); |
302 layout->AddView(label.release()); | 324 layout->AddView(label.release()); |
303 | 325 |
304 constexpr int kInputFieldHeight = 28; | 326 constexpr int kInputFieldHeight = 28; |
305 if (field.control_type == EditorField::ControlType::TEXTFIELD) { | 327 if (field.control_type == EditorField::ControlType::TEXTFIELD) { |
306 ValidatingTextfield* text_field = | 328 ValidatingTextfield* text_field = |
307 new ValidatingTextfield(CreateValidationDelegate(field)); | 329 new ValidatingTextfield(CreateValidationDelegate(field)); |
308 text_field->SetText(GetInitialValueForType(field.type)); | 330 text_field->SetText(GetInitialValueForType(field.type)); |
309 text_field->set_controller(this); | 331 text_field->set_controller(this); |
310 // Using autofill field type as a view ID (for testing). | 332 // Using autofill field type as a view ID (for testing). |
311 text_field->set_id(static_cast<int>(field.type)); | 333 text_field->set_id(GetInputFieldViewId(field.type)); |
312 text_fields_.insert(std::make_pair(text_field, field)); | 334 text_fields_.insert(std::make_pair(text_field, field)); |
313 | 335 |
314 // TODO(crbug.com/718582): Make the initial focus the first incomplete/empty | 336 // TODO(crbug.com/718582): Make the initial focus the first incomplete/empty |
315 // field. | 337 // field. |
316 if (!first_field_view_) | 338 if (!first_field_view_) |
317 first_field_view_ = text_field; | 339 first_field_view_ = text_field; |
318 | 340 |
319 // |text_field| will now be owned by |row|. | 341 // |text_field| will now be owned by |row|. |
320 layout->AddView(text_field, 1, 1, views::GridLayout::FILL, | 342 layout->AddView(text_field, 1, 1, views::GridLayout::FILL, |
321 views::GridLayout::FILL, 0, kInputFieldHeight); | 343 views::GridLayout::FILL, 0, kInputFieldHeight); |
322 } else if (field.control_type == EditorField::ControlType::COMBOBOX) { | 344 } else if (field.control_type == EditorField::ControlType::COMBOBOX) { |
323 ValidatingCombobox* combobox = new ValidatingCombobox( | 345 std::unique_ptr<ValidatingCombobox> combobox = |
324 GetComboboxModelForType(field.type), CreateValidationDelegate(field)); | 346 CreateComboboxForField(field); |
325 base::string16 initial_value = GetInitialValueForType(field.type); | |
326 if (!initial_value.empty()) | |
327 combobox->SelectValue(initial_value); | |
328 // Using autofill field type as a view ID. | |
329 combobox->set_id(static_cast<int>(field.type)); | |
330 combobox->set_listener(this); | |
331 comboboxes_.insert(std::make_pair(combobox, field)); | |
332 | 347 |
333 if (!first_field_view_) | 348 if (!first_field_view_) |
334 first_field_view_ = combobox; | 349 first_field_view_ = combobox.get(); |
335 | 350 |
336 // |combobox| will now be owned by |row|. | 351 // |combobox| will now be owned by |row|. |
337 layout->AddView(combobox, 1, 1, views::GridLayout::FILL, | 352 layout->AddView(combobox.release(), 1, 1, views::GridLayout::FILL, |
338 views::GridLayout::FILL, 0, kInputFieldHeight); | 353 views::GridLayout::FILL, 0, kInputFieldHeight); |
339 } else { | 354 } else { |
340 // Custom field view will now be owned by |row|. And it must be valid since | 355 // Custom field view will now be owned by |row|. And it must be valid since |
341 // the derived class specified a custom view for this field. | 356 // the derived class specified a custom view for this field. |
342 std::unique_ptr<views::View> field_view = CreateCustomFieldView(field.type); | 357 std::unique_ptr<views::View> field_view = CreateCustomFieldView(field.type); |
343 DCHECK(field_view); | 358 DCHECK(field_view); |
344 layout->AddView(field_view.release()); | 359 |
| 360 if (!first_field_view_) |
| 361 first_field_view_ = field_view.get(); |
| 362 |
| 363 layout->AddView(field_view.release(), 1, 1, views::GridLayout::FILL, |
| 364 views::GridLayout::FILL, 0, kInputFieldHeight); |
345 } | 365 } |
346 | 366 |
347 // If an extra view needs to go alongside the input field view, add it to the | 367 // If an extra view needs to go alongside the input field view, add it to the |
348 // last column. | 368 // last column. |
349 std::unique_ptr<views::View> extra_view = CreateExtraViewForField(field.type); | 369 std::unique_ptr<views::View> extra_view = CreateExtraViewForField(field.type); |
350 if (extra_view) | 370 if (extra_view) |
351 layout->AddView(extra_view.release()); | 371 layout->AddView(extra_view.release()); |
352 | 372 |
353 layout->StartRow(0, column_set); | 373 layout->StartRow(0, column_set); |
354 layout->SkipColumns(1); | 374 layout->SkipColumns(1); |
355 std::unique_ptr<views::View> error_label_view = | 375 std::unique_ptr<views::View> error_label_view = |
356 base::MakeUnique<views::View>(); | 376 base::MakeUnique<views::View>(); |
357 error_label_view->SetLayoutManager(new views::FillLayout); | 377 error_label_view->SetLayoutManager(new views::FillLayout); |
358 error_labels_[field] = error_label_view.get(); | 378 error_labels_[field.type] = error_label_view.get(); |
359 layout->AddView(error_label_view.release()); | 379 layout->AddView(error_label_view.release()); |
360 | 380 |
361 // Bottom padding for the row. | 381 // Bottom padding for the row. |
362 layout->AddPaddingRow(0, kInputRowSpacing); | 382 layout->AddPaddingRow(0, kInputRowSpacing); |
363 } | 383 } |
364 | 384 |
365 int EditorViewController::ComputeWidestExtraViewWidth( | 385 int EditorViewController::ComputeWidestExtraViewWidth( |
366 EditorField::LengthHint size) { | 386 EditorField::LengthHint size) { |
367 int widest_column_width = 0; | 387 int widest_column_width = 0; |
368 | 388 |
369 for (const auto& field : GetFieldDefinitions()) { | 389 for (const auto& field : GetFieldDefinitions()) { |
370 if (field.length_hint != size) | 390 if (field.length_hint != size) |
371 continue; | 391 continue; |
372 | 392 |
373 std::unique_ptr<views::View> extra_view = | 393 std::unique_ptr<views::View> extra_view = |
374 CreateExtraViewForField(field.type); | 394 CreateExtraViewForField(field.type); |
375 if (!extra_view) | 395 if (!extra_view) |
376 continue; | 396 continue; |
377 widest_column_width = | 397 widest_column_width = |
378 std::max(extra_view->GetPreferredSize().width(), widest_column_width); | 398 std::max(extra_view->GetPreferredSize().width(), widest_column_width); |
379 } | 399 } |
380 return widest_column_width; | 400 return widest_column_width; |
381 } | 401 } |
382 | 402 |
383 } // namespace payments | 403 } // namespace payments |
OLD | NEW |