nano-ui-0.2.0.0: lib/NanoUI/Internal/Widgets/Checkbox.hs
-- | Boolean buttons: a checkbox, a button in the choice look
-- ('buttonFlagChoice'), and a toggle button, filled while it is on.
module NanoUI.Internal.Widgets.Checkbox
( checkbox
, checkbox'
, checkboxWith
, checkboxWith'
, toggleButton
, toggleButton'
, toggleButtonWith
, toggleButtonWith'
) where
import Data.Functor (void)
import Data.Text (Text)
import Data.Word (Word64)
import NanoUI.Internal.Monad (NanoUI)
import NanoUI.Internal.Style (Layout, Tone, defaultLayout)
import NanoUI.Internal.Var (Binding (..), bindVar)
import NanoUI.Internal.Var.Types (Var)
import NanoUI.Internal.WidgetText (buttonFlagChoice, buttonToneStyle)
import NanoUI.Internal.Widgets.Combinators (choiceToggleWidget, widgetBaseLayout)
import NanoUI.Internal.Widgets.Node (Response)
{-# INLINE toggleWith' #-}
toggleWith' :: Word64 -> (Layout -> Layout) -> Text -> Var Bool -> NanoUI Response
toggleWith' flag f txt v = do
lay <- widgetBaseLayout defaultLayout f
bindVar RenderFinal v (choiceToggleWidget txt lay flag)
-- | Checkbox with a caption bound to mutable state ('Var'): a click or
-- Space/Enter toggles the value in place.
--
-- > checkbox "Dark mode" settings.darkMode
checkbox :: Text -> Var Bool -> NanoUI ()
checkbox txt v = void (toggleWith' buttonFlagChoice id txt v)
-- | 'checkbox' returning its 'Response'.
checkbox' :: Text -> Var Bool -> NanoUI Response
checkbox' txt v = toggleWith' buttonFlagChoice id txt v
-- | 'checkbox' with a layout modifier: @checkboxWith alignMiddle@ centers it in
-- a row taller than itself, and @checkboxWith fillW@ gives it the row.
checkboxWith :: (Layout -> Layout) -> Text -> Var Bool -> NanoUI ()
checkboxWith f txt v = void (toggleWith' buttonFlagChoice f txt v)
-- | 'checkboxWith' returning its 'Response'.
checkboxWith' :: (Layout -> Layout) -> Text -> Var Bool -> NanoUI Response
checkboxWith' f txt v = toggleWith' buttonFlagChoice f txt v
-- | Toggle button bound to mutable state ('Var'): filled in the tone's color
-- while on, a plain button while off. A click or Space/Enter toggles it in place.
--
-- > toggleButton Warning "M" muted
toggleButton :: Tone -> Text -> Var Bool -> NanoUI ()
toggleButton t txt v = void (toggleWith' (buttonToneStyle t) id txt v)
-- | 'toggleButton' returning its 'Response'.
toggleButton' :: Tone -> Text -> Var Bool -> NanoUI Response
toggleButton' t txt v = toggleWith' (buttonToneStyle t) id txt v
-- | 'toggleButton' with a layout modifier: @toggleButtonWith (fixedWH 32 30)@.
toggleButtonWith :: (Layout -> Layout) -> Tone -> Text -> Var Bool -> NanoUI ()
toggleButtonWith f t txt v = void (toggleWith' (buttonToneStyle t) f txt v)
-- | 'toggleButtonWith' returning its 'Response'.
toggleButtonWith' :: (Layout -> Layout) -> Tone -> Text -> Var Bool -> NanoUI Response
toggleButtonWith' f t txt v = toggleWith' (buttonToneStyle t) f txt v