on master help sign in
Merge pull request #99 from obsidiansystems/ts-wkwebview-open-file

wkwebview: allow launching by opening file

commit1e398448bfa2ae506be6b99912cfa4415f6162c9 authorHamish Mackenzie <Hamish.K.Mackenzie@googlemail.com> dateSat 13 Jul 2019, 22:49 (7 years ago) parentf06a9c5da9875e
3 files changed+35−2
jsaddle-wkwebview/cbits-cocoa/AppDelegate.m+10−1 jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+18−1 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+7
jsaddle-wkwebview/cbits-cocoa/AppDelegate.m+10−1
@@ -3,6 +3,7 @@
33#import <WebKit/WebKit.h>
44
55extern void callIO(HsStablePtr);
6+extern bool callWithCStringReturningBool(const char * _Nonnull, HsStablePtr);
67extern void callWithCString(const char * _Nonnull, HsStablePtr);
78extern void callWithWebView(WKWebView *, HsStablePtr);
89
@@ -16,6 +17,7 @@ extern void callWithWebView(WKWebView *, HsStablePtr);
1617HsStablePtr global_willFinishLaunchingWithOptions = 0;
1718HsStablePtr global_didFinishLaunchingWithOptions = 0;
1819HsStablePtr global_applicationUniversalLink = 0;
20+HsStablePtr global_applicationOpenFile = 0;
1921uint64_t global_developerExtrasEnabled = 1;
2022
2123@implementation AppDelegate
@@ -38,6 +40,11 @@ uint64_t global_developerExtrasEnabled = 1;
3840 return self;
3941}
4042
43+-(BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
44+ bool b = callWithCStringReturningBool([filename UTF8String], global_applicationOpenFile);
45+ return b ? YES : NO;
46+}
47+
4148-(void)applicationWillFinishLaunching:(NSNotification *)notification {
4249 WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
4350 if (global_developerExtrasEnabled) {
@@ -93,11 +100,13 @@ void runInWKWebView(HsStablePtr handler,
93100 const uint64_t hs_registerForRemoteNotifications,
94101 HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken,
95102 HsStablePtr hs_didFailToRegisterForRemoteNotificationsWithError,
96− const uint64_t hs_developerExtrasEnabled) {
103+ const uint64_t hs_developerExtrasEnabled,
104+ HsStablePtr hs_applicationOpenFile) {
97105 @autoreleasepool {
98106 global_willFinishLaunchingWithOptions = hs_willFinishLaunchingWithOptions;
99107 global_didFinishLaunchingWithOptions = hs_didFinishLaunchingWithOptions;
100108 global_applicationUniversalLink = hs_applicationUniversalLink;
109+ global_applicationOpenFile = hs_applicationOpenFile;
101110 global_developerExtrasEnabled = hs_developerExtrasEnabled;
102111 NSApplication *application = [NSApplication sharedApplication];
103112 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
2424 (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
2525import System.Environment (getProgName)
2626import Foreign.C.String (CString, withCString)
27+import Foreign.C.Types (CBool)
2728import Foreign.StablePtr (StablePtr, newStablePtr)
2829import Language.Javascript.JSaddle (JSM)
2930
@@ -47,7 +48,8 @@ foreign import ccall runInWKWebView
4748 -> Word64 -- registerForRemoteNotifications
4849 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
4950 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
50− -> Word64 -- Allow developer tools
51+ -> Word64 -- developerExtrasEnabled
52+ -> StablePtr (CString -> IO CBool) -- applicationOpenFile
5153 -> IO ()
5254
5355data AppDelegateConfig = AppDelegateConfig
@@ -60,8 +62,20 @@ data AppDelegateConfig = AppDelegateConfig
6062 , _appDelegateConfig_applicationWillTerminate :: IO ()
6163 , _appDelegateConfig_applicationSignificantTimeChange :: IO ()
6264 , _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.
6369 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
6470 , _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.
6579 }
6680
6781instance Default AppDelegateConfig where
@@ -77,6 +91,7 @@ instance Default AppDelegateConfig where
7791 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
7892 , _appDelegateConfig_appDelegateNotificationConfig = def
7993 , _appDelegateConfig_developerExtrasEnabled = True
94+ , _appDelegateConfig_applicationOpenFile = \_ -> return False
8095 }
8196
8297data AuthorizationOption = AuthorizationOption_Badge
@@ -147,6 +162,7 @@ run' cfg main = do
147162 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
148163 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
149164 applicationUniversalLink <- newStablePtr $ _appDelegateConfig_applicationUniversalLink cfg
165+ applicationOpenFile <- newStablePtr $ fmap fromBool . _appDelegateConfig_applicationOpenFile cfg
150166
151167 -- AppDelegate notification configuration
152168 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
@@ -178,3 +194,4 @@ run' cfg main = do
178194 didRegisterForRemoteNotificationsWithDeviceToken
179195 didFailToRegisterForRemoteNotificationsWithError
180196 (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)
2020import Data.Text.Encoding (encodeUtf8)
2121
2222import Foreign.C.String (CString)
23+import Foreign.C.Types (CBool(..))
2324import Foreign.Ptr (Ptr, nullPtr)
2425import Foreign.StablePtr (StablePtr, newStablePtr, deRefStablePtr)
2526
@@ -37,6 +38,7 @@ foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString ->
3738foreign export ccall jsaddleSyncResult :: StablePtr (Results -> IO Batch) -> JSaddleHandler -> CString -> IO ()
3839foreign export ccall callWithWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
3940foreign export ccall callWithCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
41+foreign export ccall callWithCStringReturningBool :: CString -> StablePtr (CString -> IO CBool) -> IO CBool
4042foreign export ccall callIO :: StablePtr (IO ()) -> IO ()
4143foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> StablePtr (Results -> IO Batch) -> IO ()
4244foreign import ccall loadHTMLStringWithBaseURL :: WKWebView -> CString -> CString -> IO ()
@@ -126,6 +128,11 @@ callWithCString c fptr = do
126128 f <- deRefStablePtr fptr
127129 f c
128130
131+callWithCStringReturningBool :: CString -> StablePtr (CString -> IO CBool) -> IO CBool
132+callWithCStringReturningBool c fptr = do
133+ f <- deRefStablePtr fptr
134+ f c
135+
129136callIO :: StablePtr (IO ()) -> IO ()
130137callIO ptr = do
131138 f <- deRefStablePtr ptr