-----------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Miso.Native.X.Element.ScrollCoordinator.Event
-- Copyright   :  (C) 2016-2026 David M. Johnson
-- License     :  BSD3-style (see the file LICENSE)
-- Maintainer  :  David M. Johnson <code@dmj.io>
-- Stability   :  experimental
-- Portability :  non-portable
----------------------------------------------------------------------------
module Miso.Native.X.Element.ScrollCoordinator.Event
  ( -- *** Events
    onOffset
  , onOffsetWith
  , onOffsetMain
  , onOffsetMainWith
    -- *** Types
  , ScrollCoordinatorOffsetEvent (..)
    -- *** Decoders
  , offsetDecoder
    -- *** Event Map
  , scrollCoordinatorEvents
  ) where
-----------------------------------------------------------------------------
import qualified Data.Map as M
-----------------------------------------------------------------------------
import           Miso.Event
import           Miso.JSON
import           Miso.Types (Attribute, EventHandler, DOMRef)
-----------------------------------------------------------------------------
scrollCoordinatorEvents :: Events
scrollCoordinatorEvents :: Events
scrollCoordinatorEvents = [(MisoString, Phase)] -> Events
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [ (MisoString
"offset", Phase
BUBBLE) ]
-----------------------------------------------------------------------------
-- | Payload of the @bindoffset@ event.
data ScrollCoordinatorOffsetEvent
  = ScrollCoordinatorOffsetEvent
  { ScrollCoordinatorOffsetEvent -> Double
height :: Double
    -- ^ The scrollable distance
  , ScrollCoordinatorOffsetEvent -> Double
offset :: Double
    -- ^ The header scroll offset
  } deriving (Int -> ScrollCoordinatorOffsetEvent -> ShowS
[ScrollCoordinatorOffsetEvent] -> ShowS
ScrollCoordinatorOffsetEvent -> String
(Int -> ScrollCoordinatorOffsetEvent -> ShowS)
-> (ScrollCoordinatorOffsetEvent -> String)
-> ([ScrollCoordinatorOffsetEvent] -> ShowS)
-> Show ScrollCoordinatorOffsetEvent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ScrollCoordinatorOffsetEvent -> ShowS
showsPrec :: Int -> ScrollCoordinatorOffsetEvent -> ShowS
$cshow :: ScrollCoordinatorOffsetEvent -> String
show :: ScrollCoordinatorOffsetEvent -> String
$cshowList :: [ScrollCoordinatorOffsetEvent] -> ShowS
showList :: [ScrollCoordinatorOffsetEvent] -> ShowS
Show, ScrollCoordinatorOffsetEvent
-> ScrollCoordinatorOffsetEvent -> Bool
(ScrollCoordinatorOffsetEvent
 -> ScrollCoordinatorOffsetEvent -> Bool)
-> (ScrollCoordinatorOffsetEvent
    -> ScrollCoordinatorOffsetEvent -> Bool)
-> Eq ScrollCoordinatorOffsetEvent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ScrollCoordinatorOffsetEvent
-> ScrollCoordinatorOffsetEvent -> Bool
== :: ScrollCoordinatorOffsetEvent
-> ScrollCoordinatorOffsetEvent -> Bool
$c/= :: ScrollCoordinatorOffsetEvent
-> ScrollCoordinatorOffsetEvent -> Bool
/= :: ScrollCoordinatorOffsetEvent
-> ScrollCoordinatorOffsetEvent -> Bool
Eq)
-----------------------------------------------------------------------------
offsetDecoder :: Decoder ScrollCoordinatorOffsetEvent
offsetDecoder :: Decoder ScrollCoordinatorOffsetEvent
offsetDecoder = [MisoString
"detail"] [MisoString]
-> (Value -> Parser ScrollCoordinatorOffsetEvent)
-> Decoder ScrollCoordinatorOffsetEvent
forall a. [MisoString] -> (Value -> Parser a) -> Decoder a
`at` Value -> Parser ScrollCoordinatorOffsetEvent
details
  where
    details :: Value -> Parser ScrollCoordinatorOffsetEvent
details = MisoString
-> (Object -> Parser ScrollCoordinatorOffsetEvent)
-> Value
-> Parser ScrollCoordinatorOffsetEvent
forall a. MisoString -> (Object -> Parser a) -> Value -> Parser a
withObject MisoString
"detail" ((Object -> Parser ScrollCoordinatorOffsetEvent)
 -> Value -> Parser ScrollCoordinatorOffsetEvent)
-> (Object -> Parser ScrollCoordinatorOffsetEvent)
-> Value
-> Parser ScrollCoordinatorOffsetEvent
forall a b. (a -> b) -> a -> b
$ \Object
o ->
      Double -> Double -> ScrollCoordinatorOffsetEvent
ScrollCoordinatorOffsetEvent
        (Double -> Double -> ScrollCoordinatorOffsetEvent)
