-- | Basic types used by this library.
{-# LANGUAGE RankNTypes #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Brick.Types
  ( -- * The Widget type
    Widget(..)

    -- * Location types and lenses
  , Location(..)
  , locL
  , TerminalLocation(..)
  , CursorLocation(..)
  , cursorLocationL
  , cursorLocationNameL

  -- * Viewports
  , Viewport(..)
  , ViewportType(..)
  , vpSize
  , vpTop
  , vpLeft
  , vpContentSize
  , VScrollBarOrientation(..)
  , HScrollBarOrientation(..)
  , ScrollbarRenderer(..)
  , ClickableScrollbarElement(..)

  -- * Event-handling types and functions
  , EventM
  , BrickEvent(..)
  , nestEventM
  , nestEventM'

  -- * Rendering infrastructure
  , RenderM
  , getContext

  -- ** The rendering context
  , Context(ctxAttrName, availWidth, availHeight, windowWidth, windowHeight, ctxBorderStyle, ctxAttrMap, ctxDynBorders)
  , attrL
  , availWidthL
  , availHeightL
  , windowWidthL
  , windowHeightL
  , ctxVScrollBarOrientationL
  , ctxVScrollBarRendererL
  , ctxHScrollBarOrientationL
  , ctxHScrollBarRendererL
  , ctxAttrMapL
  , ctxAttrNameL
  , ctxBorderStyleL
  , ctxDynBordersL

  -- ** Rendering results
  , Result(..)
  , emptyResult
  , lookupAttrName
  , Extent(..)

  -- ** Rendering result lenses
  , imageL
  , cursorsL
  , visibilityRequestsL
  , extentsL

  -- ** Visibility requests
  , VisibilityRequest(..)
  , vrPositionL
  , vrSizeL

  -- * Making lenses
  , suffixLenses
  , suffixLensesWith

  -- * Dynamic borders
  , bordersL
  , DynBorder(..)
  , dbStyleL, dbAttrL, dbSegmentsL
  , BorderSegment(..)
  , bsAcceptL, bsOfferL, bsDrawL
  , Edges(..)
  , eTopL, eBottomL, eRightL, eLeftL

  -- * Miscellaneous
  , Size(..)
  , Direction(..)

  -- * Renderer internals (for benchmarking)
  , RenderState

  -- * Re-exports for convenience
  , get
  , gets
  , put
  , modify
  , zoom
  )
where

import Lens.Micro (_1, _2, to, (^.))
import Lens.Micro.Type (Getting)
import Lens.Micro.Mtl (zoom)
#if !MIN_VERSION_base(4,13,0)
import Control.Monad.Fail (MonadFail)
#endif
import Control.Monad.State.Strict
import Control.Monad.Reader
import Graphics.Vty (Attr)

import Brick.Types.TH
import Brick.Types.Internal
import Brick.Types.EventM
import Brick.AttrMap (AttrName, attrMapLookup)

-- | Given a state value and an 'EventM' that mutates that state, run
-- the specified action and return resulting modified state.
nestEventM' :: a
           -- ^ The initial state to use in the nested action.
            -> EventM n a b
            -- ^ The action to run.
            -> EventM n s a
nestEventM' :: forall a n b s. a -> EventM n a b -> EventM n s a
nestEventM' a
s EventM n a b
act = forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a n b s. a -> EventM n a b -> EventM n s (a, b)
nestEventM a
s EventM n a b
act

-- | Given a state value and an 'EventM' that mutates that state, run
-- the specified action and return both the resulting modified state and
-- the result of the action itself.
nestEventM :: a
           -- ^ The initial state to use in the nested action.
           -> EventM n a b
           -- ^ The action to run.
           -> EventM n s (a, b)
nestEventM :: forall a n b s. a -> EventM n a b -> EventM n s (a, b)
nestEventM a
s' EventM n a b
act = do
    EventRO n
ro <- forall n s a.
ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) a
-> EventM n s a
EventM forall r (m :: * -> *). MonadReader r m => m r
ask
    EventState n
es <- forall n s a.
ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) a
-> EventM n s a
EventM forall a b. (a -> b) -> a -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
    VtyContext
