1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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/passwords/credential_item_button.h"
#include "base/i18n/rtl.h"
#import "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/ui/cocoa/passwords/passwords_bubble_utils.h"
#include "chrome/browser/ui/passwords/manage_passwords_view_utils.h"
#include "chrome/grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_util_mac.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
namespace {
constexpr CGFloat kFocusRingInset = 3;
constexpr CGFloat kHorizontalPaddingBetweenAvatarAndLabel = 10;
} // namespace
// Custom button cell that adds a left padding before the avatar, and a custom
// spacing between the avatar and label.
@interface CredentialItemButtonCell : NSButtonCell {
@private
// Padding added to the left margin of the button.
int marginSpacing_;
// Spacing between the cell image and title.
int imageTitleSpacing_;
}
- (id)initWithMarginSpacing:(int)leftMarginSpacing
imageTitleSpacing:(int)imageTitleSpacing;
@end
@implementation CredentialItemButtonCell
- (id)initWithMarginSpacing:(int)marginSpacing
imageTitleSpacing:(int)imageTitleSpacing {
if ((self = [super init])) {
marginSpacing_ = marginSpacing;
imageTitleSpacing_ = imageTitleSpacing;
}
return self;
}
- (NSRect)drawTitle:(NSAttributedString*)title
withFrame:(NSRect)frame
inView:(NSView*)controlView {
// The title frame origin isn't aware of the left margin spacing added
// in -drawImage, so it must be added when drawing the title.
NSRect marginRect;
NSDivideRect(frame, &marginRect, &frame, marginSpacing_, NSMinXEdge);
NSDivideRect(frame, &marginRect, &frame, imageTitleSpacing_,
base::i18n::IsRTL() ? NSMaxXEdge : NSMinXEdge);
NSDivideRect(frame, &marginRect, &frame, marginSpacing_, NSMaxXEdge);
return [super drawTitle:title withFrame:frame inView:controlView];
}
- (void)drawImage:(NSImage*)image
withFrame:(NSRect)frame
inView:(NSView*)controlView {
if (base::i18n::IsRTL())
frame.origin.x -= marginSpacing_;
else
frame.origin.x += marginSpacing_;
gfx::ScopedNSGraphicsContextSaveGState scopedGState;
NSBezierPath* path = [NSBezierPath bezierPathWithOvalInRect:frame];
[path addClip];
[super drawImage:image withFrame:frame inView:controlView];
}
- (NSSize)cellSize {
NSSize buttonSize = [super cellSize];
buttonSize.width += imageTitleSpacing_;
buttonSize.width += 2 * marginSpacing_;
return buttonSize;
}
- (void)drawFocusRingMaskWithFrame:(NSRect)cellFrame
inView:(NSView *)controlView {
NSRect focusRingRect =
NSInsetRect(cellFrame, kFocusRingInset, kFocusRingInset);
[[NSBezierPath bezierPathWithRoundedRect:focusRingRect
xRadius:2
yRadius:2] fill];
}
@end
@interface CredentialItemButton () {
base::scoped_nsobject<NSColor> backgroundColor_;
base::scoped_nsobject<NSColor> hoverColor_;
}
@end
@implementation CredentialItemButton
@synthesize passwordForm = passwordForm_;
@synthesize credentialType = credentialType_;
- (id)initWithFrame:(NSRect)frameRect
backgroundColor:(NSColor*)backgroundColor
hoverColor:(NSColor*)hoverColor {
if ((self = [super initWithFrame:frameRect])) {
backgroundColor_.reset([backgroundColor retain]);
hoverColor_.reset([hoverColor retain]);
base::scoped_nsobject<CredentialItemButtonCell> cell(
[[CredentialItemButtonCell alloc]
initWithMarginSpacing:kFramePadding
imageTitleSpacing:kHorizontalPaddingBetweenAvatarAndLabel]);
[cell setLineBreakMode:NSLineBreakByTruncatingTail];
[cell setHighlightsBy:NSChangeGrayCellMask];
[self setCell:cell.get()];
[self setBordered:NO];
[self setFont:ResourceBundle::GetSharedInstance()
.GetFontList(ResourceBundle::SmallFont)
.GetPrimaryFont()
.GetNativeFont()];
[self setButtonType:NSMomentaryLightButton];
[self setImagePosition:base::i18n::IsRTL() ? NSImageRight : NSImageLeft];
[self setAlignment:NSNaturalTextAlignment];
}
return self;
}
+ (NSImage*)defaultAvatar {
return gfx::NSImageFromImageSkia(ScaleImageForAccountAvatar(
*ResourceBundle::GetSharedInstance()
.GetImageNamed(IDR_PROFILE_AVATAR_PLACEHOLDER_LARGE)
.ToImageSkia()));
}
- (void)setHoverState:(HoverState)state {
[super setHoverState:state];
bool isHighlighted = ([self hoverState] != kHoverStateNone);
NSColor* backgroundColor = isHighlighted ? hoverColor_ : backgroundColor_;
[[self cell] setBackgroundColor:backgroundColor];
}
- (BOOL)canBecomeKeyView {
return YES;
}
@end
|