public
Created

Embedding WebKit using Racket Objective-C FFI

  • Download Gist
wktest.rkt
Racket
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
#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 "http://news.ycombinator.com")]
[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))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.