drawing placement is less dynamic code-wise but should be faster

This commit is contained in:
Mitchell Livingston 2007-08-11 23:49:26 +00:00
parent 5c285b8ca3
commit b899afc77a
1 changed files with 65 additions and 69 deletions

View File

@ -29,9 +29,14 @@
#define BAR_HEIGHT 12.0 #define BAR_HEIGHT 12.0
#define PADDING_HORIZONAL 2.0
#define IMAGE_SIZE_REG 32.0 #define IMAGE_SIZE_REG 32.0
#define IMAGE_SIZE_MIN 16.0 #define IMAGE_SIZE_MIN 16.0
//end up being larger than font height
#define HEIGHT_TITLE 16.0
#define HEIGHT_STATUS 12.0
#define PADDING_HORIZONAL 2.0
#define PADDING_BETWEEN_IMAGE_AND_TITLE 4.0 #define PADDING_BETWEEN_IMAGE_AND_TITLE 4.0
#define PADDING_ABOVE_TITLE 2.0 #define PADDING_ABOVE_TITLE 2.0
#define PADDING_ABOVE_MIN_STATUS 4.0 #define PADDING_ABOVE_MIN_STATUS 4.0
@ -39,6 +44,7 @@
#define PADDING_BETWEEN_TITLE_AND_PROGRESS 2.0 #define PADDING_BETWEEN_TITLE_AND_PROGRESS 2.0
#define PADDING_LESS_BETWEEN_TITLE_AND_BAR 1.0 #define PADDING_LESS_BETWEEN_TITLE_AND_BAR 1.0
#define PADDING_BETWEEN_PROGRESS_AND_BAR 2.0 #define PADDING_BETWEEN_PROGRESS_AND_BAR 2.0
#define PADDING_BETWEEN_TITLE_AND_BAR_MIN 3.0
#define PADDING_BETWEEN_BAR_AND_STATUS 2.0 #define PADDING_BETWEEN_BAR_AND_STATUS 2.0
#define MAX_PIECES 324 #define MAX_PIECES 324
@ -46,17 +52,17 @@
@interface TorrentCell (Private) @interface TorrentCell (Private)
- (NSImage *) simpleBar: (NSSize) barSize; - (NSImage *) simpleBar: (float) width;
- (NSImage *) advancedBar: (NSSize) barSize; - (NSImage *) advancedBar: (float) width;
- (NSImage *) advancedBarSimple; - (NSImage *) advancedBarSimple;
#warning rearrange #warning rearrange
- (NSRect) rectForMinimalStatusRectWithString: (NSAttributedString *) string inBounds: (NSRect) bounds; - (NSRect) rectForMinimalStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
- (NSRect) rectForTitleBasedOnMinimalStatusRect: (NSRect) statusRect withString: (NSAttributedString *) string - (NSRect) rectForTitleBasedOnMinimalStatusRect: (NSRect) statusRect withString: (NSAttributedString *) string
inBounds: (NSRect) bounds; inBounds: (NSRect) bounds;
- (NSRect) rectForProgressBasedOnTitleRect: (NSRect) titleRect withString: (NSAttributedString *) string inBounds: (NSRect) bounds; - (NSRect) rectForProgressWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
- (NSRect) rectForBarBasedOnAboveRect: (NSRect) aboveRect inBounds: (NSRect) bounds; - (NSRect) rectForBarInBounds: (NSRect) bounds;
- (NSRect) rectForStatusBasedOnProgressRect: (NSRect) progressRect withString: (NSAttributedString *) string - (NSRect) rectForStatusWithString: (NSAttributedString *) string
inBounds: (NSRect) bounds; inBounds: (NSRect) bounds;
- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color; - (NSAttributedString *) attributedTitleWithColor: (NSColor *) color;
@ -151,28 +157,25 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
Torrent * torrent = [self representedObject]; Torrent * torrent = [self representedObject];
NSString * string = [torrent isActive] && ![fDefaults boolForKey: @"SmallStatusRegular"] NSString * string = [torrent isActive] && ![fDefaults boolForKey: @"SmallStatusRegular"]
? [torrent remainingTimeString] : [torrent shortStatusString]; ? [torrent remainingTimeString] : [torrent shortStatusString];
return [self rectForMinimalStatusRectWithString: [self attributedStatusString: string withColor: nil] inBounds: bounds]; return [self rectForMinimalStatusWithString: [self attributedStatusString: string withColor: nil] inBounds: bounds];
} }
- (NSRect) progressRectForBounds: (NSRect) bounds - (NSRect) progressRectForBounds: (NSRect) bounds
{ {
NSString * string = [[self representedObject] progressString]; return [self rectForProgressWithString: [self attributedStatusString: [[self representedObject] progressString] withColor: nil]
return [self rectForProgressBasedOnTitleRect: [self titleRectForBounds: bounds] inBounds: bounds];
withString: [self attributedStatusString: string withColor: nil] inBounds: bounds];
} }
#warning remove other
- (NSRect) barRectForBounds: (NSRect) bounds - (NSRect) barRectForBounds: (NSRect) bounds
{ {
NSRect aboveRect = [fDefaults boolForKey: @"SmallView"] ? [self titleRectForBounds: bounds] return [self rectForBarInBounds: bounds];
: [self progressRectForBounds: bounds];
return [self rectForBarBasedOnAboveRect: aboveRect inBounds: bounds];
} }
- (NSRect) statusRectForBounds: (NSRect) bounds - (NSRect) statusRectForBounds: (NSRect) bounds
{ {
NSString * string = [[self representedObject] statusString]; return [self rectForStatusWithString: [self attributedStatusString: [[self representedObject] statusString] withColor: nil]
return [self rectForStatusBasedOnProgressRect: [self progressRectForBounds: bounds] inBounds: bounds];
withString: [self attributedStatusString: string withColor: nil] inBounds: bounds];
} }
- (void) drawInteriorWithFrame: (NSRect) cellFrame inView: (NSView *) controlView - (void) drawInteriorWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
@ -226,7 +229,7 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
NSString * string = ![fDefaults boolForKey: @"SmallStatusRegular"] && [torrent isActive] NSString * string = ![fDefaults boolForKey: @"SmallStatusRegular"] && [torrent isActive]
? [torrent remainingTimeString] : [torrent shortStatusString]; ? [torrent remainingTimeString] : [torrent shortStatusString];
NSAttributedString * minimalString = [self attributedStatusString: string withColor: statusColor]; NSAttributedString * minimalString = [self attributedStatusString: string withColor: statusColor];
minimalStatusRect = [self rectForMinimalStatusRectWithString: minimalString inBounds: cellFrame]; minimalStatusRect = [self rectForMinimalStatusWithString: minimalString inBounds: cellFrame];
[minimalString drawInRect: minimalStatusRect]; [minimalString drawInRect: minimalStatusRect];
} }
@ -241,21 +244,22 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
if (!minimal) if (!minimal)
{ {
NSAttributedString * progressString = [self attributedStatusString: [torrent progressString] withColor: statusColor]; NSAttributedString * progressString = [self attributedStatusString: [torrent progressString] withColor: statusColor];
progressRect = [self rectForProgressBasedOnTitleRect: titleRect withString: progressString inBounds: cellFrame]; progressRect = [self rectForProgressWithString: progressString inBounds: cellFrame];
[progressString drawInRect: progressRect]; [progressString drawInRect: progressRect];
} }
#warning bar is already sized.....
//bar //bar
NSRect barRect = [self rectForBarBasedOnAboveRect: minimal ? titleRect : progressRect inBounds: cellFrame]; NSRect barRect = [self rectForBarInBounds: cellFrame];
NSImage * bar = [fDefaults boolForKey: @"UseAdvancedBar"] ? [self advancedBar: barRect.size] : [self simpleBar: barRect.size]; NSImage * bar = [fDefaults boolForKey: @"UseAdvancedBar"]
? [self advancedBar: barRect.size.width] : [self simpleBar: barRect.size.width];
[bar drawInRect: barRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0]; [bar drawInRect: barRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
//status //status
if (!minimal) if (!minimal)
{ {
NSAttributedString * statusString = [self attributedStatusString: [torrent statusString] withColor: statusColor]; NSAttributedString * statusString = [self attributedStatusString: [torrent statusString] withColor: statusColor];
NSRect statusRect = [self rectForStatusBasedOnProgressRect: progressRect withString: statusString inBounds: cellFrame]; [statusString drawInRect: [self rectForStatusWithString: statusString inBounds: cellFrame]];
[statusString drawInRect: statusRect];
} }
} }
@ -263,41 +267,32 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
@implementation TorrentCell (Private) @implementation TorrentCell (Private)
- (NSImage *) simpleBar: (NSSize) barSize - (NSImage *) simpleBar: (float) width
{ {
Torrent * torrent = [self representedObject]; Torrent * torrent = [self representedObject];
NSImage * bar = [[NSImage alloc] initWithSize: barSize]; NSImage * bar = [[NSImage alloc] initWithSize: NSMakeSize(width, BAR_HEIGHT)];
[bar lockFocus]; [bar lockFocus];
NSRect barBounds = NSMakeRect(0, 0, barSize.width, barSize.height); float progress = [torrent progress], left = [torrent progressLeft];
float progress = [torrent progress];
float left = INVALID;
if (progress < 1.0) if (progress < 1.0)
{ {
if (!fWhiteGradient) if (!fWhiteGradient)
fWhiteGradient = [[CTGradient progressWhiteGradient] retain]; fWhiteGradient = [[CTGradient progressWhiteGradient] retain];
[fWhiteGradient fillRect: barBounds angle: -90]; [fWhiteGradient fillRect: NSMakeRect(width * progress, 0, width * left, BAR_HEIGHT) angle: -90];
left = [torrent progressLeft];
float include = progress + left; float include = progress + left;
if (include < 1.0) if (include < 1.0)
{ {
NSRect blankBounds = barBounds;
blankBounds.origin.x += barBounds.size.width * include;
blankBounds.size.width *= (1.0 - include);
if (!fLightGrayGradient) if (!fLightGrayGradient)
fLightGrayGradient = [[CTGradient progressLightGrayGradient] retain]; fLightGrayGradient = [[CTGradient progressLightGrayGradient] retain];
[fLightGrayGradient fillRect: blankBounds angle: -90]; [fLightGrayGradient fillRect: NSMakeRect(width * include, 0, width * (1.0 - include), BAR_HEIGHT)
angle: -90];
} }
} }
NSRect completeBounds = barBounds; NSRect completeBounds = NSMakeRect(0, 0, width * progress, BAR_HEIGHT);
completeBounds.size.width *= progress;
if ([torrent isActive]) if ([torrent isActive])
{ {
if ([torrent isChecking]) if ([torrent isChecking])
@ -333,9 +328,6 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
{ {
if ([torrent waitingToStart]) if ([torrent waitingToStart])
{ {
if (left == INVALID)
left = [torrent progressLeft];
if (left <= 0.0) if (left <= 0.0)
{ {
if (!fDarkGreenGradient) if (!fDarkGreenGradient)
@ -358,23 +350,23 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
} }
[[NSColor colorWithDeviceWhite: 0.0 alpha: 0.2] set]; [[NSColor colorWithDeviceWhite: 0.0 alpha: 0.2] set];
[NSBezierPath strokeRect: NSInsetRect(barBounds, 0.5, 0.5)]; [NSBezierPath strokeRect: NSInsetRect(NSMakeRect(0, 0, width, BAR_HEIGHT), 0.5, 0.5)];
[bar unlockFocus]; [bar unlockFocus];
return [bar autorelease]; return [bar autorelease];
} }
- (NSImage *) advancedBar: (NSSize) barSize - (NSImage *) advancedBar: (float) width
{ {
NSImage * image = [self advancedBarSimple]; NSImage * image = [self advancedBarSimple];
[image setScalesWhenResized: YES]; [image setScalesWhenResized: YES];
[image setSize: barSize]; [image setSize: NSMakeSize(width, BAR_HEIGHT)];
[image lockFocus]; [image lockFocus];
NSRect barBounds = NSMakeRect(0, 0, barSize.width, barSize.height); NSRect barBounds = NSMakeRect(0, 0, width, BAR_HEIGHT);
if (!fTransparentGradient) if (!fTransparentGradient)
fTransparentGradient = [[CTGradient progressTransparentGradient] retain]; fTransparentGradient = [[CTGradient progressTransparentGradient] retain];
@ -529,7 +521,7 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
return [bar autorelease]; return [bar autorelease];
} }
- (NSRect) rectForMinimalStatusRectWithString: (NSAttributedString *) string inBounds: (NSRect) bounds - (NSRect) rectForMinimalStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
{ {
if (![fDefaults boolForKey: @"SmallView"]) if (![fDefaults boolForKey: @"SmallView"])
return NSZeroRect; return NSZeroRect;
@ -549,7 +541,6 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
BOOL minimal = [fDefaults boolForKey: @"SmallView"]; BOOL minimal = [fDefaults boolForKey: @"SmallView"];
NSRect result = bounds; NSRect result = bounds;
result.origin.y += PADDING_ABOVE_TITLE; result.origin.y += PADDING_ABOVE_TITLE;
result.origin.x += PADDING_HORIZONAL + (minimal ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG) + PADDING_BETWEEN_IMAGE_AND_TITLE; result.origin.x += PADDING_HORIZONAL + (minimal ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG) + PADDING_BETWEEN_IMAGE_AND_TITLE;
@ -560,48 +551,53 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
return result; return result;
} }
- (NSRect) rectForProgressBasedOnTitleRect: (NSRect) titleRect withString: (NSAttributedString *) string inBounds: (NSRect) bounds - (NSRect) rectForProgressWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
{ {
if ([fDefaults boolForKey: @"SmallView"]) if ([fDefaults boolForKey: @"SmallView"])
return NSZeroRect; return NSZeroRect;
NSSize progressSize = [string size]; NSRect result = bounds;
result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS;
result.origin.x += PADDING_HORIZONAL + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_TITLE;
NSRect result = titleRect; result.size = [string size];
result.size.width = MIN(progressSize.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL); result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
result.origin.y += titleRect.size.height + PADDING_BETWEEN_TITLE_AND_PROGRESS;
result.size.height = progressSize.height;
return result; return result;
} }
- (NSRect) rectForBarBasedOnAboveRect: (NSRect) aboveRect inBounds: (NSRect) bounds - (NSRect) rectForBarInBounds: (NSRect) bounds
{ {
NSRect result; BOOL minimal = [fDefaults boolForKey: @"SmallView"];
NSRect result = bounds;
result.size.height = BAR_HEIGHT; result.size.height = BAR_HEIGHT;
result.origin.x = bounds.origin.x + result.origin.x = PADDING_HORIZONAL + (minimal ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG)
PADDING_HORIZONAL + ([fDefaults boolForKey: @"SmallView"] ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG)
+ PADDING_BETWEEN_IMAGE_AND_TITLE - PADDING_LESS_BETWEEN_TITLE_AND_BAR; + PADDING_BETWEEN_IMAGE_AND_TITLE - PADDING_LESS_BETWEEN_TITLE_AND_BAR;
result.origin.y = NSMaxY(aboveRect) + PADDING_BETWEEN_PROGRESS_AND_BAR; #warning rename some
result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE;
if (minimal)
result.origin.y += PADDING_BETWEEN_TITLE_AND_BAR_MIN;
else
result.origin.y += PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS + PADDING_BETWEEN_PROGRESS_AND_BAR;
result.size.width = NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL - BUTTONS_TOTAL_WIDTH; result.size.width = NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL - BUTTONS_TOTAL_WIDTH;
return result; return result;
} }
- (NSRect) rectForStatusBasedOnProgressRect: (NSRect) progressRect withString: (NSAttributedString *) string - (NSRect) rectForStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
inBounds: (NSRect) bounds
{ {
if ([fDefaults boolForKey: @"SmallView"]) if ([fDefaults boolForKey: @"SmallView"])
return NSZeroRect; return NSZeroRect;
NSSize statusSize = [string size]; NSRect result = bounds;
result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS
+ PADDING_BETWEEN_PROGRESS_AND_BAR + BAR_HEIGHT + PADDING_BETWEEN_BAR_AND_STATUS;
result.origin.x += PADDING_HORIZONAL + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_TITLE;
NSRect result = progressRect; result.size = [string size];
result.size.width = MIN(statusSize.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL); result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
result.origin.y += statusSize.height + PADDING_BETWEEN_PROGRESS_AND_BAR + BAR_HEIGHT + PADDING_BETWEEN_BAR_AND_STATUS;
result.size.height = statusSize.height;
return result; return result;
} }
@ -610,7 +606,7 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
{ {
if (color) if (color)
[fTitleAttributes setObject: color forKey: NSForegroundColorAttributeName]; [fTitleAttributes setObject: color forKey: NSForegroundColorAttributeName];
NSString * title = [[self representedObject] name]; NSString * title = [[self representedObject] name];
return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease]; return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
} }