@@ -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 ()
21002101withMenuOpen 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()@
21072149menuItem :: 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'.
21142159menuItemChecked
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--
0 commit comments