{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
module Miso.Subscription.Window where
import Control.Monad
import Control.Monad.IO.Class
import GHCJS.Marshal
import JavaScript.Object
import JavaScript.Object.Internal
import Miso.Effect (Sub)
import Miso.Event
import Miso.FFI
import Miso.String
import Data.Aeson.Types (parseEither)
windowCoordsSub :: ((Int, Int) -> action) -> Sub action
windowCoordsSub f = \sink -> do
liftIO . sink . f =<< (,) <$> windowInnerHeight <*> windowInnerWidth
windowAddEventListener "resize" $
\windowEvent -> do
target <- getProp "target" (Object windowEvent)
Just w <- fromJSVal =<< getProp "innerWidth" (Object target)
Just h <- fromJSVal =<< getProp "innerHeight" (Object target)
liftIO . sink $ f (h, w)
windowSub :: MisoString -> Decoder r -> (r -> action) -> Sub action
windowSub = windowSubWithOptions defaultOptions
windowSubWithOptions :: Options -> MisoString -> Decoder r -> (r -> action) -> Sub action
windowSubWithOptions Options{..} eventName Decoder{..} toAction = \sink -> do
windowAddEventListener eventName $
\e -> do
decodeAtVal <- toJSVal decodeAt
Just v <- fromJSVal =<< objectToJSON decodeAtVal e
case parseEither decoder v of
Left s -> error $ "Parse error on " <> unpack eventName <> ": " <> s
Right r -> do
when stopPropagation $ eventStopPropagation e
when preventDefault $ eventPreventDefault e
liftIO (sink (toAction r))