{-# LANGUAGE CPP #-}
-- |

module Test.Sandwich.Formatters.TerminalUI.Draw.Util where

import Brick
import Graphics.Vty.Image
import Lens.Micro


fixedHeightOrViewportPercent :: (Ord n, Show n) => n -> Int -> Widget n -> Widget n
fixedHeightOrViewportPercent :: forall n. (Ord n, Show n) => n -> Int -> Widget n -> Widget n
fixedHeightOrViewportPercent n
vpName Int
maxHeightPercent Widget n
w =
  forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed forall a b. (a -> b) -> a -> b
$ do
    -- Render the viewport contents in advance
    Result n
result <- forall n. Widget n -> RenderM n (Result n)
render Widget n
w
    -- If the contents will fit in the maximum allowed rows,
    -- just return the content without putting it in a viewport.

    Context n
ctx <- forall n. RenderM n (Context n)
getContext

#if MIN_VERSION_brick(0,56,0)
    let usableHeight :: Int
usableHeight = Context n
ctx forall s a. s -> Getting a s a -> a
^. forall n. Lens' (Context n) Int
windowHeightL
#else
    let usableHeight = min 80 (ctx ^. availHeightL) -- Bound this so it looks okay inside a viewport
#endif

    let maxHeight :: Int
maxHeight = forall a b. (RealFrac a, Integral b) => a -> b
round (forall a. Real a => a -> Rational
toRational Int
usableHeight forall a. Num a => a -> a -> a
* (forall a. Real a => a -> Rational
toRational Int
maxHeightPercent forall a. Fractional a => a -> a -> a
/ Rational
100))

    if Image -> Int
imageHeight (forall n. Result n -> Image
image Result n
result) forall a. Ord a => a -> a -> Bool
<= Int
maxHeight
      then forall (m :: * -> *) a. Monad m => a -> m a
return Result n
result
      -- Otherwise put the contents (pre-rendered) in a viewport
      -- and limit the height to the maximum allowable height.
      else forall n. Widget n -> RenderM n (Result n)
render (forall n. Int -> Widget n -> Widget n
vLimit Int
maxHeight forall a b. (a -> b) -> a -> b
$
                   forall n.
(Ord n, Show n) =>
n -> ViewportType -> Widget n -> Widget n
viewport n
vpName ViewportType
Vertical forall a b. (a -> b) -> a -> b
$
                   forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return Result n
result)