1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/cocoa/autofill/password_generation_popup_view_cocoa.h"
#include <cmath>
#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/ui/autofill/autofill_popup_controller.h"
#include "chrome/browser/ui/autofill/autofill_popup_view.h"
#include "chrome/browser/ui/autofill/popup_constants.h"
#include "chrome/browser/ui/chrome_style.h"
#include "chrome/browser/ui/cocoa/autofill/password_generation_popup_view_bridge.h"
#import "chrome/browser/ui/cocoa/l10n_util.h"
#include "components/autofill/core/browser/popup_item_ids.h"
#include "grit/theme_resources.h"
#include "skia/ext/skia_utils_mac.h"
#import "ui/base/cocoa/controls/hyperlink_text_view.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/range/range.h"
#include "ui/gfx/text_constants.h"
using autofill::AutofillPopupView;
using autofill::PasswordGenerationPopupController;
using autofill::PasswordGenerationPopupView;
using base::scoped_nsobject;
namespace {
// The height of the divider between the password and help sections, in pixels.
const CGFloat kDividerHeight = 1;
// The amount of whitespace, in pixels, between lines of text in the password
// section.
const CGFloat kPasswordSectionVerticalSeparation = 5;
NSColor* DividerColor() {
return gfx::SkColorToCalibratedNSColor(
PasswordGenerationPopupView::kDividerColor);
}
NSColor* HelpTextBackgroundColor() {
return gfx::SkColorToCalibratedNSColor(
PasswordGenerationPopupView::kExplanatoryTextBackgroundColor);
}
NSColor* HelpTextColor() {
return gfx::SkColorToCalibratedNSColor(
PasswordGenerationPopupView::kExplanatoryTextColor);
}
NSColor* HelpLinkColor() {
return gfx::SkColorToCalibratedNSColor(chrome_style::GetLinkColor());
}
} // namespace
@implementation PasswordGenerationPopupViewCocoa
#pragma mark Initialisers
- (id)initWithFrame:(NSRect)frame {
NOTREACHED();
return nil;
}
- (id)initWithController:
(autofill::PasswordGenerationPopupController*)controller
frame:(NSRect)frame {
if (self = [super initWithDelegate:controller frame:frame]) {
controller_ = controller;
passwordSection_.reset([[NSView alloc] initWithFrame:NSZeroRect]);
[self addSubview:passwordSection_];
passwordField_.reset(
[[self textFieldWithText:controller_->password()
attributes:[self passwordAttributes]] retain]);
[passwordSection_ addSubview:passwordField_];
passwordTitleField_.reset(
[[self textFieldWithText:controller_->SuggestedText()
attributes:[self passwordTitleAttributes]] retain]);
[passwordSection_ addSubview:passwordTitleField_];
keyIcon_.reset([[NSImageView alloc] initWithFrame:NSZeroRect]);
NSImage* keyImage = ResourceBundle::GetSharedInstance()
.GetImageNamed(IDR_GENERATE_PASSWORD_KEY)
.ToNSImage();
[keyIcon_ setImage:keyImage];
[passwordSection_ addSubview:keyIcon_];
divider_.reset([[NSBox alloc] initWithFrame:NSZeroRect]);
[divider_ setBoxType:NSBoxCustom];
[divider_ setBorderType:NSLineBorder];
[divider_ setBorderColor:DividerColor()];
[self addSubview:divider_];
helpTextView_.reset([[HyperlinkTextView alloc] initWithFrame:NSZeroRect]);
[helpTextView_ setMessage:base::SysUTF16ToNSString(controller_->HelpText())
withFont:[self textFont]
messageColor:HelpTextColor()];
[helpTextView_ addLinkRange:controller_->HelpTextLinkRange().ToNSRange()
withName:@""
linkColor:HelpLinkColor()];
[helpTextView_ setDelegate:self];
[helpTextView_ setDrawsBackground:YES];
[helpTextView_ setBackgroundColor:HelpTextBackgroundColor()];
[helpTextView_
setTextContainerInset:NSMakeSize(controller_->kHorizontalPadding,
controller_->kHelpVerticalPadding)];
// Remove the underlining.
NSTextStorage* text = [helpTextView_ textStorage];
[text addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleNone)
range:controller_->HelpTextLinkRange().ToNSRange()];
[self addSubview:helpTextView_];
}
return self;
}
- (void)updateTrackingAreas {
[super updateTrackingAreas];
if (helpTextTrackingArea_.get())
[self removeTrackingArea:helpTextTrackingArea_.get()];
// Set up tracking for the help text so the cursor, etc. is properly handled.
// Must set tracking to "always" because the autofill window is never key.
NSTrackingAreaOptions options = NSTrackingActiveAlways |
NSTrackingMouseEnteredAndExited |
NSTrackingMouseMoved |
NSTrackingCursorUpdate;
helpTextTrackingArea_.reset(
[[CrTrackingArea alloc] initWithRect:[self bounds]
options:options
owner:helpTextView_.get()
userInfo:nil]);
[self addTrackingArea:helpTextTrackingArea_.get()];
}
#pragma mark NSView implementation:
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// If the view is in the process of being destroyed, don't bother drawing.
if (!controller_)
return;
[self drawBackgroundAndBorder];
if (controller_->password_selected()) {
// Draw a highlight under the suggested password.
NSRect highlightBounds = [passwordSection_ frame];
[[self highlightColor] set];
[NSBezierPath fillRect:highlightBounds];
}
}
#pragma mark Public API:
- (NSSize)preferredSize {
const NSSize passwordTitleSize =
[base::SysUTF16ToNSString(controller_->SuggestedText())
sizeWithAttributes:@{ NSFontAttributeName : [self boldFont] }];
const NSSize passwordSize = [base::SysUTF16ToNSString(controller_->password())
sizeWithAttributes:@{ NSFontAttributeName : [self textFont] }];
CGFloat width =
autofill::kPopupBorderThickness +
controller_->kHorizontalPadding +
[[keyIcon_ image] size].width +
controller_->kHorizontalPadding +
std::max(passwordSize.width, passwordTitleSize.width) +
controller_->kHorizontalPadding +
autofill::kPopupBorderThickness;
width = std::max(width, (CGFloat)controller_->GetMinimumWidth());
CGFloat contentWidth = width - (2 * controller_->kHorizontalPadding);
CGFloat height =
autofill::kPopupBorderThickness +
controller_->kHelpVerticalPadding +
[self helpSizeForPopupWidth:contentWidth].height +
controller_->kHelpVerticalPadding +
autofill::kPopupBorderThickness;
if (controller_->display_password())
height += controller_->kPopupPasswordSectionHeight;
return NSMakeSize(width, height);
}
- (void)updateBoundsAndRedrawPopup {
const CGFloat popupWidth = controller_->popup_bounds().width();
const CGFloat contentWidth =
popupWidth - (2 * autofill::kPopupBorderThickness);
const CGFloat contentHeight = controller_->popup_bounds().height() -
(2 * autofill::kPopupBorderThickness);
if (controller_->display_password()) {
// The password can change while the bubble is shown: If the user has
// accepted the password and then selects the form again and starts deleting
// the password, the field will be initially invisible and then become
// visible.
[self updatePassword];
// Lay out the password section, which includes the key icon, the title, and
// the suggested password.
[passwordSection_
setFrame:NSMakeRect(autofill::kPopupBorderThickness,
autofill::kPopupBorderThickness,
contentWidth,
controller_->kPopupPasswordSectionHeight)];
// The key icon falls to the left of the title and password.
const NSSize imageSize = [[keyIcon_ image] size];
const CGFloat keyX = controller_->kHorizontalPadding;
const CGFloat keyY =
std::ceil((controller_->kPopupPasswordSectionHeight / 2.0) -
(imageSize.height / 2.0));
[keyIcon_ setFrame:{ NSMakePoint(keyX, keyY), imageSize }];
// The title and password fall to the right of the key icon and are centered
// vertically as a group with some padding in between.
[passwordTitleField_ sizeToFit];
[passwordField_ sizeToFit];
const CGFloat groupHeight = NSHeight([passwordField_ frame]) +
kPasswordSectionVerticalSeparation +
NSHeight([passwordTitleField_ frame]);
const CGFloat groupX =
NSMaxX([keyIcon_ frame]) + controller_->kHorizontalPadding;
const CGFloat groupY =
std::ceil((controller_->kPopupPasswordSectionHeight / 2.0) -
(groupHeight / 2.0));
[passwordField_ setFrameOrigin:NSMakePoint(groupX, groupY)];
const CGFloat titleY = groupY +
NSHeight([passwordField_ frame]) +
kPasswordSectionVerticalSeparation;
[passwordTitleField_ setFrameOrigin:NSMakePoint(groupX, titleY)];
// Layout the divider, which falls immediately below the password section.
const CGFloat dividerX = autofill::kPopupBorderThickness;
const CGFloat dividerY = NSMaxY([passwordSection_ frame]);
NSRect dividerFrame =
NSMakeRect(dividerX, dividerY, contentWidth, kDividerHeight);
[divider_ setFrame:dividerFrame];
}
// Layout the help section beneath the divider (if applicable, otherwise
// beneath the border).
const CGFloat helpX = autofill::kPopupBorderThickness;
const CGFloat helpY = controller_->display_password()
? NSMaxY([divider_ frame])
: autofill::kPopupBorderThickness;
const CGFloat helpHeight = contentHeight -
NSHeight([passwordSection_ frame]) -
NSHeight([divider_ frame]);
[helpTextView_ setFrame:NSMakeRect(helpX, helpY, contentWidth, helpHeight)];
[super updateBoundsAndRedrawPopup];
}
- (BOOL)isPointInPasswordBounds:(NSPoint)point {
return NSPointInRect(point, [passwordSection_ frame]);
}
- (void)controllerDestroyed {
controller_ = NULL;
[super delegateDestroyed];
}
#pragma mark NSTextViewDelegate implementation:
- (BOOL)textView:(NSTextView*)textView
clickedOnLink:(id)link
atIndex:(NSUInteger)charIndex {
controller_->OnSavedPasswordsLinkClicked();
return YES;
}
#pragma mark Private helpers:
- (void)updatePassword {
base::scoped_nsobject<NSMutableAttributedString> updatedPassword(
[[NSMutableAttributedString alloc]
initWithString:base::SysUTF16ToNSString(controller_->password())
attributes:[self passwordAttributes]]);
[passwordField_ setAttributedStringValue:updatedPassword];
}
- (NSDictionary*)passwordTitleAttributes {
scoped_nsobject<NSMutableParagraphStyle> paragraphStyle(
[[NSMutableParagraphStyle alloc] init]);
[paragraphStyle setAlignment:NSLeftTextAlignment];
return @{
NSFontAttributeName : [self boldFont],
NSForegroundColorAttributeName : [self nameColor],
NSParagraphStyleAttributeName : paragraphStyle.autorelease()
};
}
- (NSDictionary*)passwordAttributes {
scoped_nsobject<NSMutableParagraphStyle> paragraphStyle(
[[NSMutableParagraphStyle alloc] init]);
[paragraphStyle setAlignment:NSLeftTextAlignment];
return @{
NSFontAttributeName : [self textFont],
NSForegroundColorAttributeName : [self nameColor],
NSParagraphStyleAttributeName : paragraphStyle.autorelease()
};
}
- (NSTextField*)textFieldWithText:(const base::string16&)text
attributes:(NSDictionary*)attributes {
NSTextField* textField =
[[[NSTextField alloc] initWithFrame:NSZeroRect] autorelease];
scoped_nsobject<NSAttributedString> attributedString(
[[NSAttributedString alloc]
initWithString:base::SysUTF16ToNSString(text)
attributes:attributes]);
[textField setAttributedStringValue:attributedString.autorelease()];
[textField setEditable:NO];
[textField setSelectable:NO];
[textField setDrawsBackground:NO];
[textField setBezeled:NO];
return textField;
}
- (NSSize)helpSizeForPopupWidth:(CGFloat)width {
const CGFloat helpWidth = width -
2 * controller_->kHorizontalPadding -
2 * autofill::kPopupBorderThickness;
const NSSize size = NSMakeSize(helpWidth, MAXFLOAT);
NSRect textFrame = [base::SysUTF16ToNSString(controller_->HelpText())
boundingRectWithSize:size
options:NSLineBreakByWordWrapping |
NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName : [self textFont] }];
return textFrame.size;
}
- (NSFont*)boldFont {
return [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]];
}
- (NSFont*)textFont {
return [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
}
@end
|