Last active
August 6, 2019 13:23
-
-
Save cody271/6b4d83ca0ceb55f98a387c5630abf22f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <WebKit/WebKit.h> | |
#include "ui.h" | |
#include "ui_darwin.h" | |
#define uiWebViewSignature 0x57656276 // "Webv" | |
typedef struct uiWebView uiWebView; | |
#define uiWebView(this) ((uiWebView *) (this)) | |
struct uiWebView { | |
uiDarwinControl c; | |
WKWebView *webview; | |
}; | |
uiDarwinControlAllDefaultsExceptDestroy(uiWebView, webview) | |
static void uiWebViewDestroy(uiControl *c) | |
{ | |
uiWebView *w = uiWebView(c); | |
[w->webview release]; | |
uiFreeControl(uiControl(w)); | |
} | |
uiWebView *uiNewWebView() | |
{ | |
uiWebView *w; | |
WKWebViewConfiguration *config; | |
uiDarwinNewControl(uiWebView, w); | |
config = [[WKWebViewConfiguration alloc] init]; | |
w->webview = [[WKWebView alloc] initWithFrame:NSZeroRect configuration:config]; | |
return w; | |
} | |
void uiWebViewLoad(uiWebView *w, const char *url) | |
{ | |
NSString *nsstr; | |
NSURL *nsurl; | |
nsstr = [NSString stringWithUTF8String:url]; | |
if (![nsstr hasPrefix:@"http://"]) | |
nsstr = [NSString stringWithFormat:@"http://%@", nsstr]; | |
nsurl = [NSURL URLWithString:nsstr]; | |
[w->webview loadRequest:[NSURLRequest requestWithURL:nsurl]]; | |
} | |
uiEntry *entry; | |
uiWebView *webview; | |
static void onLoad(uiButton *b, void *data) | |
{ | |
uiWebViewLoad(webview, uiEntryText(entry)); | |
} | |
static int onClosing(uiWindow *w, void *data) | |
{ | |
uiControlDestroy(uiControl(w)); | |
uiQuit(); | |
return 0; | |
} | |
int main() | |
{ | |
uiInitOptions o; | |
const char *err; | |
uiWindow *win; | |
uiBox *vbox, *hbox; | |
uiButton *load; | |
memset(&o, 0, sizeof(uiInitOptions)); | |
err = uiInit(&o); | |
if (err != NULL) { | |
fprintf(stderr, "error initializing ui: %s\n", err); | |
uiFreeInitError(err); | |
return 1; | |
} | |
win = uiNewWindow("WebView", 640, 480, 1); | |
vbox = uiNewVerticalBox(); | |
hbox = uiNewHorizontalBox(); | |
entry = uiNewEntry(); | |
load = uiNewButton("Load"); | |
webview = uiNewWebView(); | |
uiBoxAppend(hbox, uiControl(entry), 1); | |
uiBoxAppend(hbox, uiControl(load), 0); | |
uiButtonOnClicked(load, onLoad, NULL); | |
uiWindowSetChild(win, uiControl(vbox)); | |
uiBoxAppend(vbox, uiControl(hbox), 0); | |
uiBoxAppend(vbox, uiControl(webview), 1); | |
uiWindowOnClosing(win, onClosing, NULL); | |
uiControlShow(uiControl(win)); | |
uiMain(); | |
uiUninit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment