Skip to content

Instantly share code, notes, and snippets.

@eahrold
Last active August 29, 2015 14:21
Show Gist options
  • Save eahrold/6d047d8e2a1b9e90c204 to your computer and use it in GitHub Desktop.
Save eahrold/6d047d8e2a1b9e90c204 to your computer and use it in GitHub Desktop.
Monitor NSTextFiled for a pause during user input.
//
// NSTextField+controlTextDidPause.h
//
// Created by Eldon Ahrold on 5/23/15.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import <Cocoa/Cocoa.h>
@interface NSTextField (controlTextDidPause)
- (void)observeControlTextDidPause:(void (^)())paused;
- (void)stopObservingControlTextDidPause;
@end
//
// NSTextField+controlTextDidPause.m
//
// Created by Eldon Ahrold on 5/23/15.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "NSTextField+controlTextDidPause.h"
#import <objc/runtime.h>
@interface NSTextField (controlTextDidPausePrivate)
@property (copy) void (^controlTextPausedBlock)();
@end
@implementation NSTextField (controlTextDidPausePrivate)
@dynamic controlTextPausedBlock;
- (void)dealloc
{
[self stopObservingControlTextDidPause];
}
- (void)stopObservingControlTextDidPause
{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:NSControlTextDidChangeNotification
object:nil];
self.controlTextPausedBlock = nil;
}
- (void)observeControlTextDidPause:(void (^)())didPause
{
NSAssert([NSThread isMainThread],
@"Observing control text must be executed on the main thread.");
NSAssert(didPause, @"The reply block must not be nil");
self.controlTextPausedBlock = didPause;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(watchForPause)
name:NSControlTextDidChangeNotification
object:nil];
}
- (void)watchForPause
{
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(editingDidPause)
object:nil];
[self performSelector:@selector(editingDidPause)
withObject:nil
afterDelay:2.0];
}
- (void)editingDidPause
{
dispatch_async(dispatch_get_main_queue(), ^{
if (self.controlTextPausedBlock) {
self.controlTextPausedBlock();
}
});
}
- (void)setControlTextPausedBlock:(void (^)())controlTextPausedBlock
{
objc_setAssociatedObject(self, @selector(controlTextPausedBlock),
controlTextPausedBlock,
OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)())controlTextPausedBlock
{
return objc_getAssociatedObject(self, @selector(controlTextPausedBlock));
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment