-
Notifications
You must be signed in to change notification settings - Fork 139
/
LauncherController.m
68 lines (59 loc) · 1.75 KB
/
LauncherController.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
//
// LauncherController.m
// Scroll Reverser
//
// Created by Nicholas Moore on 25/11/2020.
//
#import "LauncherController.h"
#import <ServiceManagement/ServiceManagement.h>
static NSString *const kPrefsStartAtLogin=@"StartAtLogin";
@interface LauncherController ()
@property LSSharedFileListRef loginItems;
@end
@implementation LauncherController
- (instancetype)init
{
self=[super init];
if (self) {
[self setFromPrefs];
}
return self;
}
// older versions used an embedded xpc launcher and also saved the state in prefs as a backup.
// here we "one shot" migrate the state from the old prefs to the new method.
- (void)setFromPrefs
{
const BOOL state=[[NSUserDefaults standardUserDefaults] boolForKey:kPrefsStartAtLogin];
if (state) {
[self setStartAtLogin:YES];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kPrefsStartAtLogin];
}
}
- (void)setStartAtLogin:(BOOL)state
{
if (@available(macOS 13.0, *)) {
[self willChangeValueForKey:@"startAtLogin"];
NSError *error=nil;
if (state) {
if (SMAppService.mainAppService.status==SMAppServiceStatusEnabled) {
[SMAppService.mainAppService unregisterAndReturnError:&error];
}
[SMAppService.mainAppService registerAndReturnError:&error];
} else {
[SMAppService.mainAppService unregisterAndReturnError:&error];
}
if (error) {
NSLog(@"Error setting startAtLogin to %@: %@", @(state), error);
}
[self didChangeValueForKey:@"startAtLogin"];
}
}
- (BOOL)startAtLogin
{
if (@available(macOS 13.0, *)) {
return SMAppService.mainAppService.status==SMAppServiceStatusEnabled;
} else {
return NO;
}
}
@end