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/app_list/cocoa/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"
15 const CGFloat kCornerRadius = 2;
17 const CGFloat kButtonFontSizeDelta = -1;
18 const CGFloat kTopBottomTextPadding = 8;
19 const CGFloat kLeftRightTextPadding = 16;
20 const SkColor kTextShadowColor = SkColorSetRGB(0x53, 0x8c, 0xea);
22 const SkColor kShadowColor = SkColorSetRGB(0xe9, 0xe9, 0xe9);
23 const SkColor kDefaultColor = SkColorSetRGB(0x5a, 0x97, 0xff);
24 const SkColor kHoverColor = SkColorSetRGB(0x55, 0x8f, 0xf3);
25 const SkColor kPressedColor = SkColorSetRGB(0x42, 0x79, 0xd8);
27 const SkColor kInnerRingColor = SkColorSetRGB(0x64, 0x9e, 0xff);
28 const SkColor kFocusInnerRingColor = SkColorSetRGB(0xad, 0xcc, 0xff);
29 const SkColor kPressInnerRingColor = SkColorSetRGB(0x3f, 0x73, 0xcd);
30 const SkColor kPressFocusInnerRingColor = SkColorSetRGB(0xa0, 0xb9, 0xe7);
32 const SkColor kOuterRingColor = SkColorSetRGB(0x2b, 0x67, 0xce);
33 const SkColor kPressOuterRingColor = SkColorSetRGB(0x23, 0x52, 0xa2);
37 @interface BlueLabelButtonCell : NSButtonCell
39 + (NSAttributedString*)generateAttributedString:(NSString*)buttonText;
43 @implementation BlueLabelButton
46 return [BlueLabelButtonCell class];
49 - (id)initWithFrame:(NSRect)frameRect {
50 if ((self = [super initWithFrame:frameRect])) {
51 [self setBezelStyle:NSSmallSquareBezelStyle];
58 @implementation BlueLabelButtonCell
60 + (NSAttributedString*)generateAttributedString:(NSString*)buttonText {
61 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
62 NSFont* buttonFont = rb.GetFont(ui::ResourceBundle::BaseFont).
63 DeriveFont(kButtonFontSizeDelta).GetNativeFont();
64 base::scoped_nsobject<NSMutableParagraphStyle> buttonTextParagraphStyle(
65 [[NSMutableParagraphStyle alloc] init]);
66 [buttonTextParagraphStyle setAlignment:NSCenterTextAlignment];
68 NSDictionary* buttonTextAttributes = @{
69 NSParagraphStyleAttributeName : buttonTextParagraphStyle,
70 NSFontAttributeName : buttonFont,
71 NSForegroundColorAttributeName : [NSColor whiteColor]
73 base::scoped_nsobject<NSAttributedString> attributedButtonText(
74 [[NSAttributedString alloc] initWithString:buttonText
75 attributes:buttonTextAttributes]);
76 return attributedButtonText.autorelease();
80 NSAttributedString* attributedTitle =
81 [[self class] generateAttributedString:[self title]];
82 NSSize textSize = [attributedTitle size];
83 textSize.height += 2 * kTopBottomTextPadding;
84 textSize.width += 2 * kLeftRightTextPadding;
88 - (NSRect)drawTitle:(NSAttributedString*)title
89 withFrame:(NSRect)frame
90 inView:(NSView*)controlView {
91 NSAttributedString* attributedTitle =
92 [[self class] generateAttributedString:[self title]];
93 // Draw the text with a drop shadow.
94 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]);
95 gfx::ScopedNSGraphicsContextSaveGState context;
96 [shadow setShadowOffset:NSMakeSize(0, -1)];
97 [shadow setShadowBlurRadius:0];
98 [shadow setShadowColor:gfx::SkColorToSRGBNSColor(kTextShadowColor)];
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];
112 if (hoverState == kHoverStateMouseDown && [self isHighlighted]) {
113 centerColor = gfx::SkColorToSRGBNSColor(kPressedColor);
114 innerColor = gfx::SkColorToSRGBNSColor(kPressInnerRingColor);
115 outerColor = gfx::SkColorToSRGBNSColor(kPressOuterRingColor);
117 centerColor = hoverState == kHoverStateMouseOver ?
118 gfx::SkColorToSRGBNSColor(kHoverColor) :
119 gfx::SkColorToSRGBNSColor(kDefaultColor);
120 innerColor = [self showsFirstResponder] ?
121 gfx::SkColorToSRGBNSColor(kFocusInnerRingColor) :
122 gfx::SkColorToSRGBNSColor(kInnerRingColor);
123 outerColor = gfx::SkColorToSRGBNSColor(kOuterRingColor);
126 gfx::ScopedNSGraphicsContextSaveGState context;
127 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]);
128 [shadow setShadowOffset:NSMakeSize(0, -1)];
129 [shadow setShadowBlurRadius:1.0];
130 [shadow setShadowColor:gfx::SkColorToSRGBNSColor(kShadowColor)];
134 // Inset by 1 initially for the button drop shadow.
135 [[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 1, 1)
136 xRadius:kCornerRadius
137 yRadius:kCornerRadius] fill];
141 [[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 2, 2)
142 xRadius:kCornerRadius
143 yRadius:kCornerRadius] fill];
145 [[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 3, 3)
146 xRadius:kCornerRadius
147 yRadius:kCornerRadius] fill];