Skip to content

Commit 6506433

Browse files
CopilotSaadnajmi
andcommitted
Access DevMenu from contextContainer instead of using notifications
Co-authored-by: Saadnajmi <[email protected]>
1 parent ee90ce2 commit 6506433

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

packages/react-native/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingView.mm

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
#if __has_include(<React/RCTDevMenu.h>) && RCT_DEV
1717
#import <React/RCTDevMenu.h>
18+
#import <React/RCTSurfacePresenter.h>
19+
#import <react/utils/ContextContainer.h>
20+
#import <react/utils/ManagedObjectWrapper.h>
1821
#endif
1922

2023
@interface RCTSurfaceHostingView ()
@@ -287,11 +290,30 @@ - (NSMenu *)menuForEvent:(NSEvent *)event
287290
{
288291
NSMenu *menu = nil;
289292
#if __has_include(<React/RCTDevMenu.h>) && RCT_DEV
290-
// In Fabric/bridgeless mode, trigger the dev menu via notification
291-
// This is bridge-independent and works without accessing the bridge
292-
[[NSNotificationCenter defaultCenter] postNotificationName:RCTShowDevMenuNotification object:nil];
293-
// Return nil as the menu will be shown programmatically via notification
294-
return nil;
293+
// Try to get DevMenu from the contextContainer via the surface presenter
294+
// This works in Fabric architecture where DevMenu is stored in contextContainer
295+
if ([self.surface respondsToSelector:@selector(surfacePresenter)]) {
296+
RCTSurfacePresenter *surfacePresenter = [self.surface performSelector:@selector(surfacePresenter)];
297+
if (surfacePresenter) {
298+
auto contextContainer = surfacePresenter.contextContainer;
299+
if (contextContainer) {
300+
auto optionalDevMenu = contextContainer->find<std::shared_ptr<void>>("RCTDevMenu");
301+
if (optionalDevMenu) {
302+
RCTDevMenu *devMenu = facebook::react::unwrapManagedObject(optionalDevMenu.value());
303+
if (devMenu) {
304+
menu = [devMenu menu];
305+
}
306+
}
307+
}
308+
}
309+
}
310+
311+
// Fall back to notification-based approach if contextContainer access fails
312+
if (menu == nil) {
313+
[[NSNotificationCenter defaultCenter] postNotificationName:RCTShowDevMenuNotification object:nil];
314+
// Return nil as the menu will be shown programmatically via notification
315+
return nil;
316+
}
295317
#endif
296318

297319
if (menu == nil) {

0 commit comments

Comments
 (0)