@@ -570,21 +570,36 @@ arrowButton strId dir = liftIO do
570
570
withCString strId \ strIdPtr ->
571
571
Raw. arrowButton strIdPtr dir
572
572
573
-
574
573
-- | Wraps @ImGui::Checkbox()@.
575
574
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
578
595
with (bool 0 1 currentValue) \ boolPtr -> do
579
596
changed <- withCString label \ labelPtr ->
580
597
Raw. checkbox labelPtr boolPtr
581
598
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
588
603
589
604
progressBar :: MonadIO m => Float -> Maybe String -> m ()
590
605
progressBar progress overlay = liftIO do
0 commit comments