Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// HSMain.m
// HelloSketch
//
// Created by Kevin Gutowski on 8/17/19.
// Copyright © 2019 Kevin. All rights reserved.
//
#import "HSMain.h"
@import AppKit;
@import JavaScriptCore;
// __attribute__((weak_import)) means "hey compiler, don't expect this class to be defined in this framework, it's not"
__attribute__((weak_import)) @interface MOJavaScriptObject : NSObject
@property (readonly) JSObjectRef JSObject;
@property (readonly) JSContextRef JSContext;
@end
// Declare a helper function we can use to call it easily
JSValue* callFunctionWithArguments(MOJavaScriptObject* boxedFunction, NSArray* argumentsArray) {
JSContext *context = [JSContext contextWithJSGlobalContextRef:(JSGlobalContextRef)boxedFunction.JSContext];
JSValue *function = [JSValue valueWithJSValueRef:boxedFunction.JSObject inContext:context];
return [function callWithArguments:argumentsArray];
}
@implementation HSMain {
MOJavaScriptObject *_jsCallback;
}
- (NSString *)helloText {
NSLog(@"HelloSketch (Sketch Plugin)");
return @"Hiya, sending singal from HSMain";
}
- (void)awakeFromNib {
[slider setIntValue:25];
[inputField setIntValue:[slider intValue]];
[stepper setIntValue:[slider intValue]];
}
- (IBAction)sliderChange:(id)sender {
[inputField setIntValue:[slider intValue]];
[stepper setIntValue:[slider intValue]];
}
- (IBAction)inputChange:(id)sender {
[slider setIntValue:[inputField intValue]];
[stepper setIntValue:[inputField intValue]];
}
- (IBAction)stepperChange:(id)sender {
[slider setIntValue:[stepper intValue]];
[inputField setIntValue:[stepper intValue]];
}
- (id)loadNibFile {
NSArray *topLevelObjects;
NSBundle *myBundle = [NSBundle bundleForClass:[HSMain class]];
if (! [myBundle loadNibNamed:@"HSPanel" owner:self topLevelObjects:&topLevelObjects])
{
NSLog(@"Warning! Could not load the nib file.\n");
return NULL;
} else {
return topLevelObjects;
}
}
- (IBAction)clickHello:(id)sender {
if (!_jsCallback) return;
callFunctionWithArguments(_jsCallback, @[sender]);
}
- (void)setCallbackButtonClick:(MOJavaScriptObject*)function {
_jsCallback = function;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.