{-# LANGUAGE OverloadedStrings #-}
module Brick.Widgets.Border
(
border
, borderWithLabel
, hBorder
, hBorderWithLabel
, vBorder
, borderElem
, borderAttr
, hBorderAttr
, vBorderAttr
, joinableBorder
)
where
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid ((<>))
#endif
import Lens.Micro ((^.), (&), (.~), to)
import Graphics.Vty (imageHeight, imageWidth)
import Brick.AttrMap
import Brick.Types
import Brick.Widgets.Core
import Brick.Widgets.Border.Style (BorderStyle(..))
import Brick.Widgets.Internal (renderDynBorder)
import Data.IMap (Run(..))
import qualified Brick.BorderMap as BM
borderAttr :: AttrName
borderAttr :: AttrName
borderAttr = String -> AttrName
attrName String
"border"
hBorderAttr :: AttrName
hBorderAttr :: AttrName
hBorderAttr = AttrName
borderAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> String -> AttrName
attrName String
"horizontal"
vBorderAttr :: AttrName
vBorderAttr :: AttrName
vBorderAttr = AttrName
borderAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> String -> AttrName
attrName String
"vertical"
borderElem :: (BorderStyle -> Char) -> Widget n
borderElem :: forall n. (BorderStyle -> Char) -> Widget n
borderElem BorderStyle -> Char
f =
Size -> Size -> RenderM n (Result n) -> Widget n
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed (RenderM n (Result n) -> Widget n)
-> RenderM n (Result n) -> Widget n
forall a b. (a -> b) -> a -> b
$ do
bs <- Context n -> BorderStyle
forall n. Context n -> BorderStyle
ctxBorderStyle (Context n -> BorderStyle)
-> ReaderT (Context n) (State (RenderState n)) (Context n)
-> ReaderT (Context n) (State (RenderState n)) BorderStyle
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReaderT (Context n) (State (RenderState n)) (Context n)
forall n. RenderM n (Context n)
getContext
render $ withAttr borderAttr $ str [f bs]
border :: Widget n -> Widget n
border :: forall n. Widget n -> Widget n
border = Maybe (Widget n) -> Widget n -> Widget n
forall n. Maybe (Widget n) -> Widget n -> Widget n
border_ Maybe (Widget n)
forall a. Maybe a
Nothing
borderWithLabel :: Widget n
-> Widget n
-> Widget n
borderWithLabel :: forall n. Widget n -> Widget n -> Widget n
borderWithLabel Widget n
label = Maybe (Widget n) -> Widget n -> Widget n
forall n. Maybe (Widget n) -> Widget n -> Widget n
border_ (Widget n -> Maybe (Widget n)
forall a. a -> Maybe a
Just Widget n
label)
border_ :: Maybe (Widget n) -> Widget n -> Widget n
border_ :: forall n. Maybe (Widget n) -> Widget n -> Widget n
border_ Maybe (Widget n)
label Widget n
wrapped =
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
wrapped) (Widget n -> Size
forall n. Widget n -> Size
vSize Widget n
wrapped) (RenderM n (Result n) -> Widget n)
-> RenderM n (Result n) -> Widget n
forall a b. (a -> b) -> a -> b
$ do
c <- RenderM n (Context n)
forall n. RenderM n (Context n)
getContext
middleResult <- render $ hLimit (c^.availWidthL - 2)
$ vLimit (c^.availHeightL - 2)
$ wrapped
let tl = Edges Bool -> Widget n
forall n. Edges Bool -> Widget n
joinableBorder (Bool -> Bool -> Bool -> Bool -> Edges Bool
forall a. a -> a -> a -> a -> Edges a
Edges Bool
False Bool
True Bool
False Bool
True)
tr = Edges Bool -> Widget n
forall n. Edges Bool -> Widget n
joinableBorder (Bool -> Bool -> Bool -> Bool -> Edges Bool
forall a. a -> a -> a -> a -> Edges a
Edges Bool
False Bool
True Bool
True Bool
False)
bl = Edges Bool -> Widget n
forall n. Edges Bool -> Widget n
joinableBorder (Bool -> Bool -> Bool -> Bool -> Edges Bool
forall a. a -> a -> a -> a -> Edges a
Edges Bool
True Bool
False Bool
False Bool
True)
br = Edges Bool -> Widget n
forall n. Edges Bool -> Widget n
joinableBorder (Bool -> Bool -> Bool -> Bool -> Edges Bool
forall a. a -> a -> a -> a -> Edges a
Edges Bool
True Bool
False Bool
True Bool
False)
top = Widget n
forall {n}. Widget n
tl Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Widget n -> (Widget n -> Widget n) -> Maybe (Widget n) -> Widget n
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Widget n
forall {n}. Widget n
hBorder Widget n -> Widget n
forall n. Widget n -> Widget n
hBorderWithLabel Maybe (Widget n)
label Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Widget n
forall {n}. Widget n
tr
bottom = Widget n
forall {n}. Widget n
bl Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Widget n
forall {n}. Widget n
hBorder Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Widget n
forall {n}. Widget n
br
middle = Widget n
forall {n}. Widget n
vBorder Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> (Size -> Size -> RenderM n (Result n) -> Widget n
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed (RenderM n (Result n) -> Widget n)
-> RenderM n (Result n) -> Widget n
forall a b. (a -> b) -> a -> b
$ Result n -> RenderM n (Result n)
forall a. a -> ReaderT (Context n) (State (RenderState n)) a
forall (m :: * -> *) a. Monad m => a -> m a
return Result n
middleResult) Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Widget n
forall {n}. Widget n
vBorder
total = Widget n
top Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<=> Widget n
middle Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<=> Widget n
forall {n}. Widget n
bottom
render $ hLimit (middleResult^.imageL.to imageWidth + 2)
$ vLimit (middleResult^.imageL.to imageHeight + 2)
$ total
hBorder :: Widget n
hBorder :: forall {n}. Widget n
hBorder =
AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withAttr AttrName
borderAttr (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$ Size -> Size -> RenderM n (Result n) -> Widget n
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Greedy Size
Fixed (RenderM n (Result n) -> Widget n)
-> RenderM n (Result n) -> Widget n
forall a b. (a -> b) -> a -> b
$ do
ctx <- RenderM n (Context n)
forall n. RenderM n (Context n)
getContext
let bs = Context n -> BorderStyle
forall n. Context n -> BorderStyle
ctxBorderStyle Context n
ctx
w = Context n -> Int
forall n. Context n -> Int
availWidth Context n
ctx
db <- dynBorderFromDirections (Edges False False True True)
let dynBorders = Location
-> Run DynBorder -> BorderMap DynBorder -> BorderMap DynBorder
forall a. Location -> Run a -> BorderMap a -> BorderMap a
BM.insertH Location
forall a. Monoid a => a
mempty (Int -> DynBorder -> Run DynBorder
forall a. Int -> a -> Run a
Run Int
w DynBorder
db)
(BorderMap DynBorder -> BorderMap DynBorder)
-> BorderMap DynBorder -> BorderMap DynBorder
forall a b. (a -> b) -> a -> b
$ Edges Int -> BorderMap DynBorder
forall a. Edges Int -> BorderMap a
BM.emptyCoordinates (Int -> Int -> Int -> Int -> Edges Int
forall a. a -> a -> a -> a -> Edges a
Edges Int
0 Int
0 Int
0 (Int
wInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
setDynBorders dynBorders $ render $ withAttr hBorderAttr
$ vLimit 1 $ fill (bsHorizontal bs)
hBorderWithLabel :: Widget n
-> Widget n
hBorderWithLabel :: forall n. Widget n -> Widget n
hBorderWithLabel Widget n
label =
Size -> Size -> RenderM n (Result n) -> Widget n
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Greedy Size
Fixed (RenderM n (Result n) -> Widget n)
-> RenderM n (Result n) -> Widget n
forall a b. (a -> b) -> a -> b
$ do
res <- 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
$ Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
vLimit Int
1 Widget n
label
render $ hBox [hBorder, Widget Fixed Fixed (return res), hBorder]
vBorder :: Widget n
vBorder :: forall {n}. Widget n
vBorder =
AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withAttr AttrName
borderAttr (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$ Size -> Size -> RenderM n (Result n) -> Widget n
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Greedy (RenderM n (Result n) -> Widget n)
-> RenderM n (Result n) -> Widget n
forall a b. (a -> b) -> a -> b
$ do
ctx <- RenderM n (Context n)
forall n. RenderM n (Context n)
getContext
let bs = Context n -> BorderStyle
forall n. Context n -> BorderStyle
ctxBorderStyle Context n
ctx
h = Context n -> Int
forall n. Context n -> Int
availHeight Context n
ctx
db <- dynBorderFromDirections (Edges True True False False)
let dynBorders = Location
-> Run DynBorder -> BorderMap DynBorder -> BorderMap DynBorder
forall a. Location -> Run a -> BorderMap a -> BorderMap a
BM.insertV Location
forall a. Monoid a => a
mempty (Int -> DynBorder -> Run DynBorder
forall a. Int -> a -> Run a
Run Int
h DynBorder
db)
(BorderMap DynBorder -> BorderMap DynBorder)
-> BorderMap DynBorder -> BorderMap DynBorder
forall a b. (a -> b) -> a -> b
$ Edges Int -> BorderMap DynBorder
forall a. Edges Int -> BorderMap a
BM.emptyCoordinates (Int -> Int -> Int -> Int -> Edges Int
forall a. a -> a -> a -> a -> Edges a
Edges Int
0 (Int
hInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Int
0 Int
0)
setDynBorders dynBorders $ render $ withAttr vBorderAttr
$ hLimit 1 $ fill (bsVertical bs)
dynBorderFromDirections :: Edges Bool -> RenderM n DynBorder
dynBorderFromDirections :: forall n. Edges Bool -> RenderM n DynBorder
dynBorderFromDirections Edges Bool
dirs = do
ctx <- RenderM n (Context n)
forall n. RenderM n (Context n)
getContext
return DynBorder
{ dbStyle = ctxBorderStyle ctx
, dbAttr = attrMapLookup (ctxAttrName ctx) (ctxAttrMap ctx)
, dbSegments = (\Bool
draw -> Bool -> Bool -> Bool -> BorderSegment
BorderSegment Bool
True Bool
draw Bool
draw) <$> dirs
}
setDynBorders :: BM.BorderMap DynBorder -> RenderM n (Result n) -> RenderM n (Result n)
setDynBorders :: forall n.
BorderMap DynBorder -> RenderM n (Result n) -> RenderM n (Result n)
setDynBorders BorderMap DynBorder
newBorders RenderM n (Result n)
act = do
dyn <- Context n -> Bool
forall n. Context n -> Bool
ctxDynBorders (Context n -> Bool)
-> ReaderT (Context n) (State (RenderState n)) (Context n)
-> ReaderT (Context n) (State (RenderState n)) Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReaderT (Context n) (State (RenderState n)) (Context n)
forall n. RenderM n (Context n)
getContext
res <- act
return $ if dyn
then res & bordersL .~ newBorders
else res
joinableBorder :: Edges Bool -> Widget n
joinableBorder :: forall n. Edges Bool -> Widget n
joinableBorder Edges Bool
dirs = AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withAttr AttrName
borderAttr (Widget n -> Widget n)
-> (RenderM n (Result n) -> Widget n)
-> RenderM n (Result n)
-> Widget n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Size -> Size -> RenderM n (Result n) -> Widget n
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed (RenderM n (Result n) -> Widget n)
-> RenderM n (Result n) -> Widget n
forall a b. (a -> b) -> a -> b
$ do
db <- Edges Bool -> RenderM n DynBorder
forall n. Edges Bool -> RenderM n DynBorder
dynBorderFromDirections Edges Bool
dirs
setDynBorders
(BM.singleton mempty db)
(render (raw (renderDynBorder db)))