BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / profiles / avatar_label_button.mm
blob7eeb7729b1a7be2bae2479a065fe567afe85e238
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(
40         IDS_LEGACY_SUPERVISED_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_SUPERVISED_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_SUPERVISED_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_SUPERVISED_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_SUPERVISED_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