Skip to content

Instantly share code, notes, and snippets.

@yene
Created June 22, 2010 14:30
Show Gist options
  • Save yene/448532 to your computer and use it in GitHub Desktop.
Save yene/448532 to your computer and use it in GitHub Desktop.
@interface MyApplication : NSApplication {
NSTimer *idleTimer;
NSMutableArray *idleTimerObjects;
unsigned int elapsedSeconds;
}
- (void)sendEvent:(NSEvent *)anEvent;
- (void)timerInvocation;
- (BOOL)isUser5SecondsIdle;
- (void)finishIdle;
- (void)addIdleTimer:(IdleTimer *)timerObject;
- (void)removeIdleTimer:(IdleTimer *)timerObject;
@end
#import "MyApplication.h"
#import "IdleTimer.h"
@implementation MyApplication
- (id) init
{
self = [super init];
if (self != nil) {
elapsedSeconds = 0;
idleTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerInvocation) userInfo:nil repeats:YES];
idleTimerObjects = [[NSMutableArray array] retain];
}
return self;
}
- (void) dealloc
{
[idleTimer release];
[idleTimerObjects release];
[super dealloc];
}
- (void)sendEvent:(NSEvent *)anEvent;
{
[super sendEvent:anEvent];
// you can filter events here
if (elapsedSeconds >= 1)
[self finishIdle];
elapsedSeconds = 0;
}
- (void)timerInvocation;
{
elapsedSeconds++;
for (NSValue *temp in idleTimerObjects) {
if (elapsedSeconds == [[temp nonretainedObjectValue] interval]) {
[[temp nonretainedObjectValue] timerBeginsIdling:self];
}
}
}
- (void)finishIdle;
{
for (NSValue *temp in idleTimerObjects) {
if ([[temp nonretainedObjectValue] isIdle]) {
[[temp nonretainedObjectValue] timerFinishedIdling:self];
}
}
}
- (void)addIdleTimer:(IdleTimer *)timerObject;
{
NSValue *value = [NSValue valueWithNonretainedObject:timerObject];
[idleTimerObjects addObject:value];
}
- (void)removeIdleTimer:(IdleTimer *)timerObject;
{
for (NSValue *temp in idleTimerObjects) {
if ([[NSValue valueWithNonretainedObject:timerObject] isEqual:temp]) {
[idleTimerObjects removeObject:temp];
break;
}
}
}
- (BOOL)isUser5SecondsIdle;
{
return (elapsedSeconds >= 5);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment