aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Demo/AppDelegate.h5
-rw-r--r--Demo/AppDelegate.m92
-rw-r--r--Demo/Info.plist6
-rw-r--r--Demo/MainMenu.xib53
4 files changed, 36 insertions, 120 deletions
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 <NSApplicationDelegate>
@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 @@
<string>en</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
- <key>CFBundleIconFile</key>
- <string></string>
<key>CFBundleIdentifier</key>
<string>com.shpakovski.mac.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
@@ -18,14 +16,12 @@
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
- <key>CFBundleSignature</key>
- <string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>NSHumanReadableCopyright</key>
- <string>Copyright © 2012 Vadim Shpakovski. All rights reserved.</string>
+ <string>Copyright © 2012–2015 Vadim Shpakovski. All rights reserved.</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
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 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="5056" systemVersion="13E28" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
+<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6254" systemVersion="14B25" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
- <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="5056"/>
+ <deployment identifier="macosx"/>
+ <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6254"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
@@ -10,7 +11,7 @@
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
- <customObject id="-3" userLabel="Application"/>
+ <customObject id="-3" userLabel="Application" customClass="NSObject"/>
<menu title="AMainMenu" systemMenu="main" id="29">
<items>
<menuItem title="Demo" id="56">
@@ -648,58 +649,35 @@
</menu>
<window title="Demo" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" frameAutosaveName="DemoWindow" animationBehavior="default" id="371">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES"/>
- <rect key="contentRect" x="335" y="390" width="410" height="132"/>
- <rect key="screenRect" x="0.0" y="0.0" width="1920" height="1058"/>
+ <rect key="contentRect" x="335" y="390" width="290" height="171"/>
+ <rect key="screenRect" x="0.0" y="0.0" width="1920" height="1057"/>
<view key="contentView" id="372">
- <rect key="frame" x="0.0" y="0.0" width="410" height="132"/>
+ <rect key="frame" x="0.0" y="0.0" width="290" height="171"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
- <button horizontalHuggingPriority="1000" verticalHuggingPriority="1000" horizontalCompressionResistancePriority="1000" verticalCompressionResistancePriority="1000" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="639">
- <rect key="frame" x="131" y="28" width="201" height="18"/>
- <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
- <buttonCell key="cell" type="check" title="Also use ⌘2 to play a sound" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="640">
- <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
- <font key="font" metaFont="system"/>
- </buttonCell>
- <connections>
- <binding destination="494" name="value" keyPath="constantShortcutEnabled" id="711"/>
- </connections>
- </button>
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="536" customClass="MASShortcutView">
- <rect key="frame" x="133" y="87" width="127" height="19"/>
- <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
+ <rect key="frame" x="20" y="102" width="250" height="19"/>
</customView>
- <button horizontalHuggingPriority="1000" verticalHuggingPriority="1000" horizontalCompressionResistancePriority="1000" verticalCompressionResistancePriority="1000" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="579">
- <rect key="frame" x="131" y="48" width="261" height="18"/>
- <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
- <buttonCell key="cell" type="check" title="Global shortcut to play a sound" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="580">
- <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
- <font key="font" metaFont="system"/>
- </buttonCell>
- <connections>
- <binding destination="494" name="value" keyPath="shortcutEnabled" id="638"/>
- </connections>
- </button>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="PG0-jh-Onh">
- <rect key="frame" x="20" y="89" width="105" height="17"/>
- <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
- <textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Global shortcut:" id="85u-1A-n7H">
+ <rect key="frame" x="18" y="134" width="119" height="17"/>
+ <textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Record a shortcut:" id="85u-1A-n7H">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
- <textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Ezj-wg-E35">
- <rect key="frame" x="20" y="50" width="105" height="17"/>
- <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
- <textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Options:" id="Wct-tg-3PU">
+ <textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" setsMaxLayoutWidthAtFirstLayout="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Ieo-bz-mir">
+ <rect key="frame" x="18" y="23" width="254" height="68"/>
+ <textFieldCell key="cell" sendsActionOnEndEditing="YES" alignment="left" id="WDe-Rt-e8p">
<font key="font" metaFont="system"/>
+ <string key="title">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.</string>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
</view>
+ <point key="canvasLocation" x="297" y="335.5"/>
</window>
<customObject id="494" customClass="AppDelegate">
<connections>
@@ -708,5 +686,6 @@
</connections>
</customObject>
<customObject id="420" customClass="NSFontManager"/>
+ <userDefaultsController representsSharedInstance="YES" id="rCO-Ve-DT5"/>
</objects>
</document>