Combine h/vPad into pad

This commit is contained in:
Jonathan Daugherty 2015-06-27 15:37:26 -07:00
parent adbcbf72b0
commit deb97623f0
3 changed files with 36 additions and 38 deletions

View File

@ -19,33 +19,39 @@ hCenter :: Widget -> Widget
hCenter = hCenterWith Nothing hCenter = hCenterWith Nothing
hCenterWith :: Maybe Char -> Widget -> Widget hCenterWith :: Maybe Char -> Widget -> Widget
hCenterWith Nothing p = hCenterWith mChar p =
Widget Unlimited (vSize p) $ do let ch = maybe ' ' id mChar
result <- render p in Widget Unlimited (vSize p) $ do
c <- getContext result <- render p
let offW = (c^.availW - (result^.image.to imageWidth)) `div` 2 c <- getContext
render $ translateBy (Location (offW, 0)) $ Widget Fixed Fixed $ return result let rWidth = result^.image.to imageWidth
hCenterWith (Just c) p = rHeight = result^.image.to imageHeight
hBox [ (hPad c, Low) remainder = c^.availW - (leftPaddingAmount * 2)
, (p, High) leftPaddingAmount = (c^.availW - rWidth) `div` 2
, (hPad c, Low) rightPaddingAmount = leftPaddingAmount + remainder
] render $ vLimit rHeight $
(hLimit leftPaddingAmount $ pad ch) <+>
(Widget Fixed Fixed $ return result) <+>
(hLimit rightPaddingAmount $ pad ch)
vCenter :: Widget -> Widget vCenter :: Widget -> Widget
vCenter = vCenterWith Nothing vCenter = vCenterWith Nothing
vCenterWith :: (Maybe Char) -> Widget -> Widget vCenterWith :: Maybe Char -> Widget -> Widget
vCenterWith Nothing p = vCenterWith mChar p =
Widget (hSize p) Unlimited $ do let ch = maybe ' ' id mChar
result <- render p in Widget (hSize p) Unlimited $ do
c <- getContext result <- render p
let offH = (c^.availH - (result^.image.to imageHeight)) `div` 2 c <- getContext
render $ translateBy (Location (0, offH)) $ Widget Fixed Fixed $ return result let rWidth = result^.image.to imageWidth
vCenterWith (Just c) p = rHeight = result^.image.to imageHeight
vBox [ (vPad c, Low) remainder = c^.availH - (topPaddingAmount * 2)
, (p, High) topPaddingAmount = (c^.availH - rHeight) `div` 2
, (vPad c, Low) bottomPaddingAmount = topPaddingAmount + remainder
] render $ hLimit rWidth $
(vLimit topPaddingAmount $ pad ch) <=>
(Widget Fixed Fixed $ return result) <=>
(vLimit topPaddingAmount $ pad ch)
center :: Widget -> Widget center :: Widget -> Widget
center = centerWith Nothing center = centerWith Nothing

View File

@ -24,8 +24,7 @@ module Brick.Widgets.Core
, txt , txt
, str , str
, hPad , pad
, vPad
, hFill , hFill
, vFill , vFill
, hBox , hBox

View File

@ -34,8 +34,7 @@ module Brick.Widgets.Internal
, txt , txt
, str , str
, hPad , pad
, vPad
, hFill , hFill
, vFill , vFill
@ -209,17 +208,11 @@ str s =
txt :: T.Text -> Widget txt :: T.Text -> Widget
txt = str . T.unpack txt = str . T.unpack
hPad :: Char -> Widget pad :: Char -> Widget
hPad ch = pad ch =
Widget Unlimited Fixed $ do Widget Unlimited Unlimited $ do
c <- getContext c <- getContext
return $ def & image .~ (V.charFill (c^.attr) ch (c^.availW) (max 1 (c^.availH))) return $ def & image .~ (V.charFill (c^.attr) ch (max 1 (c^.availW)) (max 1 (c^.availH)))
vPad :: Char -> Widget
vPad ch =
Widget Fixed Unlimited $ do
c <- getContext
return $ def & image .~ (V.charFill (c^.attr) ch (max 1 (c^.availW)) (c^.availH))
hFill :: Char -> Widget hFill :: Char -> Widget
hFill ch = hFill ch =
@ -451,7 +444,7 @@ viewport vpname typ p =
-- Return the translated result with the visibility requests -- Return the translated result with the visibility requests
-- discarded -- discarded
render $ cropToContext $ ((Widget Fixed Fixed $ return $ translated & visibilityRequests .~ mempty) <+> hPad ' ') <=> vPad ' ' render $ cropToContext $ ((Widget Fixed Fixed $ return $ translated & visibilityRequests .~ mempty) <+> pad ' ') <=> pad ' '
scrollTo :: ViewportType -> ScrollRequest -> V.Image -> Viewport -> Viewport scrollTo :: ViewportType -> ScrollRequest -> V.Image -> Viewport -> Viewport
scrollTo typ req img vp = vp & theStart .~ newStart scrollTo typ req img vp = vp & theStart .~ newStart