Skip to content

Commit

Permalink
Merge pull request #1201 from input-output-hk/enhance-ux-confirm-action
Browse files Browse the repository at this point in the history
TUI - Enhance UX to request confirmation for terminating actions
  • Loading branch information
ffakenz authored Dec 13, 2023
2 parents 6ba4954 + 20f7b3c commit e46318f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions hydra-tui/src/Hydra/TUI/Drawing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,15 @@ drawFocusPanelInitializing :: IdentifiedState -> InitializingState -> Widget Nam
drawFocusPanelInitializing me InitializingState{remainingParties, initializingScreen} = case initializingScreen of
InitializingHome -> drawRemainingParties me remainingParties
CommitMenu x -> vBox [txt "Select UTxOs to commit:", renderForm x]
ConfirmingAbort x -> vBox [txt "Confirm Abort action:", renderForm x]

drawFocusPanelOpen :: NetworkId -> VerificationKey PaymentKey -> UTxO -> OpenScreen -> Widget Name
drawFocusPanelOpen networkId vk utxo = \case
OpenHome -> drawUTxO (highlightOwnAddress ownAddress) utxo
SelectingUTxO x -> renderForm x
EnteringAmount _ x -> renderForm x
SelectingRecipient _ _ x -> renderForm x
ConfirmingClose x -> vBox [txt "Confirm Close action:", renderForm x]
where
ownAddress = mkVkAddress networkId vk

Expand Down
20 changes: 20 additions & 0 deletions hydra-tui/src/Hydra/TUI/Forms.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,23 @@ utxoRadioField u =
]
]
(Prelude.head $ Map.toList u)

confirmRadioField ::
forall s e n.
( s ~ Bool
, n ~ Text
) =>
Form s e n
confirmRadioField =
newForm
[ radioField
id
[ (snd opt, fst opt, fst opt)
| opt <- options
]
]
True
where
options = [("yes", True), ("no", False)]

radioFields = radioField id [(opt, fst opt, show $ fst opt) | opt <- options]
25 changes: 23 additions & 2 deletions hydra-tui/src/Hydra/TUI/Handlers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ handleVtyEventsActiveHeadState cardanoClient hydraClient utxo e = do
handleVtyEventsInitializingScreen :: CardanoClient -> Client Tx IO -> Vty.Event -> EventM Name InitializingScreen ()
handleVtyEventsInitializingScreen cardanoClient hydraClient e = do
case e of
EvKey (KChar 'a') [] -> liftIO (sendInput hydraClient Abort)
EvKey (KChar 'a') [] ->
id .= ConfirmingAbort confirmRadioField
_ -> pure ()
initializingScreen <- use id
case initializingScreen of
Expand All @@ -135,15 +136,35 @@ handleVtyEventsInitializingScreen cardanoClient hydraClient e = do
id .= InitializingHome
_ -> pure ()
zoom commitMenuL $ handleFormEvent (VtyEvent e)
ConfirmingAbort i -> do
case e of
EvKey KEsc [] -> id .= InitializingHome
EvKey KEnter [] -> do
let selected = formState i
if selected
then liftIO $ sendInput hydraClient Abort
else id .= InitializingHome
_ -> pure ()
zoom confirmingAbortFormL $ handleFormEvent (VtyEvent e)

handleVtyEventsOpen :: CardanoClient -> Client Tx IO -> UTxO -> Vty.Event -> EventM Name OpenScreen ()
handleVtyEventsOpen cardanoClient hydraClient utxo e = do
case e of
EvKey (KChar 'c') [] ->
liftIO $ sendInput hydraClient Close
id .= ConfirmingClose confirmRadioField
_ -> pure ()
k <- use id
case k of
ConfirmingClose i -> do
case e of
EvKey KEsc [] -> id .= OpenHome
EvKey KEnter [] -> do
let selected = formState i
if selected
then liftIO $ sendInput hydraClient Close
else id .= OpenHome
_ -> pure ()
zoom confirmingCloseFormL $ handleFormEvent (VtyEvent e)
OpenHome -> do
case e of
EvKey (KChar 'n') [] -> do
Expand Down
6 changes: 6 additions & 0 deletions hydra-tui/src/Hydra/TUI/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type UTxOCheckboxForm e n = Form (Map TxIn (TxOut CtxUTxO, Bool)) e n

type UTxORadioFieldForm e n = Form (TxIn, TxOut CtxUTxO) e n

type ConfirmingRadioFieldForm e n = Form Bool e n

data InitializingState = InitializingState
{ remainingParties :: [Party]
, initializingScreen :: InitializingScreen
Expand All @@ -50,12 +52,14 @@ data InitializingState = InitializingState
data InitializingScreen
= InitializingHome
| CommitMenu {commitMenu :: UTxOCheckboxForm (HydraEvent Tx) Name}
| ConfirmingAbort {confirmingAbortForm :: ConfirmingRadioFieldForm (HydraEvent Tx) Name}

data OpenScreen
= OpenHome
| SelectingUTxO {selectingUTxOForm :: UTxORadioFieldForm (HydraEvent Tx) Name}
| EnteringAmount {utxoSelected :: (TxIn, TxOut CtxUTxO), enteringAmountForm :: Form Integer (HydraEvent Tx) Name}
| SelectingRecipient {utxoSelected :: (TxIn, TxOut CtxUTxO), amountEntered :: Integer, selectingRecipientForm :: Form AddressInEra (HydraEvent Tx) Name}
| ConfirmingClose {confirmingCloseForm :: ConfirmingRadioFieldForm (HydraEvent Tx) Name}

newtype ClosedState = ClosedState {contestationDeadline :: UTCTime}

Expand Down Expand Up @@ -83,6 +87,7 @@ makeLensesFor
[ ("selectingUTxOForm", "selectingUTxOFormL")
, ("enteringAmountForm", "enteringAmountFormL")
, ("selectingRecipientForm", "selectingRecipientFormL")
, ("confirmingCloseForm", "confirmingCloseFormL")
]
''OpenScreen

Expand All @@ -107,6 +112,7 @@ makeLensesFor

makeLensesFor
[ ("commitMenu", "commitMenuL")
, ("confirmingAbortForm", "confirmingAbortFormL")
]
''InitializingScreen

Expand Down
4 changes: 4 additions & 0 deletions hydra-tui/test/Hydra/TUISpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ spec = do
shouldRender "Head id"
sendInputEvent $ EvKey (KChar 'a') []
threadDelay 1
sendInputEvent $ EvKey KEnter []
threadDelay 1
shouldRender "Idle"
sendInputEvent $ EvKey (KChar 'q') []

Expand All @@ -95,6 +97,8 @@ spec = do
shouldRender "Open"
sendInputEvent $ EvKey (KChar 'c') []
threadDelay 1
sendInputEvent $ EvKey KEnter []
threadDelay 1
shouldRender "Closed"
shouldRender "Remaining time to contest"
-- XXX: This is a hack to estimate the time it takes until we can
Expand Down

0 comments on commit e46318f

Please sign in to comment.