Upgrade pods: Alamofire GCDWebServer MASShortcut

This commit is contained in:
Qiu Yuzhou
2019-09-10 14:23:26 +08:00
parent c593564059
commit 075949cdcb
97 changed files with 3392 additions and 2975 deletions

View File

@ -0,0 +1,44 @@
#import <Carbon/Carbon.h>
#import <AppKit/AppKit.h>
#import "MASKeyMasks.h"
// These glyphs are missed in Carbon.h
typedef NS_ENUM(unsigned short, kMASShortcutGlyph) {
kMASShortcutGlyphEject = 0x23CF,
kMASShortcutGlyphClear = 0x2715,
kMASShortcutGlyphDeleteLeft = 0x232B,
kMASShortcutGlyphDeleteRight = 0x2326,
kMASShortcutGlyphLeftArrow = 0x2190,
kMASShortcutGlyphRightArrow = 0x2192,
kMASShortcutGlyphUpArrow = 0x2191,
kMASShortcutGlyphDownArrow = 0x2193,
kMASShortcutGlyphEscape = 0x238B,
kMASShortcutGlyphHelp = 0x003F,
kMASShortcutGlyphPageDown = 0x21DF,
kMASShortcutGlyphPageUp = 0x21DE,
kMASShortcutGlyphTabRight = 0x21E5,
kMASShortcutGlyphReturn = 0x2305,
kMASShortcutGlyphReturnR2L = 0x21A9,
kMASShortcutGlyphPadClear = 0x2327,
kMASShortcutGlyphNorthwestArrow = 0x2196,
kMASShortcutGlyphSoutheastArrow = 0x2198,
};
NS_INLINE NSString* NSStringFromMASKeyCode(unsigned short ch)
{
return [NSString stringWithFormat:@"%C", ch];
}
NS_INLINE NSUInteger MASPickCocoaModifiers(NSUInteger flags)
{
return (flags & (NSEventModifierFlagControl | NSEventModifierFlagShift | NSEventModifierFlagOption | NSEventModifierFlagCommand));
}
NS_INLINE UInt32 MASCarbonModifiersFromCocoaModifiers(NSUInteger cocoaFlags)
{
return
(cocoaFlags & NSEventModifierFlagCommand ? cmdKey : 0)
| (cocoaFlags & NSEventModifierFlagOption ? optionKey : 0)
| (cocoaFlags & NSEventModifierFlagControl ? controlKey : 0)
| (cocoaFlags & NSEventModifierFlagShift ? shiftKey : 0);
}

View File

@ -0,0 +1,18 @@
#import <Availability.h>
// https://github.com/shpakovski/MASShortcut/issues/99
//
// Long story short: NSControlKeyMask and friends were replaced with NSEventModifierFlagControl
// and similar in macOS Sierra. The project builds fine & clean, but including MASShortcut in
// a project with deployment target set to 10.12 results in several deprecation warnings because
// of the control masks. Simply replacing the old symbols with the new ones isnt an option,
// since it breaks the build on older SDKs in Travis, for example.
//
// It should be safe to remove this whole thing once the 10.12 SDK is ubiquitous.
#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101200
#define NSEventModifierFlagCommand NSCommandKeyMask
#define NSEventModifierFlagControl NSControlKeyMask
#define NSEventModifierFlagOption NSAlternateKeyMask
#define NSEventModifierFlagShift NSShiftKeyMask
#endif

View File

