aboutsummaryrefslogtreecommitdiff
path: root/AppKit
diff options
context:
space:
mode:
authorGravatar thomasvl@gmail.com <thomasvl@gmail.com@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2009-02-03 17:09:43 +0000
committerGravatar thomasvl@gmail.com <thomasvl@gmail.com@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2009-02-03 17:09:43 +0000
commit74ad2857a75567b273951be9cbe998133fbca26a (patch)
tree9aedbec980fc19be9f3eecf7acd0dfaa9f7c8067 /AppKit
parent2ae297214778005d95354f207753180edca51ec4 (diff)
- Added GTMNSObject+KeyValueObserving to make it easier on folks to do KVO
"correctly". Based on some excellent code by Michael Ash. http://www.mikeash.com/?page=pyblog/key-value-observing-done-right.html This has been added for iPhone and OS X. - Fixed up GTMSenTestCase on iPhone so that it has a description that matches that of OCUnit.
Diffstat (limited to 'AppKit')
-rw-r--r--AppKit/GTMHotKeyTextField.m59
1 files changed, 34 insertions, 25 deletions
diff --git a/AppKit/GTMHotKeyTextField.m b/AppKit/GTMHotKeyTextField.m
index 0f14e48..6617b9a 100644
--- a/AppKit/GTMHotKeyTextField.m
+++ b/AppKit/GTMHotKeyTextField.m
@@ -20,6 +20,7 @@
#import <Carbon/Carbon.h>
#import "GTMSystemVersion.h"
#import "GTMObjectSingleton.h"
+#import "GTMNSObject+KeyValueObserving.h"
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
typedef struct __TISInputSource* TISInputSourceRef;
@@ -51,7 +52,9 @@ static CFStringRef kGTM_TISPropertyUnicodeKeyLayoutData = NULL;
#if GTM_SUPPORT_GC
- (void)finalize {
if (boundObject_ && boundKeyPath_) {
- [boundObject_ removeObserver:self forKeyPath:boundKeyPath_];
+ [boundObject_ gtm_removeObserver:self
+ forKeyPath:boundKeyPath_
+ selector:@selector(hotKeyValueChanged:)];
}
[super finalize];
}
@@ -60,7 +63,9 @@ static CFStringRef kGTM_TISPropertyUnicodeKeyLayoutData = NULL;
- (void)dealloc {
if (boundObject_ && boundKeyPath_) {
- [boundObject_ removeObserver:self forKeyPath:boundKeyPath_];
+ [boundObject_ gtm_removeObserver:self
+ forKeyPath:boundKeyPath_
+ selector:@selector(hotKeyValueChanged:)];
}
[boundObject_ release];
[boundKeyPath_ release];
@@ -93,7 +98,9 @@ static CFStringRef kGTM_TISPropertyUnicodeKeyLayoutData = NULL;
// Clean up value on unbind
if ([binding isEqualToString:NSValueBinding]) {
if (boundObject_ && boundKeyPath_) {
- [boundObject_ removeObserver:self forKeyPath:boundKeyPath_];
+ [boundObject_ gtm_removeObserver:self
+ forKeyPath:boundKeyPath_
+ selector:@selector(hotKeyValueChanged:)];
}
[boundObject_ release];
boundObject_ = nil;
@@ -104,31 +111,32 @@ static CFStringRef kGTM_TISPropertyUnicodeKeyLayoutData = NULL;
}
-- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
- change:(NSDictionary *)change context:(void *)context {
-
- if ((object == boundObject_) && [boundKeyPath_ isEqualToString:keyPath]) {
- // Our binding has changed, update
- id changedValue = [change objectForKey:NSKeyValueChangeNewKey];
- // NSUserDefaultsController does not appear to pass on the new object and,
- // perhaps other controllers may not, so if we get a nil or NSNull back
- // here let's directly retrieve the hotKeyDict_ from the object.
- if (!changedValue || changedValue == [NSNull null]) {
- changedValue = [object valueForKeyPath:keyPath];
- }
- [hotKeyDict_ autorelease];
- hotKeyDict_ = [changedValue copy];
- [self updateDisplayedPrettyString];
- }
-
-}
+- (void)hotKeyValueChanged:(GTMKeyValueChangeNotification *)note {
+ NSDictionary *change = [note change];
+ // Our binding has changed, update
+ id changedValue = [change objectForKey:NSKeyValueChangeNewKey];
+ // NSUserDefaultsController does not appear to pass on the new object and,
+ // perhaps other controllers may not, so if we get a nil or NSNull back
+ // here let's directly retrieve the hotKeyDict_ from the object.
+ if (!changedValue || changedValue == [NSNull null]) {
+ id object = [note object];
+ NSString *keyPath = [note keyPath];
+ changedValue = [object valueForKeyPath:keyPath];
+ }
+ [hotKeyDict_ autorelease];
+ hotKeyDict_ = [changedValue copy];
+ [self updateDisplayedPrettyString];
+}
+
// Private convenience method for attaching to a new binding
- (void)setupBinding:(id)bound withPath:(NSString *)path {
// Release previous
if (boundObject_ && boundKeyPath_) {
- [boundObject_ removeObserver:self forKeyPath:boundKeyPath_];
+ [boundObject_ gtm_removeObserver:self
+ forKeyPath:boundKeyPath_
+ selector:@selector(hotKeyValueChanged:)];
}
[boundObject_ release];
[boundKeyPath_ release];
@@ -136,10 +144,11 @@ static CFStringRef kGTM_TISPropertyUnicodeKeyLayoutData = NULL;
boundObject_ = [bound retain];
boundKeyPath_ = [path copy];
// Make ourself an observer
- [boundObject_ addObserver:self
+ [boundObject_ gtm_addObserver:self
forKeyPath:boundKeyPath_
- options:NSKeyValueObservingOptionNew
- context:nil];
+ selector:@selector(hotKeyValueChanged:)
+ userInfo:nil
+ options:NSKeyValueObservingOptionNew];
// Pull in any current value
[hotKeyDict_ autorelease];
hotKeyDict_ = [[boundObject_ valueForKeyPath:boundKeyPath_] copy];