forked from indragiek/INAppStoreWindow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINAppStoreWindow.m
executable file
·634 lines (549 loc) · 23.6 KB
/
INAppStoreWindow.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//
// INAppStoreWindow.m
//
// Copyright 2011 Indragie Karunaratne. All rights reserved.
//
// Licensed under the BSD License <http://www.opensource.org/licenses/bsd-license>
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "INAppStoreWindow.h"
#define IN_RUNNING_LION (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
#define IN_COMPILING_LION __MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
#define IN_COMPILING_MOUNTAIN __MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
/** -----------------------------------------
- There are 2 sets of colors, one for an active (key) state and one for an inactivate state
- Each set contains 3 colors. 2 colors for the start and end of the title gradient, and another color to draw the separator line on the bottom
- These colors are meant to mimic the color of the default titlebar (taken from OS X 10.6), but are subject
to change at any time
----------------------------------------- **/
#define IN_COLOR_MAIN_START [NSColor colorWithDeviceWhite:0.659 alpha:1.0]
#define IN_COLOR_MAIN_END [NSColor colorWithDeviceWhite:0.812 alpha:1.0]
#define IN_COLOR_MAIN_BOTTOM [NSColor colorWithDeviceWhite:0.318 alpha:1.0]
#define IN_COLOR_NOTMAIN_START [NSColor colorWithDeviceWhite:0.851 alpha:1.0]
#define IN_COLOR_NOTMAIN_END [NSColor colorWithDeviceWhite:0.929 alpha:1.0]
#define IN_COLOR_NOTMAIN_BOTTOM [NSColor colorWithDeviceWhite:0.600 alpha:1.0]
/** Lion */
#define IN_COLOR_MAIN_START_L [NSColor colorWithDeviceWhite:0.66 alpha:1.0]
#define IN_COLOR_MAIN_END_L [NSColor colorWithDeviceWhite:0.9 alpha:1.0]
#define IN_COLOR_MAIN_BOTTOM_L [NSColor colorWithDeviceWhite:0.408 alpha:1.0]
#define IN_COLOR_NOTMAIN_START_L [NSColor colorWithDeviceWhite:0.878 alpha:1.0]
#define IN_COLOR_NOTMAIN_END_L [NSColor colorWithDeviceWhite:0.976 alpha:1.0]
#define IN_COLOR_NOTMAIN_BOTTOM_L [NSColor colorWithDeviceWhite:0.655 alpha:1.0]
/** Corner clipping radius **/
#if IN_COMPILING_MOUNTAIN
const CGFloat INCornerClipRadius = 6.0;
#else
const CGFloat INCornerClipRadius = 4.0;
#endif
const CGFloat INButtonTopOffset = 3.0;
NS_INLINE CGFloat INMidHeight(NSRect aRect){
return (aRect.size.height * (CGFloat)0.5);
}
static inline CGImageRef createNoiseImageRef(NSUInteger width, NSUInteger height, CGFloat factor)
{
NSUInteger size = width*height;
char *rgba = (char *)malloc(size); srand(124);
for(NSUInteger i=0; i < size; ++i){rgba[i] = rand()%256*factor;}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapContext =
CGBitmapContextCreate(rgba, width, height, 8, width, colorSpace, kCGImageAlphaNone);
CGImageRef image = CGBitmapContextCreateImage(bitmapContext);
CFRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);
free(rgba);
return image;
}
static inline CGPathRef createClippingPathWithRectAndRadius(NSRect rect, CGFloat radius)
{
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, NSMinX(rect), NSMinY(rect));
CGPathAddLineToPoint(path, NULL, NSMinX(rect), NSMaxY(rect)-radius);
CGPathAddArcToPoint(path, NULL, NSMinX(rect), NSMaxY(rect), NSMinX(rect)+radius, NSMaxY(rect), radius);
CGPathAddLineToPoint(path, NULL, NSMaxX(rect)-radius, NSMaxY(rect));
CGPathAddArcToPoint(path, NULL, NSMaxX(rect), NSMaxY(rect), NSMaxX(rect), NSMaxY(rect)-radius, radius);
CGPathAddLineToPoint(path, NULL, NSMaxX(rect), NSMinY(rect));
CGPathCloseSubpath(path);
return path;
}
static inline CGGradientRef createGradientWithColors(NSColor *startingColor, NSColor *endingColor)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGFloat startingComponents[2];
[startingColor getWhite:&startingComponents[0] alpha:&startingComponents[1]];
CGFloat endingComponents[2];
[endingColor getWhite:&endingComponents[0] alpha:&endingComponents[1]];
CGFloat compontents[4] = {
startingComponents[0],
startingComponents[1],
endingComponents[0],
endingComponents[1],
};
CGFloat locations[2] = {
0.0f,
1.0f,
};
CGGradientRef gradient =
CGGradientCreateWithColorComponents(colorSpace,
(const CGFloat *)&compontents,
(const CGFloat *)&locations, 2);
CGColorSpaceRelease(colorSpace);
return gradient;
}
@interface INAppStoreWindowDelegateProxy : NSObject <NSWindowDelegate>
@property (nonatomic, assign) id<NSWindowDelegate> secondaryDelegate;
@end
@implementation INAppStoreWindowDelegateProxy
@synthesize secondaryDelegate = _secondaryDelegate;
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([_secondaryDelegate respondsToSelector:[anInvocation selector]]) {
[anInvocation invokeWithTarget:_secondaryDelegate];
} else {
[super forwardInvocation:anInvocation];
}
}
- (NSRect)window:(INAppStoreWindow *)window willPositionSheet:(NSWindow *)sheet usingRect:(NSRect)rect
{
rect.origin.y = NSHeight(window.frame)-window.titleBarHeight;
return rect;
}
@end
@interface INAppStoreWindow ()
- (void)_doInitialWindowSetup;
- (void)_createTitlebarView;
- (void)_setupTrafficLightsTrackingArea;
- (void)_recalculateFrameForTitleBarView;
- (void)_layoutTrafficLightsAndContent;
- (CGFloat)_minimumTitlebarHeight;
- (void)_displayWindowAndTitlebar;
- (void)_hideTitleBarView:(BOOL)hidden;
- (CGFloat)_defaultTrafficLightLeftMargin;
- (CGFloat)_trafficLightSeparation;
@end
@implementation INTitlebarView
- (void)drawRect:(NSRect)dirtyRect
{
INAppStoreWindow *window = (INAppStoreWindow *)[self window];
BOOL drawsAsMainWindow = ([window isMainWindow] && [[NSApplication sharedApplication] isActive]);
NSRect drawingRect = [self bounds];
if ( window.titleBarDrawingBlock ) {
CGPathRef clippingPath = createClippingPathWithRectAndRadius(drawingRect, INCornerClipRadius);
window.titleBarDrawingBlock(drawsAsMainWindow, NSRectToCGRect(drawingRect), clippingPath);
CGPathRelease(clippingPath);
} else {
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
NSColor *startColor = nil;
NSColor *endColor = nil;
if (IN_RUNNING_LION) {
startColor = drawsAsMainWindow ? IN_COLOR_MAIN_START_L : IN_COLOR_NOTMAIN_START_L;
endColor = drawsAsMainWindow ? IN_COLOR_MAIN_END_L : IN_COLOR_NOTMAIN_END_L;
} else {
startColor = drawsAsMainWindow ? IN_COLOR_MAIN_START : IN_COLOR_NOTMAIN_START;
endColor = drawsAsMainWindow ? IN_COLOR_MAIN_END : IN_COLOR_NOTMAIN_END;
}
NSRect clippingRect = drawingRect;
#if IN_COMPILING_LION
if((([window styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask)){
[[NSColor blackColor] setFill];
[[NSBezierPath bezierPathWithRect:self.bounds] fill];
}
#endif
clippingRect.size.height -= 1;
CGPathRef clippingPath = createClippingPathWithRectAndRadius(clippingRect, INCornerClipRadius);
CGContextAddPath(context, clippingPath);
CGContextClip(context);
CGPathRelease(clippingPath);
CGGradientRef gradient = createGradientWithColors(startColor, endColor);
CGContextDrawLinearGradient(context, gradient, CGPointMake(NSMidX(drawingRect), NSMinY(drawingRect)),
CGPointMake(NSMidX(drawingRect), NSMaxY(drawingRect)), 0);
CGGradientRelease(gradient);
if ([window showsBaselineSeparator]) {
NSColor *bottomColor = nil;
if (IN_RUNNING_LION) {
bottomColor = drawsAsMainWindow ? IN_COLOR_MAIN_BOTTOM_L : IN_COLOR_NOTMAIN_BOTTOM_L;
} else {
bottomColor = drawsAsMainWindow ? IN_COLOR_MAIN_BOTTOM : IN_COLOR_NOTMAIN_BOTTOM;
}
NSRect bottomRect = NSMakeRect(0.0, NSMinY(drawingRect), NSWidth(drawingRect), 1.0);
[bottomColor set];
NSRectFill(bottomRect);
if (IN_RUNNING_LION) {
bottomRect.origin.y += 1.0;
[[NSColor colorWithDeviceWhite:1.0 alpha:0.12] setFill];
[[NSBezierPath bezierPathWithRect:bottomRect] fill];
}
}
if (IN_RUNNING_LION && drawsAsMainWindow) {
static CGImageRef noisePattern = nil;
if (noisePattern == nil) {
noisePattern = createNoiseImageRef(128, 128, 0.015);
}
CGPathRef noiseClippingPath =
createClippingPathWithRectAndRadius(NSInsetRect(drawingRect, 1, 1), INCornerClipRadius);
CGContextAddPath(context, noiseClippingPath);
CGContextClip(context);
CGPathRelease(noiseClippingPath);
CGContextSetBlendMode(context, kCGBlendModePlusLighter);
CGRect noisePatternRect = CGRectZero;
noisePatternRect.size = CGSizeMake(CGImageGetWidth(noisePattern), CGImageGetHeight(noisePattern));
CGContextDrawTiledImage(context, noisePatternRect, noisePattern);
}
}
}
- (void)mouseUp:(NSEvent *)theEvent
{
if ([theEvent clickCount] == 2) {
// Get settings from "System Preferences" > "Appearance" > "Double-click on windows title bar to minimize"
NSString *const MDAppleMiniaturizeOnDoubleClickKey = @"AppleMiniaturizeOnDoubleClick";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults addSuiteNamed:NSGlobalDomain];
BOOL shouldMiniaturize = [[userDefaults objectForKey:MDAppleMiniaturizeOnDoubleClickKey] boolValue];
if (shouldMiniaturize) {
[[self window] miniaturize:self];
}
}
}
@end
@implementation INAppStoreWindow{
CGFloat _cachedTitleBarHeight;
BOOL _setFullScreenButtonRightMargin;
INAppStoreWindowDelegateProxy *_delegateProxy;
}
@synthesize titleBarView = _titleBarView;
@synthesize titleBarHeight = _titleBarHeight;
@synthesize centerFullScreenButton = _centerFullScreenButton;
@synthesize centerTrafficLightButtons = _centerTrafficLightButtons;
@synthesize hideTitleBarInFullScreen = _hideTitleBarInFullScreen;
@synthesize titleBarDrawingBlock = _titleBarDrawingBlock;
@synthesize showsBaselineSeparator = _showsBaselineSeparator;
@synthesize fullScreenButtonRightMargin = _fullScreenButtonRightMargin;
@synthesize trafficLightButtonsLeftMargin = _trafficLightButtonsLeftMargin;
#pragma mark -
#pragma mark Initialization
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
if ((self = [super initWithContentRect:contentRect styleMask:aStyle backing:bufferingType defer:flag])) {
[self _doInitialWindowSetup];
}
return self;
}
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag screen:(NSScreen *)screen
{
if ((self = [super initWithContentRect:contentRect styleMask:aStyle backing:bufferingType defer:flag screen:screen])) {
[self _doInitialWindowSetup];
}
return self;
}
#pragma mark -
#pragma mark Memory Management
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self setDelegate:nil];
#if !__has_feature(objc_arc)
[_delegateProxy release];
[_titleBarView release];
[super dealloc];
#endif
}
#pragma mark -
#pragma mark NSWindow Overrides
- (void)becomeKeyWindow
{
[super becomeKeyWindow];
[_titleBarView setNeedsDisplay:YES];
}
- (void)resignKeyWindow
{
[super resignKeyWindow];
[_titleBarView setNeedsDisplay:YES];
}
- (void)becomeMainWindow
{
[super becomeMainWindow];
[_titleBarView setNeedsDisplay:YES];
}
- (void)resignMainWindow
{
[super resignMainWindow];
[_titleBarView setNeedsDisplay:YES];
}
#pragma mark -
#pragma mark Accessors
- (void)setTitleBarView:(NSView *)newTitleBarView
{
if ((_titleBarView != newTitleBarView) && newTitleBarView) {
[_titleBarView removeFromSuperview];
#if __has_feature(objc_arc)
_titleBarView = newTitleBarView;
#else
[_titleBarView release];
_titleBarView = [newTitleBarView retain];
#endif
// Configure the view properties and add it as a subview of the theme frame
NSView *themeFrame = [[self contentView] superview];
NSView *firstSubview = [[themeFrame subviews] objectAtIndex:0];
[_titleBarView setAutoresizingMask:(NSViewMinYMargin | NSViewWidthSizable)];
[self _recalculateFrameForTitleBarView];
[themeFrame addSubview:_titleBarView positioned:NSWindowBelow relativeTo:firstSubview];
[self _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
}
}
- (NSView *)titleBarView
{
return _titleBarView;
}
- (void)setTitleBarHeight:(CGFloat)newTitleBarHeight
{
if (_titleBarHeight != newTitleBarHeight) {
_cachedTitleBarHeight = MAX(22, newTitleBarHeight);
_titleBarHeight = _cachedTitleBarHeight;
[self _recalculateFrameForTitleBarView];
[self _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
}
}
- (CGFloat)titleBarHeight
{
return _titleBarHeight;
}
- (void)setShowsBaselineSeparator:(BOOL)showsBaselineSeparator
{
if (_showsBaselineSeparator != showsBaselineSeparator) {
_showsBaselineSeparator = showsBaselineSeparator;
[self.titleBarView setNeedsDisplay:YES];
}
}
- (BOOL)showsBaselineSeparator
{
return _showsBaselineSeparator;
}
- (void)setTrafficLightButtonsLeftMargin:(CGFloat)newTrafficLightButtonsLeftMargin
{
if (_trafficLightButtonsLeftMargin != newTrafficLightButtonsLeftMargin) {
_trafficLightButtonsLeftMargin = newTrafficLightButtonsLeftMargin;
[self _recalculateFrameForTitleBarView];
[self _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
[self _setupTrafficLightsTrackingArea];
}
}
- (CGFloat)trafficLightButtonsLeftMargin
{
return _trafficLightButtonsLeftMargin;
}
- (void)setFullScreenButtonRightMargin:(CGFloat)newFullScreenButtonRightMargin
{
if (_fullScreenButtonRightMargin != newFullScreenButtonRightMargin) {
_setFullScreenButtonRightMargin = YES;
_fullScreenButtonRightMargin = newFullScreenButtonRightMargin;
[self _recalculateFrameForTitleBarView];
[self _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
}
}
- (CGFloat)fullScreenButtonRightMargin
{
return _fullScreenButtonRightMargin;
}
- (void)setCenterFullScreenButton:(BOOL)centerFullScreenButton{
if( _centerFullScreenButton != centerFullScreenButton ) {
_centerFullScreenButton = centerFullScreenButton;
[self _layoutTrafficLightsAndContent];
}
}
- (void)setCenterTrafficLightButtons:(BOOL)centerTrafficLightButtons
{
if ( _centerTrafficLightButtons != centerTrafficLightButtons ) {
_centerTrafficLightButtons = centerTrafficLightButtons;
[self _layoutTrafficLightsAndContent];
[self _setupTrafficLightsTrackingArea];
}
}
- (void)setDelegate:(id<NSWindowDelegate>)anObject
{
[_delegateProxy setSecondaryDelegate:anObject];
}
- (id<NSWindowDelegate>)delegate
{
return [_delegateProxy secondaryDelegate];
}
#pragma mark -
#pragma mark Private
- (void)_doInitialWindowSetup
{
_showsBaselineSeparator = YES;
_centerTrafficLightButtons = YES;
_titleBarHeight = [self _minimumTitlebarHeight];
_trafficLightButtonsLeftMargin = [self _defaultTrafficLightLeftMargin];
[self setMovableByWindowBackground:YES];
_delegateProxy = [[INAppStoreWindowDelegateProxy alloc] init];
[super setDelegate:_delegateProxy];
/** -----------------------------------------
- The window automatically does layout every time its moved or resized, which means that the traffic lights and content view get reset at the original positions, so we need to put them back in place
- NSWindow is hardcoded to redraw the traffic lights in a specific rect, so when they are moved down, only part of the buttons get redrawn, causing graphical artifacts. Therefore, the window must be force redrawn every time it becomes key/resigns key
----------------------------------------- **/
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(_layoutTrafficLightsAndContent) name:NSWindowDidResizeNotification object:self];
[nc addObserver:self selector:@selector(_layoutTrafficLightsAndContent) name:NSWindowDidMoveNotification object:self];
[nc addObserver:self selector:@selector(_displayWindowAndTitlebar) name:NSWindowDidResignKeyNotification object:self];
[nc addObserver:self selector:@selector(_displayWindowAndTitlebar) name:NSWindowDidBecomeKeyNotification object:self];
[nc addObserver:self selector:@selector(_setupTrafficLightsTrackingArea) name:NSWindowDidBecomeKeyNotification object:self];
[nc addObserver:self selector:@selector(_displayWindowAndTitlebar) name:NSApplicationDidBecomeActiveNotification object:nil];
[nc addObserver:self selector:@selector(_displayWindowAndTitlebar) name:NSApplicationDidResignActiveNotification object:nil];
#if IN_COMPILING_LION
if (IN_RUNNING_LION) {
[nc addObserver:self selector:@selector(_setupTrafficLightsTrackingArea) name:NSWindowDidExitFullScreenNotification object:nil];
[nc addObserver:self selector:@selector(windowWillEnterFullScreen:) name:NSWindowWillEnterFullScreenNotification object:nil];
[nc addObserver:self selector:@selector(windowWillExitFullScreen:) name:NSWindowWillExitFullScreenNotification object:nil];
}
#endif
[self _createTitlebarView];
[self _layoutTrafficLightsAndContent];
[self _setupTrafficLightsTrackingArea];
}
- (void)_layoutTrafficLightsAndContent
{
NSButton *close = [self standardWindowButton:NSWindowCloseButton];
NSButton *minimize = [self standardWindowButton:NSWindowMiniaturizeButton];
NSButton *zoom = [self standardWindowButton:NSWindowZoomButton];
// Set the frame of the window buttons
NSRect closeFrame = [close frame];
NSRect minimizeFrame = [minimize frame];
NSRect zoomFrame = [zoom frame];
NSRect titleBarFrame = [_titleBarView frame];
CGFloat buttonOrigin = 0.0;
if ( self.centerTrafficLightButtons ) {
buttonOrigin = round(NSMidY(titleBarFrame) - INMidHeight(closeFrame));
} else {
buttonOrigin = NSMaxY(titleBarFrame) - NSHeight(closeFrame) - INButtonTopOffset;
}
closeFrame.origin.y = buttonOrigin;
minimizeFrame.origin.y = buttonOrigin;
zoomFrame.origin.y = buttonOrigin;
closeFrame.origin.x = _trafficLightButtonsLeftMargin;
minimizeFrame.origin.x = _trafficLightButtonsLeftMargin + [self _trafficLightSeparation];
zoomFrame.origin.x = _trafficLightButtonsLeftMargin + [self _trafficLightSeparation] * 2;
[close setFrame:closeFrame];
[minimize setFrame:minimizeFrame];
[zoom setFrame:zoomFrame];
#if IN_COMPILING_LION
// Set the frame of the FullScreen button in Lion if available
if ( IN_RUNNING_LION ) {
NSButton *fullScreen = [self standardWindowButton:NSWindowFullScreenButton];
if( fullScreen ) {
NSRect fullScreenFrame = [fullScreen frame];
if ( !_setFullScreenButtonRightMargin ) {
self.fullScreenButtonRightMargin = NSWidth([_titleBarView frame]) - NSMaxX(fullScreen.frame);
}
fullScreenFrame.origin.x = NSWidth(titleBarFrame) - NSWidth(fullScreenFrame) - _fullScreenButtonRightMargin;
if( self.centerFullScreenButton ) {
fullScreenFrame.origin.y = round(NSMidY(titleBarFrame) - INMidHeight(fullScreenFrame));
} else {
fullScreenFrame.origin.y = NSMaxY(titleBarFrame) - NSHeight(fullScreenFrame) - INButtonTopOffset;
}
[fullScreen setFrame:fullScreenFrame];
}
}
#endif
// Reposition the content view
NSView *contentView = [self contentView];
NSRect windowFrame = [self frame];
NSRect newFrame = [contentView frame];
CGFloat titleHeight = NSHeight(windowFrame) - NSHeight(newFrame);
CGFloat extraHeight = _titleBarHeight - titleHeight;
newFrame.size.height -= extraHeight;
[contentView setFrame:newFrame];
[contentView setNeedsDisplay:YES];
}
- (void)windowWillEnterFullScreen:(NSNotification *)notification
{
if (_hideTitleBarInFullScreen) {
// Recalculate the views when entering from fullscreen
_titleBarHeight = 0.0f;
[self _recalculateFrameForTitleBarView];
[self _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
[self _hideTitleBarView:YES];
}
}
- (void)windowWillExitFullScreen:(NSNotification *)notification
{
if (_hideTitleBarInFullScreen) {
_titleBarHeight = _cachedTitleBarHeight;
[self _recalculateFrameForTitleBarView];
[self _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
[self _hideTitleBarView:NO];
}
}
- (void)_createTitlebarView
{
// Create the title bar view
#if __has_feature(objc_arc)
self.titleBarView = [[INTitlebarView alloc] initWithFrame:NSZeroRect];
#else
self.titleBarView = [[[INTitlebarView alloc] initWithFrame:NSZeroRect] autorelease];
#endif
}
- (void)_hideTitleBarView:(BOOL)hidden
{
[self.titleBarView setHidden:hidden];
}
// Solution for tracking area issue thanks to @Perspx (Alex Rozanski) <https://gist.github.com/972958>
- (void)_setupTrafficLightsTrackingArea
{
[[[self contentView] superview] viewWillStartLiveResize];
[[[self contentView] superview] viewDidEndLiveResize];
}
- (void)_recalculateFrameForTitleBarView
{
NSView *themeFrame = [[self contentView] superview];
NSRect themeFrameRect = [themeFrame frame];
NSRect titleFrame = NSMakeRect(0.0, NSMaxY(themeFrameRect) - _titleBarHeight, NSWidth(themeFrameRect), _titleBarHeight);
[_titleBarView setFrame:titleFrame];
}
- (CGFloat)_minimumTitlebarHeight
{
static CGFloat minTitleHeight = 0.0;
if ( !minTitleHeight ) {
NSRect frameRect = [self frame];
NSRect contentRect = [self contentRectForFrameRect:frameRect];
minTitleHeight = NSHeight(frameRect) - NSHeight(contentRect);
}
return minTitleHeight;
}
- (CGFloat)_defaultTrafficLightLeftMargin
{
static CGFloat trafficLightLeftMargin = 0.0;
if ( !trafficLightLeftMargin ) {
NSButton *close = [self standardWindowButton:NSWindowCloseButton];
trafficLightLeftMargin = NSMinX(close.frame);
}
return trafficLightLeftMargin;
}
- (CGFloat)_trafficLightSeparation
{
static CGFloat trafficLightSeparation = 0.0;
if ( !trafficLightSeparation ) {
NSButton *close = [self standardWindowButton:NSWindowCloseButton];
NSButton *minimize = [self standardWindowButton:NSWindowMiniaturizeButton];
trafficLightSeparation = NSMinX(minimize.frame) - NSMinX(close.frame);
}
return trafficLightSeparation;
}
- (void)_displayWindowAndTitlebar
{
// Redraw the window and titlebar
[_titleBarView setNeedsDisplay:YES];
}
@end