have the code generate each attributed string once per draw instead of twice

This commit is contained in:
Mitchell Livingston 2007-08-11 17:04:10 +00:00
parent b476e6f8eb
commit 5c285b8ca3
1 changed files with 57 additions and 36 deletions

View File

@ -50,10 +50,14 @@
- (NSImage *) advancedBar: (NSSize) barSize; - (NSImage *) advancedBar: (NSSize) barSize;
- (NSImage *) advancedBarSimple; - (NSImage *) advancedBarSimple;
- (NSRect) rectForTitleBasedOnMinimalStatusRect: (NSRect) statusRect inBounds: (NSRect) bounds; #warning rearrange
- (NSRect) rectForProgressBasedOnTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds; - (NSRect) rectForMinimalStatusRectWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
- (NSRect) rectForTitleBasedOnMinimalStatusRect: (NSRect) statusRect withString: (NSAttributedString *) string
inBounds: (NSRect) bounds;
- (NSRect) rectForProgressBasedOnTitleRect: (NSRect) titleRect withString: (NSAttributedString *) string inBounds: (NSRect) bounds;
- (NSRect) rectForBarBasedOnAboveRect: (NSRect) aboveRect inBounds: (NSRect) bounds; - (NSRect) rectForBarBasedOnAboveRect: (NSRect) aboveRect inBounds: (NSRect) bounds;
- (NSRect) rectForStatusBasedOnProgressRect: (NSRect) progressRect inBounds: (NSRect) bounds; - (NSRect) rectForStatusBasedOnProgressRect: (NSRect) progressRect withString: (NSAttributedString *) string
inBounds: (NSRect) bounds;
- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color; - (NSAttributedString *) attributedTitleWithColor: (NSColor *) color;
- (NSAttributedString *) attributedStatusString: (NSString *) string withColor: (NSColor *) color; - (NSAttributedString *) attributedStatusString: (NSString *) string withColor: (NSColor *) color;
@ -138,31 +142,23 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
- (NSRect) titleRectForBounds: (NSRect) bounds - (NSRect) titleRectForBounds: (NSRect) bounds
{ {
return [self rectForTitleBasedOnMinimalStatusRect: [self minimalStatusRectForBounds: bounds] inBounds: bounds]; return [self rectForTitleBasedOnMinimalStatusRect: [self minimalStatusRectForBounds: bounds]
withString: [self attributedTitleWithColor: nil] inBounds: bounds];
} }
- (NSRect) minimalStatusRectForBounds: (NSRect) bounds - (NSRect) minimalStatusRectForBounds: (NSRect) bounds
{ {
if (![fDefaults boolForKey: @"SmallView"])
return NSZeroRect;
Torrent * torrent = [self representedObject]; Torrent * torrent = [self representedObject];
NSString * string = ![fDefaults boolForKey: @"SmallStatusRegular"] && [torrent isActive] NSString * string = [torrent isActive] && ![fDefaults boolForKey: @"SmallStatusRegular"]
? [torrent remainingTimeString] : [torrent shortStatusString]; ? [torrent remainingTimeString] : [torrent shortStatusString];
NSAttributedString * status = [self attributedStatusString: string withColor: nil]; return [self rectForMinimalStatusRectWithString: [self attributedStatusString: string withColor: nil] inBounds: bounds];
NSRect result = bounds;
result.size = [status size];
result.origin.x += bounds.size.width - result.size.width - PADDING_HORIZONAL;
result.origin.y += PADDING_ABOVE_MIN_STATUS;
return result;
} }
- (NSRect) progressRectForBounds: (NSRect) bounds - (NSRect) progressRectForBounds: (NSRect) bounds
{ {
return [self rectForProgressBasedOnTitleRect: [self titleRectForBounds: bounds] inBounds: bounds]; NSString * string = [[self representedObject] progressString];
return [self rectForProgressBasedOnTitleRect: [self titleRectForBounds: bounds]
withString: [self attributedStatusString: string withColor: nil] inBounds: bounds];
} }
- (NSRect) barRectForBounds: (NSRect) bounds - (NSRect) barRectForBounds: (NSRect) bounds
@ -174,11 +170,15 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
- (NSRect) statusRectForBounds: (NSRect) bounds - (NSRect) statusRectForBounds: (NSRect) bounds
{ {
return [self rectForStatusBasedOnProgressRect: [self progressRectForBounds: bounds] inBounds: bounds]; NSString * string = [[self representedObject] statusString];
return [self rectForStatusBasedOnProgressRect: [self progressRectForBounds: bounds]
withString: [self attributedStatusString: string withColor: nil] inBounds: bounds];
} }
- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView - (void) drawInteriorWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
{ {
[super drawInteriorWithFrame: cellFrame inView: controlView];
Torrent * torrent = [self representedObject]; Torrent * torrent = [self representedObject];
BOOL minimal = [fDefaults boolForKey: @"SmallView"]; BOOL minimal = [fDefaults boolForKey: @"SmallView"];
@ -220,24 +220,29 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
} }
//minimal status //minimal status
NSRect minimalStatusRect = [self minimalStatusRectForBounds: cellFrame]; NSRect minimalStatusRect;
if (minimal) if (minimal)
{ {
NSString * string = ![fDefaults boolForKey: @"SmallStatusRegular"] && [torrent isActive] NSString * string = ![fDefaults boolForKey: @"SmallStatusRegular"] && [torrent isActive]
? [torrent remainingTimeString] : [torrent shortStatusString]; ? [torrent remainingTimeString] : [torrent shortStatusString];
[[self attributedStatusString: string withColor: statusColor] drawInRect: minimalStatusRect]; NSAttributedString * minimalString = [self attributedStatusString: string withColor: statusColor];
minimalStatusRect = [self rectForMinimalStatusRectWithString: minimalString inBounds: cellFrame];
[minimalString drawInRect: minimalStatusRect];
} }
//title //title
NSRect titleRect = [self rectForTitleBasedOnMinimalStatusRect: minimalStatusRect inBounds: cellFrame]; NSAttributedString * titleString = [self attributedTitleWithColor: titleColor];
[[self attributedTitleWithColor: titleColor] drawInRect: titleRect]; NSRect titleRect = [self rectForTitleBasedOnMinimalStatusRect: minimalStatusRect withString: titleString inBounds: cellFrame];
[titleString drawInRect: titleRect];
//progress //progress
NSRect progressRect; NSRect progressRect;
if (!minimal) if (!minimal)
{ {
progressRect = [self rectForProgressBasedOnTitleRect: titleRect inBounds: cellFrame]; NSAttributedString * progressString = [self attributedStatusString: [torrent progressString] withColor: statusColor];
[[self attributedStatusString: [torrent progressString] withColor: statusColor] drawInRect: progressRect]; progressRect = [self rectForProgressBasedOnTitleRect: titleRect withString: progressString inBounds: cellFrame];
[progressString drawInRect: progressRect];
} }
//bar //bar
@ -248,8 +253,9 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
//status //status
if (!minimal) if (!minimal)
{ {
NSRect statusRect = [self rectForStatusBasedOnProgressRect: progressRect inBounds: cellFrame]; NSAttributedString * statusString = [self attributedStatusString: [torrent statusString] withColor: statusColor];
[[self attributedStatusString: [torrent statusString] withColor: statusColor] drawInRect: statusRect]; NSRect statusRect = [self rectForStatusBasedOnProgressRect: progressRect withString: statusString inBounds: cellFrame];
[statusString drawInRect: statusRect];
} }
} }
@ -523,31 +529,45 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
return [bar autorelease]; return [bar autorelease];
} }
- (NSRect) rectForTitleBasedOnMinimalStatusRect: (NSRect) statusRect inBounds: (NSRect) bounds - (NSRect) rectForMinimalStatusRectWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
{
if (![fDefaults boolForKey: @"SmallView"])
return NSZeroRect;
NSRect result = bounds;
result.size = [string size];
result.origin.x += bounds.size.width - result.size.width - PADDING_HORIZONAL;
result.origin.y += PADDING_ABOVE_MIN_STATUS;
return result;
}
- (NSRect) rectForTitleBasedOnMinimalStatusRect: (NSRect) statusRect withString: (NSAttributedString *) string
inBounds: (NSRect) bounds
{ {
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;
result.size = [[self attributedTitleWithColor: nil] size]; result.size = [string size];
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL
- (minimal ? PADDING_BETWEEN_TITLE_AND_MIN_STATUS + statusRect.size.width : 0)); - (minimal ? PADDING_BETWEEN_TITLE_AND_MIN_STATUS + statusRect.size.width : 0));
return result; return result;
} }
- (NSRect) rectForProgressBasedOnTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds - (NSRect) rectForProgressBasedOnTitleRect: (NSRect) titleRect withString: (NSAttributedString *) string inBounds: (NSRect) bounds
{ {
if ([fDefaults boolForKey: @"SmallView"]) if ([fDefaults boolForKey: @"SmallView"])
return NSZeroRect; return NSZeroRect;
NSSize progressSize = [[self attributedStatusString: [[self representedObject] progressString] withColor: nil] size]; NSSize progressSize = [string size];
NSRect result = titleRect; NSRect result = titleRect;
result.size.width = MIN(progressSize.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL); result.size.width = MIN(progressSize.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
result.origin.y += titleRect.size.height + PADDING_BETWEEN_TITLE_AND_PROGRESS; result.origin.y += titleRect.size.height + PADDING_BETWEEN_TITLE_AND_PROGRESS;
@ -569,12 +589,13 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
return result; return result;
} }
- (NSRect) rectForStatusBasedOnProgressRect: (NSRect) progressRect inBounds: (NSRect) bounds - (NSRect) rectForStatusBasedOnProgressRect: (NSRect) progressRect withString: (NSAttributedString *) string
inBounds: (NSRect) bounds
{ {
if ([fDefaults boolForKey: @"SmallView"]) if ([fDefaults boolForKey: @"SmallView"])
return NSZeroRect; return NSZeroRect;
NSSize statusSize = [[self attributedStatusString: [[self representedObject] statusString] withColor: nil] size]; NSSize statusSize = [string size];
NSRect result = progressRect; NSRect result = progressRect;
result.size.width = MIN(statusSize.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL); result.size.width = MIN(statusSize.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);