Skip to content

Commit

Permalink
[NEW] Migrate from PNG-based badge to vector drawing-based badge. (Tr…
Browse files Browse the repository at this point in the history
…oy Gaul)
  • Loading branch information
rentzsch committed Jan 31, 2009
1 parent 1956458 commit 4a6bf30
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ClickToFlash.xcodeproj/project.pbxproj
Expand Up @@ -34,6 +34,7 @@
55EB70480E04A8850016593D /* Plugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 55EB703D0E04A84F0016593D /* Plugin.m */; };
55EB70580E04A8B80016593D /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 55EB70560E04A8B80016593D /* Cocoa.framework */; };
55EB70590E04A8B80016593D /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 55EB70570E04A8B80016593D /* WebKit.framework */; };
69A26D0C0F302C10006648BC /* NSBezierPath-RoundedRectangle.m in Sources */ = {isa = PBXBuildFile; fileRef = 69A26D0B0F302C10006648BC /* NSBezierPath-RoundedRectangle.m */; };
79A21C4D0F30B735009424B8 /* CTFWhitelistWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 79A21C490F30B735009424B8 /* CTFWhitelistWindowController.m */; };
D0FB700F0F2FE35000DCB854 /* ContainerImage.png in Resources */ = {isa = PBXBuildFile; fileRef = D0FB700E0F2FE35000DCB854 /* ContainerImage.png */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -77,6 +78,8 @@
55EB70560E04A8B80016593D /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
55EB70570E04A8B80016593D /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = /System/Library/Frameworks/WebKit.framework; sourceTree = "<absolute>"; };
55EB70610E04A8D00016593D /* Info-Plugin.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "Info-Plugin.plist"; path = "Plugin/Info-Plugin.plist"; sourceTree = "<group>"; };
69A26D0A0F302C10006648BC /* NSBezierPath-RoundedRectangle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSBezierPath-RoundedRectangle.h"; path = "Plugin/NSBezierPath-RoundedRectangle.h"; sourceTree = "<group>"; };
69A26D0B0F302C10006648BC /* NSBezierPath-RoundedRectangle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSBezierPath-RoundedRectangle.m"; path = "Plugin/NSBezierPath-RoundedRectangle.m"; sourceTree = "<group>"; };
79A21C490F30B735009424B8 /* CTFWhitelistWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CTFWhitelistWindowController.m; path = Plugin/CTFWhitelistWindowController.m; sourceTree = "<group>"; };
79A21C4A0F30B735009424B8 /* CTFWhitelistWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CTFWhitelistWindowController.h; path = Plugin/CTFWhitelistWindowController.h; sourceTree = "<group>"; };
D0FB700E0F2FE35000DCB854 /* ContainerImage.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = ContainerImage.png; path = Images/ContainerImage.png; sourceTree = "<group>"; };
Expand Down Expand Up @@ -144,6 +147,8 @@
55EB70610E04A8D00016593D /* Info-Plugin.plist */,
55EB703C0E04A84F0016593D /* Plugin.h */,
55EB703D0E04A84F0016593D /* Plugin.m */,
69A26D0A0F302C10006648BC /* NSBezierPath-RoundedRectangle.h */,
69A26D0B0F302C10006648BC /* NSBezierPath-RoundedRectangle.m */,
072189BC0F30D9C3008C8944 /* WhitelistPanel.xib */,
);
name = Plugin;
Expand Down Expand Up @@ -269,6 +274,7 @@
buildActionMask = 2147483647;
files = (
558405550E0F2C2B00EA991B /* CTFInstaller.m in Sources */,
69A26D0C0F302C10006648BC /* NSBezierPath-RoundedRectangle.m in Sources */,
79A21C4D0F30B735009424B8 /* CTFWhitelistWindowController.m in Sources */,
55EB70480E04A8850016593D /* Plugin.m in Sources */,
);
Expand Down
13 changes: 13 additions & 0 deletions Plugin/NSBezierPath-RoundedRectangle.h
@@ -0,0 +1,13 @@
//
// NSBezierPath-RoundedRectangle.h
// Based on http://www.cocoadev.com/index.pl?RoundedRectangles
// Switched to a function instead of a category class method for use in a plug-in.
//

#import <Cocoa/Cocoa.h>

/**
Returns a closed bezier path describing a rectangle with curved corners
The corner radius will be trimmed to not exceed half of the lesser rectangle dimension.
*/
NSBezierPath* bezierPathWithRoundedRectCornerRadius( NSRect aRect, double radius );
53 changes: 53 additions & 0 deletions Plugin/NSBezierPath-RoundedRectangle.m
@@ -0,0 +1,53 @@
//
// NSBezierPath-RoundedRectangle.m
// Based on http://www.cocoadev.com/index.pl?RoundedRectangles
// Switched to a function instead of a category class method for use in a plug-in.
//

#import "NSBezierPath-RoundedRectangle.h"

