From fc43b570c7ea4802c648670b39586425ce330cec Mon Sep 17 00:00:00 2001 From: Tomáš Znamenáček Date: Thu, 8 Jan 2015 14:16:26 +0100 Subject: Simplified and updated the demo project. --- Demo/AppDelegate.h | 5 --- Demo/AppDelegate.m | 92 +++++++++++------------------------------------------- Demo/Info.plist | 6 +--- Demo/MainMenu.xib | 53 ++++++++++--------------------- 4 files changed, 36 insertions(+), 120 deletions(-) (limited to 'Demo') diff --git a/Demo/AppDelegate.h b/Demo/AppDelegate.h index 5053cfb..1c490d3 100644 --- a/Demo/AppDelegate.h +++ b/Demo/AppDelegate.h @@ -1,10 +1,5 @@ -@class MASShortcutView; - @interface AppDelegate : NSObject @property (nonatomic, assign) IBOutlet NSWindow *window; -@property (nonatomic, weak) IBOutlet MASShortcutView *shortcutView; -@property (nonatomic, getter = isShortcutEnabled) BOOL shortcutEnabled; -@property (nonatomic, getter = isConstantShortcutEnabled) BOOL constantShortcutEnabled; @end diff --git a/Demo/AppDelegate.m b/Demo/AppDelegate.m index 7aff3d1..5c1c3f7 100644 --- a/Demo/AppDelegate.m +++ b/Demo/AppDelegate.m @@ -1,90 +1,36 @@ #import "AppDelegate.h" -NSString *const MASPreferenceKeyShortcut = @"MASDemoShortcut"; -NSString *const MASPreferenceKeyShortcutEnabled = @"MASDemoShortcutEnabled"; -NSString *const MASPreferenceKeyConstantShortcutEnabled = @"MASDemoConstantShortcutEnabled"; +@interface AppDelegate () +@property (nonatomic, assign) IBOutlet MASShortcutView *shortcutView; +@end @implementation AppDelegate -#pragma mark - - -- (void)awakeFromNib +- (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 + static NSString *const ShortcutKey = @"customShortcut"; -- (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 + // Bind the shortcut recorder view’s value to user defaults. + // Run “defaults read com.shpakovski.mac.Demo” to see what’s stored + // in user defaults. + [_shortcutView setAssociatedUserDefaultsKey:ShortcutKey]; -- (BOOL)isConstantShortcutEnabled -{ - return [[NSUserDefaults standardUserDefaults] boolForKey:MASPreferenceKeyConstantShortcutEnabled]; + // Play a ping sound when the shortcut stored in user defaults is pressed. + // Note that when the shortcut stored in user defaults changes, you don’t have + // to update anything: the old shortcut will automatically stop working and + // the sound will play after pressing the new one. + [[MASShortcutBinder sharedBinder] bindShortcutWithDefaultsKey:ShortcutKey toAction:^{ + [[NSSound soundNamed:@"Ping"] play]; + }]; } -- (void)setConstantShortcutEnabled:(BOOL)enabled -{ - if (self.constantShortcutEnabled != enabled) { - [[NSUserDefaults standardUserDefaults] setBool:enabled forKey:MASPreferenceKeyConstantShortcutEnabled]; - [self resetConstantShortcutRegistration]; - } -} +#pragma mark NSApplicationDelegate -- (void)resetConstantShortcutRegistration +- (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*) sender { - 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]; - } + return YES; } @end diff --git a/Demo/Info.plist b/Demo/Info.plist index 083b650..68638de 100644 --- a/Demo/Info.plist +++ b/Demo/Info.plist @@ -6,8 +6,6 @@ en CFBundleExecutable ${EXECUTABLE_NAME} - CFBundleIconFile - CFBundleIdentifier com.shpakovski.mac.${PRODUCT_NAME:rfc1034identifier} CFBundleInfoDictionaryVersion @@ -18,14 +16,12 @@ APPL CFBundleShortVersionString 1.0 - CFBundleSignature - ???? CFBundleVersion 1 LSMinimumSystemVersion ${MACOSX_DEPLOYMENT_TARGET} NSHumanReadableCopyright - Copyright © 2012 Vadim Shpakovski. All rights reserved. + Copyright © 2012–2015 Vadim Shpakovski. All rights reserved. NSMainNibFile MainMenu NSPrincipalClass diff --git a/Demo/MainMenu.xib b/Demo/MainMenu.xib index a7d6806..488dea2 100644 --- a/Demo/MainMenu.xib +++ b/Demo/MainMenu.xib @@ -1,7 +1,8 @@ - + - + + @@ -10,7 +11,7 @@ - + @@ -648,58 +649,35 @@ - - + + - + - - - + - - - - + + - - - - + + + + After recording a shortcut, press it to play a sound. Notice that the shortcut is played even when the demo app is not in the foreground. + @@ -708,5 +686,6 @@ + -- cgit v1.2.3