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/validating_combobox.h" | 5 #include "chrome/browser/ui/views/payments/validating_combobox.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "ui/base/models/combobox_model.h" | 9 #include "ui/base/models/combobox_model.h" |
10 | 10 |
11 namespace payments { | 11 namespace payments { |
12 | 12 |
13 ValidatingCombobox::ValidatingCombobox( | 13 ValidatingCombobox::ValidatingCombobox( |
14 std::unique_ptr<ui::ComboboxModel> model, | 14 std::unique_ptr<ui::ComboboxModel> model, |
15 std::unique_ptr<ValidationDelegate> delegate) | 15 std::unique_ptr<ValidationDelegate> delegate) |
16 : Combobox(std::move(model)), delegate_(std::move(delegate)) { | 16 : Combobox(std::move(model)), delegate_(std::move(delegate)) { |
17 // No need to remove observer on owned model. | 17 // No need to remove observer on owned model. |
18 this->model()->AddObserver(this); | 18 this->model()->AddObserver(this); |
19 } | 19 } |
20 | 20 |
21 ValidatingCombobox::~ValidatingCombobox() {} | 21 ValidatingCombobox::~ValidatingCombobox() {} |
22 | 22 |
23 void ValidatingCombobox::OnBlur() { | 23 void ValidatingCombobox::OnBlur() { |
24 Combobox::OnBlur(); | 24 Combobox::OnBlur(); |
25 | 25 |
26 // The first validation should be on a blur. The subsequent validations will | 26 // Validations will occur when the content changes. Do not validate if the |
27 // occur when the content changes. Do not validate if the view is being | 27 // view is being removed. |
28 // removed. | 28 if (!being_removed_) { |
29 if (!was_blurred_ && !being_removed_) { | |
30 was_blurred_ = true; | |
31 Validate(); | 29 Validate(); |
32 } | 30 } |
33 } | 31 } |
34 | 32 |
35 void ValidatingCombobox::ViewHierarchyChanged( | 33 void ValidatingCombobox::ViewHierarchyChanged( |
36 const ViewHierarchyChangedDetails& details) { | 34 const ViewHierarchyChangedDetails& details) { |
37 if (details.child == this && !details.is_add) | 35 if (details.child == this && !details.is_add) |
38 being_removed_ = true; | 36 being_removed_ = true; |
39 } | 37 } |
40 | 38 |
41 void ValidatingCombobox::OnContentsChanged() { | 39 void ValidatingCombobox::OnContentsChanged() { |
42 // Validation on every keystroke only happens if the field has been validated | |
43 // before as part of a blur. | |
44 if (!was_blurred_) | |
45 return; | |
46 | |
47 Validate(); | 40 Validate(); |
48 } | 41 } |
49 | 42 |
50 void ValidatingCombobox::OnComboboxModelChanged( | 43 void ValidatingCombobox::OnComboboxModelChanged( |
51 ui::ComboboxModel* unused_model) { | 44 ui::ComboboxModel* unused_model) { |
52 ModelChanged(); | 45 ModelChanged(); |
53 delegate_->ComboboxModelChanged(this); | 46 delegate_->ComboboxModelChanged(this); |
54 } | 47 } |
55 | 48 |
56 void ValidatingCombobox::Validate() { | 49 void ValidatingCombobox::Validate() { |
57 // ValidateCombobox may have side-effects, such as displaying errors. | 50 // ValidateCombobox may have side-effects, such as displaying errors. |
58 SetInvalid(!delegate_->ValidateCombobox(this)); | 51 SetInvalid(!delegate_->ValidateCombobox(this)); |
59 } | 52 } |
60 | 53 |
61 } // namespace payments | 54 } // namespace payments |
OLD | NEW |