Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Embedding WebKit using Racket Objective-C FFI
#lang racket/gui
(require framework)
(require ffi/unsafe)
(require ffi/unsafe/objc)
(ffi-lib "/System/Library/Frameworks/WebKit.framework/WebKit")
(import-class WebView)
(import-class NSURLRequest)
(import-class NSURL)
(import-class NSString)
(import-class NSObject)
(define-objc-class MyWebFrameLoadDelegate NSObject
(- _void (webView: [_id wv] didFinishLoadForFrame: [_id wf])
(send frame set-status-text "Page Loaded")))
(define my-frame%
(class frame% (super-new)
(inherit set-status-text get-width get-height get-client-handle)
(define/override (on-size width height)
(set-status-text (~a "Size: (" (get-width) (get-height) ")" #:separator " "))
(define frame
(new my-frame%
[label "Webkit Testbed"]
[width 1000]
[height 800]
[x 100]
[y 100]))
(send frame create-status-line)
(send frame set-status-text "Hello World")
(send frame show #t)
(define-cstruct _NSPoint ([x _double*]
[y _double*]))
(define-cstruct _NSSize ([width _double*]
[height _double*]))
(define-cstruct _NSRect ([origin _NSPoint]
[size _NSSize]))
(define webview
(tell (tell WebView alloc)
initWithFrame: #:type _NSRect (make-NSRect (make-NSPoint 50 50) (make-NSSize 800 700))
frameName: #f
groupName: #f))
(define client-view (send frame get-client-handle))
(tell client-view addSubview: webview)
(define (release id-ptr) (tell id-ptr release))
(let* ([url-string (tell (tell NSString alloc)
initWithUTF8String: #:type _string "")]
[url (tell NSURL URLWithString: url-string)]
[req (tell NSURLRequest requestWithURL: url)]
[main-frame (tell webview mainFrame)]
[delegate (tell (tell MyWebFrameLoadDelegate alloc) init)]
(print delegate)
(tell webview setFrameLoadDelegate: delegate)
(tell main-frame loadRequest: req)
(release url-string))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment