Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: chrome/browser/ui/views/payments/editor_view_controller.cc

Issue 2871873003: [Payments] Fix up field widths in desktop editors. (Closed)
Patch Set: addressed comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 99
100 std::unique_ptr<views::View> EditorViewController::CreateHeaderView() { 100 std::unique_ptr<views::View> EditorViewController::CreateHeaderView() {
101 return nullptr; 101 return nullptr;
102 } 102 }
103 103
104 std::unique_ptr<views::View> EditorViewController::CreateCustomFieldView( 104 std::unique_ptr<views::View> EditorViewController::CreateCustomFieldView(
105 autofill::ServerFieldType type) { 105 autofill::ServerFieldType type) {
106 return nullptr; 106 return nullptr;
107 } 107 }
108 108
109 std::unique_ptr<views::View> EditorViewController::CreateExtraViewForField(
110 autofill::ServerFieldType type) {
111 return nullptr;
112 }
113
109 std::unique_ptr<views::Button> EditorViewController::CreatePrimaryButton() { 114 std::unique_ptr<views::Button> EditorViewController::CreatePrimaryButton() {
110 std::unique_ptr<views::Button> button( 115 std::unique_ptr<views::Button> button(
111 views::MdTextButton::CreateSecondaryUiBlueButton( 116 views::MdTextButton::CreateSecondaryUiBlueButton(
112 this, l10n_util::GetStringUTF16(IDS_DONE))); 117 this, l10n_util::GetStringUTF16(IDS_DONE)));
113 button->set_tag(static_cast<int>(EditorViewControllerTags::SAVE_BUTTON)); 118 button->set_tag(static_cast<int>(EditorViewControllerTags::SAVE_BUTTON));
114 button->set_id(static_cast<int>(DialogViewID::EDITOR_SAVE_BUTTON)); 119 button->set_id(static_cast<int>(DialogViewID::EDITOR_SAVE_BUTTON));
115 return button; 120 return button;
116 } 121 }
117 122
118 void EditorViewController::FillContentView(views::View* content_view) { 123 void EditorViewController::FillContentView(views::View* content_view) {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 203
199 void EditorViewController::OnPerformAction(views::Combobox* sender) { 204 void EditorViewController::OnPerformAction(views::Combobox* sender) {
200 static_cast<ValidatingCombobox*>(sender)->OnContentsChanged(); 205 static_cast<ValidatingCombobox*>(sender)->OnContentsChanged();
201 } 206 }
202 207
203 std::unique_ptr<views::View> EditorViewController::CreateEditorView() { 208 std::unique_ptr<views::View> EditorViewController::CreateEditorView() {
204 std::unique_ptr<views::View> editor_view = base::MakeUnique<views::View>(); 209 std::unique_ptr<views::View> editor_view = base::MakeUnique<views::View>();
205 text_fields_.clear(); 210 text_fields_.clear();
206 comboboxes_.clear(); 211 comboboxes_.clear();
207 212
208 std::unique_ptr<views::GridLayout> editor_layout =
209 base::MakeUnique<views::GridLayout>(editor_view.get());
210
211 // The editor view is padded horizontally. 213 // The editor view is padded horizontally.
212 editor_view->SetBorder(views::CreateEmptyBorder( 214 editor_view->SetBorder(views::CreateEmptyBorder(
213 0, payments::kPaymentRequestRowHorizontalInsets, 0, 215 0, payments::kPaymentRequestRowHorizontalInsets, 0,
214 payments::kPaymentRequestRowHorizontalInsets)); 216 payments::kPaymentRequestRowHorizontalInsets));
215 217
216 views::ColumnSet* columns = editor_layout->AddColumnSet(0); 218 // All views have fixed size except the Field which stretches. The fixed
217 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, 219 // padding at the end is computed so that Field views have a minimum of
218 views::GridLayout::USE_PREF, 0, 0); 220 // 176/272dp (short/long fields) as per spec.
221 // ___________________________________________________________________________
222 // |Label | 16dp pad | Field (flex) | 8dp pad | Extra View | Computed Padding|
223 // |______|__________|______________|_________|____________|_________________|
224 constexpr int kLabelWidth = 140;
225 // This is the horizontal padding between the label and the field.
226 constexpr int kLabelInputFieldHorizontalPadding = 16;
227 // This is the horizontal padding between the field and the extra view.
228 constexpr int kFieldExtraViewHorizontalPadding = 8;
229 constexpr int kShortFieldMinimumWidth = 176;
230 constexpr int kLongFieldMinimumWidth = 272;
219 231
220 // This is the horizontal padding between the label and the input field. 232 std::unique_ptr<views::GridLayout> editor_layout =
221 constexpr int kLabelInputFieldHorizontalPadding = 16; 233 base::MakeUnique<views::GridLayout>(editor_view.get());
222 columns->AddPaddingColumn(0, kLabelInputFieldHorizontalPadding); 234 // Column set for short fields.
235 views::ColumnSet* columns_short = editor_layout->AddColumnSet(0);
236 columns_short->AddColumn(views::GridLayout::LEADING,
237 views::GridLayout::CENTER, 0,
238 views::GridLayout::FIXED, kLabelWidth, 0);
239 columns_short->AddPaddingColumn(0, kLabelInputFieldHorizontalPadding);
240 // The field view column stretches.
241 columns_short->AddColumn(views::GridLayout::LEADING,
242 views::GridLayout::CENTER, 1,
243 views::GridLayout::USE_PREF, 0, 0);
244 columns_short->AddPaddingColumn(0, kFieldExtraViewHorizontalPadding);
245 // The extra field view column is fixed size, computed from the largest
246 // extra view.
247 int short_extra_view_width =
248 ComputeWidestExtraViewWidth(EditorField::LengthHint::HINT_SHORT);
249 columns_short->AddColumn(views::GridLayout::LEADING,
250 views::GridLayout::CENTER, 0,
251 views::GridLayout::FIXED, short_extra_view_width, 0);
252 // The padding at the end is fixed, computed to make sure the short field
253 // maintains its minimum width.
254 int short_padding = kDialogMinWidth - kShortFieldMinimumWidth - kLabelWidth -
255 (2 * kPaymentRequestRowHorizontalInsets) -
256 kLabelInputFieldHorizontalPadding -
257 kFieldExtraViewHorizontalPadding - short_extra_view_width;
258 columns_short->AddPaddingColumn(0, short_padding);
223 259
224 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, 260 // Column set for long fields.
225 views::GridLayout::USE_PREF, 0, 0); 261 views::ColumnSet* columns_long = editor_layout->AddColumnSet(1);
262 columns_long->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER,
263 0, views::GridLayout::FIXED, kLabelWidth, 0);
264 columns_long->AddPaddingColumn(0, kLabelInputFieldHorizontalPadding);
265 // The field view column stretches.
266 columns_long->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER,
267 1, views::GridLayout::USE_PREF, 0, 0);
268 columns_long->AddPaddingColumn(0, kFieldExtraViewHorizontalPadding);
269 // The extra field view column is fixed size, computed from the largest
270 // extra view.
271 int long_extra_view_width =
272 ComputeWidestExtraViewWidth(EditorField::LengthHint::HINT_LONG);
273 columns_long->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER,
274 0, views::GridLayout::FIXED, long_extra_view_width,
275 0);
276 // The padding at the end is fixed, computed to make sure the long field
277 // maintains its minimum width.
278 int long_padding = kDialogMinWidth - kLongFieldMinimumWidth - kLabelWidth -
279 (2 * kPaymentRequestRowHorizontalInsets) -
280 kLabelInputFieldHorizontalPadding -
281 kFieldExtraViewHorizontalPadding - long_extra_view_width;
282 columns_long->AddPaddingColumn(0, long_padding);
226 283
227 editor_view->SetLayoutManager(editor_layout.release()); 284 editor_view->SetLayoutManager(editor_layout.release());
228 std::vector<EditorField> fields = GetFieldDefinitions(); 285 for (const auto& field : GetFieldDefinitions()) {
229 for (const auto& field : fields) {
230 CreateInputField( 286 CreateInputField(
231 static_cast<views::GridLayout*>(editor_view->GetLayoutManager()), 287 static_cast<views::GridLayout*>(editor_view->GetLayoutManager()),
232 field); 288 field);
233 } 289 }
234 290
235 return editor_view; 291 return editor_view;
236 } 292 }
237 293
238 // Each input field is a 4-quadrant grid. 294 // Each input field is a 4-quadrant grid.
239 // +----------------------------------------------------------+ 295 // +----------------------------------------------------------+
240 // | Field Label | Input field (textfield/combobox) | 296 // | Field Label | Input field (textfield/combobox) |
241 // |_______________________|__________________________________| 297 // |_______________________|__________________________________|
242 // | (empty) | Error label | 298 // | (empty) | Error label |
243 // +----------------------------------------------------------+ 299 // +----------------------------------------------------------+
244 void EditorViewController::CreateInputField(views::GridLayout* layout, 300 void EditorViewController::CreateInputField(views::GridLayout* layout,
245 const EditorField& field) { 301 const EditorField& field) {
302 int column_set =
303 field.length_hint == EditorField::LengthHint::HINT_SHORT ? 0 : 1;
304
246 // This is the top padding for every row. 305 // This is the top padding for every row.
247 constexpr int kInputRowSpacing = 6; 306 constexpr int kInputRowSpacing = 6;
248 layout->StartRowWithPadding(0, 0, 0, kInputRowSpacing); 307 layout->StartRowWithPadding(0, column_set, 0, kInputRowSpacing);
249 308
250 std::unique_ptr<views::Label> label = base::MakeUnique<views::Label>( 309 std::unique_ptr<views::Label> label = base::MakeUnique<views::Label>(
251 field.required ? field.label + base::ASCIIToUTF16("*") : field.label); 310 field.required ? field.label + base::ASCIIToUTF16("*") : field.label);
252 311
253 label->SetMultiLine(true); 312 label->SetMultiLine(true);
254 label->SetMaximumWidth(kMaximumLabelWidth);
255 layout->AddView(label.release()); 313 layout->AddView(label.release());
256 314
257 constexpr int kInputFieldHeight = 28; 315 constexpr int kInputFieldHeight = 28;
258 if (field.control_type == EditorField::ControlType::TEXTFIELD) { 316 if (field.control_type == EditorField::ControlType::TEXTFIELD) {
259 ValidatingTextfield* text_field = 317 ValidatingTextfield* text_field =
260 new ValidatingTextfield(CreateValidationDelegate(field)); 318 new ValidatingTextfield(CreateValidationDelegate(field));
261 text_field->SetText(GetInitialValueForType(field.type)); 319 text_field->SetText(GetInitialValueForType(field.type));
262 text_field->set_controller(this); 320 text_field->set_controller(this);
263 // Using autofill field type as a view ID (for testing). 321 // Using autofill field type as a view ID (for testing).
264 text_field->set_id(static_cast<int>(field.type)); 322 text_field->set_id(static_cast<int>(field.type));
265 text_fields_.insert(std::make_pair(text_field, field)); 323 text_fields_.insert(std::make_pair(text_field, field));
266 324
267 // TODO(crbug.com/718582): Make the initial focus the first incomplete/empty 325 // TODO(crbug.com/718582): Make the initial focus the first incomplete/empty
268 // field. 326 // field.
269 if (!first_field_view_) 327 if (!first_field_view_)
270 first_field_view_ = text_field; 328 first_field_view_ = text_field;
271 329
272 // |text_field| will now be owned by |row|. 330 // |text_field| will now be owned by |row|.
273 layout->AddView(text_field, 1, 1, views::GridLayout::FILL, 331 layout->AddView(text_field, 1, 1, views::GridLayout::FILL,
274 views::GridLayout::FILL, 0, kInputFieldHeight); 332 views::GridLayout::FILL, 0, kInputFieldHeight);
275 } else if (field.control_type == EditorField::ControlType::COMBOBOX) { 333 } else if (field.control_type == EditorField::ControlType::COMBOBOX) {
276 ValidatingCombobox* combobox = new ValidatingCombobox( 334 ValidatingCombobox* combobox = new ValidatingCombobox(
277 GetComboboxModelForType(field.type), CreateValidationDelegate(field)); 335 GetComboboxModelForType(field.type), CreateValidationDelegate(field));
278 combobox->SelectValue(GetInitialValueForType(field.type)); 336 base::string16 initial_value = GetInitialValueForType(field.type);
279 // Using autofill field type as a view ID (for testing). 337 if (!initial_value.empty())
338 combobox->SelectValue(initial_value);
339 // Using autofill field type as a view ID.
280 combobox->set_id(static_cast<int>(field.type)); 340 combobox->set_id(static_cast<int>(field.type));
281 combobox->set_listener(this); 341 combobox->set_listener(this);
282 comboboxes_.insert(std::make_pair(combobox, field)); 342 comboboxes_.insert(std::make_pair(combobox, field));
283 343
284 if (!first_field_view_) 344 if (!first_field_view_)
285 first_field_view_ = combobox; 345 first_field_view_ = combobox;
286 346
287 // |combobox| will now be owned by |row|. 347 // |combobox| will now be owned by |row|.
288 layout->AddView(combobox, 1, 1, views::GridLayout::FILL, 348 layout->AddView(combobox, 1, 1, views::GridLayout::FILL,
289 views::GridLayout::FILL, 0, kInputFieldHeight); 349 views::GridLayout::FILL, 0, kInputFieldHeight);
290 } else { 350 } else {
291 // Custom field view will now be owned by |row|. And it must be valid since 351 // Custom field view will now be owned by |row|. And it must be valid since
292 // the derived class specified a custom view for this field. 352 // the derived class specified a custom view for this field.
293 layout->AddView(CreateCustomFieldView(field.type).release()); 353 std::unique_ptr<views::View> field_view = CreateCustomFieldView(field.type);
354 DCHECK(field_view);
355 layout->AddView(field_view.release());
294 } 356 }
295 357
296 layout->StartRow(0, 0); 358 // If an extra view needs to go alongside the input field view, add it to the
359 // last column.
360 std::unique_ptr<views::View> extra_view = CreateExtraViewForField(field.type);
361 if (extra_view)
362 layout->AddView(extra_view.release());
363
364 layout->StartRow(0, column_set);
297 layout->SkipColumns(1); 365 layout->SkipColumns(1);
298 std::unique_ptr<views::View> error_label_view = 366 std::unique_ptr<views::View> error_label_view =
299 base::MakeUnique<views::View>(); 367 base::MakeUnique<views::View>();
300 error_label_view->SetLayoutManager(new views::FillLayout); 368 error_label_view->SetLayoutManager(new views::FillLayout);
301 error_labels_[field] = error_label_view.get(); 369 error_labels_[field] = error_label_view.get();
302 layout->AddView(error_label_view.release()); 370 layout->AddView(error_label_view.release());
303 371
304 // Bottom padding for the row. 372 // Bottom padding for the row.
305 layout->AddPaddingRow(0, kInputRowSpacing); 373 layout->AddPaddingRow(0, kInputRowSpacing);
306 } 374 }
307 375
376 int EditorViewController::ComputeWidestExtraViewWidth(
377 EditorField::LengthHint size) {
378 int widest_column_width = 0;
379
380 for (const auto& field : GetFieldDefinitions()) {
381 if (field.length_hint != size)
382 continue;
383
384 std::unique_ptr<views::View> extra_view =
385 CreateExtraViewForField(field.type);
386 if (!extra_view)
387 continue;
388 widest_column_width =
389 std::max(extra_view->GetPreferredSize().width(), widest_column_width);
390 }
391 return widest_column_width;
392 }
393
308 } // namespace payments 394 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698