diff options
| author | Tomáš Znamenáček | 2014-08-07 10:10:08 +0200 | 
|---|---|---|
| committer | Tomáš Znamenáček | 2015-01-07 15:42:21 +0100 | 
| commit | be9358bf32dc402f3bc77c6cc20957047ab363af (patch) | |
| tree | 48edbf8bed4603fbb4ba5d4b6857dc7e4c6d2c0c /Framework | |
| parent | f9f48f5ca83f7723eabdd16d0c95195092a8a514 (diff) | |
| download | MASShortcut-be9358bf32dc402f3bc77c6cc20957047ab363af.tar.bz2 | |
Turned MASShortcutMonitor into a singleton.
There can only be one Carbon event handler, so it doesn’t make sense
to create multiple instances of the shortcut monitor.
Diffstat (limited to 'Framework')
| -rw-r--r-- | Framework/MASShortcutBinder.m | 1 | ||||
| -rw-r--r-- | Framework/MASShortcutBinderTests.m | 6 | ||||
| -rw-r--r-- | Framework/MASShortcutMonitor.h | 5 | ||||
| -rw-r--r-- | Framework/MASShortcutMonitor.m | 17 | 
4 files changed, 25 insertions, 4 deletions
| diff --git a/Framework/MASShortcutBinder.m b/Framework/MASShortcutBinder.m index d6249a8..0de99db 100644 --- a/Framework/MASShortcutBinder.m +++ b/Framework/MASShortcutBinder.m @@ -15,6 +15,7 @@      self = [super init];      [self setActions:[NSMutableDictionary dictionary]];      [self setShortcuts:[NSMutableDictionary dictionary]]; +    [self setShortcutMonitor:[MASShortcutMonitor sharedMonitor]];      return self;  } diff --git a/Framework/MASShortcutBinderTests.m b/Framework/MASShortcutBinderTests.m index f416304..9259e9d 100644 --- a/Framework/MASShortcutBinderTests.m +++ b/Framework/MASShortcutBinderTests.m @@ -13,17 +13,15 @@ static NSString *const SampleDefaultsKey = @"sampleShortcut";  - (void) setUp  {      [super setUp]; -      [self setBinder:[[MASShortcutBinder alloc] init]]; -    [self setMonitor:[[MASShortcutMonitor alloc] init]]; -    [_binder setShortcutMonitor:_monitor]; - +    [self setMonitor:[_binder shortcutMonitor]];      [self setDefaults:[[NSUserDefaults alloc] init]];      [_defaults removeObjectForKey:SampleDefaultsKey];  }  - (void) tearDown  { +    [_monitor unregisterAllShortcuts];      [self setMonitor:nil];      [self setDefaults:nil];      [self setBinder:nil]; diff --git a/Framework/MASShortcutMonitor.h b/Framework/MASShortcutMonitor.h index c29747c..5c0daa2 100644 --- a/Framework/MASShortcutMonitor.h +++ b/Framework/MASShortcutMonitor.h @@ -2,8 +2,13 @@  @interface MASShortcutMonitor : NSObject +- (instancetype) init __unavailable; ++ (instancetype) sharedMonitor; +  - (void) registerShortcut: (MASShortcut*) shortcut withAction: (dispatch_block_t) action;  - (BOOL) isShortcutRegistered: (MASShortcut*) shortcut; +  - (void) unregisterShortcut: (MASShortcut*) shortcut; +- (void) unregisterAllShortcuts;  @end diff --git a/Framework/MASShortcutMonitor.m b/Framework/MASShortcutMonitor.m index f1c7e9c..d175b82 100644 --- a/Framework/MASShortcutMonitor.m +++ b/Framework/MASShortcutMonitor.m @@ -10,6 +10,8 @@ static OSStatus MASCarbonEventCallback(EventHandlerCallRef, EventRef, void*);  @implementation MASShortcutMonitor +#pragma mark Initialization +  - (instancetype) init  {      self = [super init]; @@ -31,6 +33,16 @@ static OSStatus MASCarbonEventCallback(EventHandlerCallRef, EventRef, void*);      }  } ++ (instancetype) sharedMonitor +{ +    static dispatch_once_t once; +    static MASShortcutMonitor *sharedInstance; +    dispatch_once(&once, ^{ +        sharedInstance = [[self alloc] init]; +    }); +    return sharedInstance; +} +  #pragma mark Registration  - (void) registerShortcut: (MASShortcut*) shortcut withAction: (dispatch_block_t) action @@ -45,6 +57,11 @@ static OSStatus MASCarbonEventCallback(EventHandlerCallRef, EventRef, void*);      [_hotKeys removeObjectForKey:shortcut];  } +- (void) unregisterAllShortcuts +{ +    [_hotKeys removeAllObjects]; +} +  - (BOOL) isShortcutRegistered: (MASShortcut*) shortcut  {      return !![_hotKeys objectForKey:shortcut]; | 
