Skip to content

Commit 84ac3f8

Browse files
committed
Use static keys for Objective-C associated objects
Replaces string keys with static const void* keys for objc_setAssociatedObject and objc_getAssociatedObject calls. This improves type safety and avoids potential key collisions in menu and menu item ID association.
1 parent 4d7bf4b commit 84ac3f8

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/platform/macos/menu_macos.mm

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
// Note: This file assumes ARC (Automatic Reference Counting) is enabled
1313
// for proper memory management of Objective-C objects.
1414

15+
// Static keys for associated objects
16+
static const void* kMenuItemIdKey = &kMenuItemIdKey;
17+
static const void* kMenuIdKey = &kMenuIdKey;
18+
1519
// Forward declarations - moved to global scope
1620
typedef void (^MenuItemClickedBlock)(nativeapi::MenuItemID item_id, const std::string& item_text);
1721
typedef void (^MenuOpenedBlock)(nativeapi::MenuID menu_id);
@@ -122,7 +126,7 @@ - (void)menuItemClicked:(id)sender {
122126
if (_clickedBlock) {
123127
std::string itemText = [[menuItem title] UTF8String];
124128
// Get the MenuItemID from the menu item's associated object
125-
NSNumber* itemIdObj = objc_getAssociatedObject(menuItem, @"menuItemId");
129+
NSNumber* itemIdObj = objc_getAssociatedObject(menuItem, kMenuItemIdKey);
126130
if (itemIdObj) {
127131
nativeapi::MenuItemID itemId = [itemIdObj longValue];
128132
_clickedBlock(itemId, itemText);
@@ -136,7 +140,7 @@ @implementation MenuDelegate
136140
- (void)menuWillOpen:(NSMenu*)menu {
137141
if (_openedBlock) {
138142
// Get the MenuID from the menu's associated object
139-
NSNumber* menuIdObj = objc_getAssociatedObject(menu, @"menuId");
143+
NSNumber* menuIdObj = objc_getAssociatedObject(menu, kMenuIdKey);
140144
if (menuIdObj) {
141145
nativeapi::MenuID menuId = [menuIdObj longValue];
142146
_openedBlock(menuId);
@@ -147,7 +151,7 @@ - (void)menuWillOpen:(NSMenu*)menu {
147151
- (void)menuDidClose:(NSMenu*)menu {
148152
if (_closedBlock) {
149153
// Get the MenuID from the menu's associated object
150-
NSNumber* menuIdObj = objc_getAssociatedObject(menu, @"menuId");
154+
NSNumber* menuIdObj = objc_getAssociatedObject(menu, kMenuIdKey);
151155
if (menuIdObj) {
152156
nativeapi::MenuID menuId = [menuIdObj longValue];
153157
_closedBlock(menuId);
@@ -229,7 +233,7 @@ - (void)menuDidClose:(NSMenu*)menu {
229233
auto item = std::shared_ptr<MenuItem>(new MenuItem(text, type));
230234
item->pimpl_ = std::make_unique<Impl>(nsItem, type);
231235
item->id = g_next_menu_item_id++;
232-
objc_setAssociatedObject(nsItem, @"menuItemId", [NSNumber numberWithLong:item->id],
236+
objc_setAssociatedObject(nsItem, kMenuItemIdKey, [NSNumber numberWithLong:item->id],
233237
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
234238
item->pimpl_->text_ = text;
235239

@@ -244,7 +248,7 @@ - (void)menuDidClose:(NSMenu*)menu {
244248
: id(g_next_menu_item_id++),
245249
pimpl_(std::make_unique<Impl>((__bridge NSMenuItem*)native_item, MenuItemType::Normal)) {
246250
NSMenuItem* nsItem = (__bridge NSMenuItem*)native_item;
247-
objc_setAssociatedObject(nsItem, @"menuItemId", [NSNumber numberWithLong:id],
251+
objc_setAssociatedObject(nsItem, kMenuItemIdKey, [NSNumber numberWithLong:id],
248252
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
249253

250254
// 设置默认的 Block 处理器,直接发送事件
@@ -410,7 +414,7 @@ - (void)menuDidClose:(NSMenu*)menu {
410414
NSObject* targetObj = [sibling target];
411415
if ([targetObj isKindOfClass:[MenuItemTarget class]]) {
412416
// Get the MenuItemID from the associated object
413-
NSNumber* siblingIdObj = objc_getAssociatedObject(sibling, @"menuItemId");
417+
NSNumber* siblingIdObj = objc_getAssociatedObject(sibling, kMenuItemIdKey);
414418
if (siblingIdObj) {
415419
MenuItemID siblingId = [siblingIdObj longValue];
416420
// Find the corresponding MenuItem in the parent menu's items
@@ -507,7 +511,7 @@ - (void)menuDidClose:(NSMenu*)menu {
507511
auto menu = std::shared_ptr<Menu>(new Menu());
508512
menu->pimpl_ = std::make_unique<Impl>(nsMenu);
509513
menu->id = g_next_menu_id++;
510-
objc_setAssociatedObject(nsMenu, @"menuId", [NSNumber numberWithLong:menu->id],
514+
objc_setAssociatedObject(nsMenu, kMenuIdKey, [NSNumber numberWithLong:menu->id],
511515
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
512516
// 设置默认的 Block 处理器,直接发送事件
513517
menu->pimpl_->delegate_.openedBlock = ^(MenuID menu_id) {
@@ -531,7 +535,7 @@ - (void)menuDidClose:(NSMenu*)menu {
531535
Menu::Menu(void* native_menu)
532536
: id(g_next_menu_id++), pimpl_(std::make_unique<Impl>((__bridge NSMenu*)native_menu)) {
533537
NSMenu* nsMenu = (__bridge NSMenu*)native_menu;
534-
objc_setAssociatedObject(nsMenu, @"menuId", [NSNumber numberWithLong:id],
538+
objc_setAssociatedObject(nsMenu, kMenuIdKey, [NSNumber numberWithLong:id],
535539
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
536540

537541
// 设置默认的 Block 处理器,直接发送事件

0 commit comments

Comments
 (0)