From 444d1bccb9770738fa4ea40383c23f44a55089c2 Mon Sep 17 00:00:00 2001 From: Tomáš Znamenáček Date: Wed, 6 Aug 2014 18:05:43 +0200 Subject: Refactored the shortcut dispatcher and bindings to user defaults. This is a big change that was hard to split into smaller commits. There’s now a new class to bind shortcuts to actions, a new class to bind user defaults’ keys to actions, and a new way to associate user defaults with the recorder control (MASShortcutView). I have also updated the demo app to go with the changes. The new class to associate shortcuts with actions is called MASShortcutMonitor. It wraps the Carbon hotkey magic and offers a simple interface to add a shortcut along with a block that should be run when the shortcut is pressed. It’s the lowest-level interface. Since the usual requirement is to store the shortcuts into user defaults, there’s also a higher-level interface offered by the MASShortcutBinder class. That takes a defaults key and associates it with a block. When the shortcut stored under the defaults key changes, the binder automatically switches to the new shortcut. The class is a wrapper built atop of the previous one, the MASShortcutMonitor – it simply adds, updates and removes shortcuts as the user defaults change. I have removed the special user defaults integration code from the recorder control (MASShortcutView) and replaced it with a small Cocoa Bindings shim. This means that in order to keep the recorder control in sync with the defaults you just have to call the usual bind:toObject:withKeyPath:options: method, like this: [_shortcutView bind:MASShortcutBinding toObject:[NSUserDefaultsController sharedUserDefaultsController] withKeyPath[@"values.ExampleDefaultsKey" options:@{NSValueTransformerNameBindingOption:NSKeyedUnarchiveFromDataTransformerName}]; That’s more verbose than the previous solution, but it’s much cleaner and can be swept under a convenience call if needed. I might also add a dictionaryValue property later that would make it possible to bind the value to user defaults directly, without a transformer, and would enable backward compatibility with Shortcut Recorder. --- Framework/MASShortcutView+UserDefaults.m | 125 ------------------------------- 1 file changed, 125 deletions(-) delete mode 100644 Framework/MASShortcutView+UserDefaults.m (limited to 'Framework/MASShortcutView+UserDefaults.m') diff --git a/Framework/MASShortcutView+UserDefaults.m b/Framework/MASShortcutView+UserDefaults.m deleted file mode 100644 index 55584f4..0000000 --- a/Framework/MASShortcutView+UserDefaults.m +++ /dev/null @@ -1,125 +0,0 @@ -#import "MASShortcutView+UserDefaults.h" -#import "MASShortcut.h" -#import - -@interface MASShortcutDefaultsObserver : NSObject - -@property (nonatomic, readonly) NSString *userDefaultsKey; -@property (nonatomic, readonly, weak) MASShortcutView *shortcutView; - -- (id)initWithShortcutView:(MASShortcutView *)shortcutView userDefaultsKey:(NSString *)userDefaultsKey; - -@end - -#pragma mark - - -@implementation MASShortcutView (UserDefaults) - -void *MASAssociatedDefaultsObserver = &MASAssociatedDefaultsObserver; - -- (NSString *)associatedUserDefaultsKey -{ - MASShortcutDefaultsObserver *defaultsObserver = objc_getAssociatedObject(self, MASAssociatedDefaultsObserver); - return defaultsObserver.userDefaultsKey; -} - -- (void)setAssociatedUserDefaultsKey:(NSString *)associatedUserDefaultsKey -{ - // First, stop observing previous shortcut view - objc_setAssociatedObject(self, MASAssociatedDefaultsObserver, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); - - if (associatedUserDefaultsKey.length == 0) return; - - // Next, start observing current shortcut view - MASShortcutDefaultsObserver *defaultsObserver = [[MASShortcutDefaultsObserver alloc] initWithShortcutView:self userDefaultsKey:associatedUserDefaultsKey]; - objc_setAssociatedObject(self, MASAssociatedDefaultsObserver, defaultsObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC); -} - -@end - -#pragma mark - - -@implementation MASShortcutDefaultsObserver { - MASShortcut *_originalShortcut; - BOOL _internalPreferenceChange; - BOOL _internalShortcutChange; -} - -- (id)initWithShortcutView:(MASShortcutView *)shortcutView userDefaultsKey:(NSString *)userDefaultsKey -{ - self = [super init]; - if (self) { - _originalShortcut = shortcutView.shortcutValue; - _shortcutView = shortcutView; - _userDefaultsKey = userDefaultsKey.copy; - [self startObservingShortcutView]; - } - return self; -} - -- (void)dealloc -{ - // __weak _shortcutView is not yet deallocated because it refers MASShortcutDefaultsObserver - [self stopObservingShortcutView]; -} - -#pragma mark - - -void *kShortcutValueObserver = &kShortcutValueObserver; - -- (void)startObservingShortcutView -{ - // Read initial shortcut value from user preferences - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - NSData *data = [defaults dataForKey:_userDefaultsKey]; - _shortcutView.shortcutValue = [NSKeyedUnarchiver unarchiveObjectWithData:data]; - - // Observe user preferences to update shortcut value when it changed - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultsDidChange:) name:NSUserDefaultsDidChangeNotification object:defaults]; - - // Observe the keyboard shortcut that user inputs by hand - [_shortcutView addObserver:self forKeyPath:@"shortcutValue" options:0 context:kShortcutValueObserver]; -} - -- (void)userDefaultsDidChange:(NSNotification *)note -{ - // Ignore notifications posted from -[self observeValueForKeyPath:] - if (_internalPreferenceChange) return; - - _internalShortcutChange = YES; - NSData *data = [note.object dataForKey:_userDefaultsKey]; - _shortcutView.shortcutValue = [NSKeyedUnarchiver unarchiveObjectWithData:data]; - _internalShortcutChange = NO; -} - -- (void)stopObservingShortcutView -{ - // Stop observing keyboard hotkeys entered by user in the shortcut view - [_shortcutView removeObserver:self forKeyPath:@"shortcutValue" context:kShortcutValueObserver]; - - // Stop observing user preferences - [[NSNotificationCenter defaultCenter] removeObserver:self name:NSUserDefaultsDidChangeNotification object:[NSUserDefaults standardUserDefaults]]; - - // Restore original hotkey in the shortcut view - _shortcutView.shortcutValue = _originalShortcut; -} - -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context -{ - if (context == kShortcutValueObserver) { - if (_internalShortcutChange) return; - MASShortcut *shortcut = [object valueForKey:keyPath]; - _internalPreferenceChange = YES; - - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - [defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:shortcut] forKey:_userDefaultsKey]; - [defaults synchronize]; - - _internalPreferenceChange = NO; - } - else { - [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; - } -} - -@end -- cgit v1.2.3