Skip to content

Commit fc307a4

Browse files
authored
Add remaining popup wrappers (#136)
- BeginPopupContextItem - BeginPopupContextWindow - BeginPopupContextVoid For #132
1 parent 4517af8 commit fc307a4

File tree

3 files changed

+93
-17
lines changed

3 files changed

+93
-17
lines changed

examples/glfw/Main.hs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,12 @@ mainLoop win = do
6767
clicking <- button "Clickety Click"
6868
when clicking $
6969
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
70+
itemContextPopup do
7871
text "pop!"
7972
button "ok" >>= \clicked ->
8073
when clicked $
8174
closeCurrentPopup
8275

83-
-- Query popup status
84-
popping <- isCurrentPopupOpen popupId
85-
when popping do
86-
sameLine
87-
text "Popping right now."
88-
8976
-- Show the ImGui demo window
9077
showDemoWindow
9178

src/DearImGui.hs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,20 +234,42 @@ module DearImGui
234234
, Raw.endTooltip
235235

236236
-- * Popups/Modals
237+
238+
-- ** Generic
237239
, withPopup
238240
, withPopupOpen
239241
, beginPopup
242+
, Raw.endPopup
240243

244+
-- ** Modal
241245
, withPopupModal
242246
, withPopupModalOpen
243247
, beginPopupModal
244248

245-
, Raw.endPopup
246-
249+
-- ** Item context
250+
, itemContextPopup
251+
, withPopupContextItemOpen
252+
, withPopupContextItem
253+
, beginPopupContextItem
254+
255+
-- ** Window context
256+
, windowContextPopup
257+
, withPopupContextWindowOpen
258+
, withPopupContextWindow
259+
, beginPopupContextWindow
260+
261+
-- ** Void context
262+
, voidContextPopup
263+
, withPopupContextVoidOpen
264+
, withPopupContextVoid
265+
, beginPopupContextVoid
266+
267+
-- ** Manual
247268
, openPopup
248269
, openPopupOnItemClick
249270
, Raw.closeCurrentPopup
250271

272+
-- ** Queries
251273
, isCurrentPopupOpen
252274
, isAnyPopupOpen
253275
, isAnyLevelPopupOpen
@@ -1484,6 +1506,52 @@ withPopupModalOpen :: MonadUnliftIO m => String -> m () -> m ()
14841506
withPopupModalOpen popupId action =
14851507
withPopupModal popupId (`when` action)
14861508

1509+
beginPopupContextItem :: MonadIO m => Maybe String -> ImGuiPopupFlags -> m Bool
1510+
beginPopupContextItem itemId flags = liftIO do
1511+
withCStringOrNull itemId \popupIdPtr ->
1512+
Raw.beginPopupContextItem popupIdPtr flags
1513+
1514+
withPopupContextItem :: MonadUnliftIO m => Maybe String -> ImGuiPopupFlags -> (Bool -> m a) -> m a
1515+
withPopupContextItem popupId flags = bracket (beginPopupContextItem popupId flags) (`when` Raw.endPopup)
1516+
1517+
withPopupContextItemOpen :: MonadUnliftIO m => Maybe String -> ImGuiPopupFlags -> m () -> m ()
1518+
withPopupContextItemOpen popupId flags action = withPopupContextItem popupId flags (`when` action)
1519+
1520+
-- | Attach item context popup to right mouse button click on a last item.
1521+
itemContextPopup :: MonadUnliftIO m => m () -> m ()
1522+
itemContextPopup = withPopupContextItemOpen Nothing ImGuiPopupFlags_MouseButtonRight
1523+
1524+
beginPopupContextWindow :: MonadIO m => Maybe String -> ImGuiPopupFlags -> m Bool
1525+
beginPopupContextWindow popupId flags = liftIO do
1526+
withCStringOrNull popupId \popupIdPtr ->
1527+
Raw.beginPopupContextWindow popupIdPtr flags
1528+
1529+
withPopupContextWindow :: MonadUnliftIO m => Maybe String -> ImGuiPopupFlags -> (Bool -> m a) -> m a
1530+
withPopupContextWindow popupId flags = bracket (beginPopupContextWindow popupId flags) (`when` Raw.endPopup)
1531+
1532+
withPopupContextWindowOpen :: MonadUnliftIO m => Maybe String -> ImGuiPopupFlags -> m () -> m ()
1533+
withPopupContextWindowOpen popupId flags action = withPopupContextWindow popupId flags (`when` action)
1534+
1535+
-- | Attach item context popup to right mouse button click on a current window.
1536+
windowContextPopup :: MonadUnliftIO m => m () -> m ()
1537+
windowContextPopup = withPopupContextWindowOpen Nothing ImGuiPopupFlags_MouseButtonRight
1538+
1539+
beginPopupContextVoid :: MonadIO m => Maybe String -> ImGuiPopupFlags -> m Bool
1540+
beginPopupContextVoid popupId flags = liftIO do
1541+
withCStringOrNull popupId \popupIdPtr ->
1542+
Raw.beginPopupContextVoid popupIdPtr flags
1543+
1544+
withPopupContextVoid :: MonadUnliftIO m => Maybe String -> ImGuiPopupFlags -> (Bool -> m a) -> m a
1545+
withPopupContextVoid popupId flags = bracket (beginPopupContextVoid popupId flags) (`when` Raw.endPopup)
1546+
1547+
withPopupContextVoidOpen :: MonadUnliftIO m => Maybe String -> ImGuiPopupFlags -> m () -> m ()
1548+
withPopupContextVoidOpen popupId flags action = withPopupContextVoid popupId flags (`when` action)
1549+
1550+
-- | Attach item context popup to right mouse button click outside of any windows.
1551+
voidContextPopup :: MonadUnliftIO m => m () -> m ()
1552+
voidContextPopup = withPopupContextWindowOpen Nothing ImGuiPopupFlags_MouseButtonRight
1553+
1554+
14871555
-- | Call to mark popup as open (don't call every frame!).
14881556
--
14891557
-- Wraps @ImGui::OpenPopup()@

src/DearImGui/Raw.hs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ module DearImGui.Raw
199199
, openPopup
200200
, openPopupOnItemClick
201201
, closeCurrentPopup
202+
, beginPopupContextItem
203+
, beginPopupContextWindow
204+
, beginPopupContextVoid
202205
, isPopupOpen
203206

204207
-- * ID stack/scopes
@@ -1255,7 +1258,7 @@ openPopup popupIdPtr = liftIO do
12551258
[C.exp| void { OpenPopup($(char* popupIdPtr)) } |]
12561259

12571260

1258-
-- | helper to open popup when clicked on last item.
1261+
-- | Open popup when clicked on last item.
12591262
--
12601263
-- Note: actually triggers on the mouse _released_ event to be consistent with popup behaviors.
12611264
--
@@ -1272,6 +1275,24 @@ closeCurrentPopup :: (MonadIO m) => m ()
12721275
closeCurrentPopup = liftIO do
12731276
[C.exp| void { CloseCurrentPopup() } |]
12741277

1278+
-- | Open+begin popup when clicked on last item.
1279+
--
1280+
-- Use str_id==NULL to associate the popup to previous item.
1281+
--
1282+
-- If you want to use that on a non-interactive item such as 'text' you need to pass in an explicit ID here.
1283+
beginPopupContextItem :: (MonadIO m) => CString -> ImGuiPopupFlags-> m Bool
1284+
beginPopupContextItem popupIdPtr flags = liftIO do
1285+
(0 /=) <$> [C.exp| bool { BeginPopupContextItem($(char* popupIdPtr), $(ImGuiPopupFlags flags)) } |]
1286+
1287+
-- | Open+begin popup when clicked on current window.
1288+
beginPopupContextWindow :: (MonadIO m) => CString -> ImGuiPopupFlags-> m Bool
1289+
beginPopupContextWindow popupIdPtr flags = liftIO do
1290+
(0 /=) <$> [C.exp| bool { BeginPopupContextWindow($(char* popupIdPtr), $(ImGuiPopupFlags flags)) } |]
1291+
1292+
-- | Open+begin popup when clicked in void (where there are no windows).
1293+
beginPopupContextVoid :: (MonadIO m) => CString -> ImGuiPopupFlags-> m Bool
1294+
beginPopupContextVoid popupIdPtr flags = liftIO do
1295+
(0 /=) <$> [C.exp| bool { BeginPopupContextVoid($(char* popupIdPtr), $(ImGuiPopupFlags flags)) } |]
12751296

12761297
-- | Query popup status
12771298
--

0 commit comments

Comments
 (0)