-
Notifications
You must be signed in to change notification settings - Fork 33
/
UKWidgetButton.m
455 lines (344 loc) · 12.3 KB
/
UKWidgetButton.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//
// UKWidgetButton.m
// UKBorderlessWidgetizedWindow
//
// Created by Uli Kusterer on 27.10.09.
// Copyright 2009 Uli Kusterer.
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#import "UKWidgetButton.h"
#import "NSColor+UKBrightenDarken.h"
// -----------------------------------------------------------------------------
// Constants:
// -----------------------------------------------------------------------------
#define WIDGET_SIZE 12.5f
NSString* UKWidgetsMouseEnteredNotification = @"UKWidgetsMouseEnteredNotification";
NSString* UKWidgetsMouseLeaveNotification = @"UKWidgetsMouseLeaveNotification";
@implementation UKWidgetButton
@synthesize color = mColor;
@synthesize target = mTarget;
@synthesize action = mAction;
@synthesize shape = mShape;
+(NSColor*) closeButtonColor
{
return [NSColor colorWithCalibratedRed: 0.901 green: 0.348 blue: 0.090 alpha: 1.000];
}
+(NSColor*) collapseButtonColor
{
return [NSColor colorWithCalibratedRed: 0.983 green: 0.861 blue: 0.153 alpha: 1.000];
}
+(NSColor*) zoomButtonColor
{
return [NSColor colorWithCalibratedRed: 0.584 green: 0.962 blue: 0.152 alpha: 1.000];
}
+(NSColor*) inactiveButtonColor
{
return [NSColor colorWithCalibratedWhite: 0.9 alpha: 1.0];
}
+(NSColor*) defaultButtonColor
{
return [[NSColor colorWithCalibratedRed: 0.054 green: 0.317 blue: 0.979 alpha: 1.000] retain];
}
+(NSBezierPath*) closeButtonShape
{
NSBezierPath *thePath = [NSBezierPath bezierPath];
NSRect theBox = NSMakeRect( 2, 8, 10, 2 );
NSRect theBox2 = NSMakeRect( 6, 4, 2, 10 );
[thePath appendBezierPathWithRect: theBox];
[thePath appendBezierPathWithRect: theBox2];
NSAffineTransform *trans1 = [NSAffineTransform transform];
[trans1 translateXBy: -8.0 yBy: -8.0]; // Center over 0,0 ...
NSAffineTransform *trans2 = [NSAffineTransform transform];
[trans2 rotateByDegrees: 45.0]; // ... rotate around 0,0 ...
NSAffineTransform *trans3 = [NSAffineTransform transform];
[trans3 translateXBy: 9.0 yBy: 8.0]; // ... move it back to old position.
[thePath transformUsingAffineTransform: trans1];
[thePath transformUsingAffineTransform: trans2];
[thePath transformUsingAffineTransform: trans3];
return thePath;
}
+(NSBezierPath*) collapseButtonShape
{
NSBezierPath *thePath = [NSBezierPath bezierPath];
NSRect theBox = NSMakeRect( 3, 8, 8, 2 );
[thePath appendBezierPathWithRect: theBox];
return thePath;
}
+(NSBezierPath*) zoomButtonShape
{
NSBezierPath *thePath = [NSBezierPath bezierPath];
NSRect theBox = NSMakeRect( 3, 8, 8, 2 );
NSRect theBox2 = NSMakeRect( 6, 5, 2, 8 );
[thePath appendBezierPathWithRect: theBox];
[thePath appendBezierPathWithRect: theBox2];
return thePath;
}
+(NSBezierPath*) defaultButtonShape
{
NSBezierPath *thePath = [NSBezierPath bezierPath];
NSRect theBox = NSMakeRect( 3, 4, 9, 9 );
[thePath appendBezierPathWithOvalInRect: theBox];
[thePath appendBezierPathWithOvalInRect: NSInsetRect(theBox,2,2)];
[thePath setWindingRule: NSEvenOddWindingRule];
return thePath;
}
+(SEL) defaultAction
{
return Nil;
}
-(id) initWithFrame: (NSRect)frame
{
if(( self = [super initWithFrame: frame] ))
{
mColor = [[[self class] defaultButtonColor] retain]; // Fallback is blue, just cuz it's not yet used anywhere.
mShape = [[[self class] defaultButtonShape] retain];
mAction = [[self class] defaultAction];
}
return self;
}
-(void) dealloc
{
[mColor release];
mColor = nil;
[mTarget release];
mTarget = nil;
if( mTrackingArea )
{
[self removeTrackingArea: mTrackingArea];
[mTrackingArea release];
mTrackingArea = nil;
}
[mShape release];
mShape = nil;
[super dealloc];
}
-(void) viewWillMoveToWindow: (NSWindow *)newWindow
{
NSWindow* theWindow = [self window];
if( theWindow )
{
[[NSNotificationCenter defaultCenter] removeObserver: self name: UKWidgetsMouseLeaveNotification object: theWindow];
[[NSNotificationCenter defaultCenter] removeObserver: self name: UKWidgetsMouseEnteredNotification object: theWindow];
[[NSNotificationCenter defaultCenter] removeObserver: self name: NSWindowDidBecomeMainNotification object: nil];
[[NSNotificationCenter defaultCenter] removeObserver: self name: NSWindowDidResignMainNotification object: nil];
}
[super viewWillMoveToWindow: newWindow];
}
-(void) viewDidMoveToWindow
{
[super viewDidMoveToWindow];
NSWindow* theWindow = [self window];
if( theWindow )
{
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(mouseEnterInOtherWidget:) name: UKWidgetsMouseEnteredNotification object: theWindow];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(mouseLeaveInOtherWidget:) name: UKWidgetsMouseLeaveNotification object: theWindow];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(owningWindowMainnessChanged:) name: NSWindowDidBecomeMainNotification object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(owningWindowMainnessChanged:) name: NSWindowDidResignMainNotification object: nil];
}
}
-(void) mouseEnterInOtherWidget: (NSNotification*)notif
{
mMouseOver = YES;
[self setNeedsDisplay: YES];
}
-(void) mouseLeaveInOtherWidget: (NSNotification*)notif
{
mMouseOver = NO;
[self setNeedsDisplay: YES];
}
-(void) owningWindowMainnessChanged: (NSNotification*)notif
{
[self setNeedsDisplay: YES];
}
-(void) drawWidgetWithLightColor: (NSColor*)lightBlue
darkColor: (NSColor*)darkBlue borderColor: (NSColor*)borderBlue
mainWindow: (BOOL)areMain
{
NSColor *whitenedBlue = [lightBlue blendedColorWithFraction: 0.6f ofColor: [NSColor whiteColor]];
NSColor *midBlue = [darkBlue blendedColorWithFraction: 0.5 ofColor: lightBlue];
NSColor *shadowBlue = [borderBlue darkenColorBy: 0.1];
NSRect oneWidgetBox = NSMakeRect( 1, 2, WIDGET_SIZE, WIDGET_SIZE );
NSBezierPath *widgetPath = [NSBezierPath bezierPathWithOvalInRect: oneWidgetBox];
[widgetPath setLineWidth: 1.0f];
NSGradient *widgetBaseGradient = [[[NSGradient alloc] initWithColorsAndLocations:
whitenedBlue, 0.0, lightBlue, 0.5, midBlue, 0.7, darkBlue, 1.0, nil] autorelease];
NSGradient *widgetHighlightGradient = [[[NSGradient alloc] initWithColorsAndLocations:
[NSColor whiteColor], 0.0f, midBlue, 1.0f, nil] autorelease];
// Draw outer shadows and highlights:
[NSGraphicsContext saveGraphicsState];
NSShadow* edgeHighlight = [[[NSShadow alloc] init] autorelease];
[edgeHighlight setShadowOffset: NSMakeSize( 0.0f, -1.0f )];
[edgeHighlight setShadowBlurRadius: 1.5f];
[edgeHighlight setShadowColor: [NSColor whiteColor]];
[edgeHighlight set];
[widgetPath stroke];
[NSGraphicsContext restoreGraphicsState];
// Draw main sphere gradient:
[widgetBaseGradient drawInBezierPath: widgetPath relativeCenterPosition: NSMakePoint( 0, -0.6 )];
// Draw outline:
[borderBlue set];
[widgetPath setLineWidth: 0.5f];
[widgetPath stroke];
[widgetPath setLineWidth: 1.0f];
// Draw little reflection at top of sphere:
NSRect widgetHighlightBox = oneWidgetBox;
widgetHighlightBox.size.width /= 3;
widgetHighlightBox.size.height /= 3;
widgetHighlightBox.origin.x += (oneWidgetBox.size.width -widgetHighlightBox.size.width) /2;
widgetHighlightBox.origin.y += (oneWidgetBox.size.height /2) +(oneWidgetBox.size.height /2 -widgetHighlightBox.size.height) /2;
widgetHighlightBox.origin.y += widgetHighlightBox.size.height / 2;
[NSGraphicsContext saveGraphicsState];
[widgetPath addClip];
NSBezierPath* widgetHighlightPath = [NSBezierPath bezierPathWithOvalInRect: widgetHighlightBox];
[widgetHighlightGradient drawInBezierPath: widgetHighlightPath relativeCenterPosition: NSMakePoint( 0, 0.5 )];
[NSGraphicsContext restoreGraphicsState];
// Draw shadow at top edge:
NSRect widgetTopHalfBox = oneWidgetBox;
widgetTopHalfBox.size.height /= 2;
widgetTopHalfBox.origin.y += widgetTopHalfBox.size.height;
widgetTopHalfBox = NSOffsetRect( widgetTopHalfBox, 0.0f, 2.0f );
NSRect edgeShadowBox = NSInsetRect( oneWidgetBox, -0.0, -0.5f );
NSBezierPath* edgeShadowPath = [NSBezierPath bezierPathWithOvalInRect: edgeShadowBox];
[NSGraphicsContext saveGraphicsState];
[NSBezierPath clipRect: NSInsetRect( widgetTopHalfBox, -1.0f, -2.0f )];
[shadowBlue set];
[edgeShadowPath stroke];
[NSGraphicsContext restoreGraphicsState];
// Draw glyph if we're mouse-overed:
if( mMouseOver )
{
[NSGraphicsContext saveGraphicsState];
NSShadow *theShadow = [[[NSShadow alloc] init] autorelease];
[theShadow setShadowOffset: NSMakeSize( 0, -1 )];
[theShadow setShadowBlurRadius: 0];
[theShadow setShadowColor: [darkBlue colorWithAlphaComponent: 0.6]];
[theShadow set];
[[shadowBlue colorWithAlphaComponent: 0.9] set];
[mShape fill];
[NSGraphicsContext restoreGraphicsState];
}
}
-(void) drawWidgetWithColor: (NSColor*)theColor mainWindow: (BOOL)areMain
pressed: (BOOL)pressed
{
if( !areMain )
theColor = [[self class] inactiveButtonColor];
if( pressed )
theColor = [theColor darkenColorBy: 0.4];
NSColor* lightColor = [theColor brightenColorBy: 0.6f];
NSColor* darkColor = [theColor darkenColorBy: areMain ? 0.5 : 0.3];
NSColor* borderColor = [theColor darkenColorBy: areMain ? 0.5 : 0.3];
[self drawWidgetWithLightColor: lightColor darkColor: darkColor borderColor: borderColor
mainWindow: areMain];
}
-(void) drawRect: (NSRect)dirtyRect
{
BOOL areMain = [[self window] isMainWindow] || mMouseOver;
[self drawWidgetWithColor: mColor mainWindow: areMain pressed: mPressed];
}
- (BOOL)mouseDownCanMoveWindow
{
return NO;
}
- (void)updateTrackingAreas
{
if( mTrackingArea )
{
[self removeTrackingArea: mTrackingArea];
[mTrackingArea release];
mTrackingArea = nil;
}
mTrackingArea = [[NSTrackingArea alloc] initWithRect: [self bounds] options: NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect
owner: self userInfo: nil];
[self addTrackingArea: mTrackingArea];
}
-(void) mouseDown: (NSEvent*)evt
{
mPressed = YES;
[self setNeedsDisplay: YES];
}
-(void) mouseUp: (NSEvent*)evt
{
if( mTarget && mAction )
[mTarget performSelector: mAction withObject: self];
else if( mTarget )
[self tryToPerform: mAction with: self];
mPressed = NO;
[self setNeedsDisplay: YES];
}
-(void) mouseEntered: (NSEvent*)evt
{
mMouseOver = YES;
[self setNeedsDisplay: YES];
[[NSNotificationCenter defaultCenter] postNotificationName: UKWidgetsMouseEnteredNotification object: [self window]];
}
-(void) mouseExited: (NSEvent*)evt
{
mMouseOver = NO;
[self setNeedsDisplay: YES];
[[NSNotificationCenter defaultCenter] postNotificationName: UKWidgetsMouseLeaveNotification object: [self window]];
}
@end
@implementation UKCloseWidgetButton
+(NSColor*) defaultButtonColor
{
return [[self class] closeButtonColor];
}
+(NSBezierPath*) defaultButtonShape
{
return [[self class] closeButtonShape];
}
+(SEL) defaultAction
{
return @selector(performClose:);
}
@end
@implementation UKCollapseWidgetButton
+(NSColor*) defaultButtonColor
{
return [[self class] collapseButtonColor];
}
+(NSBezierPath*) defaultButtonShape
{
return [[self class] collapseButtonShape];
}
+(SEL) defaultAction
{
return @selector(performMiniaturize:);
}
@end
@implementation UKZoomWidgetButton
+(NSColor*) defaultButtonColor
{
return [[self class] zoomButtonColor];
}
+(NSBezierPath*) defaultButtonShape
{
return [[self class] zoomButtonShape];
}
+(SEL) defaultAction
{
return @selector(performZoom:);
}
@end