-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSYFlatButton.m
382 lines (328 loc) · 12.6 KB
/
SYFlatButton.m
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//
// SYFlatButton.m
// SYFlatButton
//
// Created by Sunnyyoung on 2016/11/17.
// Copyright © 2016年 Sunnyyoung. All rights reserved.
//
#import "SYFlatButton.h"
@interface SYFlatButton () <CALayerDelegate>
@property (nonatomic, strong) CAShapeLayer *imageLayer;
@property (nonatomic, strong) CATextLayer *titleLayer;
@property (nonatomic, assign) BOOL mouseDown;
@end
@implementation SYFlatButton
#pragma mark - Lifecycle
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
[self setup];
[self setupImageLayer];
[self setupTitleLayer];
}
return self;
}
- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
[self setup];
[self setupImageLayer];
[self setupTitleLayer];
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
[self addTrackingArea:[[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingActiveAlways|NSTrackingInVisibleRect|NSTrackingMouseEnteredAndExited owner:self userInfo:nil]];
}
#pragma mark - Drawing method
- (void)drawRect:(NSRect)dirtyRect {
// Do nothing
}
- (BOOL)layer:(CALayer *)layer shouldInheritContentsScale:(CGFloat)newScale fromWindow:(NSWindow *)window {
return YES;
}
#pragma mark - Setup method
- (void)setup {
// Setup layer
self.wantsLayer = YES;
self.layer.masksToBounds = YES;
self.layer.delegate = self;
self.layer.backgroundColor = [NSColor redColor].CGColor;
self.alphaValue = self.isEnabled ? 1.0 : 0.5; // not working?
}
- (void)setupImageLayer {
// Ignore image layer if has no image or imagePosition equal to NSNoImage
if (!self.image || self.imagePosition == NSNoImage) {
[self.imageLayer removeFromSuperlayer];
return;
}
CGSize buttonSize = self.frame.size;
CGSize imageSize = self.image.size;
CGSize titleSize = [self.title sizeWithAttributes:@{NSFontAttributeName: self.font}];
CGFloat x = 0.0; // Image's origin x
CGFloat y = 0.0; // Image's origin y
// Caculate the image's and title's position depends on button's imagePosition and imageHugsTitle property
switch (self.imagePosition) {
case NSNoImage:
return;
break;
case NSImageOnly: {
x = (buttonSize.width - imageSize.width) / 2.0;
y = (buttonSize.height - imageSize.height) / 2.0;
break;
}
case NSImageOverlaps: {
x = (buttonSize.width - imageSize.width) / 2.0;
y = (buttonSize.height - imageSize.height) / 2.0;
break;
}
case NSImageLeading:
case NSImageLeft: {
x = self.imageHugsTitle ? ((buttonSize.width - imageSize.width - titleSize.width) / 2.0 - self.spacing) : self.spacing;
y = (buttonSize.height - imageSize.height) / 2.0;
break;
}
case NSImageTrailing:
case NSImageRight: {
x = self.imageHugsTitle ? ((buttonSize.width - imageSize.width + titleSize.width) / 2.0 + self.spacing) : (buttonSize.width - imageSize.width - self.spacing);
y = (buttonSize.height - imageSize.height) / 2.0;
break;
}
case NSImageAbove: {
x = (buttonSize.width - imageSize.width) / 2.0;
y = self.imageHugsTitle ? ((buttonSize.height - imageSize.height - titleSize.height) / 2.0 - self.spacing) : self.spacing;
break;
}
case NSImageBelow: {
x = (buttonSize.width - imageSize.width) / 2.0;
y = self.imageHugsTitle ? ((buttonSize.height - imageSize.height + titleSize.height) / 2.0 + self.spacing) : (buttonSize.height - imageSize.height - self.spacing);
break;
}
default: {
break;
}
}
// Setup image layer
self.imageLayer.frame = self.bounds;
self.imageLayer.mask = ({
CALayer *layer = [CALayer layer];
NSRect rect = NSMakeRect(round(x), round(y), imageSize.width, imageSize.height);
layer.frame = rect;
layer.contents = (__bridge id _Nullable)[self.image CGImageForProposedRect:&rect context:nil hints:nil];
layer;
});
[self.layer addSublayer:self.imageLayer];
}
- (void)setupTitleLayer {
// Ignore title layer if has no title or imagePosition equal to NSImageOnly
if (!self.title || self.imagePosition == NSImageOnly) {
[self.titleLayer removeFromSuperlayer];
return;
}
CGSize buttonSize = self.frame.size;
CGSize imageSize = self.image.size;
CGSize titleSize = [self.title sizeWithAttributes:@{NSFontAttributeName: self.font}];
CGFloat x = 0.0; // Title's origin x
CGFloat y = 0.0; // Title's origin y
// Caculate the image's and title's position depends on button's imagePosition and imageHugsTitle property
switch (self.imagePosition) {
case NSImageOnly: {
return;
break;
}
case NSNoImage: {
x = (buttonSize.width - titleSize.width) / 2.0;
y = (buttonSize.height - titleSize.height) / 2.0;
break;
}
case NSImageOverlaps: {
x = (buttonSize.width - titleSize.width) / 2.0;
y = (buttonSize.height - titleSize.height) / 2.0;
break;
}
case NSImageLeading:
case NSImageLeft: {
x = self.imageHugsTitle ? ((buttonSize.width + imageSize.width - titleSize.width) / 2.0 + self.spacing) : (buttonSize.width - titleSize.width - self.spacing);
y = (buttonSize.height - titleSize.height) / 2.0;
break;
}
case NSImageTrailing:
case NSImageRight: {
x = self.imageHugsTitle ? ((buttonSize.width - imageSize.width - titleSize.width) / 2.0 - self.spacing) : self.spacing;
y = (buttonSize.height - titleSize.height) / 2.0;
break;
}
case NSImageAbove: {
x = (buttonSize.width - titleSize.width) / 2.0;
y = self.imageHugsTitle ? ((buttonSize.height + imageSize.height - titleSize.height) / 2.0 + self.spacing) : (buttonSize.height - titleSize.height - self.spacing);
break;
}
case NSImageBelow: {
y = self.imageHugsTitle ? ((buttonSize.height - imageSize.height - titleSize.height) / 2.0 - self.spacing) : self.spacing;
x = (buttonSize.width - titleSize.width) / 2.0;
break;
}
default: {
break;
}
}
// Setup title layer
self.titleLayer.frame = NSMakeRect(round(x), round(y), ceil(titleSize.width), ceil(titleSize.height));
self.titleLayer.string = self.title;
self.titleLayer.font = (__bridge CFTypeRef _Nullable)(self.font);
self.titleLayer.fontSize = self.font.pointSize;
[self.layer addSublayer:self.titleLayer];
}
#pragma mark - Animation method
- (void)removeAllAnimations {
[self.layer removeAllAnimations];
[self.layer.sublayers enumerateObjectsUsingBlock:^(CALayer * _Nonnull layer, NSUInteger index, BOOL * _Nonnull stop) {
[layer removeAllAnimations];
}];
}
- (void)animateColorWithState:(NSControlStateValue)state {
[self removeAllAnimations];
CGFloat duration = (state == NSControlStateValueOn) ? self.onAnimateDuration : self.offAnimateDuration;
NSColor *borderColor = (state == NSControlStateValueOn) ? self.borderHighlightColor : self.borderNormalColor;
NSColor *backgroundColor = (state == NSControlStateValueOn) ? self.backgroundHighlightColor : self.backgroundNormalColor;
NSColor *titleColor = (state == NSControlStateValueOn) ? self.titleHighlightColor : self.titleNormalColor;
NSColor *imageColor = (state == NSControlStateValueOn) ? self.imageHighlightColor : self.imageNormalColor;
[self animateLayer:self.layer color:borderColor keyPath:@"borderColor" duration:duration];
[self animateLayer:self.layer color:backgroundColor keyPath:@"backgroundColor" duration:duration];
[self animateLayer:self.imageLayer color:imageColor keyPath:@"backgroundColor" duration:duration];
[self animateLayer:self.titleLayer color:titleColor keyPath:@"foregroundColor" duration:duration];
}
- (void)animateLayer:(CALayer *)layer color:(NSColor *)color keyPath:(NSString *)keyPath duration:(CGFloat)duration {
CGColorRef oldColor = (__bridge CGColorRef)([layer valueForKeyPath:keyPath]);
if (!(CGColorEqualToColor(oldColor, color.CGColor))) {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath];
animation.fromValue = [layer valueForKeyPath:keyPath];
animation.toValue = (id)color.CGColor;
animation.duration = duration;
animation.removedOnCompletion = NO;
[layer addAnimation:animation forKey:keyPath];
[layer setValue:(id)color.CGColor forKey:keyPath];
}
}
#pragma mark - Event Response
- (NSView *)hitTest:(NSPoint)point {
return self.isEnabled ? [super hitTest:point] : nil;
}
- (void)mouseDown:(NSEvent *)event {
if (self.isEnabled) {
self.mouseDown = YES;
self.state = (self.state == NSControlStateValueOn) ? NSControlStateValueOff : NSControlStateValueOn;
}
}
- (void)mouseEntered:(NSEvent *)event {
if (self.mouseDown) {
self.state = (self.state == NSControlStateValueOn) ? NSControlStateValueOff : NSControlStateValueOn;
}
}
- (void)mouseExited:(NSEvent *)event {
if (self.mouseDown) {
self.mouseDown = NO;
self.state = (self.state == NSControlStateValueOn) ? NSControlStateValueOff : NSControlStateValueOn;
}
}
- (void)mouseUp:(NSEvent *)event {
if (self.mouseDown) {
self.mouseDown = NO;
if (self.momentary) {
self.state = (self.state == NSControlStateValueOn) ? NSControlStateValueOff : NSControlStateValueOn;
}
[NSApp sendAction:self.action to:self.target from:self];
}
}
#pragma mark - Property method
- (void)setFrame:(NSRect)frame {
[super setFrame:frame];
[self setupTitleLayer];
}
- (void)setFont:(NSFont *)font {
[super setFont:font];
[self setupTitleLayer];
}
- (void)setTitle:(NSString *)title {
[super setTitle:title];
[self setupTitleLayer];
}
- (void)setImage:(NSImage *)image {
[super setImage:image];
[super setImageScaling:NSImageScaleProportionallyDown];
[self setupImageLayer];
}
- (void)setState:(NSInteger)state {
[super setState:state];
[self animateColorWithState:state];
}
- (void)setImagePosition:(NSCellImagePosition)imagePosition {
[super setImagePosition:imagePosition];
[self setupImageLayer];
[self setupTitleLayer];
}
- (void)setMomentary:(BOOL)momentary {
_momentary = momentary;
[self animateColorWithState:self.state];
}
- (void)setCornerRadius:(CGFloat)cornerRadius {
_cornerRadius = cornerRadius;
self.layer.cornerRadius = _cornerRadius;
}
- (void)setBorderWidth:(CGFloat)borderWidth {
_borderWidth = borderWidth;
self.layer.borderWidth = _borderWidth;
}
- (void)setSpacing:(CGFloat)spacing {
_spacing = spacing;
[self setupImageLayer];
[self setupTitleLayer];
}
- (void)setBorderNormalColor:(NSColor *)borderNormalColor {
_borderNormalColor = borderNormalColor;
[self animateColorWithState:self.state];
}
- (void)setBorderHighlightColor:(NSColor *)borderHighlightColor {
_borderHighlightColor = borderHighlightColor;
[self animateColorWithState:self.state];
}
- (void)setBackgroundNormalColor:(NSColor *)backgroundNormalColor {
_backgroundNormalColor = backgroundNormalColor;
[self animateColorWithState:self.state];
}
- (void)setBackgroundHighlightColor:(NSColor *)backgroundHighlightColor {
_backgroundHighlightColor = backgroundHighlightColor;
[self animateColorWithState:self.state];
}
- (void)setImageNormalColor:(NSColor *)imageNormalColor {
_imageNormalColor = imageNormalColor;
[self animateColorWithState:self.state];
}
- (void)setImageHighlightColor:(NSColor *)imageHighlightColor {
_imageHighlightColor = imageHighlightColor;
[self animateColorWithState:self.state];
}
- (void)setTitleNormalColor:(NSColor *)titleNormalColor {
_titleNormalColor = titleNormalColor;
[self animateColorWithState:self.state];
}
- (void)setTitleHighlightColor:(NSColor *)titleHighlightColor {
_titleHighlightColor = titleHighlightColor;
[self animateColorWithState:self.state];
}
- (CAShapeLayer *)imageLayer {
if (_imageLayer == nil) {
_imageLayer = [[CAShapeLayer alloc] init];
_imageLayer.delegate = self;
}
return _imageLayer;
}
- (CATextLayer *)titleLayer {
if (_titleLayer == nil) {
_titleLayer = [[CATextLayer alloc] init];
_titleLayer.delegate = self;
}
return _titleLayer;
}
@end