Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@KevinGutowski
Created August 24, 2019 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinGutowski/af897f78753e7f850cce144cb3a1ce26 to your computer and use it in GitHub Desktop.
Save KevinGutowski/af897f78753e7f850cce144cb3a1ce26 to your computer and use it in GitHub Desktop.
//
// 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