Skip to content

Commit 4517af8

Browse files
authored
Add isPopupOpen and wrappers (#134)
1 parent b837d58 commit 4517af8

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

examples/glfw/Main.hs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,27 @@ mainLoop win = do
6464
text "Hello, ImGui!"
6565

6666
-- Add a button widget, and call 'putStrLn' when it's clicked
67-
button "Clickety Click" >>= \case
68-
False -> return ()
69-
True -> putStrLn "Ow!"
67+
clicking <- button "Clickety Click"
68+
when clicking $
69+
putStrLn "Ow!"
70+
71+
-- Attach a popup to the latest widget
72+
let popupId = "pop-me"
73+
openPopupOnItemClick popupId ImGuiPopupFlags_MouseButtonRight
74+
75+
-- Put some content into a popup window
76+
-- alternatively: withPopup (closes automatically)
77+
withPopupModalOpen popupId do
78+
text "pop!"
79+
button "ok" >>= \clicked ->
80+
when clicked $
81+
closeCurrentPopup
82+
83+
-- Query popup status
84+
popping <- isCurrentPopupOpen popupId
85+
when popping do
86+
sameLine
87+
text "Popping right now."
7088

7189
-- Show the ImGui demo window
7290
showDemoWindow

src/DearImGui.hs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ module DearImGui
248248
, openPopupOnItemClick
249249
, Raw.closeCurrentPopup
250250

251+
, isCurrentPopupOpen
252+
, isAnyPopupOpen
253+
, isAnyLevelPopupOpen
254+
251255
-- * Item/Widgets Utilities
252256
, Raw.isItemHovered
253257
, Raw.wantCaptureMouse
@@ -1496,7 +1500,27 @@ openPopup popupId = liftIO do
14961500
-- Wraps @ImGui::OpenPopup()@
14971501
openPopupOnItemClick :: MonadIO m => String -> ImGuiPopupFlags -> m ()
14981502
openPopupOnItemClick popupId flags = liftIO do
1499-
withCString popupId $ \popupId' -> Raw.openPopupOnItemClick popupId' flags
1503+
withCString popupId $ \idPtr ->
1504+
Raw.openPopupOnItemClick idPtr flags
1505+
1506+
-- | Check if the popup is open at the current 'beginPopup' level of the popup stack.
1507+
isCurrentPopupOpen :: MonadIO m => String -> m Bool
1508+
isCurrentPopupOpen popupId = liftIO do
1509+
withCString popupId $ \idPtr ->
1510+
Raw.isPopupOpen idPtr ImGuiPopupFlags_None
1511+
1512+
-- | Check if *any* popup is open at the current 'beginPopup' level of the popup stack.
1513+
isAnyPopupOpen :: MonadIO m => String -> m Bool
1514+
isAnyPopupOpen popupId = liftIO do
1515+
withCString popupId $ \idPtr ->
1516+
Raw.isPopupOpen idPtr ImGuiPopupFlags_AnyPopupId
1517+
1518+
-- | Check if *any* popup is open at any level of the popup stack.
1519+
isAnyLevelPopupOpen :: MonadIO m => String -> m Bool
1520+
isAnyLevelPopupOpen popupId = liftIO do
1521+
withCString popupId $ \idPtr ->
1522+
Raw.isPopupOpen idPtr $
1523+
ImGuiPopupFlags_AnyPopupId .|. ImGuiPopupFlags_AnyPopupLevel
15001524

15011525

15021526
withCStringOrNull :: Maybe String -> (Ptr CChar -> IO a) -> IO a

src/DearImGui/Raw.hs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ module DearImGui.Raw
199199
, openPopup
200200
, openPopupOnItemClick
201201
, closeCurrentPopup
202+
, isPopupOpen
202203

203204
-- * ID stack/scopes
204205
, pushIDInt
@@ -1253,7 +1254,10 @@ openPopup :: (MonadIO m) => CString -> m ()
12531254
openPopup popupIdPtr = liftIO do
12541255
[C.exp| void { OpenPopup($(char* popupIdPtr)) } |]
12551256

1256-
-- | Call to mark popup as open (don't call every frame!).
1257+
1258+
-- | helper to open popup when clicked on last item.
1259+
--
1260+
-- Note: actually triggers on the mouse _released_ event to be consistent with popup behaviors.
12571261
--
12581262
-- Wraps @ImGui::OpenPopupOnItemClick()@
12591263
openPopupOnItemClick :: (MonadIO m) => CString -> ImGuiPopupFlags-> m ()
@@ -1269,6 +1273,18 @@ closeCurrentPopup = liftIO do
12691273
[C.exp| void { CloseCurrentPopup() } |]
12701274

12711275

1276+
-- | Query popup status
1277+
--
1278+
-- - return 'True' if the popup is open at the current 'beginPopup' level of the popup stack.
1279+
-- - with 'ImGuiPopupFlags_AnyPopupId': return 'True' if any popup is open at the current 'beginPopup' level of the popup stack.
1280+
-- - with 'ImGuiPopupFlags_AnyPopupId' | 'ImGuiPopupFlags_AnyPopupLevel': return 'True' if any popup is open.
1281+
--
1282+
-- Wraps @ImGui::IsPopupOpen()@
1283+
isPopupOpen :: (MonadIO m) => CString -> ImGuiPopupFlags-> m Bool
1284+
isPopupOpen popupIdPtr flags = liftIO do
1285+
(0 /=) <$> [C.exp| bool { IsPopupOpen($(char* popupIdPtr), $(ImGuiPopupFlags flags)) } |]
1286+
1287+
12721288
-- | Is the last item hovered? (and usable, aka not blocked by a popup, etc.).
12731289
--
12741290
-- Wraps @ImGui::IsItemHovered()@

0 commit comments

Comments
 (0)