-
Notifications
You must be signed in to change notification settings - Fork 4
/
ShroudMenuBarVisibilityController.m
224 lines (175 loc) · 7.95 KB
/
ShroudMenuBarVisibilityController.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// ShroudMenuBarVisibilityController.m
// Shroud
//
// Created by Nicholas Riley on 12/31/10.
// Copyright 2010 Nicholas Riley. All rights reserved.
//
#import "ShroudAppDelegate.h"
#import "ShroudMenuBarVisibilityController.h"
#import "ShroudMenuBarView.h"
#import "ShroudPreferencesController.h"
@interface ShroudMenuBarVisibilityController ()
- (void)peekAtMenuBar:(BOOL)peek;
- (void)setShouldCoverMenuBar:(BOOL)shouldCover;
- (void)systemUIElementsDidBecomeVisible:(BOOL)visible;
@end
static OSStatus ShroudSystemUIModeChanged(EventHandlerCallRef callRef, EventRef event, void *refcon) {
UInt32 newMode = 0;
OSStatus err;
err = GetEventParameter(event, kEventParamSystemUIMode, typeUInt32, NULL, sizeof(UInt32), NULL, &newMode);
if (err != noErr)
return err;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ShroudMenuBarVisibilityController *controller = (ShroudMenuBarVisibilityController *)refcon;
[controller systemUIElementsDidBecomeVisible:newMode == kUIModeNormal];
[pool drain];
return noErr;
}
static CGEventRef ShroudKeyboardFlagsChanged(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
ShroudMenuBarVisibilityController *controller = (ShroudMenuBarVisibilityController *)refcon;
CGEventFlags peekFlags = controller->peekFlags;
if (peekFlags == 0)
return event;
static CGEventFlags lastEventFlags;
CGEventFlags eventFlags = CGEventGetFlags(event);
if ((eventFlags & peekFlags) == peekFlags && (lastEventFlags & peekFlags) != peekFlags)
[controller peekAtMenuBar:YES];
else if ((eventFlags & peekFlags) != peekFlags && (lastEventFlags & peekFlags) == peekFlags)
[controller peekAtMenuBar:NO];
lastEventFlags = eventFlags;
return event;
}
@implementation ShroudMenuBarVisibilityController
- (id)initWithWindow:(NSWindow *)window;
{
if ( (self = [super initWithWindow:window]) == nil)
return nil;
// Watch for full screen mode changes.
static const EventTypeSpec eventSpecs[] = {{kEventClassApplication, kEventAppSystemUIModeChanged}};
InstallApplicationEventHandler(NewEventHandlerUPP(ShroudSystemUIModeChanged),
GetEventTypeCount(eventSpecs),
eventSpecs, self, &systemUIModeChangedEventHandler);
// Watch for changes to accessibility access.
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(accessibilityAccessDidChange:) name:@"com.apple.accessibility.api" object:nil suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:[@"values." stringByAppendingString:ShroudPeekAtMenuBarModifierFlagsPreferenceKey] options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:NULL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidUnhide:) name:NSApplicationDidUnhideNotification object:nil];
return self;
}
+ (void)requestAccessibilityAccess;
{
[self requestAccessibilityAccessFromWindow:nil];
}
+ (void)requestAccessibilityAccessFromWindow:(nullable NSWindow *)window;
{
NSAlert *accessibilityAccessRequestAlert = [NSAlert alertWithMessageText:@"Shroud would like to request accessibility access." defaultButton:@"Deny" alternateButton:@"Open System Preferences" otherButton:nil informativeTextWithFormat:@"With accessibility access, Shroud can let you “peek” at the menu bar by holding down keys.\n\nGrant access to Shroud in Security & Privacy System Preferences by checking the box next to Shroud.\n\nClicking Deny disables Shroud’s menu bar peek feature. Change this from Shroud’s Preferences."];
accessibilityAccessRequestAlert.alertStyle = NSAlertStyleCritical;
void (^alertHandler)(NSModalResponse returnCode) = ^(NSModalResponse modalResponse) {
if (modalResponse == NSAlertDefaultReturn) {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:ShroudPeekAtMenuBarModifierFlagsPreferenceKey];
} else {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"]];
}
};
if (window == nil) {
alertHandler([accessibilityAccessRequestAlert runModal]);
} else {
[accessibilityAccessRequestAlert beginSheetModalForWindow:window completionHandler:alertHandler];
}
}
- (void)removeMenuBarPeekTap;
{
if (menuBarPeekTap != nil)
[[NSRunLoop currentRunLoop] removePort:menuBarPeekTap forMode:NSDefaultRunLoopMode];
}
- (void)createMenuBarPeekTap;
{
[self removeMenuBarPeekTap];
// Check for accessibility access.
if (!AXIsProcessTrusted()) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.class requestAccessibilityAccess];
});
return;
}
// Create event tap to watch for menu bar peek keystroke.
menuBarPeekTap = (NSMachPort *)CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionListenOnly, CGEventMaskBit(kCGEventFlagsChanged), ShroudKeyboardFlagsChanged, self);
[[NSRunLoop currentRunLoop] addPort:menuBarPeekTap forMode:NSDefaultRunLoopMode];
}
- (void)dealloc;
{
RemoveEventHandler(systemUIModeChangedEventHandler);
[menuBarPeekTap release];
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:@"com.apple.accessibility.api" object:nil];
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:[@"values." stringByAppendingString:ShroudPeekAtMenuBarModifierFlagsPreferenceKey]];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (ShroudMenuBarView *)menuBarView;
{
return (ShroudMenuBarView *)[[self window] contentView];
}
- (void)peekAtMenuBar:(BOOL)peek;
{
[[self menuBarView] setPeeking:peek];
}
- (void)restoreMenuBarCover;
{
[[self window] setAlphaValue:0];
[[self window] orderFront:nil];
[[self menuBarView] coverMenuBarIfNeededAnimatingWithDuration:0];
}
- (void)setShouldCoverMenuBar:(BOOL)shouldCover;
{
shouldCoverMenuBar = shouldCover;
if (shouldCover)
[[self window] orderFront:nil];
else
[[self window] orderOut:nil];
if (menuBarPeekTap != nil)
CGEventTapEnable((CFMachPortRef)menuBarPeekTap, shouldCover);
}
- (void)systemUIElementsDidBecomeVisible:(BOOL)visible;
{
if (!shouldCoverMenuBar)
return;
if (!visible) {
[[self window] orderOut:nil];
return;
}
if ([NSApp isHidden]) // will need to show later
return;
[self restoreMenuBarCover];
}
- (void)applicationDidUnhide:(NSNotification *)notification;
{
if (!shouldCoverMenuBar)
return;
[self restoreMenuBarCover];
}
- (void)accessibilityAccessDidChange:(NSNotification *)notification;
{
// Unfortunately AXIsProcessTrusted() takes some time to return the new value.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
BOOL hasAccessibilityAccess = AXIsProcessTrusted();
if (hasAccessibilityAccess && !hadAccessibilityAccess) {
[self createMenuBarPeekTap];
}
hadAccessibilityAccess = hasAccessibilityAccess;
});
}
@end
@implementation ShroudMenuBarVisibilityController (NSKeyValueObserving)
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
{
if (![keyPath isEqualToString:[@"values." stringByAppendingString:ShroudPeekAtMenuBarModifierFlagsPreferenceKey]])
return;
peekFlags = [[[NSUserDefaults standardUserDefaults] objectForKey:ShroudPeekAtMenuBarModifierFlagsPreferenceKey] unsignedIntegerValue];
if (peekFlags == 0) {
[self removeMenuBarPeekTap];
[self peekAtMenuBar:NO];
} else {
[self createMenuBarPeekTap];
}
}
@end