module Brick.Widgets.Center
(
hCenter
, hCenterWith
, vCenter
, vCenterWith
, center
, centerWith
, centerAbout
)
where
import Control.Lens ((^.), (&), (.~), to)
import Data.Maybe (fromMaybe)
import Graphics.Vty (imageWidth, imageHeight, horizCat, charFill, vertCat)
import Brick.Types
import Brick.Widgets.Core
hCenter :: Widget -> Widget
hCenter = hCenterWith Nothing
hCenterWith :: Maybe Char -> Widget -> Widget
hCenterWith mChar p =
let ch = fromMaybe ' ' mChar
in Widget Greedy (vSize p) $ do
result <- render p
c <- getContext
let rWidth = result^.imageL.to imageWidth
rHeight = result^.imageL.to imageHeight
remainder = c^.availWidthL (leftPaddingAmount * 2)
leftPaddingAmount = (c^.availWidthL rWidth) `div` 2
rightPaddingAmount = leftPaddingAmount + remainder
leftPadding = charFill (c^.attrL) ch leftPaddingAmount rHeight
rightPadding = charFill (c^.attrL) ch rightPaddingAmount rHeight
paddedImage = horizCat [ leftPadding
, result^.imageL
, rightPadding
]
off = Location (leftPaddingAmount, 0)
if leftPaddingAmount == 0 && rightPaddingAmount == 0 then
return result else
return $ addResultOffset off
$ result & imageL .~ paddedImage
vCenter :: Widget -> Widget
vCenter = vCenterWith Nothing
vCenterWith :: Maybe Char -> Widget -> Widget
vCenterWith mChar p =
let ch = fromMaybe ' ' mChar
in Widget (hSize p) Greedy $ do
result <- render p
c <- getContext
let rWidth = result^.imageL.to imageWidth
rHeight = result^.imageL.to imageHeight
remainder = c^.availHeightL (topPaddingAmount * 2)
topPaddingAmount = (c^.availHeightL rHeight) `div` 2
bottomPaddingAmount = topPaddingAmount + remainder
topPadding = charFill (c^.attrL) ch rWidth topPaddingAmount
bottomPadding = charFill (c^.attrL) ch rWidth bottomPaddingAmount
paddedImage = vertCat [ topPadding
, result^.imageL
, bottomPadding
]
off = Location (0, topPaddingAmount)
if topPaddingAmount == 0 && bottomPaddingAmount == 0 then
return result else
return $ addResultOffset off
$ result & imageL .~ paddedImage
center :: Widget -> Widget
center = centerWith Nothing
centerWith :: Maybe Char -> Widget -> Widget
centerWith c = vCenterWith c . hCenterWith c
centerAbout :: Location -> Widget -> Widget
centerAbout l p =
Widget Greedy Greedy $ do
c <- getContext
let centerW = c^.availWidthL `div` 2
centerH = c^.availHeightL `div` 2
off = Location ( centerW l^.columnL
, centerH l^.rowL
)
render $ translateBy off p