{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
module Brick.Widgets.Internal
( renderFinal
, cropToContext
, cropResultToContext
, renderDynBorder
, renderWidget
)
where
import Lens.Micro ((^.), (&), (%~), (.~))
import Lens.Micro.Mtl ((%=))
import Control.Monad
import Control.Monad.State.Strict
import Control.Monad.Reader
import qualified Data.Foldable as F
import qualified Data.Sequence as Seq
import qualified Data.Traversable as T
import Data.Maybe (fromMaybe, mapMaybe)
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Graphics.Vty as V
import Brick.Types
import Brick.Types.Internal
import Brick.AttrMap
import Brick.Widgets.Border.Style
import Brick.BorderMap (BorderMap)
import qualified Brick.BorderMap as BM
renderFinal :: (Ord n)
=> AttrMap
-> [Widget n]
-> V.DisplayRegion
-> ([CursorLocation n] -> Maybe (CursorLocation n))
-> RenderState n
-> (RenderState n, V.Picture, Maybe (CursorLocation n), [LayerExtents n])
renderFinal :: forall n.
Ord n =>
AttrMap
-> [Widget n]
-> DisplayRegion
-> ([CursorLocation n] -> Maybe (CursorLocation n))
-> RenderState n
-> (RenderState n, Picture, Maybe (CursorLocation n),
[LayerExtents n])
renderFinal AttrMap
aMap [Widget n]
layerRenders (Int
w, Int
h) [CursorLocation n] -> Maybe (CursorLocation n)
chooseCursor RenderState n
rs =
(RenderState n
newRS, Picture
picWithBg, Maybe (CursorLocation n)
theCursor, Seq (LayerExtents n) -> [LayerExtents n]
forall a. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList Seq (LayerExtents n)
layerExtents)
where
resetRs :: RenderState n
resetRs = RenderState n
rs RenderState n -> (RenderState n -> RenderState n) -> RenderState n
forall a b. a -> (a -> b) -> b
& (Map n (Extent n) -> Identity (Map n (Extent n)))
-> RenderState n -> Identity (RenderState n)
forall n (f :: * -> *).
Functor f =>
(Map n (Extent n) -> f (Map n (Extent n)))
-> RenderState n -> f (RenderState n)
reportedExtentsL ((Map n (Extent n) -> Identity (Map n (Extent n)))
-> RenderState n -> Identity (RenderState n))
-> Map n (Extent n) -> RenderState n -> RenderState n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Map n (Extent n)
forall a. Monoid a => a
mempty
RenderState n -> (RenderState n -> RenderState n) -> RenderState n
forall a b. a -> (a -> b) -> b
& (Set n -> Identity (Set n))
-> RenderState n -> Identity (RenderState n)
forall n (f :: * -> *).
Functor f =>
(Set n -> f (Set n)) -> RenderState n -> f (RenderState n)
observedNamesL ((Set n -> Identity (Set n))
-> RenderState n -> Identity (RenderState n))
-> Set n -> RenderState n -> RenderState n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Set n
forall a. Monoid a => a
mempty
RenderState n -> (RenderState n -> RenderState n) -> RenderState n
forall a b. a -> (a -> b) -> b
& (Set n -> Identity (Set n))
-> RenderState n -> Identity (RenderState n)
forall n (f :: * -> *).
Functor f =>
(Set n -> f (Set n)) -> RenderState n -> f (RenderState n)
clickableNamesL ((Set n -> Identity (Set n))
-> RenderState n -> Identity (RenderState n))
-> Set n -> RenderState n -> RenderState n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Set n
forall a. Monoid a => a
mempty
(Seq (DisplayRegion, Result n)
allLayers, !RenderState n
newRS) = (StateT (RenderState n) Identity (Seq (DisplayRegion, Result n))
-> RenderState n -> (Seq (DisplayRegion, Result n), RenderState n))
-> RenderState n
-> StateT (RenderState n) Identity (Seq (DisplayRegion, Result n))
-> (Seq (DisplayRegion, Result n), RenderState n)
forall a b c. (a -> b -> c) -> b -> a -> c
flip StateT (RenderState n) Identity (Seq (DisplayRegion, Result n))
-> RenderState n -> (Seq (DisplayRegion, Result n), RenderState n)
forall s a. State s a -> s -> (a, s)
runState RenderState n
resetRs (StateT (RenderState n) Identity (Seq (DisplayRegion, Result n))
-> (Seq (DisplayRegion, Result n), RenderState n))
-> StateT (RenderState n) Identity (Seq (DisplayRegion, Result n))
-> (Seq (DisplayRegion, Result n), RenderState n)
forall a b. (a -> b) -> a -> b
$ do
Seq (Widget n)
-> StateT (RenderState n) Identity (Seq (DisplayRegion, Result n))
forall {n}.
Ord n =>
Seq (Widget n)
-> State (RenderState n) (Seq (DisplayRegion, Result n))
go (Seq (Widget n)
-> StateT (RenderState n) Identity (Seq (DisplayRegion, Result n)))
-> Seq (Widget n)
-> StateT (RenderState n) Identity (Seq (DisplayRegion, Result n))
forall a b. (a -> b) -> a -> b
$ [Widget n] -> Seq (Widget n)
forall a. [a] -> Seq a
Seq.fromList [Widget n]
layerRenders
where
go :: Seq (Widget n)
-> State (RenderState n) (Seq (DisplayRegion, Result n))
go Seq (Widget n)
layers =
case Seq (Widget n) -> ViewR (Widget n)
forall a. Seq a -> ViewR a
Seq.viewr Seq (Widget n)
layers of
ViewR (Widget n)
Seq.EmptyR -> Seq (DisplayRegion, Result n)
-> State (RenderState n) (Seq (DisplayRegion, Result n))
forall a. a -> State (RenderState n) a
forall (m :: * -> *) a. Monad m => a -> m a
return Seq (DisplayRegion, Result n)
forall a. Monoid a => a
mempty
Seq (Widget n)
rest Seq.:> Widget n
next -> do
thisLayerResults <- (ReaderT
(Context n) (State (RenderState n)) (Seq (DisplayRegion, Result n))
-> Context n
-> State (RenderState n) (Seq (DisplayRegion, Result n)))
-> Context n
-> ReaderT
(Context n) (State (RenderState n)) (Seq (DisplayRegion, Result n))
-> State (RenderState n) (Seq (DisplayRegion, Result n))
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT
(Context n) (State (RenderState n)) (Seq (DisplayRegion, Result n))
-> Context n
-> State (RenderState n) (Seq (DisplayRegion, Result n))
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT Context n
forall {n}. Context n
ctx (ReaderT
(Context n) (State (RenderState n)) (Seq (DisplayRegion, Result n))
-> State (RenderState n) (Seq (DisplayRegion, Result n)))
-> ReaderT
(Context n) (State (RenderState n)) (Seq (DisplayRegion, Result n))
-> State (RenderState n) (Seq (DisplayRegion, Result n))
forall a b. (a -> b) -> a -> b
$
Widget n
-> ReaderT
(Context n) (State (RenderState n)) (Seq (DisplayRegion, Result n))
forall {n}.
Ord n =>
Widget n
-> ReaderT
(Context n) (State (RenderState n)) (Seq (DisplayRegion, Result n))
processMainLayer Widget n
next
restResults <- go rest
return $ restResults <> thisLayerResults
processMainLayer :: Widget n
-> ReaderT
(Context n) (State (RenderState n)) (Seq (DisplayRegion, Result n))
processMainLayer Widget n
layerWidget = do
let recordExtents :: Result k -> m ()
recordExtents Result k
r =
[Extent k] -> (Extent k -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Result k
rResult k -> Getting [Extent k] (Result k) [Extent k] -> [Extent k]
forall s a. s -> Getting a s a -> a
^.Getting [Extent k] (Result k) [Extent k]
forall n (f :: * -> *).
Functor f =>
([Extent n] -> f [Extent n]) -> Result n -> f (Result n)
extentsL) ((Extent k -> m ()) -> m ()) -> (Extent k -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Extent k
e ->
(Map k (Extent k) -> Identity (Map k (Extent k)))
-> RenderState k -> Identity (RenderState k)
forall n (f :: * -> *).
Functor f =>
(Map n (Extent n) -> f (Map n (Extent n)))
-> RenderState n -> f (RenderState n)
reportedExtentsL ((Map k (Extent k) -> Identity (Map k (Extent k)))
-> RenderState k -> Identity (RenderState k))
-> (Map k (Extent k) -> Map k (Extent k)) -> m ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= k -> Extent k -> Map k (Extent k) -> Map k (Extent k)
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert (Extent k -> k
forall n. Extent n -> n
extentName Extent k
e) Extent k
e
preTranslation <- Widget n -> RenderM n (Result n)
forall n. Widget n -> RenderM n (Result n)
render (Widget n -> RenderM n (Result n))
-> Widget n -> RenderM n (Result n)
forall a b. (a -> b) -> a -> b
$ Widget n -> Widget n
forall n. Widget n -> Widget n
cropToContext Widget n
layerWidget
let result = Result n -> Result n
forall {n}. Result n -> Result n
translateResult Result n
preTranslation
recordExtents result
let gatherLayer Result n
r = do
let r' :: Result n
r' = Result n -> Result n
forall {n}. Result n -> Result n
translateResult Result n
r
Result n -> m ()
forall {m :: * -> *} {k}.
(MonadState (RenderState k) m, Ord k) =>
Result k -> m ()
recordExtents Result n
r'
rest <- (Result n -> m (Seq (DisplayRegion, Result n)))
-> Seq (Result n) -> m (Seq (Seq (DisplayRegion, Result n)))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Seq a -> m (Seq b)
T.mapM Result n -> m (Seq (DisplayRegion, Result n))
gatherLayer (Seq (Result n) -> m (Seq (Seq (DisplayRegion, Result n))))
-> Seq (Result n) -> m (Seq (Seq (DisplayRegion, Result n)))
forall a b. (a -> b) -> a -> b
$ Result n
r'Result n
-> Getting (Seq (Result n)) (Result n) (Seq (Result n))
-> Seq (Result n)
forall s a. s -> Getting a s a -> a
^.Getting (Seq (Result n)) (Result n) (Seq (Result n))
forall n (f :: * -> *).
Functor f =>
(Seq (Result n) -> f (Seq (Result n))) -> Result n -> f (Result n)
extraLayersL
return $ concatSeq rest Seq.|> (resultSize r, r')
translatedLayerResults <- T.mapM gatherLayer $ result^.extraLayersL
return $ concatSeq translatedLayerResults Seq.|> (resultSize preTranslation, result)
getTranslationOffset :: Result n -> Location
getTranslationOffset Result n
r =
let originalOffset :: Location
originalOffset = Result n -> Location
forall n. Result n -> Location
translationOffset Result n
r
correction :: Location
correction = Result n -> Location
forall n. Result n -> Location
getTranslationCorrection Result n
r
in Location
originalOffset Location -> Location -> Location
forall a. Semigroup a => a -> a -> a
<> Location
correction
getTranslationCorrection :: Result n -> Location
getTranslationCorrection Result n
r =
let Location (Int
hOff, Int
vOff) = Result n -> Location
forall n. Result n -> Location
translationOffset Result n
r
rWidth :: Int
rWidth = Image -> Int
V.imageWidth (Result n
rResult n -> Getting Image (Result n) Image -> Image
forall s a. s -> Getting a s a -> a
^.Getting Image (Result n) Image
forall n (f :: * -> *).
Functor f =>
(Image -> f Image) -> Result n -> f (Result n)
imageL)
rHeight :: Int
rHeight = Image -> Int
V.imageHeight (Result n
rResult n -> Getting Image (Result n) Image -> Image
forall s a. s -> Getting a s a -> a
^.Getting Image (Result n) Image
forall n (f :: * -> *).
Functor f =>
(Image -> f Image) -> Result n -> f (Result n)
imageL)
colCorrection :: Int
colCorrection = if Int
hOff Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
then Int -> Int
forall a. Num a => a -> a
abs Int
hOff
else if Int
hOff Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
rWidth Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
w
then Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
hOff Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
rWidth)
else Int
0
rowCorrection :: Int
rowCorrection = if Int
vOff Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
then Int -> Int
forall a. Num a => a -> a
abs Int
vOff
else if Int
vOff Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
rHeight Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
h
then Int
h Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
vOff Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
rHeight)
else Int
0
hCorrection :: Location
hCorrection = case Result n -> ClampPolicy
forall n. Result n -> ClampPolicy
horizontalClampPolicy Result n
r of
ClampPolicy
Truncate -> DisplayRegion -> Location
Location (Int
0, Int
0)
ClampPolicy
Reposition -> DisplayRegion -> Location
Location (Int
colCorrection, Int
0)
vCorrection :: Location
vCorrection = case Result n -> ClampPolicy
forall n. Result n -> ClampPolicy
verticalClampPolicy Result n
r of
ClampPolicy
Truncate -> DisplayRegion -> Location
Location (Int
0, Int
0)
ClampPolicy
Reposition -> DisplayRegion -> Location
Location (Int
0, Int
rowCorrection)
in Location
hCorrection Location -> Location -> Location
forall a. Semigroup a => a -> a -> a
<> Location
vCorrection
translateResult :: Result n -> Result n
translateResult Result n
r =
let off :: Location
off = Result n -> Location
forall n. Result n -> Location
getTranslationOffset Result n
r
in Location -> Result n -> Result n
forall n. Location -> Result n -> Result n
addResultOffset Location
off (Result n -> Result n) -> Result n -> Result n
forall a b. (a -> b) -> a -> b
$
Result n
r Result n -> (Result n -> Result n) -> Result n
forall a b. a -> (a -> b) -> b
& (Image -> Identity Image) -> Result n -> Identity (Result n)
forall n (f :: * -> *).
Functor f =>
(Image -> f Image) -> Result n -> f (Result n)
imageL ((Image -> Identity Image) -> Result n -> Identity (Result n))
-> (Image -> Image) -> Result n -> Result n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (Int -> Int -> Image -> Image
V.translate (Location
offLocation -> Getting Int Location Int -> Int
forall s a. s -> Getting a s a -> a
^.Getting Int Location Int
forall a. TerminalLocation a => Lens' a Int
Lens' Location Int
locationColumnL) (Location
offLocation -> Getting Int Location Int -> Int
forall s a. s -> Getting a s a -> a
^.Getting Int Location Int
forall a. TerminalLocation a => Lens' a Int
Lens' Location Int
locationRowL))
resultSize :: Result n -> DisplayRegion
resultSize Result n
r = (Image -> Int
V.imageWidth Image
i, Image -> Int
V.imageHeight Image
i)
where
i :: Image
i = Result n
rResult n -> Getting Image (Result n) Image -> Image
forall s a. s -> Getting a s a -> a
^.Getting Image (Result n) Image
forall n (f :: * -> *).
Functor f =>
(Image -> f Image) -> Result n -> f (Result n)
imageL
concatSeq :: t (Seq a) -> Seq a
concatSeq t (Seq a)
ss =
(Seq a -> Seq a -> Seq a) -> Seq a -> t (Seq a) -> Seq a
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr Seq a -> Seq a -> Seq a
forall a. Seq a -> Seq a -> Seq a
(Seq.><) Seq a
forall a. Seq a
Seq.empty t (Seq a)
ss
ctx :: Context n
ctx = Context { ctxAttrName :: AttrName
ctxAttrName = AttrName
forall a. Monoid a => a
mempty
, availWidth :: Int
availWidth = Int
w
, availHeight :: Int
availHeight = Int
h
, windowWidth :: Int
windowWidth = Int
w
, windowHeight :: Int
windowHeight = Int
h
, ctxBorderStyle :: BorderStyle
ctxBorderStyle = BorderStyle
defaultBorderStyle
, ctxAttrMap :: AttrMap
ctxAttrMap = AttrMap
aMap
, ctxOrigAttrMap :: AttrMap
ctxOrigAttrMap = AttrMap
aMap
, ctxDynBorders :: Bool
ctxDynBorders = Bool
False
, ctxVScrollBarOrientation :: Maybe VScrollBarOrientation
ctxVScrollBarOrientation = Maybe VScrollBarOrientation
forall a. Maybe a
Nothing
, ctxVScrollBarRenderer :: Maybe (VScrollbarRenderer n)
ctxVScrollBarRenderer = Maybe (VScrollbarRenderer n)
forall a. Maybe a
Nothing
, ctxHScrollBarOrientation :: Maybe HScrollBarOrientation
ctxHScrollBarOrientation = Maybe HScrollBarOrientation
forall a. Maybe a
Nothing
, ctxHScrollBarRenderer :: Maybe (HScrollbarRenderer n)
ctxHScrollBarRenderer = Maybe (HScrollbarRenderer n)
forall a. Maybe a
Nothing
, ctxHScrollBarShowHandles :: Bool
ctxHScrollBarShowHandles = Bool
False
, ctxVScrollBarShowHandles :: Bool
ctxVScrollBarShowHandles = Bool
False
, ctxHScrollBarClickableConstr :: Maybe (ClickableScrollbarElement -> n -> n)
ctxHScrollBarClickableConstr = Maybe (ClickableScrollbarElement -> n -> n)
forall a. Maybe a
Nothing
, ctxVScrollBarClickableConstr :: Maybe (ClickableScrollbarElement -> n -> n)
ctxVScrollBarClickableConstr = Maybe (ClickableScrollbarElement -> n -> n)
forall a. Maybe a
Nothing
}
pic :: Picture
pic = [Image] -> Picture
V.picForLayers ([Image] -> Picture) -> [Image] -> Picture
forall a b. (a -> b) -> a -> b
$ Seq Image -> [Image]
forall a. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList (Seq Image -> [Image]) -> Seq Image -> [Image]
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Image -> Image
V.resize Int
w Int
h (Image -> Image) -> (Result n -> Image) -> Result n -> Image
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Result n -> Getting Image (Result n) Image -> Image
forall s a. s -> Getting a s a -> a
^.Getting Image (Result n) Image
forall n (f :: * -> *).
Functor f =>
(Image -> f Image) -> Result n -> f (Result n)
imageL) (Result n -> Image)
-> ((DisplayRegion, Result n) -> Result n)
-> (DisplayRegion, Result n)
-> Image
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (DisplayRegion, Result n) -> Result n
forall a b. (a, b) -> b
snd ((DisplayRegion, Result n) -> Image)
-> Seq (DisplayRegion, Result n) -> Seq Image
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq (DisplayRegion, Result n)
allLayers
picWithBg :: Picture
picWithBg = Picture
pic { V.picBackground = V.Background ' ' V.defAttr }
(Seq [CursorLocation n]
layerCursors, Seq (LayerExtents n)
layerExtents) = ((DisplayRegion, Result n) -> ([CursorLocation n], LayerExtents n))
-> Seq (DisplayRegion, Result n)
-> (Seq [CursorLocation n], Seq (LayerExtents n))
forall a b c. (a -> (b, c)) -> Seq a -> (Seq b, Seq c)
Seq.unzipWith (DisplayRegion, Result n) -> ([CursorLocation n], LayerExtents n)
forall {n}.
(DisplayRegion, Result n) -> ([CursorLocation n], LayerExtents n)
layerInfo Seq (DisplayRegion, Result n)
allLayers
layerInfo :: (DisplayRegion, Result n) -> ([CursorLocation n], LayerExtents n)
layerInfo (DisplayRegion
untranslatedSize, Result n
l) = (Result n
lResult n
-> Getting [CursorLocation n] (Result n) [CursorLocation n]
-> [CursorLocation n]
forall s a. s -> Getting a s a -> a
^.Getting [CursorLocation n] (Result n) [CursorLocation n]
forall n (f :: * -> *).
Functor f =>
([CursorLocation n] -> f [CursorLocation n])
-> Result n -> f (Result n)
cursorsL, DisplayRegion -> Result n -> LayerExtents n
forall {n}. DisplayRegion -> Result n -> LayerExtents n
mkLayerExtents DisplayRegion
untranslatedSize Result n
l)
mkLayerExtents :: DisplayRegion -> Result n -> LayerExtents n
mkLayerExtents DisplayRegion
untranslatedSize Result n
l =
Location -> DisplayRegion -> [Extent n] -> LayerExtents n
forall n. Location -> DisplayRegion -> [Extent n] -> LayerExtents n
LayerExtents (Result n
lResult n -> Getting Location (Result n) Location -> Location
forall s a. s -> Getting a s a -> a
^.Getting Location (Result n) Location
forall n (f :: * -> *).
Functor f =>
(Location -> f Location) -> Result n -> f (Result n)
translationOffsetL) DisplayRegion
untranslatedSize ([Extent n] -> LayerExtents n) -> [Extent n] -> LayerExtents n
forall a b. (a -> b) -> a -> b
$ Result n
lResult n -> Getting [Extent n] (Result n) [Extent n] -> [Extent n]
forall s a. s -> Getting a s a -> a
^.Getting [Extent n] (Result n) [Extent n]
forall n (f :: * -> *).
Functor f =>
([Extent n] -> f [Extent n]) -> Result n -> f (Result n)
extentsL
theCursor :: Maybe (CursorLocation n)
theCursor = [CursorLocation n] -> Maybe (CursorLocation n)
chooseCursor ([CursorLocation n] -> Maybe (CursorLocation n))
-> [CursorLocation n] -> Maybe (CursorLocation n)
forall a b. (a -> b) -> a -> b
$ [[CursorLocation n]] -> [CursorLocation n]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[CursorLocation n]] -> [CursorLocation n])
-> [[CursorLocation n]] -> [CursorLocation n]
forall a b. (a -> b) -> a -> b
$ Seq [CursorLocation n] -> [[CursorLocation n]]
forall a. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList Seq [CursorLocation n]
layerCursors
cropToContext :: Widget n -> Widget n
cropToContext :: forall n. Widget n -> Widget n
cropToContext Widget n
p =
Size -> Size -> RenderM n (Result n) -> Widget n
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget (Widget n -> Size
forall n. Widget n -> Size
hSize Widget n
p) (Widget n -> Size
forall n. Widget n -> Size
vSize Widget n
p) (Widget n -> RenderM n (Result n)
forall n. Widget n -> RenderM n (Result n)
render Widget n
p RenderM n (Result n)
-> (Result n -> RenderM n (Result n)) -> RenderM n (Result n)
forall a b.
ReaderT (Context n) (State (RenderState n)) a
-> (a -> ReaderT (Context n) (State (RenderState n)) b)
-> ReaderT (Context n) (State (RenderState n)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Result n -> RenderM n (Result n)
forall n. Result n -> RenderM n (Result n)
cropResultToContext)
cropResultToContext :: Result n -> RenderM n (Result n)
cropResultToContext :: forall n. Result n -> RenderM n (Result n)
cropResultToContext Result n
result = do
c <- RenderM n (Context n)
forall n. RenderM n (Context n)
getContext
return $ result & imageL %~ cropImage c
& cursorsL %~ cropCursors c
& extentsL %~ cropExtents c
& bordersL %~ cropBorders c
cropImage :: Context n -> V.Image -> V.Image
cropImage :: forall n. Context n -> Image -> Image
cropImage Context n
c = Int -> Int -> Image -> Image
V.crop (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Context n
cContext n -> Getting Int (Context n) Int -> Int
forall s a. s -> Getting a s a -> a
^.Getting Int (Context n) Int
forall n (f :: * -> *).
Functor f =>
(Int -> f Int) -> Context n -> f (Context n)
availWidthL) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Context n
cContext n -> Getting Int (Context n) Int -> Int
forall s a. s -> Getting a s a -> a
^.Getting Int (Context n) Int
forall n (f :: * -> *).
Functor f =>
(Int -> f Int) -> Context n -> f (Context n)
availHeightL)
cropCursors :: Context n -> [CursorLocation n] -> [CursorLocation n]
cropCursors :: forall n. Context n -> [CursorLocation n] -> [CursorLocation n]
cropCursors Context n
ctx [CursorLocation n]
cs = (CursorLocation n -> Maybe (CursorLocation n))
-> [CursorLocation n] -> [CursorLocation n]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe CursorLocation n -> Maybe (CursorLocation n)
forall {n}. CursorLocation n -> Maybe (CursorLocation n)
cropCursor [CursorLocation n]
cs
where
cropCursor :: CursorLocation n -> Maybe (CursorLocation n)
cropCursor CursorLocation n
c | CursorLocation n -> Bool
forall {n}. CursorLocation n -> Bool
outOfContext CursorLocation n
c = Maybe (CursorLocation n)
forall a. Maybe a
Nothing
| Bool
otherwise = CursorLocation n -> Maybe (CursorLocation n)
forall a. a -> Maybe a
Just CursorLocation n
c
outOfContext :: CursorLocation n -> Bool
outOfContext CursorLocation n
c =
[Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [ CursorLocation n
cCursorLocation n -> Getting Int (CursorLocation n) Int -> Int
forall s a. s -> Getting a s a -> a
^.(Location -> Const Int Location)
-> CursorLocation n -> Const Int (CursorLocation n)
forall n (f :: * -> *).
Functor f =>
(Location -> f Location)
-> CursorLocation n -> f (CursorLocation n)
cursorLocationL((Location -> Const Int Location)
-> CursorLocation n -> Const Int (CursorLocation n))
-> Getting Int Location Int -> Getting Int (CursorLocation n) Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Getting Int Location Int
forall a. TerminalLocation a => Lens' a Int
Lens' Location Int
locationRowL Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
, CursorLocation n
cCursorLocation n -> Getting Int (CursorLocation n) Int -> Int
forall s a. s -> Getting a s a -> a
^.(Location -> Const Int Location)
-> CursorLocation n -> Const Int (CursorLocation n)
forall n (f :: * -> *).
Functor f =>
(Location -> f Location)
-> CursorLocation n -> f (CursorLocation n)
cursorLocationL((Location -> Const Int Location)
-> CursorLocation n -> Const Int (CursorLocation n))
-> Getting Int Location Int -> Getting Int (CursorLocation n) Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Getting Int Location Int
forall a. TerminalLocation a => Lens' a Int
Lens' Location Int
locationColumnL Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
, CursorLocation n
cCursorLocation n -> Getting Int (CursorLocation n) Int -> Int
forall s a. s -> Getting a s a -> a
^.(Location -> Const Int Location)
-> CursorLocation n -> Const Int (CursorLocation n)
forall n (f :: * -> *).
Functor f =>
(Location -> f Location)
-> CursorLocation n -> f (CursorLocation n)
cursorLocationL((Location -> Const Int Location)
-> CursorLocation n -> Const Int (CursorLocation n))
-> Getting Int Location Int -> Getting Int (CursorLocation n) Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Getting Int Location Int
forall a. TerminalLocation a => Lens' a Int
Lens' Location Int
locationRowL Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Context n
ctxContext n -> Getting Int (Context n) Int -> Int
forall s a. s -> Getting a s a -> a
^.Getting Int (Context n) Int
forall n (f :: * -> *).
Functor f =>
(Int -> f Int) -> Context n -> f (Context n)
availHeightL
, CursorLocation n
cCursorLocation n -> Getting Int (CursorLocation n) Int -> Int
forall s a. s -> Getting a s a -> a
^.(Location -> Const Int Location)
-> CursorLocation n -> Const Int (CursorLocation n)
forall n (f :: * -> *).
Functor f =>
(Location -> f Location)
-> CursorLocation n -> f (CursorLocation n)
cursorLocationL((Location -> Const Int Location)
-> CursorLocation n -> Const Int (CursorLocation n))
-> Getting Int Location Int -> Getting Int (CursorLocation n) Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Getting Int Location Int
forall a. TerminalLocation a => Lens' a Int
Lens' Location Int
locationColumnL Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Context n
ctxContext n -> Getting Int (Context n) Int -> Int
forall s a. s -> Getting a s a -> a
^.Getting Int (Context n) Int
forall n (f :: * -> *).
Functor f =>
(Int -> f Int) -> Context n -> f (Context n)
availWidthL
]
cropExtents :: Context n -> [Extent n] -> [Extent n]
cropExtents :: forall n. Context n -> [Extent n] -> [Extent n]
cropExtents Context n
ctx [Extent n]
es = (Extent n -> Maybe (Extent n)) -> [Extent n] -> [Extent n]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Extent n -> Maybe (Extent n)
forall {n}. Extent n -> Maybe (Extent n)
cropExtent [Extent n]
es
where
cropExtent :: Extent n -> Maybe (Extent n)
cropExtent (Extent n
n (Location (Int
c, Int
r)) (Int
w, Int
h)) =
let nonEmpty :: Bool
nonEmpty = Bool
nonEmptyH Bool -> Bool -> Bool
&& Bool
nonEmptyV
nonEmptyH :: Bool
nonEmptyH = Int
newWidth Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
nonEmptyV :: Bool
nonEmptyV = Int
newHeight Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
newWidth :: Int
newWidth = Int
newEndCol Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
newStartCol
newHeight :: Int
newHeight = Int
newEndRow Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
newStartRow
(Int
newStartCol, Int
newStartRow) = DisplayRegion -> DisplayRegion
clampCorner (Int
c, Int
r)
(Int
newEndCol, Int
newEndRow) = DisplayRegion -> DisplayRegion
clampCorner (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
w, Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
h)
clampCorner :: DisplayRegion -> DisplayRegion
clampCorner (Int
cols, Int
rows) =
( Int -> Int -> Int
forall {b}. (Ord b, Num b) => b -> b -> b
clampRange (Context n
ctxContext n -> Getting Int (Context n) Int -> Int
forall s a. s -> Getting a s a -> a
^.Getting Int (Context n) Int
forall n (f :: * -> *).
Functor f =>
(Int -> f Int) -> Context n -> f (Context n)
availWidthL) Int
cols
, Int -> Int -> Int
forall {b}. (Ord b, Num b) => b -> b -> b
clampRange (Context n
ctxContext n -> Getting Int (Context n) Int -> Int
forall s a. s -> Getting a s a -> a
^.Getting Int (Context n) Int
forall n (f :: * -> *).
Functor f =>
(Int -> f Int) -> Context n -> f (Context n)
availHeightL) Int
rows
)
clampRange :: b -> b -> b
clampRange b
bound b
val =
b -> b -> b
forall a. Ord a => a -> a -> a
min b
bound (b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$ b -> b -> b
forall a. Ord a => a -> a -> a
max b
0 b
val
newExtent :: Extent n
newExtent = n -> Location -> DisplayRegion -> Extent n
forall n. n -> Location -> DisplayRegion -> Extent n
Extent n
n (DisplayRegion -> Location
Location (Int
newStartCol, Int
newStartRow)) (Int
newWidth, Int
newHeight)
in if Bool
nonEmpty
then Extent n -> Maybe (Extent n)
forall a. a -> Maybe a
Just Extent n
newExtent
else Maybe (Extent n)
forall a. Maybe a
Nothing
cropBorders :: Context n -> BorderMap DynBorder -> BorderMap DynBorder
cropBorders :: forall n. Context n -> BorderMap DynBorder -> BorderMap DynBorder
cropBorders Context n
ctx = Edges Int -> BorderMap DynBorder -> BorderMap DynBorder
forall a. Edges Int -> BorderMap a -> BorderMap a
BM.crop Edges
{ eTop :: Int
eTop = Int
0
, eBottom :: Int
eBottom = Context n -> Int
forall n. Context n -> Int
availHeight Context n
ctx Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
, eLeft :: Int
eLeft = Int
0
, eRight :: Int
eRight = Context n -> Int
forall n. Context n -> Int
availWidth Context n
ctx Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
}
renderDynBorder :: DynBorder -> V.Image
renderDynBorder :: DynBorder -> Image
renderDynBorder DynBorder
db = Attr -> Char -> Image
V.char (DynBorder -> Attr
dbAttr DynBorder
db) (Char -> Image) -> Char -> Image
forall a b. (a -> b) -> a -> b
$ BorderStyle -> Char
getBorderChar (BorderStyle -> Char) -> BorderStyle -> Char
forall a b. (a -> b) -> a -> b
$ DynBorder -> BorderStyle
dbStyle DynBorder
db
where
getBorderChar :: BorderStyle -> Char
getBorderChar = case BorderSegment -> Bool
bsDraw (BorderSegment -> Bool) -> Edges BorderSegment -> Edges Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DynBorder -> Edges BorderSegment
dbSegments DynBorder
db of
Edges Bool
False Bool
False Bool
False Bool
False -> Char -> BorderStyle -> Char
forall a b. a -> b -> a
const Char
' '
Edges Bool
False Bool
False Bool
_ Bool
_ -> BorderStyle -> Char
bsHorizontal
Edges Bool
_ Bool
_ Bool
False Bool
False -> BorderStyle -> Char
bsVertical
Edges Bool
False Bool
True Bool
False Bool
True -> BorderStyle -> Char
bsCornerTL
Edges Bool
False Bool
True Bool
True Bool
False -> BorderStyle -> Char
bsCornerTR
Edges Bool
True Bool
False Bool
False Bool
True -> BorderStyle -> Char
bsCornerBL
Edges Bool
True Bool
False Bool
True Bool
False -> BorderStyle -> Char
bsCornerBR
Edges Bool
False Bool
True Bool
True Bool
True -> BorderStyle -> Char
bsIntersectT
Edges Bool
True Bool
False Bool
True Bool
True -> BorderStyle -> Char
bsIntersectB
Edges Bool
True Bool
True Bool
False Bool
True -> BorderStyle -> Char
bsIntersectL
Edges Bool
True Bool
True Bool
True Bool
False -> BorderStyle -> Char
bsIntersectR
Edges Bool
True Bool
True Bool
True Bool
True -> BorderStyle -> Char
bsIntersectFull
renderWidget :: (Ord n)
=> Maybe AttrMap
-> [Widget n]
-> V.DisplayRegion
-> V.Picture
renderWidget :: forall n.
Ord n =>
Maybe AttrMap -> [Widget n] -> DisplayRegion -> Picture
renderWidget Maybe AttrMap
mAttrMap [Widget n]
layerRenders DisplayRegion
region = Picture
pic
where
initialRS :: RenderState n
initialRS = RS { viewportMap :: Map n Viewport
viewportMap = Map n Viewport
forall k a. Map k a
M.empty
, rsScrollRequests :: [(n, ScrollRequest)]
rsScrollRequests = []
, observedNames :: Set n
observedNames = Set n
forall a. Set a
S.empty
, renderCache :: Map n (Set n, Result n)
renderCache = Map n (Set n, Result n)
forall a. Monoid a => a
mempty
, clickableNames :: Set n
clickableNames = Set n
forall a. Monoid a => a
mempty
, requestedVisibleNames_ :: Set n
requestedVisibleNames_ = Set n
forall a. Set a
S.empty
, reportedExtents :: Map n (Extent n)
reportedExtents = Map n (Extent n)
forall a. Monoid a => a
mempty
}
am :: AttrMap
am = AttrMap -> Maybe AttrMap -> AttrMap
forall a. a -> Maybe a -> a
fromMaybe (Attr -> [(AttrName, Attr)] -> AttrMap
attrMap Attr
V.defAttr []) Maybe AttrMap
mAttrMap
(RenderState n
_, Picture
pic, Maybe (CursorLocation n)
_, [LayerExtents n]
_) = AttrMap
-> [Widget n]
-> DisplayRegion
-> ([CursorLocation n] -> Maybe (CursorLocation n))
-> RenderState n
-> (RenderState n, Picture, Maybe (CursorLocation n),
[LayerExtents n])
forall n.
Ord n =>
AttrMap
-> [Widget n]
-> DisplayRegion
-> ([CursorLocation n] -> Maybe (CursorLocation n))
-> RenderState n
-> (RenderState n, Picture, Maybe (CursorLocation n),
[LayerExtents n])
renderFinal AttrMap
am [Widget n]
layerRenders DisplayRegion
region (Maybe (CursorLocation n)
-> [CursorLocation n] -> Maybe (CursorLocation n)
forall a b. a -> b -> a
const Maybe (CursorLocation n)
forall a. Maybe a
Nothing) RenderState n
initialRS