forked from indragiek/INAppStoreWindow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINAppStoreWindow.m
1145 lines (982 loc) · 42.9 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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// 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"
#import <objc/runtime.h>
#define IN_RUNNING_LION (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
#define IN_COMPILING_LION __MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
/** -----------------------------------------
- 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]
#define IN_COLOR_MAIN_TITLE_TEXT [NSColor colorWithDeviceWhite:56.0/255.0 alpha:1.0]
#define IN_COLOR_NOTMAIN_TITLE_TEXT [NSColor colorWithDeviceWhite:56.0/255.0 alpha:0.5]
/** 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 **/
const CGFloat INCornerClipRadius = 4.0;
NS_INLINE CGFloat INMidHeight(NSRect aRect){
return (aRect.size.height * (CGFloat)0.5);
}
CF_RETURNS_RETAINED
NS_INLINE CGPathRef INCreateClippingPathWithRectAndRadius(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;
}
CF_RETURNS_RETAINED
NS_INLINE CGColorRef INCreateCGColorFromNSColor(NSColor *color)
{
NSColor *rgbColor = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
CGFloat components[4];
[rgbColor getComponents:components];
CGColorSpaceRef theColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGColorRef theColor = CGColorCreate(theColorSpace, components);
CGColorSpaceRelease(theColorSpace);
return theColor;
}
CF_RETURNS_RETAINED
NS_INLINE CGGradientRef INCreateGradientWithColors(NSColor *startingColor, NSColor *endingColor)
{
CGFloat locations[2] = {0.0f, 1.0f, };
CGColorRef cgStartingColor = INCreateCGColorFromNSColor(startingColor);
CGColorRef cgEndingColor = INCreateCGColorFromNSColor(endingColor);
#if __has_feature(objc_arc)
CFArrayRef colors = (__bridge CFArrayRef)[NSArray arrayWithObjects:(__bridge id)cgStartingColor, (__bridge id)cgEndingColor, nil];
#else
CFArrayRef colors = (CFArrayRef)[NSArray arrayWithObjects:(id)cgStartingColor, (id)cgEndingColor, nil];
#endif
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, colors, locations);
CGColorSpaceRelease(colorSpace);
CGColorRelease(cgStartingColor);
CGColorRelease(cgEndingColor);
return gradient;
}
@interface INAppStoreWindowDelegateProxy : NSProxy <NSWindowDelegate>
@property (nonatomic, assign) id<NSWindowDelegate> secondaryDelegate;
@end
@implementation INAppStoreWindowDelegateProxy
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
NSMethodSignature *signature = [[self.secondaryDelegate class] instanceMethodSignatureForSelector:selector];
if (!signature) {
signature = [super methodSignatureForSelector:selector];
}
return signature;
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ([self.secondaryDelegate respondsToSelector:aSelector]) {
return YES;
} else if (aSelector == @selector(window:willPositionSheet:usingRect:)) {
return YES;
}
return NO;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([self.secondaryDelegate respondsToSelector:[anInvocation selector]]) {
[anInvocation invokeWithTarget:self.secondaryDelegate];
}
}
- (NSRect)window:(INAppStoreWindow *)window willPositionSheet:(NSWindow *)sheet usingRect:(NSRect)rect
{
// Somehow the forwarding machinery doesn't handle this.
if ([self.secondaryDelegate respondsToSelector:_cmd]) {
return [self.secondaryDelegate window:window willPositionSheet:sheet usingRect:rect];
}
rect.origin.y = NSHeight(window.frame) - window.titleBarHeight;
return rect;
}
- (BOOL)isKindOfClass:(Class)aClass
{
if (self.secondaryDelegate) {
return [self.secondaryDelegate isKindOfClass:aClass];
}
return NO;
}
@end
@interface INAppStoreWindow ()
- (void)_doInitialWindowSetup;
- (void)_createTitlebarView;
- (void)_setupTrafficLightsTrackingArea;
- (void)_recalculateFrameForTitleBarContainer;
- (void)_repositionContentView;
- (void)_layoutTrafficLightsAndContent;
- (CGFloat)_minimumTitlebarHeight;
- (void)_displayWindowAndTitlebar;
- (void)_hideTitleBarView:(BOOL)hidden;
- (CGFloat)_defaultTrafficLightLeftMargin;
- (CGFloat)_defaultTrafficLightSeparation;
- (NSRect)_contentViewFrame;
@end
@implementation INTitlebarView
- (void)drawNoiseWithOpacity:(CGFloat)opacity
{
static CGImageRef noiseImageRef = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
NSUInteger width = 124, height = width;
NSUInteger size = width*height;
char *rgba = (char *)malloc(size); srand(120);
for(NSUInteger i=0; i < size; ++i){rgba[i] = (char)arc4random()%256;}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapContext =
CGBitmapContextCreate(rgba, width, height, 8, width, colorSpace, (CGBitmapInfo)kCGImageAlphaNone);
CFRelease(colorSpace);
noiseImageRef = CGBitmapContextCreateImage(bitmapContext);
CFRelease(bitmapContext);
free(rgba);
});
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(context);
CGContextSetAlpha(context, opacity);
CGContextSetBlendMode(context, kCGBlendModeScreen);
if ( [[self window] respondsToSelector:@selector(backingScaleFactor)] ) {
CGFloat scaleFactor = [[self window] backingScaleFactor];
CGContextScaleCTM(context, 1/scaleFactor, 1/scaleFactor);
}
CGRect imageRect = (CGRect){CGPointZero, (CGSize){CGImageGetWidth(noiseImageRef), CGImageGetHeight(noiseImageRef)}};
CGContextDrawTiledImage(context, imageRect, noiseImageRef);
CGContextRestoreGState(context);
}
- (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 = INCreateClippingPathWithRectAndRadius(drawingRect, INCornerClipRadius);
window.titleBarDrawingBlock(drawsAsMainWindow, NSRectToCGRect(drawingRect), clippingPath);
CGPathRelease(clippingPath);
} else {
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
NSColor *startColor = drawsAsMainWindow ? window.titleBarStartColor : window.inactiveTitleBarStartColor;
NSColor *endColor = drawsAsMainWindow ? window.titleBarEndColor : window.inactiveTitleBarEndColor;
if (IN_RUNNING_LION) {
startColor = startColor ?: (drawsAsMainWindow ? IN_COLOR_MAIN_START_L : IN_COLOR_NOTMAIN_START_L);
endColor = endColor ?: (drawsAsMainWindow ? IN_COLOR_MAIN_END_L : IN_COLOR_NOTMAIN_END_L);
} else {
startColor = startColor ?: (drawsAsMainWindow ? IN_COLOR_MAIN_START : IN_COLOR_NOTMAIN_START);
endColor = 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 = INCreateClippingPathWithRectAndRadius(clippingRect, INCornerClipRadius);
CGContextAddPath(context, clippingPath);
CGContextClip(context);
CGPathRelease(clippingPath);
CGGradientRef gradient = INCreateGradientWithColors(startColor, endColor);
CGContextDrawLinearGradient(context, gradient, CGPointMake(NSMidX(drawingRect), NSMinY(drawingRect)),
CGPointMake(NSMidX(drawingRect), NSMaxY(drawingRect)), 0);
CGGradientRelease(gradient);
if ([window showsBaselineSeparator]) {
NSColor *bottomColor = drawsAsMainWindow ? window.baselineSeparatorColor : window.inactiveBaselineSeparatorColor;
if (IN_RUNNING_LION) {
bottomColor = bottomColor ? bottomColor : drawsAsMainWindow ? IN_COLOR_MAIN_BOTTOM_L : IN_COLOR_NOTMAIN_BOTTOM_L;
} else {
bottomColor = bottomColor ? 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) {
CGRect noiseRect = NSInsetRect(drawingRect, 1.0, 1.0);
if (![window showsBaselineSeparator]) {
noiseRect.origin.y -= 1.0;
noiseRect.size.height += 1.0;
}
CGPathRef noiseClippingPath =
INCreateClippingPathWithRectAndRadius(noiseRect, INCornerClipRadius);
CGContextAddPath(context, noiseClippingPath);
CGContextClip(context);
CGPathRelease(noiseClippingPath);
[self drawNoiseWithOpacity:0.1];
}
}
if ([window showsTitle] && (([window styleMask] & NSFullScreenWindowMask) == 0 || window.showsTitleInFullscreen)) {
NSRect titleTextRect;
NSDictionary *titleTextStyles = nil;
[self getTitleFrame:&titleTextRect textAttributes:&titleTextStyles forWindow:window];
if (window.verticallyCenterTitle) {
titleTextRect.origin.y = floor(NSMidY(drawingRect) - (NSHeight(titleTextRect) / 2.f) + 1);
}
[window.title drawInRect:titleTextRect withAttributes:titleTextStyles];
}
}
- (void)getTitleFrame:(out NSRect *)frame textAttributes:(out NSDictionary **)attributes forWindow:(in INAppStoreWindow *)window
{
BOOL drawsAsMainWindow = ([window isMainWindow] && [[NSApplication sharedApplication] isActive]);
NSShadow *titleTextShadow = drawsAsMainWindow ? window.titleTextShadow : window.inactiveTitleTextShadow;
if (titleTextShadow == nil) {
#if __has_feature(objc_arc)
titleTextShadow = [[NSShadow alloc] init];
#else
titleTextShadow = [[[NSShadow alloc] init] autorelease];
#endif
titleTextShadow.shadowBlurRadius = 0.0;
titleTextShadow.shadowOffset = NSMakeSize(0, -1);
titleTextShadow.shadowColor = [NSColor colorWithDeviceWhite:1.0 alpha:0.5];
}
NSColor *titleTextColor = drawsAsMainWindow ? window.titleTextColor : window.inactiveTitleTextColor;
titleTextColor = titleTextColor ? titleTextColor : drawsAsMainWindow ? IN_COLOR_MAIN_TITLE_TEXT : IN_COLOR_NOTMAIN_TITLE_TEXT;
NSFont *titleFont = window.titleFont ?: [NSFont titleBarFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]];
NSDictionary *titleTextStyles = [NSDictionary dictionaryWithObjectsAndKeys:
titleFont, NSFontAttributeName,
titleTextColor, NSForegroundColorAttributeName,
titleTextShadow, NSShadowAttributeName,
nil];
NSSize titleSize = [window.title sizeWithAttributes:titleTextStyles];
NSRect titleTextRect;
titleTextRect.size = titleSize;
NSButton *docIconButton = [window standardWindowButton:NSWindowDocumentIconButton];
NSButton *versionsButton = [window standardWindowButton:NSWindowDocumentVersionsButton];
if (docIconButton) {
NSRect docIconButtonFrame = [self convertRect:docIconButton.frame fromView:docIconButton.superview];
titleTextRect.origin.x = NSMaxX(docIconButtonFrame) + 4.0;
titleTextRect.origin.y = NSMidY(docIconButtonFrame) - titleSize.height/2 + 1;
}
else if (versionsButton) {
NSRect versionsButtonFrame = [self convertRect:versionsButton.frame fromView:versionsButton.superview];
titleTextRect.origin.x = NSMinX(versionsButtonFrame) - titleSize.width - 1;
NSDocument *document = (NSDocument *)[(NSWindowController *)self.window.windowController document];
if ([document hasUnautosavedChanges] || [document isDocumentEdited]) {
titleTextRect.origin.x -= 20;
}
}
else {
titleTextRect.origin.x = NSMidX(self.bounds) - titleSize.width/2;
}
titleTextRect.origin.y = NSMaxY(self.bounds) - titleSize.height - 2.0;
if (frame) {
*frame = titleTextRect;
}
if (attributes) {
*attributes = titleTextStyles;
}
}
- (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];
BOOL shouldMiniaturize = [[userDefaults objectForKey:MDAppleMiniaturizeOnDoubleClickKey] boolValue];
if (shouldMiniaturize) {
[[self window] performMiniaturize:self];
}
}
}
@end
@interface INTitlebarContainer : NSView
@property (nonatomic) CGFloat mouseDragDetectionThreshold;
@end
@implementation INTitlebarContainer
- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
_mouseDragDetectionThreshold = 1;
}
return self;
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSWindow *window = [self window];
if ([window isMovableByWindowBackground] || ([window styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask) {
[super mouseDragged:theEvent];
return;
}
NSPoint where = [window convertBaseToScreen:[theEvent locationInWindow]];
NSPoint origin = [window frame].origin;
CGFloat deltaX = 0.0;
CGFloat deltaY = 0.0;
while ((theEvent = [NSApp nextEventMatchingMask:NSLeftMouseDownMask | NSLeftMouseDraggedMask | NSLeftMouseUpMask untilDate:[NSDate distantFuture] inMode:NSEventTrackingRunLoopMode dequeue:YES]) && ([theEvent type] != NSLeftMouseUp)) {
@autoreleasepool {
NSPoint now = [window convertBaseToScreen:[theEvent locationInWindow]];
deltaX += now.x - where.x;
deltaY += now.y - where.y;
if (fabs(deltaX) >= _mouseDragDetectionThreshold || fabs(deltaY) >= _mouseDragDetectionThreshold) {
// This part is only called if drag occurs on container view!
origin.x += deltaX;
origin.y += deltaY;
[window setFrameOrigin:origin];
deltaX = 0.0;
deltaY = 0.0;
}
where = now; // this should be inside above if but doing that results in jittering while moving the window...
}
}
}
@end
@interface INAppStoreWindowContentView : NSView
@end
@implementation INAppStoreWindowContentView
- (void)setFrame:(NSRect)frameRect
{
frameRect = [(INAppStoreWindow *)self.window _contentViewFrame];
[super setFrame:frameRect];
}
- (void)setFrameSize:(NSSize)newSize
{
newSize = [(INAppStoreWindow *)self.window _contentViewFrame].size;
[super setFrameSize:newSize];
}
@end
@implementation INAppStoreWindow{
CGFloat _cachedTitleBarHeight;
BOOL _setFullScreenButtonRightMargin;
BOOL _preventWindowFrameChange;
INAppStoreWindowDelegateProxy *_delegateProxy;
INTitlebarContainer *_titleBarContainer;
}
@synthesize titleBarView = _titleBarView;
@synthesize titleBarHeight = _titleBarHeight;
@synthesize centerFullScreenButton = _centerFullScreenButton;
@synthesize centerTrafficLightButtons = _centerTrafficLightButtons;
@synthesize verticalTrafficLightButtons = _verticalTrafficLightButtons;
@synthesize hideTitleBarInFullScreen = _hideTitleBarInFullScreen;
@synthesize titleBarDrawingBlock = _titleBarDrawingBlock;
@synthesize showsBaselineSeparator = _showsBaselineSeparator;
@synthesize fullScreenButtonRightMargin = _fullScreenButtonRightMargin;
@synthesize trafficLightButtonsLeftMargin = _trafficLightButtonsLeftMargin;
@synthesize titleBarStartColor = _titleBarStartColor;
@synthesize titleBarEndColor = _titleBarEndColor;
@synthesize baselineSeparatorColor = _baselineSeparatorColor;
@synthesize inactiveTitleBarStartColor = _inactiveTitleBarStartColor;
@synthesize inactiveTitleBarEndColor = _inactiveTitleBarEndColor;
@synthesize inactiveBaselineSeparatorColor = _inactiveBaselineSeparatorColor;
@synthesize showsDocumentProxyIcon = _showsDocumentProxyIcon;
#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;
}
- (void)setRepresentedURL:(NSURL *)url
{
[super setRepresentedURL:url];
if (_showsDocumentProxyIcon == NO) {
[[self standardWindowButton:NSWindowDocumentIconButton] setImage:nil];
}
}
#pragma mark -
#pragma mark Memory Management
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
// [self setDelegate:nil];
#if !__has_feature(objc_arc)
// [_delegateProxy release];
[_titleBarView release];
[_closeButton release];
[_minimizeButton release];
[_zoomButton release];
[_fullScreenButton release];
[super dealloc];
#endif
}
#pragma mark -
#pragma mark NSWindow Overrides
- (void)becomeKeyWindow
{
[super becomeKeyWindow];
[self _updateTitlebarView];
[self _layoutTrafficLightsAndContent];
[self _setupTrafficLightsTrackingArea];
}
- (void)resignKeyWindow
{
[super resignKeyWindow];
[self _updateTitlebarView];
[self _layoutTrafficLightsAndContent];
}
- (void)becomeMainWindow
{
[super becomeMainWindow];
[self _updateTitlebarView];
}
- (void)resignMainWindow
{
[super resignMainWindow];
[self _updateTitlebarView];
}
- (void)setContentView:(NSView *)aView
{
// Remove performance-optimized content view class when changing content views
NSView *oldView = [self contentView];
if (oldView && object_getClass(oldView) == [INAppStoreWindowContentView class]) {
object_setClass(oldView, [NSView class]);
}
[super setContentView:aView];
// Swap in performance-optimized content view class
if (aView && object_getClass(aView) == [NSView class]) {
object_setClass(aView, [INAppStoreWindowContentView class]);
}
[self _repositionContentView];
}
- (void)setTitle:(NSString *)aString
{
[super setTitle:aString];
[self _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
}
- (void)setMaxSize:(NSSize)size
{
[super setMaxSize:size];
[self _layoutTrafficLightsAndContent];
}
- (void)setMinSize:(NSSize)size
{
[super setMinSize:size];
[self _layoutTrafficLightsAndContent];
}
#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
[_titleBarView setFrame:[_titleBarContainer bounds]];
[_titleBarView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[_titleBarContainer addSubview:_titleBarView];
}
}
- (NSView *)titleBarView
{
return _titleBarView;
}
- (void)setTitleBarHeight:(CGFloat)newTitleBarHeight
{
if (_titleBarHeight != newTitleBarHeight) {
_cachedTitleBarHeight = newTitleBarHeight;
_titleBarHeight = _cachedTitleBarHeight;
[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 _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
[self _setupTrafficLightsTrackingArea];
}
}
- (CGFloat)trafficLightButtonsLeftMargin
{
return _trafficLightButtonsLeftMargin;
}
- (void)setFullScreenButtonRightMargin:(CGFloat)newFullScreenButtonRightMargin
{
if (_fullScreenButtonRightMargin != newFullScreenButtonRightMargin) {
_setFullScreenButtonRightMargin = YES;
_fullScreenButtonRightMargin = newFullScreenButtonRightMargin;
[self _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
}
}
- (CGFloat)fullScreenButtonRightMargin
{
return _fullScreenButtonRightMargin;
}
- (void)setShowsTitle:(BOOL)showsTitle {
if (_showsTitle != showsTitle) {
_showsTitle = showsTitle;
[self _displayWindowAndTitlebar];
}
}
- (void)setShowsDocumentProxyIcon:(BOOL)showsDocumentProxyIcon {
if (_showsDocumentProxyIcon != showsDocumentProxyIcon) {
_showsDocumentProxyIcon = showsDocumentProxyIcon;
[self _displayWindowAndTitlebar];
}
}
- (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)setVerticalTrafficLightButtons:(BOOL)verticalTrafficLightButtons
{
if ( _verticalTrafficLightButtons != verticalTrafficLightButtons ) {
_verticalTrafficLightButtons = verticalTrafficLightButtons;
[self _layoutTrafficLightsAndContent];
[self _setupTrafficLightsTrackingArea];
}
}
- (void)setVerticallyCenterTitle:(BOOL)verticallyCenterTitle
{
if ( _verticallyCenterTitle != verticallyCenterTitle ) {
_verticallyCenterTitle = verticallyCenterTitle;
[self _displayWindowAndTitlebar];
}
}
- (void)setTrafficLightSeparation:(CGFloat)trafficLightSeparation
{
if (_trafficLightSeparation != trafficLightSeparation) {
_trafficLightSeparation = trafficLightSeparation;
[self _layoutTrafficLightsAndContent];
[self _setupTrafficLightsTrackingArea];
}
}
- (void)setMouseDragDetectionThreshold:(CGFloat)mouseDragDetectionThreshold
{
_titleBarContainer.mouseDragDetectionThreshold = mouseDragDetectionThreshold;
}
- (CGFloat)mouseDragDetectionThreshold {
return _titleBarContainer.mouseDragDetectionThreshold;
}
- (void)setDelegate:(id<NSWindowDelegate>)anObject
{
[_delegateProxy setSecondaryDelegate:anObject];
[super setDelegate:nil];
[super setDelegate:_delegateProxy];
}
- (id<NSWindowDelegate>)delegate
{
return [_delegateProxy secondaryDelegate];
}
- (void)setCloseButton:(INWindowButton *)closeButton {
if (_closeButton != closeButton) {
[_closeButton removeFromSuperview];
_closeButton = closeButton;
if (_closeButton) {
_closeButton.target = self;
_closeButton.action = @selector(performClose:);
[_closeButton setFrameOrigin:[[self standardWindowButton:NSWindowCloseButton] frame].origin];
[_closeButton.cell accessibilitySetOverrideValue:NSAccessibilityCloseButtonSubrole forAttribute:NSAccessibilitySubroleAttribute];
[_closeButton.cell accessibilitySetOverrideValue:NSAccessibilityRoleDescription(NSAccessibilityButtonRole, NSAccessibilityCloseButtonSubrole) forAttribute:NSAccessibilityRoleDescriptionAttribute];
[[self themeFrameView] addSubview:_closeButton];
}
}
}
- (void)setMinimizeButton:(INWindowButton *)minimizeButton {
if (_minimizeButton != minimizeButton) {
[_minimizeButton removeFromSuperview];
_minimizeButton = minimizeButton;
if (_minimizeButton) {
_minimizeButton.target = self;
_minimizeButton.action = @selector(performMiniaturize:);
[_minimizeButton setFrameOrigin:[[self standardWindowButton:NSWindowMiniaturizeButton] frame].origin];
[_minimizeButton.cell accessibilitySetOverrideValue:NSAccessibilityMinimizeButtonSubrole forAttribute:NSAccessibilitySubroleAttribute];
[_minimizeButton.cell accessibilitySetOverrideValue:NSAccessibilityRoleDescription(NSAccessibilityButtonRole, NSAccessibilityMinimizeButtonSubrole) forAttribute:NSAccessibilityRoleDescriptionAttribute];
[[self themeFrameView] addSubview:_minimizeButton];
}
}
}
- (void)setZoomButton:(INWindowButton *)zoomButton {
if (_zoomButton != zoomButton) {
[_zoomButton removeFromSuperview];
_zoomButton = zoomButton;
if (_zoomButton) {
_zoomButton.target = self;
_zoomButton.action = @selector(performZoom:);
[_zoomButton setFrameOrigin:[[self standardWindowButton:NSWindowZoomButton] frame].origin];
[_zoomButton.cell accessibilitySetOverrideValue:NSAccessibilityZoomButtonSubrole forAttribute:NSAccessibilitySubroleAttribute];
[_zoomButton.cell accessibilitySetOverrideValue:NSAccessibilityRoleDescription(NSAccessibilityButtonRole, NSAccessibilityZoomButtonSubrole) forAttribute:NSAccessibilityRoleDescriptionAttribute];
[[self themeFrameView] addSubview:_zoomButton];
}
}
}
- (void)setFullScreenButton:(INWindowButton *)fullScreenButton {
if (_fullScreenButton != fullScreenButton) {
[_fullScreenButton removeFromSuperview];
_fullScreenButton = fullScreenButton;
if (_fullScreenButton) {
_fullScreenButton.target = self;
_fullScreenButton.action = @selector(toggleFullScreen:);
[_fullScreenButton setFrameOrigin:[[self standardWindowButton:NSWindowFullScreenButton] frame].origin];
[_fullScreenButton.cell accessibilitySetOverrideValue:NSAccessibilityFullScreenButtonSubrole forAttribute:NSAccessibilitySubroleAttribute];
[_fullScreenButton.cell accessibilitySetOverrideValue:NSAccessibilityRoleDescription(NSAccessibilityButtonRole, NSAccessibilityFullScreenButtonSubrole) forAttribute:NSAccessibilityRoleDescriptionAttribute];
[[self themeFrameView] addSubview:_fullScreenButton];
}
}
}
- (void)setStyleMask:(NSUInteger)styleMask
{
_preventWindowFrameChange = YES;
[super setStyleMask:styleMask];
_preventWindowFrameChange = NO;
}
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag
{
if (!_preventWindowFrameChange)
[super setFrame:frameRect display:flag];
}
- (void)setFrame:(NSRect)frameRect display:(BOOL)displayFlag animate:(BOOL)animateFlag
{
if (!_preventWindowFrameChange)
[super setFrame:frameRect display:displayFlag animate:animateFlag];
}
#pragma mark -
#pragma mark Private
- (void)_doInitialWindowSetup
{
_showsBaselineSeparator = YES;
_centerTrafficLightButtons = YES;
_titleBarHeight = [self _minimumTitlebarHeight];
_cachedTitleBarHeight = _titleBarHeight;
_trafficLightButtonsLeftMargin = [self _defaultTrafficLightLeftMargin];
_delegateProxy = [INAppStoreWindowDelegateProxy alloc];
_trafficLightButtonsTopMargin = 3.f;
_fullScreenButtonTopMargin = 3.f;
_trafficLightSeparation = [self _defaultTrafficLightSeparation];
[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(_layoutTrafficLightsAndContent) name:NSWindowDidEndSheetNotification object:self];
[nc addObserver:self selector:@selector(_updateTitlebarView) name:NSApplicationDidBecomeActiveNotification object:nil];
[nc addObserver:self selector:@selector(_updateTitlebarView) name:NSApplicationDidResignActiveNotification object:nil];
#if IN_COMPILING_LION
if (IN_RUNNING_LION) {
[nc addObserver:self selector:@selector(windowDidExitFullScreen:) name:NSWindowDidExitFullScreenNotification object:self];
[nc addObserver:self selector:@selector(windowWillEnterFullScreen:) name:NSWindowWillEnterFullScreenNotification object:self];
[nc addObserver:self selector:@selector(windowWillExitFullScreen:) name:NSWindowWillExitFullScreenNotification object:self];
}
#endif
[self _createTitlebarView];
[self _layoutTrafficLightsAndContent];
[self _setupTrafficLightsTrackingArea];
}
- (NSButton *)_windowButtonToLayout:(NSWindowButton)defaultButtonType orUserProvided:(NSButton *)userButton {
NSButton *defaultButton = [self standardWindowButton:defaultButtonType];
if (userButton) {
[defaultButton setHidden:YES];
defaultButton = userButton;
} else if ([defaultButton superview] != [self themeFrameView]) {
[defaultButton setHidden:NO];
}
return defaultButton;
}
- (NSButton *)_closeButtonToLayout {
return [self _windowButtonToLayout:NSWindowCloseButton orUserProvided:self.closeButton];
}
- (NSButton *)_minimizeButtonToLayout {
return [self _windowButtonToLayout:NSWindowMiniaturizeButton orUserProvided:self.minimizeButton];
}
- (NSButton *)_zoomButtonToLayout {
return [self _windowButtonToLayout:NSWindowZoomButton orUserProvided:self.zoomButton];
}
- (NSButton *)_fullScreenButtonToLayout {
return [self _windowButtonToLayout:NSWindowFullScreenButton orUserProvided:self.fullScreenButton];
}
- (void)_layoutTrafficLightsAndContent
{
// Reposition/resize the title bar view as needed
[self _recalculateFrameForTitleBarContainer];
NSButton *close = [self _closeButtonToLayout];
NSButton *minimize = [self _minimizeButtonToLayout];
NSButton *zoom = [self _zoomButtonToLayout];
// Set the frame of the window buttons
NSRect closeFrame = [close frame];
NSRect minimizeFrame = [minimize frame];
NSRect zoomFrame = [zoom frame];
NSRect titleBarFrame = [_titleBarContainer frame];
CGFloat buttonOrigin = 0.0;
if (!self.verticalTrafficLightButtons) {
if (self.centerTrafficLightButtons) {
buttonOrigin = round(NSMidY(titleBarFrame) - INMidHeight(closeFrame));
} else {
buttonOrigin = NSMaxY(titleBarFrame) - NSHeight(closeFrame) - self.trafficLightButtonsTopMargin;
}
closeFrame.origin.y = buttonOrigin;
minimizeFrame.origin.y = buttonOrigin;
zoomFrame.origin.y = buttonOrigin;
closeFrame.origin.x = self.trafficLightButtonsLeftMargin;
minimizeFrame.origin.x = NSMaxX(closeFrame) + self.trafficLightSeparation;
zoomFrame.origin.x = NSMaxX(minimizeFrame) + self.trafficLightSeparation;
} else {
CGFloat groupHeight = NSHeight(closeFrame) + NSHeight(minimizeFrame) + NSHeight(zoomFrame) + 2.f * (self.trafficLightSeparation - 2.f);
if (self.centerTrafficLightButtons) {
buttonOrigin = round(NSMidY(titleBarFrame) - groupHeight / 2.f);
} else {
buttonOrigin = NSMaxY(titleBarFrame) - groupHeight - self.trafficLightButtonsTopMargin;
}
closeFrame.origin.x = self.trafficLightButtonsLeftMargin;
minimizeFrame.origin.x = self.trafficLightButtonsLeftMargin;
zoomFrame.origin.x = self.trafficLightButtonsLeftMargin;
zoomFrame.origin.y = buttonOrigin;
minimizeFrame.origin.y = NSMaxY(zoomFrame) + self.trafficLightSeparation - 2.f;
closeFrame.origin.y = NSMaxY(minimizeFrame) + self.trafficLightSeparation - 2.f;
}
[close setFrame:closeFrame];
[minimize setFrame:minimizeFrame];
[zoom setFrame:zoomFrame];
NSButton *docIconButton = [self standardWindowButton:NSWindowDocumentIconButton];
if (docIconButton) {
NSRect docButtonIconFrame = [docIconButton frame];
if (self.verticallyCenterTitle) {
docButtonIconFrame.origin.y = floor(NSMidY(titleBarFrame) - INMidHeight(docButtonIconFrame));
} else {
docButtonIconFrame.origin.y = NSMaxY(titleBarFrame) - NSHeight(docButtonIconFrame);
}
[docIconButton setFrame:docButtonIconFrame];
}
#if IN_COMPILING_LION
// Set the frame of the FullScreen button in Lion if available
if (IN_RUNNING_LION) {
NSButton *fullScreen = [self _fullScreenButtonToLayout];
if (fullScreen) {
NSRect fullScreenFrame = [fullScreen frame];
if (!_setFullScreenButtonRightMargin) {
self.fullScreenButtonRightMargin = NSWidth([_titleBarContainer frame]) - NSMaxX(fullScreen.frame);
}
fullScreenFrame.origin.x = NSWidth(titleBarFrame) - NSWidth(fullScreenFrame) - _fullScreenButtonRightMargin;
if (self.centerFullScreenButton) {
fullScreenFrame.origin.y = floor(NSMidY(titleBarFrame) - INMidHeight(fullScreenFrame));
} else {
fullScreenFrame.origin.y = NSMaxY(titleBarFrame) - NSHeight(fullScreenFrame) - self.fullScreenButtonTopMargin;
}
[fullScreen setFrame:fullScreenFrame];
}
NSButton *versionsButton = [self standardWindowButton:NSWindowDocumentVersionsButton];
if (versionsButton) {
NSRect versionsButtonFrame = [versionsButton frame];
if (self.verticallyCenterTitle) {
versionsButtonFrame.origin.y = floor(NSMidY(titleBarFrame) - INMidHeight(versionsButtonFrame));
} else {
versionsButtonFrame.origin.y = NSMaxY(titleBarFrame) - NSHeight(versionsButtonFrame);
}
[versionsButton setFrame:versionsButtonFrame];
// Also ensure that the title font is set
if (self.titleFont) {
[versionsButton setFont:self.titleFont];
}
}
for (id subview in [[[self contentView] superview] subviews]) {
if ([subview isKindOfClass:[NSTextField class]]) {
NSTextField *textField = (NSTextField *)subview;
NSRect textFieldFrame = [textField frame];
if (self.verticallyCenterTitle) {
textFieldFrame.origin.y = round(NSMidY(titleBarFrame) - INMidHeight(textFieldFrame));
} else {
textFieldFrame.origin.y = NSMaxY(titleBarFrame) - NSHeight(textFieldFrame);
}
[textField setFrame:textFieldFrame];
// Also ensure that the font is set
if (self.titleFont) {
[textField setFont:self.titleFont];
}
}
}
}
#endif
[self _repositionContentView];
}
- (void)undoManagerDidCloseUndoGroupNotification:(NSNotification *)notification {
[self _displayWindowAndTitlebar];