[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / profiles / avatar_label_button.mm
blob881366813d2eb778b3034b59fba36d6faa0482f5
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"
16 namespace {
18 // Space between the left edge of the label background and the left edge of the
19 // label text.
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
26 // label text.
27 const CGFloat kLabelTextTopSpacing = 3;
29 // Space between the bottom edge of the label background and the bottom edge of
30 // the label text.
31 const CGFloat kLabelTextBottomSpacing = 4;
33 }  // namespace
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];
47   }
48   return self;
51 + (Class)cellClass {
52   return [AvatarLabelButtonCell class];
55 @end
57 @implementation AvatarLabelButtonCell
59 - (NSSize)labelTextSize {
60   NSSize size = [[self attributedTitle] size];
61   size.width += kLabelTextLeftSpacing + kLabelTextRightSpacing;
62   size.height += kLabelTextTopSpacing + kLabelTextBottomSpacing;
63   return size;
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];
74   return theRect;
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];
83   if (themeProvider) {
84     NSColor* textColor = themeProvider->GetNSColor(
85         ThemeProperties::COLOR_MANAGED_USER_LABEL);
86     [themedTitle addAttribute:NSForegroundColorAttributeName
87                         value:textColor
88                         range:NSMakeRange(0, title.length)];
89   }
90   [themedTitle drawInRect:frame];
91   return frame;
94 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
95   ui::ThemeProvider* themeProvider = [[controlView window] themeProvider];
96   if (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]];
104     }
105     NSSize frameSize = cellFrame.size;
106     NSRect backgroundRect;
107     backgroundRect.origin = NSMakePoint(1, 1);
108     backgroundRect.size = NSMakeSize(frameSize.width - 2, frameSize.height - 2);
109     NSBezierPath* path =
110         [NSBezierPath bezierPathWithRoundedRect:backgroundRect
111                                         xRadius:2.0
112                                         yRadius:2.0];
113     [borderColor set];
114     [path fill];
116     // Now paint the background.
117     NSColor* backgroundColor = themeProvider->GetNSColor(
118         ThemeProperties::COLOR_MANAGED_USER_LABEL_BACKGROUND);
119     if ([self isHighlighted]) {
120       backgroundColor =
121           [backgroundColor blendedColorWithFraction:0.5
122                                             ofColor:[NSColor blackColor]];
123     }
124     backgroundRect.origin = NSMakePoint(2, 2);
125     backgroundRect.size = NSMakeSize(frameSize.width - 4, frameSize.height - 4);
126     path = [NSBezierPath bezierPathWithRoundedRect:backgroundRect
127                                            xRadius:2.0
128                                            yRadius:2.0];
129     [backgroundColor set];
130     [path fill];
131   }
132   [super drawInteriorWithFrame:cellFrame inView:controlView];
135 @end