1111
1212using namespace nativeapi ;
1313
14- // Global registry instances for managing MenuItem and Menu object lifetimes
15- static auto & g_menu_items = globalRegistry<MenuItem>();
16- static auto & g_menus = globalRegistry<Menu>();
17-
1814// Internal structures to manage event listeners
1915
2016// Event listener data structures
@@ -154,7 +150,10 @@ native_menu_item_t native_menu_item_create(const char* text,
154150 try {
155151 auto item = std::make_shared<MenuItem>(text, convert_menu_item_type (type));
156152 void * handle = item.get ();
157- g_menu_items[handle] = item; // Store shared_ptr to keep object alive
153+
154+ // Store the shared_ptr in the registry to keep the object alive
155+ GlobalRegistry<MenuItem>().Register (handle, item);
156+
158157 return static_cast <native_menu_item_t >(handle);
159158 } catch (...) {
160159 return nullptr ;
@@ -165,7 +164,10 @@ native_menu_item_t native_menu_item_create_separator(void) {
165164 try {
166165 auto item = std::make_shared<MenuItem>(" " , MenuItemType::Separator);
167166 void * handle = item.get ();
168- g_menu_items[handle] = item; // Store shared_ptr to keep object alive
167+
168+ // Store the shared_ptr in the registry to keep the object alive
169+ GlobalRegistry<MenuItem>().Register (handle, item);
170+
169171 return static_cast <native_menu_item_t >(handle);
170172 } catch (...) {
171173 return nullptr ;
@@ -182,17 +184,18 @@ void native_menu_item_destroy(native_menu_item_t item) {
182184 g_menu_item_listeners.erase (listeners_it);
183185 }
184186
185- // Remove from global storage - this will release the shared_ptr
186- auto item_it = g_menu_items.find (item);
187- if (item_it != g_menu_items.end ()) {
188- g_menu_items.erase (item_it);
189- }
187+ // Unregister from registry - this will also destroy the object
188+ GlobalRegistry<MenuItem>().Unregister (item);
190189}
191190
192191native_menu_item_id_t native_menu_item_get_id (native_menu_item_t item) {
193192 if (!item)
194193 return -1 ;
195194
195+ // Verify the item exists in the registry
196+ if (!GlobalRegistry<MenuItem>().Contains (item))
197+ return -1 ;
198+
196199 try {
197200 auto menu_item = static_cast <MenuItem*>(item);
198201 return menu_item->id ;
@@ -486,15 +489,14 @@ void native_menu_item_set_submenu(native_menu_item_t item,
486489
487490 try {
488491 // Verify item exists in global storage
489- auto item_it = g_menu_items.find (item);
490- if (item_it == g_menu_items.end ())
492+ if (!GlobalRegistry<MenuItem>().Contains (item))
491493 return ;
492494
493495 auto menu_item = static_cast <MenuItem*>(item);
494496 // Get the shared_ptr from global storage instead of creating a new one
495- auto menu_it = g_menus. find (submenu);
496- if (menu_it != g_menus. end () ) {
497- menu_item->SetSubmenu (menu_it-> second );
497+ auto submenu_ptr = GlobalRegistry<Menu>(). Get (submenu);
498+ if (submenu_ptr ) {
499+ menu_item->SetSubmenu (submenu_ptr );
498500 }
499501 } catch (...) {
500502 // Ignore exceptions
@@ -652,7 +654,10 @@ native_menu_t native_menu_create(void) {
652654 try {
653655 auto menu = std::make_shared<Menu>();
654656 void * handle = menu.get ();
655- g_menus[handle] = menu; // Store shared_ptr to keep object alive
657+
658+ // Store the shared_ptr in the registry to keep the object alive
659+ GlobalRegistry<Menu>().Register (handle, menu);
660+
656661 return static_cast <native_menu_t >(handle);
657662 } catch (...) {
658663 return nullptr ;
@@ -669,17 +674,18 @@ void native_menu_destroy(native_menu_t menu) {
669674 g_menu_listeners.erase (listeners_it);
670675 }
671676
672- // Remove from global storage - this will release the shared_ptr
673- auto menu_it = g_menus.find (menu);
674- if (menu_it != g_menus.end ()) {
675- g_menus.erase (menu_it);
676- }
677+ // Unregister from registry - this will also destroy the object
678+ GlobalRegistry<Menu>().Unregister (menu);
677679}
678680
679681native_menu_id_t native_menu_get_id (native_menu_t menu) {
680682 if (!menu)
681683 return -1 ;
682684
685+ // Verify the menu exists in the registry
686+ if (!GlobalRegistry<Menu>().Contains (menu))
687+ return -1 ;
688+
683689 try {
684690 auto menu_ptr = static_cast <Menu*>(menu);
685691 return menu_ptr->id ;
@@ -694,15 +700,14 @@ void native_menu_add_item(native_menu_t menu, native_menu_item_t item) {
694700
695701 try {
696702 // Verify menu exists in global storage
697- auto menu_it = g_menus.find (menu);
698- if (menu_it == g_menus.end ())
703+ if (!GlobalRegistry<Menu>().Contains (menu))
699704 return ;
700705
701706 auto menu_ptr = static_cast <Menu*>(menu);
702707 // Get the shared_ptr from global storage instead of creating a new one
703- auto item_it = g_menu_items. find (item);
704- if (item_it != g_menu_items. end () ) {
705- menu_ptr->AddItem (item_it-> second );
708+ auto item_ptr = GlobalRegistry<MenuItem>(). Get (item);
709+ if (item_ptr ) {
710+ menu_ptr->AddItem (item_ptr );
706711 }
707712 } catch (...) {
708713 // Ignore exceptions
@@ -717,15 +722,14 @@ void native_menu_insert_item(native_menu_t menu,
717722
718723 try {
719724 // Verify menu exists in global storage
720- auto menu_it = g_menus.find (menu);
721- if (menu_it == g_menus.end ())
725+ if (!GlobalRegistry<Menu>().Contains (menu))
722726 return ;
723727
724728 auto menu_ptr = static_cast <Menu*>(menu);
725729 // Get the shared_ptr from global storage instead of creating a new one
726- auto item_it = g_menu_items. find (item);
727- if (item_it != g_menu_items. end () ) {
728- menu_ptr->InsertItem (index, item_it-> second );
730+ auto item_ptr = GlobalRegistry<MenuItem>(). Get (item);
731+ if (item_ptr ) {
732+ menu_ptr->InsertItem (index, item_ptr );
729733 }
730734 } catch (...) {
731735 // Ignore exceptions
@@ -738,15 +742,14 @@ bool native_menu_remove_item(native_menu_t menu, native_menu_item_t item) {
738742
739743 try {
740744 // Verify menu exists in global storage
741- auto menu_it = g_menus.find (menu);
742- if (menu_it == g_menus.end ())
745+ if (!GlobalRegistry<Menu>().Contains (menu))
743746 return false ;
744747
745748 auto menu_ptr = static_cast <Menu*>(menu);
746749 // Get the shared_ptr from global storage instead of creating a new one
747- auto item_it = g_menu_items. find (item);
748- if (item_it != g_menu_items. end () ) {
749- return menu_ptr->RemoveItem (item_it-> second );
750+ auto item_ptr = GlobalRegistry<MenuItem>(). Get (item);
751+ if (item_ptr ) {
752+ return menu_ptr->RemoveItem (item_ptr );
750753 }
751754 return false ;
752755 } catch (...) {
0 commit comments