@@ -1,15 +1,11 @@
11module Language.Javascript.JSaddle.WKWebView
22 ( jsaddleMain
3− , jsaddleMainHTMLWithBaseURL
43 , jsaddleMainFile
54 , WKWebView(..)
65 , run
7− , run'
86 , runWithAppConfig
9− , runHTMLWithBaseURL
107 , runFile
118 , mainBundleResourcePath
12− , ApplicationState (..)
139 , AppDelegateConfig (..)
1410 , AppDelegateNotificationConfig (..)
1511 , AuthorizationOption (..)
@@ -21,11 +17,9 @@ import Data.Set (Set)
2117import qualified Data.Set as Set
2218import Data.Word
2319import Foreign.Marshal.Utils (fromBool)
24−import Language.Javascript.JSaddle.WKWebView.Internal
25− (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
20+import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
2621import System.Environment (getProgName)
2722import Foreign.C.String (CString, withCString)
28−import Foreign.C.Types (CBool, CInt)
2923import Foreign.StablePtr (StablePtr, newStablePtr)
3024import Language.Javascript.JSaddle (JSM)
3125
@@ -41,7 +35,6 @@ foreign import ccall runInWKWebView
4135 -> StablePtr (IO ()) -- applicationWillTerminate
4236 -> StablePtr (IO ()) -- applicationSignificantTimeChange
4337 -> StablePtr (CString -> IO ()) -- applicationUniversalLink
44− -> StablePtr (CInt -> CString -> IO ()) -- applicationDidReceiveRemoteNotification
4538 -> Word64 -- whether to run requestAuthorizationWithOptions
4639 -> Word64 -- Ask for Badge authorization
4740 -> Word64 -- Ask for Sound authorization
@@ -50,8 +43,6 @@ foreign import ccall runInWKWebView
5043 -> Word64 -- registerForRemoteNotifications
5144 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
5245 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
53− -> Word64 -- developerExtrasEnabled
54− -> StablePtr (CString -> IO CBool) -- applicationOpenFile
5546 -> IO ()
5647
5748data AppDelegateConfig = AppDelegateConfig
@@ -64,25 +55,7 @@ data AppDelegateConfig = AppDelegateConfig
6455 , _appDelegateConfig_applicationWillTerminate :: IO ()
6556 , _appDelegateConfig_applicationSignificantTimeChange :: IO ()
6657 , _appDelegateConfig_applicationUniversalLink :: CString -> IO ()
67− -- ^ Called when the app is launched from a URL. 'CString' contains the URL.
68− --
69− -- The Core Foundation plist key CFBundleURLTypes controls which URL schemes
70− -- the app should respond to.
71− , _appDelegateConfig_applicationDidReceiveRemoteNotification :: ApplicationState -> CString -> IO ()
72− -- ^ Called when the application receives a remote notification.
73− -- 'ApplicationState' is the current state of the app.
74− -- The 'CString' JSON-encoded user info contained in the notification.
75− -- For more information see https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623117-application?language=objc
7658 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
77− , _appDelegateConfig_developerExtrasEnabled :: Bool
78− -- ^ Allow devtools in the app. Defaults to 'True'
79− , _appDelegateConfig_applicationOpenFile :: CString -> IO Bool
80− -- ^ Called when the app is launched by opening a file. 'CString' contains
81− -- the file path. Return value is 'True' if the file was successfully
82− -- opened. Defaults to ignoring the file and returning 'False'.
83− --
84− -- The Core Foundation plist key CFBundleDocumentTypes controls which file
85− -- types should be associated with the app.
8659 }
8760
8861instance Default AppDelegateConfig where
@@ -96,10 +69,7 @@ instance Default AppDelegateConfig where
9669 , _appDelegateConfig_applicationWillTerminate = return ()
9770 , _appDelegateConfig_applicationSignificantTimeChange = return ()
9871 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
99− , _appDelegateConfig_applicationDidReceiveRemoteNotification = \_ _ -> return ()
10072 , _appDelegateConfig_appDelegateNotificationConfig = def
101− , _appDelegateConfig_developerExtrasEnabled = True
102− , _appDelegateConfig_applicationOpenFile = \_ -> return False
10373 }
10474
10575data AuthorizationOption = AuthorizationOption_Badge
@@ -108,12 +78,6 @@ data AuthorizationOption = AuthorizationOption_Badge
10878 | AuthorizationOption_CarPlay
10979 deriving (Show, Read, Eq, Ord)
11080
111−data ApplicationState = ApplicationState_Active
112− | ApplicationState_Inactive
113− | ApplicationState_Background
114− | ApplicationState_Unknown CInt
115− deriving (Show, Read, Eq, Ord)
116−
11781data AppDelegateNotificationConfig = AppDelegateNotificationConfig
11882 { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
11983 , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
@@ -132,38 +96,29 @@ instance Default AppDelegateNotificationConfig where
13296
13397-- | Run JSaddle in a WKWebView
13498run :: JSM () -> IO ()
135−run f = run' def (jsaddleMain f)
99+run = run' Nothing def
136100
137101-- | Run JSaddle in a WKWebView
138102runWithAppConfig :: AppDelegateConfig -> JSM () -> IO ()
139−runWithAppConfig cfg = run' cfg . jsaddleMain
103+runWithAppConfig = run' Nothing
140104
141105-- | Run JSaddle in a WKWebView first loading the specified file
142106-- from the mainBundle (relative to the resourcePath).
143−runFile
144− :: ByteString -- ^ The file to navigate to.
145− -> ByteString -- ^ The path to allow read access to.
146− -> AppDelegateConfig
147− -> JSM ()
148− -> IO ()
149−runFile url allowing cfg = run' cfg . jsaddleMainFile url allowing
150−
151−-- | Run JSaddle in a WKWebView first loading the specified html
152−-- as though it came from the specified URL
153−-- (calls WKWebKit function loadHTMLString).
154−runHTMLWithBaseURL
155− :: ByteString -- ^ HTML to load.
156− -> ByteString -- ^ pretend it came from this URL.
157− -> AppDelegateConfig
158− -> JSM ()
159− -> IO ()
160−runHTMLWithBaseURL url allowing cfg = run' cfg . jsaddleMainHTMLWithBaseURL url allowing
161−
162−run' :: AppDelegateConfig
163− -> (WKWebView -> IO ())
107+runFile :: ByteString -- ^ The file to navigate to.
108+ -> ByteString -- ^ The path to allow read access to.
109+ -> AppDelegateConfig
110+ -> JSM ()
111+ -> IO ()
112+runFile url allowing = run' $ Just (url, allowing)
113+
114+run' :: Maybe (ByteString, ByteString)
115+ -> AppDelegateConfig
116+ -> JSM ()
164117 -> IO ()
165−run' cfg main = do
166− handler <- newStablePtr main
118+run' mUrl cfg f = do
119+ handler <- case mUrl of
120+ Just (url, allowing) -> newStablePtr (jsaddleMainFile url allowing f)
121+ Nothing -> newStablePtr (jsaddleMain f)
167122 progName <- getProgName
168123
169124 -- AppDelegate callbacks
@@ -176,14 +131,6 @@ run' cfg main = do
176131 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
177132 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
178133 applicationUniversalLink <- newStablePtr $ _appDelegateConfig_applicationUniversalLink cfg
179− applicationDidReceiveRemoteNotification <- newStablePtr $ \n s -> do
180− let appState = case n of
181− 0 -> ApplicationState_Active
182− 1 -> ApplicationState_Inactive
183− 2 -> ApplicationState_Background
184− _ -> ApplicationState_Unknown n
185− _appDelegateConfig_applicationDidReceiveRemoteNotification cfg appState s
186− applicationOpenFile <- newStablePtr $ fmap fromBool . _appDelegateConfig_applicationOpenFile cfg
187134
188135 -- AppDelegate notification configuration
189136 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
@@ -206,7 +153,6 @@ run' cfg main = do
206153 applicationWillTerminate
207154 applicationSignificantTimeChange
208155 applicationUniversalLink
209− applicationDidReceiveRemoteNotification
210156 (fromBool requestAuthorizationWithOptions)
211157 (fromBool $ Set.member AuthorizationOption_Badge authorizationOptions)
212158 (fromBool $ Set.member AuthorizationOption_Sound authorizationOptions)
@@ -215,5 +161,3 @@ run' cfg main = do
215161 (fromBool registerForRemoteNotifications)
216162 didRegisterForRemoteNotificationsWithDeviceToken
217163 didFailToRegisterForRemoteNotificationsWithError
218− (fromBool $ _appDelegateConfig_developerExtrasEnabled cfg)
219− applicationOpenFile