OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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_suggestion_container.h" | 5 #import "chrome/browser/ui/cocoa/autofill/autofill_suggestion_container.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 // #1) Many computations on Retina devices overflow with that value. | 29 // #1) Many computations on Retina devices overflow with that value. |
30 // #2) In this particular use case, it results in the message | 30 // #2) In this particular use case, it results in the message |
31 // "CGAffineTransformInvert: singular matrix." | 31 // "CGAffineTransformInvert: singular matrix." |
32 const CGFloat kInfiniteSize = 1.0e6; | 32 const CGFloat kInfiniteSize = 1.0e6; |
33 | 33 |
34 // A line fragment padding that creates the same visual look as text layout in | 34 // A line fragment padding that creates the same visual look as text layout in |
35 // an NSTextField does. (Which UX feedback was based on) | 35 // an NSTextField does. (Which UX feedback was based on) |
36 const CGFloat kLineFragmentPadding = 2.0; | 36 const CGFloat kLineFragmentPadding = 2.0; |
37 | 37 |
38 // Padding added on top of the label so its first line looks centered with | 38 // Padding added on top of the label so its first line looks centered with |
39 // respect to the input field. | 39 // respect to the input field. Only added when the input field is showing. |
40 const CGFloat kLabelTopPadding = 5.0; | 40 const CGFloat kLabelWithInputTopPadding = 5.0; |
41 | 41 |
42 } | 42 } |
43 | 43 |
44 // An attachment cell for a single icon - takes care of proper alignment of | 44 // An attachment cell for a single icon - takes care of proper alignment of |
45 // text and icon. | 45 // text and icon. |
46 @interface IconAttachmentCell : NSTextAttachmentCell { | 46 @interface IconAttachmentCell : NSTextAttachmentCell { |
47 CGFloat baseline_; // The cell's baseline adjustment. | 47 CGFloat baseline_; // The cell's baseline adjustment. |
48 } | 48 } |
49 | 49 |
50 // Adjust the cell's baseline so that the lower edge of the image aligns with | 50 // Adjust the cell's baseline so that the lower edge of the image aligns with |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 - (void)loadView { | 103 - (void)loadView { |
104 label_.reset([[NSTextView alloc] initWithFrame:NSZeroRect]); | 104 label_.reset([[NSTextView alloc] initWithFrame:NSZeroRect]); |
105 [[label_ textContainer] setLineFragmentPadding:kLineFragmentPadding]; | 105 [[label_ textContainer] setLineFragmentPadding:kLineFragmentPadding]; |
106 [label_ setEditable:NO]; | 106 [label_ setEditable:NO]; |
107 [label_ setSelectable:NO]; | 107 [label_ setSelectable:NO]; |
108 [label_ setDrawsBackground:NO]; | 108 [label_ setDrawsBackground:NO]; |
109 | 109 |
110 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( | 110 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( |
111 [[NSMutableParagraphStyle alloc] init]); | 111 [[NSMutableParagraphStyle alloc] init]); |
112 [paragraphStyle setLineHeightMultiple:1.5]; | 112 [paragraphStyle setLineSpacing:0.5 * [[label_ font] pointSize]]; |
113 [label_ setDefaultParagraphStyle:paragraphStyle]; | 113 [label_ setDefaultParagraphStyle:paragraphStyle]; |
114 | 114 |
115 inputField_.reset([[AutofillTextField alloc] initWithFrame:NSZeroRect]); | 115 inputField_.reset([[AutofillTextField alloc] initWithFrame:NSZeroRect]); |
116 [inputField_ setHidden:YES]; | 116 [inputField_ setHidden:YES]; |
117 | 117 |
118 spacer_.reset([[NSBox alloc] initWithFrame:NSZeroRect]); | 118 spacer_.reset([[NSBox alloc] initWithFrame:NSZeroRect]); |
119 [spacer_ setBoxType:NSBoxSeparator]; | 119 [spacer_ setBoxType:NSBoxSeparator]; |
120 [spacer_ setBorderType:NSLineBorder]; | 120 [spacer_ setBorderType:NSLineBorder]; |
121 | 121 |
122 base::scoped_nsobject<NSView> view([[NSView alloc] initWithFrame:NSZeroRect]); | 122 base::scoped_nsobject<NSView> view([[NSView alloc] initWithFrame:NSZeroRect]); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 | 164 |
165 // Enforce fixed width. | 165 // Enforce fixed width. |
166 NSSize frameSize = NSMakeSize(autofill::kFieldWidth, | 166 NSSize frameSize = NSMakeSize(autofill::kFieldWidth, |
167 NSHeight([inputField_ frame])); | 167 NSHeight([inputField_ frame])); |
168 [inputField_ setFrameSize:frameSize]; | 168 [inputField_ setFrameSize:frameSize]; |
169 } | 169 } |
170 | 170 |
171 | 171 |
172 - (NSSize)preferredSize { | 172 - (NSSize)preferredSize { |
173 NSSize size = [label_ bounds].size; | 173 NSSize size = [label_ bounds].size; |
174 size.height += kLabelTopPadding; | |
175 | 174 |
176 // Final inputField_ sizing/spacing depends on a TODO(estade) in Views code. | 175 // Final inputField_ sizing/spacing depends on a TODO(estade) in Views code. |
177 if (![inputField_ isHidden]) { | 176 if (![inputField_ isHidden]) { |
178 size.height = std::max(size.height, NSHeight([inputField_ frame])); | 177 size.height = std::max(size.height + kLabelWithInputTopPadding, |
| 178 NSHeight([inputField_ frame])); |
179 size.width += NSWidth([inputField_ frame]) + kAroundTextPadding; | 179 size.width += NSWidth([inputField_ frame]) + kAroundTextPadding; |
180 } | 180 } |
181 | 181 |
182 size.height += kTopPadding; | 182 size.height += kTopPadding; |
183 | 183 |
184 return size; | 184 return size; |
185 } | 185 } |
186 | 186 |
187 - (void)performLayout { | 187 - (void)performLayout { |
188 NSRect bounds = [[self view] bounds]; | 188 NSRect bounds = [[self view] bounds]; |
189 NSSize preferredContainerSize = [self preferredSize]; | 189 NSSize preferredContainerSize = [self preferredSize]; |
190 // width is externally determined. | 190 // width is externally determined. |
191 preferredContainerSize.width = NSWidth(bounds); | 191 preferredContainerSize.width = NSWidth(bounds); |
192 | 192 |
193 NSRect spacerFrame = NSMakeRect(0, preferredContainerSize.height - 1, | 193 NSRect spacerFrame = NSMakeRect(0, preferredContainerSize.height - 1, |
194 preferredContainerSize.width, 1); | 194 preferredContainerSize.width, 1); |
195 | 195 |
196 NSRect labelFrame = [label_ bounds]; | 196 NSRect labelFrame = [label_ bounds]; |
197 labelFrame.origin.x = NSMinX(bounds); | 197 labelFrame.origin.x = NSMinX(bounds); |
198 labelFrame.origin.y = | 198 labelFrame.origin.y = NSMaxY(bounds) - NSHeight(labelFrame) - kTopPadding; |
199 NSMaxY(bounds) - kLabelTopPadding - NSHeight(labelFrame) - kTopPadding; | |
200 | 199 |
201 // Position input field - top is aligned to top of label field. | 200 // Position input field - top is aligned to top of label field. |
202 if (![inputField_ isHidden]) { | 201 if (![inputField_ isHidden]) { |
203 NSRect inputfieldFrame = [inputField_ frame]; | 202 NSRect inputFieldFrame = [inputField_ frame]; |
204 inputfieldFrame.origin.x = NSMaxX(bounds) - NSWidth(inputfieldFrame); | 203 inputFieldFrame.origin.x = NSMaxX(bounds) - NSWidth(inputFieldFrame); |
205 inputfieldFrame.origin.y = | 204 inputFieldFrame.origin.y = NSMaxY(labelFrame) - NSHeight(inputFieldFrame); |
206 NSMaxY(labelFrame) + kLabelTopPadding - NSHeight(inputfieldFrame); | 205 [inputField_ setFrameOrigin:inputFieldFrame.origin]; |
207 [inputField_ setFrameOrigin:inputfieldFrame.origin]; | 206 |
| 207 // Vertically center the first line of the label with respect to the input |
| 208 // field. |
| 209 labelFrame.origin.y -= kLabelWithInputTopPadding; |
208 | 210 |
209 // Due to fixed width, fields are guaranteed to not overlap. | 211 // Due to fixed width, fields are guaranteed to not overlap. |
210 DCHECK_LE(NSMaxX(labelFrame), NSMinX(inputfieldFrame)); | 212 DCHECK_LE(NSMaxX(labelFrame), NSMinX(inputFieldFrame)); |
211 } | 213 } |
212 | 214 |
213 [spacer_ setFrame:spacerFrame]; | 215 [spacer_ setFrame:spacerFrame]; |
214 [label_ setFrame:labelFrame]; | 216 [label_ setFrame:labelFrame]; |
215 [[self view] setFrameSize:preferredContainerSize]; | 217 [[self view] setFrameSize:preferredContainerSize]; |
216 } | 218 } |
217 | 219 |
218 @end | 220 @end |
OLD | NEW |