From f72b93a3a716bfc72bbc26088cdc923ff6f31780 Mon Sep 17 00:00:00 2001 From: Mitchell Livingston Date: Sat, 19 May 2007 14:39:06 +0000 Subject: [PATCH] create the drag badge's background only once --- macosx/DragOverlayView.h | 2 +- macosx/DragOverlayView.m | 61 ++++++++++++++++++++++---------------- macosx/DragOverlayWindow.m | 6 ++-- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/macosx/DragOverlayView.h b/macosx/DragOverlayView.h index 0ce9d7de7..1930f0129 100644 --- a/macosx/DragOverlayView.h +++ b/macosx/DragOverlayView.h @@ -26,7 +26,7 @@ @interface DragOverlayView : NSView { - NSImage * fImage, * fAppIcon; + NSImage * fBadge, * fBackBadge, * fAppIcon; NSDictionary * fMainLineAttributes, * fSubLineAttributes; } diff --git a/macosx/DragOverlayView.m b/macosx/DragOverlayView.m index 893584dac..9619aaf41 100644 --- a/macosx/DragOverlayView.m +++ b/macosx/DragOverlayView.m @@ -30,6 +30,21 @@ { if ((self = [super initWithFrame: frame])) { + //create badge + fBackBadge = [[NSImage alloc] initWithSize: NSMakeSize(300.0, 84.0)]; + [fBackBadge lockFocus]; + + NSRect badgeRect = NSMakeRect(0, 0, 300.0, 80.0); + [[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.75] set]; + [NSBezierPath fillRect: badgeRect]; + + [[NSColor whiteColor] set]; + [NSBezierPath setDefaultLineWidth: 3.0]; + [NSBezierPath strokeRect: badgeRect]; + + [fBackBadge unlockFocus]; + + //create attributes NSShadow * stringShadow = [[NSShadow alloc] init]; [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)]; [stringShadow setShadowBlurRadius: 4.0]; @@ -58,8 +73,10 @@ - (void) dealloc { - if (fImage) - [fImage release]; + [fBackBadge release]; + + if (fBadge) + [fBadge release]; if (fAppIcon) [fAppIcon release]; @@ -71,6 +88,12 @@ - (void) setOverlay: (NSImage *) icon mainLine: (NSString *) mainLine subLine: (NSString *) subLine { + if (fBadge) + [fBadge release]; + fBadge = [fBackBadge copy]; + NSSize badgeSize = [fBadge size]; + + //get icon NSSize iconSize = NSMakeSize(64.0, 64.0); if (!icon) { @@ -89,26 +112,12 @@ [icon setSize: iconSize]; } - if (fImage) - [fImage release]; - fImage = [[NSImage alloc] initWithSize: NSMakeSize(300.0, 84.0)]; - - [fImage lockFocus]; - - //create oval - #warning don't do each time - NSRect badgeRect = NSMakeRect(0, 0, 300.0, 80.0); - [[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.75] set]; - [NSBezierPath fillRect: badgeRect]; - - [[NSColor whiteColor] set]; - [NSBezierPath setDefaultLineWidth: 3.0]; - [NSBezierPath strokeRect: badgeRect]; - float padding = 10.0; + [fBadge lockFocus]; + //place icon - [icon compositeToPoint: NSMakePoint(padding, (badgeRect.size.height - iconSize.height) * 0.5) + [icon compositeToPoint: NSMakePoint(padding, (badgeSize.height - iconSize.height) * 0.5) operation: NSCompositeSourceOver]; //place main text @@ -116,8 +125,8 @@ NSSize subLineSize = [subLine sizeWithAttributes: fSubLineAttributes]; NSRect lineRect = NSMakeRect(padding + iconSize.width + 5.0, - (badgeRect.size.height + (subLineSize.height + 2.0 - mainLineSize.height)) * 0.5, - badgeRect.size.width - (padding + iconSize.width + 2.0) - padding, mainLineSize.height); + (badgeSize.height + (subLineSize.height + 2.0 - mainLineSize.height)) * 0.5, + badgeSize.width - (padding + iconSize.width + 2.0) - padding, mainLineSize.height); [mainLine drawInRect: lineRect withAttributes: fMainLineAttributes]; //place sub text @@ -125,20 +134,20 @@ lineRect.size.height = subLineSize.height; [subLine drawInRect: lineRect withAttributes: fSubLineAttributes]; - [fImage unlockFocus]; + [fBadge unlockFocus]; [icon release]; - [self setNeedsDisplay:YES]; + [self setNeedsDisplay: YES]; } -(void) drawRect: (NSRect) rect { - if (fImage) + if (fBadge) { NSRect frame = [self frame]; - NSSize imageSize = [fImage size]; - [fImage compositeToPoint: NSMakePoint((frame.size.width - imageSize.width) * 0.5, + NSSize imageSize = [fBadge size]; + [fBadge compositeToPoint: NSMakePoint((frame.size.width - imageSize.width) * 0.5, (frame.size.height - imageSize.height) * 0.5) operation: NSCompositeSourceOver]; } } diff --git a/macosx/DragOverlayWindow.m b/macosx/DragOverlayWindow.m index 36b006605..6a20b67f0 100644 --- a/macosx/DragOverlayWindow.m +++ b/macosx/DragOverlayWindow.m @@ -41,9 +41,9 @@ [self setHasShadow: NO]; [self setLevel: NSStatusWindowLevel]; - DragOverlayView * backgroundView = [[DragOverlayView alloc] initWithFrame: [self frame]]; - [self setContentView: backgroundView]; - [backgroundView release]; + DragOverlayView * view = [[DragOverlayView alloc] initWithFrame: [self frame]]; + [self setContentView: view]; + [view release]; [self setReleasedWhenClosed: NO]; [self setIgnoresMouseEvents: YES];