Skip to content

Commit 4d47030

Browse files
committed
Experiment: make StateRef wrappers optional
1 parent 7d4f3a8 commit 4d47030

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/DearImGui.hs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -570,21 +570,36 @@ arrowButton strId dir = liftIO do
570570
withCString strId \strIdPtr ->
571571
Raw.arrowButton strIdPtr dir
572572

573-
574573
-- | Wraps @ImGui::Checkbox()@.
575574
checkbox :: (HasSetter ref Bool, HasGetter ref Bool, MonadIO m) => String -> ref -> m Bool
576-
checkbox label ref = liftIO do
577-
currentValue <- get ref
575+
checkbox label ref = stateful ref $ checkboxM label
576+
577+
{-# INLINEABLE stateful #-}
578+
stateful
579+
:: (HasGetter t a, MonadIO m, HasSetter t a)
580+
=> t -> (a -> m (Maybe a)) -> m Bool
581+
stateful ref action = get ref >>= action >>= maybeSet ref
582+
583+
{-# INLINEABLE maybeSet #-}
584+
maybeSet :: (HasSetter t a, MonadIO f) => t -> Maybe a -> f Bool
585+
maybeSet ref = \case
586+
Nothing ->
587+
pure False
588+
Just val -> do
589+
ref $=! val
590+
pure True
591+
592+
-- | Wraps @ImGui::Checkbox()@.
593+
checkboxM :: (MonadIO m) => String -> Bool -> m (Maybe Bool)
594+
checkboxM label currentValue = liftIO do
578595
with (bool 0 1 currentValue) \boolPtr -> do
579596
changed <- withCString label \labelPtr ->
580597
Raw.checkbox labelPtr boolPtr
581598

582-
when changed do
583-
newValue <- peek boolPtr
584-
ref $=! (newValue == 1)
585-
586-
return changed
587-
599+
if changed then
600+
(Just . (/=) 0) <$> peek boolPtr
601+
else
602+
pure Nothing
588603

589604
progressBar :: MonadIO m => Float -> Maybe String -> m ()
590605
progressBar progress overlay = liftIO do

0 commit comments

Comments
 (0)