OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/autofill/password_generation_popup_view_cocoa.h
" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/strings/sys_string_conversions.h" |
| 9 #include "chrome/browser/ui/autofill/autofill_popup_controller.h" |
| 10 #include "chrome/browser/ui/autofill/autofill_popup_view.h" |
| 11 #include "chrome/browser/ui/autofill/popup_constants.h" |
| 12 #include "chrome/browser/ui/cocoa/autofill/password_generation_popup_view_bridge
.h" |
| 13 #include "components/autofill/core/browser/popup_item_ids.h" |
| 14 #include "grit/ui_resources.h" |
| 15 #include "skia/ext/skia_utils_mac.h" |
| 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/gfx/font_list.h" |
| 18 #include "ui/gfx/image/image.h" |
| 19 #include "ui/gfx/point.h" |
| 20 #include "ui/gfx/rect.h" |
| 21 #include "ui/gfx/text_constants.h" |
| 22 |
| 23 using autofill::AutofillPopupView; |
| 24 using autofill::PasswordGenerationPopupView; |
| 25 |
| 26 namespace { |
| 27 |
| 28 NSColor* DividerColor() { |
| 29 return gfx::SkColorToCalibratedNSColor( |
| 30 PasswordGenerationPopupView::kDividerColor); |
| 31 } |
| 32 |
| 33 NSColor* HelpTextBackgroundColor() { |
| 34 return gfx::SkColorToCalibratedNSColor( |
| 35 PasswordGenerationPopupView::kExplanatoryTextBackgroundColor); |
| 36 } |
| 37 |
| 38 NSColor* HelpTextColor() { |
| 39 return gfx::SkColorToCalibratedNSColor( |
| 40 PasswordGenerationPopupView::kExplanatoryTextColor); |
| 41 } |
| 42 |
| 43 } // namespace |
| 44 |
| 45 @implementation PasswordGenerationPopupViewCocoa |
| 46 |
| 47 #pragma mark - |
| 48 #pragma mark Initialisers |
| 49 |
| 50 - (id)initWithFrame:(NSRect)frame { |
| 51 NOTREACHED(); |
| 52 return nil; |
| 53 } |
| 54 |
| 55 - (id)initWithController: |
| 56 (autofill::PasswordGenerationPopupController*)controller |
| 57 frame:(NSRect)frame { |
| 58 self = [super initWithDelegate:controller frame:frame]; |
| 59 if (self) |
| 60 controller_ = controller; |
| 61 |
| 62 return self; |
| 63 } |
| 64 |
| 65 #pragma mark - |
| 66 #pragma mark NSView implementation: |
| 67 |
| 68 - (void)drawRect:(NSRect)dirtyRect { |
| 69 // If the view is in the process of being destroyed, don't bother drawing. |
| 70 if (!controller_) |
| 71 return; |
| 72 |
| 73 [self drawBackgroundAndBorder]; |
| 74 |
| 75 NSRect bounds = [self bounds]; |
| 76 bounds.origin.y += autofill::kPopupBorderThickness; |
| 77 |
| 78 if (controller_->password_selected()) { |
| 79 // Draw a highlight under the suggested password. |
| 80 NSRect highlightBounds = |
| 81 NSRectFromCGRect(controller_->password_bounds().ToCGRect()); |
| 82 highlightBounds.origin.y += |
| 83 PasswordGenerationPopupView::kPasswordVerticalInset; |
| 84 highlightBounds.size.height -= |
| 85 2 * PasswordGenerationPopupView::kPasswordVerticalInset; |
| 86 [[self highlightColor] set]; |
| 87 [NSBezierPath fillRect:highlightBounds]; |
| 88 } |
| 89 |
| 90 NSFont* font = controller_->font_list().GetPrimaryFont().GetNativeFont(); |
| 91 NSRect passwordBounds = |
| 92 NSRectFromCGRect(controller_->password_bounds().ToCGRect()); |
| 93 |
| 94 BOOL isRTL = NO; // TODO(dubroy): Implement RTL support. |
| 95 [self drawText:base::SysUTF16ToNSString(controller_->password()) |
| 96 withFont:font |
| 97 color:[self nameColor] |
| 98 bounds:passwordBounds |
| 99 alignment:isRTL ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT]; |
| 100 |
| 101 [self drawText:base::SysUTF16ToNSString(controller_->SuggestedText()) |
| 102 withFont:font |
| 103 color:[self subtextColor] |
| 104 bounds:passwordBounds |
| 105 alignment:isRTL ? gfx::ALIGN_LEFT : gfx::ALIGN_RIGHT]; |
| 106 |
| 107 // Render the background of the help text. |
| 108 NSRect helpBounds = |
| 109 NSRectFromCGRect(controller_->help_bounds().ToCGRect()); |
| 110 [HelpTextBackgroundColor() set]; |
| 111 [NSBezierPath fillRect:helpBounds]; |
| 112 |
| 113 // Render the divider. |
| 114 NSRect helpBorder = helpBounds; |
| 115 helpBorder.size.height = 1; |
| 116 [DividerColor() set]; |
| 117 [NSBezierPath fillRect:helpBorder]; |
| 118 |
| 119 // Adjust |helpBounds| so that the divider is not included when calculating |
| 120 // where the text should be rendered. |
| 121 helpBounds.origin.x += 1; |
| 122 helpBounds.size.height -= 1; |
| 123 |
| 124 // Render the help text. |
| 125 [self drawText:base::SysUTF16ToNSString(controller_->HelpText()) |
| 126 withFont:font |
| 127 color:HelpTextColor() |
| 128 bounds:helpBounds |
| 129 alignment:isRTL ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT]; |
| 130 } |
| 131 |
| 132 - (void)drawText:(NSString*)text |
| 133 withFont:(NSFont*)font |
| 134 color:(id)color |
| 135 bounds:(NSRect)bounds |
| 136 alignment:(gfx::HorizontalAlignment)alignment { |
| 137 NSDictionary* textAttributes = @{ |
| 138 NSFontAttributeName: font, |
| 139 NSForegroundColorAttributeName: color, |
| 140 }; |
| 141 // Adjust horizontal padding before measuring. |
| 142 bounds.size.width -= 2 * controller_->kHorizontalPadding; |
| 143 bounds.origin.x += controller_->kHorizontalPadding; |
| 144 |
| 145 NSSize textSize = |
| 146 [text boundingRectWithSize:bounds.size |
| 147 options:NSStringDrawingUsesLineFragmentOrigin |
| 148 attributes:textAttributes].size; |
| 149 |
| 150 // Center the text vertically within the bounds. |
| 151 bounds.origin.y = NSMinY(bounds) + (NSHeight(bounds) - textSize.height) / 2; |
| 152 |
| 153 if (alignment == gfx::ALIGN_RIGHT) |
| 154 bounds.origin.x += NSWidth(bounds) - textSize.width; |
| 155 |
| 156 [text drawInRect:bounds withAttributes:textAttributes]; |
| 157 } |
| 158 |
| 159 #pragma mark - |
| 160 #pragma mark Public API: |
| 161 |
| 162 - (void)controllerDestroyed { |
| 163 controller_ = NULL; |
| 164 [super delegateDestroyed]; |
| 165 } |
| 166 |
| 167 @end |
OLD | NEW |