1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ui/base/cocoa/controls/blue_label_button.h"
#include "base/mac/foundation_util.h"
#include "skia/ext/skia_utils_mac.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
const CGFloat kCornerRadius = 2;
const CGFloat kTopBottomTextPadding = 7;
const CGFloat kLeftRightTextPadding = 15;
const SkColor kTextShadowColor = SkColorSetRGB(0x53, 0x8c, 0xea);
const SkColor kShadowColor = SkColorSetRGB(0xe9, 0xe9, 0xe9);
const SkColor kDefaultColor = SkColorSetRGB(0x5a, 0x97, 0xff);
const SkColor kHoverColor = SkColorSetRGB(0x55, 0x8f, 0xf3);
const SkColor kPressedColor = SkColorSetRGB(0x42, 0x79, 0xd8);
const SkColor kInnerRingColor = SkColorSetRGB(0x64, 0x9e, 0xff);
const SkColor kFocusInnerRingColor = SkColorSetRGB(0xad, 0xcc, 0xff);
const SkColor kPressInnerRingColor = SkColorSetRGB(0x3f, 0x73, 0xcd);
const SkColor kOuterRingColor = SkColorSetRGB(0x2b, 0x67, 0xce);
const SkColor kPressOuterRingColor = SkColorSetRGB(0x23, 0x52, 0xa2);
@interface BlueLabelButtonCell : NSButtonCell
+ (NSAttributedString*)generateAttributedString:(NSString*)buttonText;
@end
@implementation BlueLabelButton
+ (Class)cellClass {
return [BlueLabelButtonCell class];
}
- (id)initWithFrame:(NSRect)frameRect {
if ((self = [super initWithFrame:frameRect])) {
[self setBezelStyle:NSSmallSquareBezelStyle];
}
return self;
}
@end
@implementation BlueLabelButtonCell
+ (NSAttributedString*)generateAttributedString:(NSString*)buttonText {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
NSFont* buttonFont = rb.GetFontList(ui::ResourceBundle::SmallFont).
GetPrimaryFont().GetNativeFont();
base::scoped_nsobject<NSMutableParagraphStyle> buttonTextParagraphStyle(
[[NSMutableParagraphStyle alloc] init]);
[buttonTextParagraphStyle setAlignment:NSCenterTextAlignment];
base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]);
[shadow setShadowOffset:NSMakeSize(0, -1)];
[shadow setShadowBlurRadius:0];
[shadow setShadowColor:gfx::SkColorToSRGBNSColor(kTextShadowColor)];
NSDictionary* buttonTextAttributes = @{
NSParagraphStyleAttributeName : buttonTextParagraphStyle,
NSFontAttributeName : buttonFont,
NSForegroundColorAttributeName : [NSColor whiteColor],
NSShadowAttributeName : shadow.get()
};
base::scoped_nsobject<NSAttributedString> attributedButtonText(
[[NSAttributedString alloc] initWithString:buttonText
attributes:buttonTextAttributes]);
return attributedButtonText.autorelease();
}
- (NSSize)cellSize {
NSAttributedString* attributedTitle =
[[self class] generateAttributedString:[self title]];
NSSize textSize = [attributedTitle size];
// Add 1 to maintain identical height w/ previous drawing code.
textSize.height += 2 * kTopBottomTextPadding + 1;
textSize.width += 2 * kLeftRightTextPadding;
return textSize;
}
- (NSRect)drawTitle:(NSAttributedString*)title
withFrame:(NSRect)frame
inView:(NSView*)controlView {
// Fuzz factor to adjust for the drop shadow. Based on visual inspection.
frame.origin.y -= 1;
NSAttributedString* attributedTitle =
[[self class] generateAttributedString:[self title]];
gfx::ScopedNSGraphicsContextSaveGState context;
[attributedTitle drawInRect:frame];
return frame;
}
- (void)drawBezelWithFrame:(NSRect)frame
inView:(NSView*)controlView {
NSColor* centerColor;
NSColor* innerColor;
NSColor* outerColor;
HoverState hoverState =
[base::mac::ObjCCastStrict<HoverButton>(controlView) hoverState];
// Leave a sliver of height 1 for the button drop shadow.
frame.size.height -= 1;
if (hoverState == kHoverStateMouseDown && [self isHighlighted]) {
centerColor = gfx::SkColorToSRGBNSColor(kPressedColor);
innerColor = gfx::SkColorToSRGBNSColor(kPressInnerRingColor);
outerColor = gfx::SkColorToSRGBNSColor(kPressOuterRingColor);
} else {
centerColor = hoverState == kHoverStateMouseOver ?
gfx::SkColorToSRGBNSColor(kHoverColor) :
gfx::SkColorToSRGBNSColor(kDefaultColor);
innerColor = [self showsFirstResponder] ?
gfx::SkColorToSRGBNSColor(kFocusInnerRingColor) :
gfx::SkColorToSRGBNSColor(kInnerRingColor);
outerColor = gfx::SkColorToSRGBNSColor(kOuterRingColor);
}
{
gfx::ScopedNSGraphicsContextSaveGState context;
base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]);
[shadow setShadowOffset:NSMakeSize(0, -1)];
[shadow setShadowBlurRadius:1.0];
[shadow setShadowColor:gfx::SkColorToSRGBNSColor(kShadowColor)];
[shadow set];
[outerColor set];
[[NSBezierPath bezierPathWithRoundedRect:frame
xRadius:kCornerRadius
yRadius:kCornerRadius] fill];
}
[innerColor set];
[[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 1, 1)
xRadius:kCornerRadius
yRadius:kCornerRadius] fill];
[centerColor set];
[[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 2, 2)
xRadius:kCornerRadius
yRadius:kCornerRadius] fill];
}
@end
|