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