vtyCtx <- forall n s. EventM n s VtyContext
getVtyContext
    let stInner :: EventState n
stInner = ES { nextAction :: NextAction
nextAction = NextAction
Continue
                     , esScrollRequests :: [(n, ScrollRequest)]
esScrollRequests = forall n. EventState n -> [(n, ScrollRequest)]
esScrollRequests EventState n
es
                     , cacheInvalidateRequests :: Set (CacheInvalidateRequest n)
cacheInvalidateRequests = forall n. EventState n -> Set (CacheInvalidateRequest n)
cacheInvalidateRequests EventState n
es
                     , requestedVisibleNames :: Set n
requestedVisibleNames = forall n. EventState n -> Set n
requestedVisibleNames EventState n
es
                     , vtyContext :: VtyContext
vtyContext = VtyContext
vtyCtx
                     }
    ((b
actResult, a
newSt), EventState n
stInnerFinal) <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT (forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT (forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (forall n s a.
EventM n s a
-> ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) a
runEventM EventM n a b
act) EventRO n
ro) a
s') EventState n
stInner

    forall n s a.
ReaderT (EventRO n) (StateT s (StateT (EventState n) IO)) a
-> EventM n s a
EventM forall a b. (a -> b) -> a -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$
        \EventState n
st -> EventState n
st { nextAction :: NextAction
nextAction = forall n. EventState n -> NextAction
nextAction EventState n
stInnerFinal
                  , esScrollRequests :: [(n, ScrollRequest)]
esScrollRequests = forall n. EventState n -> [(n, ScrollRequest)]
esScrollRequests EventState n
stInnerFinal
                  , cacheInvalidateRequests :: Set (CacheInvalidateRequest n)
cacheInvalidateRequests = forall n. EventState n -> Set (CacheInvalidateRequest n)
cacheInvalidateRequests EventState n
stInnerFinal
                  , requestedVisibleNames :: Set n
requestedVisibleNames = forall n. EventState n -> Set n
requestedVisibleNames EventState n
stInnerFinal
                  , vtyContext :: VtyContext
vtyContext = forall n. EventState n -> VtyContext
vtyContext EventState n
stInnerFinal
                  }
    forall (m :: * -> *) a. Monad m => a -> m a
return (a
newSt, b
actResult)

-- | The rendering context's current drawing attribute.
attrL :: forall r n. Getting r (Context n) Attr
attrL :: forall r n. Getting r (Context n) Attr
attrL = forall s a. (s -> a) -> SimpleGetter s a
to (\Context n
c -> AttrName -> AttrMap -> Attr
attrMapLookup (Context n
cforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Context n) AttrName
ctxAttrNameL) (Context n
cforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Context n) AttrMap
ctxAttrMapL))

instance TerminalLocation (CursorLocation n) where
    locationColumnL :: Lens' (CursorLocation n) Int
locationColumnL = forall n. Lens' (CursorLocation n) Location
cursorLocationLforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s t a b. Field1 s t a b => Lens s t a b
_1
    locationColumn :: CursorLocation n -> Int
locationColumn = forall a. TerminalLocation a => a -> Int
locationColumn forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. CursorLocation n -> Location
cursorLocation
    locationRowL :: Lens' (CursorLocation n) Int
locationRowL = forall n. Lens' (CursorLocation n) Location
cursorLocationLforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s t a b. Field2 s t a b => Lens s t a b
_2
    locationRow :: CursorLocation n -> Int
locationRow = forall a. TerminalLocation a => a -> Int
locationRow forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. CursorLocation n -> Location
cursorLocation

-- | Given an attribute name, obtain the attribute for the attribute
-- name by consulting the context's attribute map.
lookupAttrName :: AttrName -> RenderM n Attr
lookupAttrName :: forall n. AttrName -> RenderM n Attr
lookupAttrName AttrName
n = do
    Context n
c <- forall n. RenderM n (Context n)
getContext
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ AttrName -> AttrMap -> Attr
attrMapLookup AttrName
n (Context n
cforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Context n) AttrMap
ctxAttrMapL)