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