1 // Copyright 2013 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/browser/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 = {
68 IDR_MANAGED_USER_LABEL_TOP_LEFT,
69 IDR_MANAGED_USER_LABEL_TOP,
70 IDR_MANAGED_USER_LABEL_TOP_RIGHT,
71 IDR_MANAGED_USER_LABEL_LEFT,
72 IDR_MANAGED_USER_LABEL_CENTER,
73 IDR_MANAGED_USER_LABEL_RIGHT,
74 IDR_MANAGED_USER_LABEL_BOTTOM_LEFT,
75 IDR_MANAGED_USER_LABEL_BOTTOM,
76 IDR_MANAGED_USER_LABEL_BOTTOM_RIGHT
78 ui::DrawNinePartImage(frame, imageIds, NSCompositeSourceOver, 1.0, true);
81 - (NSRect)titleRectForBounds:(NSRect)theRect {
82 theRect.origin = NSMakePoint(kLabelTextLeftSpacing, kLabelTextBottomSpacing);
83 theRect.size = [[self attributedTitle] size];
87 - (NSRect)drawTitle:(NSAttributedString*)title
88 withFrame:(NSRect)frame
89 inView:(NSView*)controlView {
90 base::scoped_nsobject<NSMutableAttributedString> themedTitle(
91 [[NSMutableAttributedString alloc] initWithAttributedString:title]);
92 ui::ThemeProvider* themeProvider = [[controlView window] themeProvider];
94 NSColor* textColor = themeProvider->GetNSColor(
95 ThemeProperties::COLOR_MANAGED_USER_LABEL);
96 [themedTitle addAttribute:NSForegroundColorAttributeName
98 range:NSMakeRange(0, title.length)];
100 [themedTitle drawInRect:frame];
104 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
105 ui::ThemeProvider* themeProvider = [[controlView window] themeProvider];
107 // Draw the label button background using the color provided by
108 // |themeProvider|. First paint the border.
109 NSColor* borderColor = themeProvider->GetNSColor(
110 ThemeProperties::COLOR_MANAGED_USER_LABEL_BORDER);
111 if ([self isHighlighted]) {
112 borderColor = [borderColor blendedColorWithFraction:0.5
113 ofColor:[NSColor blackColor]];
115 NSSize frameSize = cellFrame.size;
116 NSRect backgroundRect;
117 backgroundRect.origin = NSMakePoint(1, 1);
118 backgroundRect.size = NSMakeSize(frameSize.width - 2, frameSize.height - 2);
120 [NSBezierPath bezierPathWithRoundedRect:backgroundRect
126 // Now paint the background.
127 NSColor* backgroundColor = themeProvider->GetNSColor(
128 ThemeProperties::COLOR_MANAGED_USER_LABEL_BACKGROUND);
129 if ([self isHighlighted]) {
131 [backgroundColor blendedColorWithFraction:0.5
132 ofColor:[NSColor blackColor]];
134 backgroundRect.origin = NSMakePoint(2, 2);
135 backgroundRect.size = NSMakeSize(frameSize.width - 4, frameSize.height - 4);
136 path = [NSBezierPath bezierPathWithRoundedRect:backgroundRect
139 [backgroundColor set];
142 [super drawInteriorWithFrame:cellFrame inView:controlView];