Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(749)

Side by Side Diff: chrome/browser/ui/cocoa/passwords/credential_item_view.mm

Issue 878743007: Implement CredentialItemView and tests for the Mac account chooser. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix order Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #import "chrome/browser/ui/cocoa/passwords/credential_item_view.h"
6
7 #include <algorithm>
8
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"
18
19 namespace {
20 const CGFloat kHorizontalPaddingBetweenAvatarAndLabels = 10.0f;
21 const CGFloat kVerticalPaddingBetweenLabels = 2.0f;
22 } // namespace
23
24 @interface CredentialItemView()
25 @property(nonatomic, readonly) NSTextField* nameLabel;
26 @property(nonatomic, readonly) NSTextField* usernameLabel;
27 @property(nonatomic, readonly) NSImageView* avatarView;
28 @end
29
30 @implementation CredentialItemView
31
32 @synthesize passwordForm = passwordForm_;
33 @synthesize credentialType = credentialType_;
34
35 - (id)initWithPasswordForm:(const autofill::PasswordForm&)passwordForm
36 credentialType:(password_manager::CredentialType)credentialType
37 delegate:(id<CredentialItemDelegate>)delegate {
38 if ((self = [super init])) {
39 passwordForm_ = passwordForm;
40 credentialType_ = credentialType;
41 delegate_ = delegate;
42
43 // -----------------------------------------------
44 // | | John Q. Facebooker |
45 // | icon | john@somewhere.com |
46 // -----------------------------------------------
47
48 // Create the views.
49
50 avatarView_.reset([[NSImageView alloc] initWithFrame:NSZeroRect]);
51 [avatarView_ setWantsLayer:YES];
52 [[avatarView_ layer] setCornerRadius:kAvatarImageSize / 2.0f];
53 [[avatarView_ layer] setMasksToBounds:YES];
54 [self addSubview:avatarView_];
55
56 if (!passwordForm_.display_name.empty()) {
57 nameLabel_.reset([[NSTextField alloc] initWithFrame:NSZeroRect]);
58 [self addSubview:nameLabel_];
59 [nameLabel_ setBezeled:NO];
60 [nameLabel_ setDrawsBackground:NO];
61 [nameLabel_ setEditable:NO];
62 [nameLabel_ setSelectable:NO];
63 [nameLabel_
64 setStringValue:base::SysUTF16ToNSString(passwordForm_.display_name)];
65 [nameLabel_ setAlignment:base::i18n::IsRTL() ? NSRightTextAlignment
66 : NSLeftTextAlignment];
67 [nameLabel_ sizeToFit];
68 }
69
70 usernameLabel_.reset([[NSTextField alloc] initWithFrame:NSZeroRect]);
71 [self addSubview:usernameLabel_];
72 [usernameLabel_ setBezeled:NO];
73 [usernameLabel_ setDrawsBackground:NO];
74 [usernameLabel_ setEditable:NO];
75 [usernameLabel_ setSelectable:NO];
76 [usernameLabel_
77 setStringValue:base::SysUTF16ToNSString(passwordForm_.username_value)];
78 [usernameLabel_ setAlignment:base::i18n::IsRTL() ? NSRightTextAlignment
79 : NSLeftTextAlignment];
80 [usernameLabel_ sizeToFit];
81
82 // Compute the heights and widths of everything, as the layout depends on
83 // these measurements.
84 const CGFloat labelsHeight = NSHeight([nameLabel_ frame]) +
85 NSHeight([usernameLabel_ frame]) +
86 kVerticalPaddingBetweenLabels;
87 const CGFloat height = std::max(labelsHeight, CGFloat(kAvatarImageSize));
88 const CGFloat width =
89 kAvatarImageSize + kHorizontalPaddingBetweenAvatarAndLabels +
90 std::max(NSWidth([nameLabel_ frame]), NSWidth([usernameLabel_ frame]));
91 self.frame = NSMakeRect(0, 0, width, height);
92
93 // Lay out the views (RTL reverses the order horizontally).
94
95 const CGFloat avatarX = base::i18n::IsRTL() ? width - kAvatarImageSize : 0;
96 const CGFloat avatarY =
97 (kAvatarImageSize > height) ? 0 : (height - kAvatarImageSize) / 2.0f;
98 [avatarView_ setFrame:NSMakeRect(avatarX, avatarY, kAvatarImageSize,
99 kAvatarImageSize)];
100
101 const CGFloat usernameX =
102 base::i18n::IsRTL()
103 ? NSMinX([avatarView_ frame]) -
104 kHorizontalPaddingBetweenAvatarAndLabels -
105 NSWidth([usernameLabel_ frame])
106 : NSMaxX([avatarView_ frame]) +
107 kHorizontalPaddingBetweenAvatarAndLabels;
108 const CGFloat usernameLabelY =
109 (labelsHeight > height) ? 0 : (height - labelsHeight) / 2.0f;
110 NSRect usernameFrame = [usernameLabel_ frame];
111 usernameFrame.origin = NSMakePoint(usernameX, usernameLabelY);
112 [usernameLabel_ setFrame:usernameFrame];
113
114 const CGFloat nameX = base::i18n::IsRTL()
115 ? NSMinX([avatarView_ frame]) -
116 kHorizontalPaddingBetweenAvatarAndLabels -
117 NSWidth([nameLabel_ frame])
118 : NSMaxX([avatarView_ frame]) +
119 kHorizontalPaddingBetweenAvatarAndLabels;
120 const CGFloat nameLabelY =
121 NSMaxY(usernameFrame) + kVerticalPaddingBetweenLabels;
122 NSRect nameFrame = [nameLabel_ frame];
123 nameFrame.origin = NSMakePoint(nameX, nameLabelY);
124 [nameLabel_ setFrame:nameFrame];
125
126 // Use a default avatar and fetch the custom one, if it exists.
127 [self updateAvatar:[[self class] defaultAvatar]];
128 if (passwordForm_.avatar_url.is_valid())
129 [delegate_ fetchAvatar:passwordForm_.avatar_url forView:self];
130
131 // When resizing, stick to the left (resp. right for RTL) edge.
132 const NSUInteger autoresizingMask =
133 (base::i18n::IsRTL() ? NSViewMinXMargin : NSViewMaxXMargin);
134 [avatarView_ setAutoresizingMask:autoresizingMask];
135 [usernameLabel_ setAutoresizingMask:autoresizingMask];
136 [nameLabel_ setAutoresizingMask:autoresizingMask];
137 [self setAutoresizingMask:NSViewWidthSizable];
138 }
139
140 return self;
141 }
142
143 - (void)updateAvatar:(NSImage*)avatar {
144 [avatarView_ setImage:avatar];
145 }
146
147 + (NSImage*)defaultAvatar {
148 return gfx::NSImageFromImageSkia(ScaleImageForAccountAvatar(
149 *ResourceBundle::GetSharedInstance()
150 .GetImageNamed(IDR_PROFILE_AVATAR_PLACEHOLDER_LARGE)
151 .ToImageSkia()));
152 }
153
154 - (NSTextField*)nameLabel {
155 return nameLabel_.get();
156 }
157
158 - (NSTextField*)usernameLabel {
159 return usernameLabel_.get();
160 }
161
162 - (NSImageView*)avatarView {
163 return avatarView_.get();
164 }
165
166 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698