diff options
| author | Tomáš Znamenáček | 2014-08-07 12:12:06 +0200 |
|---|---|---|
| committer | Tomáš Znamenáček | 2015-01-07 15:42:22 +0100 |
| commit | 118abba104a1eeabc5c449ac86e25a6d87f1954c (patch) | |
| tree | d95879766a8a5decff97f7240edd09072dbef10a | |
| parent | bbf2f69da4253b9fc28bd622a3041622f064d22c (diff) | |
| download | MASShortcut-118abba104a1eeabc5c449ac86e25a6d87f1954c.tar.bz2 | |
Added a shared binder instance singleton.
This adds a really simple API to set up some bindings without having
to keep a binder instance around by hand. If somebody wants to, it’s
not a problem to allocate a separate instance and have precise control
over its lifetime.
| -rw-r--r-- | Demo/AppDelegate.m | 17 | ||||
| -rw-r--r-- | Framework/MASShortcutBinder.h | 2 | ||||
| -rw-r--r-- | Framework/MASShortcutBinder.m | 10 |
3 files changed, 17 insertions, 12 deletions
diff --git a/Demo/AppDelegate.m b/Demo/AppDelegate.m index 81b2661..d55d43c 100644 --- a/Demo/AppDelegate.m +++ b/Demo/AppDelegate.m @@ -4,20 +4,13 @@ NSString *const MASPreferenceKeyShortcut = @"MASDemoShortcut"; NSString *const MASPreferenceKeyShortcutEnabled = @"MASDemoShortcutEnabled"; NSString *const MASPreferenceKeyConstantShortcutEnabled = @"MASDemoConstantShortcutEnabled"; -@implementation AppDelegate { - MASShortcutMonitor *_shortcutMonitor; - MASShortcutBinder *_shortcutBinder; -} +@implementation AppDelegate #pragma mark - - (void)awakeFromNib { [super awakeFromNib]; - - _shortcutBinder = [[MASShortcutBinder alloc] init]; - _shortcutMonitor = [MASShortcutMonitor sharedMonitor]; - // Checkbox will enable and disable the shortcut view [self.shortcutView bind:@"enabled" toObject:self withKeyPath:@"shortcutEnabled" options:nil]; } @@ -62,14 +55,14 @@ NSString *const MASPreferenceKeyConstantShortcutEnabled = @"MASDemoConstantShort - (void)resetShortcutRegistration { if (self.shortcutEnabled) { - [_shortcutBinder bindShortcutWithDefaultsKey:MASPreferenceKeyShortcut toAction:^{ + [[MASShortcutBinder sharedBinder] bindShortcutWithDefaultsKey:MASPreferenceKeyShortcut toAction:^{ [[NSAlert alertWithMessageText:NSLocalizedString(@"Global hotkey has been pressed.", @"Alert message for custom shortcut") defaultButton:NSLocalizedString(@"OK", @"Default button for the alert on custom shortcut") alternateButton:nil otherButton:nil informativeTextWithFormat:@""] runModal]; }]; } else { - [_shortcutBinder breakBindingWithDefaultsKey:MASPreferenceKeyShortcut]; + [[MASShortcutBinder sharedBinder] breakBindingWithDefaultsKey:MASPreferenceKeyShortcut]; } } @@ -92,14 +85,14 @@ NSString *const MASPreferenceKeyConstantShortcutEnabled = @"MASDemoConstantShort { MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:kVK_F2 modifierFlags:NSCommandKeyMask]; if (self.constantShortcutEnabled) { - [_shortcutMonitor registerShortcut:shortcut withAction:^{ + [[MASShortcutMonitor sharedMonitor] registerShortcut:shortcut withAction:^{ [[NSAlert alertWithMessageText:NSLocalizedString(@"⌘F2 has been pressed.", @"Alert message for constant shortcut") defaultButton:NSLocalizedString(@"OK", @"Default button for the alert on constant shortcut") alternateButton:nil otherButton:nil informativeTextWithFormat:@""] runModal]; }]; } else { - [_shortcutMonitor unregisterShortcut:shortcut]; + [[MASShortcutMonitor sharedMonitor] unregisterShortcut:shortcut]; } } diff --git a/Framework/MASShortcutBinder.h b/Framework/MASShortcutBinder.h index c38dbd4..98e01c7 100644 --- a/Framework/MASShortcutBinder.h +++ b/Framework/MASShortcutBinder.h @@ -2,6 +2,8 @@ @interface MASShortcutBinder : NSObject ++ (instancetype) sharedBinder; + @property(strong) MASShortcutMonitor *shortcutMonitor; @property(copy) NSDictionary *bindingOptions; diff --git a/Framework/MASShortcutBinder.m b/Framework/MASShortcutBinder.m index 1039f64..bf85c41 100644 --- a/Framework/MASShortcutBinder.m +++ b/Framework/MASShortcutBinder.m @@ -27,6 +27,16 @@ } } ++ (instancetype) sharedBinder +{ + static dispatch_once_t once; + static MASShortcutBinder *sharedInstance; + dispatch_once(&once, ^{ + sharedInstance = [[self alloc] init]; + }); + return sharedInstance; +} + #pragma mark Registration - (void) bindShortcutWithDefaultsKey: (NSString*) defaultsKeyName toAction: (dispatch_block_t) action |
