| 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 #ifndef CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ | 6 #define CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <tuple> | 10 #include <tuple> |
| 11 #include <unordered_map> | 11 #include <unordered_map> |
| 12 #include <utility> | 12 #include <utility> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
| 18 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
| 18 #include "chrome/browser/ui/views/payments/payment_request_sheet_controller.h" | 19 #include "chrome/browser/ui/views/payments/payment_request_sheet_controller.h" |
| 19 #include "chrome/browser/ui/views/payments/validation_delegate.h" | 20 #include "chrome/browser/ui/views/payments/validation_delegate.h" |
| 20 #include "components/autofill/core/browser/field_types.h" | 21 #include "components/autofill/core/browser/field_types.h" |
| 21 #include "ui/views/controls/combobox/combobox_listener.h" | 22 #include "ui/views/controls/combobox/combobox_listener.h" |
| 22 #include "ui/views/controls/textfield/textfield_controller.h" | 23 #include "ui/views/controls/textfield/textfield_controller.h" |
| 23 #include "ui/views/view.h" | 24 #include "ui/views/view.h" |
| 24 | 25 |
| 25 namespace ui { | 26 namespace ui { |
| 26 class ComboboxModel; | 27 class ComboboxModel; |
| 27 } | 28 } |
| 28 | 29 |
| 29 namespace views { | 30 namespace views { |
| 30 class GridLayout; | 31 class GridLayout; |
| 31 class Label; | 32 class Label; |
| 32 class Textfield; | 33 class Textfield; |
| 33 class View; | 34 class View; |
| 34 } // namespace views | 35 } // namespace views |
| 35 | 36 |
| 36 namespace payments { | 37 namespace payments { |
| 37 | 38 |
| 38 class PaymentRequestSpec; | 39 class PaymentRequestSpec; |
| 39 class PaymentRequestState; | 40 class PaymentRequestState; |
| 40 class PaymentRequestDialogView; | |
| 41 class ValidatingCombobox; | 41 class ValidatingCombobox; |
| 42 class ValidatingTextfield; | 42 class ValidatingTextfield; |
| 43 | 43 |
| 44 // Field definition for an editor field, used to build the UI. | 44 // Field definition for an editor field, used to build the UI. |
| 45 struct EditorField { | 45 struct EditorField { |
| 46 enum class LengthHint : int { HINT_LONG, HINT_SHORT }; | 46 enum class LengthHint : int { HINT_LONG, HINT_SHORT }; |
| 47 enum class ControlType : int { TEXTFIELD, COMBOBOX }; | 47 enum class ControlType : int { TEXTFIELD, COMBOBOX, CUSTOMFIELD }; |
| 48 | 48 |
| 49 EditorField(autofill::ServerFieldType type, | 49 EditorField(autofill::ServerFieldType type, |
| 50 base::string16 label, | 50 base::string16 label, |
| 51 LengthHint length_hint, | 51 LengthHint length_hint, |
| 52 bool required, | 52 bool required, |
| 53 ControlType control_type = ControlType::TEXTFIELD) | 53 ControlType control_type = ControlType::TEXTFIELD) |
| 54 : type(type), | 54 : type(type), |
| 55 label(std::move(label)), | 55 label(std::move(label)), |
| 56 length_hint(length_hint), | 56 length_hint(length_hint), |
| 57 required(required), | 57 required(required), |
| (...skipping 24 matching lines...) Expand all Loading... |
| 82 public views::ComboboxListener { | 82 public views::ComboboxListener { |
| 83 public: | 83 public: |
| 84 using TextFieldsMap = | 84 using TextFieldsMap = |
| 85 std::unordered_map<ValidatingTextfield*, const EditorField>; | 85 std::unordered_map<ValidatingTextfield*, const EditorField>; |
| 86 using ComboboxMap = | 86 using ComboboxMap = |
| 87 std::unordered_map<ValidatingCombobox*, const EditorField>; | 87 std::unordered_map<ValidatingCombobox*, const EditorField>; |
| 88 using ErrorLabelMap = | 88 using ErrorLabelMap = |
| 89 std::map<const EditorField, views::View*, EditorField::Compare>; | 89 std::map<const EditorField, views::View*, EditorField::Compare>; |
| 90 | 90 |
| 91 // Does not take ownership of the arguments, which should outlive this object. | 91 // Does not take ownership of the arguments, which should outlive this object. |
| 92 // |back_navigation_type| identifies what sort of back navigation should be |
| 93 // done when editing is successful. This is independent of the back arrow |
| 94 // which always goes back one step. |
| 92 EditorViewController(PaymentRequestSpec* spec, | 95 EditorViewController(PaymentRequestSpec* spec, |
| 93 PaymentRequestState* state, | 96 PaymentRequestState* state, |
| 94 PaymentRequestDialogView* dialog); | 97 PaymentRequestDialogView* dialog, |
| 98 BackNavigationType back_navigation_type); |
| 95 ~EditorViewController() override; | 99 ~EditorViewController() override; |
| 96 | 100 |
| 97 // Will display |error_message| alongside the input field represented by | 101 // Will display |error_message| alongside the input field represented by |
| 98 // |field|. | 102 // |field|. |
| 99 void DisplayErrorMessageForField(const EditorField& field, | 103 void DisplayErrorMessageForField(const EditorField& field, |
| 100 const base::string16& error_message); | 104 const base::string16& error_message); |
| 101 | 105 |
| 102 const ComboboxMap& comboboxes() const { return comboboxes_; } | 106 const ComboboxMap& comboboxes() const { return comboboxes_; } |
| 103 const TextFieldsMap& text_fields() const { return text_fields_; } | 107 const TextFieldsMap& text_fields() const { return text_fields_; } |
| 104 | 108 |
| 105 protected: | 109 protected: |
| 106 virtual std::unique_ptr<views::View> CreateHeaderView() = 0; | 110 // A very long label will wrap. Value picked so that left + right label |
| 111 // padding bring the label to half-way in the dialog (~225). |
| 112 static constexpr int kMaximumLabelWidth = 192; |
| 113 |
| 114 virtual std::unique_ptr<views::View> CreateHeaderView(); |
| 115 virtual std::unique_ptr<views::View> CreateCustomFieldView( |
| 116 autofill::ServerFieldType type); |
| 107 // Returns the field definitions used to build the UI. | 117 // Returns the field definitions used to build the UI. |
| 108 virtual std::vector<EditorField> GetFieldDefinitions() = 0; | 118 virtual std::vector<EditorField> GetFieldDefinitions() = 0; |
| 109 virtual base::string16 GetInitialValueForType( | 119 virtual base::string16 GetInitialValueForType( |
| 110 autofill::ServerFieldType type) = 0; | 120 autofill::ServerFieldType type) = 0; |
| 111 // Validates the data entered and attempts to save; returns true on success. | 121 // Validates the data entered and attempts to save; returns true on success. |
| 112 virtual bool ValidateModelAndSave() = 0; | 122 virtual bool ValidateModelAndSave() = 0; |
| 113 // Creates a ValidationDelegate which knows how to validate for a given | 123 // Creates a ValidationDelegate which knows how to validate for a given |
| 114 // |field| definition. | 124 // |field| definition. |
| 115 virtual std::unique_ptr<ValidationDelegate> CreateValidationDelegate( | 125 virtual std::unique_ptr<ValidationDelegate> CreateValidationDelegate( |
| 116 const EditorField& field) = 0; | 126 const EditorField& field) = 0; |
| 117 virtual std::unique_ptr<ui::ComboboxModel> GetComboboxModelForType( | 127 virtual std::unique_ptr<ui::ComboboxModel> GetComboboxModelForType( |
| 118 const autofill::ServerFieldType& type) = 0; | 128 const autofill::ServerFieldType& type) = 0; |
| 119 | 129 |
| 120 // PaymentRequestSheetController; | 130 // PaymentRequestSheetController; |
| 121 std::unique_ptr<views::Button> CreatePrimaryButton() override; | 131 std::unique_ptr<views::Button> CreatePrimaryButton() override; |
| 122 void FillContentView(views::View* content_view) override; | 132 void FillContentView(views::View* content_view) override; |
| 123 std::unique_ptr<views::View> CreateExtraFooterView() override; | 133 std::unique_ptr<views::View> CreateExtraFooterView() override; |
| 124 | 134 |
| 125 // views::ComboboxListener: | 135 // views::ComboboxListener: |
| 126 void OnPerformAction(views::Combobox* combobox) override; | 136 void OnPerformAction(views::Combobox* combobox) override; |
| 127 | 137 |
| 128 // Update the editor view by removing all it's child views and recreating | 138 // Update the editor view by removing all it's child views and recreating |
| 129 // the input fields returned by GetFieldDefinitions. Note that | 139 // the input fields returned by GetFieldDefinitions. Note that |
| 130 // CreateEditorView MUST have been called at least once before calling | 140 // CreateEditorView MUST have been called at least once before calling |
| 131 // UpdateEditorView. | 141 // UpdateEditorView. |
| 132 virtual void UpdateEditorView(); | 142 virtual void UpdateEditorView(); |
| 133 | 143 |
| 134 private: | |
| 135 // PaymentRequestSheetController: | 144 // PaymentRequestSheetController: |
| 136 void ButtonPressed(views::Button* sender, const ui::Event& event) override; | 145 void ButtonPressed(views::Button* sender, const ui::Event& event) override; |
| 137 views::View* GetFirstFocusedView() override; | 146 views::View* GetFirstFocusedView() override; |
| 138 | 147 |
| 148 private: |
| 139 // views::TextfieldController: | 149 // views::TextfieldController: |
| 140 void ContentsChanged(views::Textfield* sender, | 150 void ContentsChanged(views::Textfield* sender, |
| 141 const base::string16& new_contents) override; | 151 const base::string16& new_contents) override; |
| 142 | 152 |
| 143 // Creates the whole editor view to go within the editor dialog. It | 153 // Creates the whole editor view to go within the editor dialog. It |
| 144 // encompasses all the input fields created by CreateInputField(). | 154 // encompasses all the input fields created by CreateInputField(). |
| 145 std::unique_ptr<views::View> CreateEditorView(); | 155 std::unique_ptr<views::View> CreateEditorView(); |
| 146 | 156 |
| 147 // Adds some views to |layout|, to represent an input field and its labels. | 157 // Adds some views to |layout|, to represent an input field and its labels. |
| 148 // |field| is the field definition, which contains the label and the hint | 158 // |field| is the field definition, which contains the label and the hint |
| 149 // about the length of the input field. A placeholder error label is also | 159 // about the length of the input field. A placeholder error label is also |
| 150 // added (see implementation). | 160 // added (see implementation). |
| 151 void CreateInputField(views::GridLayout* layout, const EditorField& field); | 161 void CreateInputField(views::GridLayout* layout, const EditorField& field); |
| 152 | 162 |
| 153 // Used to remember the association between the input field UI element and the | 163 // Used to remember the association between the input field UI element and the |
| 154 // original field definition. The ValidatingTextfield* and ValidatingCombobox* | 164 // original field definition. The ValidatingTextfield* and ValidatingCombobox* |
| 155 // are owned by their parent view, this only keeps a reference that is good as | 165 // are owned by their parent view, this only keeps a reference that is good as |
| 156 // long as the input field is visible. | 166 // long as the input field is visible. |
| 157 TextFieldsMap text_fields_; | 167 TextFieldsMap text_fields_; |
| 158 ComboboxMap comboboxes_; | 168 ComboboxMap comboboxes_; |
| 159 // Tracks the relationship between a field and its error label. | 169 // Tracks the relationship between a field and its error label. |
| 160 ErrorLabelMap error_labels_; | 170 ErrorLabelMap error_labels_; |
| 161 | 171 |
| 162 // The first label in the editor, used to set the initial focus. | 172 // The first label in the editor, used to set the initial focus. |
| 163 views::View* first_field_view_; | 173 views::View* first_field_view_; |
| 164 | 174 |
| 175 // Identifies where to go back when the editing completes successfully. |
| 176 BackNavigationType back_navigation_type_; |
| 177 |
| 165 DISALLOW_COPY_AND_ASSIGN(EditorViewController); | 178 DISALLOW_COPY_AND_ASSIGN(EditorViewController); |
| 166 }; | 179 }; |
| 167 | 180 |
| 168 } // namespace payments | 181 } // namespace payments |
| 169 | 182 |
| 170 #endif // CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ | 183 #endif // CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ |
| OLD | NEW |