@@ -97,7 +97,6 @@ class MenuItem::Impl {
9797 std::optional<std::string> tooltip_;
9898 KeyboardAccelerator accelerator_;
9999 bool has_accelerator_;
100- bool enabled_;
101100 MenuItemState state_;
102101 int radio_group_;
103102 std::shared_ptr<Menu> submenu_;
@@ -108,7 +107,6 @@ class MenuItem::Impl {
108107 type_(type),
109108 accelerator_(" " , KeyboardAccelerator::None),
110109 has_accelerator_(false ),
111- enabled_(true ),
112110 state_(MenuItemState::Unchecked),
113111 radio_group_(-1 ) {}
114112
@@ -195,14 +193,19 @@ void MenuItem::RemoveAccelerator() {
195193}
196194
197195void MenuItem::SetEnabled (bool enabled) {
198- pimpl_->enabled_ = enabled;
199196 if (pimpl_->parent_menu_ ) {
200197 EnableMenuItem (pimpl_->parent_menu_ , pimpl_->id_ , enabled ? MF_ENABLED : MF_GRAYED);
201198 }
202199}
203200
204201bool MenuItem::IsEnabled () const {
205- return pimpl_->enabled_ ;
202+ if (pimpl_->parent_menu_ ) {
203+ UINT state = GetMenuState (pimpl_->parent_menu_ , pimpl_->id_ , MF_BYCOMMAND);
204+ if (state != (UINT)-1 ) {
205+ return !(state & MF_GRAYED);
206+ }
207+ }
208+ return true ;
206209}
207210
208211void MenuItem::SetState (MenuItemState state) {
@@ -267,7 +270,7 @@ void MenuItem::RemoveSubmenu() {
267270}
268271
269272bool MenuItem::Trigger () {
270- if (!pimpl_-> enabled_ )
273+ if (!IsEnabled () )
271274 return false ;
272275
273276 try {
@@ -288,12 +291,10 @@ class Menu::Impl {
288291 MenuId id_;
289292 HMENU hmenu_;
290293 std::vector<std::shared_ptr<MenuItem>> items_;
291- bool enabled_;
292- bool visible_;
293294 int window_proc_handle_id_;
294295
295296 Impl (MenuId id, HMENU menu)
296- : id_(id), hmenu_(menu), enabled_( true ), visible_( false ), window_proc_handle_id_(-1 ) {}
297+ : id_(id), hmenu_(menu), window_proc_handle_id_(-1 ) {}
297298
298299 ~Impl () {
299300 // Unregister window procedure handler
@@ -320,8 +321,6 @@ class Menu::Impl {
320321
321322 if (popup_menu == hmenu_) {
322323 // This is our menu being opened
323- visible_ = true ;
324-
325324 // Emit menu opened event
326325 try {
327326 menu->Emit <MenuOpenedEvent>(id_);
@@ -335,8 +334,6 @@ class Menu::Impl {
335334
336335 if (popup_menu == hmenu_) {
337336 // This is our menu being closed
338- visible_ = false ;
339-
340337 // Emit menu closed event
341338 try {
342339 menu->Emit <MenuClosedEvent>(id_);
@@ -568,30 +565,16 @@ bool Menu::Open() {
568565}
569566
570567bool Menu::Close () {
571- if (pimpl_->visible_ ) {
572- // Send WM_CANCELMODE to close any open menus
573- HWND host_window = WindowMessageDispatcher::GetInstance ().GetHostWindow ();
574- if (host_window) {
575- // This will close the menu and trigger WM_UNINITMENUPOPUP
576- SendMessage (host_window, WM_CANCELMODE, 0 , 0 );
577- }
568+ // Send WM_CANCELMODE to close any open menus
569+ HWND host_window = WindowMessageDispatcher::GetInstance ().GetHostWindow ();
570+ if (host_window) {
571+ // This will close the menu and trigger WM_UNINITMENUPOPUP
572+ SendMessage (host_window, WM_CANCELMODE, 0 , 0 );
578573 return true ;
579574 }
580575 return false ;
581576}
582577
583- void Menu::SetEnabled (bool enabled) {
584- pimpl_->enabled_ = enabled;
585- // Enable/disable all items
586- for (auto & item : pimpl_->items_ ) {
587- item->SetEnabled (enabled);
588- }
589- }
590-
591- bool Menu::IsEnabled () const {
592- return pimpl_->enabled_ ;
593- }
594-
595578void * Menu::GetNativeObjectInternal () const {
596579 return pimpl_->hmenu_ ;
597580}
0 commit comments