1 // Copyright 2015 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.
5 #import "chrome/browser/ui/cocoa/passwords/credential_item_view.h"
9 #include "base/i18n/rtl.h"
10 #include "base/mac/foundation_util.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h"
13 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h"
14 #include "grit/theme_resources.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/image/image_skia.h"
17 #include "ui/gfx/image/image_skia_util_mac.h"
20 const CGFloat kHorizontalPaddingBetweenAvatarAndLabels = 10.0f;
21 const CGFloat kVerticalPaddingBetweenLabels = 2.0f;
24 @interface CredentialItemView()
25 @property(nonatomic, readonly) NSTextField* nameLabel;
26 @property(nonatomic, readonly) NSTextField* usernameLabel;
27 @property(nonatomic, readonly) NSImageView* avatarView;
30 @implementation CredentialItemView
32 @synthesize nameLabel = nameLabel_;
33 @synthesize usernameLabel = usernameLabel_;
34 @synthesize avatarView = avatarView_;
35 @synthesize passwordForm = passwordForm_;
36 @synthesize credentialType = credentialType_;
38 - (id)initWithPasswordForm:(const autofill::PasswordForm&)passwordForm
39 credentialType:(password_manager::CredentialType)credentialType
40 delegate:(id<CredentialItemDelegate>)delegate {
41 if ((self = [super init])) {
42 passwordForm_ = passwordForm;
43 credentialType_ = credentialType;
46 // -----------------------------------------------
47 // | | John Q. Facebooker |
48 // | icon | john@somewhere.com |
49 // -----------------------------------------------
53 avatarView_ = [[[NSImageView alloc] initWithFrame:NSZeroRect] autorelease];
54 [avatarView_ setWantsLayer:YES];
55 [[avatarView_ layer] setCornerRadius:kAvatarImageSize / 2.0f];
56 [[avatarView_ layer] setMasksToBounds:YES];
57 [self addSubview:avatarView_];
59 if (!passwordForm_.display_name.empty()) {
60 nameLabel_ = [[[NSTextField alloc] initWithFrame:NSZeroRect] autorelease];
61 [self addSubview:nameLabel_];
62 [nameLabel_ setBezeled:NO];
63 [nameLabel_ setDrawsBackground:NO];
64 [nameLabel_ setEditable:NO];
65 [nameLabel_ setSelectable:NO];
67 setStringValue:base::SysUTF16ToNSString(passwordForm_.display_name)];
68 [nameLabel_ setAlignment:base::i18n::IsRTL() ? NSRightTextAlignment
69 : NSLeftTextAlignment];
70 [nameLabel_ sizeToFit];
74 [[[NSTextField alloc] initWithFrame:NSZeroRect] autorelease];
75 [self addSubview:usernameLabel_];
76 [usernameLabel_ setBezeled:NO];
77 [usernameLabel_ setDrawsBackground:NO];
78 [usernameLabel_ setEditable:NO];
79 [usernameLabel_ setSelectable:NO];
81 setStringValue:base::SysUTF16ToNSString(passwordForm_.username_value)];
82 [usernameLabel_ setAlignment:base::i18n::IsRTL() ? NSRightTextAlignment
83 : NSLeftTextAlignment];
84 [usernameLabel_ sizeToFit];
86 // Compute the heights and widths of everything, as the layout depends on
87 // these measurements.
88 const CGFloat labelsHeight = NSHeight([nameLabel_ frame]) +
89 NSHeight([usernameLabel_ frame]) +
90 kVerticalPaddingBetweenLabels;
91 const CGFloat height = std::max(labelsHeight, CGFloat(kAvatarImageSize));
93 kAvatarImageSize + kHorizontalPaddingBetweenAvatarAndLabels +
94 std::max(NSWidth([nameLabel_ frame]), NSWidth([usernameLabel_ frame]));
95 self.frame = NSMakeRect(0, 0, width, height);
97 // Lay out the views (RTL reverses the order horizontally).
99 const CGFloat avatarX = base::i18n::IsRTL() ? width - kAvatarImageSize : 0;
100 const CGFloat avatarY =
101 (kAvatarImageSize > height) ? 0 : (height - kAvatarImageSize) / 2.0f;
102 [avatarView_ setFrame:NSMakeRect(avatarX, avatarY, kAvatarImageSize,
105 const CGFloat usernameX =
107 ? NSMinX([avatarView_ frame]) -
108 kHorizontalPaddingBetweenAvatarAndLabels -
109 NSWidth([usernameLabel_ frame])
110 : NSMaxX([avatarView_ frame]) +
111 kHorizontalPaddingBetweenAvatarAndLabels;
112 const CGFloat usernameLabelY =
113 (labelsHeight > height) ? 0 : (height - labelsHeight) / 2.0f;
114 NSRect usernameFrame = [usernameLabel_ frame];
115 usernameFrame.origin = NSMakePoint(usernameX, usernameLabelY);
116 [usernameLabel_ setFrame:usernameFrame];
118 const CGFloat nameX = base::i18n::IsRTL()
119 ? NSMinX([avatarView_ frame]) -
120 kHorizontalPaddingBetweenAvatarAndLabels -
121 NSWidth([nameLabel_ frame])
122 : NSMaxX([avatarView_ frame]) +
123 kHorizontalPaddingBetweenAvatarAndLabels;
124 const CGFloat nameLabelY =
125 NSMaxY(usernameFrame) + kVerticalPaddingBetweenLabels;
126 NSRect nameFrame = [nameLabel_ frame];
127 nameFrame.origin = NSMakePoint(nameX, nameLabelY);
128 [nameLabel_ setFrame:nameFrame];
130 // Use a default avatar and fetch the custom one, if it exists.
131 [self updateAvatar:[[self class] defaultAvatar]];
132 if (passwordForm_.avatar_url.is_valid())
133 [delegate_ fetchAvatar:passwordForm_.avatar_url forView:self];
135 // When resizing, stick to the left (resp. right for RTL) edge.
136 const NSUInteger autoresizingMask =
137 (base::i18n::IsRTL() ? NSViewMinXMargin : NSViewMaxXMargin);
138 [avatarView_ setAutoresizingMask:autoresizingMask];
139 [usernameLabel_ setAutoresizingMask:autoresizingMask];
140 [nameLabel_ setAutoresizingMask:autoresizingMask];
141 [self setAutoresizingMask:NSViewWidthSizable];
147 - (void)updateAvatar:(NSImage*)avatar {
148 [avatarView_ setImage:avatar];
151 + (NSImage*)defaultAvatar {
152 return gfx::NSImageFromImageSkia(ScaleImageForAccountAvatar(
153 *ResourceBundle::GetSharedInstance()
154 .GetImageNamed(IDR_PROFILE_AVATAR_PLACEHOLDER_LARGE)