{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Brick.Types.EventM
  ( EventM(..)
  , getVtyContext
  )
where

import Control.Monad.Catch (MonadThrow, MonadCatch, MonadMask)
#if !MIN_VERSION_base(4,13,0)
import Control.Monad.Fail (MonadFail)
#endif
import Control.Monad.Reader
import Control.Monad.State.Strict
import Lens.Micro.Mtl
import Lens.Micro.Mtl.Internal

import Brick.Types.Internal

-- | The monad in which event handlers run.
newtype EventM n s a =
    EventM { forall n s a.
EventM n s a
-> ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) a
runEventM :: ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) a
           }
           deriving ( (forall a b. (a -> b) -> EventM n s a -> EventM n s b)
-> (forall a b. a -> EventM n s b -> EventM n s a)
-> Functor (EventM n s)
forall a b. a -> EventM n s b -> EventM n s a
forall a b. (a -> b) -> EventM n s a -> EventM n s b
forall n s a b. a -> EventM n s b -> EventM n s a
forall n s a b. (a -> b) -> EventM n s a -> EventM n s b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall n s a b. (a -> b) -> EventM n s a -> EventM n s b
fmap :: forall a b. (a -> b) -> EventM n s a -> EventM n s b
$c<$ :: forall n s a b. a -> EventM n s b -> EventM n s a
<$ :: forall a b. a -> EventM n s b -> EventM n s a
Functor, Functor (EventM n s)
Functor (EventM n s) =>
(forall a. a -> EventM n s a)
-> (forall a b.
    EventM n s (a -> b) -> EventM n s a -> EventM n s b)
-> (forall a b c.
    (a -> b -> c) -> EventM n s a -> EventM n s b -> EventM n s c)
-> (forall a b. EventM n s a -> EventM n s b -> EventM n s b)
-> (forall a b. EventM n s a -> EventM n s b -> EventM n s a)
-> Applicative (EventM n s)
forall a. a -> EventM n s a
forall n s. Functor (EventM n s)
forall a b. EventM n s a -> EventM n s b -> EventM n s a
forall a b. EventM n s a -> EventM n s b -> EventM n s b
forall a b. EventM n s (a -> b) -> EventM n s a -> EventM n s b
forall n s a. a -> EventM n s a
forall a b c.
(a -> b -> c) -> EventM n s a -> EventM n s b -> EventM n s c
forall n s a b. EventM n s a -> EventM n s b -> EventM n s a
forall n s a b. EventM n s a -> EventM n s b -> EventM n s b
forall n s a b. EventM n s (a -> b) -> EventM n s a -> EventM n s b
forall n s a b c.
(a -> b -> c) -> EventM n s a -> EventM n s b -> EventM n s c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall n s a. a -> EventM n s a
pure :: forall a. a -> EventM n s a
$c<*> :: forall n s a b. EventM n s (a -> b) -> EventM n s a -> EventM n s b
<*> :: forall a b. EventM n s (a -> b) -> EventM n s a -> EventM n s b
$cliftA2 :: forall n s a b c.
(a -> b -> c) -> EventM n s a -> EventM n s b -> EventM n s c
liftA2 :: forall a b c.
(a -> b -> c) -> EventM n s a -> EventM n s b -> EventM n s c
$c*> :: forall n s a b. EventM n s a -> EventM n s b -> EventM n s b
*> :: forall a b. EventM n s a -> EventM n s b -> EventM n s b
$c<* :: forall n s a b. EventM n s a -> EventM n s b -> EventM n s a
<* :: forall a b. EventM n s a -> EventM n s b -> EventM n s a
Applicative, Applicative (EventM n s)
Applicative (EventM n s) =>
(forall a b. EventM n s a -> (a -> EventM n s b) -> EventM n s b)
-> (forall a b. EventM n s a -> EventM n s b -> EventM n s b)
-> (forall a. a -> EventM n s a)
-> Monad (EventM n s)
forall a. a -> EventM n s a
forall n s. Applicative (EventM n s)
forall a b. EventM n s a -> EventM n s b -> EventM n s b
forall a b. EventM n s a -> (a -> EventM n s b) -> EventM n s b
forall n s a. a -> EventM n s a
forall n s a b. EventM n s a -> EventM n s b -> EventM n s b
forall n s a b. EventM n s a -> (a -> EventM n s b) -> EventM n s b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall n s a b. EventM n s a -> (a -> EventM n s b) -> EventM n s b
>>= :: forall a b. EventM n s a -> (a -> EventM n s b) -> EventM n s b
$c>> :: forall n s a b. EventM n s a -> EventM n s b -> EventM n s b
>> :: forall a b. EventM n s a -> EventM n s b -> EventM n s b
$creturn :: forall n s a. a -> EventM n s a
return :: forall a. a -> EventM n s a
Monad, Monad (EventM n s)
Monad (EventM n s) =>
(forall a. IO a -> EventM n s a) -> MonadIO (EventM n s)
forall a. IO a -> EventM n s a
forall n s. Monad (EventM n s)
forall n s a. IO a -> EventM n s a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
$cliftIO :: forall n s a. IO a -> EventM n s a
liftIO :: forall a. IO a -> EventM n s a
MonadIO
                    , Monad (EventM n s)
Monad (EventM n s) =>
(forall e a. (HasCallStack, Exception e) => e -> EventM n s a)
-> MonadThrow (EventM n s)
forall n s. Monad (EventM n s)
forall e a. (HasCallStack, Exception e) => e -> EventM n s a
forall n s e a. (HasCallStack, Exception e) => e -> EventM n s a
forall (m :: * -> *).
Monad m =>
(forall e a. (HasCallStack, Exception e) => e -> m a)
-> MonadThrow m
$cthrowM :: forall n s e a. (HasCallStack, Exception e) => e -> EventM n s a
throwM :: forall e a. (HasCallStack, Exception e) => e -> EventM n s a
MonadThrow, MonadThrow (EventM n s)
MonadThrow (EventM n s) =>
(forall e a.
 (HasCallStack, Exception e) =>
 EventM n s a -> (e -> EventM n s a) -> EventM n s a)
-> MonadCatch (EventM n s)
forall n s. MonadThrow (EventM n s)
forall e a.
(HasCallStack, Exception e) =>
EventM n s a -> (e -> EventM n s a) -> EventM n s a
forall n s e a.
(HasCallStack, Exception e) =>
EventM n s a -> (e -> EventM n s a) -> EventM n s a
forall (m :: * -> *).
MonadThrow m =>
(forall e a.
 (HasCallStack, Exception e) =>
 m a -> (e -> m a) -> m a)
-> MonadCatch m
$ccatch :: forall n s e a.
(HasCallStack, Exception e) =>
EventM n s a -> (e -> EventM n s a) -> EventM n s a
catch :: forall e a.
(HasCallStack, Exception e) =>
EventM n s a -> (e -> EventM n s a) -> EventM n s a
MonadCatch, MonadCatch (EventM n s)
MonadCatch (EventM n s) =>
(forall b.
 HasCallStack =>
 ((forall a. EventM n s a -> EventM n s a) -> EventM n s b)
 -> EventM n s b)
-> (forall b.
    HasCallStack =>
    ((forall a. EventM n s a -> EventM n s a) -> EventM n s b)
    -> EventM n s b)
-> (forall a b c.
    HasCallStack =>
    EventM n s a
    -> (a -> ExitCase b -> EventM n s c)
    -> (a -> EventM n s b)
    -> EventM n s (b, c))
-> MonadMask (EventM n s)
forall b.
HasCallStack =>
((forall a. EventM n s a -> EventM n s a) -> EventM n s b)
-> EventM n s b
forall n s. MonadCatch (EventM n s)
forall a b c.
HasCallStack =>
EventM n s a
-> (a -> ExitCase b -> EventM n s c)
-> (a -> EventM n s b)
-> EventM n s (b, c)
forall n s b.
HasCallStack =>
((forall a. EventM n s a -> EventM n s a) -> EventM n s b)
-> EventM n s b
forall n s a b c.
HasCallStack =>
EventM n s a
-> (a -> ExitCase b -> EventM n s c)
-> (a -> EventM n s b)
-> EventM n s (b, c)
forall (m :: * -> *).
MonadCatch m =>
(forall b. HasCallStack => ((forall a. m a -> m a) -> m b) -> m b)
-> (forall b.
    HasCallStack =>
    ((forall a. m a -> m a) -> m b) -> m b)
-> (forall a b c.
    HasCallStack =>
    m a -> (a -> ExitCase b -> m c) -> (a -> m b) -> m (b, c))
-> MonadMask m
$cmask :: forall n s b.
HasCallStack =>
((forall a. EventM n s a -> EventM n s a) -> EventM n s b)
-> EventM n s b
mask :: forall b.
HasCallStack =>
((forall a. EventM n s a -> EventM n s a) -> EventM n s b)
-> EventM n s b
$cuninterruptibleMask :: forall n s b.
HasCallStack =>
((forall a. EventM n s a -> EventM n s a) -> EventM n s b)
-> EventM n s b
uninterruptibleMask :: forall b.
HasCallStack =>
((forall a. EventM n s a -> EventM n s a) -> EventM n s b)
-> EventM n s b
$cgeneralBracket :: forall n s a b c.
HasCallStack =>
EventM n s a
-> (a -> ExitCase b -> EventM n s c)
-> (a -> EventM n s b)
-> EventM n s (b, c)
generalBracket :: forall a b c.
HasCallStack =>
EventM n s a
-> (a -> ExitCase b -> EventM n s c)
-> (a -> EventM n s b)
-> EventM n s (b, c)
MonadMask
#if !MIN_VERSION_base(4,13,0)
                    , MonadFail
#endif
                    )

