Chromium Code Reviews| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 NSColor* WarningColor() { | 45 NSColor* WarningColor() { |
| 46 return [NSColor grayColor]; | 46 return [NSColor grayColor]; |
| 47 } | 47 } |
| 48 | 48 |
| 49 NSColor* SubtextColor() { | 49 NSColor* SubtextColor() { |
| 50 return [NSColor grayColor]; | 50 return [NSColor grayColor]; |
| 51 } | 51 } |
| 52 | 52 |
| 53 } // namespace | 53 } // namespace |
| 54 | 54 |
| 55 @implementation AutofillPopupBaseViewCocoa | |
|
Patrick Dubroy
2014/05/06 15:43:52
I will move this into a separate file -- it's just
| |
| 56 - (id)initWithAutofillPopupViewDelegate:(autofill::AutofillPopupViewDelegate*)de legate | |
| 57 frame:(NSRect)frame { | |
| 58 self = [super initWithFrame:frame]; | |
| 59 if (self) | |
| 60 delegate_ = delegate; | |
| 61 | |
| 62 return self; | |
| 63 } | |
| 64 | |
| 65 // Informs the view that its delegate has been (or will imminently be) | |
| 66 // destroyed. | |
| 67 // TODO: What do we need this for? | |
| 68 - (void)delegateDestroyed { | |
| 69 delegate_ = NULL; | |
| 70 } | |
| 71 | |
| 72 - (void)drawSeparatorWithBounds:(NSRect)bounds { | |
| 73 [SeparatorColor() set]; | |
| 74 [NSBezierPath fillRect:bounds]; | |
| 75 } | |
| 76 | |
| 77 // A slight optimization for drawing: | |
| 78 // https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Cocoa ViewsGuide/Optimizing/Optimizing.html | |
| 79 - (BOOL)isOpaque { | |
| 80 return YES; | |
| 81 } | |
| 82 | |
| 83 - (BOOL)isFlipped { | |
| 84 // Flipped so that it's easier to share controller logic with other OSes. | |
| 85 return YES; | |
| 86 } | |
| 87 | |
| 88 - (void)drawBackgroundAndBorderInRect:(NSRect)dirtyRect { | |
| 89 // The inset is needed since the border is centered on the |path|. | |
| 90 // TODO(isherman): We should consider using asset-based drawing for the | |
| 91 // border, creating simple bitmaps for the view's border and background, and | |
| 92 // drawing them using NSDrawNinePartImage(). | |
| 93 CGFloat inset = autofill::kPopupBorderThickness / 2.0; | |
| 94 NSRect borderRect = NSInsetRect([self bounds], inset, inset); | |
| 95 NSBezierPath* path = [NSBezierPath bezierPathWithRect:borderRect]; | |
| 96 [BackgroundColor() setFill]; | |
| 97 [path fill]; | |
| 98 [path setLineWidth:autofill::kPopupBorderThickness]; | |
| 99 [BorderColor() setStroke]; | |
| 100 [path stroke]; | |
| 101 } | |
| 102 | |
| 103 @end | |
| 104 | |
| 55 #pragma mark - | 105 #pragma mark - |
| 56 #pragma mark Private methods | 106 #pragma mark Private methods |
| 57 | 107 |
| 58 @interface AutofillPopupViewCocoa () | 108 @interface AutofillPopupViewCocoa () |
| 59 | 109 |
| 60 // Draws a thin separator in the popup UI. | |
| 61 - (void)drawSeparatorWithBounds:(NSRect)bounds; | |
| 62 | |
| 63 // Draws an Autofill suggestion in the given |bounds|, labeled with the given | 110 // Draws an Autofill suggestion in the given |bounds|, labeled with the given |
| 64 // |name| and |subtext| hint. If the suggestion |isSelected|, then it is drawn | 111 // |name| and |subtext| hint. If the suggestion |isSelected|, then it is drawn |
| 65 // with a highlight. |index| determines the font to use, as well as the icon, | 112 // with a highlight. |index| determines the font to use, as well as the icon, |
| 66 // if the row requires it -- such as for credit cards. | 113 // if the row requires it -- such as for credit cards. |
| 67 - (void)drawSuggestionWithName:(NSString*)name | 114 - (void)drawSuggestionWithName:(NSString*)name |
| 68 subtext:(NSString*)subtext | 115 subtext:(NSString*)subtext |
| 69 index:(size_t)index | 116 index:(size_t)index |
| 70 bounds:(NSRect)bounds | 117 bounds:(NSRect)bounds |
| 71 selected:(BOOL)isSelected; | 118 selected:(BOOL)isSelected; |
| 72 | 119 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 91 self = [super initWithFrame:frame]; | 138 self = [super initWithFrame:frame]; |
| 92 if (self) | 139 if (self) |
| 93 controller_ = controller; | 140 controller_ = controller; |
| 94 | 141 |
| 95 return self; | 142 return self; |
| 96 } | 143 } |
| 97 | 144 |
| 98 #pragma mark - | 145 #pragma mark - |
| 99 #pragma mark NSView implementation: | 146 #pragma mark NSView implementation: |
| 100 | 147 |
| 101 // A slight optimization for drawing: | |
| 102 // https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Cocoa ViewsGuide/Optimizing/Optimizing.html | |
| 103 - (BOOL)isOpaque { | |
| 104 return YES; | |
| 105 } | |
| 106 | |
| 107 - (BOOL)isFlipped { | |
| 108 // Flipped so that it's easier to share controller logic with other OSes. | |
| 109 return YES; | |
| 110 } | |
| 111 | |
| 112 - (void)drawRect:(NSRect)dirtyRect { | 148 - (void)drawRect:(NSRect)dirtyRect { |
| 113 // If the view is in the process of being destroyed, don't bother drawing. | 149 // If the view is in the process of being destroyed, don't bother drawing. |
| 114 if (!controller_) | 150 if (!controller_) |
| 115 return; | 151 return; |
| 116 | 152 |
| 117 // Draw the popup's background and border. | 153 [self drawBackgroundAndBorderInRect:dirtyRect]; |
| 118 // The inset is needed since the border is centered on the |path|. | |
| 119 // TODO(isherman): We should consider using asset-based drawing for the | |
| 120 // border, creating simple bitmaps for the view's border and background, and | |
| 121 // drawing them using NSDrawNinePartImage(). | |
| 122 CGFloat inset = autofill::kPopupBorderThickness / 2.0; | |
| 123 NSRect borderRect = NSInsetRect([self bounds], inset, inset); | |
| 124 NSBezierPath* path = [NSBezierPath bezierPathWithRect:borderRect]; | |
| 125 [BackgroundColor() setFill]; | |
| 126 [path fill]; | |
| 127 [path setLineWidth:autofill::kPopupBorderThickness]; | |
| 128 [BorderColor() setStroke]; | |
| 129 [path stroke]; | |
| 130 | 154 |
| 131 for (size_t i = 0; i < controller_->names().size(); ++i) { | 155 for (size_t i = 0; i < controller_->names().size(); ++i) { |
| 132 // Skip rows outside of the dirty rect. | 156 // Skip rows outside of the dirty rect. |
| 133 NSRect rowBounds = | 157 NSRect rowBounds = |
| 134 NSRectFromCGRect(controller_->GetRowBounds(i).ToCGRect()); | 158 NSRectFromCGRect(controller_->GetRowBounds(i).ToCGRect()); |
| 135 if (!NSIntersectsRect(rowBounds, dirtyRect)) | 159 if (!NSIntersectsRect(rowBounds, dirtyRect)) |
| 136 continue; | 160 continue; |
| 137 | 161 |
| 138 if (controller_->identifiers()[i] == autofill::POPUP_ITEM_ID_SEPARATOR) { | 162 if (controller_->identifiers()[i] == autofill::POPUP_ITEM_ID_SEPARATOR) { |
| 139 [self drawSeparatorWithBounds:rowBounds]; | 163 [self drawSeparatorWithBounds:rowBounds]; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 | 216 |
| 193 - (void)controllerDestroyed { | 217 - (void)controllerDestroyed { |
| 194 // Since the |controller_| either already has been destroyed or is about to | 218 // Since the |controller_| either already has been destroyed or is about to |
| 195 // be, about the only thing we can safely do with it is to null it out. | 219 // be, about the only thing we can safely do with it is to null it out. |
| 196 controller_ = NULL; | 220 controller_ = NULL; |
| 197 } | 221 } |
| 198 | 222 |
| 199 #pragma mark - | 223 #pragma mark - |
| 200 #pragma mark Private API: | 224 #pragma mark Private API: |
| 201 | 225 |
| 202 - (void)drawSeparatorWithBounds:(NSRect)bounds { | |
| 203 [SeparatorColor() set]; | |
| 204 [NSBezierPath fillRect:bounds]; | |
| 205 } | |
| 206 | |
| 207 - (void)drawSuggestionWithName:(NSString*)name | 226 - (void)drawSuggestionWithName:(NSString*)name |
| 208 subtext:(NSString*)subtext | 227 subtext:(NSString*)subtext |
| 209 index:(size_t)index | 228 index:(size_t)index |
| 210 bounds:(NSRect)bounds | 229 bounds:(NSRect)bounds |
| 211 selected:(BOOL)isSelected { | 230 selected:(BOOL)isSelected { |
| 212 // If this row is selected, highlight it. | 231 // If this row is selected, highlight it. |
| 213 if (isSelected) { | 232 if (isSelected) { |
| 214 [HighlightColor() set]; | 233 [HighlightColor() set]; |
| 215 [NSBezierPath fillRect:bounds]; | 234 [NSBezierPath fillRect:bounds]; |
| 216 } | 235 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 return nil; | 295 return nil; |
| 277 | 296 |
| 278 int iconId = controller_->GetIconResourceID(controller_->icons()[index]); | 297 int iconId = controller_->GetIconResourceID(controller_->icons()[index]); |
| 279 DCHECK_NE(-1, iconId); | 298 DCHECK_NE(-1, iconId); |
| 280 | 299 |
| 281 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 300 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 282 return rb.GetNativeImageNamed(iconId).ToNSImage(); | 301 return rb.GetNativeImageNamed(iconId).ToNSImage(); |
| 283 } | 302 } |
| 284 | 303 |
| 285 @end | 304 @end |
| OLD | NEW |