UKDockProgressIndicator
Language: Objective-C, Author: Uli Kusterer
License: MIT/X11
A class that shows a small determinate progress bar overlaid over your application's dock icon.
This is intended as a drop-in replacement for the NSProgressIndicator view, as it understands the same messages. You can even have the dock progress indicator forward all calls to the view, thus driving both without changing your existing code.
UKDockProgressIndicator source preview
//
// UKDockProgressIndicator.m
// Doublette
// LICENSE: MIT License
//
// Created by Uli Kusterer on 30.04.05.
// Copyright 2005 M. Uli Kusterer. All rights reserved.
//
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#import "UKDockProgressIndicator.h"
@implementation UKDockProgressIndicator
// -----------------------------------------------------------------------------
// NSProgressIndicator-like methods:
// -----------------------------------------------------------------------------
-(void) setMinValue: (double)mn
{
min = mn;
[progress setMinValue: mn]; // Call through to associated view if user wants us to.
[self updateDockTile];
}
-(double) minValue
{
return min;
}
-(void) setMaxValue: (double)mn
{
max = mn;
[progress setMaxValue: mn]; // Call through to associated view if user wants us to.
[self updateDockTile];
}
-(double) maxValue
{
return max;
}
-(void) setDoubleValue: (double)mn
{
current = mn;
[progress setDoubleValue: mn]; // Call through to associated view if user wants us to.
[self updateDockTile];
}
-(double) doubleValue
{
return current;
}
-(void) setNeedsDisplay: (BOOL)mn
{
[progress setNeedsDisplay: mn]; // Call through to associated view if user wants us to.
}
-(void) display
{
[progress display]; // Call through to associated view if user wants us to.
}
-(void) setHidden: (BOOL)flag
{
[progress setHidden: flag]; // Call through to associated view if user wants us to.
if( flag ) // Progress indicator is being hidden? Reset dock tile to regular icon again:
[NSApp setApplicationIconImage: [NSImage imageNamed: @"NSApplicationIcon"]];
}
-(BOOL) isHidden
{
return [progress isHidden];
}
// -----------------------------------------------------------------------------
// updateDockTile:
// Main drawing bottleneck. This takes our min, max and current values and
// draws them onto the dock tile. If the MiniProgressGradient.png image is
// present, this stretches that image to draw the progress bar.
//
// If no image is present this falls back on the knob color.
// -----------------------------------------------------------------------------
-(void) updateDockTile
{
NSImage* dockIcon = [[[NSImage alloc] initWithSize: NSMakeSize(128,128)] autorelease];
[dockIcon lockFocus];
NSRect box = { {4, 4}, {120, 16} };
// App icon:
[[NSImage imageNamed: @"NSApplicationIcon"] dissolveToPoint: NSZeroPoint fraction: 1.0];
// Track & Outline:
[[NSColor whiteColor] set];
[NSBezierPath fillRect: box];
[[NSColor blackColor] set];
[NSBezierPath strokeRect: box];
// State fill:
box = NSInsetRect( box, 1, 1 );
[[NSColor knobColor] set];
box.size.width = (box.size.width / (max -min)) * (current -min);
NSImage* prImg = [NSImage imageNamed: @"MiniProgressGradient"];
NSRect picBox = { { 0,0 }, { 0,0 } };
if( prImg )
{
picBox.size = [prImg size];
[prImg drawInRect: box fromRect: picBox operation: NSCompositeCopy fraction: 1.0];
}
else
NSRectFill( box );
[dockIcon unlockFocus];
[NSApp setApplicationIconImage: dockIcon];
}
@end
UKDockProgressIndicator header preview
//
// UKDockProgressIndicator.h
// Doublette
//
// Created by Uli Kusterer on 30.04.05.
// Copyright 2005 M. Uli Kusterer. All rights reserved.
//
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#import <Cocoa/Cocoa.h>
/* A class that displays a determinate progress indicator (progress bar)
on top of the app's icon in the dock. Use it just like an NSProgressIndicator.
You can even have it call through to another progress indicator if desired. */
// -----------------------------------------------------------------------------
// Class Declarationâ:
// -----------------------------------------------------------------------------
@interface UKDockProgressIndicator : NSObject
{
IBOutlet NSProgressIndicator* progress; // A progress indicator view to call through to.
double max;
double min;
double current;
}
// NSProgressIndicator compatibility stuff:
// These forward to "progress" if you've hooked that up in IB.
-(void) setMinValue: (double)mn;
-(double) minValue;
-(void) setMaxValue: (double)mn;
-(double) maxValue;
-(void) setDoubleValue: (double)mn;
-(double) doubleValue;
-(void) setNeedsDisplay: (BOOL)mn;
-(void) display;
-(void) setHidden: (BOOL)flag;
-(BOOL) isHidden;
// private:
-(void) updateDockTile;
@end
Download Archive
Compatible with:
- Mac OS X 10.3
- Mac OS X 10.4 PPC
- Mac OS X 10.4 Intel
- Mac OS X 10.5 PPC
- Mac OS X 10.5 Intel
Comments
Comment feed
