| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_popup_view_cocoa.h" | 5 #import "chrome/browser/ui/cocoa/autofill/autofill_popup_view_cocoa.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
| 9 #include "chrome/browser/ui/autofill/autofill_popup_controller.h" | 9 #include "chrome/browser/ui/autofill/autofill_popup_controller.h" |
| 10 #include "chrome/browser/ui/autofill/popup_constants.h" | 10 #include "chrome/browser/ui/autofill/popup_constants.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 // with a highlight. |index| determines the font to use, as well as the icon, | 30 // with a highlight. |index| determines the font to use, as well as the icon, |
| 31 // if the row requires it -- such as for credit cards. |imageFirst| indicates | 31 // if the row requires it -- such as for credit cards. |imageFirst| indicates |
| 32 // whether the image should be drawn before the name, and with the same | 32 // whether the image should be drawn before the name, and with the same |
| 33 // alignment, or whether it should be drawn afterwards, with the opposite | 33 // alignment, or whether it should be drawn afterwards, with the opposite |
| 34 // alignment. | 34 // alignment. |
| 35 - (void)drawSuggestionWithName:(NSString*)name | 35 - (void)drawSuggestionWithName:(NSString*)name |
| 36 subtext:(NSString*)subtext | 36 subtext:(NSString*)subtext |
| 37 index:(size_t)index | 37 index:(size_t)index |
| 38 bounds:(NSRect)bounds | 38 bounds:(NSRect)bounds |
| 39 selected:(BOOL)isSelected | 39 selected:(BOOL)isSelected |
| 40 imageFirst:(BOOL)imageFirst; | 40 imageFirst:(BOOL)imageFirst |
| 41 textYOffset:(CGFloat)textYOffset; |
| 41 | 42 |
| 42 // This comment block applies to all three draw* methods that follow. | 43 // This comment block applies to all three draw* methods that follow. |
| 43 // If |rightAlign| == YES. | 44 // If |rightAlign| == YES. |
| 44 // Draws the widget with right border aligned to |x|. | 45 // Draws the widget with right border aligned to |x|. |
| 45 // Returns the x value of left border of the widget. | 46 // Returns the x value of left border of the widget. |
| 46 // If |rightAlign| == NO. | 47 // If |rightAlign| == NO. |
| 47 // Draws the widget with left border aligned to |x|. | 48 // Draws the widget with left border aligned to |x|. |
| 48 // Returns the x value of right border of the widget. | 49 // Returns the x value of right border of the widget. |
| 49 - (CGFloat)drawName:(NSString*)name | 50 - (CGFloat)drawName:(NSString*)name |
| 50 atX:(CGFloat)x | 51 atX:(CGFloat)x |
| 51 index:(size_t)index | 52 index:(size_t)index |
| 52 rightAlign:(BOOL)rightAlign | 53 rightAlign:(BOOL)rightAlign |
| 53 bounds:(NSRect)bounds; | 54 bounds:(NSRect)bounds |
| 55 textYOffset:(CGFloat)textYOffset; |
| 54 - (CGFloat)drawIconAtIndex:(size_t)index | 56 - (CGFloat)drawIconAtIndex:(size_t)index |
| 55 atX:(CGFloat)x | 57 atX:(CGFloat)x |
| 56 rightAlign:(BOOL)rightAlign | 58 rightAlign:(BOOL)rightAlign |
| 57 bounds:(NSRect)bounds; | 59 bounds:(NSRect)bounds; |
| 58 - (CGFloat)drawSubtext:(NSString*)subtext | 60 - (CGFloat)drawSubtext:(NSString*)subtext |
| 59 atX:(CGFloat)x | 61 atX:(CGFloat)x |
| 60 rightAlign:(BOOL)rightAlign | 62 rightAlign:(BOOL)rightAlign |
| 61 bounds:(NSRect)bounds; | 63 bounds:(NSRect)bounds |
| 64 textYOffset:(CGFloat)textYOffset; |
| 62 | 65 |
| 63 // Returns the icon for the row with the given |index|, or |nil| if there is | 66 // Returns the icon for the row with the given |index|, or |nil| if there is |
| 64 // none. | 67 // none. |
| 65 - (NSImage*)iconAtIndex:(size_t)index; | 68 - (NSImage*)iconAtIndex:(size_t)index; |
| 66 | 69 |
| 67 @end | 70 @end |
| 68 | 71 |
| 69 @implementation AutofillPopupViewCocoa | 72 @implementation AutofillPopupViewCocoa |
| 70 | 73 |
| 71 #pragma mark - | 74 #pragma mark - |
| (...skipping 28 matching lines...) Expand all Loading... |
| 100 NSRect rowBounds = | 103 NSRect rowBounds = |
| 101 NSRectFromCGRect(controller_->GetRowBounds(i).ToCGRect()); | 104 NSRectFromCGRect(controller_->GetRowBounds(i).ToCGRect()); |
| 102 if (!NSIntersectsRect(rowBounds, dirtyRect)) | 105 if (!NSIntersectsRect(rowBounds, dirtyRect)) |
| 103 continue; | 106 continue; |
| 104 | 107 |
| 105 if (controller_->identifiers()[i] == autofill::POPUP_ITEM_ID_SEPARATOR) { | 108 if (controller_->identifiers()[i] == autofill::POPUP_ITEM_ID_SEPARATOR) { |
| 106 [self drawSeparatorWithBounds:rowBounds]; | 109 [self drawSeparatorWithBounds:rowBounds]; |
| 107 continue; | 110 continue; |
| 108 } | 111 } |
| 109 | 112 |
| 110 BOOL imageFirst = (controller_->identifiers()[i] == | 113 // Additional offset applied to the text in the vertical direction. |
| 111 autofill::POPUP_ITEM_ID_MAC_ACCESS_CONTACTS); | 114 CGFloat textYOffset = 0; |
| 115 BOOL imageFirst = NO; |
| 116 if (controller_->identifiers()[i] == |
| 117 autofill::POPUP_ITEM_ID_MAC_ACCESS_CONTACTS) { |
| 118 // Due to the weighting of the asset used for this autofill entry, the |
| 119 // text needs to be bumped up by 1 pt to make it look vertically aligned. |
| 120 textYOffset = -1; |
| 121 imageFirst = YES; |
| 122 } |
| 123 |
| 112 NSString* name = SysUTF16ToNSString(controller_->names()[i]); | 124 NSString* name = SysUTF16ToNSString(controller_->names()[i]); |
| 113 NSString* subtext = SysUTF16ToNSString(controller_->subtexts()[i]); | 125 NSString* subtext = SysUTF16ToNSString(controller_->subtexts()[i]); |
| 114 BOOL isSelected = static_cast<int>(i) == controller_->selected_line(); | 126 BOOL isSelected = static_cast<int>(i) == controller_->selected_line(); |
| 115 [self drawSuggestionWithName:name | 127 [self drawSuggestionWithName:name |
| 116 subtext:subtext | 128 subtext:subtext |
| 117 index:i | 129 index:i |
| 118 bounds:rowBounds | 130 bounds:rowBounds |
| 119 selected:isSelected | 131 selected:isSelected |
| 120 imageFirst:imageFirst]; | 132 imageFirst:imageFirst |
| 133 textYOffset:textYOffset]; |
| 121 } | 134 } |
| 122 } | 135 } |
| 123 | 136 |
| 124 #pragma mark - | 137 #pragma mark - |
| 125 #pragma mark Public API: | 138 #pragma mark Public API: |
| 126 | 139 |
| 127 - (void)controllerDestroyed { | 140 - (void)controllerDestroyed { |
| 128 // Since the |controller_| either already has been destroyed or is about to | 141 // Since the |controller_| either already has been destroyed or is about to |
| 129 // be, about the only thing we can safely do with it is to null it out. | 142 // be, about the only thing we can safely do with it is to null it out. |
| 130 controller_ = NULL; | 143 controller_ = NULL; |
| 131 [super delegateDestroyed]; | 144 [super delegateDestroyed]; |
| 132 } | 145 } |
| 133 | 146 |
| 134 - (void)invalidateRow:(size_t)row { | 147 - (void)invalidateRow:(size_t)row { |
| 135 NSRect dirty_rect = | 148 NSRect dirty_rect = |
| 136 NSRectFromCGRect(controller_->GetRowBounds(row).ToCGRect()); | 149 NSRectFromCGRect(controller_->GetRowBounds(row).ToCGRect()); |
| 137 [self setNeedsDisplayInRect:dirty_rect]; | 150 [self setNeedsDisplayInRect:dirty_rect]; |
| 138 } | 151 } |
| 139 | 152 |
| 140 #pragma mark - | 153 #pragma mark - |
| 141 #pragma mark Private API: | 154 #pragma mark Private API: |
| 142 | 155 |
| 143 - (void)drawSuggestionWithName:(NSString*)name | 156 - (void)drawSuggestionWithName:(NSString*)name |
| 144 subtext:(NSString*)subtext | 157 subtext:(NSString*)subtext |
| 145 index:(size_t)index | 158 index:(size_t)index |
| 146 bounds:(NSRect)bounds | 159 bounds:(NSRect)bounds |
| 147 selected:(BOOL)isSelected | 160 selected:(BOOL)isSelected |
| 148 imageFirst:(BOOL)imageFirst { | 161 imageFirst:(BOOL)imageFirst |
| 162 textYOffset:(CGFloat)textYOffset { |
| 149 // If this row is selected, highlight it. | 163 // If this row is selected, highlight it. |
| 150 if (isSelected) { | 164 if (isSelected) { |
| 151 [[self highlightColor] set]; | 165 [[self highlightColor] set]; |
| 152 [NSBezierPath fillRect:bounds]; | 166 [NSBezierPath fillRect:bounds]; |
| 153 } | 167 } |
| 154 | 168 |
| 155 BOOL isRTL = controller_->IsRTL(); | 169 BOOL isRTL = controller_->IsRTL(); |
| 156 | 170 |
| 157 // The X values of the left and right borders of the autofill widget. | 171 // The X values of the left and right borders of the autofill widget. |
| 158 CGFloat leftX = NSMinX(bounds) + AutofillPopupView::kEndPadding; | 172 CGFloat leftX = NSMinX(bounds) + AutofillPopupView::kEndPadding; |
| 159 CGFloat rightX = NSMaxX(bounds) - AutofillPopupView::kEndPadding; | 173 CGFloat rightX = NSMaxX(bounds) - AutofillPopupView::kEndPadding; |
| 160 | 174 |
| 161 // Draw left side if isRTL == NO, right side if isRTL == YES. | 175 // Draw left side if isRTL == NO, right side if isRTL == YES. |
| 162 CGFloat x = isRTL ? rightX : leftX; | 176 CGFloat x = isRTL ? rightX : leftX; |
| 163 if (imageFirst) | 177 if (imageFirst) |
| 164 x = [self drawIconAtIndex:index atX:x rightAlign:isRTL bounds:bounds]; | 178 x = [self drawIconAtIndex:index atX:x rightAlign:isRTL bounds:bounds]; |
| 165 [self drawName:name atX:x index:index rightAlign:isRTL bounds:bounds]; | 179 [self drawName:name |
| 180 atX:x |
| 181 index:index |
| 182 rightAlign:isRTL |
| 183 bounds:bounds |
| 184 textYOffset:textYOffset]; |
| 166 | 185 |
| 167 // Draw right side if isRTL == NO, left side if isRTL == YES. | 186 // Draw right side if isRTL == NO, left side if isRTL == YES. |
| 168 x = isRTL ? leftX : rightX; | 187 x = isRTL ? leftX : rightX; |
| 169 if (!imageFirst) | 188 if (!imageFirst) |
| 170 x = [self drawIconAtIndex:index atX:x rightAlign:!isRTL bounds:bounds]; | 189 x = [self drawIconAtIndex:index atX:x rightAlign:!isRTL bounds:bounds]; |
| 171 [self drawSubtext:subtext atX:x rightAlign:!isRTL bounds:bounds]; | 190 [self drawSubtext:subtext |
| 191 atX:x |
| 192 rightAlign:!isRTL |
| 193 bounds:bounds |
| 194 textYOffset:textYOffset]; |
| 172 } | 195 } |
| 173 | 196 |
| 174 - (CGFloat)drawName:(NSString*)name | 197 - (CGFloat)drawName:(NSString*)name |
| 175 atX:(CGFloat)x | 198 atX:(CGFloat)x |
| 176 index:(size_t)index | 199 index:(size_t)index |
| 177 rightAlign:(BOOL)rightAlign | 200 rightAlign:(BOOL)rightAlign |
| 178 bounds:(NSRect)bounds { | 201 bounds:(NSRect)bounds |
| 202 textYOffset:(CGFloat)textYOffset { |
| 179 NSColor* nameColor = | 203 NSColor* nameColor = |
| 180 controller_->IsWarning(index) ? [self warningColor] : [self nameColor]; | 204 controller_->IsWarning(index) ? [self warningColor] : [self nameColor]; |
| 181 NSDictionary* nameAttributes = | 205 NSDictionary* nameAttributes = |
| 182 [NSDictionary dictionaryWithObjectsAndKeys: | 206 [NSDictionary dictionaryWithObjectsAndKeys: |
| 183 controller_->GetNameFontListForRow(index).GetPrimaryFont(). | 207 controller_->GetNameFontListForRow(index).GetPrimaryFont(). |
| 184 GetNativeFont(), | 208 GetNativeFont(), |
| 185 NSFontAttributeName, nameColor, NSForegroundColorAttributeName, | 209 NSFontAttributeName, nameColor, NSForegroundColorAttributeName, |
| 186 nil]; | 210 nil]; |
| 187 NSSize nameSize = [name sizeWithAttributes:nameAttributes]; | 211 NSSize nameSize = [name sizeWithAttributes:nameAttributes]; |
| 188 x -= rightAlign ? nameSize.width : 0; | 212 x -= rightAlign ? nameSize.width : 0; |
| 189 CGFloat y = bounds.origin.y + (bounds.size.height - nameSize.height) / 2; | 213 CGFloat y = bounds.origin.y + (bounds.size.height - nameSize.height) / 2; |
| 214 y += textYOffset; |
| 190 | 215 |
| 191 [name drawAtPoint:NSMakePoint(x, y) withAttributes:nameAttributes]; | 216 [name drawAtPoint:NSMakePoint(x, y) withAttributes:nameAttributes]; |
| 192 | 217 |
| 193 x += rightAlign ? 0 : nameSize.width; | 218 x += rightAlign ? 0 : nameSize.width; |
| 194 return x; | 219 return x; |
| 195 } | 220 } |
| 196 | 221 |
| 197 - (CGFloat)drawIconAtIndex:(size_t)index | 222 - (CGFloat)drawIconAtIndex:(size_t)index |
| 198 atX:(CGFloat)x | 223 atX:(CGFloat)x |
| 199 rightAlign:(BOOL)rightAlign | 224 rightAlign:(BOOL)rightAlign |
| (...skipping 12 matching lines...) Expand all Loading... |
| 212 hints:nil]; | 237 hints:nil]; |
| 213 | 238 |
| 214 x += rightAlign ? -AutofillPopupView::kIconPadding | 239 x += rightAlign ? -AutofillPopupView::kIconPadding |
| 215 : iconSize.width + AutofillPopupView::kIconPadding; | 240 : iconSize.width + AutofillPopupView::kIconPadding; |
| 216 return x; | 241 return x; |
| 217 } | 242 } |
| 218 | 243 |
| 219 - (CGFloat)drawSubtext:(NSString*)subtext | 244 - (CGFloat)drawSubtext:(NSString*)subtext |
| 220 atX:(CGFloat)x | 245 atX:(CGFloat)x |
| 221 rightAlign:(BOOL)rightAlign | 246 rightAlign:(BOOL)rightAlign |
| 222 bounds:(NSRect)bounds { | 247 bounds:(NSRect)bounds |
| 248 textYOffset:(CGFloat)textYOffset { |
| 223 NSDictionary* subtextAttributes = | 249 NSDictionary* subtextAttributes = |
| 224 [NSDictionary dictionaryWithObjectsAndKeys: | 250 [NSDictionary dictionaryWithObjectsAndKeys: |
| 225 controller_->subtext_font_list().GetPrimaryFont().GetNativeFont(), | 251 controller_->subtext_font_list().GetPrimaryFont().GetNativeFont(), |
| 226 NSFontAttributeName, | 252 NSFontAttributeName, |
| 227 [self subtextColor], | 253 [self subtextColor], |
| 228 NSForegroundColorAttributeName, | 254 NSForegroundColorAttributeName, |
| 229 nil]; | 255 nil]; |
| 230 NSSize subtextSize = [subtext sizeWithAttributes:subtextAttributes]; | 256 NSSize subtextSize = [subtext sizeWithAttributes:subtextAttributes]; |
| 231 x -= rightAlign ? subtextSize.width : 0; | 257 x -= rightAlign ? subtextSize.width : 0; |
| 232 CGFloat y = bounds.origin.y + (bounds.size.height - subtextSize.height) / 2; | 258 CGFloat y = bounds.origin.y + (bounds.size.height - subtextSize.height) / 2; |
| 259 y += textYOffset; |
| 233 | 260 |
| 234 [subtext drawAtPoint:NSMakePoint(x, y) withAttributes:subtextAttributes]; | 261 [subtext drawAtPoint:NSMakePoint(x, y) withAttributes:subtextAttributes]; |
| 235 x += rightAlign ? 0 : subtextSize.width; | 262 x += rightAlign ? 0 : subtextSize.width; |
| 236 return x; | 263 return x; |
| 237 } | 264 } |
| 238 | 265 |
| 239 - (NSImage*)iconAtIndex:(size_t)index { | 266 - (NSImage*)iconAtIndex:(size_t)index { |
| 240 if (controller_->icons()[index].empty()) | 267 if (controller_->icons()[index].empty()) |
| 241 return nil; | 268 return nil; |
| 242 | 269 |
| 243 int iconId = controller_->GetIconResourceID(controller_->icons()[index]); | 270 int iconId = controller_->GetIconResourceID(controller_->icons()[index]); |
| 244 DCHECK_NE(-1, iconId); | 271 DCHECK_NE(-1, iconId); |
| 245 | 272 |
| 246 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 273 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 247 return rb.GetNativeImageNamed(iconId).ToNSImage(); | 274 return rb.GetNativeImageNamed(iconId).ToNSImage(); |
| 248 } | 275 } |
| 249 | 276 |
| 250 @end | 277 @end |
| OLD | NEW |