/
AppDelegate.m
120 lines (96 loc) · 3.93 KB
/
AppDelegate.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
//
// AppDelegate.m
// NinjaMode
//
// Created by Nirbhay Agarwal on 08/10/14.
// Updated by Benedikt-Alexander Mokroß on 21/04/2017 (Fixed depricated API).
// Copyright (c) 2014 NSRover. All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@property (strong, nonatomic) NSStatusItem *statusItem;
@property (assign, nonatomic) BOOL darkModeOn;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
NSStatusBarButton* statusBarButton = _statusItem.button;
statusBarButton.image = [NSImage imageNamed:@"switchIcon.png"];
[statusBarButton.image setTemplate:YES];
statusButton.highlighted = NO;
statusBarButton.toolTip = @"control-click to quit";
[statusBarButton setAction:@selector(itemClicked:)];
[self refreshDarkMode];
}
- (void)refreshDarkMode {
NSString * value = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
if ([value isEqualToString:@"Dark"]) {
self.darkModeOn = YES;
}
else {
self.darkModeOn = NO;
}
}
- (void)itemClicked:(id)sender {
//Look for control click, close app if so
NSEvent *event = [NSApp currentEvent];
if([event modifierFlags] & NSControlKeyMask) {
[[NSApplication sharedApplication] terminate:self];
return;
}
//Change theme
[self toggleTheme];
//Toggle darkMode
_darkModeOn = !_darkModeOn;
//Change desktop
if (_darkModeOn) {
[self makeDesktopDark];
}
else {
[self makeDesktopBright];
}
}
- (void)toggleTheme {
NSString* path = [[NSBundle mainBundle] pathForResource:@"ThemeToggle" ofType:@"scpt"];
NSURL* url = [NSURL fileURLWithPath:path];
NSDictionary* errors = [NSDictionary dictionary];
NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
[appleScript executeAndReturnError:nil];
}
- (void)makeDesktopDark {
//set wallpaper
[self changeWallpaperWithImagePath:[@"~/Documents/NinjaModes/dark.png"
stringByExpandingTildeInPath]];
}
- (void)makeDesktopBright {
//set wallpaper
[self changeWallpaperWithImagePath:[@"~/Documents/NinjaModes/bright.png"
stringByExpandingTildeInPath]];
}
- (void)changeWallpaperWithImagePath:(NSString *)path {
//If NinjaModes directory does not exist, assume not interested.
if (![[NSFileManager defaultManager] fileExistsAtPath:[@"~/Documents/NinjaModes" stringByExpandingTildeInPath]]) {
return;
}
NSError *error;
[[NSWorkspace sharedWorkspace] setDesktopImageURL:[NSURL fileURLWithPath:path]
forScreen:[NSScreen mainScreen]
options:[NSDictionary dictionaryWithObjectsAndKeys:
nil, NSWorkspaceDesktopImageFillColorKey,
[NSNumber numberWithBool:NO], NSWorkspaceDesktopImageAllowClippingKey,
[NSNumber numberWithInteger:NSImageScaleProportionallyUpOrDown], NSWorkspaceDesktopImageScalingKey, nil]
error:&error];
if (error) {
[[NSApplication sharedApplication] presentError: error
modalForWindow: self.window
delegate: nil
didPresentSelector: nil
contextInfo: NULL];
}
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
//Attempt to restore things back the way we found them
[self makeDesktopBright];
}
@end