@@ -570,21 +570,58 @@ arrowButton strId dir = liftIO do
570570 withCString strId \ strIdPtr ->
571571 Raw. arrowButton strIdPtr dir
572572
573-
574573-- | Wraps @ImGui::Checkbox()@.
575574checkbox :: (HasSetter ref Bool , HasGetter ref Bool , MonadIO m ) => String -> ref -> m Bool
576- checkbox label ref = liftIO do
577- currentValue <- get ref
578- with (bool 0 1 currentValue) \ boolPtr -> do
579- changed <- withCString label \ labelPtr ->
580- Raw. checkbox labelPtr boolPtr
581-
582- when changed do
583- newValue <- peek boolPtr
584- ref $=! (newValue == 1 )
585-
586- return changed
575+ checkbox label ref = stateful ref $ checkboxM label
587576
577+ -- | Wraps @ImGui::Checkbox()@.
578+ checkboxM :: (MonadIO m ) => String -> Bool -> m (Maybe Bool )
579+ checkboxM label currentValue =
580+ changing
581+ (bool 0 1 currentValue)
582+ ( \ valuePtr ->
583+ withCString label \ labelPtr ->
584+ Raw. checkbox labelPtr valuePtr
585+ )
586+ (pure . (/=) 0 )
587+
588+ {-# INLINEABLE changing #-}
589+ changing
590+ :: (MonadIO m , Storable a1 )
591+ => a1
592+ -> (Ptr a1 -> IO Bool )
593+ -> (a1 -> IO a2 )
594+ -> m (Maybe a2 )
595+ changing oldValue action extract = liftIO do
596+ with oldValue \ valuePtr ->
597+ action valuePtr >>=
598+ peekChanged valuePtr extract
599+
600+ {-# INLINEABLE peekChanged #-}
601+ peekChanged
602+ :: (MonadIO m , Storable a1 )
603+ => Ptr a1 -> (a1 -> m a2 ) -> Bool -> m (Maybe a2 )
604+ peekChanged ptr action flag = do
605+ if flag then
606+ liftIO (peek ptr) >>=
607+ fmap Just . action
608+ else
609+ pure Nothing
610+
611+ {-# INLINEABLE stateful #-}
612+ stateful
613+ :: (HasGetter t a , MonadIO m , HasSetter t a )
614+ => t -> (a -> m (Maybe a )) -> m Bool
615+ stateful ref action = get ref >>= action >>= maybeSet ref
616+
617+ {-# INLINEABLE maybeSet #-}
618+ maybeSet :: (HasSetter t a , MonadIO f ) => t -> Maybe a -> f Bool
619+ maybeSet ref = \ case
620+ Nothing ->
621+ pure False
622+ Just val -> do
623+ ref $=! val
624+ pure True
588625
589626progressBar :: MonadIO m => Float -> Maybe String -> m ()
590627progressBar progress overlay = liftIO do
0 commit comments