@@ -2,6 +2,8 @@
2Module: Reflex.Vty.Widget.Input
2Module: Reflex.Vty.Widget.Input
3Description: User input widgets for reflex-vty
3Description: User input widgets for reflex-vty
5+{-# LANGUAGE LambdaCase #-}
6+{-# LANGUAGE OverloadedStrings #-}
5module Reflex.Vty.Widget.Input
7module Reflex.Vty.Widget.Input
6 ( module Export
8 ( module Export
7 , module Reflex.Vty.Widget.Input
9 , module Reflex.Vty.Widget.Input
@@ -10,8 +12,11 @@ module Reflex.Vty.Widget.Input
11import Reflex.Vty.Widget.Input.Text as Export
13import Reflex.Vty.Widget.Input.Text as Export
15+import Control.Monad (join)
16+import Control.Monad.Fix (MonadFix)
13−import Data.Default
17+import Data.Default (Default(..))
14import Data.Text (Text)
18import Data.Text (Text)
19+import Data.Text.Zipper (Span)
15import qualified Graphics.Vty as V
20import qualified Graphics.Vty as V
16import Reflex
21import Reflex
17import Reflex.Vty.Widget
22import Reflex.Vty.Widget
@@ -68,3 +73,59 @@ linkStatic
69 -> VtyWidget t m (Event t MouseUp)
74 -> VtyWidget t m (Event t MouseUp)
70linkStatic = link . pure
75linkStatic = link . pure
77+-- | Characters used to render checked and unchecked textboxes
78+data CheckboxStyle = CheckboxStyle
79+ { _checkboxStyle_unchecked :: Text
80+ , _checkboxStyle_checked :: Text
83+instance Default CheckboxStyle where
84+ def = checkboxStyleTick
86+-- | This checkbox style uses an "x" to indicate the checked state
87+checkboxStyleX :: CheckboxStyle
88+checkboxStyleX = CheckboxStyle
89+ { _checkboxStyle_unchecked = "[ ]"
90+ , _checkboxStyle_checked = "[x]"
93+-- | This checkbox style uses a unicode tick mark to indicate the checked state
94+checkboxStyleTick :: CheckboxStyle
95+checkboxStyleTick = CheckboxStyle
96+ { _checkboxStyle_unchecked = "[ ]"
97+ , _checkboxStyle_checked = "[✓]"
100+-- | Configuration options for a checkbox
101+data CheckboxConfig t = CheckboxConfig
102+ { _checkboxConfig_checkboxStyle :: Behavior t CheckboxStyle
103+ , _checkboxConfig_attributes :: Behavior t V.Attr
106+instance (Reflex t) => Default (CheckboxConfig t) where
107+ def = CheckboxConfig
108+ { _checkboxConfig_checkboxStyle = pure def
109+ , _checkboxConfig_attributes = pure V.defAttr
112+-- | A checkbox widget
114+ :: (MonadHold t m, MonadFix m, Reflex t)
117+ -> VtyWidget t m (Dynamic t Bool)
119+ md <- mouseDown V.BLeft
121+ v <- toggle v0 $ () <$ mu
122+ depressed <- hold mempty $ leftmost
123+ [ V.withStyle mempty V.bold <$ md
126+ let attrs = (<>) <$> (_checkboxConfig_attributes cfg) <*> depressed
127+ richText (RichTextConfig attrs) $ join . current $ ffor v $ \checked ->
129+ then fmap _checkboxStyle_checked $ _checkboxConfig_checkboxStyle cfg
130+ else fmap _checkboxStyle_unchecked $ _checkboxConfig_checkboxStyle cfg