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/chrome_style.h" |
12 #include "chrome/browser/ui/cocoa/autofill/password_generation_popup_view_bridge
.h" | 13 #include "chrome/browser/ui/cocoa/autofill/password_generation_popup_view_bridge
.h" |
| 14 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h" |
13 #import "chrome/browser/ui/cocoa/l10n_util.h" | 15 #import "chrome/browser/ui/cocoa/l10n_util.h" |
14 #include "components/autofill/core/browser/popup_item_ids.h" | 16 #include "components/autofill/core/browser/popup_item_ids.h" |
15 #include "grit/ui_resources.h" | 17 #include "grit/ui_resources.h" |
16 #include "skia/ext/skia_utils_mac.h" | 18 #include "skia/ext/skia_utils_mac.h" |
17 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
18 #include "ui/gfx/font_list.h" | 20 #include "ui/gfx/font_list.h" |
19 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
20 #include "ui/gfx/point.h" | 22 #include "ui/gfx/point.h" |
| 23 #include "ui/gfx/range/range.h" |
21 #include "ui/gfx/rect.h" | 24 #include "ui/gfx/rect.h" |
22 #include "ui/gfx/text_constants.h" | 25 #include "ui/gfx/text_constants.h" |
23 | 26 |
24 using autofill::AutofillPopupView; | 27 using autofill::AutofillPopupView; |
25 using autofill::PasswordGenerationPopupView; | 28 using autofill::PasswordGenerationPopupView; |
26 using base::scoped_nsobject; | 29 using base::scoped_nsobject; |
27 | 30 |
28 namespace { | 31 namespace { |
29 | 32 |
30 NSColor* DividerColor() { | 33 NSColor* DividerColor() { |
31 return gfx::SkColorToCalibratedNSColor( | 34 return gfx::SkColorToCalibratedNSColor( |
32 PasswordGenerationPopupView::kDividerColor); | 35 PasswordGenerationPopupView::kDividerColor); |
33 } | 36 } |
34 | 37 |
35 NSColor* HelpTextBackgroundColor() { | 38 NSColor* HelpTextBackgroundColor() { |
36 return gfx::SkColorToCalibratedNSColor( | 39 return gfx::SkColorToCalibratedNSColor( |
37 PasswordGenerationPopupView::kExplanatoryTextBackgroundColor); | 40 PasswordGenerationPopupView::kExplanatoryTextBackgroundColor); |
38 } | 41 } |
39 | 42 |
40 NSColor* HelpTextColor() { | 43 NSColor* HelpTextColor() { |
41 return gfx::SkColorToCalibratedNSColor( | 44 return gfx::SkColorToCalibratedNSColor( |
42 PasswordGenerationPopupView::kExplanatoryTextColor); | 45 PasswordGenerationPopupView::kExplanatoryTextColor); |
43 } | 46 } |
44 | 47 |
| 48 NSColor* HelpLinkColor() { |
| 49 return gfx::SkColorToCalibratedNSColor(chrome_style::GetLinkColor()); |
| 50 } |
| 51 |
45 } // namespace | 52 } // namespace |
46 | 53 |
47 @implementation PasswordGenerationPopupViewCocoa | 54 @implementation PasswordGenerationPopupViewCocoa |
48 | 55 |
49 #pragma mark Initialisers | 56 #pragma mark Initialisers |
50 | 57 |
51 - (id)initWithFrame:(NSRect)frame { | 58 - (id)initWithFrame:(NSRect)frame { |
52 NOTREACHED(); | 59 NOTREACHED(); |
53 return nil; | 60 return nil; |
54 } | 61 } |
55 | 62 |
56 - (id)initWithController: | 63 - (id)initWithController: |
57 (autofill::PasswordGenerationPopupController*)controller | 64 (autofill::PasswordGenerationPopupController*)controller |
58 frame:(NSRect)frame { | 65 frame:(NSRect)frame { |
59 if (self = [super initWithDelegate:controller frame:frame]) { | 66 if (self = [super initWithDelegate:controller frame:frame]) { |
60 controller_ = controller; | 67 controller_ = controller; |
61 NSFont* font = controller_->font_list().GetPrimaryFont().GetNativeFont(); | |
62 | 68 |
63 passwordField_ = [self textFieldWithText:controller_->password() | 69 passwordField_ = [self textFieldWithText:controller_->password() |
64 withFont:font | |
65 color:[self nameColor] | 70 color:[self nameColor] |
66 alignment:NSLeftTextAlignment]; | 71 alignment:NSLeftTextAlignment]; |
67 [self addSubview:passwordField_]; | 72 [self addSubview:passwordField_]; |
68 | 73 |
69 passwordSubtextField_ = | 74 passwordSubtextField_ = [self textFieldWithText:controller_->SuggestedText() |
70 [self textFieldWithText:controller_->SuggestedText() | 75 color:[self subtextColor] |
71 withFont:font | 76 alignment:NSRightTextAlignment]; |
72 color:[self subtextColor] | |
73 alignment:NSRightTextAlignment]; | |
74 [self addSubview:passwordSubtextField_]; | 77 [self addSubview:passwordSubtextField_]; |
75 | 78 |
76 helpTextField_ = [self textFieldWithText:controller_->HelpText() | 79 scoped_nsobject<HyperlinkTextView> helpTextView( |
77 withFont:font | 80 [[HyperlinkTextView alloc] initWithFrame:NSZeroRect]); |
78 color:HelpTextColor() | 81 [helpTextView setMessage:base::SysUTF16ToNSString(controller_->HelpText()) |
79 alignment:NSLeftTextAlignment]; | 82 withFont:[self textFont] |
80 [self addSubview:helpTextField_]; | 83 messageColor:HelpTextColor()]; |
| 84 [helpTextView addLinkRange:controller_->HelpTextLinkRange().ToNSRange() |
| 85 withName:@"" |
| 86 linkColor:HelpLinkColor()]; |
| 87 [helpTextView setDelegate:self]; |
| 88 [[helpTextView textContainer] setLineFragmentPadding:0.0f]; |
| 89 [helpTextView setVerticallyResizable:YES]; |
| 90 [self addSubview:helpTextView]; |
| 91 helpTextView_ = helpTextView.get(); |
81 } | 92 } |
82 | 93 |
83 return self; | 94 return self; |
84 } | 95 } |
85 | 96 |
86 #pragma mark NSView implementation: | 97 #pragma mark NSView implementation: |
87 | 98 |
88 - (void)drawRect:(NSRect)dirtyRect { | 99 - (void)drawRect:(NSRect)dirtyRect { |
89 // If the view is in the process of being destroyed, don't bother drawing. | 100 // If the view is in the process of being destroyed, don't bother drawing. |
90 if (!controller_) | 101 if (!controller_) |
(...skipping 17 matching lines...) Expand all Loading... |
108 [NSBezierPath fillRect:[self helpBounds]]; | 119 [NSBezierPath fillRect:[self helpBounds]]; |
109 | 120 |
110 // Render the divider. | 121 // Render the divider. |
111 [DividerColor() set]; | 122 [DividerColor() set]; |
112 [NSBezierPath fillRect:[self dividerBounds]]; | 123 [NSBezierPath fillRect:[self dividerBounds]]; |
113 } | 124 } |
114 | 125 |
115 #pragma mark Public API: | 126 #pragma mark Public API: |
116 | 127 |
117 - (void)updateBoundsAndRedrawPopup { | 128 - (void)updateBoundsAndRedrawPopup { |
118 [self positionTextField:passwordField_ inRect:[self passwordBounds]]; | 129 [self positionView:passwordField_ inRect:[self passwordBounds]]; |
119 [self positionTextField:passwordSubtextField_ inRect:[self passwordBounds]]; | 130 [self positionView:passwordSubtextField_ inRect:[self passwordBounds]]; |
120 [self positionTextField:helpTextField_ inRect:[self helpBounds]]; | 131 [self positionView:helpTextView_ inRect:[self helpBounds]]; |
121 | 132 |
122 [super updateBoundsAndRedrawPopup]; | 133 [super updateBoundsAndRedrawPopup]; |
123 } | 134 } |
124 | 135 |
125 - (void)controllerDestroyed { | 136 - (void)controllerDestroyed { |
126 controller_ = NULL; | 137 controller_ = NULL; |
127 [super delegateDestroyed]; | 138 [super delegateDestroyed]; |
128 } | 139 } |
129 | 140 |
| 141 #pragma mark NSTextViewDelegate implementation: |
| 142 |
| 143 - (BOOL)textView:(NSTextView*)textView |
| 144 clickedOnLink:(id)link |
| 145 atIndex:(NSUInteger)charIndex { |
| 146 controller_->OnSavedPasswordsLinkClicked(); |
| 147 return YES; |
| 148 } |
| 149 |
130 #pragma mark Private helpers: | 150 #pragma mark Private helpers: |
131 | 151 |
132 - (NSTextField*)textFieldWithText:(const base::string16&)text | 152 - (NSTextField*)textFieldWithText:(const base::string16&)text |
133 withFont:(NSFont*)font | |
134 color:(NSColor*)color | 153 color:(NSColor*)color |
135 alignment:(NSTextAlignment)alignment { | 154 alignment:(NSTextAlignment)alignment { |
136 scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( | 155 scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( |
137 [[NSMutableParagraphStyle alloc] init]); | 156 [[NSMutableParagraphStyle alloc] init]); |
138 [paragraphStyle setAlignment:alignment]; | 157 [paragraphStyle setAlignment:alignment]; |
139 | 158 |
140 NSDictionary* textAttributes = @{ | 159 NSDictionary* textAttributes = @{ |
141 NSFontAttributeName : font, | 160 NSFontAttributeName : [self textFont], |
142 NSForegroundColorAttributeName : color, | 161 NSForegroundColorAttributeName : color, |
143 NSParagraphStyleAttributeName : paragraphStyle | 162 NSParagraphStyleAttributeName : paragraphStyle |
144 }; | 163 }; |
145 | 164 |
146 scoped_nsobject<NSAttributedString> attributedString( | 165 scoped_nsobject<NSAttributedString> attributedString( |
147 [[NSAttributedString alloc] | 166 [[NSAttributedString alloc] |
148 initWithString:base::SysUTF16ToNSString(text) | 167 initWithString:base::SysUTF16ToNSString(text) |
149 attributes:textAttributes]); | 168 attributes:textAttributes]); |
150 | 169 |
151 NSTextField* textField = | 170 NSTextField* textField = |
152 [[[NSTextField alloc] initWithFrame:NSZeroRect] autorelease]; | 171 [[[NSTextField alloc] initWithFrame:NSZeroRect] autorelease]; |
153 [textField setAttributedStringValue:attributedString]; | 172 [textField setAttributedStringValue:attributedString]; |
154 [textField setEditable:NO]; | 173 [textField setEditable:NO]; |
155 [textField setSelectable:NO]; | 174 [textField setSelectable:NO]; |
156 [textField setDrawsBackground:NO]; | 175 [textField setDrawsBackground:NO]; |
157 [textField setBezeled:NO]; | 176 [textField setBezeled:NO]; |
158 | |
159 return textField; | 177 return textField; |
160 } | 178 } |
161 | 179 |
162 - (void)positionTextField:(NSTextField*)textField inRect:(NSRect)bounds { | 180 - (void)positionView:(NSView*)view inRect:(NSRect)bounds { |
163 NSRect frame = NSInsetRect(bounds, controller_->kHorizontalPadding, 0); | 181 NSRect frame = NSInsetRect(bounds, controller_->kHorizontalPadding, 0); |
164 [textField setFrame:frame]; | 182 [view setFrame:frame]; |
165 | 183 |
166 // Center the text vertically within the bounds. | 184 // Center the text vertically within the bounds. |
167 NSSize delta = cocoa_l10n_util::WrapOrSizeToFit(textField); | 185 NSSize delta = cocoa_l10n_util::WrapOrSizeToFit(view); |
168 [textField setFrameOrigin: | 186 [view setFrameOrigin: |
169 NSInsetRect(frame, 0, floor(-delta.height/2)).origin]; | 187 NSInsetRect(frame, 0, floor(-delta.height/2)).origin]; |
170 } | 188 } |
171 | 189 |
172 - (NSRect)passwordBounds { | 190 - (NSRect)passwordBounds { |
173 return NSRectFromCGRect(controller_->password_bounds().ToCGRect()); | 191 return NSRectFromCGRect(controller_->password_bounds().ToCGRect()); |
174 } | 192 } |
175 | 193 |
176 - (NSRect)helpBounds { | 194 - (NSRect)helpBounds { |
177 return NSRectFromCGRect(controller_->help_bounds().ToCGRect()); | 195 return NSRectFromCGRect(controller_->help_bounds().ToCGRect()); |
178 } | 196 } |
179 | 197 |
180 - (NSRect)dividerBounds { | 198 - (NSRect)dividerBounds { |
181 return NSRectFromCGRect(controller_->divider_bounds().ToCGRect()); | 199 return NSRectFromCGRect(controller_->divider_bounds().ToCGRect()); |
182 } | 200 } |
183 | 201 |
| 202 - (NSFont*)textFont { |
| 203 return controller_->font_list().GetPrimaryFont().GetNativeFont(); |
| 204 } |
| 205 |
184 @end | 206 @end |
OLD | NEW |