-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppController.m
104 lines (88 loc) · 3.44 KB
/
AppController.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
#import "AppController.h"
#define IS_CAPS_LOCK(x) (x & alphaLock)
@implementation AppController
+ (void)initialize {
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObject:[NSNumber numberWithBool:NO]] forKeys:[NSArray arrayWithObject:CLStateOfAlarm]]];
}
- (void)awakeFromNib {
NSStatusBar *bar = [NSStatusBar systemStatusBar];
menuItem = [bar statusItemWithLength:NSVariableStatusItemLength];
[menuItem retain]; // keep it!
menuItem.button.title = NSLocalizedString(@"📢",@""); // title
menuItem.button.cell.highlighted = YES; // behave like main menu
[menuItem setMenu:menuItemMenu];
isAlarming = NO;
alarmIsEnabled = [[[NSUserDefaults standardUserDefaults] objectForKey:CLStateOfAlarm] boolValue];
sound = [[NSSound soundNamed:@"Alarm"] retain];
// Set menu state
if (alarmIsEnabled) {
[enabledMenuItem setTitle:NSLocalizedString(@"Deactivate warning",@"")];
[enabledMenuItem setTag:1];
} else {
[enabledMenuItem setTitle:NSLocalizedString(@"Activate warning",@"")];
[enabledMenuItem setTag:0];
}
[quitMenuItem setTitle:[NSLocalizedString(@"Quit",@"") stringByAppendingString:@" Carbon CapsBeeper"]];
}
- (IBAction)menuChanged:(id)sender {
if ([enabledMenuItem tag] == 0) { // Activate alarm
[enabledMenuItem setTitle:NSLocalizedString(@"Deactivate warning",@"")];
[self setAlarmIsEnabled:YES];
[enabledMenuItem setTag:1];
} else { // Deactivate alarm
[enabledMenuItem setTitle:NSLocalizedString(@"Activate warning",@"")];
[self setAlarmIsEnabled:NO];
[enabledMenuItem setTag:0];
}
}
- (void)setAlarmIsEnabled:(BOOL)aBool {
if (aBool != alarmIsEnabled) {
if (aBool == YES) {
timer = [[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(checkCapsLock:)
userInfo:nil
repeats:YES] retain];
} else {
if ([sound isPlaying])
[sound stop];
isAlarming = NO;
[timer invalidate];
[timer release];
timer = nil;
}
alarmIsEnabled = aBool;
}
}
- (BOOL)alarmIsEnabled {
return alarmIsEnabled;
}
- (void)checkCapsLock:(NSTimer *)aTimer {
int keyModifier = GetCurrentKeyModifiers(); // the only real Carbon function
if ( IS_CAPS_LOCK(keyModifier) && !isAlarming) {
isAlarming = YES;
[sound play];
}
else if ( !IS_CAPS_LOCK(keyModifier) && isAlarming) {
isAlarming = NO;
[sound stop];
}
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
alarmIsEnabled = !alarmIsEnabled;
[self setAlarmIsEnabled:!alarmIsEnabled];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
[[NSStatusBar systemStatusBar] removeStatusItem:menuItem];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:[self alarmIsEnabled]] forKey:CLStateOfAlarm];
}
- (void)dealloc {
[self setAlarmIsEnabled:NO];
[menuItem release];
[sound release];
[super dealloc];
}
- (IBAction)openWebsite:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://github.com/henrik242/ccb"]];
}
@end