brick/programs/DialogDemo.hs

75 lines
1.9 KiB
Haskell
Raw Permalink Normal View History

2015-07-09 00:47:39 +03:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}
2015-07-09 00:47:39 +03:00
module Main where
#if !(MIN_VERSION_base(4,11,0))
2015-07-09 00:47:39 +03:00
import Data.Monoid
#endif
2015-07-10 23:09:10 +03:00
import qualified Graphics.Vty as V
2015-07-09 00:47:39 +03:00
2015-07-10 23:09:10 +03:00
import qualified Brick.Main as M
import Brick.Types
2015-07-10 23:09:10 +03:00
( Widget
, BrickEvent(..)
)
import Brick.Widgets.Core
( padAll
2015-07-10 23:09:10 +03:00
, str
)
import qualified Brick.Widgets.Dialog as D
import qualified Brick.Widgets.Center as C
import qualified Brick.AttrMap as A
import Brick.Util (on, bg)
import qualified Brick.Types as T
2015-07-09 00:47:39 +03:00
data Choice = Red | Blue | Green
deriving Show
data Name =
RedButton
| BlueButton
| GreenButton
deriving (Show, Eq, Ord)
drawUI :: D.Dialog Choice Name -> [Widget Name]
2015-07-09 00:47:39 +03:00
drawUI d = [ui]
where
ui = D.renderDialog d $ C.hCenter $ padAll 1 $ str "This is the dialog body."
2015-07-09 00:47:39 +03:00
appEvent :: BrickEvent Name e -> T.EventM Name (D.Dialog Choice Name) ()
appEvent (VtyEvent ev) =
2015-07-09 00:47:39 +03:00
case ev of
V.EvKey V.KEsc [] -> M.halt
V.EvKey V.KEnter [] -> M.halt
_ -> D.handleDialogEvent ev
appEvent _ = return ()
2015-07-09 00:47:39 +03:00
initialState :: D.Dialog Choice Name
initialState = D.dialog (Just $ str "Title") (Just (RedButton, choices)) 50
2015-07-09 00:47:39 +03:00
where
choices = [ ("Red", RedButton, Red)
, ("Blue", BlueButton, Blue)
, ("Green", GreenButton, Green)
2015-07-09 00:47:39 +03:00
]
2015-07-10 23:09:10 +03:00
theMap :: A.AttrMap
theMap = A.attrMap V.defAttr
[ (D.dialogAttr, V.white `on` V.blue)
, (D.buttonAttr, V.black `on` V.white)
, (D.buttonSelectedAttr, bg V.yellow)
2015-07-09 00:47:39 +03:00
]
theApp :: M.App (D.Dialog Choice Name) e Name
2015-07-09 00:47:39 +03:00
theApp =
2015-07-10 23:09:10 +03:00
M.App { M.appDraw = drawUI
, M.appChooseCursor = M.showFirstCursor
, M.appHandleEvent = appEvent
, M.appStartEvent = return ()
2015-07-10 23:09:10 +03:00
, M.appAttrMap = const theMap
}
2015-07-09 00:47:39 +03:00
main :: IO ()
main = do
2015-07-10 23:09:10 +03:00
d <- M.defaultMain theApp initialState
putStrLn $ "You chose: " <> show (D.dialogSelection d)