Skip to content

Commit 93b729d

Browse files
committed
Wrap ImGui popups
1 parent c4d54a6 commit 93b729d

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

Main.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ loop w checked = do
4545
text "Hello!"
4646

4747
button "Click me" >>= \case
48-
True -> putStrLn "Oh hi Mark"
48+
True -> openPopup "Button Popup"
4949
False -> return ()
5050

51+
beginPopupModal "Button Popup" >>= whenTrue do
52+
button "Close" >>= whenTrue closeCurrentPopup
53+
endPopup
54+
5155
sameLine >> smallButton "Click me" >>= \case
5256
True -> putStrLn "Oh hi Mark"
5357
False -> return ()

src/DearImGui.hs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ module DearImGui
8080
, endMenu
8181
, menuItem
8282

83+
-- * Popups/Modals
84+
, beginPopup
85+
, beginPopupModal
86+
, endPopup
87+
, openPopup
88+
, closeCurrentPopup
89+
8390
-- * Types
8491
, ImGuiDir
8592
, pattern ImGuiDirLeft
@@ -483,6 +490,49 @@ menuItem label = liftIO do
483490
(1 ==) <$> [C.exp| bool { MenuItem($(char* labelPtr)) } |]
484491

485492

493+
-- | Returns 'True' if the popup is open, and you can start outputting to it.
494+
--
495+
-- Wraps @ImGui::BeginPopup()@
496+
beginPopup :: MonadIO m => String -> m Bool
497+
beginPopup popupId = liftIO do
498+
withCString popupId \popupIdPtr ->
499+
(1 ==) <$> [C.exp| bool { BeginPopup($(char* popupIdPtr)) } |]
500+
501+
502+
-- | Returns 'True' if the modal is open, and you can start outputting to it.
503+
--
504+
-- Wraps @ImGui::BeginPopupModal()@
505+
beginPopupModal :: MonadIO m => String -> m Bool
506+
beginPopupModal popupId = liftIO do
507+
withCString popupId \popupIdPtr ->
508+
(1 ==) <$> [C.exp| bool { BeginPopupModal($(char* popupIdPtr)) } |]
509+
510+
511+
-- | Only call 'endPopup' if 'beginPopup' or 'beginPopupModal' returns 'True'!
512+
--
513+
-- Wraps @ImGui::BeginPopupModal()@
514+
endPopup :: MonadIO m => m ()
515+
endPopup = liftIO do
516+
[C.exp| void { EndPopup() } |]
517+
518+
519+
-- | Call to mark popup as open (don't call every frame!).
520+
--
521+
-- Wraps @ImGui::OpenPopup()@
522+
openPopup :: MonadIO m => String -> m ()
523+
openPopup popupId = liftIO do
524+
withCString popupId \popupIdPtr ->
525+
[C.exp| void { OpenPopup($(char* popupIdPtr)) } |]
526+
527+
528+
-- | Manually close the popup we have begin-ed into.
529+
--
530+
-- Wraps @ImGui::ClosePopup()@
531+
closeCurrentPopup :: MonadIO m => m ()
532+
closeCurrentPopup = liftIO do
533+
[C.exp| void { CloseCurrentPopup() } |]
534+
535+
486536
-- | A cardinal direction.
487537
newtype ImGuiDir = ImGuiDir CInt
488538

0 commit comments

Comments
 (0)