@ -0,0 +1,81 @@
#import "MASKeyCodes.h"
/**
A model class to hold a key combination.
This class just represents a combination of keys. It does not care if
the combination is valid or can be used as a hotkey, it doesnt watch
the input system for the shortcut appearance, nor it does access user
defaults.
*/
@interface MASShortcut : NSObject <NSSecureCoding, NSCopying>
/**
The virtual key code for the keyboard key.
Hardware independent, same as in `NSEvent`. See `Events.h` in the HIToolbox
framework for a complete list, or Command-click this symbol: `kVK_ANSI_A`.
*/
@property (nonatomic, readonly) NSInteger keyCode;
/**
Cocoa keyboard modifier flags.
Same as in `NSEvent`: `NSCommandKeyMask`, `NSAlternateKeyMask`, etc.
*/
@property (nonatomic, readonly) NSEventModifierFlags modifierFlags;
/**
Same as `keyCode`, just a different type.
*/
@property (nonatomic, readonly) UInt32 carbonKeyCode;
/**
Carbon modifier flags.
A bit sum of `cmdKey`, `optionKey`, etc.
*/
@property (nonatomic, readonly) UInt32 carbonFlags;
/**
A string representing the “key” part of a shortcut, like the `5` in `⌘5`.
@warning The value may change depending on the active keyboard layout.
For example for the `^2` keyboard shortcut (`kVK_ANSI_2+NSControlKeyMask`
to be precise) the `keyCodeString` is `2` on the US keyboard, but `ě` when
the Czech keyboard layout is active. See the spec for details.
*/
@property (nonatomic, readonly) NSString *keyCodeString;
/**
A key-code string used in key equivalent matching.
For precise meaning of “key equivalents” see the `keyEquivalent`
property of `NSMenuItem`. Here the string is used to support shortcut
validation (“is the shortcut already taken in this menu?”) and
for display in `NSMenu`.
The value of this property may differ from `keyCodeString`. For example
the Russian keyboard has a `Г` (Ge) Cyrillic character in place of the
latin `U` key. This means you can create a `^Г` shortcut, but in menus
thats always displayed as `^U`. So the `keyCodeString` returns `Г`
and `keyCodeStringForKeyEquivalent` returns `U`.
*/
@property (nonatomic, readonly) NSString *keyCodeStringForKeyEquivalent;
/**
A string representing the shortcut modifiers, like the `⌘` in `⌘5`.
*/
@property (nonatomic, readonly) NSString *modifierFlagsString;
- (instancetype)initWithKeyCode:(NSInteger)code modifierFlags:(NSEventModifierFlags)flags;
+ (instancetype)shortcutWithKeyCode:(NSInteger)code modifierFlags:(NSEventModifierFlags)flags;
/**
Creates a new shortcut from an `NSEvent` object.
This is just a convenience initializer that reads the key code and modifiers from an `NSEvent`.
*/
+ (instancetype)shortcutWithEvent:(NSEvent *)anEvent;
@end

View File

