| 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 28 matching lines...) Expand all Loading... |
| 39 // The tag for the button that saves the model being edited. Starts at | 39 // The tag for the button that saves the model being edited. Starts at |
| 40 // |kFirstTagValue| not to conflict with tags common to all views. | 40 // |kFirstTagValue| not to conflict with tags common to all views. |
| 41 SAVE_BUTTON = kFirstTagValue, | 41 SAVE_BUTTON = kFirstTagValue, |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 constexpr int kNumCharactersInShortField = 8; | 44 constexpr int kNumCharactersInShortField = 8; |
| 45 constexpr int kNumCharactersInLongField = 20; | 45 constexpr int kNumCharactersInLongField = 20; |
| 46 | 46 |
| 47 } // namespace | 47 } // namespace |
| 48 | 48 |
| 49 EditorViewController::EditorViewController(PaymentRequestSpec* spec, | 49 EditorViewController::EditorViewController( |
| 50 PaymentRequestState* state, | 50 PaymentRequestSpec* spec, |
| 51 PaymentRequestDialogView* dialog) | 51 PaymentRequestState* state, |
| 52 PaymentRequestDialogView* dialog, |
| 53 BackNavigationType back_navigation_type) |
| 52 : PaymentRequestSheetController(spec, state, dialog), | 54 : PaymentRequestSheetController(spec, state, dialog), |
| 53 first_field_view_(nullptr) {} | 55 first_field_view_(nullptr), |
| 56 back_navigation_type_(back_navigation_type) {} |
| 54 | 57 |
| 55 EditorViewController::~EditorViewController() {} | 58 EditorViewController::~EditorViewController() {} |
| 56 | 59 |
| 57 void EditorViewController::DisplayErrorMessageForField( | 60 void EditorViewController::DisplayErrorMessageForField( |
| 58 const EditorField& field, | 61 const EditorField& field, |
| 59 const base::string16& error_message) { | 62 const base::string16& error_message) { |
| 60 const auto& label_it = error_labels_.find(field); | 63 const auto& label_it = error_labels_.find(field); |
| 61 DCHECK(label_it != error_labels_.end()); | 64 DCHECK(label_it != error_labels_.end()); |
| 62 label_it->second->SetText(error_message); | 65 label_it->second->SetText(error_message); |
| 63 label_it->second->SchedulePaint(); | 66 label_it->second->SchedulePaint(); |
| 64 dialog()->Layout(); | 67 dialog()->Layout(); |
| 65 } | 68 } |
| 66 | 69 |
| 70 std::unique_ptr<views::View> EditorViewController::CreateHeaderView() { |
| 71 return nullptr; |
| 72 } |
| 73 |
| 74 std::unique_ptr<views::View> EditorViewController::CreateCustomFieldView( |
| 75 autofill::ServerFieldType type) { |
| 76 return nullptr; |
| 77 } |
| 78 |
| 67 std::unique_ptr<views::Button> EditorViewController::CreatePrimaryButton() { | 79 std::unique_ptr<views::Button> EditorViewController::CreatePrimaryButton() { |
| 68 std::unique_ptr<views::Button> button( | 80 std::unique_ptr<views::Button> button( |
| 69 views::MdTextButton::CreateSecondaryUiBlueButton( | 81 views::MdTextButton::CreateSecondaryUiBlueButton( |
| 70 this, l10n_util::GetStringUTF16(IDS_DONE))); | 82 this, l10n_util::GetStringUTF16(IDS_DONE))); |
| 71 button->set_tag(static_cast<int>(EditorViewControllerTags::SAVE_BUTTON)); | 83 button->set_tag(static_cast<int>(EditorViewControllerTags::SAVE_BUTTON)); |
| 72 button->set_id(static_cast<int>(DialogViewID::EDITOR_SAVE_BUTTON)); | 84 button->set_id(static_cast<int>(DialogViewID::EDITOR_SAVE_BUTTON)); |
| 73 return button; | 85 return button; |
| 74 } | 86 } |
| 75 | 87 |
| 76 void EditorViewController::FillContentView(views::View* content_view) { | 88 void EditorViewController::FillContentView(views::View* content_view) { |
| 77 views::BoxLayout* layout = | 89 views::BoxLayout* layout = |
| 78 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); | 90 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); |
| 79 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); | 91 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
| 80 layout->set_cross_axis_alignment( | 92 layout->set_cross_axis_alignment( |
| 81 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); | 93 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); |
| 82 content_view->SetLayoutManager(layout); | 94 content_view->SetLayoutManager(layout); |
| 83 // No insets. Child views below are responsible for their padding. | 95 // No insets. Child views below are responsible for their padding. |
| 84 | 96 |
| 85 // An editor can optionally have a header view specific to it. | 97 // An editor can optionally have a header view specific to it. |
| 86 content_view->AddChildView(CreateHeaderView().release()); | 98 std::unique_ptr<views::View> header_view = CreateHeaderView(); |
| 99 if (header_view.get()) |
| 100 content_view->AddChildView(header_view.release()); |
| 87 | 101 |
| 88 // The heart of the editor dialog: all the input fields with their labels. | 102 // The heart of the editor dialog: all the input fields with their labels. |
| 89 content_view->AddChildView(CreateEditorView().release()); | 103 content_view->AddChildView(CreateEditorView().release()); |
| 90 } | 104 } |
| 91 | 105 |
| 92 // Adds the "required fields" label in disabled text, to obtain this result. | 106 // Adds the "required fields" label in disabled text, to obtain this result. |
| 93 // +---------------------------------------------------------+ | 107 // +---------------------------------------------------------+ |
| 94 // | "* indicates required fields" | CANCEL | DONE | | 108 // | "* indicates required fields" | CANCEL | DONE | |
| 95 // +---------------------------------------------------------+ | 109 // +---------------------------------------------------------+ |
| 96 std::unique_ptr<views::View> EditorViewController::CreateExtraFooterView() { | 110 std::unique_ptr<views::View> EditorViewController::CreateExtraFooterView() { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 117 UpdateContentView(); | 131 UpdateContentView(); |
| 118 // TODO(crbug.com/704254): Find how to update the parent view bounds so that | 132 // TODO(crbug.com/704254): Find how to update the parent view bounds so that |
| 119 // the vertical scrollbar size gets updated. | 133 // the vertical scrollbar size gets updated. |
| 120 dialog()->EditorViewUpdated(); | 134 dialog()->EditorViewUpdated(); |
| 121 } | 135 } |
| 122 | 136 |
| 123 void EditorViewController::ButtonPressed(views::Button* sender, | 137 void EditorViewController::ButtonPressed(views::Button* sender, |
| 124 const ui::Event& event) { | 138 const ui::Event& event) { |
| 125 switch (sender->tag()) { | 139 switch (sender->tag()) { |
| 126 case static_cast<int>(EditorViewControllerTags::SAVE_BUTTON): | 140 case static_cast<int>(EditorViewControllerTags::SAVE_BUTTON): |
| 127 if (ValidateModelAndSave()) | 141 if (ValidateModelAndSave()) { |
| 128 dialog()->GoBackToPaymentSheet(); | 142 switch (back_navigation_type_) { |
| 143 case BackNavigationType::kOneStep: |
| 144 dialog()->GoBack(); |
| 145 break; |
| 146 case BackNavigationType::kPaymentSheet: |
| 147 dialog()->GoBackToPaymentSheet(); |
| 148 break; |
| 149 } |
| 150 } |
| 129 break; | 151 break; |
| 130 default: | 152 default: |
| 131 PaymentRequestSheetController::ButtonPressed(sender, event); | 153 PaymentRequestSheetController::ButtonPressed(sender, event); |
| 132 break; | 154 break; |
| 133 } | 155 } |
| 134 } | 156 } |
| 135 | 157 |
| 136 views::View* EditorViewController::GetFirstFocusedView() { | 158 views::View* EditorViewController::GetFirstFocusedView() { |
| 137 if (first_field_view_) | 159 if (first_field_view_) |
| 138 return first_field_view_; | 160 return first_field_view_; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 168 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, | 190 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
| 169 views::GridLayout::USE_PREF, 0, 0); | 191 views::GridLayout::USE_PREF, 0, 0); |
| 170 | 192 |
| 171 // This is the horizontal padding between the label and the input field. | 193 // This is the horizontal padding between the label and the input field. |
| 172 constexpr int kLabelInputFieldHorizontalPadding = 16; | 194 constexpr int kLabelInputFieldHorizontalPadding = 16; |
| 173 columns->AddPaddingColumn(0, kLabelInputFieldHorizontalPadding); | 195 columns->AddPaddingColumn(0, kLabelInputFieldHorizontalPadding); |
| 174 | 196 |
| 175 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, | 197 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
| 176 views::GridLayout::USE_PREF, 0, 0); | 198 views::GridLayout::USE_PREF, 0, 0); |
| 177 | 199 |
| 178 // The LayoutManager needs to be set before input fields are created, so we | |
| 179 // keep a handle to it before we release it to the view. | |
| 180 views::GridLayout* layout_handle = editor_layout.get(); | |
| 181 editor_view->SetLayoutManager(editor_layout.release()); | 200 editor_view->SetLayoutManager(editor_layout.release()); |
| 182 std::vector<EditorField> fields = GetFieldDefinitions(); | 201 std::vector<EditorField> fields = GetFieldDefinitions(); |
| 183 for (const auto& field : fields) { | 202 for (const auto& field : fields) { |
| 184 CreateInputField(layout_handle, field); | 203 CreateInputField( |
| 204 static_cast<views::GridLayout*>(editor_view->GetLayoutManager()), |
| 205 field); |
| 185 } | 206 } |
| 186 | 207 |
| 187 return editor_view; | 208 return editor_view; |
| 188 } | 209 } |
| 189 | 210 |
| 190 // Each input field is a 4-quadrant grid. | 211 // Each input field is a 4-quadrant grid. |
| 191 // +----------------------------------------------------------+ | 212 // +----------------------------------------------------------+ |
| 192 // | Field Label | Input field (textfield/combobox) | | 213 // | Field Label | Input field (textfield/combobox) | |
| 193 // |_______________________|__________________________________| | 214 // |_______________________|__________________________________| |
| 194 // | (empty) | Error label | | 215 // | (empty) | Error label | |
| 195 // +----------------------------------------------------------+ | 216 // +----------------------------------------------------------+ |
| 196 void EditorViewController::CreateInputField(views::GridLayout* layout, | 217 void EditorViewController::CreateInputField(views::GridLayout* layout, |
| 197 const EditorField& field) { | 218 const EditorField& field) { |
| 198 // This is the top padding for every row. | 219 // This is the top padding for every row. |
| 199 constexpr int kInputRowSpacing = 6; | 220 constexpr int kInputRowSpacing = 6; |
| 200 layout->StartRowWithPadding(0, 0, 0, kInputRowSpacing); | 221 layout->StartRowWithPadding(0, 0, 0, kInputRowSpacing); |
| 201 | 222 |
| 202 std::unique_ptr<views::Label> label = base::MakeUnique<views::Label>( | 223 std::unique_ptr<views::Label> label = base::MakeUnique<views::Label>( |
| 203 field.required ? field.label + base::ASCIIToUTF16("*") : field.label); | 224 field.required ? field.label + base::ASCIIToUTF16("*") : field.label); |
| 204 // A very long label will wrap. Value picked so that left + right label | 225 |
| 205 // padding bring the label to half-way in the dialog (~225). | |
| 206 constexpr int kMaximumLabelWidth = 192; | |
| 207 label->SetMultiLine(true); | 226 label->SetMultiLine(true); |
| 208 label->SetMaximumWidth(kMaximumLabelWidth); | 227 label->SetMaximumWidth(kMaximumLabelWidth); |
| 209 layout->AddView(label.release()); | 228 layout->AddView(label.release()); |
| 210 | 229 |
| 211 if (field.control_type == EditorField::ControlType::TEXTFIELD) { | 230 if (field.control_type == EditorField::ControlType::TEXTFIELD) { |
| 212 ValidatingTextfield* text_field = | 231 ValidatingTextfield* text_field = |
| 213 new ValidatingTextfield(CreateValidationDelegate(field)); | 232 new ValidatingTextfield(CreateValidationDelegate(field)); |
| 214 text_field->SetText(GetInitialValueForType(field.type)); | 233 text_field->SetText(GetInitialValueForType(field.type)); |
| 215 text_field->set_controller(this); | 234 text_field->set_controller(this); |
| 216 // Using autofill field type as a view ID (for testing). | 235 // Using autofill field type as a view ID (for testing). |
| (...skipping 20 matching lines...) Expand all Loading... |
| 237 combobox->set_id(static_cast<int>(field.type)); | 256 combobox->set_id(static_cast<int>(field.type)); |
| 238 combobox->set_listener(this); | 257 combobox->set_listener(this); |
| 239 comboboxes_.insert(std::make_pair(combobox, field)); | 258 comboboxes_.insert(std::make_pair(combobox, field)); |
| 240 | 259 |
| 241 if (!first_field_view_) | 260 if (!first_field_view_) |
| 242 first_field_view_ = combobox; | 261 first_field_view_ = combobox; |
| 243 | 262 |
| 244 // |combobox| will now be owned by |row|. | 263 // |combobox| will now be owned by |row|. |
| 245 layout->AddView(combobox); | 264 layout->AddView(combobox); |
| 246 } else { | 265 } else { |
| 247 NOTREACHED(); | 266 // Custom field view will now be owned by |row|. And it must be valid since |
| 267 // the derived class specified a custom view for this field. |
| 268 layout->AddView(CreateCustomFieldView(field.type).release()); |
| 248 } | 269 } |
| 249 | 270 |
| 250 // This is the vertical space between the input field and its error label. | 271 // This is the vertical space between the input field and its error label. |
| 251 constexpr int kInputErrorLabelPadding = 6; | 272 constexpr int kInputErrorLabelPadding = 6; |
| 252 layout->StartRowWithPadding(0, 0, 0, kInputErrorLabelPadding); | 273 layout->StartRowWithPadding(0, 0, 0, kInputErrorLabelPadding); |
| 253 layout->SkipColumns(1); | 274 layout->SkipColumns(1); |
| 254 // Error label is initially empty. | 275 // Error label is initially empty. |
| 255 std::unique_ptr<views::Label> error_label = | 276 std::unique_ptr<views::Label> error_label = |
| 256 base::MakeUnique<views::Label>(base::ASCIIToUTF16("")); | 277 base::MakeUnique<views::Label>(base::ASCIIToUTF16("")); |
| 257 error_label->set_id(static_cast<int>(DialogViewID::ERROR_LABEL_OFFSET) + | 278 error_label->set_id(static_cast<int>(DialogViewID::ERROR_LABEL_OFFSET) + |
| 258 field.type); | 279 field.type); |
| 259 error_label->SetFontList( | 280 error_label->SetFontList( |
| 260 error_label->GetDefaultFontList().DeriveWithSizeDelta(-1)); | 281 error_label->GetDefaultFontList().DeriveWithSizeDelta(-1)); |
| 261 error_label->SetEnabledColor(error_label->GetNativeTheme()->GetSystemColor( | 282 error_label->SetEnabledColor(error_label->GetNativeTheme()->GetSystemColor( |
| 262 ui::NativeTheme::kColorId_AlertSeverityHigh)); | 283 ui::NativeTheme::kColorId_AlertSeverityHigh)); |
| 263 error_labels_[field] = error_label.get(); | 284 error_labels_[field] = error_label.get(); |
| 264 | 285 |
| 265 layout->AddView(error_label.release()); | 286 layout->AddView(error_label.release()); |
| 266 } | 287 } |
| 267 | 288 |
| 268 } // namespace payments | 289 } // namespace payments |
| OLD | NEW |