Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: chrome/browser/ui/cocoa/autofill/autofill_popup_view_cocoa.mm

Issue 11740033: [Autofill] Add Mac implementation for the in-browser process popup view. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Calibrated colors, fewer defaults Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #import "chrome/browser/ui/cocoa/autofill/autofill_popup_view_cocoa.h"
6
7 #include "base/i18n/rtl.h"
8 #include "base/logging.h"
9 #include "base/sys_string_conversions.h"
10 #include "chrome/browser/ui/autofill/autofill_popup_controller.h"
11 #include "chrome/browser/ui/cocoa/autofill/autofill_popup_view_mac.h"
12 #include "grit/ui_resources.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/gfx/image/image.h"
16 #include "ui/gfx/point.h"
17 #include "ui/gfx/rect.h"
18
19 #pragma mark -
20 #pragma mark Private methods
21
22 @interface AutofillPopupViewCocoa ()
23
24 // Draws a thin separator in the popup UI.
25 - (void)drawSeparatorWithBounds:(NSRect)bounds;
26
27 // Draws an Autofill suggestion in the given |bounds|, labeled with the given
28 // |name| and |subtext| hint. If the suggestion |isSelected|, then it is drawn
29 // with a highlight. Some suggestions -- such as for credit cards -- might also
30 // include an |icon| -- e.g. for the card type. Finally, if |canDelete| is
31 // true, a delete icon is also drawn.
32 - (void)drawSuggestionWithName:(NSString*)name
33 subtext:(NSString*)subtext
34 icon:(NSImage*)icon
35 bounds:(NSRect)bounds
36 selected:(BOOL)isSelected
37 canDelete:(BOOL)canDelete;
38
39 // Returns the icon for the row with the given |index|, or |nil| if there is
40 // none.
41 - (NSImage*)iconAtIndex:(size_t)index;
42
43 // Flips the given |point|'s y coordinate to match Chrome's screen coordinate
44 // system.
45 - (gfx::Point)flipNSPointToPoint:(NSPoint)point;
46
47 @end
48
49 @implementation AutofillPopupViewCocoa
50
51 #pragma mark -
52 #pragma mark Initialisers
53
54 - (id)initWithFrame:(NSRect)frame {
55 NOTREACHED();
56 return [self initWithController:NULL frame:frame];
57 }
58
59 - (id)initWithController:(AutofillPopupController*)controller
60 frame:(NSRect)frame {
61 self = [super initWithFrame:frame];
62 if (self)
63 controller_ = controller;
64
65 return self;
66 }
67
68 #pragma mark -
69 #pragma mark NSView implementation:
70
71 // A slight optimization for drawing:
72 // https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Cocoa ViewsGuide/Optimizing/Optimizing.html
73 - (BOOL)isOpaque {
74 return YES;
75 }
76
77 - (void)drawRect:(NSRect)dirtyRect {
78 // If the view is in the process of being destroyed, don't bother drawing.
79 if (!controller_)
80 return;
81
82 // TODO(isherman): Is there a better way to leave room for the border?
83 // TODO(isherman): Drawing the border as part of the content view means that
84 // the rest of the content has to be careful not to overlap the border.
85 // Should the border be part of the window instead? If not, should the rest
86 // of the view be a subview? Or should I just draw the window content
87 // carefully?
88 NSRect borderRect = NSInsetRect([self bounds], 0.5, 0.5);
89 NSBezierPath* border = [NSBezierPath bezierPathWithRect:borderRect];
90
91 [[NSColor whiteColor] set];
92 [border fill];
93
94 // TODO(isherman): This color does not match the other platforms, but it
95 // matches what the existing UI on Mac has as the color. The other platforms
96 // have a strange color: the RGB values are almost, but not quite, identical
97 // to each other.
98 // TODO(isherman): Maybe use a system color for this?
99 [[NSColor colorWithCalibratedRed:127/255.0
100 green:157/255.0
101 blue:185/255.0
102 alpha:1.0] set];
103 [border stroke];
104
105 for (size_t i = 0; i < controller_->names().size(); ++i) {
106 // Skip rows outside of the dirty rect.
107 NSRect rowBounds = [self flipRectToNSRect:controller_->GetRowBounds(i)];
108 if (!NSIntersectsRect(rowBounds, dirtyRect))
109 continue;
110
111 if (controller_->identifiers()[i] ==
112 WebKit::WebAutofillClient::MenuItemIDSeparator) {
113 [self drawSeparatorWithBounds:rowBounds];
114 } else {
115 NSString* name = SysUTF16ToNSString(controller_->names()[i]);
116 NSString* subtext = SysUTF16ToNSString(controller_->subtexts()[i]);
117 BOOL isSelected = static_cast<int>(i) == controller_->selected_line();
118 [self drawSuggestionWithName:name
119 subtext:subtext
120 icon:[self iconAtIndex:i]
121 bounds:rowBounds
122 selected:isSelected
123 canDelete:controller_->CanDelete(i)];
124 }
125 }
126 }
127
128 #pragma mark -
129 #pragma mark BaseView implementation:
130
131 - (void)mouseEvent:(NSEvent *)theEvent {
132 // If the view is in the process of being destroyed, abort.
133 if (!controller_)
134 return;
135
136 NSEventType event_type = [theEvent type];
Scott Hess - ex-Googler 2013/01/09 21:36:17 It seems kind of roundabout to override -mouseUp:,
Ilya Sherman 2013/01/10 02:21:21 Done. I was mostly just following the BaseView cl
Scott Hess - ex-Googler 2013/01/10 22:02:57 I think BaseView might do that for purposes of sub
137 NSPoint location = [self convertPoint:[theEvent locationInWindow]
138 fromView:nil];
139
140 // Convert to Chrome's screen coordinates.
Scott Hess - ex-Googler 2013/01/09 21:36:17 I don't understand what "screen coordinates" means
Ilya Sherman 2013/01/10 02:21:21 Moot.
141 gfx::Point screen_location = [self flipNSPointToPoint:location];
142
143 if (event_type == NSLeftMouseUp && NSPointInRect(location, [self bounds]))
144 controller_->MouseClicked(screen_location.x(), screen_location.y());
145 else if (event_type == NSMouseExited)
146 controller_->MouseExitedPopup();
147 else if (event_type == NSMouseMoved || event_type == NSLeftMouseDragged)
148 controller_->MouseHovered(screen_location.x(), screen_location.y());
149 }
150
151 #pragma mark -
152 #pragma mark Public API:
153
154 - (void)controllerDestroyed {
Scott Hess - ex-Googler 2013/01/09 21:36:17 This is where I meant to drop a comment. When thi
Ilya Sherman 2013/01/10 02:21:21 Done.
155 controller_ = NULL;
156 }
157
158 #pragma mark -
159 #pragma mark Private API:
160
161 - (void)drawSeparatorWithBounds:(NSRect)bounds {
162 [[NSColor colorWithCalibratedWhite:220/255.0 alpha:1] set];
163 [NSBezierPath fillRect:bounds];
164 }
165
166 - (void)drawSuggestionWithName:(NSString*)name
167 subtext:(NSString*)subtext
168 icon:(NSImage*)icon
169 bounds:(NSRect)bounds
170 selected:(BOOL)isSelected
171 canDelete:(BOOL)canDelete {
172 // If this row is selected, highlight it.
173 if (isSelected) {
174 // TODO(isherman): The highlight color should match the system highlight
175 // color. Maybe use controlHighlightColor or selectedTextBackgroundColor
176 // for this?
177 [[NSColor colorWithCalibratedWhite:(0xCD / 255.0) alpha:1] set];
178 [NSBezierPath fillRect:bounds];
179 }
180
181 BOOL isRTL = base::i18n::IsRTL();
182
183 // TODO(isherman): Set font, colors, and any other appropriate attributes.
184 NSSize nameSize = [name sizeWithAttributes:nil];
185 CGFloat x = bounds.origin.x +
186 (isRTL ?
187 bounds.size.width - AutofillPopupView::kEndPadding - nameSize.width:
188 AutofillPopupView::kEndPadding);
189 CGFloat y = bounds.origin.y + (bounds.size.height - nameSize.height) / 2;
190
191 [name drawAtPoint:NSMakePoint(x, y) withAttributes:nil];
192
193 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
194
195 // The x-coordinate will be updated as each element is drawn.
196 x = bounds.origin.x +
197 (isRTL ?
198 AutofillPopupView::kEndPadding :
199 bounds.size.width - AutofillPopupView::kEndPadding);
200
201 // Draw the delete icon, if one is needed.
202 if (canDelete) {
203 // TODO(csharp): Create a custom resource for the delete icon.
204 // http://crbug.com/131801
205 NSImage* deleteIcon;
206 if (isSelected && controller_->delete_icon_hovered())
207 deleteIcon = rb.GetImageNamed(IDR_CLOSE_BAR_H).ToNSImage();
Robert Sesek 2013/01/09 22:17:30 drive-by: use GetNativeImageNamed to avoid an Skia
Ilya Sherman 2013/01/10 02:21:21 Thanks, done.
208 else
209 deleteIcon = rb.GetImageNamed(IDR_CLOSE_BAR).ToNSImage();
210
211 NSSize iconSize = [deleteIcon size];
212 x += isRTL ? 0 : -iconSize.width;
213 y = bounds.origin.y + (bounds.size.height - iconSize.height) / 2;
214 [deleteIcon drawAtPoint:NSMakePoint(x, y)
215 fromRect:NSZeroRect
216 operation:NSCompositeSourceOver
217 fraction:1.0];
218
219 x += isRTL ?
220 iconSize.width + AutofillPopupView::kIconPadding :
221 -AutofillPopupView::kIconPadding;
222 }
223
224 // Draw the Autofill icon, if one exists.
225 if (icon) {
226 NSSize iconSize = [icon size];
227 x += isRTL ? 0 : -iconSize.width;
228 y = bounds.origin.y + (bounds.size.height - iconSize.height) / 2;
229 [icon drawAtPoint:NSMakePoint(x, y)
230 fromRect:NSZeroRect
231 operation:NSCompositeSourceOver
232 fraction:1.0];
233
234 x += isRTL ?
235 iconSize.width + AutofillPopupView::kIconPadding :
236 -AutofillPopupView::kIconPadding;
237 }
238
239 // Draw the subtext.
240 NSSize subtextSize = [subtext sizeWithAttributes:nil];
241 x += isRTL ? 0 : -subtextSize.width;
242 y = bounds.origin.y + (bounds.size.height - subtextSize.height) / 2;
243
244 [subtext drawAtPoint:NSMakePoint(x, y) withAttributes:nil];
245 }
246
247 - (NSImage*)iconAtIndex:(size_t)index {
248 if (controller_->icons()[index].empty())
249 return nil;
250
251 int iconId = controller_->GetIconResourceID(controller_->icons()[index]);
252 DCHECK_NE(-1, iconId);
253 return
254 ui::ResourceBundle::GetSharedInstance().GetImageNamed(iconId).ToNSImage();
255 }
256
257 - (gfx::Point)flipNSPointToPoint:(NSPoint)point {
258 NSRect rect;
259 rect.origin = point;
260 rect.size = NSZeroSize;
Scott Hess - ex-Googler 2013/01/09 21:36:17 NSRect rect = {point, NSZeroSize}; should work. Or
Ilya Sherman 2013/01/10 02:21:21 Moot.
261 return [self flipNSRectToRect:rect].origin();
262 }
263
264 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698