-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathINAppStoreWindow.m
executable file
·1474 lines (1246 loc) · 195 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 (c) 2011-2014 Indragie Karunaratne. All rights reserved.
// Copyright (c) 2014 Petroules Corporation. All rights reserved.
//
// Licensed under the BSD 2-clause License. See LICENSE file distributed in the source
// code of this project.
//
#import "INAppStoreWindow.h"
#import "INAppStoreWindowCompatibility.h"
#import "INWindowButton.h"
#import <Carbon/Carbon.h>
#import <objc/runtime.h>
const NSInteger kINAppStoreWindowSmallBottomBarHeight = 22;
const NSInteger kINAppStoreWindowLargeBottomBarHeight = 32;
static NSString * const INWindowBackgroundPatternOverlayLayer;
static NSString * const INWindowBackgroundPatternOverlayLayer2x;
/** Values chosen to match the defaults in OS X 10.9, which may change in future versions **/
const CGFloat INWindowDocumentIconButtonOriginY = 3.f;
const CGFloat INWindowDocumentVersionsButtonOriginY = 2.f;
const CGFloat INWindowDocumentVersionsDividerOriginY = 2.f;
/** Corner clipping radius **/
const CGFloat INCornerClipRadius = 4.0;
/** Padding used between traffic light/fullscreen buttons and title **/
const NSSize INTitleMargins = {8.0, 2.0};
/** Title offsets used when the document button is showing */
const NSSize INTitleDocumentButtonOffset = {4.0, 1.0};
/** X offset used when the document status label is showing */
const CGFloat INTitleDocumentStatusXOffset = -20.0;
/** X offset used when the versions button is showing */
const CGFloat INTitleVersionsButtonXOffset = -1.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;
}
NS_INLINE void INApplyClippingPath(CGPathRef path, CGContextRef ctx) {
CGContextAddPath(ctx, path);
CGContextClip(ctx);
}
NS_INLINE void INApplyClippingPathInCurrentContext(CGPathRef path) {
INApplyClippingPath(path, [[NSGraphicsContext currentContext] graphicsPort]);
}
@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 - window.toolbarHeight;
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)_setTitleBarViewHidden:(BOOL)hidden;
- (CGFloat)_defaultTrafficLightLeftMargin;
- (CGFloat)_defaultTrafficLightSeparation;
- (NSRect)_contentViewFrame;
- (NSButton *)_closeButtonToLayout;
- (NSButton *)_minimizeButtonToLayout;
- (NSButton *)_zoomButtonToLayout;
- (NSButton *)_fullScreenButtonToLayout;
@end
@implementation INWindowBackgroundView
/*!
Color used to draw the noise pattern over the window's title bar gradient.
*/
+ (NSColor *)windowPatternOverlayColor
{
static NSColor *noiseColor = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithData:[[NSData alloc] initWithBase64Encoding:INWindowBackgroundPatternOverlayLayer]];
NSImage *image = [[NSImage alloc] initWithSize:rep.size];
[image addRepresentation:rep];
[image addRepresentation:[[NSBitmapImageRep alloc] initWithData:[[NSData alloc] initWithBase64Encoding:INWindowBackgroundPatternOverlayLayer2x]]];
noiseColor = [NSColor colorWithPatternImage:image];
});
return noiseColor;
}
- (void)drawWindowPatternOverlayColorInRect:(NSRect)rect forEdge:(NSRectEdge)edge
{
NSAssert(edge == NSMinYEdge || edge == NSMaxYEdge, @"edge must be NSMinYEdge or NSMaxYEdge");
// known constants used by OS X
const CGFloat opacity = 0.3;
const CGSize patternPhase = CGSizeMake(edge == NSMaxYEdge ? -5 : 0, self.window.frame.size.height);
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(context);
CGContextSetAlpha(context, opacity);
CGContextSetBlendMode(context, kCGBlendModeOverlay);
CGContextSetPatternPhase(context, patternPhase);
[[INWindowBackgroundView windowPatternOverlayColor] setFill];
CGContextFillRect(context, NSRectToCGRect(rect));
CGContextRestoreGState(context);
}
- (void)drawSeparatorInRect:(NSRect)separatorFrame forEdge:(NSRectEdge)edge
{
NSAssert(edge == NSMinYEdge || edge == NSMaxYEdge, @"edge must be NSMinYEdge or NSMaxYEdge");
CGFloat multiplier = 0;
switch (edge) {
case NSMinYEdge:
multiplier = -1;
break;
case NSMaxYEdge:
multiplier = 1;
break;
}
INAppStoreWindow *window = (INAppStoreWindow *)self.window;
BOOL drawsAsMainWindow = (window.isMainWindow && [NSApplication sharedApplication].isActive);
NSColor *bottomColor = drawsAsMainWindow ? window.baselineSeparatorColor : window.inactiveBaselineSeparatorColor;
bottomColor = bottomColor ? bottomColor : [INAppStoreWindow defaultBaselineSeparatorColor:drawsAsMainWindow];
[bottomColor set];
NSRectFill(separatorFrame);
if (INRunningLion()) {
separatorFrame.origin.y += (separatorFrame.size.height * multiplier);
separatorFrame.size.height = 1.0;
[[NSColor colorWithDeviceWhite:1.0 alpha:0.12] setFill];
[[NSBezierPath bezierPathWithRect:separatorFrame] fill];
}
}
- (void)drawBackgroundGradientInRect:(NSRect)drawingRect forEdge:(NSRectEdge)edge
{
NSAssert(edge == NSMinYEdge || edge == NSMaxYEdge, @"edge must be NSMinYEdge or NSMaxYEdge");
INAppStoreWindow *window = (INAppStoreWindow *)self.window;
BOOL drawsAsMainWindow = (window.isMainWindow && [NSApplication sharedApplication].isActive);
NSGradient *gradient = nil;
if (edge == NSMaxYEdge) {
gradient = drawsAsMainWindow ? window.titleBarGradient : window.inactiveTitleBarGradient;
gradient = gradient ? gradient : [INAppStoreWindow defaultTitleBarGradient:drawsAsMainWindow];
} else if (edge == NSMinYEdge) {
gradient = drawsAsMainWindow ? window.bottomBarGradient : window.inactiveBottomBarGradient;
gradient = gradient ? gradient : [INAppStoreWindow defaultBottomBarGradient:drawsAsMainWindow];
}
[gradient drawInRect:drawingRect angle:self.isFlipped ? -90 : 90];
}
- (void)drawWindowBackgroundLayersInRect:(NSRect)drawingRect forEdge:(NSRectEdge)drawingEdge showsSeparator:(BOOL)showsSeparator clippingPath:(CGPathRef)clippingPath
{
INAppStoreWindow *window = (INAppStoreWindow *)self.window;
if (clippingPath) {
INApplyClippingPathInCurrentContext(clippingPath);
}
if ((window.styleMask & NSTexturedBackgroundWindowMask) == NSTexturedBackgroundWindowMask) {
// If this is a textured window, we can draw the real background gradient and noise pattern
CGFloat contentBorderThickness = 0;
if (drawingEdge == NSMaxYEdge) {
contentBorderThickness = window.titleBarHeight;
if (((window.styleMask & NSFullScreenWindowMask) != NSFullScreenWindowMask)) {
contentBorderThickness -= window._minimumTitlebarHeight;
}
} else if (drawingEdge == NSMinYEdge) {
contentBorderThickness = window.bottomBarHeight;
}
[window setAutorecalculatesContentBorderThickness:NO forEdge:drawingEdge];
[window setContentBorderThickness:contentBorderThickness forEdge:drawingEdge];
// Technically the noise should be drawn over the separator but we can't do that here
if (showsSeparator) {
[self drawSeparatorInRect:[self baselineSeparatorFrameForRect:drawingRect edge:drawingEdge] forEdge:drawingEdge];
}
} else {
// Not textured, we have to fake the background gradient and noise pattern
[self drawBackgroundGradientInRect:drawingRect forEdge:drawingEdge];
if (showsSeparator) {
[self drawSeparatorInRect:[self baselineSeparatorFrameForRect:drawingRect edge:drawingEdge] forEdge:drawingEdge];
}
if (INRunningLion() && !INRunningYosemite() && window.drawsTitlePatternOverlay) {
[self drawWindowPatternOverlayColorInRect:drawingRect forEdge:drawingEdge];
}
}
}
- (NSRect)baselineSeparatorFrameForRect:(NSRect)rect edge:(NSRectEdge)edge
{
NSAssert(edge == NSMinYEdge || edge == NSMaxYEdge, @"edge must be NSMinYEdge or NSMaxYEdge");
return NSMakeRect(0, edge == NSMaxYEdge ? NSMinY(rect) : NSMaxY(rect) - 1, NSWidth(rect), 1);
}
@end
@implementation INTitlebarView
- (void)drawWindowBorderAccentLineInRect:(NSRect)rect
{
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(context);
CGContextSetAlpha(context, 0.4);
CGContextSetBlendMode(context, kCGBlendModeScreen);
[[NSColor whiteColor] setFill];
CGContextFillRect(context, NSRectToCGRect(rect));
CGContextRestoreGState(context);
}
- (NSRect)windowBorderAccentLineFrameForRect:(NSRect)rect
{
return NSMakeRect(0, NSMaxY(rect) - 1, NSWidth(rect), 1);
}
- (void)drawBackgroundGradientInRect:(NSRect)drawingRect forEdge:(NSRectEdge)edge
{
[super drawBackgroundGradientInRect:drawingRect forEdge:edge];
[self drawWindowBorderAccentLineInRect:[self windowBorderAccentLineFrameForRect:drawingRect]];
}
- (void)drawRect:(NSRect)dirtyRect
{
INAppStoreWindow *window = (INAppStoreWindow *)self.window;
BOOL drawsAsMainWindow = (window.isMainWindow && [NSApplication sharedApplication].isActive);
// Start by filling the title bar area with black in fullscreen mode to match native apps
// Custom title bar drawing blocks can simply override this by not applying the clipping path
if (((window.styleMask & NSFullScreenWindowMask) == NSFullScreenWindowMask)) {
[[NSColor blackColor] setFill];
[[NSBezierPath bezierPathWithRect:self.bounds] fill];
}
NSRect drawingRect = self.bounds;
drawingRect.origin.y -= window.toolbarHeight;
drawingRect.size.height += window.toolbarHeight;
CGPathRef clippingPath = INCreateClippingPathWithRectAndRadius(drawingRect, INCornerClipRadius);
[self drawWindowBackgroundLayersInRect:drawingRect forEdge:NSMaxYEdge showsSeparator:window.showsBaselineSeparator clippingPath:clippingPath];
if (window.titleBarDrawingBlock) {
window.titleBarDrawingBlock(drawsAsMainWindow, NSRectToCGRect(drawingRect), CGRectMaxYEdge, clippingPath);
}
CGPathRelease(clippingPath);
if (window.showsTitle && ((window.styleMask & NSFullScreenWindowMask) == 0 || window.showsTitleInFullscreen)) {
NSRect titleTextRect;
NSDictionary *titleTextStyles = nil;
[self getTitleFrame:&titleTextRect textAttributes:&titleTextStyles forWindow:window];
if (titleTextStyles) {
if (window.verticallyCenterTitle) {
titleTextRect.origin.y = floor(NSMidY(drawingRect) - (NSHeight(titleTextRect) / 2.f) + 1);
}
[window.title drawInRect:titleTextRect withAttributes:titleTextStyles];
} else {
[self drawNativeWindowTitleInRect:titleTextRect];
}
}
}
- (void)getNativeTitleTextInfo:(out HIThemeTextInfo *)titleTextInfo
{
INAppStoreWindow *window = (INAppStoreWindow *)self.window;
BOOL drawsAsMainWindow = (window.isMainWindow && [NSApplication sharedApplication].isActive);
titleTextInfo->version = 1;
titleTextInfo->state = drawsAsMainWindow ? kThemeStateActive : kThemeStateUnavailableInactive;
titleTextInfo->fontID = kThemeWindowTitleFont;
titleTextInfo->horizontalFlushness = kHIThemeTextHorizontalFlushCenter;
titleTextInfo->verticalFlushness = kHIThemeTextVerticalFlushCenter;
titleTextInfo->options = kHIThemeTextBoxOptionEngraved;
titleTextInfo->truncationPosition = kHIThemeTextTruncationDefault;
titleTextInfo->truncationMaxLines = 1;
}
- (void)drawNativeWindowTitleInRect:(NSRect)titleTextRect
{
INAppStoreWindow *window = (INAppStoreWindow *)self.window;
HIThemeTextInfo titleTextInfo;
[self getNativeTitleTextInfo:&titleTextInfo];
// Let kHIThemeTextVerticalFlushCenter handle the vertical alignment instead of manually calculating it
titleTextRect.size.height = window.verticallyCenterTitle ? self.bounds.size.height : window._minimumTitlebarHeight;
titleTextRect.origin.y = self.isFlipped ? 0 : self.bounds.size.height - titleTextRect.size.height;
HIThemeDrawTextBox((__bridge CFTypeRef)(window.title), &titleTextRect, &titleTextInfo, [[NSGraphicsContext currentContext] graphicsPort], self.isFlipped ? kHIThemeOrientationNormal : kHIThemeOrientationInverted);
}
- (void)getTitleFrame:(out NSRect *)frame textAttributes:(out NSDictionary **)attributes forWindow:(in INAppStoreWindow *)window
{
BOOL drawsAsMainWindow = (window.isMainWindow && [NSApplication sharedApplication].isActive);
BOOL isUsingCustomTitleDrawing = window.titleTextShadow
|| window.inactiveTitleTextShadow
|| window.titleTextColor
|| window.inactiveTitleTextColor
|| window.titleFont;
NSSize titleSize;
NSRect titleTextRect;
if (isUsingCustomTitleDrawing)
{
NSShadow *titleTextShadow = drawsAsMainWindow ? window.titleTextShadow : window.inactiveTitleTextShadow;
if (titleTextShadow == nil) {
titleTextShadow = [[NSShadow alloc] init];
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 : [INAppStoreWindow defaultTitleTextColor:drawsAsMainWindow];
NSFont *titleFont = window.titleFont ?: [NSFont titleBarFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]];
NSMutableParagraphStyle *titleParagraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[titleParagraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
NSDictionary *titleTextStyles = @{NSFontAttributeName : titleFont,
NSForegroundColorAttributeName : titleTextColor,
NSShadowAttributeName : titleTextShadow,
NSParagraphStyleAttributeName : titleParagraphStyle};
titleSize = [window.title sizeWithAttributes:titleTextStyles];
if (attributes) {
*attributes = titleTextStyles;
}
}
else
{
HIThemeTextInfo titleTextInfo;
[self getNativeTitleTextInfo:&titleTextInfo];
HIThemeGetTextDimensions((__bridge CFTypeRef)(window.title), 0, &titleTextInfo, &titleSize.width, &titleSize.height, NULL);
}
titleTextRect.size = titleSize;
NSButton *docIconButton = [window standardWindowButton:NSWindowDocumentIconButton];
NSButton *versionsButton = [window standardWindowButton:NSWindowDocumentVersionsButton];
NSButton *closeButton = window._closeButtonToLayout;
NSButton *minimizeButton = window._minimizeButtonToLayout;
NSButton *zoomButton = window._zoomButtonToLayout;
if (docIconButton) {
NSRect docIconButtonFrame = [self convertRect:docIconButton.frame fromView:docIconButton.superview];
titleTextRect.origin.x = NSMaxX(docIconButtonFrame) + INTitleDocumentButtonOffset.width;
titleTextRect.origin.y = NSMidY(docIconButtonFrame) - titleSize.height / 2 + INTitleDocumentButtonOffset.height;
} else if (versionsButton) {
NSRect versionsButtonFrame = [self convertRect:versionsButton.frame fromView:versionsButton.superview];
titleTextRect.origin.x = NSMinX(versionsButtonFrame) - titleSize.width + INTitleVersionsButtonXOffset;
NSDocument *document = (NSDocument *)[(NSWindowController *)self.window.windowController document];
if (document.hasUnautosavedChanges || document.isDocumentEdited) {
titleTextRect.origin.x += INTitleDocumentStatusXOffset;
}
} else if (closeButton || minimizeButton || zoomButton) {
CGFloat closeMaxX = NSMaxX(closeButton.frame);
CGFloat minimizeMaxX = NSMaxX(minimizeButton.frame);
CGFloat zoomMaxX = NSMaxX(zoomButton.frame);
CGFloat adjustedX = MAX(MAX(closeMaxX, minimizeMaxX), zoomMaxX) + INTitleMargins.width;
CGFloat proposedX = NSMidX(self.bounds) - titleSize.width / 2;
titleTextRect.origin.x = (proposedX < adjustedX) ? adjustedX : proposedX;
} else {
titleTextRect.origin.x = NSMidX(self.bounds) - titleSize.width / 2;
}
if (versionsButton) {
NSRect versionsButtonFrame = [self convertRect:versionsButton.frame fromView:versionsButton.superview];
NSTextField *titleDivider = window.titleDivider;
if (titleDivider) {
versionsButtonFrame = [self convertRect:titleDivider.frame fromView:titleDivider.superview];
}
if (NSMaxX(titleTextRect) > NSMinX(versionsButtonFrame)) {
titleTextRect.size.width = NSMinX(versionsButtonFrame) - NSMinX(titleTextRect) - INTitleMargins.width;
}
}
NSButton *fullScreenButton = window._fullScreenButtonToLayout;
if (fullScreenButton) {
CGFloat fullScreenX = fullScreenButton.frame.origin.x;
CGFloat maxTitleX = NSMaxX(titleTextRect);
if ((fullScreenX - INTitleMargins.width) < NSMaxX(titleTextRect)) {
titleTextRect.size.width = titleTextRect.size.width - (maxTitleX - fullScreenX) - INTitleMargins.width;
}
}
titleTextRect.origin.y = NSMaxY(self.bounds) - titleSize.height - INTitleMargins.height;
if (frame) {
*frame = titleTextRect;
}
}
- (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
@implementation INBottomBarView
- (void)drawRect:(NSRect)dirtyRect
{
INAppStoreWindow *window = (INAppStoreWindow *)self.window;
BOOL drawsAsMainWindow = (window.isMainWindow && [NSApplication sharedApplication].isActive);
NSRect drawingRect = self.bounds;
[self drawWindowBackgroundLayersInRect:drawingRect forEdge:NSMinYEdge showsSeparator:window.showsBottomBarSeparator clippingPath:NULL];
if (window.bottomBarDrawingBlock) {
window.bottomBarDrawingBlock(drawsAsMainWindow, NSRectToCGRect(drawingRect), CGRectMinYEdge, NULL);
}
}
@end
@interface INMovableByBackgroundContainerView : NSView
@property (nonatomic) CGFloat mouseDragDetectionThreshold;
@end
@implementation INMovableByBackgroundContainerView
- (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.frameOrigin = 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.frame = frameRect;
}
- (void)setFrameSize:(NSSize)newSize
{
newSize = [(INAppStoreWindow *)self.window _contentViewFrame].size;
super.frameSize = newSize;
}
@end
@implementation INAppStoreWindow {
CGFloat _cachedTitleBarHeight;
BOOL _setFullScreenButtonRightMargin;
BOOL _preventWindowFrameChange;
INAppStoreWindowDelegateProxy *_delegateProxy;
INMovableByBackgroundContainerView *_titleBarContainer;
INMovableByBackgroundContainerView *_bottomBarContainer;
}
@synthesize titleBarView = _titleBarView;
@synthesize titleBarHeight = _titleBarHeight;
@synthesize bottomBarView = _bottomBarView;
@synthesize bottomBarHeight = _bottomBarHeight;
@synthesize centerFullScreenButton = _centerFullScreenButton;
@synthesize centerTrafficLightButtons = _centerTrafficLightButtons;
@synthesize verticalTrafficLightButtons = _verticalTrafficLightButtons;
@synthesize hideTitleBarInFullScreen = _hideTitleBarInFullScreen;
@synthesize titleBarDrawingBlock = _titleBarDrawingBlock;
@synthesize bottomBarDrawingBlock = _bottomBarDrawingBlock;
@synthesize showsBaselineSeparator = _showsBaselineSeparator;
@synthesize showsBottomBarSeparator = _showsBottomBarSeparator;
@synthesize fullScreenButtonRightMargin = _fullScreenButtonRightMargin;
@synthesize trafficLightButtonsLeftMargin = _trafficLightButtonsLeftMargin;
@synthesize titleBarGradient = _titleBarGradient;
@synthesize bottomBarGradient = _bottomBarGradient;
@synthesize baselineSeparatorColor = _baselineSeparatorColor;
@synthesize inactiveTitleBarGradient = _inactiveTitleBarGradient;
@synthesize inactiveBottomBarGradient = _inactiveBottomBarGradient;
@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.representedURL = url;
if (_showsDocumentProxyIcon == NO) {
[self standardWindowButton:NSWindowDocumentIconButton].image = nil;
}
}
#pragma mark -
#pragma mark Memory Management
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark -
#pragma mark NSWindow Overrides
- (void)becomeKeyWindow
{
[super becomeKeyWindow];
[self _updateTitlebarView];
[self _updateBottomBarView];
[self _layoutTrafficLightsAndContent];
[self _setupTrafficLightsTrackingArea];
}
- (void)resignKeyWindow
{
[super resignKeyWindow];
[self _updateTitlebarView];
[self _updateBottomBarView];
[self _layoutTrafficLightsAndContent];
}
- (void)becomeMainWindow
{
[super becomeMainWindow];
[self _updateTitlebarView];
[self _updateBottomBarView];
}
- (void)resignMainWindow
{
[super resignMainWindow];
[self _updateTitlebarView];
[self _updateBottomBarView];
}
- (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.contentView = 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.title = aString;
[self _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
}
- (void)setMaxSize:(NSSize)size
{
super.maxSize = size;
[self _layoutTrafficLightsAndContent];
}
- (void)setMinSize:(NSSize)size
{
super.minSize = size;
[self _layoutTrafficLightsAndContent];
}
- (void)setToolbar:(NSToolbar *)toolbar
{
self.titleBarHeight = self._minimumTitlebarHeight;
super.toolbar = toolbar;
}
- (IBAction)toggleToolbarShown:(id)sender
{
[super toggleToolbarShown:sender];
[self _repositionContentView];
}
#pragma mark -
#pragma mark Accessors
- (void)setTitleBarView:(NSView *)newTitleBarView
{
if ((_titleBarView != newTitleBarView) && newTitleBarView) {
[_titleBarView removeFromSuperview];
_titleBarView = newTitleBarView;
_titleBarView.frame = _titleBarContainer.bounds;
_titleBarView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[_titleBarContainer addSubview:_titleBarView];
}
}
- (void)setTitleBarHeight:(CGFloat)newTitleBarHeight
{
[self setTitleBarHeight:newTitleBarHeight adjustWindowFrame:YES];
}
- (void)setTitleBarHeight:(CGFloat)newTitleBarHeight adjustWindowFrame:(BOOL)adjustWindowFrame
{
if (_titleBarHeight != newTitleBarHeight) {
if (self.toolbar) {
newTitleBarHeight = self._minimumTitlebarHeight;
}
NSRect windowFrame = self.frame;
if (adjustWindowFrame)
{
windowFrame.origin.y -= newTitleBarHeight - _titleBarHeight;
windowFrame.size.height += newTitleBarHeight - _titleBarHeight;
}
_cachedTitleBarHeight = newTitleBarHeight;
_titleBarHeight = _cachedTitleBarHeight;
[self setFrame:windowFrame display:YES];
[self _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
if ((self.styleMask & NSTexturedBackgroundWindowMask) == NSTexturedBackgroundWindowMask) {
[self.contentView displayIfNeeded];
}
}
}
- (void)setBottomBarView:(NSView *)bottomBarView
{
if ((_bottomBarView != bottomBarView) && bottomBarView) {
[_bottomBarView removeFromSuperview];
_bottomBarView = bottomBarView;
_bottomBarView.frame = _bottomBarContainer.bounds;
_bottomBarView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[_bottomBarContainer addSubview:_bottomBarView];
}
}
- (void)setBottomBarHeight:(CGFloat)bottomBarHeight
{
if (_bottomBarHeight != bottomBarHeight) {
_bottomBarHeight = bottomBarHeight;
[self _layoutTrafficLightsAndContent];
if ((self.styleMask & NSTexturedBackgroundWindowMask) == NSTexturedBackgroundWindowMask) {
[self.contentView displayIfNeeded];
}
}
}
- (void)setShowsBaselineSeparator:(BOOL)showsBaselineSeparator
{
if (_showsBaselineSeparator != showsBaselineSeparator) {
_showsBaselineSeparator = showsBaselineSeparator;
[self.titleBarView setNeedsDisplay:YES];
}
}
- (void)setShowsBottomBarSeparator:(BOOL)showsBottomBarSeparator
{
if (_showsBottomBarSeparator != showsBottomBarSeparator) {
_showsBottomBarSeparator = showsBottomBarSeparator;
[self.bottomBarView setNeedsDisplay:YES];
}
}
- (void)setTrafficLightButtonsLeftMargin:(CGFloat)newTrafficLightButtonsLeftMargin
{
if (_trafficLightButtonsLeftMargin != newTrafficLightButtonsLeftMargin) {
_trafficLightButtonsLeftMargin = newTrafficLightButtonsLeftMargin;
[self _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
[self _setupTrafficLightsTrackingArea];
}
}
- (void)setFullScreenButtonRightMargin:(CGFloat)newFullScreenButtonRightMargin
{
if (_fullScreenButtonRightMargin != newFullScreenButtonRightMargin) {
_setFullScreenButtonRightMargin = YES;
_fullScreenButtonRightMargin = newFullScreenButtonRightMargin;
[self _layoutTrafficLightsAndContent];
[self _displayWindowAndTitlebar];
}
}
- (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.secondaryDelegate = anObject;
super.delegate = nil;
super.delegate = _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.frameOrigin = [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.frameOrigin = [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.frameOrigin = [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.frameOrigin = [self standardWindowButton:NSWindowFullScreenButton].frame.origin;
[_fullScreenButton.cell accessibilitySetOverrideValue:NSAccessibilityFullScreenButtonSubrole forAttribute:NSAccessibilitySubroleAttribute];
[_fullScreenButton.cell accessibilitySetOverrideValue:NSAccessibilityRoleDescription(NSAccessibilityButtonRole, NSAccessibilityFullScreenButtonSubrole) forAttribute:NSAccessibilityRoleDescriptionAttribute];
[self.themeFrameView addSubview:_fullScreenButton];
}
}
}
- (NSTextField *)titleDivider
{
for (NSTextField *divider in self.themeFrameView.subviews) {
if ([divider isKindOfClass:[NSTextField class]]) {
if ([divider.stringValue isEqualToString:@"\u2014"]) {
return divider;
}
}
}
return nil;
}
- (void)setStyleMask:(NSUInteger)styleMask
{
_preventWindowFrameChange = YES;
// Prevent drawing artifacts when turning off NSTexturedBackgroundWindowMask before
// exiting from full screen and then resizing the title bar; the problem is that internally