diff options
| author | Tomáš Znamenáček | 2015-01-08 12:00:53 +0100 |
|---|---|---|
| committer | Tomáš Znamenáček | 2015-01-08 12:00:53 +0100 |
| commit | 9b919cba51e4cd11b0c4424930d6c18a1baec73c (patch) | |
| tree | a8107774609d5f4263f7b79749d93e6c6ff2642d /Demo/AppDelegate.m | |
| parent | a3a459b4e4e47bf18dccd5dc7f315389346e3d6c (diff) | |
| parent | ea69d5939511f61a7082ba1e8ff46d247862a3fa (diff) | |
| download | MASShortcut-9b919cba51e4cd11b0c4424930d6c18a1baec73c.tar.bz2 | |
Merge pull request #53 from zoul/2.0-candidate
Thank you very much!
Diffstat (limited to 'Demo/AppDelegate.m')
| -rw-r--r-- | Demo/AppDelegate.m | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/Demo/AppDelegate.m b/Demo/AppDelegate.m new file mode 100644 index 0000000..7aff3d1 --- /dev/null +++ b/Demo/AppDelegate.m @@ -0,0 +1,90 @@ +#import "AppDelegate.h" + +NSString *const MASPreferenceKeyShortcut = @"MASDemoShortcut"; +NSString *const MASPreferenceKeyShortcutEnabled = @"MASDemoShortcutEnabled"; +NSString *const MASPreferenceKeyConstantShortcutEnabled = @"MASDemoConstantShortcutEnabled"; + +@implementation AppDelegate + +#pragma mark - + +- (void)awakeFromNib +{ + [super awakeFromNib]; + // Checkbox will enable and disable the shortcut view + [self.shortcutView bind:@"enabled" toObject:self withKeyPath:@"shortcutEnabled" options:nil]; +} + +#pragma mark NSApplicationDelegate + +- (void)applicationDidFinishLaunching:(NSNotification *)aNotification +{ + // Shortcut view will follow and modify user preferences automatically + [_shortcutView setAssociatedUserDefaultsKey:MASPreferenceKeyShortcut]; + + // Activate the global keyboard shortcut if it was enabled last time + [self resetShortcutRegistration]; + + // Activate the shortcut Command-F1 if it was enabled + [self resetConstantShortcutRegistration]; +} + +- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender +{ + return YES; +} + +#pragma mark - Custom shortcut + +- (BOOL)isShortcutEnabled +{ + return [[NSUserDefaults standardUserDefaults] boolForKey:MASPreferenceKeyShortcutEnabled]; +} + +- (void)setShortcutEnabled:(BOOL)enabled +{ + if (self.shortcutEnabled != enabled) { + [[NSUserDefaults standardUserDefaults] setBool:enabled forKey:MASPreferenceKeyShortcutEnabled]; + [self resetShortcutRegistration]; + } +} + +- (void)resetShortcutRegistration +{ + if (self.shortcutEnabled) { + [[MASShortcutBinder sharedBinder] bindShortcutWithDefaultsKey:MASPreferenceKeyShortcut toAction:^{ + [[NSSound soundNamed:@"Ping"] play]; + }]; + } else { + [[MASShortcutBinder sharedBinder] breakBindingWithDefaultsKey:MASPreferenceKeyShortcut]; + } +} + +#pragma mark - Constant shortcut + +- (BOOL)isConstantShortcutEnabled +{ + return [[NSUserDefaults standardUserDefaults] boolForKey:MASPreferenceKeyConstantShortcutEnabled]; +} + +- (void)setConstantShortcutEnabled:(BOOL)enabled +{ + if (self.constantShortcutEnabled != enabled) { + [[NSUserDefaults standardUserDefaults] setBool:enabled forKey:MASPreferenceKeyConstantShortcutEnabled]; + [self resetConstantShortcutRegistration]; + } +} + +- (void)resetConstantShortcutRegistration +{ + MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:kVK_ANSI_Keypad2 modifierFlags:NSCommandKeyMask]; + if (self.constantShortcutEnabled) { + [[MASShortcutMonitor sharedMonitor] registerShortcut:shortcut withAction:^{ + [[NSSound soundNamed:@"Ping"] play]; + }]; + } else { + [[MASShortcutMonitor sharedMonitor] unregisterShortcut:shortcut]; + } +} + +@end |
