diff options
| -rw-r--r-- | Demo/AppDelegate.m | 5 | ||||
| -rw-r--r-- | Framework/MASShortcutView+Bindings.h | 25 | ||||
| -rw-r--r-- | Framework/MASShortcutView+Bindings.m | 50 | ||||
| -rw-r--r-- | Framework/Shortcut.h | 1 | ||||
| -rw-r--r-- | MASShortcut.xcodeproj/project.pbxproj | 8 | 
5 files changed, 85 insertions, 4 deletions
| diff --git a/Demo/AppDelegate.m b/Demo/AppDelegate.m index 0c62358..7aff3d1 100644 --- a/Demo/AppDelegate.m +++ b/Demo/AppDelegate.m @@ -20,10 +20,7 @@ NSString *const MASPreferenceKeyConstantShortcutEnabled = @"MASDemoConstantShort  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification  {      // Shortcut view will follow and modify user preferences automatically -    [_shortcutView bind:MASShortcutBinding -        toObject:[NSUserDefaultsController sharedUserDefaultsController] -        withKeyPath:[@"values." stringByAppendingString:MASPreferenceKeyShortcut] -        options:@{NSValueTransformerNameBindingOption:NSKeyedUnarchiveFromDataTransformerName}]; +    [_shortcutView setAssociatedUserDefaultsKey:MASPreferenceKeyShortcut];      // Activate the global keyboard shortcut if it was enabled last time      [self resetShortcutRegistration]; diff --git a/Framework/MASShortcutView+Bindings.h b/Framework/MASShortcutView+Bindings.h new file mode 100644 index 0000000..b0148e7 --- /dev/null +++ b/Framework/MASShortcutView+Bindings.h @@ -0,0 +1,25 @@ +#import "MASShortcutView.h" + +/** +    @brief A simplified interface to bind the recorder value to user defaults. + +    You can bind the @p shortcutValue to user defaults using the standard +    @p bind:toObject:withKeyPath:options: call, but since that’s a lot to type +    and read, here’s a simpler option. + +    Setting the @p associatedUserDefaultsKey binds the view’s shortcut value +    to the given user defaults key. You can supply a value transformer to convert +    values between user defaults and @p MASShortcut. If you don’t supply +    a transformer, the @p NSUnarchiveFromDataTransformerName will be used +    automatically. + +    Set @p associatedUserDefaultsKey to @p nil to disconnect the binding. +*/ +@interface MASShortcutView (Bindings) + +@property(copy) NSString *associatedUserDefaultsKey; + +- (void) setAssociatedUserDefaultsKey: (NSString*) newKey withTransformer: (NSValueTransformer*) transformer; +- (void) setAssociatedUserDefaultsKey: (NSString*) newKey withTransformerName: (NSString*) transformerName; + +@end diff --git a/Framework/MASShortcutView+Bindings.m b/Framework/MASShortcutView+Bindings.m new file mode 100644 index 0000000..7a6064c --- /dev/null +++ b/Framework/MASShortcutView+Bindings.m @@ -0,0 +1,50 @@ +#import "MASShortcutView+Bindings.h" + +@implementation MASShortcutView (Bindings) + +- (NSString*) associatedUserDefaultsKey +{ +	NSDictionary* bindingInfo = [self infoForBinding:MASShortcutBinding]; +    if (bindingInfo != nil) { +        NSString *keyPath = [bindingInfo objectForKey:NSObservedKeyPathKey]; +        NSString *key = [keyPath stringByReplacingOccurrencesOfString:@"values." withString:@""]; +        return key; +    } else { +        return nil; +    } +} + +- (void) setAssociatedUserDefaultsKey: (NSString*) newKey withTransformer: (NSValueTransformer*) transformer +{ +    // Break previous binding if any +    NSString *currentKey = [self associatedUserDefaultsKey]; +    if (currentKey != nil) { +        [self unbind:currentKey]; +    } + +    // Stop if the new binding is nil +    if (newKey == nil) { +        return; +    } + +    NSDictionary *options = transformer ? +        @{NSValueTransformerBindingOption:transformer} : +        nil; + +    [self bind:MASShortcutBinding +        toObject:[NSUserDefaultsController sharedUserDefaultsController] +        withKeyPath:[@"values." stringByAppendingString:newKey] +        options:options]; +} + +- (void) setAssociatedUserDefaultsKey: (NSString*) newKey withTransformerName: (NSString*) transformerName +{ +    [self setAssociatedUserDefaultsKey:newKey withTransformer:[NSValueTransformer valueTransformerForName:transformerName]]; +} + +- (void) setAssociatedUserDefaultsKey: (NSString*) newKey +{ +    [self setAssociatedUserDefaultsKey:newKey withTransformerName:NSUnarchiveFromDataTransformerName]; +} + +@end diff --git a/Framework/Shortcut.h b/Framework/Shortcut.h index df33f17..e131395 100644 --- a/Framework/Shortcut.h +++ b/Framework/Shortcut.h @@ -4,3 +4,4 @@  #import "MASShortcutBinder.h"  #import "MASDictionaryTransformer.h"  #import "MASShortcutView.h" +#import "MASShortcutView+Bindings.h"
\ No newline at end of file diff --git a/MASShortcut.xcodeproj/project.pbxproj b/MASShortcut.xcodeproj/project.pbxproj index ae9fde9..51aeae2 100644 --- a/MASShortcut.xcodeproj/project.pbxproj +++ b/MASShortcut.xcodeproj/project.pbxproj @@ -36,6 +36,8 @@  		0DC2F18D1993708A003A0131 /* MASDictionaryTransformer.h in Headers */ = {isa = PBXBuildFile; fileRef = 0DC2F18B1993708A003A0131 /* MASDictionaryTransformer.h */; };  		0DC2F18E1993708A003A0131 /* MASDictionaryTransformer.m in Sources */ = {isa = PBXBuildFile; fileRef = 0DC2F18C1993708A003A0131 /* MASDictionaryTransformer.m */; };  		0DC2F190199372B4003A0131 /* MASDictionaryTransformerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 0DC2F18F199372B4003A0131 /* MASDictionaryTransformerTests.m */; }; +		0DC2F19819938EFA003A0131 /* MASShortcutView+Bindings.h in Headers */ = {isa = PBXBuildFile; fileRef = 0DC2F19619938EFA003A0131 /* MASShortcutView+Bindings.h */; }; +		0DC2F19919938EFA003A0131 /* MASShortcutView+Bindings.m in Sources */ = {isa = PBXBuildFile; fileRef = 0DC2F19719938EFA003A0131 /* MASShortcutView+Bindings.m */; };  /* End PBXBuildFile section */  /* Begin PBXContainerItemProxy section */ @@ -100,6 +102,8 @@  		0DC2F18B1993708A003A0131 /* MASDictionaryTransformer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MASDictionaryTransformer.h; path = Framework/MASDictionaryTransformer.h; sourceTree = "<group>"; };  		0DC2F18C1993708A003A0131 /* MASDictionaryTransformer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MASDictionaryTransformer.m; path = Framework/MASDictionaryTransformer.m; sourceTree = "<group>"; };  		0DC2F18F199372B4003A0131 /* MASDictionaryTransformerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MASDictionaryTransformerTests.m; path = Framework/MASDictionaryTransformerTests.m; sourceTree = "<group>"; }; +		0DC2F19619938EFA003A0131 /* MASShortcutView+Bindings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "MASShortcutView+Bindings.h"; path = "Framework/MASShortcutView+Bindings.h"; sourceTree = "<group>"; }; +		0DC2F19719938EFA003A0131 /* MASShortcutView+Bindings.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "MASShortcutView+Bindings.m"; path = "Framework/MASShortcutView+Bindings.m"; sourceTree = "<group>"; };  /* End PBXFileReference section */  /* Begin PBXFrameworksBuildPhase section */ @@ -220,6 +224,8 @@  			children = (  				0D827D211990D55E0010B8EF /* MASShortcutView.h */,  				0D827D221990D55E0010B8EF /* MASShortcutView.m */, +				0DC2F19619938EFA003A0131 /* MASShortcutView+Bindings.h */, +				0DC2F19719938EFA003A0131 /* MASShortcutView+Bindings.m */,  			);  			name = UI;  			sourceTree = "<group>"; @@ -262,6 +268,7 @@  				0D827DAD199132840010B8EF /* MASShortcutBinder.h in Headers */,  				0D827D771990F81E0010B8EF /* Shortcut.h in Headers */,  				0DC2F17619922798003A0131 /* MASHotKey.h in Headers */, +				0DC2F19819938EFA003A0131 /* MASShortcutView+Bindings.h in Headers */,  				0D827D9E19911A190010B8EF /* MASShortcutValidator.h in Headers */,  				0DC2F18D1993708A003A0131 /* MASDictionaryTransformer.h in Headers */,  				0D827DA519912D240010B8EF /* MASShortcutMonitor.h in Headers */, @@ -395,6 +402,7 @@  				0D827D2C1990D55E0010B8EF /* MASShortcutView.m in Sources */,  				0D827D261990D55E0010B8EF /* MASShortcut.m in Sources */,  				0DC2F18E1993708A003A0131 /* MASDictionaryTransformer.m in Sources */, +				0DC2F19919938EFA003A0131 /* MASShortcutView+Bindings.m in Sources */,  				0DC2F17D199232F7003A0131 /* MASShortcutBinder.m in Sources */,  			);  			runOnlyForDeploymentPostprocessing = 0; | 
