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

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: memory 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 nameLabel = nameLabel_;
33 @synthesize usernameLabel = usernameLabel_;
34 @synthesize avatarView = avatarView_;
35 @synthesize passwordForm = passwordForm_;
36 @synthesize credentialType = credentialType_;
37
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;
44 delegate_ = delegate;
45
46 // -----------------------------------------------
47 // | | John Q. Facebooker |
48 // | icon | john@somewhere.com |
49 // -----------------------------------------------
50
51 // Create the views.
52
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_];
58
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];
66 [nameLabel_
67 setStringValue:base::SysUTF16ToNSString(passwordForm_.display_name)];
68 [nameLabel_ setAlignment:base::i18n::IsRTL() ? NSRightTextAlignment
69 : NSLeftTextAlignment];
70 [nameLabel_ sizeToFit];
71 }
72
73 usernameLabel_ =
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];
80 [usernameLabel_
81 setStringValue:base::SysUTF16ToNSString(passwordForm_.username_value)];
82 [usernameLabel_ setAlignment:base::i18n::IsRTL() ? NSRightTextAlignment
83 : NSLeftTextAlignment];
84 [usernameLabel_ sizeToFit];
85
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));
92 const CGFloat width =
93 kAvatarImageSize + kHorizontalPaddingBetweenAvatarAndLabels +
94 std::max(NSWidth([nameLabel_ frame]), NSWidth([usernameLabel_ frame]));
95 self.frame = NSMakeRect(0, 0, width, height);
96
97 // Lay out the views (RTL reverses the order horizontally).
98
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,
103 kAvatarImageSize)];
104
105 const CGFloat usernameX =
106 base::i18n::IsRTL()
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];
117
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];
129
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];
134
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];
142 }
143
144 return self;
145 }
146
147 - (void)updateAvatar:(NSImage*)avatar {
148 [avatarView_ setImage:avatar];
149 }
150
151 + (NSImage*)defaultAvatar {
152 return gfx::NSImageFromImageSkia(ScaleImageForAccountAvatar(
153 *ResourceBundle::GetSharedInstance()
154 .GetImageNamed(IDR_PROFILE_AVATAR_PLACEHOLDER_LARGE)
155 .ToImageSkia()));
156 }
157
158 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698