Chromium Code Reviews| Index: chrome/browser/ui/cocoa/autofill/password_generation_popup_view_cocoa.mm |
| diff --git a/chrome/browser/ui/cocoa/autofill/password_generation_popup_view_cocoa.mm b/chrome/browser/ui/cocoa/autofill/password_generation_popup_view_cocoa.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b61413b2930d67f0e06be3a7aa825ea6c4713086 |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/autofill/password_generation_popup_view_cocoa.mm |
| @@ -0,0 +1,160 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
|
Ilya Sherman
2014/05/09 21:51:28
nit: 2014
Patrick Dubroy
2014/05/12 14:13:39
Done.
|
| +// 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 "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/cocoa/autofill/password_generation_popup_view_bridge.h" |
| +#include "components/autofill/core/browser/popup_item_ids.h" |
| +#include "grit/ui_resources.h" |
| +#include "skia/ext/skia_utils_mac.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| +#include "ui/gfx/font_list.h" |
| +#include "ui/gfx/image/image.h" |
| +#include "ui/gfx/point.h" |
| +#include "ui/gfx/rect.h" |
| +#include "ui/gfx/text_constants.h" |
| + |
| +using autofill::AutofillPopupView; |
| +using autofill::PasswordGenerationPopupView; |
| + |
| +@interface PasswordGenerationPopupViewCocoa () |
| +@end |
|
Ilya Sherman
2014/05/09 21:51:28
What's this for?
Patrick Dubroy
2014/05/12 14:13:39
Not sure. Removed.
|
| + |
| +@implementation PasswordGenerationPopupViewCocoa |
| + |
| +#pragma mark - |
| +#pragma mark Initialisers |
| + |
| +- (id)initWithFrame:(NSRect)frame { |
| + NOTREACHED(); |
| + return [self initWithController:NULL frame:frame]; |
|
Ilya Sherman
2014/05/09 21:51:28
nit: If the method should not be reachable, just r
Patrick Dubroy
2014/05/12 14:13:39
Done.
|
| +} |
| + |
| +- (id)initWithController: |
| + (autofill::PasswordGenerationPopupController*)controller |
| + frame:(NSRect)frame { |
| + self = [super initWithAutofillPopupViewDelegate:controller frame:frame]; |
| + if (self) |
| + controller_ = controller; |
| + |
| + return self; |
| +} |
| + |
| +- (void)controllerDestroyed { |
| + // Since the |controller_| either already has been destroyed or is about to |
| + // be, about the only thing we can safely do with it is to null it out. |
| + controller_ = NULL; |
|
Ilya Sherman
2014/05/09 21:51:28
Should this call [super controllerDestroyed] as we
Patrick Dubroy
2014/05/12 14:13:39
Actually, I don't think we need this at all. It's
Ilya Sherman
2014/05/12 21:46:10
The view is destroyed asynchronously, though -- th
|
| +} |
| + |
| +#pragma mark - |
| +#pragma mark NSView implementation: |
| + |
| +- (void)drawRect:(NSRect)dirtyRect { |
| + // If the view is in the process of being destroyed, don't bother drawing. |
| + if (!controller_) |
| + return; |
| + |
| + [self drawBackgroundAndBorderInRect:dirtyRect]; |
| + |
| + NSRect bounds = [self bounds]; |
| + bounds.origin.y += autofill::kPopupBorderThickness; |
| + |
| + if (controller_->password_selected()) { |
| + // Draw a highlight under the suggested password. |
| + NSRect highlightBounds = |
| + NSRectFromCGRect(controller_->password_bounds().ToCGRect()); |
| + highlightBounds.origin.y += |
| + PasswordGenerationPopupView::kPasswordVerticalInset; |
| + highlightBounds.size.height -= |
| + 2 * PasswordGenerationPopupView::kPasswordVerticalInset; |
| + [[self highlightColor] set]; |
| + [NSBezierPath fillRect:highlightBounds]; |
| + } |
| + |
| + NSFont* font = controller_->font_list().GetPrimaryFont().GetNativeFont(); |
| + NSRect passwordBounds = |
| + NSRectFromCGRect(controller_->password_bounds().ToCGRect()); |
| + |
| + BOOL isRTL = NO; // TODO(dubroy): Implement RTL support. |
| + [self drawText:base::SysUTF16ToNSString(controller_->password()) |
| + withFont:font |
| + color:[self nameColor] |
| + bounds:passwordBounds |
| + alignment:isRTL ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT]; |
| + |
| + [self drawText:base::SysUTF16ToNSString(controller_->SuggestedText()) |
| + withFont:font |
| + color:[self subtextColor] |
| + bounds:passwordBounds |
| + alignment:isRTL ? gfx::ALIGN_LEFT : gfx::ALIGN_RIGHT]; |
| + |
| + // Render the background of the help text. |
| + NSRect helpBounds = |
| + NSRectFromCGRect(controller_->help_bounds().ToCGRect()); |
| + [[self helpTextBackgroundColor] set]; |
| + [NSBezierPath fillRect:helpBounds]; |
| + |
| + // Render the divider. |
| + NSRect helpBorder = helpBounds; |
| + helpBorder.size.height = 1; |
| + [[self dividerColor] set]; |
| + [NSBezierPath fillRect:helpBorder]; |
| + |
| + // Render the help text. |
| + [self drawText:base::SysUTF16ToNSString(controller_->HelpText()) |
| + withFont:font |
| + color:[self helpTextColor] |
| + bounds:helpBounds |
|
Ilya Sherman
2014/05/09 21:51:28
Hmm, shouldn't this not include the pixel used for
Patrick Dubroy
2014/05/12 14:13:39
Done.
|
| + alignment:isRTL ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT]; |
| +} |
| + |
| +- (void)drawText:(NSString*)text |
| + withFont:(NSFont*)font |
| + color:(id)color |
| + bounds:(NSRect)bounds |
| + alignment:(gfx::HorizontalAlignment)alignment { |
| + NSDictionary* textAttributes = |
| + [NSDictionary dictionaryWithObjectsAndKeys: |
| + font, NSFontAttributeName, |
| + color, NSForegroundColorAttributeName, |
| + nil]; |
| + // Adjust horizontal padding before measuring. |
| + bounds.size.width -= 2 * controller_->kHorizontalPadding; |
| + bounds.origin.x += controller_->kHorizontalPadding; |
| + |
| + NSSize textSize = |
| + [text boundingRectWithSize:bounds.size |
| + options:NSStringDrawingUsesLineFragmentOrigin |
| + attributes:textAttributes].size; |
| + |
| + // Center the text vertically within the bounds. |
| + bounds.origin.y = NSMinY(bounds) + (NSHeight(bounds) - textSize.height) / 2; |
| + |
| + if (alignment == gfx::ALIGN_RIGHT) |
| + bounds.origin.x += NSWidth(bounds) - textSize.width; |
| + |
| + [text drawInRect:bounds withAttributes:textAttributes]; |
| +} |
| + |
| +- (NSColor*)dividerColor { |
| + return gfx::SkColorToCalibratedNSColor( |
| + PasswordGenerationPopupView::kDividerColor); |
| +} |
| + |
| +- (NSColor*)helpTextBackgroundColor { |
| + return gfx::SkColorToCalibratedNSColor( |
| + PasswordGenerationPopupView::kExplanatoryTextBackgroundColor); |
| +} |
| + |
| +- (NSColor*)helpTextColor { |
| + return gfx::SkColorToCalibratedNSColor( |
| + PasswordGenerationPopupView::kExplanatoryTextColor); |
| +} |
|
Ilya Sherman
2014/05/09 21:51:28
These methods don't seem to be declared anywhere.
Patrick Dubroy
2014/05/12 14:13:39
Done.
|
| + |
| +@end |