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.
5 #import "chrome/browser/ui/cocoa/profiles/avatar_label_button.h"
7 #include "chrome/browser/themes/theme_properties.h"
8 #include "chrome/browser/ui/cocoa/themed_window.h"
9 #include "grit/generated_resources.h"
10 #include "grit/theme_resources.h"
11 #include "ui/base/cocoa/appkit_utils.h"
12 #include "ui/base/l10n/l10n_util_mac.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/base/theme_provider.h"
18 // Space between the left edge of the label background and the left edge of the
20 const CGFloat kLabelTextLeftSpacing = 10;
22 // Space between the right edge of the label text and the avatar icon.
23 const CGFloat kLabelTextRightSpacing = 4;
25 // Space between the top edge of the label background and the top edge of the
27 const CGFloat kLabelTextTopSpacing = 3;
29 // Space between the bottom edge of the label background and the bottom edge of
31 const CGFloat kLabelTextBottomSpacing = 4;
35 @implementation AvatarLabelButton
37 - (id)initWithFrame:(NSRect)frameRect {
38 if ((self = [super initWithFrame:frameRect])) {
39 [self setBezelStyle:NSSmallSquareBezelStyle];
40 [self setTitle:l10n_util::GetNSString(IDS_MANAGED_USER_AVATAR_LABEL)];
41 [self setFont:[NSFont labelFontOfSize:12.0]];
42 // Increase the frame by the size of the label to be displayed.
43 NSSize textSize = [[self cell] labelTextSize];
44 frameRect.size = NSMakeSize(frameRect.size.width + textSize.width,
45 frameRect.size.height + textSize.height);
46 [self setFrame:frameRect];
52 return [AvatarLabelButtonCell class];
57 @implementation AvatarLabelButtonCell
59 - (NSSize)labelTextSize {
60 NSSize size = [[self attributedTitle] size];
61 size.width += kLabelTextLeftSpacing + kLabelTextRightSpacing;
62 size.height += kLabelTextTopSpacing + kLabelTextBottomSpacing;
66 - (void)drawBezelWithFrame:(NSRect)frame inView:(NSView*)controlView {
67 ui::NinePartImageIds imageIds = IMAGE_GRID(IDR_MANAGED_USER_LABEL);
68 ui::DrawNinePartImage(frame, imageIds, NSCompositeSourceOver, 1.0, true);
71 - (NSRect)titleRectForBounds:(NSRect)theRect {
72 theRect.origin = NSMakePoint(kLabelTextLeftSpacing, kLabelTextBottomSpacing);
73 theRect.size = [[self attributedTitle] size];
77 - (NSRect)drawTitle:(NSAttributedString*)title
78 withFrame:(NSRect)frame
79 inView:(NSView*)controlView {
80 base::scoped_nsobject<NSMutableAttributedString> themedTitle(
81 [[NSMutableAttributedString alloc] initWithAttributedString:title]);
82 ui::ThemeProvider* themeProvider = [[controlView window] themeProvider];
84 NSColor* textColor = themeProvider->GetNSColor(
85 ThemeProperties::COLOR_MANAGED_USER_LABEL);
86 [themedTitle addAttribute:NSForegroundColorAttributeName
88 range:NSMakeRange(0, title.length)];
90 [themedTitle drawInRect:frame];
94 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
95 ui::ThemeProvider* themeProvider = [[controlView window] themeProvider];
97 // Draw the label button background using the color provided by
98 // |themeProvider|. First paint the border.
99 NSColor* borderColor = themeProvider->GetNSColor(
100 ThemeProperties::COLOR_MANAGED_USER_LABEL_BORDER);
101 if ([self isHighlighted]) {
102 borderColor = [borderColor blendedColorWithFraction:0.5
103 ofColor:[NSColor blackColor]];
105 NSSize frameSize = cellFrame.size;
106 NSRect backgroundRect;
107 backgroundRect.origin = NSMakePoint(1, 1);
108 backgroundRect.size = NSMakeSize(frameSize.width - 2, frameSize.height - 2);
110 [NSBezierPath bezierPathWithRoundedRect:backgroundRect
116 // Now paint the background.
117 NSColor* backgroundColor = themeProvider->GetNSColor(
118 ThemeProperties::COLOR_MANAGED_USER_LABEL_BACKGROUND);
119 if ([self isHighlighted]) {
121 [backgroundColor blendedColorWithFraction:0.5
122 ofColor:[NSColor blackColor]];
124 backgroundRect.origin = NSMakePoint(2, 2);
125 backgroundRect.size = NSMakeSize(frameSize.width - 4, frameSize.height - 4);
126 path = [NSBezierPath bezierPathWithRoundedRect:backgroundRect
129 [backgroundColor set];
132 [super drawInteriorWithFrame:cellFrame inView:controlView];