aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomáš Znamenáček2014-08-07 10:10:08 +0200
committerTomáš Znamenáček2015-01-07 15:42:21 +0100
commitbe9358bf32dc402f3bc77c6cc20957047ab363af (patch)
tree48edbf8bed4603fbb4ba5d4b6857dc7e4c6d2c0c
parentf9f48f5ca83f7723eabdd16d0c95195092a8a514 (diff)
downloadMASShortcut-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.
-rw-r--r--Demo/AppDelegate.m3
-rw-r--r--Framework/MASShortcutBinder.m1
-rw-r--r--Framework/MASShortcutBinderTests.m6
-rw-r--r--Framework/MASShortcutMonitor.h5
-rw-r--r--Framework/MASShortcutMonitor.m17
5 files changed, 26 insertions, 6 deletions
diff --git a/Demo/AppDelegate.m b/Demo/AppDelegate.m
index d2c07ba..81b2661 100644
--- a/Demo/AppDelegate.m
+++ b/Demo/AppDelegate.m
@@ -16,8 +16,7 @@ NSString *const MASPreferenceKeyConstantShortcutEnabled = @"MASDemoConstantShort
[super awakeFromNib];
_shortcutBinder = [[MASShortcutBinder alloc] init];
- _shortcutMonitor = [[MASShortcutMonitor alloc] init];
- [_shortcutBinder setShortcutMonitor:_shortcutMonitor];
+ _shortcutMonitor = [MASShortcutMonitor sharedMonitor];
// Checkbox will enable and disable the shortcut view
[self.shortcutView bind:@"enabled" toObject:self withKeyPath:@"shortcutEnabled" options:nil];
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];