aboutsummaryrefslogtreecommitdiff
path: root/VKPC/WindowController.m
blob: 247377a4102291e7163ba6a4848300aaeacf199e (plain)
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
//
//  WindowController.m
//  VKPC
//
//  Created by Eugene on 12/1/13.
//  Copyright (c) 2013-2014 Eugene Z. All rights reserved.
//

#import "WindowController.h"

@implementation WindowController {
    id eventMonitor;
    BOOL eventMonitorSet;
}

- (id)initWithWindow:(NSWindow *)window {
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (BOOL)allowsClosingWithShortcut {
    return NO;
}

- (void)showWindow:(id)sender {
    [super showWindow:sender];
    
    if ([self allowsClosingWithShortcut] && !eventMonitorSet) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(windowWillClose:)
                                                     name:NSWindowWillCloseNotification
                                                   object:self.window];
        
        // Close window on esc or cmd+w
        NSEvent *(^handler)(NSEvent *) = ^(NSEvent *theEvent) {
            NSWindow *targetWindow = theEvent.window;
            if (targetWindow != self.window) {
                return theEvent;
            }
            
            NSEvent *result = theEvent;
            if (theEvent.keyCode == 53 || ( theEvent.keyCode == 13 && [theEvent modifierFlags] & NSCommandKeyMask )) {
                [self.window close];
            }
            
            return result;
        };
        
        eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:handler];
        eventMonitorSet = YES;
    }
}

- (void)windowDidLoad {
    eventMonitorSet = NO;
    [super windowDidLoad];
}

- (void)windowWillClose:(NSNotification *)notification {
    // [NSEvent removeMonitor:eventMonitor];
}


@end