| 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 10 matching lines...) Expand all Loading... |
| 21 using autofill::AutofillPopupView; | 21 using autofill::AutofillPopupView; |
| 22 | 22 |
| 23 @interface AutofillPopupViewCocoa () | 23 @interface AutofillPopupViewCocoa () |
| 24 | 24 |
| 25 #pragma mark - | 25 #pragma mark - |
| 26 #pragma mark Private methods | 26 #pragma mark Private methods |
| 27 | 27 |
| 28 // Draws an Autofill suggestion in the given |bounds|, labeled with the given | 28 // Draws an Autofill suggestion in the given |bounds|, labeled with the given |
| 29 // |name| and |subtext| hint. If the suggestion |isSelected|, then it is drawn | 29 // |name| and |subtext| hint. If the suggestion |isSelected|, then it is drawn |
| 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. | 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 |
| 33 // alignment, or whether it should be drawn afterwards, with the opposite |
| 34 // alignment. |
| 32 - (void)drawSuggestionWithName:(NSString*)name | 35 - (void)drawSuggestionWithName:(NSString*)name |
| 33 subtext:(NSString*)subtext | 36 subtext:(NSString*)subtext |
| 34 index:(size_t)index | 37 index:(size_t)index |
| 35 bounds:(NSRect)bounds | 38 bounds:(NSRect)bounds |
| 36 selected:(BOOL)isSelected; | 39 selected:(BOOL)isSelected |
| 40 imageFirst:(BOOL)imageFirst; |
| 41 |
| 42 // This comment block applies to all three draw* methods that follow. |
| 43 // If |rightAlign| == YES. |
| 44 // Draws the widget with right border aligned to |x|. |
| 45 // Returns the x value of left border of the widget. |
| 46 // If |rightAlign| == NO. |
| 47 // Draws the widget with left border aligned to |x|. |
| 48 // Returns the x value of right border of the widget. |
| 49 - (CGFloat)drawName:(NSString*)name |
| 50 atX:(CGFloat)x |
| 51 index:(size_t)index |
| 52 rightAlign:(BOOL)rightAlign |
| 53 bounds:(NSRect)bounds; |
| 54 - (CGFloat)drawIconAtIndex:(size_t)index |
| 55 atX:(CGFloat)x |
| 56 rightAlign:(BOOL)rightAlign |
| 57 bounds:(NSRect)bounds; |
| 58 - (CGFloat)drawSubtext:(NSString*)subtext |
| 59 atX:(CGFloat)x |
| 60 rightAlign:(BOOL)rightAlign |
| 61 bounds:(NSRect)bounds; |
| 37 | 62 |
| 38 // Returns the icon for the row with the given |index|, or |nil| if there is | 63 // Returns the icon for the row with the given |index|, or |nil| if there is |
| 39 // none. | 64 // none. |
| 40 - (NSImage*)iconAtIndex:(size_t)index; | 65 - (NSImage*)iconAtIndex:(size_t)index; |
| 41 | 66 |
| 42 @end | 67 @end |
| 43 | 68 |
| 44 @implementation AutofillPopupViewCocoa | 69 @implementation AutofillPopupViewCocoa |
| 45 | 70 |
| 46 #pragma mark - | 71 #pragma mark - |
| (...skipping 25 matching lines...) Expand all Loading... |
| 72 | 97 |
| 73 for (size_t i = 0; i < controller_->names().size(); ++i) { | 98 for (size_t i = 0; i < controller_->names().size(); ++i) { |
| 74 // Skip rows outside of the dirty rect. | 99 // Skip rows outside of the dirty rect. |
| 75 NSRect rowBounds = | 100 NSRect rowBounds = |
| 76 NSRectFromCGRect(controller_->GetRowBounds(i).ToCGRect()); | 101 NSRectFromCGRect(controller_->GetRowBounds(i).ToCGRect()); |
| 77 if (!NSIntersectsRect(rowBounds, dirtyRect)) | 102 if (!NSIntersectsRect(rowBounds, dirtyRect)) |
| 78 continue; | 103 continue; |
| 79 | 104 |
| 80 if (controller_->identifiers()[i] == autofill::POPUP_ITEM_ID_SEPARATOR) { | 105 if (controller_->identifiers()[i] == autofill::POPUP_ITEM_ID_SEPARATOR) { |
| 81 [self drawSeparatorWithBounds:rowBounds]; | 106 [self drawSeparatorWithBounds:rowBounds]; |
| 82 } else { | 107 continue; |
| 83 NSString* name = SysUTF16ToNSString(controller_->names()[i]); | |
| 84 NSString* subtext = SysUTF16ToNSString(controller_->subtexts()[i]); | |
| 85 BOOL isSelected = static_cast<int>(i) == controller_->selected_line(); | |
| 86 [self drawSuggestionWithName:name | |
| 87 subtext:subtext | |
| 88 index:i | |
| 89 bounds:rowBounds | |
| 90 selected:isSelected]; | |
| 91 } | 108 } |
| 109 |
| 110 BOOL imageFirst = (controller_->identifiers()[i] == |
| 111 autofill::POPUP_ITEM_ID_MAC_ACCESS_CONTACTS); |
| 112 NSString* name = SysUTF16ToNSString(controller_->names()[i]); |
| 113 NSString* subtext = SysUTF16ToNSString(controller_->subtexts()[i]); |
| 114 BOOL isSelected = static_cast<int>(i) == controller_->selected_line(); |
| 115 [self drawSuggestionWithName:name |
| 116 subtext:subtext |
| 117 index:i |
| 118 bounds:rowBounds |
| 119 selected:isSelected |
| 120 imageFirst:imageFirst]; |
| 92 } | 121 } |
| 93 } | 122 } |
| 94 | 123 |
| 95 #pragma mark - | 124 #pragma mark - |
| 96 #pragma mark Public API: | 125 #pragma mark Public API: |
| 97 | 126 |
| 98 - (void)controllerDestroyed { | 127 - (void)controllerDestroyed { |
| 99 // Since the |controller_| either already has been destroyed or is about to | 128 // Since the |controller_| either already has been destroyed or is about to |
| 100 // be, about the only thing we can safely do with it is to null it out. | 129 // be, about the only thing we can safely do with it is to null it out. |
| 101 controller_ = NULL; | 130 controller_ = NULL; |
| 102 [super delegateDestroyed]; | 131 [super delegateDestroyed]; |
| 103 } | 132 } |
| 104 | 133 |
| 105 - (void)invalidateRow:(size_t)row { | 134 - (void)invalidateRow:(size_t)row { |
| 106 NSRect dirty_rect = | 135 NSRect dirty_rect = |
| 107 NSRectFromCGRect(controller_->GetRowBounds(row).ToCGRect()); | 136 NSRectFromCGRect(controller_->GetRowBounds(row).ToCGRect()); |
| 108 [self setNeedsDisplayInRect:dirty_rect]; | 137 [self setNeedsDisplayInRect:dirty_rect]; |
| 109 } | 138 } |
| 110 | 139 |
| 111 #pragma mark - | 140 #pragma mark - |
| 112 #pragma mark Private API: | 141 #pragma mark Private API: |
| 113 | 142 |
| 114 - (void)drawSuggestionWithName:(NSString*)name | 143 - (void)drawSuggestionWithName:(NSString*)name |
| 115 subtext:(NSString*)subtext | 144 subtext:(NSString*)subtext |
| 116 index:(size_t)index | 145 index:(size_t)index |
| 117 bounds:(NSRect)bounds | 146 bounds:(NSRect)bounds |
| 118 selected:(BOOL)isSelected { | 147 selected:(BOOL)isSelected |
| 148 imageFirst:(BOOL)imageFirst { |
| 119 // If this row is selected, highlight it. | 149 // If this row is selected, highlight it. |
| 120 if (isSelected) { | 150 if (isSelected) { |
| 121 [[self highlightColor] set]; | 151 [[self highlightColor] set]; |
| 122 [NSBezierPath fillRect:bounds]; | 152 [NSBezierPath fillRect:bounds]; |
| 123 } | 153 } |
| 124 | 154 |
| 125 BOOL isRTL = controller_->IsRTL(); | 155 BOOL isRTL = controller_->IsRTL(); |
| 126 | 156 |
| 157 // The X values of the left and right borders of the autofill widget. |
| 158 CGFloat leftX = NSMinX(bounds) + AutofillPopupView::kEndPadding; |
| 159 CGFloat rightX = NSMaxX(bounds) - AutofillPopupView::kEndPadding; |
| 160 |
| 161 // Draw left side if isRTL == NO, right side if isRTL == YES. |
| 162 CGFloat x = isRTL ? rightX : leftX; |
| 163 if (imageFirst) |
| 164 x = [self drawIconAtIndex:index atX:x rightAlign:isRTL bounds:bounds]; |
| 165 [self drawName:name atX:x index:index rightAlign:isRTL bounds:bounds]; |
| 166 |
| 167 // Draw right side if isRTL == NO, left side if isRTL == YES. |
| 168 x = isRTL ? leftX : rightX; |
| 169 if (!imageFirst) |
| 170 x = [self drawIconAtIndex:index atX:x rightAlign:!isRTL bounds:bounds]; |
| 171 [self drawSubtext:subtext atX:x rightAlign:!isRTL bounds:bounds]; |
| 172 } |
| 173 |
| 174 - (CGFloat)drawName:(NSString*)name |
| 175 atX:(CGFloat)x |
| 176 index:(size_t)index |
| 177 rightAlign:(BOOL)rightAlign |
| 178 bounds:(NSRect)bounds { |
| 127 NSColor* nameColor = | 179 NSColor* nameColor = |
| 128 controller_->IsWarning(index) ? [self warningColor] : [self nameColor]; | 180 controller_->IsWarning(index) ? [self warningColor] : [self nameColor]; |
| 129 NSDictionary* nameAttributes = | 181 NSDictionary* nameAttributes = |
| 130 [NSDictionary dictionaryWithObjectsAndKeys: | 182 [NSDictionary dictionaryWithObjectsAndKeys: |
| 131 controller_->GetNameFontListForRow(index).GetPrimaryFont(). | 183 controller_->GetNameFontListForRow(index).GetPrimaryFont(). |
| 132 GetNativeFont(), | 184 GetNativeFont(), |
| 133 NSFontAttributeName, nameColor, NSForegroundColorAttributeName, | 185 NSFontAttributeName, nameColor, NSForegroundColorAttributeName, |
| 134 nil]; | 186 nil]; |
| 135 NSSize nameSize = [name sizeWithAttributes:nameAttributes]; | 187 NSSize nameSize = [name sizeWithAttributes:nameAttributes]; |
| 136 CGFloat x = bounds.origin.x + | 188 x -= rightAlign ? nameSize.width : 0; |
| 137 (isRTL ? | |
| 138 bounds.size.width - AutofillPopupView::kEndPadding - nameSize.width : | |
| 139 AutofillPopupView::kEndPadding); | |
| 140 CGFloat y = bounds.origin.y + (bounds.size.height - nameSize.height) / 2; | 189 CGFloat y = bounds.origin.y + (bounds.size.height - nameSize.height) / 2; |
| 141 | 190 |
| 142 [name drawAtPoint:NSMakePoint(x, y) withAttributes:nameAttributes]; | 191 [name drawAtPoint:NSMakePoint(x, y) withAttributes:nameAttributes]; |
| 143 | 192 |
| 144 // The x-coordinate will be updated as each element is drawn. | 193 x += rightAlign ? 0 : nameSize.width; |
| 145 x = bounds.origin.x + | 194 return x; |
| 146 (isRTL ? | 195 } |
| 147 AutofillPopupView::kEndPadding : | |
| 148 bounds.size.width - AutofillPopupView::kEndPadding); | |
| 149 | 196 |
| 150 // Draw the Autofill icon, if one exists. | 197 - (CGFloat)drawIconAtIndex:(size_t)index |
| 198 atX:(CGFloat)x |
| 199 rightAlign:(BOOL)rightAlign |
| 200 bounds:(NSRect)bounds { |
| 151 NSImage* icon = [self iconAtIndex:index]; | 201 NSImage* icon = [self iconAtIndex:index]; |
| 152 if (icon) { | 202 if (!icon) |
| 153 NSSize iconSize = [icon size]; | 203 return x; |
| 154 x += isRTL ? 0 : -iconSize.width; | 204 NSSize iconSize = [icon size]; |
| 155 y = bounds.origin.y + (bounds.size.height - iconSize.height) / 2; | 205 x -= rightAlign ? iconSize.width : 0; |
| 206 CGFloat y = bounds.origin.y + (bounds.size.height - iconSize.height) / 2; |
| 156 [icon drawInRect:NSMakeRect(x, y, iconSize.width, iconSize.height) | 207 [icon drawInRect:NSMakeRect(x, y, iconSize.width, iconSize.height) |
| 157 fromRect:NSZeroRect | 208 fromRect:NSZeroRect |
| 158 operation:NSCompositeSourceOver | 209 operation:NSCompositeSourceOver |
| 159 fraction:1.0 | 210 fraction:1.0 |
| 160 respectFlipped:YES | 211 respectFlipped:YES |
| 161 hints:nil]; | 212 hints:nil]; |
| 162 | 213 |
| 163 x += isRTL ? | 214 x += rightAlign ? -AutofillPopupView::kIconPadding |
| 164 iconSize.width + AutofillPopupView::kIconPadding : | 215 : iconSize.width + AutofillPopupView::kIconPadding; |
| 165 -AutofillPopupView::kIconPadding; | 216 return x; |
| 166 } | 217 } |
| 167 | 218 |
| 168 // Draw the subtext. | 219 - (CGFloat)drawSubtext:(NSString*)subtext |
| 220 atX:(CGFloat)x |
| 221 rightAlign:(BOOL)rightAlign |
| 222 bounds:(NSRect)bounds { |
| 169 NSDictionary* subtextAttributes = | 223 NSDictionary* subtextAttributes = |
| 170 [NSDictionary dictionaryWithObjectsAndKeys: | 224 [NSDictionary dictionaryWithObjectsAndKeys: |
| 171 controller_->subtext_font_list().GetPrimaryFont().GetNativeFont(), | 225 controller_->subtext_font_list().GetPrimaryFont().GetNativeFont(), |
| 172 NSFontAttributeName, | 226 NSFontAttributeName, |
| 173 [self subtextColor], | 227 [self subtextColor], |
| 174 NSForegroundColorAttributeName, | 228 NSForegroundColorAttributeName, |
| 175 nil]; | 229 nil]; |
| 176 NSSize subtextSize = [subtext sizeWithAttributes:subtextAttributes]; | 230 NSSize subtextSize = [subtext sizeWithAttributes:subtextAttributes]; |
| 177 x += isRTL ? 0 : -subtextSize.width; | 231 x -= rightAlign ? subtextSize.width : 0; |
| 178 y = bounds.origin.y + (bounds.size.height - subtextSize.height) / 2; | 232 CGFloat y = bounds.origin.y + (bounds.size.height - subtextSize.height) / 2; |
| 179 | 233 |
| 180 [subtext drawAtPoint:NSMakePoint(x, y) withAttributes:subtextAttributes]; | 234 [subtext drawAtPoint:NSMakePoint(x, y) withAttributes:subtextAttributes]; |
| 235 x += rightAlign ? 0 : subtextSize.width; |
| 236 return x; |
| 181 } | 237 } |
| 182 | 238 |
| 183 - (NSImage*)iconAtIndex:(size_t)index { | 239 - (NSImage*)iconAtIndex:(size_t)index { |
| 184 if (controller_->icons()[index].empty()) | 240 if (controller_->icons()[index].empty()) |
| 185 return nil; | 241 return nil; |
| 186 | 242 |
| 187 int iconId = controller_->GetIconResourceID(controller_->icons()[index]); | 243 int iconId = controller_->GetIconResourceID(controller_->icons()[index]); |
| 188 DCHECK_NE(-1, iconId); | 244 DCHECK_NE(-1, iconId); |
| 189 | 245 |
| 190 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 246 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 191 return rb.GetNativeImageNamed(iconId).ToNSImage(); | 247 return rb.GetNativeImageNamed(iconId).ToNSImage(); |
| 192 } | 248 } |
| 193 | 249 |
| 194 @end | 250 @end |
| OLD | NEW |