-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathPHEventHandler.m
90 lines (67 loc) · 2.31 KB
/
PHEventHandler.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
/*
* Phoenix is released under the MIT License. Refer to https://github.com/kasper/phoenix/blob/master/LICENSE.md
*/
@import Cocoa;
#import "PHEventHandler.h"
#import "PHAXObserver.h"
#import "PHApp.h"
#import "PHEventTranslator.h"
#import "PHGlobalEventMonitor.h"
#import "PHWindow.h"
@interface PHEventHandler ()
@property(weak) NSNotificationCenter *notificationCenter;
@property(copy) NSString *name;
@property(copy) NSString *notification;
@end
@implementation PHEventHandler
#pragma mark - Initialising
- (instancetype)initWithEvent:(NSString *)event callback:(JSValue *)callback {
if (self = [super initWithCallback:callback]) {
self.name = event;
self.notification = [PHEventTranslator notificationForEvent:event];
// Event not supported
if (!self.notification) {
NSLog(@"Warning: Event “%@” not supported.", event);
return self;
}
// Observe event notification
self.notificationCenter = [PHEventTranslator notificationCenterForNotification:self.notification];
[self.notificationCenter addObserver:self
selector:@selector(didReceiveNotification:)
name:self.notification
object:nil];
}
return self;
}
#pragma mark - Deallocing
- (void)dealloc {
[self disable];
}
#pragma mark - Binding
- (void)disable {
[self.notificationCenter removeObserver:self name:self.notification object:nil];
}
#pragma mark - Notification Handling
- (void)didReceiveNotification:(NSNotification *)notification {
NSDictionary<NSString *, id> *mouse = notification.userInfo[PHGlobalEventMonitorMouseKey];
NSRunningApplication *runningApp = notification.userInfo[NSWorkspaceApplicationKey];
PHWindow *window = notification.userInfo[PHAXObserverWindowKey];
// Notification for mouse
if (mouse) {
[self callWithArguments:@[mouse, self]];
return;
}
// Notification for app
if (runningApp) {
PHApp *app = [[PHApp alloc] initWithApp:runningApp];
[self callWithArguments:@[app, self]];
return;
}
// Notification for window
if (window) {
[self callWithArguments:@[window, self]];
return;
}
[self callWithArguments:@[self]];
}
@end