aboutsummaryrefslogtreecommitdiff
path: root/VKPC/AXStatusItemPopup/Popover.m
blob: 3c622752e10d2440ca8505dc7c6786e76f4b37ca (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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
//  Created by Alexander Schuch on 06/03/13.
//  Modified by Eugene Zinoviev on 12/03/13.
//  Copyright (c) 2013 Alexander Schuch. All rights reserved.
//  Copyright (c) 2013-2014 Eugene Zinoviev. All rights reserved.
//

#import "Popover.h"
#import "PopoverImageView.h"

static const int kMinViewWidth = 28;

@implementation Popover {
    PopoverImageView *imageView;
    id popoverTransiencyMonitor;
    BOOL popoverTransiencyMonitorEnabled;
//    BOOL firstDrawed;
//    id eventMonitor;
//    BOOL escMonitorSet;
}

+ (id)shared {
    static Popover *shared = nil;
    @synchronized (self) {
        if (shared == nil){
            shared = [[self alloc] init];
        }
    }
    return shared;
}

- (id)init {
    popoverTransiencyMonitorEnabled = NO;
//    firstDrawed = NO;
    CGFloat height = [NSStatusBar systemStatusBar].thickness;
    _active = NO;
//    escMonitorSet = NO;
    
    if (self = [super initWithFrame:NSMakeRect(0, 0, kMinViewWidth, height)]) {
        imageView = [[PopoverImageView alloc] initWithFrame:NSMakeRect(0, 0, kMinViewWidth, height)];
        [self addSubview:imageView];
        
        _statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
        _statusItem.view = self;
        
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
//            [self showPopover];
//        });
    }
    return self;
}

- (BOOL)allowsVibrancy {
    return YES;
}

- (void)drawRect:(NSRect)dirtyRect {
//    if (!firstDrawed) {
//        firstDrawed = YES;
//        [self showPopover];
//    }
    
    if (_active) {
        [[NSColor selectedMenuItemColor] setFill];
    } else {
        [[NSColor clearColor] setFill];
    }
    NSRectFill(dirtyRect);
    
    NSDictionary *images = VKPCGetImagesDictionary();
    NSImage *imgDefault = images[VKPCImageStatus], *imgActive = images[VKPCImageStatusPressed];
    imageView.image = _active ? imgActive : imgDefault;
}

- (void)mouseDown:(NSEvent *)theEvent {
    if (_popover.isShown) {
        [self hidePopover];
    } else {
        [self showPopover];
    }
}

- (void)setActive:(BOOL)active {
    _active = active;
    [self setNeedsDisplay:YES];
}

- (void)updateViewFrame {
    CGFloat width = MAX(MAX(kMinViewWidth, _altImage.size.width), _defaultImage.size.width);
    CGFloat height = [NSStatusBar systemStatusBar].thickness;
    
    NSRect frame = NSMakeRect(0, 0, width, height);
    self.frame = frame;
    imageView.frame = frame;
    
    [self setNeedsDisplay:YES];
}

- (void)showPopover {
    self.active = YES;
    
    if (!_popover) {
        _popover = [[NSPopover alloc] init];
        _popover.contentViewController = [PopoverController shared];
        
//        NSEvent *(^handler)(NSEvent *) = ^NSEvent *(NSEvent *theEvent) {
//            if (theEvent.keyCode == 53) {
//                NSLog(@"[Popover] catch ESC");
//                return nil;
//            }
//
//            return theEvent;
//        };
//        
//        eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:handler];
        
    };
    
    if (!_popover.isShown) {
        _popover.animates = NO;
        [_popover showRelativeToRect:self.frame ofView:self preferredEdge:NSMinYEdge];
        if (!popoverTransiencyMonitorEnabled) {
            popoverTransiencyMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDownMask|NSRightMouseDownMask handler:^(NSEvent* event) {
                [self hidePopover];
            }];
            popoverTransiencyMonitorEnabled = YES;
        }
    }
    
    [[PopoverController shared] popoverDidShow];
}

- (void)hidePopover {
    self.active = NO;
    
    if (_popover && _popover.isShown) {
        [_popover close];
        [[PopoverController shared] popoverDidHide];
        if (popoverTransiencyMonitorEnabled) {
            [NSEvent removeMonitor:popoverTransiencyMonitor];
            popoverTransiencyMonitorEnabled = NO;
        }
    }
}

- (NSSize)getSize {
    return [_popover contentSize];
}

- (void)setSize:(NSSize)size animate:(BOOL)animate {
    BOOL bkAnimates = _popover.animates;
    _popover.animates = animate;
    [_popover setContentSize:size];
    _popover.animates = bkAnimates;
}

@end