Skip to content

Commit e93d463

Browse files
committed
Monitor file changes to keys file and reload hotkeys
1 parent 8f3185a commit e93d463

6 files changed

Lines changed: 74 additions & 10 deletions

File tree

CodingKeys.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@
497497
DCC6EB97182D9E3700E2EAE6 /* Release */,
498498
);
499499
defaultConfigurationIsVisible = 0;
500+
defaultConfigurationName = Release;
500501
};
501502
DCC6EB98182D9E3700E2EAE6 /* Build configuration list for PBXNativeTarget "CodingKeysTests" */ = {
502503
isa = XCConfigurationList;
@@ -505,6 +506,7 @@
505506
DCC6EB9A182D9E3700E2EAE6 /* Release */,
506507
);
507508
defaultConfigurationIsVisible = 0;
509+
defaultConfigurationName = Release;
508510
};
509511
/* End XCConfigurationList section */
510512
};

CodingKeys/AppDelegate.m

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
1616
[self setup];
1717
}
1818

19+
- (void)setup {
20+
[self setupNotifications];
21+
[self registerHotKeys];
22+
}
23+
1924
- (void)awakeFromNib {
25+
[self setupStatusBarItem];
26+
}
27+
28+
- (void)setupStatusBarItem {
2029
self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
2130
self.statusItem.menu = self.statusMenu;
2231
self.statusItem.image = [NSImage imageNamed:@"status_bar_icon"];
2332
self.statusItem.highlightMode = YES;
2433
}
2534

26-
- (void)setup {
27-
[self setupNotifications];
28-
[self setupHotKeyService];
29-
}
30-
3135
- (void)setupNotifications {
3236
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
3337
selector:@selector(didActivateApplication:)
@@ -38,13 +42,18 @@ - (void)setupNotifications {
3842
selector:@selector(didTriggerHotKey:)
3943
name:HotKeyHandlerDidTriggerHotKey
4044
object:nil];
41-
}
42-
43-
- (void)setupHotKeyService {
4445

46+
[[NSNotificationCenter defaultCenter] addObserver:self
47+
selector:@selector(didChangeHotKeys:)
48+
name:AppServiceDidChangeHotKeys
49+
object:nil];
4550
}
4651

4752
- (void)didActivateApplication:(NSNotification *)notification {
53+
[self registerHotKeys];
54+
}
55+
56+
- (void)registerHotKeys {
4857
[self unregisterHotKeys];
4958

5059
NSString *activeAppName = [self activeApplicationName];
@@ -83,6 +92,10 @@ - (void)didTriggerHotKey:(NSNotification *)notification {
8392
[[HotKeyService sharedService] dispatchKeyEventForHotKey:mappedHotKey];
8493
}
8594

95+
- (void)didChangeHotKeys:(NSNotification *)notification {
96+
[self registerHotKeys];
97+
}
98+
8699
- (IBAction)settingsClicked:(id)sender {
87100
[[AppService sharedService] openKeyMappings];
88101
}

CodingKeys/AppService.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#import <Foundation/Foundation.h>
22

3+
NSString * const AppServiceDidChangeHotKeys;
4+
35
@interface AppService : NSObject
46

57
+ (AppService *)sharedService;

CodingKeys/AppService.m

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
static NSString * const KeysFileName = @"keys";
55
static NSString * const AboutURL = @"https://github.com/fe9lix/CodingKeys";
66

7-
@interface AppService ()
7+
NSString * const AppServiceDidChangeHotKeys = @"AppServiceDidChangeHotKeys";
8+
9+
@interface AppService () <NSFilePresenter>
810

911
@property (nonatomic, strong) NSDictionary *hotKeysForAppName;
12+
@property (nonatomic, strong) NSFileCoordinator *fileCoordinator;
13+
@property (strong) NSURL *presentedItemURL;
14+
@property (strong) NSOperationQueue *presentedItemOperationQueue;
15+
@property (nonatomic, strong) NSTimer *timer;
1016

1117
@end
1218

@@ -30,6 +36,11 @@ - (id)init {
3036
}
3137

3238
- (void)setup {
39+
[self setupHotKeysForAppName];
40+
[self watchKeyFile];
41+
}
42+
43+
- (void)setupHotKeysForAppName {
3344
NSArray *keyMappings = [self loadKeyMappingsFile];
3445

3546
NSMutableDictionary *hotKeysForAppName = [NSMutableDictionary dictionary];
@@ -69,7 +80,43 @@ - (NSArray *)loadKeyMappingsFile {
6980

7081
- (NSString *)keysFilePath {
7182
return [[NSBundle mainBundle] pathForResource:KeysFileName
72-
ofType:@"json"];
83+
ofType:@"json"];
84+
}
85+
86+
- (void)watchKeyFile {
87+
self.presentedItemOperationQueue = [[NSOperationQueue alloc] init];
88+
self.presentedItemOperationQueue.maxConcurrentOperationCount = 1;
89+
90+
self.presentedItemURL = [[NSBundle mainBundle] resourceURL];
91+
92+
self.fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];
93+
94+
[NSFileCoordinator addFilePresenter:self];
95+
}
96+
97+
- (void)presentedItemDidChange {
98+
[self.timer invalidate];
99+
100+
dispatch_async(dispatch_get_main_queue(), ^{
101+
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5f
102+
target:self
103+
selector:@selector(keysDidChange:)
104+
userInfo:nil
105+
repeats:NO];
106+
});
107+
}
108+
109+
- (void)keysDidChange:(id)obj {
110+
[self setupHotKeysForAppName];
111+
[self dispatchKeysDidChangeNotification];
112+
}
113+
114+
- (void)dispatchKeysDidChangeNotification {
115+
NSNotification *notification = [NSNotification notificationWithName:AppServiceDidChangeHotKeys
116+
object:nil
117+
userInfo:nil];
118+
119+
[[NSNotificationCenter defaultCenter] postNotification:notification];
73120
}
74121

75122
- (BOOL)isAppRegistered:(NSString *)appName {
-73 Bytes
Loading

Resources/StatusBarIcon.psd

350 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)