forked from Kentzo/ShortcutRecorder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSRValidator.m
268 lines (218 loc) · 11.9 KB
/
SRValidator.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// SRValidator.h
// ShortcutRecorder
//
// Copyright 2006-2012 Contributors. All rights reserved.
//
// License: BSD
//
// Contributors:
// David Dauer
// Jesper
// Jamie Kirkpatrick
// Andy Kim
// Silvio Rizzi
// Ilya Kulakov
#import "SRValidator.h"
#import "SRCommon.h"
#import "SRKeyCodeTransformer.h"
@implementation SRValidator
- (instancetype)initWithDelegate:(NSObject<SRValidatorDelegate> *)aDelegate;
{
self = [super init];
if (self)
{
_delegate = aDelegate;
}
return self;
}
- (instancetype)init
{
return [self initWithDelegate:nil];
}
#pragma mark Methods
- (BOOL)isKeyCode:(unsigned short)aKeyCode andFlagsTaken:(NSUInteger)aFlags error:(NSError **)outError;
{
if ([self isKeyCode:aKeyCode andFlagTakenInDelegate:aFlags error:outError])
return YES;
if ((![self.delegate respondsToSelector:@selector(shortcutValidatorShouldCheckSystemShortcuts:)] ||
[self.delegate shortcutValidatorShouldCheckSystemShortcuts:self]) &&
[self isKeyCode:aKeyCode andFlagsTakenInSystemShortcuts:aFlags error:outError])
{
return YES;
}
if ((![self.delegate respondsToSelector:@selector(shortcutValidatorShouldCheckMenu:)] ||
[self.delegate shortcutValidatorShouldCheckMenu:self]) &&
[self isKeyCode:aKeyCode andFlags:aFlags takenInMenu:[NSApp mainMenu] error:outError])
{
return YES;
}
return NO;
}
- (BOOL)isKeyCode:(unsigned short)aKeyCode andFlagTakenInDelegate:(NSUInteger)aFlags error:(NSError **)outError
{
if (self.delegate)
{
NSString *delegateReason = nil;
if ([self.delegate respondsToSelector:@selector(shortcutValidator:isKeyCode:andFlagsTaken:reason:)] &&
[self.delegate shortcutValidator:self
isKeyCode:aKeyCode
andFlagsTaken:aFlags
reason:&delegateReason])
{
if (outError)
{
BOOL isASCIIOnly = YES;
if ([self.delegate respondsToSelector:@selector(shortcutValidatorShouldUseASCIIStringForKeyCodes:)])
isASCIIOnly = [self.delegate shortcutValidatorShouldUseASCIIStringForKeyCodes:self];
NSString *shortcut = isASCIIOnly ? SRReadableASCIIStringForCocoaModifierFlagsAndKeyCode(aFlags, aKeyCode) : SRReadableStringForCocoaModifierFlagsAndKeyCode(aFlags, aKeyCode);
NSString *failureReason = [NSString stringWithFormat:
NSLocalizedStringFromTableInBundle(@"The key combination \"%@\" can't be used!",
@"ShortcutRecorder",
[NSBundle bundleWithIdentifier:SRBundleIdentifier],
nil),
shortcut];
NSString *description = [NSString stringWithFormat:
NSLocalizedStringFromTableInBundle(@"The key combination \"%@\" can't be used because %@.",
@"ShortcutRecorder",
[NSBundle bundleWithIdentifier:SRBundleIdentifier],
nil),
shortcut,
[delegateReason length] ? delegateReason : @"it's already used"];
NSDictionary *userInfo = @{
NSLocalizedFailureReasonErrorKey : failureReason,
NSLocalizedDescriptionKey: description
};
*outError = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:userInfo];
}
return YES;
}
}
return NO;
}
- (BOOL)isKeyCode:(unsigned short)aKeyCode andFlagsTakenInSystemShortcuts:(NSUInteger)aFlags error:(NSError **)outError
{
CFArrayRef s = NULL;
OSStatus err = CopySymbolicHotKeys(&s);
if (err != noErr)
return YES;
NSArray *symbolicHotKeys = (NSArray *)CFBridgingRelease(s);
aFlags &= SRCocoaModifierFlagsMask;
for (NSDictionary *symbolicHotKey in symbolicHotKeys)
{
if ((__bridge CFBooleanRef)symbolicHotKey[(__bridge NSString *)kHISymbolicHotKeyEnabled] != kCFBooleanTrue)
continue;
unsigned short symbolicHotKeyCode = [symbolicHotKey[(__bridge NSString *)kHISymbolicHotKeyCode] integerValue];
if (symbolicHotKeyCode == aKeyCode)
{
UInt32 symbolicHotKeyFlags = [symbolicHotKey[(__bridge NSString *)kHISymbolicHotKeyModifiers] unsignedIntValue];
symbolicHotKeyFlags &= SRCarbonModifierFlagsMask;
if (SRCarbonToCocoaFlags(symbolicHotKeyFlags) == aFlags)
{
if (outError)
{
BOOL isASCIIOnly = YES;
if ([self.delegate respondsToSelector:@selector(shortcutValidatorShouldUseASCIIStringForKeyCodes:)])
isASCIIOnly = [self.delegate shortcutValidatorShouldUseASCIIStringForKeyCodes:self];
NSString *shortcut = isASCIIOnly ? SRReadableASCIIStringForCocoaModifierFlagsAndKeyCode(aFlags, aKeyCode) : SRReadableStringForCocoaModifierFlagsAndKeyCode(aFlags, aKeyCode);
NSString *failureReason = [NSString stringWithFormat:
NSLocalizedStringFromTableInBundle(@"The key combination \"%@\" can't be used!",
@"ShortcutRecorder",
[NSBundle bundleWithIdentifier:SRBundleIdentifier],
nil),
shortcut];
NSString *description = [NSString stringWithFormat:
NSLocalizedStringFromTableInBundle(@"The key combination \"%@\" can't be used because it's already used by a system-wide keyboard shortcut. If you really want to use this key combination, most shortcuts can be changed in the Keyboard panel in System Preferences.",
@"ShortcutRecorder",
[NSBundle bundleWithIdentifier:SRBundleIdentifier],
nil),
shortcut];
NSDictionary *userInfo = @{
NSLocalizedFailureReasonErrorKey: failureReason,
NSLocalizedDescriptionKey: description
};
*outError = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:userInfo];
}
return YES;
}
}
}
return NO;
}
- (BOOL)isKeyCode:(unsigned short)aKeyCode andFlags:(NSUInteger)aFlags takenInMenu:(NSMenu *)aMenu error:(NSError **)outError
{
aFlags &= SRCocoaModifierFlagsMask;
NSString *keyCodeASCIIRepresentation = [[SRKeyCodeTransformer sharedASCIITransformer] transformedValue:@(aKeyCode)];
NSString *keyCodeCurrentLayoutRepresentation = [[SRKeyCodeTransformer sharedTransformer] transformedValue:@(aKeyCode)];
// Shift flag may be set implicitly in key equivalent.
NSUInteger flagsWithoutShift = aFlags & ~NSEventModifierFlagShift;
NSString *keyCodeASCIIShiftedRepresentation = [[SRKeyCodeTransformer sharedASCIITransformer] transformedValue:@(aKeyCode)
withModifierFlags:@(NSEventModifierFlagShift)];
NSString *keyCodeCurrentLayoutShiftedRepresentation = [[SRKeyCodeTransformer sharedTransformer] transformedValue:@(aKeyCode)
withModifierFlags:@(NSEventModifierFlagShift)];
for (NSMenuItem *menuItem in [aMenu itemArray])
{
if (menuItem.hasSubmenu && [self isKeyCode:aKeyCode andFlags:aFlags takenInMenu:menuItem.submenu error:outError])
return YES;
NSString *keyEquivalent = menuItem.keyEquivalent;
if (![keyEquivalent length])
continue;
NSUInteger keyEquivalentModifierMask = menuItem.keyEquivalentModifierMask;
if ((keyEquivalentModifierMask == aFlags && [keyEquivalent isEqualToString:keyCodeASCIIRepresentation]) ||
(keyEquivalentModifierMask == aFlags && [keyEquivalent isEqualToString:keyCodeCurrentLayoutRepresentation]) ||
(aFlags & NSEventModifierFlagShift && keyEquivalentModifierMask == flagsWithoutShift && [keyEquivalent isEqualToString:keyCodeASCIIShiftedRepresentation]) ||
(aFlags & NSEventModifierFlagShift && keyEquivalentModifierMask == flagsWithoutShift && [keyEquivalent isEqualToString:keyCodeCurrentLayoutShiftedRepresentation]))
{
if (outError)
{
BOOL isASCIIOnly = YES;
if ([self.delegate respondsToSelector:@selector(shortcutValidatorShouldUseASCIIStringForKeyCodes:)])
isASCIIOnly = [self.delegate shortcutValidatorShouldUseASCIIStringForKeyCodes:self];
NSString *shortcut = isASCIIOnly ? SRReadableASCIIStringForCocoaModifierFlagsAndKeyCode(aFlags, aKeyCode) : SRReadableStringForCocoaModifierFlagsAndKeyCode(aFlags, aKeyCode);
NSString *failureReason = [NSString stringWithFormat:
NSLocalizedStringFromTableInBundle(@"The key combination \"%@\" can't be used!",
@"ShortcutRecorder",
[NSBundle bundleWithIdentifier:SRBundleIdentifier],
nil),
shortcut];
NSString *description = [NSString stringWithFormat:
NSLocalizedStringFromTableInBundle(@"The key combination \"%@\" can't be used because it's already used by the menu item \"%@\".",
@"ShortcutRecorder",
[NSBundle bundleWithIdentifier:SRBundleIdentifier],
nil),
shortcut,
menuItem.SR_path];
NSDictionary *userInfo = @{
NSLocalizedFailureReasonErrorKey: failureReason,
NSLocalizedDescriptionKey: description
};
*outError = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:userInfo];
}
return YES;
}
}
return NO;
}
@end
@implementation NSMenuItem (SRValidator)
- (NSString *)SR_path
{
NSMutableArray *items = [NSMutableArray array];
static const NSUInteger Limit = 1000;
NSMenuItem *currentMenuItem = self;
NSUInteger i = 0;
do
{
[items insertObject:currentMenuItem atIndex:0];
currentMenuItem = currentMenuItem.parentItem;
++i;
}
while (currentMenuItem && i < Limit);
NSMutableString *path = [NSMutableString string];
for (NSMenuItem *menuItem in items)
[path appendFormat:@"%@➝", menuItem.title];
if ([path length] > 1)
[path deleteCharactersInRange:NSMakeRange([path length] - 1, 1)];
return path;
}
@end