Skip to content

Commit cfdb5e7

Browse files
AskBiddpwiz
authored andcommitted
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 d2e8295 commit cfdb5e7

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
@@ -326,6 +326,7 @@ module DearImGui
326326
, beginMenu
327327
, Raw.endMenu
328328

329+
, menuItemEx
329330
, menuItem
330331
, menuItemChecked
331332

@@ -2139,34 +2140,76 @@ withMenuOpen :: MonadUnliftIO m => Text -> m () -> m ()
21392140
withMenuOpen label action =
21402141
withMenu label (`when` action)
21412142

2142-
-- | Return true when activated. Shortcuts are displayed for convenience but not
2143-
-- processed by ImGui at the moment
2143+
-- | Menu item with optional shortcut and checkmark.
2144+
--
2145+
-- Returns whether the item was activated (clicked).
2146+
-- If a checkmark is provided, it will be updated to reflect
2147+
-- the new state.
2148+
--
2149+
-- Note that shortcuts are displayed for convenience only and
2150+
-- are not automatically handled by ImGui.
2151+
--
2152+
-- Wraps @ImGui::MenuItem()@
2153+
menuItemEx
2154+
:: MonadIO m
2155+
=> Text
2156+
-> Maybe Text
2157+
-> Maybe Bool
2158+
-> m (Bool, Maybe Bool)
2159+
menuItemEx label maybeShortcut maybeChecked = liftIO do
2160+
Text.withCString label $ \labelPtr ->
2161+
case maybeShortcut of
2162+
Nothing ->
2163+
withCString "" $ \shortcutPtr ->
2164+
go labelPtr shortcutPtr
2165+
Just shortcut ->
2166+
Text.withCString shortcut $ \shortcutPtr ->
2167+
go labelPtr shortcutPtr
2168+
where
2169+
go labelPtr shortcutPtr =
2170+
case maybeChecked of
2171+
Nothing -> do
2172+
activated <- Raw.menuItemEx labelPtr shortcutPtr nullPtr
2173+
pure (activated, Nothing)
2174+
2175+
Just checked ->
2176+
alloca $ \ptr -> do
2177+
poke ptr (fromBool checked)
2178+
activated <- Raw.menuItemEx labelPtr shortcutPtr ptr
2179+
newChecked <- toBool <$> peek ptr
2180+
pure (activated, Just newChecked)
2181+
2182+
-- | Simple menu item without shortcut or checkmark.
2183+
--
2184+
-- Returns True when activated.
2185+
-- This is a convenience wrapper over 'menuItemEx'.
21442186
--
21452187
-- Wraps @ImGui::MenuItem()@
21462188
menuItem :: MonadIO m => Text -> m Bool
2147-
menuItem label = liftIO do
2148-
Text.withCString label Raw.menuItem
2189+
menuItem label = do
2190+
(activated, _) <- menuItemEx label Nothing Nothing
2191+
pure activated
21492192

21502193
-- | Menu item with checkmark
21512194
--
2152-
-- Returns True when activated (clicked). The ref's value will be updated to match the checkmark state.
2195+
-- Returns (activated, newCheckedState) for whether the item
2196+
-- was activated and the updated checkmark state.
2197+
-- This is a convenience wrapper over 'menuItemEx'.
21532198
menuItemChecked
21542199
:: MonadIO m
21552200
=> Text
21562201
-> Maybe Text
21572202
-> Bool
21582203
-> m (Bool, Bool)
2159-
menuItemChecked label maybeShortcut isChecked = liftIO do
2160-
let shortcut = case maybeShortcut of
2161-
Nothing -> ""
2162-
Just s -> s
2163-
Text.withCString label $ \labelPtr ->
2164-
Text.withCString shortcut $ \shortcutPtr ->
2165-
alloca $ \ptr -> do
2166-
poke ptr (fromBool isChecked)
2167-
activated <- Raw.menuItemChecked labelPtr shortcutPtr ptr
2168-
newChecked <- toBool <$> peek ptr
2169-
pure (activated, newChecked)
2204+
menuItemChecked label maybeShortcut isChecked = do
2205+
(activated, mChecked) <-
2206+
menuItemEx label maybeShortcut (Just isChecked)
2207+
case mChecked of
2208+
Just newChecked ->
2209+
pure (activated, newChecked)
2210+
Nothing ->
2211+
-- This should be impossible, but keeps the function total
2212+
pure (activated, isChecked)
21702213

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

src/DearImGui/Raw.hs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,7 @@ module DearImGui.Raw
275275
, endMainMenuBar
276276
, beginMenu
277277
, endMenu
278-
, menuItem
279-
, menuItemChecked
278+
, menuItemEx
280279

281280
-- ** Tabs, tab bar
282281
, beginTabBar
@@ -1666,29 +1665,14 @@ endMenu = liftIO do
16661665
[C.exp| void { EndMenu(); } |]
16671666

16681667

1669-
-- | Return true when activated. Shortcuts are displayed for convenience but not
1670-
-- processed by ImGui at the moment
1671-
--
1672-
-- Wraps @ImGui::MenuItem()@
1673-
menuItem :: (MonadIO m) => CString -> m Bool
1674-
menuItem labelPtr = liftIO do
1675-
(0 /=) <$> [C.exp| bool { MenuItem($(char* labelPtr)) } |]
1676-
1677-
1678-
-- | Menu item with checkmark support
1668+
-- | Full MenuItem binding
16791669
--
16801670
-- Return true when activated. The bool pointer will be updated to reflect the new state.
16811671
--
1682-
-- Wraps @ImGui::MenuItem()@ with bool* parameter
1683-
menuItemChecked :: (MonadIO m) => CString -> CString -> Ptr CBool -> m Bool
1684-
menuItemChecked labelPtr shortcutPtr selectedPtr = liftIO do
1685-
(0 /=) <$> [C.exp| bool {
1686-
MenuItem(
1687-
$(char* labelPtr),
1688-
$(char* shortcutPtr),
1689-
$(bool* selectedPtr)
1690-
)
1691-
} |]
1672+
-- Wraps @ImGui::MenuItem(const char*, const char*, bool*)@
1673+
menuItemEx :: (MonadIO m) => CString -> CString -> Ptr CBool -> m Bool
1674+
menuItemEx labelPtr shortcutPtr selectedPtr = liftIO do
1675+
(0 /=) <$> [C.exp| bool { MenuItem($(char* labelPtr), $(char* shortcutPtr), $(bool* selectedPtr) ) } |]
16921676

16931677

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

0 commit comments

Comments
 (0)