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 |
100 std::unique_ptr<views::View> EditorViewController::CreateHeaderView() { | 101 std::unique_ptr<views::View> EditorViewController::CreateHeaderView() { |
101 return nullptr; | 102 return nullptr; |
102 } | 103 } |
103 | 104 |
104 std::unique_ptr<views::View> EditorViewController::CreateCustomFieldView( | 105 std::unique_ptr<views::View> EditorViewController::CreateCustomFieldView( |
105 autofill::ServerFieldType type) { | 106 autofill::ServerFieldType type) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 break; | 166 break; |
166 } | 167 } |
167 } | 168 } |
168 | 169 |
169 views::View* EditorViewController::GetFirstFocusedView() { | 170 views::View* EditorViewController::GetFirstFocusedView() { |
170 if (first_field_view_) | 171 if (first_field_view_) |
171 return first_field_view_; | 172 return first_field_view_; |
172 return PaymentRequestSheetController::GetFirstFocusedView(); | 173 return PaymentRequestSheetController::GetFirstFocusedView(); |
173 } | 174 } |
174 | 175 |
176 std::unique_ptr<ValidatingCombobox> | |
177 EditorViewController::CreateComboboxForField(const EditorField& field) { | |
178 std::unique_ptr<ValidatingCombobox> combobox = | |
179 base::MakeUnique<ValidatingCombobox>(GetComboboxModelForType(field.type), | |
180 CreateValidationDelegate(field)); | |
181 base::string16 initial_value = GetInitialValueForType(field.type); | |
182 if (!initial_value.empty()) | |
183 combobox->SelectValue(initial_value); | |
184 // Using autofill field type as a view ID. | |
185 combobox->set_id(static_cast<int>(field.type)); | |
anthonyvd
2017/05/19 19:16:59
That's probably not a good idea because it'll caus
Mathieu
2017/05/19 21:13:46
Done.
| |
186 combobox->set_listener(this); | |
187 comboboxes_.insert(std::make_pair(combobox.get(), field)); | |
188 return combobox; | |
189 } | |
190 | |
175 void EditorViewController::ContentsChanged(views::Textfield* sender, | 191 void EditorViewController::ContentsChanged(views::Textfield* sender, |
176 const base::string16& new_contents) { | 192 const base::string16& new_contents) { |
177 static_cast<ValidatingTextfield*>(sender)->OnContentsChanged(); | 193 static_cast<ValidatingTextfield*>(sender)->OnContentsChanged(); |
178 } | 194 } |
179 | 195 |
180 void EditorViewController::OnPerformAction(views::Combobox* sender) { | 196 void EditorViewController::OnPerformAction(views::Combobox* sender) { |
181 static_cast<ValidatingCombobox*>(sender)->OnContentsChanged(); | 197 static_cast<ValidatingCombobox*>(sender)->OnContentsChanged(); |
182 } | 198 } |
183 | 199 |
184 std::unique_ptr<views::View> EditorViewController::CreateEditorView() { | 200 std::unique_ptr<views::View> EditorViewController::CreateEditorView() { |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
313 | 329 |
314 // TODO(crbug.com/718582): Make the initial focus the first incomplete/empty | 330 // TODO(crbug.com/718582): Make the initial focus the first incomplete/empty |
315 // field. | 331 // field. |
316 if (!first_field_view_) | 332 if (!first_field_view_) |
317 first_field_view_ = text_field; | 333 first_field_view_ = text_field; |
318 | 334 |
319 // |text_field| will now be owned by |row|. | 335 // |text_field| will now be owned by |row|. |
320 layout->AddView(text_field, 1, 1, views::GridLayout::FILL, | 336 layout->AddView(text_field, 1, 1, views::GridLayout::FILL, |
321 views::GridLayout::FILL, 0, kInputFieldHeight); | 337 views::GridLayout::FILL, 0, kInputFieldHeight); |
322 } else if (field.control_type == EditorField::ControlType::COMBOBOX) { | 338 } else if (field.control_type == EditorField::ControlType::COMBOBOX) { |
323 ValidatingCombobox* combobox = new ValidatingCombobox( | 339 std::unique_ptr<ValidatingCombobox> combobox = |
324 GetComboboxModelForType(field.type), CreateValidationDelegate(field)); | 340 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 | 341 |
333 if (!first_field_view_) | 342 if (!first_field_view_) |
334 first_field_view_ = combobox; | 343 first_field_view_ = combobox.get(); |
335 | 344 |
336 // |combobox| will now be owned by |row|. | 345 // |combobox| will now be owned by |row|. |
337 layout->AddView(combobox, 1, 1, views::GridLayout::FILL, | 346 layout->AddView(combobox.release(), 1, 1, views::GridLayout::FILL, |
338 views::GridLayout::FILL, 0, kInputFieldHeight); | 347 views::GridLayout::FILL, 0, kInputFieldHeight); |
339 } else { | 348 } else { |
340 // Custom field view will now be owned by |row|. And it must be valid since | 349 // 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. | 350 // the derived class specified a custom view for this field. |
342 std::unique_ptr<views::View> field_view = CreateCustomFieldView(field.type); | 351 std::unique_ptr<views::View> field_view = CreateCustomFieldView(field.type); |
343 DCHECK(field_view); | 352 DCHECK(field_view); |
344 layout->AddView(field_view.release()); | 353 |
354 if (!first_field_view_) | |
355 first_field_view_ = field_view.get(); | |
356 | |
357 layout->AddView(field_view.release(), 1, 1, views::GridLayout::FILL, | |
358 views::GridLayout::FILL, 0, kInputFieldHeight); | |
345 } | 359 } |
346 | 360 |
347 // If an extra view needs to go alongside the input field view, add it to the | 361 // If an extra view needs to go alongside the input field view, add it to the |
348 // last column. | 362 // last column. |
349 std::unique_ptr<views::View> extra_view = CreateExtraViewForField(field.type); | 363 std::unique_ptr<views::View> extra_view = CreateExtraViewForField(field.type); |
350 if (extra_view) | 364 if (extra_view) |
351 layout->AddView(extra_view.release()); | 365 layout->AddView(extra_view.release()); |
352 | 366 |
353 layout->StartRow(0, column_set); | 367 layout->StartRow(0, column_set); |
354 layout->SkipColumns(1); | 368 layout->SkipColumns(1); |
355 std::unique_ptr<views::View> error_label_view = | 369 std::unique_ptr<views::View> error_label_view = |
356 base::MakeUnique<views::View>(); | 370 base::MakeUnique<views::View>(); |
357 error_label_view->SetLayoutManager(new views::FillLayout); | 371 error_label_view->SetLayoutManager(new views::FillLayout); |
358 error_labels_[field] = error_label_view.get(); | 372 error_labels_[field.type] = error_label_view.get(); |
359 layout->AddView(error_label_view.release()); | 373 layout->AddView(error_label_view.release()); |
360 | 374 |
361 // Bottom padding for the row. | 375 // Bottom padding for the row. |
362 layout->AddPaddingRow(0, kInputRowSpacing); | 376 layout->AddPaddingRow(0, kInputRowSpacing); |
363 } | 377 } |
364 | 378 |
365 int EditorViewController::ComputeWidestExtraViewWidth( | 379 int EditorViewController::ComputeWidestExtraViewWidth( |
366 EditorField::LengthHint size) { | 380 EditorField::LengthHint size) { |
367 int widest_column_width = 0; | 381 int widest_column_width = 0; |
368 | 382 |
369 for (const auto& field : GetFieldDefinitions()) { | 383 for (const auto& field : GetFieldDefinitions()) { |
370 if (field.length_hint != size) | 384 if (field.length_hint != size) |
371 continue; | 385 continue; |
372 | 386 |
373 std::unique_ptr<views::View> extra_view = | 387 std::unique_ptr<views::View> extra_view = |
374 CreateExtraViewForField(field.type); | 388 CreateExtraViewForField(field.type); |
375 if (!extra_view) | 389 if (!extra_view) |
376 continue; | 390 continue; |
377 widest_column_width = | 391 widest_column_width = |
378 std::max(extra_view->GetPreferredSize().width(), widest_column_width); | 392 std::max(extra_view->GetPreferredSize().width(), widest_column_width); |
379 } | 393 } |
380 return widest_column_width; | 394 return widest_column_width; |
381 } | 395 } |
382 | 396 |
383 } // namespace payments | 397 } // namespace payments |
OLD | NEW |