module Brick.Widgets.Dialog
( Dialog
, dialogTitle
, dialogName
, dialogButtons
, dialogSelectedIndex
, dialogWidth
, dialog
, renderDialog
, dialogSelection
, dialogAttr
, buttonAttr
, buttonSelectedAttr
, dialogNameL
, dialogButtonsL
, dialogSelectedIndexL
, dialogWidthL
, dialogTitleL
)
where
import Control.Lens
import Control.Applicative
import Data.Monoid
import Data.List (intersperse)
import Graphics.Vty.Input (Event(..), Key(..))
import Brick.Util (clamp)
import Brick.Types
import Brick.Widgets.Core
import Brick.Widgets.Center
import Brick.Widgets.Border
import Brick.AttrMap
data Dialog a =
Dialog { dialogName :: Name
, dialogTitle :: Maybe String
, dialogButtons :: [(String, a)]
, dialogSelectedIndex :: Maybe Int
, dialogWidth :: Int
}
suffixLenses ''Dialog
instance HandleEvent (Dialog a) where
handleEvent ev d =
case ev of
EvKey (KChar '\t') [] -> return $ nextButtonBy 1 d
EvKey KBackTab [] -> return $ nextButtonBy (1) d
_ -> return d
dialog :: Name
-> Maybe String
-> Maybe (Int, [(String, a)])
-> Int
-> Dialog a
dialog name title buttonData w =
let (buttons, idx) = case buttonData of
Nothing -> ([], Nothing)
Just (_, []) -> ([], Nothing)
Just (i, bs) -> (bs, Just $ clamp 0 (length bs 1) i)
in Dialog name title buttons idx w
dialogAttr :: AttrName
dialogAttr = "dialog"
buttonAttr :: AttrName
buttonAttr = "button"
buttonSelectedAttr :: AttrName
buttonSelectedAttr = buttonAttr <> "selected"
renderDialog :: Dialog a -> Widget -> Widget
renderDialog d body =
let buttonPadding = str " "
mkButton (i, (s, _)) = let att = if Just i == d^.dialogSelectedIndexL
then buttonSelectedAttr
else buttonAttr
in withAttr att $ str $ " " <> s <> " "
buttons = hBox $ intersperse buttonPadding $
mkButton <$> (zip [0..] (d^.dialogButtonsL))
doBorder = maybe border borderWithLabel (str <$> d^.dialogTitleL)
in center $
withDefAttr dialogAttr $
hLimit (d^.dialogWidthL) $
doBorder $
vBox [ body
, hCenter buttons
]
nextButtonBy :: Int -> Dialog a -> Dialog a
nextButtonBy amt d =
let numButtons = length $ d^.dialogButtonsL
in if numButtons == 0 then d
else case d^.dialogSelectedIndexL of
Nothing -> d & dialogSelectedIndexL .~ (Just 0)
Just i -> d & dialogSelectedIndexL .~ (Just $ (i + amt) `mod` numButtons)
dialogSelection :: Dialog a -> Maybe a
dialogSelection d =
case d^.dialogSelectedIndexL of
Nothing -> Nothing
Just i -> Just $ ((d^.dialogButtonsL) !! i)^._2