@@ -19,14 +19,14 @@ import Brick.Forms (
19
19
newForm ,
20
20
radioField ,
21
21
)
22
- import Brick.Types (Location (.. ))
22
+ import Brick.Types (Location (.. ), Widget )
23
23
import Brick.Widgets.Core (clickable , putCursor , txt , (<+>) )
24
24
import Cardano.Api.UTxO qualified as UTxO
25
25
import Data.Map.Strict qualified as Map
26
26
import Data.Text qualified as Text
27
27
import Graphics.Vty (Event (.. ), Key (.. ))
28
28
import Hydra.Chain.Direct.State ()
29
- import Lens.Micro (Lens' )
29
+ import Lens.Micro (Lens' , (^.) )
30
30
import Prelude qualified
31
31
32
32
utxoCheckboxField ::
@@ -83,14 +83,15 @@ confirmRadioField =
83
83
84
84
radioFields = radioField id [(opt, fst opt, show $ fst opt) | opt <- options]
85
85
86
+ type LeftBracketChar = Char
87
+ type CheckmarkChar = Char
88
+ type RightBracketChar = Char
89
+
86
90
checkboxGroupField ::
87
91
(Ord k , Ord n ) =>
88
- -- | Left bracket character.
89
- Char ->
90
- -- | Checkmark character.
91
- Char ->
92
- -- | Right bracket character.
93
- Char ->
92
+ LeftBracketChar ->
93
+ CheckmarkChar ->
94
+ RightBracketChar ->
94
95
-- | The state lens for this value.
95
96
Lens' (Map k (a , Bool )) (Map k (a , Bool )) ->
96
97
-- | The available choices, in order.
@@ -148,3 +149,69 @@ checkboxGroupField lb check rb stLens options initialState =
148
149
case Map. lookup k cur of
149
150
Nothing -> return ()
150
151
Just _ -> put $ Map. adjust (second not ) k cur
152
+
153
+ type FormFieldRenderHelper a n = (a -> Text -> Bool -> Widget n -> Widget n )
154
+
155
+ customRadioField ::
156
+ (Ord n , Eq a ) =>
157
+ LeftBracketChar ->
158
+ CheckmarkChar ->
159
+ RightBracketChar ->
160
+ -- | The state lens for this value.
161
+ Lens' s a ->
162
+ -- | The available choices, in order. Each choice has a value
163
+ -- of type @a@, a resource name, and a text label.
164
+ [(a , n , Text. Text )] ->
165
+ -- | Render widget helper.
166
+ FormFieldRenderHelper a n ->
167
+ -- | The initial form state.
168
+ s ->
169
+ FormFieldState s e n
170
+ customRadioField lb check rb stLens options decorator initialState =
171
+ let initVal = initialState ^. stLens
172
+
173
+ lookupOptionValue n =
174
+ let results = filter (\ (_, n', _) -> n' == n) options
175
+ in case results of
176
+ [(val, _, _)] -> Just val
177
+ _ -> Nothing
178
+
179
+ handleEvent _ (MouseDown n _ _ _) = forM_ (lookupOptionValue n) put
180
+ handleEvent new (VtyEvent (EvKey (KChar ' ' ) [] )) = put new
181
+ handleEvent _ _ = return ()
182
+
183
+ optionFields = mkOptionField <$> options
184
+ mkOptionField (val, name, lbl) =
185
+ FormField
186
+ name
187
+ Just
188
+ True
189
+ (renderRadio val name lbl)
190
+ (handleEvent val)
191
+ in FormFieldState
192
+ { formFieldState = initVal
193
+ , formFields = optionFields
194
+ , formFieldLens = stLens
195
+ , formFieldUpdate = const
196
+ , formFieldRenderHelper = id
197
+ , formFieldConcat = vBox
198
+ , formFieldVisibilityMode = ShowFocusedFieldOnly
199
+ }
200
+ where
201
+ renderRadio val name lbl foc cur =
202
+ let addAttr =
203
+ if foc
204
+ then withDefAttr focusedFormInputAttr
205
+ else id
206
+ isSet = val == cur
207
+ csr = if foc then putCursor name (Location (1 , 0 )) else id
208
+ in clickable name $
209
+ addAttr $
210
+ csr $
211
+ decorator val lbl isSet $
212
+ txt $
213
+ Text. concat
214
+ [ Text. singleton lb
215
+ , if isSet then Text. singleton check else " "
216
+ , Text. singleton rb <> " " <> lbl
217
+ ]
0 commit comments