-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWindowController.m
More file actions
160 lines (124 loc) · 4.14 KB
/
WindowController.m
File metadata and controls
160 lines (124 loc) · 4.14 KB
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
@implementation WindowController
+(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
{
dispatch_async(dispatch_get_main_queue(),^()
{
Delegate.shared.ignoreFrameChanges=true;
NSWindow* previousKeyWindow=NSApp.keyWindow;
WindowController* previous=nil;
for(WindowController* instance in WindowController.allInstances)
{
[instance syncProjectModeWithPrevious:previous];
previous=instance;
}
if(Delegate.shared.projectMode)
{
[previousKeyWindow mergeAllWindows:nil];
}
[previousKeyWindow makeKeyAndOrderFront:nil];
if(Delegate.shared.projectMode&&!Settings.showedProjectModeExplanation)
{
Settings.showedProjectModeExplanation=true;
alert(@"Project Mode uses tabs and remembers the window dimensions across launches (like TextMate). Switch back to the default mode for little windows (like TextEdit).");
}
Delegate.shared.ignoreFrameChanges=false;
});
}
+(void)syncTheme
{
dispatch_async(dispatch_get_main_queue(),^()
{
[WindowController.allInstances makeObjectsPerformSelector:@selector(syncTheme)];
});
}
-(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.delegate=Delegate.shared;
// TODO: a tahoe bug or i was relying on undefined behavior? idk
self.window.backgroundColor=[Settings colorWithString:AmyThemeBackgroundColor];
[self syncProjectModeWithPrevious:WindowController.lastInstance];
self.syncTheme;
return self;
}
-(void)setXcodeViewController:(XcodeViewController*)newController
{
NSRange oldSelection=[Xcode selectionWithViewController:_xcodeViewController];
if(_xcodeViewController)
{
[Xcode destroyViewController:_xcodeViewController];
_xcodeViewController.release;
}
_xcodeViewController=newController.retain;
if(_xcodeViewController)
{
self.window.contentView=_xcodeViewController.view;
[Xcode focusViewController:_xcodeViewController withSelection:oldSelection];
}
}
-(void)replaceDocument:(Document*)document
{
self.xcodeViewController=[Xcode viewControllerWithDocument:document.xcodeDocument];
}
-(void)syncProjectModeWithPrevious:(WindowController*)previous
{
if(!Delegate.shared.projectMode)
{
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=CGRectMake(previousRect.origin.x+toolbarHeight,previousRect.origin.y+previousRect.size.height-DefaultProjectHeight-toolbarHeight,DefaultProjectWidth,DefaultProjectHeight);
}
if(Delegate.shared.projectMode)
{
self.window.tabbingMode=NSWindowTabbingModePreferred;
[self.window setFrame:Settings.projectRect display:false];
}
else
{
[self.window setFrame:cascadedRect display:false];
}
}
-(void)syncTheme
{
NSAppearance* appearance=[NSAppearance appearanceNamed:Xcode.themeIsLight?NSAppearanceNameAqua:NSAppearanceNameVibrantDark];
if(@available(macOS 10.14,*))
{
NSApp.appearance=appearance;
}
else
{
self.window.appearance=appearance;
}
}
-(void)dealloc
{
self.xcodeViewController=nil;
super.dealloc;
}
@end