@ -0,0 +1,243 @@
#import "MASShortcut.h"
#import "MASLocalization.h"
static NSString *const MASShortcutKeyCode = @"KeyCode";
static NSString *const MASShortcutModifierFlags = @"ModifierFlags";
@implementation MASShortcut
#pragma mark Initialization
- (instancetype)initWithKeyCode:(NSInteger)code modifierFlags:(NSEventModifierFlags)flags
{
self = [super init];
if (self) {
_keyCode = code;
_modifierFlags = MASPickCocoaModifiers(flags);
}
return self;
}
+ (instancetype)shortcutWithKeyCode:(NSInteger)code modifierFlags:(NSEventModifierFlags)flags
{
return [[self alloc] initWithKeyCode:code modifierFlags:flags];
}
+ (instancetype)shortcutWithEvent:(NSEvent *)event
{
return [[self alloc] initWithKeyCode:event.keyCode modifierFlags:event.modifierFlags];
}
#pragma mark Shortcut Accessors
- (UInt32)carbonKeyCode
{
return (self.keyCode == NSNotFound ? 0 : (UInt32)self.keyCode);
}
- (UInt32)carbonFlags
{
return MASCarbonModifiersFromCocoaModifiers(self.modifierFlags);
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@%@", self.modifierFlagsString, self.keyCodeString];
}
- (NSString *)keyCodeStringForKeyEquivalent
{
NSString *keyCodeString = self.keyCodeString;
if (keyCodeString.length <= 1) {
return keyCodeString.lowercaseString;
}
switch (self.keyCode) {
case kVK_F1: return NSStringFromMASKeyCode(NSF1FunctionKey);
case kVK_F2: return NSStringFromMASKeyCode(NSF2FunctionKey);
case kVK_F3: return NSStringFromMASKeyCode(NSF3FunctionKey);
case kVK_F4: return NSStringFromMASKeyCode(NSF4FunctionKey);
case kVK_F5: return NSStringFromMASKeyCode(NSF5FunctionKey);
case kVK_F6: return NSStringFromMASKeyCode(NSF6FunctionKey);
case kVK_F7: return NSStringFromMASKeyCode(NSF7FunctionKey);
case kVK_F8: return NSStringFromMASKeyCode(NSF8FunctionKey);
case kVK_F9: return NSStringFromMASKeyCode(NSF9FunctionKey);
case kVK_F10: return NSStringFromMASKeyCode(NSF10FunctionKey);
case kVK_F11: return NSStringFromMASKeyCode(NSF11FunctionKey);
case kVK_F12: return NSStringFromMASKeyCode(NSF12FunctionKey);
case kVK_F13: return NSStringFromMASKeyCode(NSF13FunctionKey);
case kVK_F14: return NSStringFromMASKeyCode(NSF14FunctionKey);
case kVK_F15: return NSStringFromMASKeyCode(NSF15FunctionKey);
case kVK_F16: return NSStringFromMASKeyCode(NSF16FunctionKey);
case kVK_F17: return NSStringFromMASKeyCode(NSF17FunctionKey);
case kVK_F18: return NSStringFromMASKeyCode(NSF18FunctionKey);
case kVK_F19: return NSStringFromMASKeyCode(NSF19FunctionKey);
case kVK_Space: return NSStringFromMASKeyCode(0x20);
default: return @"";
}
}
- (NSString *)keyCodeString
{
// Some key codes don't have an equivalent
switch (self.keyCode) {
case NSNotFound: return @"";
case kVK_F1: return @"F1";
case kVK_F2: return @"F2";
case kVK_F3: return @"F3";
case kVK_F4: return @"F4";
case kVK_F5: return @"F5";
case kVK_F6: return @"F6";
case kVK_F7: return @"F7";
case kVK_F8: return @"F8";
case kVK_F9: return @"F9";
case kVK_F10: return @"F10";
case kVK_F11: return @"F11";
case kVK_F12: return @"F12";
case kVK_F13: return @"F13";
case kVK_F14: return @"F14";
case kVK_F15: return @"F15";
case kVK_F16: return @"F16";
case kVK_F17: return @"F17";
case kVK_F18: return @"F18";
case kVK_F19: return @"F19";
case kVK_Space: return MASLocalizedString(@"Space", @"Shortcut glyph name for SPACE key");
case kVK_Escape: return NSStringFromMASKeyCode(kMASShortcutGlyphEscape);
case kVK_Delete: return NSStringFromMASKeyCode(kMASShortcutGlyphDeleteLeft);
case kVK_ForwardDelete: return NSStringFromMASKeyCode(kMASShortcutGlyphDeleteRight);
case kVK_LeftArrow: return NSStringFromMASKeyCode(kMASShortcutGlyphLeftArrow);
case kVK_RightArrow: return NSStringFromMASKeyCode(kMASShortcutGlyphRightArrow);
case kVK_UpArrow: return NSStringFromMASKeyCode(kMASShortcutGlyphUpArrow);
case kVK_DownArrow: return NSStringFromMASKeyCode(kMASShortcutGlyphDownArrow);
case kVK_Help: return NSStringFromMASKeyCode(kMASShortcutGlyphHelp);
case kVK_PageUp: return NSStringFromMASKeyCode(kMASShortcutGlyphPageUp);
case kVK_PageDown: return NSStringFromMASKeyCode(kMASShortcutGlyphPageDown);
case kVK_Tab: return NSStringFromMASKeyCode(kMASShortcutGlyphTabRight);
case kVK_Return: return NSStringFromMASKeyCode(kMASShortcutGlyphReturnR2L);
// Keypad
case kVK_ANSI_Keypad0: return @"0";
case kVK_ANSI_Keypad1: return @"1";
case kVK_ANSI_Keypad2: return @"2";
case kVK_ANSI_Keypad3: return @"3";
case kVK_ANSI_Keypad4: return @"4";
case kVK_ANSI_Keypad5: return @"5";
case kVK_ANSI_Keypad6: return @"6";
case kVK_ANSI_Keypad7: return @"7";
case kVK_ANSI_Keypad8: return @"8";
case kVK_ANSI_Keypad9: return @"9";
case kVK_ANSI_KeypadDecimal: return @".";
case kVK_ANSI_KeypadMultiply: return @"*";
case kVK_ANSI_KeypadPlus: return @"+";
case kVK_ANSI_KeypadClear: return NSStringFromMASKeyCode(kMASShortcutGlyphPadClear);
case kVK_ANSI_KeypadDivide: return @"/";
case kVK_ANSI_KeypadEnter: return NSStringFromMASKeyCode(kMASShortcutGlyphReturn);
case kVK_ANSI_KeypadMinus: return @"-";
case kVK_ANSI_KeypadEquals: return @"=";
// Hardcode
case 119: return NSStringFromMASKeyCode(kMASShortcutGlyphSoutheastArrow);
case 115: return NSStringFromMASKeyCode(kMASShortcutGlyphNorthwestArrow);
}
// Everything else should be printable so look it up in the current ASCII capable keyboard layout
OSStatus error = noErr;
NSString *keystroke = nil;
TISInputSourceRef inputSource = TISCopyCurrentASCIICapableKeyboardLayoutInputSource();
if (inputSource) {
CFDataRef layoutDataRef = TISGetInputSourceProperty(inputSource, kTISPropertyUnicodeKeyLayoutData);
if (layoutDataRef) {
UCKeyboardLayout *layoutData = (UCKeyboardLayout *)CFDataGetBytePtr(layoutDataRef);
UniCharCount length = 0;
UniChar chars[256] = { 0 };
UInt32 deadKeyState = 0;
error = UCKeyTranslate(layoutData, (UInt16)self.keyCode, kUCKeyActionDisplay, 0, // No modifiers
LMGetKbdType(), kUCKeyTranslateNoDeadKeysMask, &deadKeyState,
sizeof(chars) / sizeof(UniChar), &length, chars);
keystroke = ((error == noErr) && length ? [NSString stringWithCharacters:chars length:length] : @"");
}
CFRelease(inputSource);
}
// Validate keystroke
if (keystroke.length) {
static NSMutableCharacterSet *validChars = nil;
if (validChars == nil) {
validChars = [[NSMutableCharacterSet alloc] init];
[validChars formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
[validChars formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
[validChars formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];
}
for (NSUInteger i = 0, length = keystroke.length; i < length; i++) {
if (![validChars characterIsMember:[keystroke characterAtIndex:i]]) {
keystroke = @"";
break;
}
}
}
// Finally, we've got a shortcut!
return keystroke.uppercaseString;
}
- (NSString *)modifierFlagsString
{
unichar chars[4];
NSUInteger count = 0;
// These are in the same order as the menu manager shows them
if (self.modifierFlags & NSControlKeyMask) chars[count++] = kControlUnicode;
if (self.modifierFlags & NSAlternateKeyMask) chars[count++] = kOptionUnicode;
if (self.modifierFlags & NSShiftKeyMask) chars[count++] = kShiftUnicode;
if (self.modifierFlags & NSCommandKeyMask) chars[count++] = kCommandUnicode;
return (count ? [NSString stringWithCharacters:chars length:count] : @"");
}
#pragma mark NSObject
- (BOOL) isEqual: (MASShortcut*) object
{
return [object isKindOfClass:[self class]]
&& (object.keyCode == self.keyCode)
&& (object.modifierFlags == self.modifierFlags);
}
- (NSUInteger) hash
{
return self.keyCode + self.modifierFlags;
}
#pragma mark NSCoding
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeInteger:(self.keyCode != NSNotFound ? self.keyCode : - 1) forKey:MASShortcutKeyCode];
[coder encodeInteger:(NSInteger)self.modifierFlags forKey:MASShortcutModifierFlags];
}
- (instancetype)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if (self) {
NSInteger code = [decoder decodeIntegerForKey:MASShortcutKeyCode];
_keyCode = (code < 0) ? NSNotFound : code;
_modifierFlags = [decoder decodeIntegerForKey:MASShortcutModifierFlags];
}
return self;
}
#pragma mark NSSecureCoding
+ (BOOL)supportsSecureCoding
{
return YES;
}
#pragma mark NSCopying
- (instancetype) copyWithZone:(NSZone *)zone
{
return [[self class] shortcutWithKeyCode:_keyCode modifierFlags:_modifierFlags];
}
@end