-> Parser Double -> Parser (Double -> ScrollCoordinatorOffsetEvent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> MisoString -> Parser Double
forall a. FromJSON a => Object -> MisoString -> Parser a
.: MisoString
"height"
        Parser (Double -> ScrollCoordinatorOffsetEvent)
-> Parser Double -> Parser ScrollCoordinatorOffsetEvent
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> MisoString -> Parser Double
forall a. FromJSON a => Object -> MisoString -> Parser a
.: MisoString
"offset"
-----------------------------------------------------------------------------
-- | https://lynxjs.org/api/elements/built-in/scroll-coordinator.html#bindoffset
--
-- Callback reporting folding progress.
--
onOffset :: (ScrollCoordinatorOffsetEvent -> action) -> Attribute model action
onOffset :: forall action model.
(ScrollCoordinatorOffsetEvent -> action) -> Attribute model action
onOffset ScrollCoordinatorOffsetEvent -> action
action = MisoString
-> Decoder ScrollCoordinatorOffsetEvent
-> (ScrollCoordinatorOffsetEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"offset" Decoder ScrollCoordinatorOffsetEvent
offsetDecoder (\ScrollCoordinatorOffsetEvent
e model
_ DOMRef
_ -> ScrollCoordinatorOffsetEvent -> action
action ScrollCoordinatorOffsetEvent
e)
-----------------------------------------------------------------------------
-- | Like 'onOffset', but dispatched on the Lynx __main thread__ ('MTS').
--
-- Runs imperatively on the MTS (no VDOM diff). Meant to be used with
-- @-XStaticPointers@.
--
-- @
-- data Action = Offset ScrollCoordinatorOffsetEvent
--
-- view_ [ event (static (onOffsetMain Offset)) ] [ "some view" ]
-- @
--
onOffsetMain :: (ScrollCoordinatorOffsetEvent -> action) -> EventHandler model action
onOffsetMain :: forall action model.
(ScrollCoordinatorOffsetEvent -> action)
-> EventHandler model action
onOffsetMain ScrollCoordinatorOffsetEvent -> action
action = MisoString
-> Decoder ScrollCoordinatorOffsetEvent
-> (ScrollCoordinatorOffsetEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"offset" Decoder ScrollCoordinatorOffsetEvent
offsetDecoder (\ScrollCoordinatorOffsetEvent
e model
_ DOMRef
_ -> ScrollCoordinatorOffsetEvent -> action
action ScrollCoordinatorOffsetEvent
e)
-----------------------------------------------------------------------------
-- | Like 'onOffsetMain', but the handler also receives read-only access to the
-- @model@ and the target element's 'DOMRef' (for imperative MTS mutation).
--
-- @
-- data Action = Offset ScrollCoordinatorOffsetEvent Model DOMRef
--
-- view_ [ event (static (onOffsetMainWith Offset)) ] [ "some view" ]
-- @
--
onOffsetMainWith :: (ScrollCoordinatorOffsetEvent -> model -> DOMRef -> action) -> EventHandler model action
onOffsetMainWith :: forall model action.
(ScrollCoordinatorOffsetEvent -> model -> DOMRef -> action)
-> EventHandler model action
onOffsetMainWith ScrollCoordinatorOffsetEvent -> model -> DOMRef -> action
action = MisoString
-> Decoder ScrollCoordinatorOffsetEvent
-> (ScrollCoordinatorOffsetEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"offset" Decoder ScrollCoordinatorOffsetEvent
offsetDecoder ScrollCoordinatorOffsetEvent -> model -> DOMRef -> action
action
-----------------------------------------------------------------------------
-- | Like 'onOffset', but the handler also receives the target element's 'DOMRef'.
-- Use for main-thread ('MTS') handlers that imperatively mutate the element.
onOffsetWith :: (ScrollCoordinatorOffsetEvent -> DOMRef -> action) -> Attribute model action
onOffsetWith :: forall action model.
(ScrollCoordinatorOffsetEvent -> DOMRef -> action)
-> Attribute model action
onOffsetWith ScrollCoordinatorOffsetEvent -> DOMRef -> action
action = MisoString
-> Decoder ScrollCoordinatorOffsetEvent
-> (ScrollCoordinatorOffsetEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"offset" Decoder ScrollCoordinatorOffsetEvent
offsetDecoder ((ScrollCoordinatorOffsetEvent -> model -> DOMRef -> action)
 -> Attribute model action)
-> (ScrollCoordinatorOffsetEvent -> model -> DOMRef -> action)
-> Attribute model action
forall a b. (a -> b) -> a -> b
$ \ScrollCoordinatorOffsetEvent
v model
_ DOMRef
domRef -> ScrollCoordinatorOffsetEvent -> DOMRef -> action
action ScrollCoordinatorOffsetEvent
v DOMRef
domRef
-----------------------------------------------------------------------------