Progress indicator in dock icon with animation
Progress indicator in dock icon with animation
- Subject: Progress indicator in dock icon with animation
- From: Christopher Atlan <email@hidden>
- Date: Sat, 6 Aug 2005 15:40:13 +0200
Hi,
one base of http://lists.apple.com/archives/cocoa-dev/2004/Mar/
msg01443.html and http://lists.apple.com/archives/cocoa-dev/2004/Feb/
msg00518.html i try do get a NSProgressIndicator Indeterminate with
animation in the dock icon.
My current version work, but is slow and need to much cpu power. Has
someone a idea to get i faster?
Here the code:
DockIconProgressIndicatior.h:
#include <Cocoa/Cocoa.h>
@interface DockIconProgressIndicator : NSWindow {
NSProgressIndicator *dockIndicator;
NSImage *applicationIconImage;
NSRect makeRect;
NSRect rect;
NSPoint point;
}
- (id) init;
- (void) updateApplicationIconImage;
@end
DockIconProgressIndicatior.m:
#include <Cocoa/Cocoa.h>
#import "DockIconProgressIndicator.h"
@implementation DockIconProgressIndicator
- (id) init {
makeRect = NSMakeRect(0, 0, 128, 20);
self = [super initWithContentRect:makeRect
styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered
defer:NO];
dockIndicator = [[NSProgressIndicator alloc]
initWithFrame:makeRect];
[[self contentView] addSubview:dockIndicator];
[dockIndicator setIndeterminate:YES];
[dockIndicator setUsesThreadedAnimation:YES];
//[dockIndicator setControlTint:NSBlueControlTint];
[dockIndicator setControlSize:NSSmallControlSize];
applicationIconImage = [[NSApp applicationIconImage] copy];
rect = (NSRect){{0.0,0.0},{128.0,20.0}};
point = (NSPoint){0.0,0.0};
[dockIndicator setDoubleValue:(double)50];
return self;
}
- (void) updateApplicationIconImage {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//[dockIndicator setAnimationDelay:0.08];
[dockIndicator startAnimation:self];
while([dockIndicator doubleValue] < [dockIndicator maxValue]) {
// *** Snap the progress indicator into an image (NSView
dataWithPDFInsideRect: selector - very useful).
NSImage *dockIndicatorImage = [[NSImage alloc]
initWithData:[dockIndicator
dataWithPDFInsideRect:rect]];
// *** Get the app's default icon.
NSImage *mainIcon = [applicationIconImage copy];
// *** Lock image ops onto the default icon.
[mainIcon lockFocus];
// *** composite the progress indicator image onto the dock
icon. I keep the dock icon
// *** on top of the progress bar using
NSCompositeDestinationAtop, but other apps may
// *** obscure too much of the bar and require
NSCompositeSourceAtop instead.
[dockIndicatorImage compositeToPoint:point
operation:NSCompositeCopy];
[mainIcon unlockFocus];
// *** Replace the dock icon with the image containing the
progress bar.
[NSApp setApplicationIconImage:mainIcon];
[dockIndicatorImage release];
[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:0.08]];
//printf("sei langsam\n");
}
// set the orginal dock icon
[NSApp setApplicationIconImage:applicationIconImage];
[pool release];
return;
}
- (BOOL) isKeyWindow {
return YES;
}
- (BOOL) isVisible {
return YES;
}
@end
To start the animation:
DockIconProgressIndicator *dipi = [[DockIconProgressIndicator alloc]
init];
[NSThread detachNewThreadSelector: @selector
(updateApplicationIconImage) toTarget: dipi withObject: nil];
Regards,
Christopher Atlan
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden