forked from pilotmoon/Scroll-Reverser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.m
328 lines (279 loc) · 10.9 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
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
// This file is part of Scroll Reverser <https://pilotmoon.com/scrollreverser/>
// Licensed under Apache License v2.0 <http://www.apache.org/licenses/LICENSE-2.0>
#import "AppDelegate.h"
#import "StatusItemController.h"
#import "LoginItemsController.h"
#import "MouseTap.h"
#import "WelcomeWindowController.h"
#import "PrefsWindowController.h"
#import "DebugWindowController.h"
#import "TestWindowController.h"
#import "TapLogger.h"
#import <Sparkle/SUUpdater.h>
NSString *const PrefsReverseScrolling=@"InvertScrollingOn";
NSString *const PrefsReverseHorizontal=@"ReverseX";
NSString *const PrefsReverseVertical=@"ReverseY";
NSString *const PrefsReverseTrackpad=@"ReverseTrackpad";
NSString *const PrefsReverseMouse=@"ReverseMouse";
NSString *const PrefsReverseTablet=@"ReverseTablet";
NSString *const PrefsHasRunBefore=@"HasRunBefore";
NSString *const PrefsHideIcon=@"HideIcon";
void *kContextHideIcon=&kContextHideIcon;
void *kContextReverseScrolling=&kContextReverseScrolling;
@implementation AppDelegate
+ (void)initialize
{
if ([self class]==[AppDelegate class])
{
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
PrefsReverseScrolling: @(YES),
PrefsReverseHorizontal: @(NO),
PrefsReverseVertical: @(YES),
PrefsReverseTrackpad: @(YES),
PrefsReverseMouse: @(YES),
PrefsReverseTablet: @(YES),
LoggerMaxEntries: @(50000),
}];
}
}
- (void)relaunch
{
// based on https://gist.github.com/cdfmr/2204627
const int processID=[[NSProcessInfo processInfo] processIdentifier];
NSString *const command=[NSString stringWithFormat:@"logger \"waiting for pid %1$d\"; while kill -0 %1$d >/dev/null 2>&1; do sleep 0.01; done; logger \"restarting Scroll Reverser\"; open \"%2$@\"", processID, [[NSBundle mainBundle] bundlePath]];
NSLog(@"[%@]", command);
NSTask *const task=[[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:@[@"-c", command]];
[task launch];
[NSApp terminate:nil];
}
- (BOOL)alreadyRunning
{
const BOOL alreadyRunning=^BOOL {
for (NSRunningApplication *app in [[NSWorkspace sharedWorkspace] runningApplications]) {
if (![app isEqual:[NSRunningApplication currentApplication]]) {
if ([app.bundleIdentifier isEqualToString:[NSRunningApplication currentApplication].bundleIdentifier]) {
return YES;
}
}
}
return NO;
}();
if (alreadyRunning) {
[[NSAlert alertWithMessageText:[NSString stringWithFormat:NSLocalizedString(@"%@ is already running.", nil), self.appName]
defaultButton:NSLocalizedString(@"Quit", nil)
alternateButton:nil
otherButton:nil
informativeTextWithFormat:NSLocalizedString(@"%@ cannot start while another copy is running.", nil), self.appName] runModal];
}
return alreadyRunning;
}
- (NSURL *)feedURL
{
if ([[self.appVersion componentsSeparatedByString:@"-"] count]>1) { // if version string has a dash, it's a beta
return [NSURL URLWithString:@"https://rink.hockeyapp.net/api/2/apps/4eb70fe73a84cb8cd252855a6d7b1bb3"];
}
else {
return [NSURL URLWithString:@"https://softwareupdate.pilotmoon.com/update/scrollreverser/appcast.xml"];
}
}
- (id)init
{
self=[super init];
if (self) {
if([self alreadyRunning]) {
quitting=YES;
dispatch_async(dispatch_get_main_queue(), ^{
[NSApp terminate:nil];
});
}
else {
tap=[[MouseTap alloc] init];
loginItemsController=[[LoginItemsController alloc] init];
statusController=[[StatusItemController alloc] init];
statusController.statusItemDelegate=self;
statusController.visible=![[NSUserDefaults standardUserDefaults] boolForKey:PrefsHideIcon];
[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:PrefsHideIcon options:0 context:kContextHideIcon];
[[SUUpdater sharedUpdater] setDelegate:self];
[[SUUpdater sharedUpdater] setFeedURL:[self feedURL]];
}
}
return self;
}
- (void)dealloc
{
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
}
- (void)awakeFromNib
{
if(quitting) return;
[statusController attachMenu:statusMenu];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
if(quitting) return;
[NSApp setMainMenu:self.theMainMenu];
const BOOL first=![[NSUserDefaults standardUserDefaults] boolForKey:PrefsHasRunBefore];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:PrefsHasRunBefore];
if(first) {
welcomeWindowController=[[WelcomeWindowController alloc] initWithWindowNibName:@"WelcomeWindow"];
[welcomeWindowController showWindow:self];
}
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(appDidWake:) name:NSWorkspaceDidWakeNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(appWillSleep:) name:NSWorkspaceWillSleepNotification object:nil];
NSLog(@"Scroll Reverser ready. Option-click the Scroll Reverser menu bar icon to show the debug console.");
// this will kick off the mouse tap if already enables
[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:PrefsReverseScrolling options:NSKeyValueObservingOptionInitial context:kContextReverseScrolling];
}
- (void)appDidWake:(NSNotification *)note
{
[self logAppEvent:@"OS woke from sleep"];
NSLog(@"Scroll Reverser will relaunch itself because the OS woke from sleep.");
[self relaunch];
}
- (void)appWillSleep:(NSNotification *)note
{
[self logAppEvent:@"OS is going to sleep"];
}
- (NSString *)settingsSummary
{
NSString *(^yn)(NSString *, BOOL) = ^(NSString *label, BOOL state) {
return [NSString stringWithFormat:@"[%@ %@]", label, state?@"yes":@"no"];
};
NSString *temp=yn(@"on", [[NSUserDefaults standardUserDefaults] boolForKey:PrefsReverseScrolling]);
temp=[temp stringByAppendingString:yn(@"v", [[NSUserDefaults standardUserDefaults] boolForKey:PrefsReverseVertical])];
temp=[temp stringByAppendingString:yn(@"h", [[NSUserDefaults standardUserDefaults] boolForKey:PrefsReverseHorizontal])];
temp=[temp stringByAppendingString:yn(@"trackpad", [[NSUserDefaults standardUserDefaults] boolForKey:PrefsReverseTrackpad])];
temp=[temp stringByAppendingString:yn(@"tablet", [[NSUserDefaults standardUserDefaults] boolForKey:PrefsReverseTablet])];
temp=[temp stringByAppendingString:yn(@"mouse/other", [[NSUserDefaults standardUserDefaults] boolForKey:PrefsReverseMouse])];
return temp;
}
- (void)logAppEvent:(NSString *)str
{
[logger logMessage:[NSString stringWithFormat:@"%@ %@", str, [self settingsSummary]] special:YES];
}
- (void)toggleReversing
{
const BOOL state=[[NSUserDefaults standardUserDefaults] boolForKey:PrefsReverseScrolling];
[[NSUserDefaults standardUserDefaults] setBool:!state forKey:PrefsReverseScrolling];
}
- (Logger *)startLogging
{
if (!logger) {
logger=[[TapLogger alloc] init];
tap->logger=logger;
}
return logger;
}
- (IBAction)showDebug:(id)sender
{
[NSApp activateIgnoringOtherApps:YES];
if(!debugWindowController) {
debugWindowController=[[DebugWindowController alloc] initWithWindowNibName:@"DebugWindow"];
debugWindowController.logger=[self startLogging];
}
[debugWindowController showWindow:self];
}
- (IBAction)showPrefs:(id)sender
{
[NSApp activateIgnoringOtherApps:YES];
if(!prefsWindowController) {
prefsWindowController=[[PrefsWindowController alloc] initWithWindowNibName:@"PrefsWindow"];
}
[prefsWindowController showWindow:self];
}
- (IBAction)showAbout:(id)sender
{
[prefsWindowController close];
[NSApp activateIgnoringOtherApps:YES];
NSDictionary *dict=@{@"ApplicationName": @"Scroll Reverser"};
[NSApp orderFrontStandardAboutPanelWithOptions:dict];
}
- (IBAction)showTestWindow:(id)sender
{
[NSApp activateIgnoringOtherApps:YES];
if(!testWindowController) {
testWindowController=[[TestWindowController alloc] initWithWindowNibName:@"TestWindow"];
}
[testWindowController showWindow:self];
}
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:PrefsHideIcon];
[statusController openMenu];
return NO;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context==kContextHideIcon) {
BOOL wasVisible=statusController.visible;
statusController.visible=![[NSUserDefaults standardUserDefaults] boolForKey:PrefsHideIcon];;
if (wasVisible&&!statusController.visible)
{
[NSApp activateIgnoringOtherApps:YES];
NSAlert *alert=[NSAlert alertWithMessageText:NSLocalizedString(@"Status Icon Hidden",nil)
defaultButton:NSLocalizedString(@"OK",nil)
alternateButton:nil
otherButton:nil
informativeTextWithFormat:NSLocalizedString(@"MENU_HIDDEN_TEXT", @"text shown when the menu bar icon is hidden")];
[alert runModal];
}
}
else if (context==kContextReverseScrolling) {
if ([[NSUserDefaults standardUserDefaults] boolForKey:PrefsReverseScrolling]) {
statusController.enabled=YES;
[tap start];
}
else {
statusController.enabled=NO;
[tap stop];
}
}
}
#pragma mark Status item handling
- (void)statusItemClicked
{
// do nothing
}
- (void)statusItemRightClicked
{
[self toggleReversing];
}
- (void)statusItemAltClicked
{
[self showDebug:self];
}
#pragma mark Sparkle delegate methods
- (NSArray *)feedParametersForUpdater:(SUUpdater *)updater sendingSystemProfile:(BOOL)sendingProfile
{
NSLog(@"Checking for updates at %@", [updater feedURL]);
return [NSArray array];
}
#pragma mark App info
- (NSString *)appName {
return @"Scroll Reverser";
}
- (NSString *)appVersion {
return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}
- (NSString *)appCredit {
return @"by Nick Moore";
}
- (NSURL *)appLink {
return [NSURL URLWithString:@"https://pilotmoon.com/link/scrollreverser"];
}
- (NSString *)appDisplayLink {
return @"pilotmoon.com/scrollreverser";
}
#pragma mark Strings
- (NSString *)menuStringReverseScrolling {
return NSLocalizedString(@"Reverse Scrolling", nil);
}
- (NSString *)menuStringPreferences {
return [NSLocalizedString(@"Preferences", nil) stringByAppendingString:@"..."];
}
- (NSString *)menuStringQuit {
return NSLocalizedString(@"Quit Scroll Reverser", nil);
}
@end