Skip to content

Commit

Permalink
Move drawing and mouse handling to controls.
Browse files Browse the repository at this point in the history
Use separate NSButtons for the gear menu and the clicking on the whole view. This saves us the effort of handling the mouse movements. Splitting things up also separates the drawing more clearly and should be a first step towards making the plug-in accessible (as controls provide most of the AX stuff out of the box).
  • Loading branch information
ssp committed Oct 11, 2009
1 parent ea59ac5 commit 4fa5e84
Show file tree
Hide file tree
Showing 7 changed files with 746 additions and 476 deletions.
12 changes: 12 additions & 0 deletions ClickToFlash.xcodeproj/project.pbxproj
Expand Up @@ -45,6 +45,8 @@
6953E4360F3EDE9D0014ECF7 /* CTFMenubarMenuController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6953E4340F3EDE9D0014ECF7 /* CTFMenubarMenuController.m */; };
6953E43C0F3EDEB50014ECF7 /* MenubarMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6953E43A0F3EDEB50014ECF7 /* MenubarMenu.xib */; };
69A26D0C0F302C10006648BC /* NSBezierPath-RoundedRectangle.m in Sources */ = {isa = PBXBuildFile; fileRef = 69A26D0B0F302C10006648BC /* NSBezierPath-RoundedRectangle.m */; };
6C09630C107FCA610006923A /* CTFActionButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C09630B107FCA610006923A /* CTFActionButton.m */; };
6C09648010808D4B0006923A /* CTFMainButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C09647F10808D4B0006923A /* CTFMainButton.m */; };
6C2C5A6A1068CE8700A90A54 /* Credits.css in Resources */ = {isa = PBXBuildFile; fileRef = A40485B110629B0E00FDC4E2 /* Credits.css */; };
6C436DAF107830A200A0D525 /* QTKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6C436DAE107830A200A0D525 /* QTKit.framework */; };
6C8E03FF10795EB900E21A50 /* CTFLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C8E03FE10795EB900E21A50 /* CTFLoader.m */; };
Expand Down Expand Up @@ -154,6 +156,10 @@
6953E43B0F3EDEB50014ECF7 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = Plugin/English.lproj/MenubarMenu.xib; 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>"; };
6C09630A107FCA610006923A /* CTFActionButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CTFActionButton.h; path = Plugin/CTFActionButton.h; sourceTree = "<group>"; };
6C09630B107FCA610006923A /* CTFActionButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CTFActionButton.m; path = Plugin/CTFActionButton.m; sourceTree = "<group>"; };
6C09647E10808D4B0006923A /* CTFMainButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CTFMainButton.h; path = Plugin/CTFMainButton.h; sourceTree = "<group>"; };
6C09647F10808D4B0006923A /* CTFMainButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CTFMainButton.m; path = Plugin/CTFMainButton.m; sourceTree = "<group>"; };
6C436DAE107830A200A0D525 /* QTKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QTKit.framework; path = /System/Library/Frameworks/QTKit.framework; sourceTree = "<absolute>"; };
6C8E03FD10795EB900E21A50 /* CTFLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CTFLoader.h; path = Plugin/CTFLoader.h; sourceTree = "<group>"; };
6C8E03FE10795EB900E21A50 /* CTFLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CTFLoader.m; path = Plugin/CTFLoader.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -246,6 +252,10 @@
children = (
55EB703D0E04A84F0016593D /* Plugin.m */,
55EB703C0E04A84F0016593D /* Plugin.h */,
6C09647F10808D4B0006923A /* CTFMainButton.m */,
6C09647E10808D4B0006923A /* CTFMainButton.h */,
6C09630B107FCA610006923A /* CTFActionButton.m */,
6C09630A107FCA610006923A /* CTFActionButton.h */,
6C8E03FE10795EB900E21A50 /* CTFLoader.m */,
6C8E03FD10795EB900E21A50 /* CTFLoader.h */,
6C8EC6F310764F810053587F /* CTFKiller.m */,
Expand Down Expand Up @@ -515,6 +525,8 @@
6C8EC7E9107686780053587F /* CTFKillerSIFR.m in Sources */,
6CC18AE61076D09B00D9E1A0 /* CTFKillerVimeo.m in Sources */,
6C8E03FF10795EB900E21A50 /* CTFLoader.m in Sources */,
6C09630C107FCA610006923A /* CTFActionButton.m in Sources */,
6C09648010808D4B0006923A /* CTFMainButton.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
28 changes: 28 additions & 0 deletions Plugin/CTFActionButton.h
@@ -0,0 +1,28 @@
//
// CTFActionButton.h
// ClickToFlash
//
// Created by Sven on 09.10.09.
// Copyright 2009 earthlingsoft. All rights reserved.
//

#import <Cocoa/Cocoa.h>




@interface CTFActionButton : NSButton {
}

+ (id) actionButton;

@end


@interface CTFActionButtonCell : NSButtonCell {

}

- (BOOL) gearVisible;

@end
156 changes: 156 additions & 0 deletions Plugin/CTFActionButton.m
@@ -0,0 +1,156 @@
//
// CTFActionButton.m
// ClickToFlash
//
// Created by Sven on 09.10.09.
// Copyright 2009 earthlingsoft. All rights reserved.
//

#import "CTFActionButton.h"
#import "CTFUtilities.h"

static CGFloat padding = 3.;
static CGFloat leftMargin = 5.;
static CGFloat backgroundOpacity = .8;


@implementation CTFActionButton

+ (id) actionButton {
CGFloat margin = 5.;
CGFloat size = 20.;
NSRect gearButtonRect = NSMakeRect( .0, .0, size + 2.*margin , size + 2.*margin );

CTFActionButton * gearButton = [[[CTFActionButton alloc] initWithFrame: gearButtonRect] autorelease];
[gearButton setButtonType: NSMomentaryPushInButton];

return gearButton;
}



#pragma mark NSButton subclassing

+ (Class) cellClass {
return NSClassFromString(@"CTFActionButtonCell");
}



- (void) mouseDown: (NSEvent *) event {
[NSMenu popUpContextMenu:[self menuForEvent:event] withEvent:event forView:self];
}



- (NSMenu*) menuForEvent: (NSEvent*) event {
return [[self superview] menuForEvent: event];
}



- (void) resizeWithOldSuperviewSize:(NSSize) oldBoundsSize {
NSPoint newOrigin;

if ( [[self cell ] gearVisible] ) {
NSSize superSize = [[self superview] bounds].size;
NSRect myRect = [self bounds];
newOrigin = NSMakePoint(myRect.origin.x, superSize.height - myRect.size.height);
}
else {
newOrigin = NSMakePoint( -1000. , -1000. );
}

[self setFrameOrigin: newOrigin];
}


@end






#pragma mark -
#pragma mark NSView subclassing



@implementation CTFActionButtonCell

#pragma mark NSCell subclassing

- (void) drawWithFrame: (NSRect) rect inView:(NSView *) controlView {
NSRect bounds = [[self controlView] bounds];

NSImage * gearImage = [NSImage imageNamed:@"NSActionTemplate"];
// On systems older than 10.5 we need to supply our own image.
if (gearImage == nil) {
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"NSActionTemplate" ofType:@"png"];
gearImage = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
}

if( gearImage ) {
CGFloat gearSize = [gearImage size].width; // assumes the gear to be square
CGFloat size = gearSize + 2.0 * padding;
CGFloat x = round(bounds.size.width * .5) - round(size * .5);
CGFloat y = round(bounds.size.height * .5 ) - round(size * .5);
NSRect backgroundFrame = NSMakeRect(x, y, size, size);

NSBezierPath * circle = [NSBezierPath bezierPathWithOvalInRect:backgroundFrame];
CGFloat alpha = ( [self isHighlighted] ) ? .9 : .7 ;
[[NSColor colorWithDeviceWhite:1.0 alpha:alpha] set];
[circle fill];

// draw the gear image
[gearImage drawAtPoint:NSMakePoint(x + padding, y + padding)
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:.9];
}
}




#pragma mark -
#pragma mark Helper

- (BOOL) gearVisible {
NSRect bounds = [[[self controlView] superview] bounds ];
return NSWidth( bounds ) > 32 && NSHeight( bounds ) > 32;
}





#pragma mark -
#pragma mark Accessibility

- (NSArray *) accessibilityAttributeNames {
NSMutableArray * attributes = [[[super accessibilityAttributeNames] mutableCopy] autorelease];
[attributes addObject: NSAccessibilityDescriptionAttribute];
return attributes;
}



- (id) accessibilityAttributeValue: (NSString *) attribute {
id value;

if ( [attribute isEqualToString: NSAccessibilityDescriptionAttribute] ) {
value = CtFLocalizedString( @"ClickTo Flash Contextual menu", @"Accessibility: CTFActionButton, Title of Contextual Menu");
}
else if ( [attribute isEqualToString: NSAccessibilityParentAttribute] ){
value = NSAccessibilityUnignoredAncestor([[self controlView] superview]);
}
else {
value = [super accessibilityAttributeValue:attribute];
}
return value;
}


@end
33 changes: 33 additions & 0 deletions Plugin/CTFMainButton.h
@@ -0,0 +1,33 @@
//
// CTFMainButton.h
// ClickToFlash
//
// Created by Sven on 10.10.09.
// Copyright 2009 earthlingsoft. All rights reserved.
//

#import <Cocoa/Cocoa.h>


@interface CTFMainButton : NSButton {

}

@end



#pragma mark -

@interface CTFMainButtonCell : NSButtonCell {

}

- (void) drawGradientInRect: (NSRect) rect;
- (void) drawPreviewInRect: (NSRect) rect;
- (void) drawBadgeForBounds: (NSRect) bounds;
- (void) drawGlossForBounds: (NSRect) bounds;

- (NSString*) badgeLabelText;

@end

0 comments on commit 4fa5e84

Please sign in to comment.