From 293ba9ceba5eb6a8b8e6258b56868594f76e33c5 Mon Sep 17 00:00:00 2001 From: Jonathan Daugherty Date: Sun, 17 Jul 2022 19:03:29 -0700 Subject: [PATCH] Move EventM from Brick.Types to Brick.Types.EventM, stop exporting internals of EventM --- brick.cabal | 1 + src/Brick/Main.hs | 2 +- src/Brick/Types.hs | 21 ++------------------- src/Brick/Types/EventM.hs | 28 ++++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 src/Brick/Types/EventM.hs diff --git a/brick.cabal b/brick.cabal index f305861..4d02631 100644 --- a/brick.cabal +++ b/brick.cabal @@ -108,6 +108,7 @@ library other-modules: Brick.Types.Common Brick.Types.TH + Brick.Types.EventM Brick.Types.Internal Brick.Widgets.Internal diff --git a/src/Brick/Main.hs b/src/Brick/Main.hs index 0d493b4..cbd15a3 100644 --- a/src/Brick/Main.hs +++ b/src/Brick/Main.hs @@ -79,7 +79,7 @@ import Graphics.Vty import Graphics.Vty.Attributes (defAttr) import Brick.BChan (BChan, newBChan, readBChan, readBChan2, writeBChan) -import Brick.Types (EventM(..)) +import Brick.Types.EventM import Brick.Types.Internal import Brick.Widgets.Internal import Brick.AttrMap diff --git a/src/Brick/Types.hs b/src/Brick/Types.hs index f67c243..f50f5c1 100644 --- a/src/Brick/Types.hs +++ b/src/Brick/Types.hs @@ -1,9 +1,6 @@ -- | Basic types used by this library. {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE RankNTypes #-} -{-# LANGUAGE GeneralizedNewtypeDeriving #-} -{-# LANGUAGE FlexibleInstances #-} -{-# LANGUAGE MultiParamTypeClasses #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Brick.Types ( -- * The Widget type @@ -30,7 +27,7 @@ module Brick.Types , ClickableScrollbarElement(..) -- * Event-handling types - , EventM(..) + , EventM , BrickEvent(..) , withLens , nestEventM @@ -104,7 +101,6 @@ where import Lens.Micro (_1, _2, to, (^.), Lens') import Lens.Micro.Type (Getting) import Lens.Micro.Mtl ((.=), use) -import Control.Monad.Catch (MonadThrow, MonadCatch, MonadMask) #if !MIN_VERSION_base(4,13,0) import Control.Monad.Fail (MonadFail) #endif @@ -114,6 +110,7 @@ import Graphics.Vty (Attr) import Brick.Types.TH import Brick.Types.Internal +import Brick.Types.EventM import Brick.AttrMap (AttrName, attrMapLookup) -- | The type of padding. @@ -169,20 +166,6 @@ withLens target act = do target .= val' return result --- | The monad in which event handlers run. Although it may be tempting --- to dig into the reader value yourself, just use --- 'Brick.Main.lookupViewport'. -newtype EventM n s a = - EventM { runEventM :: ReaderT (EventRO n) (StateT (EventState n s) IO) a - } - deriving ( Functor, Applicative, Monad, MonadIO - , MonadThrow, MonadCatch, MonadMask, MonadFail - ) - -instance MonadState s (EventM n s) where - get = EventM $ lift $ gets applicationState - put s = EventM $ lift $ modify $ \es -> es { applicationState = s } - -- | The rendering context's current drawing attribute. attrL :: forall r n. Getting r (Context n) Attr attrL = to (\c -> attrMapLookup (c^.ctxAttrNameL) (c^.ctxAttrMapL)) diff --git a/src/Brick/Types/EventM.hs b/src/Brick/Types/EventM.hs new file mode 100644 index 0000000..3de7759 --- /dev/null +++ b/src/Brick/Types/EventM.hs @@ -0,0 +1,28 @@ +{-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE FlexibleInstances #-} +module Brick.Types.EventM + ( EventM(..) + ) +where + +import Control.Monad.Catch (MonadThrow, MonadCatch, MonadMask) +import Control.Monad.Reader +import Control.Monad.State.Strict + +import Brick.Types.Internal + +-- | The monad in which event handlers run. Although it may be tempting +-- to dig into the reader value yourself, just use +-- 'Brick.Main.lookupViewport'. +newtype EventM n s a = + EventM { runEventM :: ReaderT (EventRO n) (StateT (EventState n s) IO) a + } + deriving ( Functor, Applicative, Monad, MonadIO + , MonadThrow, MonadCatch, MonadMask, MonadFail + ) + +instance MonadState s (EventM n s) where + get = EventM $ lift $ gets applicationState + put s = EventM $ lift $ modify $ \es -> es { applicationState = s } +