on master help sign in
Add run function for Mac OS
commita279a35208c08bf229d77570a92da5112f102595 authorHamish Mackenzie <Hamish.K.Mackenzie@googlemail.com> dateFri 23 Dec 2016, 15:30 (10 years ago) parent4a88ba0
4 files changed+88−9
jsaddle-wkwebview/cbits/Main.m+62 jsaddle-wkwebview/cbits/WKWebView.m+1−1 jsaddle-wkwebview/jsaddle-wkwebview.cabal+5−2 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView.hs+20−6
jsaddle-wkwebview/cbits/Main.mnew file+62
@@ -0,0 +1,62 @@
1+#include "HsFFI.h"
2+#import <Cocoa/Cocoa.h>
3+#import <WebKit/WebKit.h>
4+
5+extern void withWebView(WKWebView *, HsStablePtr);
6+
7+@interface AppDelegate : NSObject <NSApplicationDelegate>
8+@property (nonatomic, assign) IBOutlet NSWindow *window;
9+@property (nonatomic, assign) HsStablePtr handler;
10+
11+- (instancetype)initApp:(HsStablePtr)handler progName:(NSString *)progName;
12+@end
13+
14+@implementation AppDelegate
15+
16+- (void)applicationWillTerminate:(NSNotification *)aNotification {
17+ // Insert code here to tear down your application
18+}
19+
20+-(id)initApp:(HsStablePtr)handler progName:(NSString *)progName {
21+ self = [super init];
22+ if (self) {
23+ _handler = handler;
24+ NSRect contentSize = NSMakeRect(0.0, 500.0, 1000.0, 700.0);
25+ NSUInteger windowStyleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskResizable | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable;
26+ _window = [[NSWindow alloc] initWithContentRect:contentSize styleMask:windowStyleMask backing:NSBackingStoreBuffered defer:YES];
27+ _window.backgroundColor = [NSColor whiteColor];
28+ _window.title = progName;
29+ }
30+ return self;
31+}
32+
33+-(void)applicationWillFinishLaunching:(NSNotification *)notification {
34+ WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
35+ [theConfiguration.preferences setValue:@YES forKey:@"developerExtrasEnabled"];
36+ WKWebView *webView = [[WKWebView alloc] initWithFrame:_window.contentView.frame configuration:theConfiguration];
37+ [_window setContentView:webView];
38+ withWebView(webView, _handler);
39+}
40+
41+-(void)applicationDidFinishLaunching:(NSNotification *)notification {
42+ [_window orderFrontRegardless];
43+ [_window center];
44+ [NSApp activateIgnoringOtherApps:YES];
45+}
46+
47+-(BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app {
48+ return YES;
49+}
50+
51+@end
52+
53+void runInWKWebView(HsStablePtr handler, const char * _Nonnull progName) {
54+ @autoreleasepool {
55+ NSApplication *application = [NSApplication sharedApplication];
56+ AppDelegate *appDelegate = [[AppDelegate alloc] initApp:handler progName:[NSString stringWithCString:progName encoding:NSUTF8StringEncoding]];
57+ [application setDelegate:appDelegate];
58+ char * argv [] = {"", 0};
59+ NSApplicationMain(0, argv);
60+ }
61+}
62+
jsaddle-wkwebview/cbits/WKWebView.m+1−1
@@ -39,7 +39,7 @@ extern void jsaddleResult(HsStablePtr, const char * _Nonnull result);
3939
4040@end
4141
42−void addJSaddleHandler(WKWebView *webView, HsStablePtr startHandler, HsStablePtr resultHandler) {
42+void addJSaddleHandler(WKWebView *webView, HsStablePtr startHandler, HsStablePtr resultHandler) {
4343 JSaddleHandler * handler = [[JSaddleHandler alloc] initHandler:startHandler resultHandler:resultHandler];
4444 [[[webView configuration] userContentController] addScriptMessageHandler:handler name:@"jsaddle"];
4545 webView.navigationDelegate = handler;
jsaddle-wkwebview/jsaddle-wkwebview.cabal+5−2
@@ -1,5 +1,5 @@
11name: jsaddle-wkwebview
2−version: 0.8.0.0
2+version: 0.8.1.0
33cabal-version: >=1.10
44build-type: Simple
55license: MIT
@@ -28,6 +28,9 @@ library
2828 jsaddle >= 0.8.0.0 && <0.9
2929 default-language: Haskell2010
3030 hs-source-dirs: src
31+ frameworks: Cocoa, WebKit
3132 ghc-options: -ferror-spans -Wall
32− c-sources: cbits/WKWebView.m
33+ c-sources:
34+ cbits/Main.m
35+ cbits/WKWebView.m
3336
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView.hs+20−6
@@ -2,8 +2,10 @@
22module Language.Javascript.JSaddle.WKWebView
33 ( jsaddleMain
44 , WKWebView(..)
5+ , run
56 ) where
67
8+import System.Environment (getProgName)
79import Control.Monad (void, join)
810import Control.Concurrent (forkIO)
911
@@ -12,22 +14,29 @@ import Data.ByteString (useAsCString, packCString)
1214import Data.ByteString.Lazy (ByteString, toStrict, fromStrict)
1315import Data.Aeson (encode, decode)
1416
15−import Foreign.C.String (CString)
17+import Foreign.C.String (CString, withCString)
1618import Foreign.Ptr (Ptr)
1719import Foreign.StablePtr (StablePtr, newStablePtr, deRefStablePtr)
1820
19−import Language.Javascript.JSaddle (Result, JSM)
20−import Language.Javascript.JSaddle.Types (Batch(..))
21+import Language.Javascript.JSaddle (Results, JSM)
2122import Language.Javascript.JSaddle.Run (runJavaScript)
2223import Language.Javascript.JSaddle.Run.Files (initState, runBatch, ghcjsHelpers)
2324
2425newtype WKWebView = WKWebView (Ptr WKWebView)
2526
2627foreign export ccall jsaddleStart :: StablePtr (IO ()) -> IO ()
27−foreign export ccall jsaddleResult :: StablePtr (Result -> IO ()) -> CString -> IO ()
28−foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Result -> IO ()) -> IO ()
28+foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString -> IO ()
29+foreign export ccall withWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
30+foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> IO ()
2931foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
3032foreign import ccall evaluateJavaScript :: WKWebView -> CString -> IO ()
33+foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ()) -> CString -> IO ()
34+
35+run :: JSM () -> IO ()
36+run f = do
37+ handler <- newStablePtr (jsaddleMain f)
38+ progName <- getProgName
39+ withCString progName $ runInWKWebView handler
3140
3241jsaddleMain :: JSM () -> WKWebView -> IO ()
3342jsaddleMain f webView = do
@@ -46,7 +55,7 @@ jsaddleMain f webView = do
4655jsaddleStart :: StablePtr (IO ()) -> IO ()
4756jsaddleStart ptrHandler = join (deRefStablePtr ptrHandler)
4857
49−jsaddleResult :: StablePtr (Result -> IO ()) -> CString -> IO ()
58+jsaddleResult :: StablePtr (Results -> IO ()) -> CString -> IO ()
5059jsaddleResult ptrHandler s = do
5160 processResult <- deRefStablePtr ptrHandler
5261 result <- packCString s
@@ -54,6 +63,11 @@ jsaddleResult ptrHandler s = do
5463 Nothing -> error $ "jsaddle WebSocket decode failed : " <> show result
5564 Just r -> processResult r
5665
66+withWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
67+withWebView webView ptrF = do
68+ f <- deRefStablePtr ptrF
69+ f webView
70+
5771jsaddleJs :: ByteString
5872jsaddleJs = ghcjsHelpers <> "\
5973 \runJSaddleBatch = (function() {\n\