forked from bhaller/Jiggler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMOverlayWindow.m
296 lines (225 loc) · 7.61 KB
/
MMOverlayWindow.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
//
// MMOverlayWindow.m
// MM
//
// Created by Ben Haller on Wed Aug 25 2004.
// Copyright (c) 2004 Stick Software. All rights reserved.
//
#import "MMOverlayWindow.h"
static NSString *MMOverlayHorizontalPositionDefaultsKey = @"OverlayHorizontalPosition";
static NSString *MMOverlayVerticalPositionDefaultsKey = @"OverlayVerticalPosition";
@interface MMOverlayWindow (PrivateAPI)
+ (MMOverlayWindow *)sharedOverlayWindow;
- (void)scheduleTimer;
- (void)activate;
- (void)deactivate;
- (BOOL)isActivated;
@end
@interface MMOverlayView : NSImageView
@end
@implementation MMOverlayWindow
+ (void)activateOverlay
{
MMOverlayWindow *sharedOverlay = [MMOverlayWindow sharedOverlayWindow];
[sharedOverlay activate];
}
+ (void)deactivateOverlay
{
MMOverlayWindow *sharedOverlay = [MMOverlayWindow sharedOverlayWindow];
[sharedOverlay deactivate];
}
+ (BOOL)isActivated
{
MMOverlayWindow *sharedOverlay = [MMOverlayWindow sharedOverlayWindow];
return [sharedOverlay isActivated];
}
+ (MMOverlayWindow *)sharedOverlayWindow
{
static MMOverlayWindow *sharedOverlay = nil;
if (!sharedOverlay)
sharedOverlay = [[MMOverlayWindow alloc] init];
return sharedOverlay;
}
- (void)createOverlayWindow
{
NSScreen *mainScreen = [[NSScreen screens] objectAtIndex:0];
NSRect screenFrame = [mainScreen frame];
NSRect contentRect;
MMOverlayView *iconView;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber *horizontalPosition = [defaults objectForKey:MMOverlayHorizontalPositionDefaultsKey];
NSNumber *verticalPosition = [defaults objectForKey:MMOverlayVerticalPositionDefaultsKey];
// Figure out a centered rect for the overlay
contentRect.size.width = 200;
contentRect.size.height = 200;
contentRect.origin.x = screenFrame.origin.x + floor((screenFrame.size.width - contentRect.size.width) / 2.0);
contentRect.origin.y = screenFrame.origin.y + floor((screenFrame.size.height - contentRect.size.height) / 2.0);
// Use our defaults window location if it exists
if (horizontalPosition && verticalPosition)
{
int horizontalInt = [horizontalPosition intValue];
int verticalInt = [verticalPosition intValue];
NSRect defaultBasedRect = NSMakeRect(horizontalInt, verticalInt, contentRect.size.width, contentRect.size.height);
NSArray *screens = [NSScreen screens];
int i, c;
for (i = 0, c = (int)[screens count]; i < c; ++i)
{
NSScreen *screen = [screens objectAtIndex:i];
NSRect frame = [screen frame];
if (NSIntersectsRect(frame, defaultBasedRect))
{
NSRect intersection = NSIntersectionRect(frame, defaultBasedRect);
float intersectionSize = intersection.size.width * intersection.size.height;
if (intersectionSize >= 400.0)
{
// We have a screen on which our defaults-based frame will lie, so we can use it.
contentRect = defaultBasedRect;
break;
}
}
}
}
// Make our overlay window
overlayWindow = [[NSWindow alloc] initWithContentRect:contentRect styleMask:NSWindowStyleMaskBorderless backing:NSBackingStoreBuffered defer:YES];
[overlayWindow setCanHide:NO];
[overlayWindow setHasShadow:NO];
[overlayWindow setHidesOnDeactivate:NO];
[overlayWindow setExcludedFromWindowsMenu:YES];
[overlayWindow setOpaque:NO];
[overlayWindow setReleasedWhenClosed:NO];
[overlayWindow setLevel:NSScreenSaverWindowLevel - 20];
[overlayWindow setAlphaValue:0.0];
[overlayWindow setBackgroundColor:[NSColor clearColor]];
[overlayWindow setIgnoresMouseEvents:NO];
// Configure our behavior with Spaces / Exposé / Mission Control
[overlayWindow setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces |
NSWindowCollectionBehaviorStationary |
NSWindowCollectionBehaviorIgnoresCycle |
NSWindowCollectionBehaviorFullScreenAuxiliary |
NSWindowCollectionBehaviorFullScreenDisallowsTiling];
// Make our icon view
iconView = [[MMOverlayView alloc] initWithFrame:NSMakeRect(0, 0, contentRect.size.width, contentRect.size.height)];
[iconView setEditable:NO];
[iconView setImageAlignment:NSImageAlignCenter];
[iconView setImageFrameStyle:NSImageFrameGrayBezel];
[iconView setImageScaling:NSImageScaleNone];
[iconView setImage:[NSImage imageNamed:@"NSApplicationIcon"]];
// Make our icon view be our content view
[overlayWindow setContentView:iconView];
[iconView release];
}
- (void)timer:(id)unused
{
float targetAlpha;
BOOL needsAnotherUpdate;
// We no longer have a scheduled timer
scheduledTimer = NO;
// Figure out what our target and new alpha are
if (activated)
targetAlpha = 1.0;
else
targetAlpha = 0.0;
if (activated)
{
currentAlpha += 0.025;
if (currentAlpha < targetAlpha - 0.01)
{
needsAnotherUpdate = YES;
}
else
{
currentAlpha = targetAlpha;
needsAnotherUpdate = NO;
}
}
else
{
currentAlpha -= 0.1; // fade out much faster than we fade in
if (currentAlpha > targetAlpha + 0.01)
{
needsAnotherUpdate = YES;
}
else
{
currentAlpha = targetAlpha;
needsAnotherUpdate = NO;
}
}
// If we will need another update, schedule it now for greater timing precision
if (needsAnotherUpdate)
[self scheduleTimer];
// And finally, do our update for this tick
[overlayWindow setAlphaValue:currentAlpha];
if (currentAlpha < 0.01)
{
if (isOrderedIn)
{
[overlayWindow orderOut:nil];
isOrderedIn = NO;
}
}
else
{
if (!isOrderedIn)
{
[overlayWindow orderBack:nil];
isOrderedIn = YES;
}
}
}
- (void)scheduleTimer
{
if (!scheduledTimer)
{
[self performSelector:@selector(timer:) withObject:nil afterDelay:0.05 inModes:@[NSDefaultRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode]];
scheduledTimer = YES;
}
}
- (void)activate
{
//NSLog(@"+++ activate");
if (!overlayWindow)
[self createOverlayWindow];
activated = YES;
[self scheduleTimer];
}
- (void)deactivate
{
//NSLog(@"--- deactivate");
activated = NO;
[self scheduleTimer];
}
- (BOOL)isActivated
{
return activated;
}
@end
@implementation MMOverlayView
- (void)mouseDown:(NSEvent *)event
{
if ([event type] == NSEventTypeLeftMouseDown)
{
NSWindow *eventWindow = [event window];
NSPoint locInWindow = [event locationInWindow];
NSRect locInWindowRect = NSMakeRect(locInWindow.x, locInWindow.y, 1, 1);
NSPoint startMouse = [eventWindow convertRectToScreen:locInWindowRect].origin; // these NSRect shenanigans are because convertBaseToScreen: has been deprecated, for no apparent reason...
NSRect windowFrame = [eventWindow frame];
do
{
NSPoint nextLocInWindow = [event locationInWindow];
NSRect nextLocInWindowRect = NSMakeRect(nextLocInWindow.x, nextLocInWindow.y, 1, 1);
NSPoint nextMouse = [eventWindow convertRectToScreen:nextLocInWindowRect].origin; // these NSRect shenanigans are because convertBaseToScreen: has been deprecated, for no apparent reason...
int dx = (nextMouse.x - startMouse.x), dy = (nextMouse.y - startMouse.y);
startMouse = nextMouse;
windowFrame.origin.x += dx;
windowFrame.origin.y += dy;
[eventWindow setFrame:windowFrame display:YES];
[[NSUserDefaults standardUserDefaults] setInteger:(int)windowFrame.origin.x forKey:MMOverlayHorizontalPositionDefaultsKey];
[[NSUserDefaults standardUserDefaults] setInteger:(int)windowFrame.origin.y forKey:MMOverlayVerticalPositionDefaultsKey];
if ([event type] == NSEventTypeLeftMouseUp)
break;
}
while ((event = [eventWindow nextEventMatchingMask:(NSEventMaskLeftMouseDragged | NSEventMaskLeftMouseUp) untilDate:[NSDate distantFuture] inMode:NSEventTrackingRunLoopMode dequeue:YES]));
}
}
@end