| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); | 61 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
| 62 layout->set_cross_axis_alignment( | 62 layout->set_cross_axis_alignment( |
| 63 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); | 63 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); |
| 64 content_view->SetLayoutManager(layout); | 64 content_view->SetLayoutManager(layout); |
| 65 // No insets. Child views below are responsible for their padding. | 65 // No insets. Child views below are responsible for their padding. |
| 66 | 66 |
| 67 // An editor can optionally have a header view specific to it. | 67 // An editor can optionally have a header view specific to it. |
| 68 content_view->AddChildView(CreateHeaderView().release()); | 68 content_view->AddChildView(CreateHeaderView().release()); |
| 69 | 69 |
| 70 // The heart of the editor dialog: all the input fields with their labels. | 70 // The heart of the editor dialog: all the input fields with their labels. |
| 71 content_view->AddChildView(CreateEditorView().release()); | 71 CreateEditorView(); |
| 72 content_view->AddChildView(editor_view_.get()); |
| 72 | 73 |
| 73 return CreatePaymentView( | 74 return CreatePaymentView( |
| 74 CreateSheetHeaderView( | 75 CreateSheetHeaderView( |
| 75 true, l10n_util::GetStringUTF16(GetViewHeaderTitleId()), this), | 76 true, l10n_util::GetStringUTF16(GetViewHeaderTitleId()), this), |
| 76 std::move(content_view)); | 77 std::move(content_view)); |
| 77 } | 78 } |
| 78 | 79 |
| 79 void EditorViewController::DisplayErrorMessageForField( | 80 void EditorViewController::DisplayErrorMessageForField( |
| 80 const EditorField& field, | 81 const EditorField& field, |
| 81 const base::string16& error_message) { | 82 const base::string16& error_message) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 112 // Adds the "* indicates a required field" label in "disabled" grey text. | 113 // Adds the "* indicates a required field" label in "disabled" grey text. |
| 113 std::unique_ptr<views::Label> label = base::MakeUnique<views::Label>( | 114 std::unique_ptr<views::Label> label = base::MakeUnique<views::Label>( |
| 114 l10n_util::GetStringUTF16(IDS_PAYMENTS_REQUIRED_FIELD_MESSAGE)); | 115 l10n_util::GetStringUTF16(IDS_PAYMENTS_REQUIRED_FIELD_MESSAGE)); |
| 115 label->SetDisabledColor(label->GetNativeTheme()->GetSystemColor( | 116 label->SetDisabledColor(label->GetNativeTheme()->GetSystemColor( |
| 116 ui::NativeTheme::kColorId_LabelDisabledColor)); | 117 ui::NativeTheme::kColorId_LabelDisabledColor)); |
| 117 label->SetEnabled(false); | 118 label->SetEnabled(false); |
| 118 content_view->AddChildView(label.release()); | 119 content_view->AddChildView(label.release()); |
| 119 return content_view; | 120 return content_view; |
| 120 } | 121 } |
| 121 | 122 |
| 123 void EditorViewController::UpdateEditorView() { |
| 124 UpdateEditorViewImpl(); |
| 125 // TODO(crbug.com/704254): Find how to update the parent view bounds so that |
| 126 // the vertical scrollbar size gets updated. |
| 127 dialog()->EditorViewUpdated(); |
| 128 } |
| 129 |
| 122 void EditorViewController::ButtonPressed(views::Button* sender, | 130 void EditorViewController::ButtonPressed(views::Button* sender, |
| 123 const ui::Event& event) { | 131 const ui::Event& event) { |
| 124 switch (sender->tag()) { | 132 switch (sender->tag()) { |
| 125 case static_cast<int>(EditorViewControllerTags::SAVE_BUTTON): | 133 case static_cast<int>(EditorViewControllerTags::SAVE_BUTTON): |
| 126 if (ValidateModelAndSave()) | 134 if (ValidateModelAndSave()) |
| 127 dialog()->GoBack(); | 135 dialog()->GoBack(); |
| 128 break; | 136 break; |
| 129 default: | 137 default: |
| 130 PaymentRequestSheetController::ButtonPressed(sender, event); | 138 PaymentRequestSheetController::ButtonPressed(sender, event); |
| 131 break; | 139 break; |
| 132 } | 140 } |
| 133 } | 141 } |
| 134 | 142 |
| 135 void EditorViewController::ContentsChanged(views::Textfield* sender, | 143 void EditorViewController::ContentsChanged(views::Textfield* sender, |
| 136 const base::string16& new_contents) { | 144 const base::string16& new_contents) { |
| 137 static_cast<ValidatingTextfield*>(sender)->OnContentsChanged(); | 145 static_cast<ValidatingTextfield*>(sender)->OnContentsChanged(); |
| 138 } | 146 } |
| 139 | 147 |
| 140 void EditorViewController::OnPerformAction(views::Combobox* sender) { | 148 void EditorViewController::OnPerformAction(views::Combobox* sender) { |
| 141 static_cast<ValidatingCombobox*>(sender)->OnContentsChanged(); | 149 static_cast<ValidatingCombobox*>(sender)->OnContentsChanged(); |
| 142 } | 150 } |
| 143 | 151 |
| 144 std::unique_ptr<views::View> EditorViewController::CreateEditorView() { | 152 void EditorViewController::CreateEditorView() { |
| 145 std::unique_ptr<views::View> editor_view = base::MakeUnique<views::View>(); | 153 editor_view_ = base::MakeUnique<views::View>(); |
| 154 editor_view_->set_owned_by_client(); |
| 146 | 155 |
| 147 views::GridLayout* editor_layout = new views::GridLayout(editor_view.get()); | 156 UpdateEditorViewImpl(); |
| 148 | |
| 149 // The editor grid layout is padded vertically from the top and bottom, and | |
| 150 // horizontally inset like other content views. The top padding needs to be | |
| 151 // added to the top padding of the first row. | |
| 152 constexpr int kEditorVerticalInset = 16; | |
| 153 editor_layout->SetInsets( | |
| 154 kEditorVerticalInset, payments::kPaymentRequestRowHorizontalInsets, | |
| 155 kEditorVerticalInset, payments::kPaymentRequestRowHorizontalInsets); | |
| 156 | |
| 157 editor_view->SetLayoutManager(editor_layout); | |
| 158 views::ColumnSet* columns = editor_layout->AddColumnSet(0); | |
| 159 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, | |
| 160 views::GridLayout::USE_PREF, 0, 0); | |
| 161 | |
| 162 // This is the horizontal padding between the label and the input field. | |
| 163 constexpr int kLabelInputFieldHorizontalPadding = 16; | |
| 164 columns->AddPaddingColumn(0, kLabelInputFieldHorizontalPadding); | |
| 165 | |
| 166 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, | |
| 167 views::GridLayout::USE_PREF, 0, 0); | |
| 168 | |
| 169 std::vector<EditorField> fields = GetFieldDefinitions(); | |
| 170 for (const auto& field : fields) { | |
| 171 CreateInputField(editor_layout, field); | |
| 172 } | |
| 173 | |
| 174 return editor_view; | |
| 175 } | 157 } |
| 176 | 158 |
| 177 // Each input field is a 4-quadrant grid. | 159 // Each input field is a 4-quadrant grid. |
| 178 // +----------------------------------------------------------+ | 160 // +----------------------------------------------------------+ |
| 179 // | Field Label | Input field (textfield/combobox) | | 161 // | Field Label | Input field (textfield/combobox) | |
| 180 // |_______________________|__________________________________| | 162 // |_______________________|__________________________________| |
| 181 // | (empty) | Error label | | 163 // | (empty) | Error label | |
| 182 // +----------------------------------------------------------+ | 164 // +----------------------------------------------------------+ |
| 183 void EditorViewController::CreateInputField(views::GridLayout* layout, | 165 void EditorViewController::CreateInputField(views::GridLayout* layout, |
| 184 const EditorField& field) { | 166 const EditorField& field) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 field.type); | 215 field.type); |
| 234 error_label->SetFontList( | 216 error_label->SetFontList( |
| 235 error_label->GetDefaultFontList().DeriveWithSizeDelta(-1)); | 217 error_label->GetDefaultFontList().DeriveWithSizeDelta(-1)); |
| 236 error_label->SetEnabledColor(error_label->GetNativeTheme()->GetSystemColor( | 218 error_label->SetEnabledColor(error_label->GetNativeTheme()->GetSystemColor( |
| 237 ui::NativeTheme::kColorId_AlertSeverityHigh)); | 219 ui::NativeTheme::kColorId_AlertSeverityHigh)); |
| 238 error_labels_[field] = error_label.get(); | 220 error_labels_[field] = error_label.get(); |
| 239 | 221 |
| 240 layout->AddView(error_label.release()); | 222 layout->AddView(error_label.release()); |
| 241 } | 223 } |
| 242 | 224 |
| 225 void EditorViewController::UpdateEditorViewImpl() { |
| 226 DCHECK_NE(nullptr, editor_view_.get()); |
| 227 text_fields_.clear(); |
| 228 comboboxes_.clear(); |
| 229 editor_view_->RemoveAllChildViews(true); |
| 230 |
| 231 std::unique_ptr<views::GridLayout> editor_layout = |
| 232 base::MakeUnique<views::GridLayout>(editor_view_.get()); |
| 233 |
| 234 // The editor grid layout is padded vertically from the top and bottom, and |
| 235 // horizontally inset like other content views. The top padding needs to be |
| 236 // added to the top padding of the first row. |
| 237 constexpr int kEditorVerticalInset = 16; |
| 238 editor_layout->SetInsets( |
| 239 kEditorVerticalInset, payments::kPaymentRequestRowHorizontalInsets, |
| 240 kEditorVerticalInset, payments::kPaymentRequestRowHorizontalInsets); |
| 241 |
| 242 views::ColumnSet* columns = editor_layout->AddColumnSet(0); |
| 243 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
| 244 views::GridLayout::USE_PREF, 0, 0); |
| 245 |
| 246 // This is the horizontal padding between the label and the input field. |
| 247 constexpr int kLabelInputFieldHorizontalPadding = 16; |
| 248 columns->AddPaddingColumn(0, kLabelInputFieldHorizontalPadding); |
| 249 |
| 250 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
| 251 views::GridLayout::USE_PREF, 0, 0); |
| 252 |
| 253 // The LayoutManager needs to be set before input fields are created, so we |
| 254 // keep a handle to it before we release it to the view. |
| 255 views::GridLayout* layout_handle = editor_layout.get(); |
| 256 editor_view_->SetLayoutManager(editor_layout.release()); |
| 257 std::vector<EditorField> fields = GetFieldDefinitions(); |
| 258 for (const auto& field : fields) { |
| 259 CreateInputField(layout_handle, field); |
| 260 } |
| 261 editor_view_->Layout(); |
| 262 } |
| 243 } // namespace payments | 263 } // namespace payments |
| OLD | NEW |