Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / profiles / avatar_label_button.mm
blobc372cb5da0fc56112f9bfff6ae3adc9747ed9329
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 "chrome/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/theme_provider.h"
15 namespace {
17 // Space between the left edge of the label background and the left edge of the
18 // label text.
19 const CGFloat kLabelTextLeftSpacing = 10;
21 // Space between the right edge of the label text and the avatar icon.
22 const CGFloat kLabelTextRightSpacing = 4;
24 // Space between the top edge of the label background and the top edge of the
25 // label text.
26 const CGFloat kLabelTextTopSpacing = 3;
28 // Space between the bottom edge of the label background and the bottom edge of
29 // the label text.
30 const CGFloat kLabelTextBottomSpacing = 4;
32 }  // namespace
34 @implementation AvatarLabelButton
36 - (id)initWithFrame:(NSRect)frameRect {
37   if ((self = [super initWithFrame:frameRect])) {
38     [self setBezelStyle:NSSmallSquareBezelStyle];
39     [self setTitle:l10n_util::GetNSString(IDS_SUPERVISED_USER_AVATAR_LABEL)];
40     [self setFont:[NSFont labelFontOfSize:12.0]];
41     // Increase the frame by the size of the label to be displayed.
42     NSSize textSize = [[self cell] labelTextSize];
43     frameRect.size = NSMakeSize(frameRect.size.width + textSize.width,
44                                 frameRect.size.height + textSize.height);
45     [self setFrame:frameRect];
46   }
47   return self;
50 + (Class)cellClass {
51   return [AvatarLabelButtonCell class];
54 @end
56 @implementation AvatarLabelButtonCell
58 - (NSSize)labelTextSize {
59   NSSize size = [[self attributedTitle] size];
60   size.width += kLabelTextLeftSpacing + kLabelTextRightSpacing;
61   size.height += kLabelTextTopSpacing + kLabelTextBottomSpacing;
62   return size;
65 - (void)drawBezelWithFrame:(NSRect)frame inView:(NSView*)controlView {
66   ui::NinePartImageIds imageIds = IMAGE_GRID(IDR_SUPERVISED_USER_LABEL);
67   ui::DrawNinePartImage(frame, imageIds, NSCompositeSourceOver, 1.0, true);
70 - (NSRect)titleRectForBounds:(NSRect)theRect {
71   theRect.origin = NSMakePoint(kLabelTextLeftSpacing, kLabelTextBottomSpacing);
72   theRect.size = [[self attributedTitle] size];
73   return theRect;
76 - (NSRect)drawTitle:(NSAttributedString*)title
77           withFrame:(NSRect)frame
78              inView:(NSView*)controlView {
79   base::scoped_nsobject<NSMutableAttributedString> themedTitle(
80       [[NSMutableAttributedString alloc] initWithAttributedString:title]);
81   ui::ThemeProvider* themeProvider = [[controlView window] themeProvider];
82   if (themeProvider) {
83     NSColor* textColor = themeProvider->GetNSColor(
84         ThemeProperties::COLOR_SUPERVISED_USER_LABEL);
85     [themedTitle addAttribute:NSForegroundColorAttributeName
86                         value:textColor
87                         range:NSMakeRange(0, title.length)];
88   }
89   [themedTitle drawInRect:frame];
90   return frame;
93 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
94   ui::ThemeProvider* themeProvider = [[controlView window] themeProvider];
95   if (themeProvider) {
96     // Draw the label button background using the color provided by
97     // |themeProvider|. First paint the border.
98     NSColor* borderColor = themeProvider->GetNSColor(
99         ThemeProperties::COLOR_SUPERVISED_USER_LABEL_BORDER);
100     if ([self isHighlighted]) {
101       borderColor = [borderColor blendedColorWithFraction:0.5
102                                                   ofColor:[NSColor blackColor]];
103     }
104     NSSize frameSize = cellFrame.size;
105     NSRect backgroundRect;
106     backgroundRect.origin = NSMakePoint(1, 1);
107     backgroundRect.size = NSMakeSize(frameSize.width - 2, frameSize.height - 2);
108     NSBezierPath* path =
109         [NSBezierPath bezierPathWithRoundedRect:backgroundRect
110                                         xRadius:2.0
111                                         yRadius:2.0];
112     [borderColor set];
113     [path fill];
115     // Now paint the background.
116     NSColor* backgroundColor = themeProvider->GetNSColor(
117         ThemeProperties::COLOR_SUPERVISED_USER_LABEL_BACKGROUND);
118     if ([self isHighlighted]) {
119       backgroundColor =
120           [backgroundColor blendedColorWithFraction:0.5
121                                             ofColor:[NSColor blackColor]];
122     }
123     backgroundRect.origin = NSMakePoint(2, 2);
124     backgroundRect.size = NSMakeSize(frameSize.width - 4, frameSize.height - 4);
125     path = [NSBezierPath bezierPathWithRoundedRect:backgroundRect
126                                            xRadius:2.0
127                                            yRadius:2.0];
128     [backgroundColor set];
129     [path fill];
130   }
131   [super drawInteriorWithFrame:cellFrame inView:controlView];
134 @end