This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
darwin.m
97 lines (73 loc) · 2.93 KB
/
darwin.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#import "_cgo_export.h"
#import "darwin.h"
NSAutoreleasePool* appPool = nil;
@implementation InteropScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message {
scriptMessageCallback((char *) [message.body description].UTF8String);
}
@end
webview webview_create() {
NSWindow* window;
NSRect frame;
appPool = [[NSAutoreleasePool alloc] init];
frame = NSMakeRect(0, 0, 480, 640);
window = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSWindowStyleMaskTitled
|NSWindowStyleMaskClosable
|NSWindowStyleMaskMiniaturizable
backing:NSBackingStoreBuffered
defer:NO];
[window setTitle:@"WebView"];
[window center];
WKWebViewConfiguration* config;
id<WKScriptMessageHandler> messageHandler;
WKWebView* view;
config = [[[WKWebViewConfiguration alloc] init] autorelease];
messageHandler = [[[InteropScriptMessageHandler alloc] init] autorelease];
[config.userContentController addScriptMessageHandler:messageHandler name:@"webview"];
[config.preferences setValue:@YES forKey:@"developerExtrasEnabled"];
view = [[[WKWebView alloc] initWithFrame:frame configuration:config] autorelease];
[window setContentView:view];
[window makeKeyAndOrderFront:nil];
webview v = malloc(sizeof(webview));
v->view = view;
return v;
}
void webview_load(webview v, const char* url) {
NSString* str = [NSString stringWithUTF8String:url];
NSURL* nsurl = [NSURL URLWithString:str];
NSURLRequest* req = [NSURLRequest requestWithURL:nsurl];
[v->view loadRequest:req];
}
void webview_add_user_script(webview v, const char* source) {
NSString* str = [NSString stringWithUTF8String:source];
WKUserScript* script = [[WKUserScript alloc] initWithSource:str
injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
[v->view.configuration.userContentController addUserScript:script];
}
void webview_add_user_style(webview v, const char* source) {
}
void webview_eval(webview v, const char* source) {
NSString *str = [NSString stringWithUTF8String:source];
[v->view evaluateJavaScript:str completionHandler:nil];
}
void webview_close(webview v) {
[v->view.window close];
}
void _webview_dispatch_cb(_webview_dispatch_args args) {
dispatchCallback(args->id);
free(args);
}
void _webview_dispatch(int id) {
_webview_dispatch_args args = malloc(sizeof(_webview_dispatch_args));
args->id = id;
dispatch_async_f(dispatch_get_main_queue(), (void *) args, (dispatch_function_t) _webview_dispatch_cb);
}
void webview_loop() {
[NSApp run];
[NSApp release];
[appPool release];
}