instance MonadState s (EventM n s) where
    get :: EventM n s s
get = ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) s
-> EventM n s s
forall n s a.
ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) a
-> EventM n s a
EventM (ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) s
 -> EventM n s s)
-> ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) s
-> EventM n s s
forall a b. (a -> b) -> a -> b
$ StateT s (StateT (EventState n) IO) s
-> ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) s
forall (m :: * -> *) a. Monad m => m a -> ReaderT (EventRO n) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift StateT s (StateT (EventState n) IO) s
forall s (m :: * -> *). MonadState s m => m s
get
    put :: s -> EventM n s ()
put = ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) ()
-> EventM n s ()
forall n s a.
ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) a
-> EventM n s a
EventM (ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) ()
 -> EventM n s ())
-> (s
    -> ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) ())
-> s
-> EventM n s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StateT s (StateT (EventState n) IO) ()
-> ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) ()
forall (m :: * -> *) a. Monad m => m a -> ReaderT (EventRO n) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (StateT s (StateT (EventState n) IO) ()
 -> ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) ())
-> (s -> StateT s (StateT (EventState n) IO) ())
-> s
-> ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> StateT s (StateT (EventState n) IO) ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put

getVtyContext :: EventM n s VtyContext
getVtyContext :: forall n s. EventM n s VtyContext
getVtyContext = ReaderT
  (EventRO n) (StateT s (StateT (EventState n) IO)) VtyContext
