diff options
| author | Tomáš Znamenáček | 2014-08-06 18:05:43 +0200 |
|---|---|---|
| committer | Tomáš Znamenáček | 2015-01-07 15:39:39 +0100 |
| commit | 444d1bccb9770738fa4ea40383c23f44a55089c2 (patch) | |
| tree | a4f06e82263b751685e51ee3e1479fd9eebb1d16 /Demo | |
| parent | d8efb0755ab99ef60411f77fa4b635f466973d0f (diff) | |
| download | MASShortcut-444d1bccb9770738fa4ea40383c23f44a55089c2.tar.bz2 | |
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.
Diffstat (limited to 'Demo')
| -rw-r--r-- | Demo/AppDelegate.m | 38 | ||||
| -rw-r--r-- | Demo/Prefix.pch | 5 |
2 files changed, 18 insertions, 25 deletions
diff --git a/Demo/AppDelegate.m b/Demo/AppDelegate.m index 54b64e9..d2c07ba 100644 --- a/Demo/AppDelegate.m +++ b/Demo/AppDelegate.m @@ -1,43 +1,37 @@ #import "AppDelegate.h" -#import <MASShortcut/Shortcut.h> NSString *const MASPreferenceKeyShortcut = @"MASDemoShortcut"; NSString *const MASPreferenceKeyShortcutEnabled = @"MASDemoShortcutEnabled"; NSString *const MASPreferenceKeyConstantShortcutEnabled = @"MASDemoConstantShortcutEnabled"; @implementation AppDelegate { - __weak id _constantShortcutMonitor; + MASShortcutMonitor *_shortcutMonitor; + MASShortcutBinder *_shortcutBinder; } -@synthesize window = _window; -@synthesize shortcutView = _shortcutView; - #pragma mark - - (void)awakeFromNib { [super awakeFromNib]; + _shortcutBinder = [[MASShortcutBinder alloc] init]; + _shortcutMonitor = [[MASShortcutMonitor alloc] init]; + [_shortcutBinder setShortcutMonitor:_shortcutMonitor]; + // Checkbox will enable and disable the shortcut view [self.shortcutView bind:@"enabled" toObject:self withKeyPath:@"shortcutEnabled" options:nil]; } -- (void)dealloc -{ - // Cleanup - [self.shortcutView unbind:@"enabled"]; -} - -#pragma mark - NSApplicationDelegate +#pragma mark NSApplicationDelegate - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { - // Uncomment the following lines to make Command-Shift-D the default shortcut -// MASShortcut *defaultShortcut = [MASShortcut shortcutWithKeyCode:0x2 modifierFlags:NSCommandKeyMask|NSShiftKeyMask]; -// [MASShortcut setGlobalShortcut:defaultShortcut forUserDefaultsKey:MASPreferenceKeyShortcut]; - // Shortcut view will follow and modify user preferences automatically - self.shortcutView.associatedUserDefaultsKey = MASPreferenceKeyShortcut; + [_shortcutView bind:MASShortcutBinding + toObject:[NSUserDefaultsController sharedUserDefaultsController] + withKeyPath:[@"values." stringByAppendingString:MASPreferenceKeyShortcut] + options:@{NSValueTransformerNameBindingOption:NSKeyedUnarchiveFromDataTransformerName}]; // Activate the global keyboard shortcut if it was enabled last time [self resetShortcutRegistration]; @@ -69,14 +63,14 @@ NSString *const MASPreferenceKeyConstantShortcutEnabled = @"MASDemoConstantShort - (void)resetShortcutRegistration { if (self.shortcutEnabled) { - [MASShortcut registerGlobalShortcutWithUserDefaultsKey:MASPreferenceKeyShortcut handler:^{ + [_shortcutBinder 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 { - [MASShortcut unregisterGlobalShortcutWithUserDefaultsKey:MASPreferenceKeyShortcut]; + [_shortcutBinder breakBindingWithDefaultsKey:MASPreferenceKeyShortcut]; } } @@ -97,16 +91,16 @@ NSString *const MASPreferenceKeyConstantShortcutEnabled = @"MASDemoConstantShort - (void)resetConstantShortcutRegistration { + MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:kVK_F2 modifierFlags:NSCommandKeyMask]; if (self.constantShortcutEnabled) { - MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:kVK_F2 modifierFlags:NSCommandKeyMask]; - _constantShortcutMonitor = [MASShortcut addGlobalHotkeyMonitorWithShortcut:shortcut handler:^{ + [_shortcutMonitor 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 { - [MASShortcut removeGlobalHotkeyMonitor:_constantShortcutMonitor]; + [_shortcutMonitor unregisterShortcut:shortcut]; } } diff --git a/Demo/Prefix.pch b/Demo/Prefix.pch index aabef47..20d47b6 100644 --- a/Demo/Prefix.pch +++ b/Demo/Prefix.pch @@ -1,3 +1,2 @@ -#ifdef __OBJC__ - #import <Cocoa/Cocoa.h> -#endif +#import <Cocoa/Cocoa.h> +#import <MASShortcut/Shortcut.h> |
