-
Notifications
You must be signed in to change notification settings - Fork 6
/
NSWindow+KKSwizzle.mm
57 lines (50 loc) · 1.96 KB
/
NSWindow+KKSwizzle.mm
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
//
// NSWindow+KKSwizzle.mm
//
//
#import "JRSwizzle.h"
#import "NSWindow+KKSwizzle.h"
#import "WakaTime.h"
@implementation NSWindow (KKSwizzle)
+ (void) load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[[self class] jr_swizzleMethod:@selector(setRepresentedFilename:) withMethod:@selector(xxx_setRepresentedFilename:) error:nil];
[[self class] jr_swizzleMethod:@selector(becomeMainWindow:) withMethod:@selector(xxx_becomeMainWindow:) error:nil];
[[self class] jr_swizzleMethod:@selector(sendEvent:) withMethod:@selector(xxx_sendEvent:) error:nil];
[[self class] jr_swizzleMethod:@selector(setDocumentEdited:) withMethod:@selector(xxx_setDocumentEdited:) error:nil];
});
}
// called when the user switches tabs (or load files)
- (void)xxx_setRepresentedFilename:(NSString*)aPath {
//NSLog(@"%s", _cmd);
[WakaTime setFileForWindow:aPath forWindow:self.windowNumber];
NSString *file = [WakaTime getFileForWindow:self.windowNumber];
[WakaTime handleEditorAction:file isWrite:false];
[self xxx_setRepresentedFilename:aPath];
}
// called when a window is focused
- (void)xxx_becomeMainWindow {
//NSLog(@"%s", _cmd);
NSString *file = [WakaTime getFileForWindow:self.windowNumber];
[WakaTime handleEditorAction:file isWrite:false];
[self xxx_becomeMainWindow];
}
// called when a key is pressed
- (void)xxx_sendEvent:(NSEvent *)event {
if (event.type == NSKeyDown) {
//NSLog(@"%s", _cmd);
NSString *file = [WakaTime getFileForWindow:self.windowNumber];
[WakaTime handleEditorAction:file isWrite:false];
}
[self xxx_sendEvent:event];
}
// called when a document change state (e.g. when saved to disk)
- (void)xxx_setDocumentEdited:(BOOL)flag {
//NSLog(@"%s", _cmd);
NSString *file = [WakaTime getFileForWindow:self.windowNumber];
bool isWrite = !flag;
[WakaTime handleEditorAction:file isWrite:isWrite];
[self xxx_setDocumentEdited:flag];
}
@end