| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 #import "chrome/browser/ui/cocoa/autofill/autofill_section_container.h" | 5 #import "chrome/browser/ui/cocoa/autofill/autofill_section_container.h" |
| 6 | 6 |
| 7 #include "base/strings/sys_string_conversions.h" | 7 #include "base/strings/sys_string_conversions.h" |
| 8 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h" | 8 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h" |
| 9 #import "chrome/browser/ui/cocoa/autofill/autofill_textfield.h" | 9 #import "chrome/browser/ui/cocoa/autofill/autofill_textfield.h" |
| 10 #import "chrome/browser/ui/cocoa/autofill/layout_view.h" | 10 #import "chrome/browser/ui/cocoa/autofill/layout_view.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 // Top/bottom inset for contents of a detail section. | 43 // Top/bottom inset for contents of a detail section. |
| 44 const size_t kDetailSectionInset = 10; | 44 const size_t kDetailSectionInset = 10; |
| 45 | 45 |
| 46 } | 46 } |
| 47 | 47 |
| 48 @interface AutofillSectionContainer (Internal) | 48 @interface AutofillSectionContainer (Internal) |
| 49 | 49 |
| 50 // Create properly styled label for section. Autoreleased. | 50 // Create properly styled label for section. Autoreleased. |
| 51 - (NSTextField*)makeDetailSectionLabel:(NSString*)labelText; | 51 - (NSTextField*)makeDetailSectionLabel:(NSString*)labelText; |
| 52 | 52 |
| 53 // Create NSView containing inputs & labelling. Autoreleased. | |
| 54 - (NSView*)makeSectionView:(NSString*)labelText | |
| 55 withControls:(LayoutView*)controls; | |
| 56 | |
| 57 // Create a button offering input suggestions. | 53 // Create a button offering input suggestions. |
| 58 - (MenuButton*)makeSuggestionButton; | 54 - (MenuButton*)makeSuggestionButton; |
| 59 | 55 |
| 60 // Create a view with all inputs requested by |controller_|. Autoreleased. | 56 // Create a view with all inputs requested by |controller_|. Autoreleased. |
| 61 - (LayoutView*)makeInputControls; | 57 - (LayoutView*)makeInputControls; |
| 62 | 58 |
| 63 @end | 59 @end |
| 64 | 60 |
| 65 @implementation AutofillSectionContainer | 61 @implementation AutofillSectionContainer |
| 66 | 62 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 96 const BOOL hasSuggestions = [menu numberOfItems] > 0; | 92 const BOOL hasSuggestions = [menu numberOfItems] > 0; |
| 97 [suggestButton_ setHidden:!hasSuggestions]; | 93 [suggestButton_ setHidden:!hasSuggestions]; |
| 98 | 94 |
| 99 [suggestButton_ setAttachedMenu:menu]; | 95 [suggestButton_ setAttachedMenu:menu]; |
| 100 } | 96 } |
| 101 | 97 |
| 102 - (void)loadView { | 98 - (void)loadView { |
| 103 inputs_.reset([[self makeInputControls] retain]); | 99 inputs_.reset([[self makeInputControls] retain]); |
| 104 string16 labelText = controller_->LabelForSection(section_); | 100 string16 labelText = controller_->LabelForSection(section_); |
| 105 | 101 |
| 106 scoped_nsobject<NSView> sectionView( | 102 label_.reset([[self makeDetailSectionLabel: |
| 107 [[self makeSectionView:base::SysUTF16ToNSString(labelText) | 103 base::SysUTF16ToNSString(labelText)] retain]); |
| 108 withControls:inputs_] retain]); | 104 |
| 109 suggestButton_.reset([[self makeSuggestionButton] retain]); | 105 suggestButton_.reset([[self makeSuggestionButton] retain]); |
| 110 | 106 |
| 111 NSRect buttonFrame = [suggestButton_ frame]; | |
| 112 buttonFrame.origin.x = NSMaxX([sectionView frame]); | |
| 113 NSRect frame = NSUnionRect(buttonFrame, [sectionView frame]); | |
| 114 DCHECK(NSHeight(frame) >= NSHeight(buttonFrame) + 2 * kDetailSectionInset); | |
| 115 buttonFrame.origin.y = | |
| 116 NSMaxY(frame) - NSHeight(buttonFrame) - kDetailSectionInset; | |
| 117 [suggestButton_ setFrame:buttonFrame]; | |
| 118 [self modelChanged]; | 107 [self modelChanged]; |
| 119 | 108 |
| 120 [self setView:[[[NSView alloc] initWithFrame:frame] autorelease]]; | 109 view_.reset([[NSView alloc] initWithFrame:NSZeroRect]); |
| 121 [[self view] setSubviews:@[sectionView, suggestButton_]]; | 110 [self performLayout]; |
| 111 [self setView:view_]; |
| 112 [[self view] setSubviews:@[label_, inputs_, suggestButton_]]; |
| 113 } |
| 114 |
| 115 - (NSSize)preferredSize { |
| 116 NSSize labelSize = [label_ frame].size; // Assumes sizeToFit was called. |
| 117 CGFloat contentHeight = [inputs_ preferredHeightForWidth:kDetailsWidth]; |
| 118 contentHeight = std::max(contentHeight, labelSize.height); |
| 119 contentHeight = std::max(contentHeight, NSHeight([suggestButton_ frame])); |
| 120 |
| 121 return NSMakeSize(kLabelWidth + kPadding + kDetailsWidth, |
| 122 contentHeight + 2 * kDetailSectionInset); |
| 123 } |
| 124 |
| 125 - (void)performLayout { |
| 126 NSSize buttonSize = [suggestButton_ frame].size; // Assume sizeToFit. |
| 127 NSSize labelSize = [label_ frame].size; // Assumes sizeToFit was called. |
| 128 CGFloat controlHeight = [inputs_ preferredHeightForWidth:kDetailsWidth]; |
| 129 |
| 130 NSRect viewFrame = NSZeroRect; |
| 131 viewFrame.size = [self preferredSize]; |
| 132 |
| 133 NSRect contentFrame = NSInsetRect(viewFrame, 0, kDetailSectionInset); |
| 134 NSRect dummy; |
| 135 |
| 136 // Set up three content columns. kLabelWidth is first column width, |
| 137 // then padding, then have suggestButton and inputs share kDetailsWidth. |
| 138 NSRect column[3]; |
| 139 NSDivideRect(contentFrame, &column[0], &dummy, kLabelWidth, NSMinXEdge); |
| 140 NSDivideRect(contentFrame, &column[1], &dummy, kDetailsWidth, NSMaxXEdge); |
| 141 NSDivideRect(column[1], |
| 142 &column[2], &column[1], buttonSize.width, NSMaxXEdge); |
| 143 |
| 144 // Center inputs by height in column 1. |
| 145 NSRect controlFrame = column[1]; |
| 146 int centerOffset = (NSHeight(controlFrame) - controlHeight) / 2; |
| 147 controlFrame.origin.x += centerOffset; |
| 148 controlFrame.size.height = controlHeight; |
| 149 |
| 150 // Align label to right top in column 0. |
| 151 NSRect labelFrame; |
| 152 NSDivideRect(column[0], &labelFrame, &dummy, labelSize.height, NSMaxYEdge); |
| 153 NSDivideRect(labelFrame, &labelFrame, &dummy, labelSize.width, NSMaxXEdge); |
| 154 |
| 155 // suggest button is top left of column 2. |
| 156 NSRect buttonFrame = column[2]; |
| 157 NSDivideRect(column[2], &buttonFrame, &dummy, buttonSize.height, NSMaxYEdge); |
| 158 |
| 159 [inputs_ setFrame:controlFrame]; |
| 160 [label_ setFrame:labelFrame]; |
| 161 [suggestButton_ setFrame:buttonFrame]; |
| 162 [view_ setFrame:viewFrame]; |
| 122 } | 163 } |
| 123 | 164 |
| 124 - (NSTextField*)makeDetailSectionLabel:(NSString*)labelText { | 165 - (NSTextField*)makeDetailSectionLabel:(NSString*)labelText { |
| 125 scoped_nsobject<NSTextField> label([[NSTextField alloc] init]); | 166 scoped_nsobject<NSTextField> label([[NSTextField alloc] init]); |
| 126 [label setFont: | 167 [label setFont: |
| 127 [[NSFontManager sharedFontManager] convertFont:[label font] | 168 [[NSFontManager sharedFontManager] convertFont:[label font] |
| 128 toHaveTrait:NSBoldFontMask]]; | 169 toHaveTrait:NSBoldFontMask]]; |
| 129 [label setStringValue:labelText]; | 170 [label setStringValue:labelText]; |
| 130 [label sizeToFit]; | 171 [label sizeToFit]; |
| 131 [label setEditable:NO]; | 172 [label setEditable:NO]; |
| 132 [label setBordered:NO]; | 173 [label setBordered:NO]; |
| 133 [label sizeToFit]; | 174 [label sizeToFit]; |
| 134 return label.autorelease(); | 175 return label.autorelease(); |
| 135 } | 176 } |
| 136 | 177 |
| 137 - (NSView*)makeSectionView:(NSString*)labelText | |
| 138 withControls:(LayoutView*)controls { | |
| 139 scoped_nsobject<NSTextField> label( | |
| 140 [[self makeDetailSectionLabel:labelText] retain]); | |
| 141 | |
| 142 CGFloat controlHeight = [controls preferredHeightForWidth:kDetailsWidth]; | |
| 143 NSRect frame = NSZeroRect; | |
| 144 frame.size.width = kLabelWidth + kPadding + kDetailsWidth; | |
| 145 frame.size.height = std::max(NSHeight([label frame]), controlHeight) + | |
| 146 2 * kDetailSectionInset; | |
| 147 scoped_nsobject<NSView> section_container( | |
| 148 [[NSView alloc] initWithFrame:frame]); | |
| 149 | |
| 150 NSPoint labelOrigin = NSMakePoint( | |
| 151 kLabelWidth - NSWidth([label frame]), | |
| 152 NSHeight(frame) - NSHeight([label frame]) - kDetailSectionInset); | |
| 153 [label setFrameOrigin:labelOrigin]; | |
| 154 [label setAutoresizingMask:(NSViewMinYMargin | NSViewMinYMargin)]; | |
| 155 | |
| 156 NSRect dummyFrame; | |
| 157 NSRect controlFrame = [controls frame]; | |
| 158 NSDivideRect(NSInsetRect(frame, 0, kDetailSectionInset), | |
| 159 &controlFrame, &dummyFrame, kDetailsWidth, NSMaxXEdge); | |
| 160 controlFrame.size.height = controlHeight; | |
| 161 [controls setFrame:controlFrame]; | |
| 162 [controls setAutoresizingMask:(NSViewMaxXMargin | NSViewMinYMargin)]; | |
| 163 | |
| 164 [section_container setSubviews:@[label, controls]]; | |
| 165 return section_container.autorelease(); | |
| 166 } | |
| 167 | |
| 168 - (MenuButton*)makeSuggestionButton { | 178 - (MenuButton*)makeSuggestionButton { |
| 169 scoped_nsobject<MenuButton> button([[MenuButton alloc] init]); | 179 scoped_nsobject<MenuButton> button([[MenuButton alloc] init]); |
| 170 | 180 |
| 171 [button setOpenMenuOnClick:YES]; | 181 [button setOpenMenuOnClick:YES]; |
| 172 [button setBordered:NO]; | 182 [button setBordered:NO]; |
| 173 [button setShowsBorderOnlyWhileMouseInside:YES]; | 183 [button setShowsBorderOnlyWhileMouseInside:YES]; |
| 174 | 184 |
| 175 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 185 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 176 NSImage* image = | 186 NSImage* image = |
| 177 rb.GetNativeImageNamed(IDR_AUTOFILL_DIALOG_MENU_BUTTON).ToNSImage(); | 187 rb.GetNativeImageNamed(IDR_AUTOFILL_DIALOG_MENU_BUTTON).ToNSImage(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 for (NSControl* control in [inputs_ subviews]) { | 275 for (NSControl* control in [inputs_ subviews]) { |
| 266 const autofill::DetailInput* detailInput = | 276 const autofill::DetailInput* detailInput = |
| 267 reinterpret_cast<autofill::DetailInput*>([control tag]); | 277 reinterpret_cast<autofill::DetailInput*>([control tag]); |
| 268 DCHECK(detailInput); | 278 DCHECK(detailInput); |
| 269 if (detailInput->type == type) | 279 if (detailInput->type == type) |
| 270 return control; | 280 return control; |
| 271 } | 281 } |
| 272 return nil; | 282 return nil; |
| 273 } | 283 } |
| 274 | 284 |
| 275 @end | 285 @end |
| OLD | NEW |