| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 back_navigation_type_(back_navigation_type) {} | 85 back_navigation_type_(back_navigation_type) {} |
| 86 | 86 |
| 87 EditorViewController::~EditorViewController() {} | 87 EditorViewController::~EditorViewController() {} |
| 88 | 88 |
| 89 void EditorViewController::DisplayErrorMessageForField( | 89 void EditorViewController::DisplayErrorMessageForField( |
| 90 autofill::ServerFieldType type, | 90 autofill::ServerFieldType type, |
| 91 const base::string16& error_message) { | 91 const base::string16& error_message) { |
| 92 const auto& label_view_it = error_labels_.find(type); | 92 const auto& label_view_it = error_labels_.find(type); |
| 93 DCHECK(label_view_it != error_labels_.end()); | 93 DCHECK(label_view_it != error_labels_.end()); |
| 94 | 94 |
| 95 label_view_it->second->RemoveAllChildViews(/*delete_children=*/true); | 95 if (error_message.empty()) { |
| 96 if (!error_message.empty()) { | 96 label_view_it->second->RemoveAllChildViews(/*delete_children=*/true); |
| 97 label_view_it->second->AddChildView( | 97 } else { |
| 98 CreateErrorLabelView(error_message, type).release()); | 98 if (!label_view_it->second->has_children()) { |
| 99 // If there was no error label view, add it. |
| 100 label_view_it->second->AddChildView( |
| 101 CreateErrorLabelView(error_message, type).release()); |
| 102 } else { |
| 103 // The error view is the only child, and has a Label as only child itself. |
| 104 static_cast<views::Label*>( |
| 105 label_view_it->second->child_at(0)->child_at(0)) |
| 106 ->SetText(error_message); |
| 107 } |
| 99 } | 108 } |
| 100 RelayoutPane(); | 109 RelayoutPane(); |
| 101 } | 110 } |
| 102 | 111 |
| 103 // static | 112 // static |
| 104 int EditorViewController::GetInputFieldViewId(autofill::ServerFieldType type) { | 113 int EditorViewController::GetInputFieldViewId(autofill::ServerFieldType type) { |
| 105 return static_cast<int>(DialogViewID::INPUT_FIELD_TYPE_OFFSET) + | 114 return static_cast<int>(DialogViewID::INPUT_FIELD_TYPE_OFFSET) + |
| 106 static_cast<int>(type); | 115 static_cast<int>(type); |
| 107 } | 116 } |
| 108 | 117 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 return initial_focus_field_view_; | 190 return initial_focus_field_view_; |
| 182 return PaymentRequestSheetController::GetFirstFocusedView(); | 191 return PaymentRequestSheetController::GetFirstFocusedView(); |
| 183 } | 192 } |
| 184 | 193 |
| 185 std::unique_ptr<ValidatingCombobox> | 194 std::unique_ptr<ValidatingCombobox> |
| 186 EditorViewController::CreateComboboxForField(const EditorField& field) { | 195 EditorViewController::CreateComboboxForField(const EditorField& field) { |
| 187 std::unique_ptr<ValidatingCombobox> combobox = | 196 std::unique_ptr<ValidatingCombobox> combobox = |
| 188 base::MakeUnique<ValidatingCombobox>(GetComboboxModelForType(field.type), | 197 base::MakeUnique<ValidatingCombobox>(GetComboboxModelForType(field.type), |
| 189 CreateValidationDelegate(field)); | 198 CreateValidationDelegate(field)); |
| 190 base::string16 initial_value = GetInitialValueForType(field.type); | 199 base::string16 initial_value = GetInitialValueForType(field.type); |
| 191 if (!initial_value.empty()) | 200 if (!initial_value.empty()) { |
| 192 combobox->SelectValue(initial_value); | 201 combobox->SelectValue(initial_value); |
| 202 combobox->SetInvalid(!combobox->IsValid()); |
| 203 } |
| 193 // Using autofill field type as a view ID. | 204 // Using autofill field type as a view ID. |
| 194 combobox->set_id(GetInputFieldViewId(field.type)); | 205 combobox->set_id(GetInputFieldViewId(field.type)); |
| 195 combobox->set_listener(this); | 206 combobox->set_listener(this); |
| 196 comboboxes_.insert(std::make_pair(combobox.get(), field)); | 207 comboboxes_.insert(std::make_pair(combobox.get(), field)); |
| 197 return combobox; | 208 return combobox; |
| 198 } | 209 } |
| 199 | 210 |
| 200 void EditorViewController::ContentsChanged(views::Textfield* sender, | 211 void EditorViewController::ContentsChanged(views::Textfield* sender, |
| 201 const base::string16& new_contents) { | 212 const base::string16& new_contents) { |
| 202 static_cast<ValidatingTextfield*>(sender)->OnContentsChanged(); | 213 static_cast<ValidatingTextfield*>(sender)->OnContentsChanged(); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 | 349 |
| 339 label->SetMultiLine(true); | 350 label->SetMultiLine(true); |
| 340 layout->AddView(label.release()); | 351 layout->AddView(label.release()); |
| 341 | 352 |
| 342 views::View* focusable_field = nullptr; | 353 views::View* focusable_field = nullptr; |
| 343 constexpr int kInputFieldHeight = 28; | 354 constexpr int kInputFieldHeight = 28; |
| 344 if (field.control_type == EditorField::ControlType::TEXTFIELD || | 355 if (field.control_type == EditorField::ControlType::TEXTFIELD || |
| 345 field.control_type == EditorField::ControlType::TEXTFIELD_NUMBER) { | 356 field.control_type == EditorField::ControlType::TEXTFIELD_NUMBER) { |
| 346 ValidatingTextfield* text_field = | 357 ValidatingTextfield* text_field = |
| 347 new ValidatingTextfield(CreateValidationDelegate(field)); | 358 new ValidatingTextfield(CreateValidationDelegate(field)); |
| 348 text_field->SetText(GetInitialValueForType(field.type)); | 359 // Set the initial value and validity state. |
| 360 base::string16 initial_value = GetInitialValueForType(field.type); |
| 361 text_field->SetText(initial_value); |
| 362 *valid = text_field->IsValid(); |
| 363 if (!initial_value.empty()) |
| 364 text_field->SetInvalid(!(*valid)); |
| 365 |
| 349 if (field.control_type == EditorField::ControlType::TEXTFIELD_NUMBER) | 366 if (field.control_type == EditorField::ControlType::TEXTFIELD_NUMBER) |
| 350 text_field->SetTextInputType(ui::TextInputType::TEXT_INPUT_TYPE_NUMBER); | 367 text_field->SetTextInputType(ui::TextInputType::TEXT_INPUT_TYPE_NUMBER); |
| 351 text_field->set_controller(this); | 368 text_field->set_controller(this); |
| 352 // Using autofill field type as a view ID (for testing). | 369 // Using autofill field type as a view ID (for testing). |
| 353 text_field->set_id(GetInputFieldViewId(field.type)); | 370 text_field->set_id(GetInputFieldViewId(field.type)); |
| 354 text_fields_.insert(std::make_pair(text_field, field)); | 371 text_fields_.insert(std::make_pair(text_field, field)); |
| 355 focusable_field = text_field; | 372 focusable_field = text_field; |
| 356 *valid = text_field->IsValid(); | |
| 357 | 373 |
| 358 // |text_field| will now be owned by |row|. | 374 // |text_field| will now be owned by |row|. |
| 359 layout->AddView(text_field, 1, 1, views::GridLayout::FILL, | 375 layout->AddView(text_field, 1, 1, views::GridLayout::FILL, |
| 360 views::GridLayout::FILL, 0, kInputFieldHeight); | 376 views::GridLayout::FILL, 0, kInputFieldHeight); |
| 361 } else if (field.control_type == EditorField::ControlType::COMBOBOX) { | 377 } else if (field.control_type == EditorField::ControlType::COMBOBOX) { |
| 362 std::unique_ptr<ValidatingCombobox> combobox = | 378 std::unique_ptr<ValidatingCombobox> combobox = |
| 363 CreateComboboxForField(field); | 379 CreateComboboxForField(field); |
| 364 | 380 |
| 365 focusable_field = combobox.get(); | 381 focusable_field = combobox.get(); |
| 366 *valid = combobox->IsValid(); | 382 *valid = combobox->IsValid(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 CreateExtraViewForField(field.type); | 426 CreateExtraViewForField(field.type); |
| 411 if (!extra_view) | 427 if (!extra_view) |
| 412 continue; | 428 continue; |
| 413 widest_column_width = | 429 widest_column_width = |
| 414 std::max(extra_view->GetPreferredSize().width(), widest_column_width); | 430 std::max(extra_view->GetPreferredSize().width(), widest_column_width); |
| 415 } | 431 } |
| 416 return widest_column_width; | 432 return widest_column_width; |
| 417 } | 433 } |
| 418 | 434 |
| 419 } // namespace payments | 435 } // namespace payments |
| OLD | NEW |