OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/password_generation_popup_view_cocoa.h " | 5 #import "chrome/browser/ui/cocoa/autofill/password_generation_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_view.h" | 10 #include "chrome/browser/ui/autofill/autofill_popup_view.h" |
11 #include "chrome/browser/ui/autofill/popup_constants.h" | 11 #include "chrome/browser/ui/autofill/popup_constants.h" |
12 #include "chrome/browser/ui/cocoa/autofill/password_generation_popup_view_bridge .h" | 12 #include "chrome/browser/ui/cocoa/autofill/password_generation_popup_view_bridge .h" |
13 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h" | |
13 #import "chrome/browser/ui/cocoa/l10n_util.h" | 14 #import "chrome/browser/ui/cocoa/l10n_util.h" |
14 #include "components/autofill/core/browser/popup_item_ids.h" | 15 #include "components/autofill/core/browser/popup_item_ids.h" |
15 #include "grit/ui_resources.h" | 16 #include "grit/ui_resources.h" |
16 #include "skia/ext/skia_utils_mac.h" | 17 #include "skia/ext/skia_utils_mac.h" |
17 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
18 #include "ui/gfx/font_list.h" | 19 #include "ui/gfx/font_list.h" |
19 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
20 #include "ui/gfx/point.h" | 21 #include "ui/gfx/point.h" |
22 #include "ui/gfx/range/range.h" | |
21 #include "ui/gfx/rect.h" | 23 #include "ui/gfx/rect.h" |
22 #include "ui/gfx/text_constants.h" | 24 #include "ui/gfx/text_constants.h" |
23 | 25 |
24 using autofill::AutofillPopupView; | 26 using autofill::AutofillPopupView; |
25 using autofill::PasswordGenerationPopupView; | 27 using autofill::PasswordGenerationPopupView; |
26 using base::scoped_nsobject; | 28 using base::scoped_nsobject; |
27 | 29 |
28 namespace { | 30 namespace { |
29 | 31 |
30 NSColor* DividerColor() { | 32 NSColor* DividerColor() { |
(...skipping 20 matching lines...) Expand all Loading... | |
51 - (id)initWithFrame:(NSRect)frame { | 53 - (id)initWithFrame:(NSRect)frame { |
52 NOTREACHED(); | 54 NOTREACHED(); |
53 return nil; | 55 return nil; |
54 } | 56 } |
55 | 57 |
56 - (id)initWithController: | 58 - (id)initWithController: |
57 (autofill::PasswordGenerationPopupController*)controller | 59 (autofill::PasswordGenerationPopupController*)controller |
58 frame:(NSRect)frame { | 60 frame:(NSRect)frame { |
59 if (self = [super initWithDelegate:controller frame:frame]) { | 61 if (self = [super initWithDelegate:controller frame:frame]) { |
60 controller_ = controller; | 62 controller_ = controller; |
61 NSFont* font = controller_->font_list().GetPrimaryFont().GetNativeFont(); | |
62 | 63 |
63 passwordField_ = [self textFieldWithText:controller_->password() | 64 passwordView_ = [self textViewWithText:controller_->password() |
groby-ooo-7-16
2014/05/30 18:05:01
I'm not clear why you're switching the password fi
Patrick Dubroy
2014/06/02 07:46:52
It's not really necessary, I just thought NSTextVi
| |
64 withFont:font | 65 color:[self nameColor] |
65 color:[self nameColor] | 66 alignment:NSLeftTextAlignment]; |
66 alignment:NSLeftTextAlignment]; | 67 [self addSubview:passwordView_]; |
67 [self addSubview:passwordField_]; | |
68 | 68 |
69 passwordSubtextField_ = | 69 passwordSubtextView_ = [self textViewWithText:controller_->SuggestedText() |
70 [self textFieldWithText:controller_->SuggestedText() | 70 color:[self subtextColor] |
71 withFont:font | 71 alignment:NSRightTextAlignment]; |
72 color:[self subtextColor] | 72 [self addSubview:passwordSubtextView_]; |
73 alignment:NSRightTextAlignment]; | |
74 [self addSubview:passwordSubtextField_]; | |
75 | 73 |
76 helpTextField_ = [self textFieldWithText:controller_->HelpText() | 74 helpTextView_ = |
groby-ooo-7-16
2014/05/30 18:05:01
IIRC, we prefer scoped_nsobject over autoreleased
Patrick Dubroy
2014/06/02 07:46:52
Done. Though I'm not sure whether you meant that I
| |
77 withFont:font | 75 [[[HyperlinkTextView alloc] initWithFrame:NSZeroRect] autorelease]; |
78 color:HelpTextColor() | 76 [helpTextView_ setMessage:base::SysUTF16ToNSString(controller_->HelpText()) |
79 alignment:NSLeftTextAlignment]; | 77 withFont:[self textFont] |
80 [self addSubview:helpTextField_]; | 78 messageColor:HelpTextColor()]; |
79 [helpTextView_ addLinkRange:controller_->HelpTextLinkRange().ToNSRange() | |
80 withName:@"" | |
81 linkColor:[NSColor blueColor]]; | |
82 [helpTextView_ setDelegate:self]; | |
83 [[helpTextView_ textContainer] setLineFragmentPadding:0.0f]; | |
84 [helpTextView_ setVerticallyResizable:YES]; | |
85 [self addSubview:helpTextView_]; | |
81 } | 86 } |
82 | 87 |
83 return self; | 88 return self; |
84 } | 89 } |
85 | 90 |
86 #pragma mark NSView implementation: | 91 #pragma mark NSView implementation: |
87 | 92 |
88 - (void)drawRect:(NSRect)dirtyRect { | 93 - (void)drawRect:(NSRect)dirtyRect { |
89 // If the view is in the process of being destroyed, don't bother drawing. | 94 // If the view is in the process of being destroyed, don't bother drawing. |
90 if (!controller_) | 95 if (!controller_) |
(...skipping 17 matching lines...) Expand all Loading... | |
108 [NSBezierPath fillRect:[self helpBounds]]; | 113 [NSBezierPath fillRect:[self helpBounds]]; |
109 | 114 |
110 // Render the divider. | 115 // Render the divider. |
111 [DividerColor() set]; | 116 [DividerColor() set]; |
112 [NSBezierPath fillRect:[self dividerBounds]]; | 117 [NSBezierPath fillRect:[self dividerBounds]]; |
113 } | 118 } |
114 | 119 |
115 #pragma mark Public API: | 120 #pragma mark Public API: |
116 | 121 |
117 - (void)updateBoundsAndRedrawPopup { | 122 - (void)updateBoundsAndRedrawPopup { |
118 [self positionTextField:passwordField_ inRect:[self passwordBounds]]; | 123 [self positionTextView:passwordView_ inRect:[self passwordBounds]]; |
119 [self positionTextField:passwordSubtextField_ inRect:[self passwordBounds]]; | 124 [self positionTextView:passwordSubtextView_ inRect:[self passwordBounds]]; |
120 [self positionTextField:helpTextField_ inRect:[self helpBounds]]; | 125 [self positionTextView:helpTextView_ inRect:[self helpBounds]]; |
121 | 126 |
122 [super updateBoundsAndRedrawPopup]; | 127 [super updateBoundsAndRedrawPopup]; |
123 } | 128 } |
124 | 129 |
125 - (void)controllerDestroyed { | 130 - (void)controllerDestroyed { |
126 controller_ = NULL; | 131 controller_ = NULL; |
127 [super delegateDestroyed]; | 132 [super delegateDestroyed]; |
128 } | 133 } |
129 | 134 |
135 #pragma mark NSTextViewDelegate implementation: | |
136 | |
137 - (BOOL)textView:(NSTextView *)textView | |
138 clickedOnLink:(id)link | |
139 atIndex:(NSUInteger)charIndex { | |
140 controller_->OnSavedPasswordsLinkClicked(); | |
141 return YES; | |
142 } | |
143 | |
130 #pragma mark Private helpers: | 144 #pragma mark Private helpers: |
131 | 145 |
132 - (NSTextField*)textFieldWithText:(const base::string16&)text | 146 - (NSTextView*)textViewWithText:(const base::string16&)text |
133 withFont:(NSFont*)font | 147 color:(NSColor*)color |
134 color:(NSColor*)color | 148 alignment:(NSTextAlignment)alignment { |
135 alignment:(NSTextAlignment)alignment { | |
136 scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( | 149 scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( |
137 [[NSMutableParagraphStyle alloc] init]); | 150 [[NSMutableParagraphStyle alloc] init]); |
138 [paragraphStyle setAlignment:alignment]; | 151 [paragraphStyle setAlignment:alignment]; |
139 | 152 |
140 NSDictionary* textAttributes = @{ | 153 NSDictionary* textAttributes = @{ |
141 NSFontAttributeName : font, | 154 NSFontAttributeName : [self textFont], |
142 NSForegroundColorAttributeName : color, | 155 NSForegroundColorAttributeName : color, |
143 NSParagraphStyleAttributeName : paragraphStyle | 156 NSParagraphStyleAttributeName : paragraphStyle |
144 }; | 157 }; |
145 | 158 |
146 scoped_nsobject<NSAttributedString> attributedString( | 159 scoped_nsobject<NSAttributedString> attributedString( |
147 [[NSAttributedString alloc] | 160 [[NSAttributedString alloc] |
148 initWithString:base::SysUTF16ToNSString(text) | 161 initWithString:base::SysUTF16ToNSString(text) |
149 attributes:textAttributes]); | 162 attributes:textAttributes]); |
150 | 163 |
151 NSTextField* textField = | 164 NSTextView* textView = |
152 [[[NSTextField alloc] initWithFrame:NSZeroRect] autorelease]; | 165 [[[NSTextView alloc] initWithFrame:NSZeroRect] autorelease]; |
153 [textField setAttributedStringValue:attributedString]; | 166 [[textView textStorage] setAttributedString:attributedString]; |
154 [textField setEditable:NO]; | 167 [[textView textContainer] setLineFragmentPadding:0.0f]; |
155 [textField setSelectable:NO]; | 168 [textView setSelectable:NO]; |
156 [textField setDrawsBackground:NO]; | 169 [textView setDrawsBackground:NO]; |
157 [textField setBezeled:NO]; | 170 return textView; |
158 | |
159 return textField; | |
160 } | 171 } |
161 | 172 |
162 - (void)positionTextField:(NSTextField*)textField inRect:(NSRect)bounds { | 173 - (void)positionTextView:(NSTextView*)textView inRect:(NSRect)bounds { |
163 NSRect frame = NSInsetRect(bounds, controller_->kHorizontalPadding, 0); | 174 NSRect frame = NSInsetRect(bounds, controller_->kHorizontalPadding, 0); |
164 [textField setFrame:frame]; | 175 [textView setFrame:frame]; |
165 | 176 |
166 // Center the text vertically within the bounds. | 177 // Center the text vertically within the bounds. |
167 NSSize delta = cocoa_l10n_util::WrapOrSizeToFit(textField); | 178 NSSize delta = cocoa_l10n_util::WrapOrSizeToFit(textView); |
168 [textField setFrameOrigin: | 179 [textView setFrameOrigin: |
169 NSInsetRect(frame, 0, floor(-delta.height/2)).origin]; | 180 NSInsetRect(frame, 0, floor(-delta.height/2)).origin]; |
170 } | 181 } |
171 | 182 |
172 - (NSRect)passwordBounds { | 183 - (NSRect)passwordBounds { |
173 return NSRectFromCGRect(controller_->password_bounds().ToCGRect()); | 184 return NSRectFromCGRect(controller_->password_bounds().ToCGRect()); |
174 } | 185 } |
175 | 186 |
176 - (NSRect)helpBounds { | 187 - (NSRect)helpBounds { |
177 return NSRectFromCGRect(controller_->help_bounds().ToCGRect()); | 188 return NSRectFromCGRect(controller_->help_bounds().ToCGRect()); |
178 } | 189 } |
179 | 190 |
180 - (NSRect)dividerBounds { | 191 - (NSRect)dividerBounds { |
181 return NSRectFromCGRect(controller_->divider_bounds().ToCGRect()); | 192 return NSRectFromCGRect(controller_->divider_bounds().ToCGRect()); |
182 } | 193 } |
183 | 194 |
195 - (NSFont*)textFont { | |
196 return controller_->font_list().GetPrimaryFont().GetNativeFont(); | |
197 } | |
198 | |
184 @end | 199 @end |
OLD | NEW |