NSBezierPath* bezierPathWithRoundedRectCornerRadius( NSRect aRect, double cRadius )
{
double left = aRect.origin.x, bottom = aRect.origin.y, width = aRect.size.width, height = aRect.size.height;

//now, crop the radius so we don't get weird effects
double lesserDim = width < height ? width : height;
if ( cRadius > lesserDim / 2 )
{
cRadius = lesserDim / 2;
}

//these points describe the rectangle as start and stop points of the
//arcs making up its corners --points c, e, & g are implicit endpoints of arcs
//and are unnecessary
NSPoint a = NSMakePoint( 0, cRadius ), b = NSMakePoint( 0, height - cRadius ),
d = NSMakePoint( width - cRadius, height ), f = NSMakePoint( width, cRadius ),
h = NSMakePoint( cRadius, 0 );

//these points describe the center points of the corner arcs
NSPoint cA = NSMakePoint( cRadius, height - cRadius ),
cB = NSMakePoint( width - cRadius, height - cRadius ),
cC = NSMakePoint( width - cRadius, cRadius ),
cD = NSMakePoint( cRadius, cRadius );

//start
NSBezierPath *bp = [NSBezierPath bezierPath];
[bp moveToPoint: a ];
[bp lineToPoint: b ];
[bp appendBezierPathWithArcWithCenter: cA radius: cRadius startAngle:180 endAngle:90 clockwise: YES];
[bp lineToPoint: d ];
[bp appendBezierPathWithArcWithCenter: cB radius: cRadius startAngle:90 endAngle:0 clockwise: YES];
[bp lineToPoint: f ];
[bp appendBezierPathWithArcWithCenter: cC radius: cRadius startAngle:0 endAngle:270 clockwise: YES];
[bp lineToPoint: h ];
[bp appendBezierPathWithArcWithCenter: cD radius: cRadius startAngle:270 endAngle:180 clockwise: YES];
[bp closePath];

//Transform path to rectangle's origin
NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy: left yBy: bottom];
[bp transformUsingAffineTransform: transform];

return bp; //it's already been autoreleased
}

82 changes: 80 additions & 2 deletions Plugin/Plugin.m
Expand Up @@ -26,6 +26,7 @@ of this software and associated documentation files (the "Software"), to deal


#import "Plugin.h"
#import "NSBezierPath-RoundedRectangle.h"
#import "CTFWhitelistWindowController.h"

static NSString *sFlashOldMIMEType = @"application/x-shockwave-flash";
Expand Down Expand Up @@ -300,6 +301,78 @@ - (IBAction)loadFlash:(id)sender;
#pragma mark -
#pragma mark Drawing

- (void) _drawBadge
{
// What and how are we going to draw?

const float kFrameXInset = 10;
const float kFrameYInset = 4;
const float kMinMargin = 11.5;
const float kMinHeight = 6;

NSString* str = NSLocalizedString( @"Flash", @"Flash" );

NSColor* badgeColor = [ NSColor colorWithCalibratedWhite: 0.0 alpha: 0.25 ];

NSDictionary* attrs = [ NSDictionary dictionaryWithObjectsAndKeys:
[ NSFont boldSystemFontOfSize: 20 ], NSFontAttributeName,
[ NSNumber numberWithInt: -1 ], NSKernAttributeName,
badgeColor, NSForegroundColorAttributeName,
nil ];

// Set up for drawing.

NSRect bounds = [ self bounds ];

// How large would this text be?

NSSize strSize = [ str sizeWithAttributes: attrs ];

float w = strSize.width + kFrameXInset * 2;
float h = strSize.height + kFrameYInset * 2;

// Compute a scale factor based on the view's size.

float maxW = NSWidth( bounds ) - kMinMargin;
float maxH = NSHeight( bounds ) - kMinMargin;

if( maxW <= kMinHeight * w / h || maxH <= kMinHeight )
return; // nothing to draw

float scaleFactor = 1.0;

if( maxW < w )
scaleFactor = maxW / w;

if( maxH < h && maxH / h < scaleFactor )
scaleFactor = maxH / h;

// Apply the scale, and a transform so the result is centered in the view.

[ NSGraphicsContext saveGraphicsState ];

NSAffineTransform* xform = [ NSAffineTransform transform ];
[ xform translateXBy: NSWidth( bounds ) / 2 yBy: NSHeight( bounds ) / 2 ];
[ xform scaleBy: scaleFactor ];
[ xform concat ];

// Draw everything at full size, centered on the origin.

NSPoint loc = { -strSize.width / 2, -strSize.height / 2 };
NSRect borderRect = NSMakeRect( loc.x - kFrameXInset, loc.y - kFrameYInset, w, h );

[ str drawAtPoint: loc withAttributes: attrs ];

NSBezierPath* path = bezierPathWithRoundedRectCornerRadius( borderRect, 4 );
[ badgeColor set ];
[ path setLineWidth: 3 ];
[ path stroke ];

// Now restore the graphics state:

[ NSGraphicsContext restoreGraphicsState ];
}

- (void) _drawBackground
{
NSRect selfBounds = [self bounds];
Expand All @@ -322,7 +395,8 @@ - (void) _drawBackground
[[NSBezierPath bezierPathWithRect:strokeRect] stroke];

// Draw an image on top to make it insanely obvious that this is clickable Flash.
NSString *containerImageName = [[NSBundle bundleForClass:[self class]] pathForResource:@"ContainerImage" ofType:@"png"];
/*
NSString *containerImageName = [[NSBundle bundleForClass:[self class]] pathForResource:@"ContainerImage" ofType:@"png"];
NSImage *containerImage = [[NSImage alloc] initWithContentsOfFile:containerImageName];
NSSize viewSize = fillRect.size;
Expand All @@ -342,9 +416,13 @@ - (void) _drawBackground
// Draw the image centered in the view
[containerImage drawInRect:destinationRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[containerImage release];
*/

[gradient release];
[containerImage release];

// Draw label
[ self _drawBadge ];
}


Expand Down

0 comments on commit 4a6bf30

Please sign in to comment.