View File

@ -0,0 +1,29 @@
#import "MASShortcut.h"
/**
This class is used by the recording control to tell which shortcuts are acceptable.
There are two kinds of shortcuts that are not considered acceptable: shortcuts that
are too simple (like single letter keys) and shortcuts that are already used by the
operating system.
*/
@interface MASShortcutValidator : NSObject
/**
Set to `YES` if you want to accept Option-something shortcuts.
`NO` by default, since Option-something shortcuts are often used by system,
for example Option-G will type the © sign. This also applies to Option-Shift
shortcuts in other words, shortcut recorder will not accept shortcuts like
Option-Shift-K by default. (Again, since Option-Shift-K inserts the Apple
logo sign by default.)
*/
@property(assign) BOOL allowAnyShortcutWithOptionModifier;
+ (instancetype) sharedValidator;
- (BOOL) isShortcutValid: (MASShortcut*) shortcut;
- (BOOL) isShortcut: (MASShortcut*) shortcut alreadyTakenInMenu: (NSMenu*) menu explanation: (NSString**) explanation;
- (BOOL) isShortcutAlreadyTakenBySystem: (MASShortcut*) shortcut explanation: (NSString**) explanation;
@end

View File

@ -0,0 +1,112 @@
#import "MASShortcutValidator.h"
#import "MASLocalization.h"
@implementation MASShortcutValidator
+ (instancetype) sharedValidator
{
static dispatch_once_t once;
static MASShortcutValidator *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (BOOL) isShortcutValid: (MASShortcut*) shortcut
{
NSInteger keyCode = [shortcut keyCode];
NSEventModifierFlags modifiers = [shortcut modifierFlags];
// Allow any function key with any combination of modifiers
BOOL includesFunctionKey = ((keyCode == kVK_F1) || (keyCode == kVK_F2) || (keyCode == kVK_F3) || (keyCode == kVK_F4) ||
(keyCode == kVK_F5) || (keyCode == kVK_F6) || (keyCode == kVK_F7) || (keyCode == kVK_F8) ||
(keyCode == kVK_F9) || (keyCode == kVK_F10) || (keyCode == kVK_F11) || (keyCode == kVK_F12) ||
(keyCode == kVK_F13) || (keyCode == kVK_F14) || (keyCode == kVK_F15) || (keyCode == kVK_F16) ||
(keyCode == kVK_F17) || (keyCode == kVK_F18) || (keyCode == kVK_F19) || (keyCode == kVK_F20));
if (includesFunctionKey) return YES;
// Do not allow any other key without modifiers
BOOL hasModifierFlags = (modifiers > 0);
if (!hasModifierFlags) return NO;
// Allow any hotkey containing Control or Command modifier
BOOL includesCommand = ((modifiers & NSCommandKeyMask) > 0);
BOOL includesControl = ((modifiers & NSControlKeyMask) > 0);
if (includesCommand || includesControl) return YES;
// Allow Option key only in selected cases
BOOL includesOption = ((modifiers & NSAlternateKeyMask) > 0);
if (includesOption) {
// Always allow Option-Space and Option-Escape because they do not have any bind system commands
if ((keyCode == kVK_Space) || (keyCode == kVK_Escape)) return YES;
// Allow Option modifier with any key even if it will break the system binding
if (_allowAnyShortcutWithOptionModifier) return YES;
}
// The hotkey does not have any modifiers or violates system bindings
return NO;
}
- (BOOL) isShortcut: (MASShortcut*) shortcut alreadyTakenInMenu: (NSMenu*) menu explanation: (NSString**) explanation
{
NSString *keyEquivalent = [shortcut keyCodeStringForKeyEquivalent];
NSEventModifierFlags flags = [shortcut modifierFlags];
for (NSMenuItem *menuItem in menu.itemArray) {
if (menuItem.hasSubmenu && [self isShortcut:shortcut alreadyTakenInMenu:[menuItem submenu] explanation:explanation]) return YES;
BOOL equalFlags = (MASPickCocoaModifiers(menuItem.keyEquivalentModifierMask) == flags);
BOOL equalHotkeyLowercase = [menuItem.keyEquivalent.lowercaseString isEqualToString:keyEquivalent];
// Check if the cases are different, we know ours is lower and that shift is included in our modifiers
// If theirs is capitol, we need to add shift to their modifiers
if (equalHotkeyLowercase && ![menuItem.keyEquivalent isEqualToString:keyEquivalent]) {
equalFlags = (MASPickCocoaModifiers(menuItem.keyEquivalentModifierMask | NSShiftKeyMask) == flags);
}
if (equalFlags && equalHotkeyLowercase) {
if (explanation) {
*explanation = MASLocalizedString(@"This shortcut cannot be used because it is already used by the menu item %@.",
@"Message for alert when shortcut is already used");
*explanation = [NSString stringWithFormat:*explanation, menuItem.title];
}
return YES;
}
}
return NO;
}
- (BOOL) isShortcutAlreadyTakenBySystem: (MASShortcut*) shortcut explanation: (NSString**) explanation
{
CFArrayRef globalHotKeys;
if (CopySymbolicHotKeys(&globalHotKeys) == noErr) {
// Enumerate all global hotkeys and check if any of them matches current shortcut
for (CFIndex i = 0, count = CFArrayGetCount(globalHotKeys); i < count; i++) {
CFDictionaryRef hotKeyInfo = CFArrayGetValueAtIndex(globalHotKeys, i);
CFNumberRef code = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyCode);
CFNumberRef flags = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyModifiers);
CFNumberRef enabled = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyEnabled);
if (([(__bridge NSNumber *)code integerValue] == [shortcut keyCode]) &&
([(__bridge NSNumber *)flags unsignedIntegerValue] == [shortcut carbonFlags]) &&
([(__bridge NSNumber *)enabled boolValue])) {
if (explanation) {
*explanation = MASLocalizedString(@"This combination cannot be used because it is already used by a system-wide "
@"keyboard shortcut.\nIf you really want to use this key combination, most shortcuts "
@"can be changed in the Keyboard & Mouse panel in System Preferences.",
@"Message for alert when shortcut is already used by the system");
}
return YES;
}
}
CFRelease(globalHotKeys);
}
return [self isShortcut:shortcut alreadyTakenInMenu:[NSApp mainMenu] explanation:explanation];
}
@end