-> EventM n s VtyContext
forall n s a.
ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) a
-> EventM n s a
EventM (ReaderT
   (EventRO n) (StateT s (StateT (EventState n) IO)) VtyContext
 -> EventM n s VtyContext)
-> ReaderT
     (EventRO n) (StateT s (StateT (EventState n) IO)) VtyContext
-> EventM n s VtyContext
forall a b. (a -> b) -> a -> b
$ StateT s (StateT (EventState n) IO) VtyContext
-> ReaderT
     (EventRO n) (StateT s (StateT (EventState n) IO)) VtyContext
forall (m :: * -> *) a. Monad m => m a -> ReaderT (EventRO n) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (StateT s (StateT (EventState n) IO) VtyContext
 -> ReaderT
      (EventRO n) (StateT s (StateT (EventState n) IO)) VtyContext)
-> StateT s (StateT (EventState n) IO) VtyContext
-> ReaderT
     (EventRO n) (StateT s (StateT (EventState n) IO)) VtyContext
forall a b. (a -> b) -> a -> b
$ StateT (EventState n) IO VtyContext
-> StateT s (StateT (EventState n) IO) VtyContext
forall (m :: * -> *) a. Monad m => m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (StateT (EventState n) IO VtyContext
 -> StateT s (StateT (EventState n) IO) VtyContext)
-> StateT (EventState n) IO VtyContext
-> StateT s (StateT (EventState n) IO) VtyContext
forall a b. (a -> b) -> a -> b
$ (EventState n -> VtyContext) -> StateT (EventState n) IO VtyContext
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets EventState n -> VtyContext
forall n. EventState n -> VtyContext
vtyContext

type instance Zoomed (EventM n s) = Zoomed (StateT s (StateT (EventState n) IO))

instance Zoom (EventM n s) (EventM n t) s t where
    zoom :: forall c.
LensLike' (Zoomed (EventM n s) c) t s
-> EventM n s c -> EventM n t c
zoom LensLike' (Zoomed (EventM n s) c) t s
l (EventM ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) c
m) = ReaderT (EventRO n) (StateT t (StateT (EventState n) IO)) c
-> EventM n t c
forall n s a.
ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) a
-> EventM n s a
EventM (LensLike'
  (Zoomed
     (ReaderT (EventRO n) (StateT s (StateT (EventState n) IO))) c)
  t
  s
-> ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) c
-> ReaderT (EventRO n) (StateT t (StateT (EventState n) IO)) c
forall c.
LensLike'
  (Zoomed
     (ReaderT (EventRO n) (StateT s (StateT (EventState n) IO))) c)
  t
  s
-> ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) c
-> ReaderT (EventRO n) (StateT t (StateT (EventState n) IO)) c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed
     (ReaderT (EventRO n) (StateT s (StateT (EventState n) IO))) c)
  t
  s
LensLike' (Zoomed (EventM n s) c) t s
l ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) c
m)