-
Notifications
You must be signed in to change notification settings - Fork 10
/
Rik+Button.m
311 lines (285 loc) · 10.6 KB
/
Rik+Button.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
#import "Rik.h"
#import "Rik+Button.h"
NSString * const kRikIsDefaultButton = @"kRikIsDefaultButton";
NSString * const kRikPulseProgressKey = @"kRikPulseProgressKey";
@implementation NSButtonCell(RikDefaultButtonAnimation)
- (void)setIsDefaultButton:(NSNumber*) val
{
objc_setAssociatedObject(self, kRikIsDefaultButton, val, OBJC_ASSOCIATION_COPY);
}
- (NSNumber *) isDefaultButton
{
return objc_getAssociatedObject(self, kRikIsDefaultButton);
}
- (BOOL)defaultButton
{
return [[self isDefaultButton] boolValue];
}
- (void)setPulseProgress:(NSNumber *)pulseProgress
{
objc_setAssociatedObject(self, kRikPulseProgressKey, pulseProgress, OBJC_ASSOCIATION_COPY);
}
- (NSNumber*)pulseProgress
{
return objc_getAssociatedObject(self, kRikPulseProgressKey);
}
@end
@implementation Rik(RikButton)
- (NSColor*) pulseColorInCell:(NSButtonCell*) bc
{
NSColor * color;
CGFloat pulse = [[bc pulseProgress] floatValue];
color = [NSColor colorWithCalibratedRed: 0.62 green: 0.82 blue: 0.965 alpha: 1];
color = [NSColor colorWithCalibratedHue: [color hueComponent] saturation: 1.0 - pulse*0.6 brightness: 0.9 + pulse*0.1 alpha: [color alphaComponent]];
return color;
}
- (NSColor*) buttonColorInCell:(NSCell*) cell forState: (GSThemeControlState) state
{
NSColor *color = nil;
NSString *name = [super nameForElement: cell];
color = [self colorNamed: name state: state];
if (color == nil)
{
if (state == GSThemeNormalState)
{
color = [[NSColor controlBackgroundColor] shadowWithLevel: 0.1];
}
else if (state == GSThemeHighlightedState
|| state == GSThemeHighlightedFirstResponderState)
{
color = [NSColor selectedControlColor];
}
else if (state == GSThemeSelectedState
|| state == GSThemeSelectedFirstResponderState)
{
color = [NSColor selectedControlColor];
}
else
{
color = [[NSColor controlBackgroundColor] shadowWithLevel: 0.1];
}
}
//PULSE ANIMATION COLOR IF IS PRESSED DONT ANIMATE..
if([cell class] == [NSButtonCell class] && state != GSThemeSelectedState)
{
NSButtonCell * bc = (NSButtonCell *)cell;
if(bc.isDefaultButton)
{
color = [self pulseColorInCell: bc];
}
}
return color;
}
- (NSBezierPath *) _roundBezierPath: (NSRect) frame
withRadius:(CGFloat) radius
{
frame = NSInsetRect(frame, 0.5, 0.5);
NSBezierPath* roundedRectanglePath = [NSBezierPath bezierPathWithRoundedRect: frame
xRadius: radius
yRadius: radius];
return roundedRectanglePath;
}
- (void) _drawRoundBezel: (NSRect)cellFrame
withColor: (NSColor*)backgroundColor
andRadius: (CGFloat) radius
{
NSColor* strokeColorButton = [Rik controlStrokeColor];
NSGradient* buttonBackgroundGradient = [self _bezelGradientWithColor: backgroundColor];
NSBezierPath* roundedRectanglePath = [self _roundBezierPath: cellFrame withRadius: radius];
[buttonBackgroundGradient drawInBezierPath: roundedRectanglePath angle: -90];
[strokeColorButton setStroke];
[roundedRectanglePath setLineWidth: 1];
[roundedRectanglePath stroke];
}
- (void) drawPathButton: (NSBezierPath*) path
in: (NSCell*)cell
state: (GSThemeControlState) state
{
NSColor *backgroundColor = [self buttonColorInCell: cell forState: state];
NSColor* strokeColorButton = [Rik controlStrokeColor];
NSGradient* buttonBackgroundGradient = [self _bezelGradientWithColor: backgroundColor];
[buttonBackgroundGradient drawInBezierPath: path angle: -90];
[strokeColorButton setStroke];
[path setLineWidth: 1];
[path stroke];
}
- (void) _drawRoundBezel: (NSRect)cellFrame withColor: (NSColor*)backgroundColor
{
[self _drawRoundBezel: cellFrame withColor: backgroundColor andRadius: 4];
}
- (void) _drawRoundedBezel: (NSRect)cellFrame withColor: (NSColor*)backgroundColor
{
float r = MIN(cellFrame.size.width, cellFrame.size.height) / 2.0;
[self _drawRoundBezel: cellFrame withColor: backgroundColor andRadius: r];
}
- (void) drawCircularBezel: (NSRect)cellFrame withColor: (NSColor*)backgroundColor
{
CGFloat circle_radius = MIN(NSWidth(cellFrame), NSHeight(cellFrame)) / 2;
CGFloat x = cellFrame.origin.x + cellFrame.size.width/2.0 - circle_radius;
cellFrame = NSMakeRect( x,
cellFrame.origin.y,
circle_radius*2,
circle_radius*2);
[self _drawRoundBezel: cellFrame withColor: backgroundColor
andRadius: circle_radius];
}
- (NSRect) drawButton: (NSRect)border withClip: (NSRect)clip
{
NSColor * c = [NSColor controlBackgroundColor];
[self _drawRoundBezel: border withColor: c];
return border;
}
- (NSBezierPath*) buttonBezierPathWithRect: (NSRect)frame andStyle: (int) style
{
NSBezierPath* bezierPath;
CGFloat r;
CGFloat x;
switch (style)
{
case NSRoundRectBezelStyle:
bezierPath = [self _roundBezierPath: frame
withRadius: 4];
break;
case NSTexturedRoundedBezelStyle:
case NSRoundedBezelStyle:
r = MIN(frame.size.width, frame.size.height) / 2.0;
bezierPath = [self _roundBezierPath: frame
withRadius: r];
break;
case NSTexturedSquareBezelStyle:
frame = NSInsetRect(frame, 0, 1);
case NSSmallSquareBezelStyle:
case NSRegularSquareBezelStyle:
case NSShadowlessSquareBezelStyle:
case NSThickSquareBezelStyle:
case NSThickerSquareBezelStyle:
bezierPath = [NSBezierPath bezierPathWithRect: frame];
break;
case NSCircularBezelStyle:
case NSHelpButtonBezelStyle:
r = MIN(NSWidth(frame), NSHeight(frame)) / 2;
x = frame.origin.x + frame.size.width/2.0 - r;
frame = NSMakeRect( x,
frame.origin.y,
r*2,
r*2);
bezierPath = [self _roundBezierPath: frame
withRadius: r];
break;
case NSDisclosureBezelStyle:
case NSRoundedDisclosureBezelStyle:
case NSRecessedBezelStyle:
r = MIN(frame.size.width, frame.size.height) / 2.0;
bezierPath = [self _roundBezierPath: frame
withRadius: r];
break;
default:
bezierPath = [self _roundBezierPath: frame
withRadius: 4];
}
return RETAIN(bezierPath);
}
- (void) drawButton: (NSRect) frame
in: (NSCell*) cell
view: (NSView*) view
style: (int) style
state: (GSThemeControlState) state
{
NSColor *color = [self buttonColorInCell: cell forState: state];
switch (style)
{
case NSRoundRectBezelStyle:
[self _drawRoundBezel: frame withColor: color];
break;
case NSTexturedRoundedBezelStyle:
case NSRoundedBezelStyle:
[self _drawRoundedBezel: frame withColor: color];
break;
case NSTexturedSquareBezelStyle:
frame = NSInsetRect(frame, 0, 1);
case NSSmallSquareBezelStyle:
case NSRegularSquareBezelStyle:
case NSShadowlessSquareBezelStyle:
[color set];
NSRectFill(frame);
[[Rik controlStrokeColor] set];
NSFrameRectWithWidth(frame, 1);
break;
case NSThickSquareBezelStyle:
[color set];
NSRectFill(frame);
[[Rik controlStrokeColor] set];
NSFrameRectWithWidth(frame, 1.5);
break;
case NSThickerSquareBezelStyle:
[color set];
NSRectFill(frame);
[[NSColor controlShadowColor] set];
NSFrameRectWithWidth(frame, 2);
break;
case NSCircularBezelStyle:
[self drawCircularBezel: frame withColor: color];
break;
case NSHelpButtonBezelStyle:
[self drawCircularBezel: frame withColor: color];
{
NSDictionary *attributes = [NSDictionary dictionaryWithObject: [NSFont controlContentFontOfSize: 0]
forKey: NSFontAttributeName];
NSAttributedString *questionMark = [[[NSAttributedString alloc]
initWithString: _(@"?")
attributes: attributes] autorelease];
NSRect textRect;
textRect.size = [questionMark size];
textRect.origin.x = NSMidX(frame) - (textRect.size.width / 2);
textRect.origin.y = NSMidY(frame) - (textRect.size.height / 2);
[questionMark drawInRect: textRect];
}
break;
case NSDisclosureBezelStyle:
case NSRoundedDisclosureBezelStyle:
case NSRecessedBezelStyle:
[self _drawRoundBezel: frame withColor: color];
break;
default:
[self _drawRoundBezel: frame withColor: color];
}
}
// currently not used
- (void) _drawButtonMetal:(NSRect) rect
{
NSColor* strokeColorButton = [Rik controlStrokeColor];
NSColor* baseColor = [NSColor colorWithCalibratedRed: 0.75
green: 0.75
blue: 0.75
alpha: 1];
NSColor* baseColorLight = [baseColor highlightWithLevel: 0.6];
NSGradient* buttonBackgroundGradient = [[NSGradient alloc] initWithColorsAndLocations:
baseColorLight, 1.0,
baseColor, 0.0, nil];
CGFloat roundedRectangleStrokeWidth = 1;
NSBezierPath* roundedRectanglePath = [NSBezierPath bezierPathWithRoundedRect: rect xRadius: 3 yRadius: 3];
[buttonBackgroundGradient drawInBezierPath: roundedRectanglePath angle: -90];
[strokeColorButton setStroke];
[roundedRectanglePath setLineWidth: roundedRectangleStrokeWidth];
[roundedRectanglePath stroke];
}
- (NSRect) drawDarkButton: (NSRect)border withClip: (NSRect)clip
{
NSColor* strokeColorButton = [Rik controlStrokeColor];
NSColor* baseColor = [NSColor colorWithCalibratedRed: 0.75
green: 0.75
blue: 0.75
alpha: 1];
NSColor* baseColorLight = [baseColor highlightWithLevel: 0.6];
NSGradient* buttonBackgroundGradient = [[NSGradient alloc] initWithColorsAndLocations:
baseColorLight, 1.0,
baseColor, 0.0, nil];
CGFloat roundedRectangleStrokeWidth = 1;
NSBezierPath* roundedRectanglePath = [NSBezierPath bezierPathWithRect: border];
[buttonBackgroundGradient drawInBezierPath: roundedRectanglePath angle: -90];
[strokeColorButton setStroke];
[roundedRectanglePath setLineWidth: roundedRectangleStrokeWidth];
// [roundedRectanglePath stroke];
return border;
}
@end