| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/autofill/autofill_section_container.h" |
| 6 |
| 7 #include "base/strings/sys_string_conversions.h" |
| 8 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h" |
| 9 #import "chrome/browser/ui/cocoa/autofill/autofill_textfield.h" |
| 10 #import "chrome/browser/ui/cocoa/autofill/layout_view.h" |
| 11 #include "chrome/browser/ui/cocoa/autofill/simple_grid_layout.h" |
| 12 #include "ui/base/l10n/l10n_util_mac.h" |
| 13 #include "ui/base/models/combobox_model.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 // Constants used for layouting controls. These variables are copied from |
| 18 // "ui/views/layout/layout_constants.h". |
| 19 |
| 20 // Horizontal spacing between controls that are logically related. |
| 21 const int kRelatedControlHorizontalSpacing = 8; |
| 22 |
| 23 // Vertical spacing between controls that are logically related. |
| 24 const int kRelatedControlVerticalSpacing = 8; |
| 25 |
| 26 } |
| 27 |
| 28 @interface AutofillSectionContainer (Internal) |
| 29 |
| 30 // Create properly styled label for section. Autoreleased. |
| 31 - (NSTextField*)makeDetailSectionLabel:(NSString*)labelText; |
| 32 |
| 33 // Create NSView containing inputs & labelling. Autoreleased. |
| 34 - (NSView*)makeSectionView:(NSString*)labelText |
| 35 withControls:(LayoutView*)controls; |
| 36 |
| 37 // Create a view with all inputs requested by |controller_|. Autoreleased. |
| 38 - (LayoutView*)makeInputControls; |
| 39 |
| 40 @end |
| 41 |
| 42 @implementation AutofillSectionContainer |
| 43 |
| 44 @synthesize section = section_; |
| 45 |
| 46 - (id)initWithController:(autofill::AutofillDialogController*)controller |
| 47 forSection:(autofill::DialogSection)section { |
| 48 if (self = [super init]) { |
| 49 section_ = section; |
| 50 controller_ = controller; |
| 51 } |
| 52 return self; |
| 53 } |
| 54 |
| 55 - (void)loadView { |
| 56 scoped_nsobject<LayoutView> inputs([[self makeInputControls] retain]); |
| 57 string16 labelText = controller_->LabelForSection(section_); |
| 58 [self setView:[self makeSectionView:base::SysUTF16ToNSString(labelText) |
| 59 withControls:inputs]]; |
| 60 } |
| 61 |
| 62 - (NSTextField*)makeDetailSectionLabel:(NSString*)labelText { |
| 63 scoped_nsobject<NSTextField> label([[NSTextField alloc] init]); |
| 64 [label setFont: |
| 65 [[NSFontManager sharedFontManager] convertFont:[label font] |
| 66 toHaveTrait:NSBoldFontMask]]; |
| 67 [label setStringValue:labelText]; |
| 68 [label sizeToFit]; |
| 69 [label setEditable:NO]; |
| 70 [label setBordered:NO]; |
| 71 [label sizeToFit]; |
| 72 return label.autorelease(); |
| 73 } |
| 74 |
| 75 - (NSView*)makeSectionView:(NSString*)labelText |
| 76 withControls:(LayoutView*)controls { |
| 77 // TODO(estade): pull out these constants, and figure out better values |
| 78 // for them. Note: These are duplicated from Views code. |
| 79 const int kLabelWidth = 180; |
| 80 const int kPadding = 30; |
| 81 const int kDetailsWidth = 300; |
| 82 const size_t kDetailSectionInset = 10; |
| 83 |
| 84 scoped_nsobject<NSTextField> label( |
| 85 [[self makeDetailSectionLabel:labelText] retain]); |
| 86 |
| 87 CGFloat controlHeight = [controls preferredHeightForWidth:kDetailsWidth]; |
| 88 NSRect frame = NSZeroRect; |
| 89 frame.size.width = kLabelWidth + kPadding + kDetailsWidth; |
| 90 frame.size.height = std::max(NSHeight([label frame]), controlHeight) + |
| 91 2 * kDetailSectionInset; |
| 92 scoped_nsobject<NSView> section_container( |
| 93 [[NSView alloc] initWithFrame:frame]); |
| 94 |
| 95 NSPoint labelOrigin = NSMakePoint( |
| 96 kLabelWidth - NSWidth([label frame]), |
| 97 NSHeight(frame) - NSHeight([label frame]) - kDetailSectionInset); |
| 98 [label setFrameOrigin:labelOrigin]; |
| 99 [label setAutoresizingMask:(NSViewMinYMargin | NSViewMinYMargin)]; |
| 100 |
| 101 NSRect dummyFrame; |
| 102 NSRect controlFrame = [controls frame]; |
| 103 NSDivideRect(NSInsetRect(frame, 0, kDetailSectionInset), |
| 104 &controlFrame, &dummyFrame, kDetailsWidth, NSMaxXEdge); |
| 105 controlFrame.size.height = controlHeight; |
| 106 [controls setFrame:controlFrame]; |
| 107 [controls setAutoresizingMask:(NSViewMaxXMargin | NSViewMinYMargin)]; |
| 108 |
| 109 [section_container setSubviews:@[label, controls]]; |
| 110 return section_container.autorelease(); |
| 111 } |
| 112 |
| 113 // TODO(estade): we should be using Chrome-style constrained window padding |
| 114 // values. |
| 115 - (LayoutView*)makeInputControls { |
| 116 const autofill::DetailInputs& inputs = |
| 117 controller_->RequestedFieldsForSection(section_); |
| 118 |
| 119 scoped_nsobject<LayoutView> view([[LayoutView alloc] init]); |
| 120 [view setLayoutManager: |
| 121 scoped_ptr<SimpleGridLayout>(new SimpleGridLayout(view))]; |
| 122 SimpleGridLayout* layout = [view layoutManager]; |
| 123 |
| 124 for (autofill::DetailInputs::const_iterator it = inputs.begin(); |
| 125 it != inputs.end(); ++it) { |
| 126 const autofill::DetailInput& input = *it; |
| 127 int kColumnSetId = input.row_id; |
| 128 ColumnSet* column_set = layout->GetColumnSet(kColumnSetId); |
| 129 if (!column_set) { |
| 130 // Create a new column set and row. |
| 131 column_set = layout->AddColumnSet(kColumnSetId); |
| 132 if (it != inputs.begin()) |
| 133 layout->AddPaddingRow(kRelatedControlVerticalSpacing); |
| 134 layout->StartRow(0, kColumnSetId); |
| 135 } else { |
| 136 // Add a new column to existing row. |
| 137 column_set->AddPaddingColumn(kRelatedControlHorizontalSpacing); |
| 138 // Must explicitly skip the padding column since we've already started |
| 139 // adding views. |
| 140 layout->SkipColumns(1); |
| 141 } |
| 142 |
| 143 column_set->AddColumn(input.expand_weight ? input.expand_weight : 1.0f); |
| 144 |
| 145 ui::ComboboxModel* input_model = |
| 146 controller_->ComboboxModelForAutofillType(input.type); |
| 147 if (input_model) { |
| 148 scoped_nsobject<NSPopUpButton> popup( |
| 149 [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:YES]); |
| 150 for (int i = 0; i < input_model->GetItemCount(); ++i) { |
| 151 [popup addItemWithTitle: |
| 152 base::SysUTF16ToNSString(input_model->GetItemAt(i))]; |
| 153 } |
| 154 [popup selectItemWithTitle:base::SysUTF16ToNSString(input.initial_value)]; |
| 155 [popup sizeToFit]; |
| 156 layout->AddView(popup); |
| 157 } else { |
| 158 scoped_nsobject<AutofillTextField> field( |
| 159 [[AutofillTextField alloc] init]); |
| 160 [[field cell] setPlaceholderString: |
| 161 l10n_util::GetNSStringWithFixup(input.placeholder_text_rid)]; |
| 162 [[field cell] setIcon: |
| 163 controller_->IconForField( |
| 164 input.type, input.initial_value).AsNSImage()]; |
| 165 [[field cell] setInvalid:YES]; |
| 166 [field sizeToFit]; |
| 167 layout->AddView(field); |
| 168 } |
| 169 } |
| 170 |
| 171 return view.autorelease(); |
| 172 } |
| 173 |
| 174 @end |
| OLD | NEW |