From c8f30fa7f8aff39b5105a3ea5abe847805922955 Mon Sep 17 00:00:00 2001 From: Jason Shipman Date: Thu, 14 Dec 2023 12:53:52 -0500 Subject: [PATCH] Add DearImGui.withCloseableWindow --- src/DearImGui.hs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/DearImGui.hs b/src/DearImGui.hs index ee53f81..a20a471 100644 --- a/src/DearImGui.hs +++ b/src/DearImGui.hs @@ -45,6 +45,7 @@ module DearImGui -- * Windows , withWindow , withWindowOpen + , withCloseableWindow , withFullscreen , fullscreenFlags @@ -416,6 +417,26 @@ withWindowOpen :: MonadUnliftIO m => Text -> m () -> m () withWindowOpen name action = withWindow name (`when` action) +-- | Append items to a closeable window unless it is collapsed or fully clipped. +-- +-- You may append multiple times to the same window during the same frame +-- by calling 'withWindowOpen' in multiple places. +-- +-- The 'Bool' state variable will be set to 'False' when the window's close +-- button is pressed. +withCloseableWindow :: (HasSetter ref Bool, MonadUnliftIO m) => Text -> ref -> m () -> m () +withCloseableWindow name ref action = bracket open close (`when` action) + where + open = liftIO do + with 1 \boolPtr -> do + Text.withCString name \namePtr -> do + isVisible <- Raw.begin namePtr (Just boolPtr) Nothing + isOpen <- peek boolPtr + when (isOpen == 0) $ ref $=! False + pure isVisible + + close = liftIO . const Raw.end + -- | Append items to a fullscreen window. -- -- The action runs inside a window that is set to behave as a backdrop.