Skip to content

Commit 9c0746d

Browse files
author
AskBid
committed
MenuItem: introduce menuItemEx and normalize API
- Replace raw MenuItem bindings with a single full-parameter variant - Add menuItemEx to the high-level API - Keep menuItem and menuItemChecked as a shorthand wrapper
1 parent c4fa5bf commit 9c0746d

2 files changed

Lines changed: 65 additions & 38 deletions

File tree

src/DearImGui.hs

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ module DearImGui
320320
, beginMenu
321321
, Raw.endMenu
322322

323+
, menuItemEx
323324
, menuItem
324325
, menuItemChecked
325326

@@ -2100,34 +2101,76 @@ withMenuOpen :: MonadUnliftIO m => Text -> m () -> m ()
21002101
withMenuOpen label action =
21012102
withMenu label (`when` action)
21022103

2103-
-- | Return true when activated. Shortcuts are displayed for convenience but not
2104-
-- processed by ImGui at the moment
2104+
-- | Menu item with optional shortcut and checkmark.
2105+
--
2106+
-- Returns whether the item was activated (clicked).
2107+
-- If a checkmark is provided, it will be updated to reflect
2108+
-- the new state.
2109+
--
2110+
-- Note that shortcuts are displayed for convenience only and
2111+
-- are not automatically handled by ImGui.
2112+
--
2113+
-- Wraps @ImGui::MenuItem()@
2114+
menuItemEx
2115+
:: MonadIO m
2116+
=> Text
2117+
-> Maybe Text
2118+
-> Maybe Bool
2119+
-> m (Bool, Maybe Bool)
2120+
menuItemEx label maybeShortcut maybeChecked = liftIO do
2121+
Text.withCString label $ \labelPtr ->
2122+
case maybeShortcut of
2123+
Nothing ->
2124+
withCString "" $ \shortcutPtr ->
2125+
go labelPtr shortcutPtr
2126+
Just shortcut ->
2127+
Text.withCString shortcut $ \shortcutPtr ->
2128+
go labelPtr shortcutPtr
2129+
where
2130+
go labelPtr shortcutPtr =
2131+
case maybeChecked of
2132+
Nothing -> do
2133+
activated <- Raw.menuItemEx labelPtr shortcutPtr nullPtr
2134+
pure (activated, Nothing)
2135+
2136+
Just checked ->
2137+
alloca $ \ptr -> do
2138+
poke ptr (fromBool checked)
2139+
activated <- Raw.menuItemEx labelPtr shortcutPtr ptr
2140+
newChecked <- toBool <$> peek ptr
2141+
pure (activated, Just newChecked)
2142+
2143+
-- | Simple menu item without shortcut or checkmark.
2144+
--
2145+
-- Returns True when activated.
2146+
-- This is a convenience wrapper over 'menuItemEx'.
21052147
--
21062148
-- Wraps @ImGui::MenuItem()@
21072149
menuItem :: MonadIO m => Text -> m Bool
2108-
menuItem label = liftIO do
2109-
Text.withCString label Raw.menuItem
2150+
menuItem label = do
2151+
(activated, _) <- menuItemEx label Nothing Nothing
2152+
pure activated
21102153

21112154
-- | Menu item with checkmark
21122155
--
2113-
-- Returns True when activated (clicked). The ref's value will be updated to match the checkmark state.
2156+
-- Returns (activated, newCheckedState) for whether the item
2157+
-- was activated and the updated checkmark state.
2158+
-- This is a convenience wrapper over 'menuItemEx'.
21142159
menuItemChecked
21152160
:: MonadIO m
21162161
=> Text
21172162
-> Maybe Text
21182163
-> Bool
21192164
-> m (Bool, Bool)
2120-
menuItemChecked label maybeShortcut isChecked = liftIO do
2121-
let shortcut = case maybeShortcut of
2122-
Nothing -> ""
2123-
Just s -> s
2124-
Text.withCString label $ \labelPtr ->
2125-
Text.withCString shortcut $ \shortcutPtr ->
2126-
alloca $ \ptr -> do
2127-
poke ptr (fromBool isChecked)
2128-
activated <- Raw.menuItemChecked labelPtr shortcutPtr ptr
2129-
newChecked <- toBool <$> peek ptr
2130-
pure (activated, newChecked)
2165+
menuItemChecked label maybeShortcut isChecked = do
2166+
(activated, mChecked) <-
2167+
menuItemEx label maybeShortcut (Just isChecked)
2168+
case mChecked of
2169+
Just newChecked ->
2170+
pure (activated, newChecked)
2171+
Nothing ->
2172+
-- This should be impossible, but keeps the function total
2173+
pure (activated, isChecked)
21312174

21322175
-- | Create a @TabBar@ and start appending to it.
21332176
--

src/DearImGui/Raw.hs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ module DearImGui.Raw
269269
, endMainMenuBar
270270
, beginMenu
271271
, endMenu
272-
, menuItem
273-
, menuItemChecked
272+
, menuItemEx
274273

275274
-- ** Tabs, tab bar
276275
, beginTabBar
@@ -1639,29 +1638,14 @@ endMenu = liftIO do
16391638
[C.exp| void { EndMenu(); } |]
16401639

16411640

1642-
-- | Return true when activated. Shortcuts are displayed for convenience but not
1643-
-- processed by ImGui at the moment
1644-
--
1645-
-- Wraps @ImGui::MenuItem()@
1646-
menuItem :: (MonadIO m) => CString -> m Bool
1647-
menuItem labelPtr = liftIO do
1648-
(0 /=) <$> [C.exp| bool { MenuItem($(char* labelPtr)) } |]
1649-
1650-
1651-
-- | Menu item with checkmark support
1641+
-- | Full MenuItem binding
16521642
--
16531643
-- Return true when activated. The bool pointer will be updated to reflect the new state.
16541644
--
1655-
-- Wraps @ImGui::MenuItem()@ with bool* parameter
1656-
menuItemChecked :: (MonadIO m) => CString -> CString -> Ptr CBool -> m Bool
1657-
menuItemChecked labelPtr shortcutPtr selectedPtr = liftIO do
1658-
(0 /=) <$> [C.exp| bool {
1659-
MenuItem(
1660-
$(char* labelPtr),
1661-
$(char* shortcutPtr),
1662-
$(bool* selectedPtr)
1663-
)
1664-
} |]
1645+
-- Wraps @ImGui::MenuItem(const char*, const char*, bool*)@
1646+
menuItemEx :: (MonadIO m) => CString -> CString -> Ptr CBool -> m Bool
1647+
menuItemEx labelPtr shortcutPtr selectedPtr = liftIO do
1648+
(0 /=) <$> [C.exp| bool { MenuItem($(char* labelPtr), $(char* shortcutPtr), $(bool* selectedPtr) ) } |]
16651649

16661650

16671651
-- | Create a @TabBar@ and start appending to it.

0 commit comments

Comments
 (0)