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/autofill_popup_layout_model.h" |
10 #include "chrome/browser/ui/autofill/popup_constants.h" | 11 #include "chrome/browser/ui/autofill/popup_constants.h" |
11 #include "chrome/browser/ui/cocoa/autofill/autofill_popup_view_bridge.h" | 12 #include "chrome/browser/ui/cocoa/autofill/autofill_popup_view_bridge.h" |
12 #include "components/autofill/core/browser/popup_item_ids.h" | 13 #include "components/autofill/core/browser/popup_item_ids.h" |
13 #include "components/autofill/core/browser/suggestion.h" | 14 #include "components/autofill/core/browser/suggestion.h" |
14 #include "ui/base/cocoa/window_size_constants.h" | 15 #include "ui/base/cocoa/window_size_constants.h" |
15 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
16 #include "ui/gfx/font_list.h" | 17 #include "ui/gfx/font_list.h" |
17 #include "ui/gfx/geometry/point.h" | 18 #include "ui/gfx/geometry/point.h" |
18 #include "ui/gfx/geometry/rect.h" | 19 #include "ui/gfx/geometry/rect.h" |
19 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
20 | 21 |
21 using autofill::AutofillPopupView; | 22 using autofill::AutofillPopupView; |
| 23 using autofill::AutofillPopupLayoutModel; |
22 | 24 |
23 @interface AutofillPopupViewCocoa () | 25 @interface AutofillPopupViewCocoa () |
24 | 26 |
25 #pragma mark - | 27 #pragma mark - |
26 #pragma mark Private methods | 28 #pragma mark Private methods |
27 | 29 |
28 // Draws an Autofill suggestion in the given |bounds|, labeled with the given | 30 // 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 | 31 // |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, | 32 // 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. | 33 // if the row requires it -- such as for credit cards. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 67 |
66 @end | 68 @end |
67 | 69 |
68 @implementation AutofillPopupViewCocoa | 70 @implementation AutofillPopupViewCocoa |
69 | 71 |
70 #pragma mark - | 72 #pragma mark - |
71 #pragma mark Initialisers | 73 #pragma mark Initialisers |
72 | 74 |
73 - (id)initWithFrame:(NSRect)frame { | 75 - (id)initWithFrame:(NSRect)frame { |
74 NOTREACHED(); | 76 NOTREACHED(); |
75 return [self initWithController:NULL frame:frame]; | 77 return [self initWithController:NULL frame:frame delegate:NULL]; |
76 } | 78 } |
77 | 79 |
78 - (id)initWithController:(autofill::AutofillPopupController*)controller | 80 - (id)initWithController:(autofill::AutofillPopupController*)controller |
79 frame:(NSRect)frame { | 81 frame:(NSRect)frame |
| 82 delegate:(autofill::AutofillPopupViewCocoaDelegate*)delegate { |
80 self = [super initWithDelegate:controller frame:frame]; | 83 self = [super initWithDelegate:controller frame:frame]; |
81 if (self) | 84 if (self) { |
82 controller_ = controller; | 85 controller_ = controller; |
| 86 delegate_ = delegate; |
| 87 } |
83 | 88 |
84 return self; | 89 return self; |
85 } | 90 } |
86 | 91 |
87 #pragma mark - | 92 #pragma mark - |
88 #pragma mark NSView implementation: | 93 #pragma mark NSView implementation: |
89 | 94 |
90 - (void)drawRect:(NSRect)dirtyRect { | 95 - (void)drawRect:(NSRect)dirtyRect { |
91 // If the view is in the process of being destroyed, don't bother drawing. | 96 // If the view is in the process of being destroyed, don't bother drawing. |
92 if (!controller_) | 97 if (!controller_) |
93 return; | 98 return; |
94 | 99 |
95 [self drawBackgroundAndBorder]; | 100 [self drawBackgroundAndBorder]; |
96 | 101 |
97 for (size_t i = 0; i < controller_->GetLineCount(); ++i) { | 102 for (size_t i = 0; i < controller_->GetLineCount(); ++i) { |
98 // Skip rows outside of the dirty rect. | 103 // Skip rows outside of the dirty rect. |
99 NSRect rowBounds = | 104 NSRect rowBounds = NSRectFromCGRect(delegate_->GetRowBounds(i).ToCGRect()); |
100 NSRectFromCGRect(controller_->GetRowBounds(i).ToCGRect()); | |
101 if (!NSIntersectsRect(rowBounds, dirtyRect)) | 105 if (!NSIntersectsRect(rowBounds, dirtyRect)) |
102 continue; | 106 continue; |
103 const autofill::Suggestion& suggestion = controller_->GetSuggestionAt(i); | 107 const autofill::Suggestion& suggestion = controller_->GetSuggestionAt(i); |
104 | 108 |
105 if (suggestion.frontend_id == autofill::POPUP_ITEM_ID_SEPARATOR) { | 109 if (suggestion.frontend_id == autofill::POPUP_ITEM_ID_SEPARATOR) { |
106 [self drawSeparatorWithBounds:rowBounds]; | 110 [self drawSeparatorWithBounds:rowBounds]; |
107 continue; | 111 continue; |
108 } | 112 } |
109 | 113 |
110 // Additional offset applied to the text in the vertical direction. | 114 // Additional offset applied to the text in the vertical direction. |
(...skipping 15 matching lines...) Expand all Loading... |
126 #pragma mark Public API: | 130 #pragma mark Public API: |
127 | 131 |
128 - (void)controllerDestroyed { | 132 - (void)controllerDestroyed { |
129 // Since the |controller_| either already has been destroyed or is about to | 133 // Since the |controller_| either already has been destroyed or is about to |
130 // be, about the only thing we can safely do with it is to null it out. | 134 // be, about the only thing we can safely do with it is to null it out. |
131 controller_ = NULL; | 135 controller_ = NULL; |
132 [super delegateDestroyed]; | 136 [super delegateDestroyed]; |
133 } | 137 } |
134 | 138 |
135 - (void)invalidateRow:(size_t)row { | 139 - (void)invalidateRow:(size_t)row { |
136 NSRect dirty_rect = | 140 NSRect dirty_rect = NSRectFromCGRect(delegate_->GetRowBounds(row).ToCGRect()); |
137 NSRectFromCGRect(controller_->GetRowBounds(row).ToCGRect()); | |
138 [self setNeedsDisplayInRect:dirty_rect]; | 141 [self setNeedsDisplayInRect:dirty_rect]; |
139 } | 142 } |
140 | 143 |
141 #pragma mark - | 144 #pragma mark - |
142 #pragma mark Private API: | 145 #pragma mark Private API: |
143 | 146 |
144 - (void)drawSuggestionWithName:(NSString*)name | 147 - (void)drawSuggestionWithName:(NSString*)name |
145 subtext:(NSString*)subtext | 148 subtext:(NSString*)subtext |
146 index:(size_t)index | 149 index:(size_t)index |
147 bounds:(NSRect)bounds | 150 bounds:(NSRect)bounds |
148 selected:(BOOL)isSelected | 151 selected:(BOOL)isSelected |
149 textYOffset:(CGFloat)textYOffset { | 152 textYOffset:(CGFloat)textYOffset { |
150 // If this row is selected, highlight it. | 153 // If this row is selected, highlight it. |
151 if (isSelected) { | 154 if (isSelected) { |
152 [[self highlightColor] set]; | 155 [[self highlightColor] set]; |
153 [NSBezierPath fillRect:bounds]; | 156 [NSBezierPath fillRect:bounds]; |
154 } | 157 } |
155 | 158 |
156 BOOL isRTL = controller_->IsRTL(); | 159 BOOL isRTL = controller_->IsRTL(); |
157 | 160 |
158 // The X values of the left and right borders of the autofill widget. | 161 // The X values of the left and right borders of the autofill widget. |
159 CGFloat leftX = NSMinX(bounds) + AutofillPopupView::kEndPadding; | 162 CGFloat leftX = NSMinX(bounds) + AutofillPopupLayoutModel::kEndPadding; |
160 CGFloat rightX = NSMaxX(bounds) - AutofillPopupView::kEndPadding; | 163 CGFloat rightX = NSMaxX(bounds) - AutofillPopupLayoutModel::kEndPadding; |
161 | 164 |
162 // Draw left side if isRTL == NO, right side if isRTL == YES. | 165 // Draw left side if isRTL == NO, right side if isRTL == YES. |
163 CGFloat x = isRTL ? rightX : leftX; | 166 CGFloat x = isRTL ? rightX : leftX; |
164 [self drawName:name | 167 [self drawName:name |
165 atX:x | 168 atX:x |
166 index:index | 169 index:index |
167 rightAlign:isRTL | 170 rightAlign:isRTL |
168 bounds:bounds | 171 bounds:bounds |
169 textYOffset:textYOffset]; | 172 textYOffset:textYOffset]; |
170 | 173 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 NSSize iconSize = [icon size]; | 216 NSSize iconSize = [icon size]; |
214 x -= rightAlign ? iconSize.width : 0; | 217 x -= rightAlign ? iconSize.width : 0; |
215 CGFloat y = bounds.origin.y + (bounds.size.height - iconSize.height) / 2; | 218 CGFloat y = bounds.origin.y + (bounds.size.height - iconSize.height) / 2; |
216 [icon drawInRect:NSMakeRect(x, y, iconSize.width, iconSize.height) | 219 [icon drawInRect:NSMakeRect(x, y, iconSize.width, iconSize.height) |
217 fromRect:NSZeroRect | 220 fromRect:NSZeroRect |
218 operation:NSCompositeSourceOver | 221 operation:NSCompositeSourceOver |
219 fraction:1.0 | 222 fraction:1.0 |
220 respectFlipped:YES | 223 respectFlipped:YES |
221 hints:nil]; | 224 hints:nil]; |
222 | 225 |
223 x += rightAlign ? -AutofillPopupView::kIconPadding | 226 x += rightAlign ? -AutofillPopupLayoutModel::kIconPadding |
224 : iconSize.width + AutofillPopupView::kIconPadding; | 227 : iconSize.width + AutofillPopupLayoutModel::kIconPadding; |
225 return x; | 228 return x; |
226 } | 229 } |
227 | 230 |
228 - (CGFloat)drawSubtext:(NSString*)subtext | 231 - (CGFloat)drawSubtext:(NSString*)subtext |
229 atX:(CGFloat)x | 232 atX:(CGFloat)x |
230 rightAlign:(BOOL)rightAlign | 233 rightAlign:(BOOL)rightAlign |
231 bounds:(NSRect)bounds | 234 bounds:(NSRect)bounds |
232 textYOffset:(CGFloat)textYOffset { | 235 textYOffset:(CGFloat)textYOffset { |
233 NSDictionary* subtextAttributes = | 236 NSDictionary* subtextAttributes = |
234 [NSDictionary dictionaryWithObjectsAndKeys: | 237 [NSDictionary dictionaryWithObjectsAndKeys: |
(...skipping 10 matching lines...) Expand all Loading... |
245 [subtext drawAtPoint:NSMakePoint(x, y) withAttributes:subtextAttributes]; | 248 [subtext drawAtPoint:NSMakePoint(x, y) withAttributes:subtextAttributes]; |
246 x += rightAlign ? 0 : subtextSize.width; | 249 x += rightAlign ? 0 : subtextSize.width; |
247 return x; | 250 return x; |
248 } | 251 } |
249 | 252 |
250 - (NSImage*)iconAtIndex:(size_t)index { | 253 - (NSImage*)iconAtIndex:(size_t)index { |
251 const base::string16& icon = controller_->GetSuggestionAt(index).icon; | 254 const base::string16& icon = controller_->GetSuggestionAt(index).icon; |
252 if (icon.empty()) | 255 if (icon.empty()) |
253 return nil; | 256 return nil; |
254 | 257 |
255 int iconId = controller_->GetIconResourceID(icon); | 258 int iconId = delegate_->GetIconResourceID(icon); |
256 DCHECK_NE(-1, iconId); | 259 DCHECK_NE(-1, iconId); |
257 | 260 |
258 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 261 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
259 return rb.GetNativeImageNamed(iconId).ToNSImage(); | 262 return rb.GetNativeImageNamed(iconId).ToNSImage(); |
260 } | 263 } |
261 | 264 |
262 @end | 265 @end |
OLD | NEW |