{-# LANGUAGE OverloadedStrings #-}
-- | This module provides border widgets: vertical borders, horizontal
-- borders, and a box border wrapper widget. All functions in this
-- module use the rendering context's active 'BorderStyle'; to change
-- the 'BorderStyle', use 'withBorderStyle'.
module Brick.Widgets.Border
  ( -- * Border wrapper
    border
  , borderWithLabel

  -- * Horizontal border
  , hBorder
  , hBorderWithLabel

  -- * Vertical border
  , vBorder

  -- * Drawing single border elements
  , borderElem

  -- * Attribute names
  , borderAttr
  , hBorderAttr
  , vBorderAttr

  -- * Utility
  , 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

-- | The top-level border attribute name.
borderAttr :: AttrName
borderAttr :: AttrName
borderAttr = String -> AttrName
attrName String
"border"

-- | The horizontal border attribute name. Inherits from 'borderAttr'.
hBorderAttr :: AttrName
hBorderAttr :: AttrName
hBorderAttr = AttrName
borderAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> String -> AttrName
attrName String
"horizontal"

-- | The vertical border attribute name. Inherits from 'borderAttr'.
vBorderAttr :: AttrName
vBorderAttr :: AttrName
vBorderAttr = AttrName
borderAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> String -> AttrName
attrName String
"vertical"

-- | Draw the specified border element using the active border style
-- using 'borderAttr'.
--
-- Does not participate in dynamic borders (due to the difficulty of
-- introspecting on the first argument); consider using 'joinableBorder'
-- instead.
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]

-- | Put a border around the specified widget.
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

-- | Put a border around the specified widget with the specified label
-- widget placed in the middle of the top horizontal border.
--
-- Note that a border will wrap its child widget as tightly as possible,
-- which means that if the child widget is narrower than the label
-- widget, the label widget will be truncated. If you want to avoid
-- this behavior, add a 'fill' or other space-filling wrapper to the
-- bordered widget so that it takes up enough room to make the border
-- horizontally able to avoid truncating the label.
borderWithLabel :: Widget n
                -- ^ The label widget
                -> Widget n
                -- ^ The widget to put a border around
                -> 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

-- | A horizontal border. Fills all horizontal space. Draws using
-- 'hBorderAttr'.
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)

-- | A horizontal border with a label placed in the center of the
-- border. Fills all horizontal space.
hBorderWithLabel :: Widget n
                 -- ^ The label widget
                 -> 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]

-- | A vertical border. Fills all vertical space. Draws using
-- 'vBorderAttr'.
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)

-- | Initialize a 'DynBorder'. It will be 'bsDraw'n and 'bsOffer'ing
-- in the given directions to begin with, and accept join offers from
-- all directions. We consult the context to choose the 'dbStyle' and
-- 'dbAttr'.
--
-- This is likely to be useful only for custom widgets that need more
-- complicated dynamic border behavior than 'border', 'vBorder', or
-- 'hBorder' offer.
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
        }

-- | Replace the 'Result'\'s dynamic borders with the given one,
-- provided the context says to use dynamic borders at all.
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

-- | A single-character dynamic border that will react to neighboring
-- borders, initially connecting in the given directions.
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)))