on master help sign in
wkwebview: allow launching by opening file
da9875eTom Smalley
jsaddle-wkwebview/cbits-cocoa/AppDelegate.m+10−1
@@ -3,6 +3,7 @@
3#import <WebKit/WebKit.h>
3#import <WebKit/WebKit.h>
4
4
5extern void callIO(HsStablePtr);
5extern void callIO(HsStablePtr);
6+extern bool callWithCStringReturningBool(const char * _Nonnull, HsStablePtr);
6extern void callWithCString(const char * _Nonnull, HsStablePtr);
7extern void callWithCString(const char * _Nonnull, HsStablePtr);
7extern void callWithWebView(WKWebView *, HsStablePtr);
8extern void callWithWebView(WKWebView *, HsStablePtr);
8
9
@@ -16,6 +17,7 @@ extern void callWithWebView(WKWebView *, HsStablePtr);
16HsStablePtr global_willFinishLaunchingWithOptions = 0;
17HsStablePtr global_willFinishLaunchingWithOptions = 0;
17HsStablePtr global_didFinishLaunchingWithOptions = 0;
18HsStablePtr global_didFinishLaunchingWithOptions = 0;
18HsStablePtr global_applicationUniversalLink = 0;
19HsStablePtr global_applicationUniversalLink = 0;
20+HsStablePtr global_applicationOpenFile = 0;
19uint64_t global_developerExtrasEnabled = 1;
21uint64_t global_developerExtrasEnabled = 1;
20
22
21@implementation AppDelegate
23@implementation AppDelegate
@@ -38,6 +40,11 @@ uint64_t global_developerExtrasEnabled = 1;
38 return self;
40 return self;
39}
41}
40
42
43+-(BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
44+ bool b = callWithCStringReturningBool([filename UTF8String], global_applicationOpenFile);
45+ return b ? YES : NO;
46+}
47+
41-(void)applicationWillFinishLaunching:(NSNotification *)notification {
48-(void)applicationWillFinishLaunching:(NSNotification *)notification {
42 WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
49 WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
43 if (global_developerExtrasEnabled) {
50 if (global_developerExtrasEnabled) {
@@ -93,11 +100,13 @@ void runInWKWebView(HsStablePtr handler,
93 const uint64_t hs_registerForRemoteNotifications,
100 const uint64_t hs_registerForRemoteNotifications,
94 HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken,
101 HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken,
95 HsStablePtr hs_didFailToRegisterForRemoteNotificationsWithError,
102 HsStablePtr hs_didFailToRegisterForRemoteNotificationsWithError,
96− const uint64_t hs_developerExtrasEnabled) {
103+ const uint64_t hs_developerExtrasEnabled,
104+ HsStablePtr hs_applicationOpenFile) {
97 @autoreleasepool {
105 @autoreleasepool {
98 global_willFinishLaunchingWithOptions = hs_willFinishLaunchingWithOptions;
106 global_willFinishLaunchingWithOptions = hs_willFinishLaunchingWithOptions;
99 global_didFinishLaunchingWithOptions = hs_didFinishLaunchingWithOptions;
107 global_didFinishLaunchingWithOptions = hs_didFinishLaunchingWithOptions;
100 global_applicationUniversalLink = hs_applicationUniversalLink;
108 global_applicationUniversalLink = hs_applicationUniversalLink;
109+ global_applicationOpenFile = hs_applicationOpenFile;
101 global_developerExtrasEnabled = hs_developerExtrasEnabled;
110 global_developerExtrasEnabled = hs_developerExtrasEnabled;
102 NSApplication *application = [NSApplication sharedApplication];
111 NSApplication *application = [NSApplication sharedApplication];
103 AppDelegate *appDelegate = [[AppDelegate alloc] initApp:handler progName:[NSString stringWithCString:progName encoding:NSUTF8StringEncoding]];
112 AppDelegate *appDelegate = [[AppDelegate alloc] initApp:handler progName:[NSString stringWithCString:progName encoding:NSUTF8StringEncoding]];
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+18−1
@@ -24,6 +24,7 @@ import Language.Javascript.JSaddle.WKWebView.Internal
24 (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
24 (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
25import System.Environment (getProgName)
25import System.Environment (getProgName)
26import Foreign.C.String (CString, withCString)
26import Foreign.C.String (CString, withCString)
27+import Foreign.C.Types (CBool)
27import Foreign.StablePtr (StablePtr, newStablePtr)
28import Foreign.StablePtr (StablePtr, newStablePtr)
28import Language.Javascript.JSaddle (JSM)
29import Language.Javascript.JSaddle (JSM)
29
30
@@ -47,7 +48,8 @@ foreign import ccall runInWKWebView
47 -> Word64 -- registerForRemoteNotifications
48 -> Word64 -- registerForRemoteNotifications
48 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
49 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
49 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
50 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
50− -> Word64 -- Allow developer tools
51+ -> Word64 -- developerExtrasEnabled
52+ -> StablePtr (CString -> IO CBool) -- applicationOpenFile
51 -> IO ()
53 -> IO ()
52
54
53data AppDelegateConfig = AppDelegateConfig
55data AppDelegateConfig = AppDelegateConfig
@@ -60,8 +62,20 @@ data AppDelegateConfig = AppDelegateConfig
60 , _appDelegateConfig_applicationWillTerminate :: IO ()
62 , _appDelegateConfig_applicationWillTerminate :: IO ()
61 , _appDelegateConfig_applicationSignificantTimeChange :: IO ()
63 , _appDelegateConfig_applicationSignificantTimeChange :: IO ()
62 , _appDelegateConfig_applicationUniversalLink :: CString -> IO ()
64 , _appDelegateConfig_applicationUniversalLink :: CString -> IO ()
65+ -- ^ Called when the app is launched from a URL. 'CString' contains the URL.
66+ --
67+ -- The Core Foundation plist key CFBundleURLTypes controls which URL schemes
68+ -- the app should respond to.
63 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
69 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
64 , _appDelegateConfig_developerExtrasEnabled :: Bool
70 , _appDelegateConfig_developerExtrasEnabled :: Bool
71+ -- ^ Allow devtools in the app. Defaults to 'True'
72+ , _appDelegateConfig_applicationOpenFile :: CString -> IO Bool
73+ -- ^ Called when the app is launched by opening a file. 'CString' contains
74+ -- the file path. Return value is 'True' if the file was successfully
75+ -- opened. Defaults to ignoring the file and returning 'False'.
76+ --
77+ -- The Core Foundation plist key CFBundleDocumentTypes controls which file
78+ -- types should be associated with the app.
65 }
79 }
66
80
67instance Default AppDelegateConfig where
81instance Default AppDelegateConfig where
@@ -77,6 +91,7 @@ instance Default AppDelegateConfig where
77 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
91 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
78 , _appDelegateConfig_appDelegateNotificationConfig = def
92 , _appDelegateConfig_appDelegateNotificationConfig = def
79 , _appDelegateConfig_developerExtrasEnabled = True
93 , _appDelegateConfig_developerExtrasEnabled = True
94+ , _appDelegateConfig_applicationOpenFile = \_ -> return False
80 }
95 }
81
96
82data AuthorizationOption = AuthorizationOption_Badge
97data AuthorizationOption = AuthorizationOption_Badge
@@ -147,6 +162,7 @@ run' cfg main = do
147 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
162 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
148 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
163 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
149 applicationUniversalLink <- newStablePtr $ _appDelegateConfig_applicationUniversalLink cfg
164 applicationUniversalLink <- newStablePtr $ _appDelegateConfig_applicationUniversalLink cfg
165+ applicationOpenFile <- newStablePtr $ fmap fromBool . _appDelegateConfig_applicationOpenFile cfg
150
166
151 -- AppDelegate notification configuration
167 -- AppDelegate notification configuration
152 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
168 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
@@ -178,3 +194,4 @@ run' cfg main = do
178 didRegisterForRemoteNotificationsWithDeviceToken
194 didRegisterForRemoteNotificationsWithDeviceToken
179 didFailToRegisterForRemoteNotificationsWithError
195 didFailToRegisterForRemoteNotificationsWithError
180 (fromBool $ _appDelegateConfig_developerExtrasEnabled cfg)
196 (fromBool $ _appDelegateConfig_developerExtrasEnabled cfg)
197+ applicationOpenFile
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+7
@@ -20,6 +20,7 @@ import qualified Data.Text as T (pack)
20import Data.Text.Encoding (encodeUtf8)
20import Data.Text.Encoding (encodeUtf8)
21
21
22import Foreign.C.String (CString)
22import Foreign.C.String (CString)
23+import Foreign.C.Types (CBool(..))
23import Foreign.Ptr (Ptr, nullPtr)
24import Foreign.Ptr (Ptr, nullPtr)
24import Foreign.StablePtr (StablePtr, newStablePtr, deRefStablePtr)
25import Foreign.StablePtr (StablePtr, newStablePtr, deRefStablePtr)
25
26
@@ -37,6 +38,7 @@ foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString ->
37foreign export ccall jsaddleSyncResult :: StablePtr (Results -> IO Batch) -> JSaddleHandler -> CString -> IO ()
38foreign export ccall jsaddleSyncResult :: StablePtr (Results -> IO Batch) -> JSaddleHandler -> CString -> IO ()
38foreign export ccall callWithWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
39foreign export ccall callWithWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
39foreign export ccall callWithCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
40foreign export ccall callWithCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
41+foreign export ccall callWithCStringReturningBool :: CString -> StablePtr (CString -> IO CBool) -> IO CBool
40foreign export ccall callIO :: StablePtr (IO ()) -> IO ()
42foreign export ccall callIO :: StablePtr (IO ()) -> IO ()
41foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> StablePtr (Results -> IO Batch) -> IO ()
43foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> StablePtr (Results -> IO Batch) -> IO ()
42foreign import ccall loadHTMLStringWithBaseURL :: WKWebView -> CString -> CString -> IO ()
44foreign import ccall loadHTMLStringWithBaseURL :: WKWebView -> CString -> CString -> IO ()
@@ -126,6 +128,11 @@ callWithCString c fptr = do
126 f <- deRefStablePtr fptr
128 f <- deRefStablePtr fptr
127 f c
129 f c
128
130
131+callWithCStringReturningBool :: CString -> StablePtr (CString -> IO CBool) -> IO CBool
132+callWithCStringReturningBool c fptr = do
133+ f <- deRefStablePtr fptr
134+ f c
135+
129callIO :: StablePtr (IO ()) -> IO ()
136callIO :: StablePtr (IO ()) -> IO ()
130callIO ptr = do
137callIO ptr = do
131 f <- deRefStablePtr ptr
138 f <- deRefStablePtr ptr