-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathViewController.m
184 lines (128 loc) · 6.09 KB
/
ViewController.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
#import "ViewController.h"
#import "AppDelegate.h"
static NSString *githubURL = @"https://github.com/pixel-point/mute-me";
static NSString *projectURL = @"https://muteme.pixelpoint.io/";
static NSString *companyURL = @"https://pixelpoint.io/";
static NSString *const MASCustomShortcutKey = @"customShortcut";
static void *MASObservingContext = &MASObservingContext;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"auto_login"] == nil) {
// the opposite is used later
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"auto_login"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
BOOL state = [[NSUserDefaults standardUserDefaults] boolForKey:@"auto_login"];
[self.autoLoginState setState: !state];
BOOL hideStatusBarState = [[NSUserDefaults standardUserDefaults] boolForKey:@"hide_status_bar"];
[self.showInMenuBarState setState: hideStatusBarState];
BOOL statusBarButtonToggle = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_button_toggle"];
[self.statusBarButtonToggle setState: statusBarButtonToggle];
BOOL useAlternateStatusBarIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_alternate_icons"];
[self.useAlternateStatusBarIcons setState: useAlternateStatusBarIcons];
// Make a global context reference
void *kGlobalShortcutContext = &kGlobalShortcutContext;
// this sets the existing shortcut and allows it to save
[self.masShortCutView setAssociatedUserDefaultsKey:MASCustomShortcutKey];
// Implement when loading view
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults addObserver:self forKeyPath:MASCustomShortcutKey
options:NSKeyValueObservingOptionInitial
context:MASObservingContext];
// set version from plist to label
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSString *buildLabel = [buildVersion isEqualToString:@"1"] ? @"" :[NSString stringWithFormat:@"(%@)", buildVersion];
NSString *versionFieldValue = [NSString stringWithFormat:@"Version %@%@", version, buildLabel];
[self.versionTextFieldCell setStringValue:versionFieldValue];
}
- (void) observeValueForKeyPath: (NSString*) keyPath ofObject: (id) object change: (NSDictionary*) change context: (void*) context
{
if (context != MASObservingContext) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
return;
}
if ([keyPath isEqualToString:MASCustomShortcutKey]) {
NSLog (@"change key");
[[MASShortcutBinder sharedBinder] bindShortcutWithDefaultsKey:MASCustomShortcutKey toAction:^{
AppDelegate *appDelegate = (AppDelegate *) [[NSApplication sharedApplication] delegate];
[appDelegate shortCutKeyPressed];
}];
}
}
-(void)viewDidAppear {
[super viewDidAppear];
[[self.view window] center];
}
- (IBAction)quitPressed:(id)sender {
[NSApp terminate:nil]; //TODO or quit about window
}
- (IBAction)onGithubPressed:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:githubURL]];
}
- (IBAction)onWebsitePressed:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:projectURL]];
}
- (IBAction)onLoginStartChanged:(id)sender {
NSInteger state = [self.autoLoginState state];
BOOL enableState = NO;
if(state == NSOnState) {
enableState = YES;
}
if(SMLoginItemSetEnabled((__bridge CFStringRef)@"Pixel-Point.Mute-Me-Now-Launcher", enableState)) {
[[NSUserDefaults standardUserDefaults] setBool:!enableState forKey:@"auto_login"];
}
}
- (IBAction)showMenuBarChanged:(id)sender {
NSInteger state = [self.showInMenuBarState state];
BOOL enableState = NO;
if(state == NSOnState) {
enableState = YES;
}
[[NSUserDefaults standardUserDefaults] setBool:enableState forKey:@"hide_status_bar"];
[[NSUserDefaults standardUserDefaults] synchronize];
AppDelegate *appDelegate = (AppDelegate *) [[NSApplication sharedApplication] delegate];
[appDelegate hideMenuBar:enableState];
if (enableState == YES) {
NSString *msgText = @"Long press on the Touch Bar Mute Button to show Preferences when the Menu Item is disabled.";
NSAlert* msgBox = [[NSAlert alloc] init] ;
[msgBox setMessageText:msgText];
[msgBox addButtonWithTitle: @"OK"];
[msgBox runModal];
}
}
- (IBAction)statusBarToggleChanged:(id)sender {
NSInteger hideState = [self.showInMenuBarState state];
if(hideState == NSOnState) {
return;
}
NSInteger state = [self.statusBarButtonToggle state];
BOOL enableState = NO;
if(state == NSOnState) {
enableState = YES;
}
[[NSUserDefaults standardUserDefaults] setBool:enableState forKey:@"status_bar_button_toggle"];
[[NSUserDefaults standardUserDefaults] synchronize];
AppDelegate *appDelegate = (AppDelegate *) [[NSApplication sharedApplication] delegate];
[appDelegate hideMenuBar:NO];
}
- (IBAction)useAlternateStatusBarIconsChanged:(id)sender {
NSInteger hideState = [self.showInMenuBarState state];
if(hideState == NSOnState) {
return;
}
NSInteger state = [self.useAlternateStatusBarIcons state];
BOOL enableState = NO;
if(state == NSOnState) {
enableState = YES;
}
[[NSUserDefaults standardUserDefaults] setBool:enableState forKey:@"status_bar_alternate_icons"];
[[NSUserDefaults standardUserDefaults] synchronize];
AppDelegate *appDelegate = (AppDelegate *) [[NSApplication sharedApplication] delegate];
[appDelegate hideMenuBar:NO];
}
- (IBAction)onMainWebsitePressed:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:companyURL]];
}
@end