-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCustomWindow.m
235 lines (205 loc) · 5.4 KB
/
CustomWindow.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
#import "CustomWindow.h"
#import "Controller.h"
@implementation CustomWindow
-(void)awakeFromNib
{
id defaults = [NSUserDefaults standardUserDefaults];
[self setLevel:NSNormalWindowLevel];
[self setAcceptsMouseMovedEvents:YES];
fullscreen = [defaults boolForKey:@"Fullscreen"];
if ([defaults boolForKey:@"DontHideMenuBar"]) {
hideMenuBar = NO;
} else {
hideMenuBar = YES;
}
if (![defaults objectForKey:@"NSWindow Frame NormalWindow"]) {
[self saveFrameUsingName:@"NormalWindow"];
}
resizable = YES;
[self setFullScreen:fullscreen];
if (!fullscreen) [[[[[NSApp mainMenu] itemWithTitle:NSLocalizedString(@"Window", @"")] submenu] itemWithTitle:NSLocalizedString(@"Fullscreen", @"")] setState:NSOffState];
[self setShowsResizeIndicator:NO];
}
- (void)setFrame:(NSRect)windowFrame display:(BOOL)displayViews
{
if (!resizable) {
return;
} else {
[super setFrame:windowFrame display:displayViews];
[view setAccessoryWindowFrame];
}
}
- (void)setFullScreen:(BOOL)b
{
fullscreen = b;
if (!fullscreen) {
resizable = YES;
[NSMenu setMenuBarVisible:YES];
[self setFrameUsingName:@"NormalWindow"];
[self setHidesOnDeactivate:NO];
} else {
[self setFrame:NSMakeRect(0,0,100,100) display:YES];
[self setHidesOnDeactivate:YES];
[self setHideMenuBar:hideMenuBar];
resizable = NO;
}
}
- (BOOL)isFullScreen
{
return fullscreen;
}
-(NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)aScreen
{
if (fullscreen) {
NSRect result = [[NSScreen mainScreen] frame];
if (hideMenuBar == YES) result.size.height+= 22;
return result;
} else {
NSRect result = [super constrainFrameRect:frameRect toScreen:aScreen];
NSRect screen = [[NSScreen mainScreen] frame];
if (result.size.height>screen.size.height || result.size.width>screen.size.width) {
result = NSMakeRect(0,0,screen.size.height/4,screen.size.width/4);
}
return result;
}
}
- (void)setHideMenuBar:(BOOL)b
{
hideMenuBar = b;
if (fullscreen) {
resizable = YES;
if (!hideMenuBar) {
[NSMenu setMenuBarVisible:YES];
NSRect result = [[NSScreen mainScreen] frame];
[self setFrame:result display:YES];
} else {
if ([self isKeyWindow]) [NSMenu setMenuBarVisible:NO];
NSRect result = [[NSScreen mainScreen] frame];
result.size.height += 22;
[self setFrame:result display:YES];
}
resizable = NO;
[self updateTrackingRect];
}
}
- (void)setTarget:(id)tar
{
target = tar;
}
- (void)setAction:(SEL)sel
{
selector = sel;
}
- (void)keyDown:(NSEvent *)theEvent
{
if (fullscreen) [NSCursor setHiddenUntilMouseMoves:YES];
[controller keyAction:theEvent];
}
- (void)performClose:(id)sender
{
[NSMenu setMenuBarVisible:YES];
[super performClose:sender];
[self display];
}
- (BOOL)performKeyEquivalent:(NSEvent *)anEvent
{
if (fullscreen) {
unsigned int cMod = 0;
BOOL option = ([anEvent modifierFlags] & NSAlternateKeyMask) ? YES : NO;
BOOL control = ([anEvent modifierFlags] & NSControlKeyMask) ? YES : NO;
BOOL command = ([anEvent modifierFlags] & NSCommandKeyMask) ? YES : NO;
if (option) cMod += 1;
if (control) cMod += 2;
if (command) cMod += 4;
if (cMod == 4 && [[anEvent charactersIgnoringModifiers] isEqualToString:@"m"] && ![NSMenu menuBarVisible]) {
[NSMenu setMenuBarVisible:YES];
[NSTimer scheduledTimerWithTimeInterval:0.0
target:self
selector:@selector(performMiniaturize:)
userInfo:NULL
repeats:NO];
return YES;
}
}
return [super performKeyEquivalent:anEvent];
}
- (void)performMiniaturize:(id)sender
{
[NSMenu setMenuBarVisible:YES];
[super performMiniaturize:sender];
}
- (void)deminiaturize:(id)sender
{
[NSMenu setMenuBarVisible:YES];
[super deminiaturize:sender];
[NSMenu setMenuBarVisible:YES];
}
- (void)makeKeyAndOrderFront:(id)sender
{
if (fullscreen) {
if (![NSMenu menuBarVisible]) {
[self makeKeyWindow];
} else {
[self orderFront:self];
[self makeKeyWindow];
if (hideMenuBar) [NSMenu setMenuBarVisible:NO];
}
if (hideMenuBar == NO) [NSMenu setMenuBarVisible:YES];
} else {
[super makeKeyAndOrderFront:sender];
}
}
- (void)mouseEntered:(NSEvent *)theEvent
{
if (fullscreen && [self isKeyWindow]) {
if (hideMenuBar) [NSMenu setMenuBarVisible:NO];
}
}
- (void)mouseExited:(NSEvent *)theEvent
{
if (fullscreen && [self isKeyWindow]) {
//[NSMenu setMenuBarVisible:YES];
NSRect fullscreenRect = [[NSScreen mainScreen] frame];
NSRect newRect = NSOffsetRect(fullscreenRect, 0, -10);
if (NSPointInRect([theEvent locationInWindow],newRect)) {
if (hideMenuBar) [NSMenu setMenuBarVisible:NO];
} else {
[NSMenu setMenuBarVisible:YES];
}
}
}
-(void)updateTrackingRect
{
if (!fullscreen) return;
NSRect fullscreenRect = [[NSScreen mainScreen] frame];
NSRect newRect = NSOffsetRect(fullscreenRect, 0, -10);
[[self contentView] addTrackingRect:newRect owner:self userData:NULL assumeInside:YES];
}
- (void)cursorHide
{
if (fullscreen && [self isKeyWindow]) {
[NSCursor setHiddenUntilMouseMoves:YES];
cursorTimer = nil;
}
}
- (void)mouseMoved:(NSEvent *)theEvent
{
if (fullscreen && !cursorTimer) {
cursorTimer = [NSTimer scheduledTimerWithTimeInterval:3
target:self
selector:@selector(cursorHide)
userInfo:NULL
repeats:NO];
}
[view mouseMoved:theEvent];
}
-(void)becomeKeyWindow
{
[super becomeKeyWindow];
if (fullscreen && hideMenuBar) {
[NSMenu setMenuBarVisible:NO];
} else {
[NSMenu setMenuBarVisible:YES];
}
}
@end