<commit_before>{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
module Graphics.Vty.Widgets.CheckBox
    ( CheckBox
    , RadioGroup
    , newCheckbox
    , newRadioGroup
    , onRadioChange
    , addToRadioGroup
    , toggleChecked
    , setCheckboxUnchecked
    , setCheckboxChecked
    , setCheckedChar
    , onCheckboxChange
    , checkboxIsChecked
    , getCheckboxLabel
    )
where

import Data.IORef
import Data.Maybe
import Control.Monad
import Control.Monad.Trans
import Graphics.Vty
import Graphics.Vty.Widgets.Core
import Graphics.Vty.Widgets.Events
import Graphics.Vty.Widgets.Util

data RadioGroupData = RadioGroupData { currentlySelected :: Maybe (Widget CheckBox)
                                     , changeHandlers :: Handlers (Widget CheckBox)
                                     }

type RadioGroup = IORef RadioGroupData

newRadioGroup :: (MonadIO m) => m RadioGroup
newRadioGroup = do
  hs <- mkHandlers
  liftIO $ newIORef $ RadioGroupData Nothing hs

onRadioChange :: (MonadIO m) => RadioGroup -> (Widget CheckBox -> IO ())
              -> m ()
onRadioChange rg act = do
  rd <- liftIO $ readIORef rg
  addHandler (return . changeHandlers) rd act

setCheckedChar :: (MonadIO m) => Widget CheckBox -> Char -> m ()
setCheckedChar wRef ch =
    updateWidgetState wRef $ \s -> s { checkedChar = ch
                                      }

addToRadioGroup :: (MonadIO m) => RadioGroup -> Widget CheckBox -> m ()
addToRadioGroup rg wRef = do
  updateWidgetState wRef $ \s -> s { radioGroup = Just rg
                                   }
  setCheckedChar wRef '*'

  wRef `onCheckboxChange` \v ->
      when v $ do
        rd <- readIORef rg
        fireEvent rd (return . changeHandlers) wRef

radioGroupSetCurrent :: (MonadIO m) => Widget CheckBox -> m ()<commit_msg>RadioGroup: when adding a checkbox, initially uncheck it<commit_after>{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
module Graphics.Vty.Widgets.CheckBox
    ( CheckBox
    , RadioGroup
    , newCheckbox
    , newRadioGroup
    , onRadioChange
    , addToRadioGroup
    , toggleChecked
    , setCheckboxUnchecked
    , setCheckboxChecked
    , setCheckedChar
    , onCheckboxChange
    , checkboxIsChecked
    , getCheckboxLabel
    )
where

import Data.IORef
import Data.Maybe
import Control.Monad
import Control.Monad.Trans
import Graphics.Vty
import Graphics.Vty.Widgets.Core
import Graphics.Vty.Widgets.Events
import Graphics.Vty.Widgets.Util

data RadioGroupData = RadioGroupData { currentlySelected :: Maybe (Widget CheckBox)
                                     , changeHandlers :: Handlers (Widget CheckBox)
                                     }

type RadioGroup = IORef RadioGroupData

newRadioGroup :: (MonadIO m) => m RadioGroup
newRadioGroup = do
  hs <- mkHandlers
  liftIO $ newIORef $ RadioGroupData Nothing hs

onRadioChange :: (MonadIO m) => RadioGroup -> (Widget CheckBox -> IO ())
              -> m ()
onRadioChange rg act = do
  rd <- liftIO $ readIORef rg
  addHandler (return . changeHandlers) rd act

setCheckedChar :: (MonadIO m) => Widget CheckBox -> Char -> m ()
setCheckedChar wRef ch =
    updateWidgetState wRef $ \s -> s { checkedChar = ch
                                      }

addToRadioGroup :: (MonadIO m) => RadioGroup -> Widget CheckBox -> m ()
addToRadioGroup rg wRef = do
  updateWidgetState wRef $ \s -> s { radioGroup = Just rg
                                   }
  setCheckedChar wRef '*'
  setCheckboxUnchecked wRef

  wRef `onCheckboxChange` \v ->
      when v $ do
        rd <- readIORef rg
        fireEvent rd (return . changeHandlers) wRef

radioGroupSetCurrent :: (MonadIO m) => Widget CheckBox -> m ()
{'fraction_non_alphanumeric': 0.08522261677137394, 'fraction_numerical': 0.0, 'mean_word_length': 2.507662835249042, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5920097', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1066, 'n_words': 293}