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,12 @@
#import "MASShortcut.h"
extern FourCharCode const MASHotKeySignature;
@interface MASHotKey : NSObject
@property(readonly) UInt32 carbonID;
@property(copy) dispatch_block_t action;
+ (instancetype) registeredHotKeyWithShortcut: (MASShortcut*) shortcut;
@end

View File

@ -0,0 +1,44 @@
#import "MASHotKey.h"
FourCharCode const MASHotKeySignature = 'MASS';
@interface MASHotKey ()
@property(assign) EventHotKeyRef hotKeyRef;
@property(assign) UInt32 carbonID;
@end
@implementation MASHotKey
- (instancetype) initWithShortcut: (MASShortcut*) shortcut
{
self = [super init];
static UInt32 CarbonHotKeyID = 0;
_carbonID = ++CarbonHotKeyID;
EventHotKeyID hotKeyID = { .signature = MASHotKeySignature, .id = _carbonID };
OSStatus status = RegisterEventHotKey([shortcut carbonKeyCode], [shortcut carbonFlags],
hotKeyID, GetEventDispatcherTarget(), 0, &_hotKeyRef);
if (status != noErr) {
return nil;
}
return self;
}
+ (instancetype) registeredHotKeyWithShortcut: (MASShortcut*) shortcut
{
return [[self alloc] initWithShortcut:shortcut];
}
- (void) dealloc
{
if (_hotKeyRef) {
UnregisterEventHotKey(_hotKeyRef);
_hotKeyRef = NULL;
}
}
@end

View File

@ -0,0 +1,27 @@
#import "MASShortcut.h"
/**
Executes action when a shortcut is pressed.
There can only be one instance of this class, otherwise things
will probably not work. (Theres a Carbon event handler inside
and there can only be one Carbon event handler of a given type.)
*/
@interface MASShortcutMonitor : NSObject
- (instancetype) init __unavailable;
+ (instancetype) sharedMonitor;
/**
Register a shortcut along with an action.
Attempting to insert an already registered shortcut probably wont work.
It may burn your house or cut your fingers. You have been warned.
*/
- (BOOL) registerShortcut: (MASShortcut*) shortcut withAction: (dispatch_block_t) action;
- (BOOL) isShortcutRegistered: (MASShortcut*) shortcut;
- (void) unregisterShortcut: (MASShortcut*) shortcut;
- (void) unregisterAllShortcuts;
@end

View File

@ -0,0 +1,108 @@
#import "MASShortcutMonitor.h"
#import "MASHotKey.h"
@interface MASShortcutMonitor ()
@property(assign) EventHandlerRef eventHandlerRef;
@property(strong) NSMutableDictionary *hotKeys;
@end
static OSStatus MASCarbonEventCallback(EventHandlerCallRef, EventRef, void*);
@implementation MASShortcutMonitor
#pragma mark Initialization
- (instancetype) init
{
self = [super init];
[self setHotKeys:[NSMutableDictionary dictionary]];
EventTypeSpec hotKeyPressedSpec = { .eventClass = kEventClassKeyboard, .eventKind = kEventHotKeyPressed };
OSStatus status = InstallEventHandler(GetEventDispatcherTarget(), MASCarbonEventCallback,
1, &hotKeyPressedSpec, (__bridge void*)self, &_eventHandlerRef);
if (status != noErr) {
return nil;
}
return self;
}
- (void) dealloc
{
if (_eventHandlerRef) {
RemoveEventHandler(_eventHandlerRef);
_eventHandlerRef = NULL;
}
}
+ (instancetype) sharedMonitor
{
static dispatch_once_t once;
static MASShortcutMonitor *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
#pragma mark Registration
- (BOOL) registerShortcut: (MASShortcut*) shortcut withAction: (dispatch_block_t) action
{
MASHotKey *hotKey = [MASHotKey registeredHotKeyWithShortcut:shortcut];
if (hotKey) {
[hotKey setAction:action];
[_hotKeys setObject:hotKey forKey:shortcut];
return YES;
} else {
return NO;
}
}
- (void) unregisterShortcut: (MASShortcut*) shortcut
{
if (shortcut) {
[_hotKeys removeObjectForKey:shortcut];
}
}
- (void) unregisterAllShortcuts
{
[_hotKeys removeAllObjects];
}
- (BOOL) isShortcutRegistered: (MASShortcut*) shortcut
{
return !![_hotKeys objectForKey:shortcut];
}
#pragma mark Event Handling
- (void) handleEvent: (EventRef) event
{
if (GetEventClass(event) != kEventClassKeyboard) {
return;
}
EventHotKeyID hotKeyID;
OSStatus status = GetEventParameter(event, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyID), NULL, &hotKeyID);
if (status != noErr || hotKeyID.signature != MASHotKeySignature) {
return;
}
[_hotKeys enumerateKeysAndObjectsUsingBlock:^(MASShortcut *shortcut, MASHotKey *hotKey, BOOL *stop) {
if (hotKeyID.id == [hotKey carbonID]) {
if ([hotKey action]) {
dispatch_async(dispatch_get_main_queue(), [hotKey action]);
}
*stop = YES;
}
}];
}
@end
static OSStatus MASCarbonEventCallback(EventHandlerCallRef _, EventRef event, void *context)
{
MASShortcutMonitor *dispatcher = (__bridge id)context;
[dispatcher handleEvent:event];
return noErr;
}