Refactor TUI input dialog

This commit is contained in:
Rene Vergara 2024-02-27 09:44:17 -06:00
parent 7b7c653d02
commit 67e303af38
Signed by: pitmutt
GPG Key ID: 65122AD495A7F5B2
1 changed files with 51 additions and 29 deletions

View File

@ -59,14 +59,19 @@ data Name
| AList | AList
| TList | TList
| HelpDialog | HelpDialog
| WalNameField | DialogInputField
deriving (Eq, Show, Ord) deriving (Eq, Show, Ord)
data WalletName = WalletName data DialogInput = DialogInput
{ _walName :: !T.Text { _dialogInput :: !T.Text
} deriving (Show) } deriving (Show)
makeLenses ''WalletName makeLenses ''DialogInput
data DialogType
= WName
| AName
| Blank
data State = State data State = State
{ _network :: !String { _network :: !String
@ -75,16 +80,16 @@ data State = State
, _transactions :: !(L.List Name String) , _transactions :: !(L.List Name String)
, _msg :: !String , _msg :: !String
, _helpBox :: !Bool , _helpBox :: !Bool
, _walletBox :: !Bool , _dialogBox :: !DialogType
, _splashBox :: !Bool , _splashBox :: !Bool
, _walletForm :: !(Form WalletName () Name) , _inputForm :: !(Form DialogInput () Name)
, _focusRing :: !(F.FocusRing Name) , _focusRing :: !(F.FocusRing Name)
} }
makeLenses ''State makeLenses ''State
drawUI :: State -> [Widget Name] drawUI :: State -> [Widget Name]
drawUI s = [splashDialog s, helpDialog s, walletDialog s, ui s] drawUI s = [splashDialog s, helpDialog s, inputDialog s, ui s]
where where
ui :: State -> Widget Name ui :: State -> Widget Name
ui st = ui st =
@ -122,13 +127,18 @@ drawUI s = [splashDialog s, helpDialog s, walletDialog s, ui s]
map map
(hLimit 40 . str) (hLimit 40 . str)
["Open help", "Close dialog", "Create Wallet", "Quit"] ["Open help", "Close dialog", "Create Wallet", "Quit"]
walletDialog :: State -> Widget Name inputDialog :: State -> Widget Name
walletDialog st = inputDialog st =
if st ^. walletBox case st ^. dialogBox of
then D.renderDialog WName ->
D.renderDialog
(D.dialog (Just (str "Create Wallet")) Nothing 50) (D.dialog (Just (str "Create Wallet")) Nothing 50)
(renderForm $ st ^. walletForm) (renderForm $ st ^. inputForm)
else emptyWidget AName ->
D.renderDialog
(D.dialog (Just (str "Create Account")) Nothing 50)
(renderForm $ st ^. inputForm)
Blank -> emptyWidget
splashDialog :: State -> Widget Name splashDialog :: State -> Widget Name
splashDialog st = splashDialog st =
if st ^. splashBox if st ^. splashBox
@ -143,9 +153,10 @@ drawUI s = [splashDialog s, helpDialog s, walletDialog s, ui s]
C.hCenter (withAttr blinkAttr $ str "Press any key...")) C.hCenter (withAttr blinkAttr $ str "Press any key..."))
else emptyWidget else emptyWidget
mkWalletForm :: WalletName -> Form WalletName e Name mkInputForm :: DialogInput -> Form DialogInput e Name
mkWalletForm = mkInputForm =
newForm [label "Name: " @@= editTextField walName WalNameField (Just 1)] newForm
[label "Name: " @@= editTextField dialogInput DialogInputField (Just 1)]
where where
label s w = label s w =
padBottom (Pad 1) $ vLimit 1 (hLimit 15 $ str s <+> fill ' ') <+> w padBottom (Pad 1) $ vLimit 1 (hLimit 15 $ str s <+> fill ' ') <+> w
@ -180,23 +191,32 @@ appEvent (BT.VtyEvent e) = do
BT.modify $ set helpBox False BT.modify $ set helpBox False
_ev -> return () _ev -> return ()
else do else do
if s ^. walletBox case s ^. dialogBox of
then do WName -> do
case e of case e of
V.EvKey V.KEsc [] -> BT.modify $ set walletBox False V.EvKey V.KEsc [] -> BT.modify $ set dialogBox Blank
V.EvKey V.KEnter [] -> do V.EvKey V.KEnter [] -> do
BT.modify $ set walletBox False BT.modify $ set dialogBox Blank
fs <- BT.zoom walletForm $ BT.gets formState fs <- BT.zoom inputForm $ BT.gets formState
printMsg $ printMsg $
"Creating new wallet " <> T.unpack (fs ^. walName) "Creating new wallet " <> T.unpack (fs ^. dialogInput)
ev -> BT.zoom walletForm $ handleFormEvent (BT.VtyEvent ev) ev -> BT.zoom inputForm $ handleFormEvent (BT.VtyEvent ev)
else do AName -> do
case e of
V.EvKey V.KEsc [] -> BT.modify $ set dialogBox Blank
V.EvKey V.KEnter [] -> do
BT.modify $ set dialogBox Blank
fs <- BT.zoom inputForm $ BT.gets formState
printMsg $
"Creating new address " <> T.unpack (fs ^. dialogInput)
ev -> BT.zoom inputForm $ handleFormEvent (BT.VtyEvent ev)
Blank -> do
case e of case e of
V.EvKey (V.KChar '\t') [] -> focusRing %= F.focusNext V.EvKey (V.KChar '\t') [] -> focusRing %= F.focusNext
V.EvKey (V.KChar 'q') [] -> M.halt V.EvKey (V.KChar 'q') [] -> M.halt
V.EvKey (V.KChar '?') [] -> BT.modify $ set helpBox True V.EvKey (V.KChar '?') [] -> BT.modify $ set helpBox True
V.EvKey (V.KChar 'c') [] -> BT.modify $ set walletBox True V.EvKey (V.KChar 'w') [] -> BT.modify $ set dialogBox WName
V.EvKey (V.KChar 's') [] -> printMsg "You pressed S!" V.EvKey (V.KChar 'a') [] -> BT.modify $ set dialogBox AName
ev -> ev ->
case r of case r of
Just AList -> BT.zoom addresses $ L.handleListEvent ev Just AList -> BT.zoom addresses $ L.handleListEvent ev
@ -251,9 +271,11 @@ runZenithCLI host port dbFilePath = do
("Start up Ok! Connected to Zebra " ++ ("Start up Ok! Connected to Zebra " ++
(T.unpack . zgi_build) zebra ++ " on port " ++ show port ++ ".") (T.unpack . zgi_build) zebra ++ " on port " ++ show port ++ ".")
False False
(null walList) (if null walList
then WName
else Blank)
True True
(mkWalletForm $ WalletName "Main") (mkInputForm $ DialogInput "Main")
(F.focusRing [AList, TList]) (F.focusRing [AList, TList])
Nothing -> do Nothing -> do
print $ print $