mirror of
https://github.com/jtdaugherty/brick.git
synced 2024-10-27 09:38:28 +03:00
API: remove dialogName, dialogNameL, dialog name parameter
This commit is contained in:
parent
4e2abd400b
commit
4f0f74b851
@ -21,20 +21,20 @@ import qualified Brick.Types as T
|
|||||||
data Choice = Red | Blue | Green
|
data Choice = Red | Blue | Green
|
||||||
deriving Show
|
deriving Show
|
||||||
|
|
||||||
drawUI :: D.Dialog () Choice -> [Widget ()]
|
drawUI :: D.Dialog Choice -> [Widget ()]
|
||||||
drawUI d = [ui]
|
drawUI d = [ui]
|
||||||
where
|
where
|
||||||
ui = D.renderDialog d $ C.hCenter $ padAll 1 $ str "This is the dialog body."
|
ui = D.renderDialog d $ C.hCenter $ padAll 1 $ str "This is the dialog body."
|
||||||
|
|
||||||
appEvent :: D.Dialog () Choice -> V.Event -> T.EventM () (T.Next (D.Dialog () Choice))
|
appEvent :: D.Dialog Choice -> V.Event -> T.EventM () (T.Next (D.Dialog Choice))
|
||||||
appEvent d ev =
|
appEvent d ev =
|
||||||
case ev of
|
case ev of
|
||||||
V.EvKey V.KEsc [] -> M.halt d
|
V.EvKey V.KEsc [] -> M.halt d
|
||||||
V.EvKey V.KEnter [] -> M.halt d
|
V.EvKey V.KEnter [] -> M.halt d
|
||||||
_ -> M.continue =<< D.handleDialogEvent ev d
|
_ -> M.continue =<< D.handleDialogEvent ev d
|
||||||
|
|
||||||
initialState :: D.Dialog () Choice
|
initialState :: D.Dialog Choice
|
||||||
initialState = D.dialog () (Just "Title") (Just (0, choices)) 50
|
initialState = D.dialog (Just "Title") (Just (0, choices)) 50
|
||||||
where
|
where
|
||||||
choices = [ ("Red", Red)
|
choices = [ ("Red", Red)
|
||||||
, ("Blue", Blue)
|
, ("Blue", Blue)
|
||||||
@ -48,7 +48,7 @@ theMap = A.attrMap V.defAttr
|
|||||||
, (D.buttonSelectedAttr, bg V.yellow)
|
, (D.buttonSelectedAttr, bg V.yellow)
|
||||||
]
|
]
|
||||||
|
|
||||||
theApp :: M.App (D.Dialog () Choice) V.Event ()
|
theApp :: M.App (D.Dialog Choice) V.Event ()
|
||||||
theApp =
|
theApp =
|
||||||
M.App { M.appDraw = drawUI
|
M.App { M.appDraw = drawUI
|
||||||
, M.appChooseCursor = M.showFirstCursor
|
, M.appChooseCursor = M.showFirstCursor
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
module Brick.Widgets.Dialog
|
module Brick.Widgets.Dialog
|
||||||
( Dialog
|
( Dialog
|
||||||
, dialogTitle
|
, dialogTitle
|
||||||
, dialogName
|
|
||||||
, dialogButtons
|
, dialogButtons
|
||||||
, dialogSelectedIndex
|
, dialogSelectedIndex
|
||||||
, dialogWidth
|
, dialogWidth
|
||||||
@ -21,7 +20,6 @@ module Brick.Widgets.Dialog
|
|||||||
, buttonAttr
|
, buttonAttr
|
||||||
, buttonSelectedAttr
|
, buttonSelectedAttr
|
||||||
-- * Lenses
|
-- * Lenses
|
||||||
, dialogNameL
|
|
||||||
, dialogButtonsL
|
, dialogButtonsL
|
||||||
, dialogSelectedIndexL
|
, dialogSelectedIndexL
|
||||||
, dialogWidthL
|
, dialogWidthL
|
||||||
@ -52,10 +50,8 @@ import Brick.AttrMap
|
|||||||
--
|
--
|
||||||
-- * Tab: selecte the next button
|
-- * Tab: selecte the next button
|
||||||
-- * Shift-tab: select the previous button
|
-- * Shift-tab: select the previous button
|
||||||
data Dialog n a =
|
data Dialog a =
|
||||||
Dialog { dialogName :: n
|
Dialog { dialogTitle :: Maybe String
|
||||||
-- ^ The dialog name
|
|
||||||
, dialogTitle :: Maybe String
|
|
||||||
-- ^ The dialog title
|
-- ^ The dialog title
|
||||||
, dialogButtons :: [(String, a)]
|
, dialogButtons :: [(String, a)]
|
||||||
-- ^ The dialog button labels and values
|
-- ^ The dialog button labels and values
|
||||||
@ -67,7 +63,7 @@ data Dialog n a =
|
|||||||
|
|
||||||
suffixLenses ''Dialog
|
suffixLenses ''Dialog
|
||||||
|
|
||||||
handleDialogEvent :: Event -> Dialog n a -> EventM n (Dialog n a)
|
handleDialogEvent :: Event -> Dialog a -> EventM n (Dialog a)
|
||||||
handleDialogEvent ev d =
|
handleDialogEvent ev d =
|
||||||
return $ case ev of
|
return $ case ev of
|
||||||
EvKey (KChar '\t') [] -> nextButtonBy 1 d
|
EvKey (KChar '\t') [] -> nextButtonBy 1 d
|
||||||
@ -75,23 +71,20 @@ handleDialogEvent ev d =
|
|||||||
_ -> d
|
_ -> d
|
||||||
|
|
||||||
-- | Create a dialog.
|
-- | Create a dialog.
|
||||||
dialog :: n
|
dialog :: Maybe String
|
||||||
-- ^ The dialog name, provided so that you can use this as a
|
|
||||||
-- basis for viewport names in the dialog if desired
|
|
||||||
-> Maybe String
|
|
||||||
-- ^ The dialog title
|
-- ^ The dialog title
|
||||||
-> Maybe (Int, [(String, a)])
|
-> Maybe (Int, [(String, a)])
|
||||||
-- ^ The currently-selected button index (starting at zero) and
|
-- ^ The currently-selected button index (starting at zero) and
|
||||||
-- the button labels and values to use
|
-- the button labels and values to use
|
||||||
-> Int
|
-> Int
|
||||||
-- ^ The maximum width of the dialog
|
-- ^ The maximum width of the dialog
|
||||||
-> Dialog n a
|
-> Dialog a
|
||||||
dialog name title buttonData w =
|
dialog title buttonData w =
|
||||||
let (buttons, idx) = case buttonData of
|
let (buttons, idx) = case buttonData of
|
||||||
Nothing -> ([], Nothing)
|
Nothing -> ([], Nothing)
|
||||||
Just (_, []) -> ([], Nothing)
|
Just (_, []) -> ([], Nothing)
|
||||||
Just (i, bs) -> (bs, Just $ clamp 0 (length bs - 1) i)
|
Just (i, bs) -> (bs, Just $ clamp 0 (length bs - 1) i)
|
||||||
in Dialog name title buttons idx w
|
in Dialog title buttons idx w
|
||||||
|
|
||||||
-- | The default attribute of the dialog
|
-- | The default attribute of the dialog
|
||||||
dialogAttr :: AttrName
|
dialogAttr :: AttrName
|
||||||
@ -106,7 +99,7 @@ buttonSelectedAttr :: AttrName
|
|||||||
buttonSelectedAttr = buttonAttr <> "selected"
|
buttonSelectedAttr = buttonAttr <> "selected"
|
||||||
|
|
||||||
-- | Render a dialog with the specified body widget.
|
-- | Render a dialog with the specified body widget.
|
||||||
renderDialog :: Dialog n a -> Widget n -> Widget n
|
renderDialog :: Dialog a -> Widget n -> Widget n
|
||||||
renderDialog d body =
|
renderDialog d body =
|
||||||
let buttonPadding = str " "
|
let buttonPadding = str " "
|
||||||
mkButton (i, (s, _)) = let att = if Just i == d^.dialogSelectedIndexL
|
mkButton (i, (s, _)) = let att = if Just i == d^.dialogSelectedIndexL
|
||||||
@ -125,7 +118,7 @@ renderDialog d body =
|
|||||||
, hCenter buttons
|
, hCenter buttons
|
||||||
]
|
]
|
||||||
|
|
||||||
nextButtonBy :: Int -> Dialog n a -> Dialog n a
|
nextButtonBy :: Int -> Dialog a -> Dialog a
|
||||||
nextButtonBy amt d =
|
nextButtonBy amt d =
|
||||||
let numButtons = length $ d^.dialogButtonsL
|
let numButtons = length $ d^.dialogButtonsL
|
||||||
in if numButtons == 0 then d
|
in if numButtons == 0 then d
|
||||||
@ -136,7 +129,7 @@ nextButtonBy amt d =
|
|||||||
-- | Obtain the value associated with the dialog's currently-selected
|
-- | Obtain the value associated with the dialog's currently-selected
|
||||||
-- button, if any. This function is probably what you want when someone
|
-- button, if any. This function is probably what you want when someone
|
||||||
-- presses 'Enter' in a dialog.
|
-- presses 'Enter' in a dialog.
|
||||||
dialogSelection :: Dialog n a -> Maybe a
|
dialogSelection :: Dialog a -> Maybe a
|
||||||
dialogSelection d =
|
dialogSelection d =
|
||||||
case d^.dialogSelectedIndexL of
|
case d^.dialogSelectedIndexL of
|
||||||
Nothing -> Nothing
|
Nothing -> Nothing
|
||||||
|
Loading…
Reference in New Issue
Block a user