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 "ui/base/cocoa/controls/blue_label_button.h"
7 #include "base/mac/foundation_util.h"
8 #include "skia/ext/skia_utils_mac.h"
9 #include "third_party/skia/include/core/SkColor.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
13 const CGFloat kCornerRadius = 2;
15 const CGFloat kButtonFontSizeDelta = -1;
16 const CGFloat kTopBottomTextPadding = 7;
17 const CGFloat kLeftRightTextPadding = 15;
18 const SkColor kTextShadowColor = SkColorSetRGB(0x53, 0x8c, 0xea);
20 const SkColor kShadowColor = SkColorSetRGB(0xe9, 0xe9, 0xe9);
21 const SkColor kDefaultColor = SkColorSetRGB(0x5a, 0x97, 0xff);
22 const SkColor kHoverColor = SkColorSetRGB(0x55, 0x8f, 0xf3);
23 const SkColor kPressedColor = SkColorSetRGB(0x42, 0x79, 0xd8);
25 const SkColor kInnerRingColor = SkColorSetRGB(0x64, 0x9e, 0xff);
26 const SkColor kFocusInnerRingColor = SkColorSetRGB(0xad, 0xcc, 0xff);
27 const SkColor kPressInnerRingColor = SkColorSetRGB(0x3f, 0x73, 0xcd);
29 const SkColor kOuterRingColor = SkColorSetRGB(0x2b, 0x67, 0xce);
30 const SkColor kPressOuterRingColor = SkColorSetRGB(0x23, 0x52, 0xa2);
32 @interface BlueLabelButtonCell : NSButtonCell
34 + (NSAttributedString*)generateAttributedString:(NSString*)buttonText;
38 @implementation BlueLabelButton
41 return [BlueLabelButtonCell class];
44 - (id)initWithFrame:(NSRect)frameRect {
45 if ((self = [super initWithFrame:frameRect])) {
46 [self setBezelStyle:NSSmallSquareBezelStyle];
53 @implementation BlueLabelButtonCell
55 + (NSAttributedString*)generateAttributedString:(NSString*)buttonText {
56 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
57 NSFont* buttonFont = rb.GetFont(ui::ResourceBundle::BaseFont).
58 DeriveFont(kButtonFontSizeDelta).GetNativeFont();
59 base::scoped_nsobject<NSMutableParagraphStyle> buttonTextParagraphStyle(
60 [[NSMutableParagraphStyle alloc] init]);
61 [buttonTextParagraphStyle setAlignment:NSCenterTextAlignment];
63 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]);
64 [shadow setShadowOffset:NSMakeSize(0, -1)];
65 [shadow setShadowBlurRadius:0];
66 [shadow setShadowColor:gfx::SkColorToSRGBNSColor(kTextShadowColor)];
68 NSDictionary* buttonTextAttributes = @{
69 NSParagraphStyleAttributeName : buttonTextParagraphStyle,
70 NSFontAttributeName : buttonFont,
71 NSForegroundColorAttributeName : [NSColor whiteColor],
72 NSShadowAttributeName : shadow.get()
74 base::scoped_nsobject<NSAttributedString> attributedButtonText(
75 [[NSAttributedString alloc] initWithString:buttonText
76 attributes:buttonTextAttributes]);
77 return attributedButtonText.autorelease();
81 NSAttributedString* attributedTitle =
82 [[self class] generateAttributedString:[self title]];
83 NSSize textSize = [attributedTitle size];
85 // Add 1 to maintain identical height w/ previous drawing code.
86 textSize.height += 2 * kTopBottomTextPadding + 1;
87 textSize.width += 2 * kLeftRightTextPadding;
91 - (NSRect)drawTitle:(NSAttributedString*)title
92 withFrame:(NSRect)frame
93 inView:(NSView*)controlView {
94 // Fuzz factor to adjust for the drop shadow. Based on visual inspection.
97 NSAttributedString* attributedTitle =
98 [[self class] generateAttributedString:[self title]];
99 gfx::ScopedNSGraphicsContextSaveGState context;
100 [attributedTitle drawInRect:frame];
104 - (void)drawBezelWithFrame:(NSRect)frame
105 inView:(NSView*)controlView {
106 NSColor* centerColor;
109 HoverState hoverState =
110 [base::mac::ObjCCastStrict<HoverButton>(controlView) hoverState];
111 // Leave a sliver of height 1 for the button drop shadow.
112 frame.size.height -= 1;
114 if (hoverState == kHoverStateMouseDown && [self isHighlighted]) {
115 centerColor = gfx::SkColorToSRGBNSColor(kPressedColor);
116 innerColor = gfx::SkColorToSRGBNSColor(kPressInnerRingColor);
117 outerColor = gfx::SkColorToSRGBNSColor(kPressOuterRingColor);
119 centerColor = hoverState == kHoverStateMouseOver ?
120 gfx::SkColorToSRGBNSColor(kHoverColor) :
121 gfx::SkColorToSRGBNSColor(kDefaultColor);
122 innerColor = [self showsFirstResponder] ?
123 gfx::SkColorToSRGBNSColor(kFocusInnerRingColor) :
124 gfx::SkColorToSRGBNSColor(kInnerRingColor);
125 outerColor = gfx::SkColorToSRGBNSColor(kOuterRingColor);
128 gfx::ScopedNSGraphicsContextSaveGState context;
129 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]);
130 [shadow setShadowOffset:NSMakeSize(0, -1)];
131 [shadow setShadowBlurRadius:1.0];
132 [shadow setShadowColor:gfx::SkColorToSRGBNSColor(kShadowColor)];
136 [[NSBezierPath bezierPathWithRoundedRect:frame
137 xRadius:kCornerRadius
138 yRadius:kCornerRadius] fill];
142 [[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 1, 1)
143 xRadius:kCornerRadius
144 yRadius:kCornerRadius] fill];
146 [[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 2, 2)
147 xRadius:kCornerRadius
148 yRadius:kCornerRadius] fill];