-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShadyAppDelegate.m
More file actions
356 lines (275 loc) · 10.1 KB
/
ShadyAppDelegate.m
File metadata and controls
356 lines (275 loc) · 10.1 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
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
//
// ShadyAppDelegate.m
// Shady
//
// Created by Matt Gemmell on 02/11/2009.
//
#import "ShadyAppDelegate.h"
#import <QuartzCore/QuartzCore.h>
#import "MGTransparentWindow.h"
#import "NSApplication+DockIcon.h"
#import "Shady.h"
#define OPACITY_UNIT 0.05; // "20 shades ought to be enough for _anybody_."
#define DEFAULT_OPACITY 0.4
#define STATE_MENU NSLocalizedString(@"Turn Shady Off", nil) // global status menu-item title when enabled
#define STATE_MENU_OFF NSLocalizedString(@"Turn Shady On", nil) // global status menu-item title when disabled
#define HELP_TEXT NSLocalizedString(@"When Shady is frontmost:\rPress Up/Down to alter shade,\ror press Q to Quit.", nil)
#define HELP_TEXT_OFF NSLocalizedString(@"Shady is Off.\rPress S to turn Shady on,\ror press Q to Quit.", nil)
#define STATUS_MENU_ICON [NSImage imageNamed:@"Shady_Menu_Dark"]
#define STATUS_MENU_ICON_ALT [NSImage imageNamed:@"Shady_Menu_Light"]
#define STATUS_MENU_ICON_OFF [NSImage imageNamed:@"Shady_Menu_Dark_Off"]
#define STATUS_MENU_ICON_OFF_ALT [NSImage imageNamed:@"Shady_Menu_Light_Off"]
#define MAX_OPACITY 0.90 // the darkest the screen can be, where 1.0 is pure black.
#define KEY_OPACITY @"ShadySavedOpacityKey" // name of the saved opacity setting.
#define KEY_DOCKICON @"ShadySavedDockIconKey" // name of the saved dock icon state setting.
#define KEY_ENABLED @"ShadySavedEnabledKey" // name of the saved primary state setting.
@implementation ShadyAppDelegate
@synthesize window;
@synthesize opacity;
@synthesize statusMenu;
@synthesize opacitySlider;
@synthesize prefsWindow;
@synthesize dockIconCheckbox;
@synthesize stateMenuItemMainMenu;
@synthesize stateMenuItemStatusBar;
#pragma mark Setup and Tear-down
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Set the default opacity value and load any saved settings.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:DEFAULT_OPACITY], KEY_OPACITY,
[NSNumber numberWithBool:YES], KEY_DOCKICON,
[NSNumber numberWithBool:YES], KEY_ENABLED,
nil]];
// Set up Dock icon.
BOOL showsDockIcon = [defaults boolForKey:KEY_DOCKICON];
[dockIconCheckbox setState:(showsDockIcon) ? NSOnState : NSOffState];
if (showsDockIcon) {
// Only set it here if it's YES, since we've just read a saved default and we always start with no Dock icon.
[NSApp setShowsDockIcon:showsDockIcon];
}
// Create transparent window.
NSRect screensFrame = [[NSScreen mainScreen] frame];
for (NSScreen *thisScreen in [NSScreen screens]) {
screensFrame = NSUnionRect(screensFrame, [thisScreen frame]);
}
window = [[MGTransparentWindow windowWithFrame:screensFrame] retain];
// Configure window.
[window setReleasedWhenClosed:YES];
[window setHidesOnDeactivate:NO];
[window setCanHide:NO];
[window setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
[window setIgnoresMouseEvents:YES];
[window setLevel:NSScreenSaverWindowLevel];
[window setDelegate:self];
// Configure contentView.
NSView *contentView = [window contentView];
[contentView setWantsLayer:YES];
CALayer *layer = [contentView layer];
layer.backgroundColor = CGColorGetConstantColor(kCGColorBlack);
layer.opacity = 0;
[window makeFirstResponder:contentView];
// Activate statusItem.
NSStatusBar *bar = [NSStatusBar systemStatusBar];
statusItem = [bar statusItemWithLength:NSSquareStatusItemLength];
[statusItem retain];
[statusItem setImage:STATUS_MENU_ICON];
[statusItem setAlternateImage:STATUS_MENU_ICON_ALT];
[statusItem setHighlightMode:YES];
[opacitySlider setFloatValue:(1.0 - opacity)];
[statusItem setMenu:statusMenu];
// Set appropriate initial display state.
shadyEnabled = [defaults boolForKey:KEY_ENABLED];
[self updateEnabledStatus];
self.opacity = [defaults floatForKey:KEY_OPACITY];
// Only show help text when activated _after_ we've launched and hidden ourselves.
showsHelpWhenActive = NO;
// Put this app into the background (the shade won't hide due to how its window is set up above).
[NSApp hide:self];
// Put window on screen.
[window makeKeyAndOrderFront:self];
}
- (void)dealloc
{
if (statusItem) {
[[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
[statusItem release];
statusItem = nil;
}
[window removeChildWindow:helpWindow];
[helpWindow close];
[window close];
window = nil; // released when closed.
helpWindow = nil; // released when closed.
[super dealloc];
}
#pragma mark Notifications handlers
- (void)applicationDidBecomeActive:(NSNotification *)aNotification
{
[self applicationActiveStateChanged:aNotification];
}
- (void)applicationDidResignActive:(NSNotification *)aNotification
{
[self applicationActiveStateChanged:aNotification];
}
- (void)applicationActiveStateChanged:(NSNotification *)aNotification
{
BOOL appActive = [NSApp isActive];
if (appActive) {
// Give the window a kick into focus, so we still get key-presses.
[window makeKeyAndOrderFront:self];
}
if (!showsHelpWhenActive && !appActive) {
// Enable help text display when active from now on.
showsHelpWhenActive = YES;
} else if (showsHelpWhenActive) {
[self toggleHelpDisplay];
}
}
#pragma mark IBActions
- (IBAction)showAbout:(id)sender
{
// We wrap this for the statusItem to ensure Shady comes to the front first.
[NSApp activateIgnoringOtherApps:YES];
[NSApp orderFrontStandardAboutPanel:self];
}
- (IBAction)showPreferences:(id)sender
{
[NSApp activateIgnoringOtherApps:YES];
[prefsWindow makeKeyAndOrderFront:self];
}
- (IBAction)increaseOpacity:(id)sender
{
// i.e. make screen darker by making our mask less transparent.
if (shadyEnabled) {
self.opacity = opacity + OPACITY_UNIT;
} else {
NSBeep();
}
}
- (IBAction)decreaseOpacity:(id)sender
{
// i.e. make screen lighter by making our mask more transparent.
if (shadyEnabled) {
self.opacity = opacity - OPACITY_UNIT;
} else {
NSBeep();
}
}
- (IBAction)opacitySliderChanged:(id)sender
{
self.opacity = (1.0 - [sender floatValue]);
}
- (IBAction)toggleDockIcon:(id)sender
{
BOOL showsDockIcon = ([sender state] != NSOffState);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:showsDockIcon forKey:KEY_DOCKICON];
[defaults synchronize];
[NSApp setShowsDockIcon:showsDockIcon];
}
- (IBAction)toggleEnabledStatus:(id)sender
{
shadyEnabled = !shadyEnabled;
[self updateEnabledStatus];
}
- (void)keyDown:(NSEvent *)event
{
if ([event window] == window) {
unsigned short keyCode = [event keyCode];
if (keyCode == 12 || keyCode == 53) { // q || Esc
[NSApp terminate:self];
} else if (keyCode == 126) { // up-arrow
[self decreaseOpacity:self];
} else if (keyCode == 125) { // down-arrow
[self increaseOpacity:self];
} else if (keyCode == 1) { // s
[self toggleEnabledStatus:self];
} else if (keyCode == 43) { // ,
[self showPreferences:self];
} else {
//NSLog(@"keyCode: %d", keyCode);
}
}
}
#pragma mark Helper methods
- (void)toggleHelpDisplay
{
if (!helpWindow) {
// Create helpWindow.
NSRect mainFrame = [[NSScreen mainScreen] frame];
NSRect helpFrame = NSZeroRect;
float width = 600;
float height = 200;
helpFrame.origin.x = (mainFrame.size.width - width) / 2.0;
helpFrame.origin.y = 200.0;
helpFrame.size.width = width;
helpFrame.size.height = height;
helpWindow = [[MGTransparentWindow windowWithFrame:helpFrame] retain];
// Configure window.
[helpWindow setReleasedWhenClosed:YES];
[helpWindow setHidesOnDeactivate:NO];
[helpWindow setCanHide:NO];
[helpWindow setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
[helpWindow setIgnoresMouseEvents:YES];
// Configure contentView.
NSView *contentView = [helpWindow contentView];
[contentView setWantsLayer:YES];
CATextLayer *layer = [CATextLayer layer];
layer.opacity = 0;
[contentView setLayer:layer];
CGColorRef bgColor = CGColorCreateGenericGray(0.0, 0.6);
layer.backgroundColor = bgColor;
CGColorRelease(bgColor);
layer.string = (shadyEnabled) ? HELP_TEXT : HELP_TEXT_OFF;
layer.contentsRect = CGRectMake(0, 0, 1, 1.2);
layer.fontSize = 40.0;
layer.foregroundColor = CGColorGetConstantColor(kCGColorWhite);
layer.borderColor = CGColorGetConstantColor(kCGColorWhite);
layer.borderWidth = 4.0;
layer.cornerRadius = 15.0;
layer.alignmentMode = kCAAlignmentCenter;
[window addChildWindow:helpWindow ordered:NSWindowAbove];
}
if (showsHelpWhenActive) {
float helpOpacity = (([NSApp isActive] ? 1 : 0));
[[[helpWindow contentView] layer] setOpacity:helpOpacity];
}
}
- (void)updateEnabledStatus
{
// Save state.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:shadyEnabled forKey:KEY_ENABLED];
[defaults synchronize];
// Show or hide the shade layer's view appropriately.
[[[window contentView] animator] setHidden:!shadyEnabled];
// Modify help text shown when we're frontmost.
if (helpWindow) {
CATextLayer *helpLayer = (CATextLayer *)[[helpWindow contentView] layer];
helpLayer.string = (shadyEnabled) ? HELP_TEXT : HELP_TEXT_OFF;
}
// Update both enable/disable menu-items (in the main menubar and in the NSStatusItem's menu).
[stateMenuItemMainMenu setTitle:(shadyEnabled) ? STATE_MENU : STATE_MENU_OFF];
[stateMenuItemStatusBar setTitle:(shadyEnabled) ? STATE_MENU : STATE_MENU_OFF];
// Update status item's regular and alt/selected images.
[statusItem setImage:(shadyEnabled) ? STATUS_MENU_ICON : STATUS_MENU_ICON_OFF];
[statusItem setAlternateImage:(shadyEnabled) ? STATUS_MENU_ICON_ALT : STATUS_MENU_ICON_OFF_ALT];
// Enable/disable slider.
[opacitySlider setEnabled:shadyEnabled];
}
#pragma mark Accessors
- (void)setOpacity:(float)newOpacity
{
float normalisedOpacity = MIN(MAX_OPACITY, MAX(newOpacity, 0.0));
if (normalisedOpacity != opacity) {
opacity = normalisedOpacity;
[[[window contentView] layer] setOpacity:opacity];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setFloat:opacity forKey:KEY_OPACITY];
[defaults synchronize];
[opacitySlider setFloatValue:(1.0 - opacity)];
}
}
@end