Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Commit

Permalink
Fix a problem with defaults keys with dot symbols (#64).
Browse files Browse the repository at this point in the history
  • Loading branch information
zoul committed Mar 5, 2015
1 parent a9e6e52 commit fdc43c1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
25 changes: 21 additions & 4 deletions Framework/MASShortcutBinder.m
Expand Up @@ -23,7 +23,7 @@ - (id) init
- (void) dealloc
{
for (NSString *bindingName in [_actions allKeys]) {
[self unbind:bindingName];
[self unbind:[self encodeBindingName:bindingName]];
}
}

Expand All @@ -42,16 +42,18 @@ + (instancetype) sharedBinder
- (void) bindShortcutWithDefaultsKey: (NSString*) defaultsKeyName toAction: (dispatch_block_t) action
{
[_actions setObject:[action copy] forKey:defaultsKeyName];
[self bind:defaultsKeyName toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:[@"values." stringByAppendingString:defaultsKeyName] options:_bindingOptions];
[self bind:[self encodeBindingName:defaultsKeyName]
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:[@"values." stringByAppendingString:defaultsKeyName]
options:_bindingOptions];
}

- (void) breakBindingWithDefaultsKey: (NSString*) defaultsKeyName
{
[_shortcutMonitor unregisterShortcut:[_shortcuts objectForKey:defaultsKeyName]];
[_shortcuts removeObjectForKey:defaultsKeyName];
[_actions removeObjectForKey:defaultsKeyName];
[self unbind:defaultsKeyName];
[self unbind:[self encodeBindingName:defaultsKeyName]];
}

- (void) registerDefaultShortcuts: (NSDictionary*) defaultShortcuts
Expand All @@ -74,20 +76,35 @@ - (void) registerDefaultShortcuts: (NSDictionary*) defaultShortcuts

#pragma mark Bindings

static NSString *const MASDotSymbolReplacement = @"__dot__";

- (NSString*) encodeBindingName: (NSString*) binding
{
return [binding stringByReplacingOccurrencesOfString:@"." withString:MASDotSymbolReplacement];
}

- (NSString*) decodeBindingName: (NSString*) binding
{
return [binding stringByReplacingOccurrencesOfString:MASDotSymbolReplacement withString:@"."];
}

- (BOOL) isRegisteredAction: (NSString*) name
{
return !![_actions objectForKey:name];
}

- (id) valueForUndefinedKey: (NSString*) key
{
key = [self decodeBindingName:key];
return [self isRegisteredAction:key] ?
[_shortcuts objectForKey:key] :
[super valueForUndefinedKey:key];
}

- (void) setValue: (id) value forUndefinedKey: (NSString*) key
{
key = [self decodeBindingName:key];

if (![self isRegisteredAction:key]) {
[super setValue:value forUndefinedKey:key];
return;
Expand Down
15 changes: 13 additions & 2 deletions Framework/MASShortcutBinderTests.m
Expand Up @@ -95,11 +95,22 @@ - (void) testDefaultShortcuts
@"Bind shortcut using a default value.");
}

// The connection between user defaults and shortcuts is implemented
// using Cocoa Bindings where the dot symbol (“.”) has a special meaning.
// This means we have to escape the dot symbol used in defaults keys,
// otherwise things blow up. Details at <http://git.io/x5YS>.
- (void) testBindingsWithDotSymbol
{
static NSString *const SampleDefaultsKeyWithDotSymbol = @"sample.Shortcut";
XCTAssertThrows([_binder bindShortcutWithDefaultsKey:SampleDefaultsKeyWithDotSymbol toAction:^{}],
@"Attempting to use a defaults key with a dot symbol crashes with an exception.");
MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:1 modifierFlags:1];
XCTAssertNoThrow([_binder bindShortcutWithDefaultsKey:SampleDefaultsKeyWithDotSymbol toAction:^{}],
@"Binding a shortcut to a defaults key with a dot symbol must not throw.");
[_defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:shortcut] forKey:SampleDefaultsKeyWithDotSymbol];
XCTAssertTrue([_monitor isShortcutRegistered:shortcut],
@"Read a shortcut value using a defaults key with a dot symbol.");
[_binder breakBindingWithDefaultsKey:SampleDefaultsKeyWithDotSymbol];
XCTAssertFalse([_monitor isShortcutRegistered:shortcut],
@"Breaking a binding with a dot symbol.");
}

@end

0 comments on commit fdc43c1

Please sign in to comment.