Skip to content

Commit

Permalink
Tweaked some stuff a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccgus committed Nov 12, 2013
1 parent fa212c4 commit 1200d74
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 61 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ var userClickedOK = false
[okButton sizeToFit]
[okButton setFrame:NSMakeRect([window frame].size.width - [okButton frame].size.width - 20, 14, [okButton frame].size.width, [okButton frame].size.height)]
[okButton setKeyEquivalent:"\r"] // return key
[okButton setTarget:[COSTarget targetWithAction:function(sender) {
[okButton setCOSTarget:[COSTarget targetWithJSFunction:function(sender) {
userClickedOK = true
[window orderOut:nil]
[NSApp stopModal]
}]]
[okButton setAction:"callAction:"]
}]];

[[window contentView] addSubview:okButton]

// create cancel button
Expand All @@ -56,11 +56,11 @@ var userClickedOK = false
[cancelButton sizeToFit]
[cancelButton setFrame:NSMakeRect([okButton frame].origin.x - [cancelButton frame].size.width, 14, [cancelButton frame].size.width, [cancelButton frame].size.height)]
[cancelButton setKeyEquivalent:@"\033"] // escape key
[cancelButton setTarget:[COSTarget targetWithAction:function(sender) {
[cancelButton setCOSTarget:[COSTarget targetWithJSFunction:function(sender) {
[window orderOut:nil]
[NSApp stopModal]
}]]
[cancelButton setAction:"callAction:"]

[[window contentView] addSubview:cancelButton]

// get the user input
Expand Down
15 changes: 12 additions & 3 deletions src/framework/COSTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@

@interface COSTarget : NSObject

@property (strong) MOJavaScriptObject *action;
@property (strong) MOJavaScriptObject *jsFunction;
@property NSUInteger callCount;

+ (instancetype)targetWithAction:(MOJavaScriptObject *)action;
+ (instancetype)targetWithJSFunction:(MOJavaScriptObject *)jsFunction;

- (instancetype)initWithAction:(MOJavaScriptObject *)action;
- (instancetype)initWithJSFunction:(MOJavaScriptObject *)jsFunction;

- (void)callAction:(id)sender;

- (SEL)action;

@end

@interface NSObject (COSTargetAdditions)

- (void)setCOSTarget:(COSTarget*)target;

@end

43 changes: 31 additions & 12 deletions src/framework/COSTarget.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,46 @@

@implementation COSTarget

+ (instancetype)targetWithAction:(MOJavaScriptObject *)action
{
return [[[self class] alloc] initWithAction:action];
+ (instancetype)targetWithJSFunction:(MOJavaScriptObject *)jsFunction {
return [[[self class] alloc] initWithJSFunction:jsFunction];
}

- (instancetype)initWithAction:(MOJavaScriptObject *)action
{
if (!(self = [super init]))
return nil;

self.action = action;
- (instancetype)initWithJSFunction:(MOJavaScriptObject *)jsFunction {
self = [super init];
if (self != nil) {
[self setJsFunction:jsFunction];
}

return self;
}

- (void)callAction:(id)sender
{
JSObjectRef actionRef = [self.action JSObject];
- (void)callAction:(id)sender {
JSObjectRef actionRef = [[self jsFunction] JSObject];

#pragma message "FIXME: we should stash a weak ref to the COScript at creation time, because who knows what other COScript might be around"

COScript *script = [COScript currentCOScript];
[script callJSFunction:actionRef withArgumentsInArray:@[sender]];
}

- (SEL)action {
return @selector(callAction:);
}

@end


@implementation NSObject (COSTargetAdditions)

- (void)setCOSTarget:(COSTarget*)target {

if (!([self respondsToSelector:@selector(setTarget:)] && [self respondsToSelector:@selector(setAction:)])) {
NSLog(@"Could not set the target and action on %@", self);
return;
}

[(id)self setTarget:target];
[(id)self setAction:[target action]];
}

@end

0 comments on commit 1200d74

Please sign in to comment.