-
Notifications
You must be signed in to change notification settings - Fork 3
/
WindowController.m
210 lines (169 loc) · 5.73 KB
/
WindowController.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
#define ScratchWidth 600
#define ScratchHeight 500
NSColor* (*hackRealColor)(NSObject*,SEL,NSString*,NSBundle*)=NULL;
NSColor* hackFakeColor(NSObject* self,SEL sel,NSString* name,NSBundle* bundle)
{
if([name containsString:@"_NSTabBar"])
{
if([@[@"_NSTabBarTabFillColorSelectedActiveWindow"] containsObject:name])
{
return getXcodeTheme().sourceTextBackgroundColor;
}
if([@[@"_NSTabBarTabFillColorActiveWindow"] containsObject:name])
{
return getXcodeTheme().sourceTextCurrentLineHighlightColor;
}
if([@[@"_NSTabBarInactiveTabHoverColor",@"_NSTabBarNewTabButtonHoverColor",@"_NSTabBarSemitransparentDividerColor"] containsObject:name])
{
return getXcodeTheme().sourceTextSelectionColor;
}
}
return hackRealColor(self,sel,name,bundle);
}
NSColor* hackFakeShadow()
{
return NSColor.clearColor;
}
void (*hackRealHeaderLayout)(NSView*,SEL);
void hackFakeHeaderLayout(NSView* self,SEL sel)
{
hackRealHeaderLayout(self,sel);
self.subviews.firstObject.hidden=true;
self.layer.backgroundColor=getXcodeTheme().sourceTextBackgroundColor.CGColor;
}
CGImageRef createThemeAppIcon()
{
return createAppIcon(getXcodeTheme().sourceTextBackgroundColor.CGColor,getXcodeTheme().sourcePlainTextColor.CGColor,getXcodeTheme().sourceTextCurrentLineHighlightColor.CGColor);
}
@implementation WindowController
+(void)initialize
{
if(@available(macOS 11,*))
{
swizzle(@"NSColor",@"colorNamed:bundle:",false,(IMP)hackFakeColor,(IMP*)&hackRealColor);
swizzle(@"NSTitlebarSeparatorView",@"updateLayer",true,(IMP)returnNil,NULL);
}
// TODO: uhh
if(NSClassFromString(@"_TtC12SourceEditor21StickyHeaderStackView"))
{
swizzle(@"_TtC12SourceEditor21StickyHeaderStackView",@"layout",true,(IMP)hackFakeHeaderLayout,(IMP*)&hackRealHeaderLayout);
swizzle(@"NSColor",@"shadowWithLevel:",true,(IMP)hackFakeShadow,NULL);
swizzle(@"NSColor",@"highlightWithLevel:",true,(IMP)hackFakeShadow,NULL);
}
[NSNotificationCenter.defaultCenter addObserverForName:XcodeThemeChangedKey object:nil queue:nil usingBlock:^(NSNotification* note)
{
[WindowController.allInstances makeObjectsPerformSelector:@selector(syncTheme)];
}];
}
+(NSArray<WindowController*>*)allInstances
{
NSMutableArray* result=NSMutableArray.alloc.init.autorelease;
for(Document* document in NSDocumentController.sharedDocumentController.documents)
{
[result addObjectsFromArray:document.windowControllers];
}
return result;
}
+(WindowController*)firstInstance
{
return WindowController.allInstances.firstObject;
}
+(WindowController*)lastInstance
{
return WindowController.allInstances.lastObject;
}
+(void)syncProjectMode
{
NSWindow* previousKeyWindow=NSApp.keyWindow;
WindowController* previous=nil;
for(WindowController* instance in WindowController.allInstances)
{
[instance syncProjectModeWithPrevious:previous];
previous=instance;
}
if(Delegate.shared.projectMode)
{
// TODO: hack to preserve window ordering
// it seems key window becomes tab 1; who calls mergeAllWindows: is irrelevant
[WindowController.firstInstance.window makeKeyAndOrderFront:nil];
[WindowController.firstInstance.window mergeAllWindows:nil];
}
[previousKeyWindow makeKeyAndOrderFront:nil];
}
-(instancetype)init
{
self=super.init;
NSWindowStyleMask style=NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable|NSWindowStyleMaskMiniaturizable;
self.window=[NSWindow.alloc initWithContentRect:CGRectZero styleMask:style backing:NSBackingStoreBuffered defer:false].autorelease;
self.window.titlebarAppearsTransparent=true;
self.window.delegate=Delegate.shared;
[self syncProjectModeWithPrevious:WindowController.lastInstance];
self.syncTheme;
return self;
}
-(void)replaceDocument:(Document*)document
{
NSRange oldSelection=getXcodeViewControllerSelection(self.xcodeViewController);
self.xcodeViewController=getXcodeViewController(document.xcodeDocument);
self.window.contentView=self.xcodeViewController.view;
focusXcodeViewController(self.xcodeViewController,oldSelection);
}
-(void)syncProjectModeWithPrevious:(WindowController*)previous
{
if(Delegate.shared.projectMode)
{
self.window.tabbingMode=NSWindowTabbingModePreferred;
}
else
{
self.window.tabbingMode=NSWindowTabbingModeDisallowed;
[self.window moveTabToNewWindow:nil];
}
// TODO: i feel like some of this should be in Settings, but we don't have access to previous window and toolbar height there..
CGRect previousRect=previous?previous.window.frame:NSScreen.mainScreen.visibleFrame;
CGFloat toolbarHeight=[self.window frameRectForContentRect:CGRectZero].size.height;
CGRect cascadedRect=CGRectMake(previousRect.origin.x+toolbarHeight,previousRect.origin.y+previousRect.size.height-ScratchHeight-toolbarHeight,ScratchWidth,ScratchHeight);
if(CGRectEqualToRect(Settings.projectRect,CGRectZero))
{
Settings.projectRect=cascadedRect;
}
if(Delegate.shared.projectMode)
{
[self.window setFrame:Settings.projectRect display:false];
}
else
{
[self.window setFrame:cascadedRect display:false];
}
}
-(void)syncTheme
{
dispatch_async(dispatch_get_main_queue(),^()
{
NSAppearance* appearance=[NSAppearance appearanceNamed:getXcodeTheme().hasLightBackground?NSAppearanceNameAqua:NSAppearanceNameVibrantDark];
if(@available(macOS 10.14,*))
{
NSApp.appearance=appearance;
}
else
{
self.window.appearance=appearance;
}
self.window.backgroundColor=getXcodeTheme().sourceTextBackgroundColor;
// TODO: hack to refresh the "new tab" button
if(self.window.isKeyWindow)
{
self.window.resignKeyWindow;
self.window.becomeKeyWindow;
}
CGImageRef icon=createThemeAppIcon();
NSApp.applicationIconImage=[NSImage.alloc initWithCGImage:icon size:CGSizeZero].autorelease;
CFRelease(icon);
});
}
-(void)dealloc
{
self.xcodeViewController=nil;
super.dealloc;
}
@end