Skip to content

Commit 62022f3

Browse files
committed
it worksss
1 parent 23240f7 commit 62022f3

File tree

4 files changed

+113
-8
lines changed

4 files changed

+113
-8
lines changed

packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@
2525
#import <React/RCTComponentViewProtocol.h>
2626
#import <React/RCTFabricSurface.h>
2727
#import <React/RCTSurfaceHostingProxyRootView.h>
28+
#import <React/RCTSurfaceHostingView+Private.h> // [macOS]
2829
#import <React/RCTSurfacePresenter.h>
30+
31+
#if TARGET_OS_OSX && __has_include("RCTDevMenu.h") // [macOS]
32+
#import "RCTDevMenu.h"
33+
#endif // [macOS]
2934
#import <ReactCommon/RCTHost+Internal.h>
3035
#import <ReactCommon/RCTHost.h>
3136
#import <ReactCommon/RCTTurboModuleManager.h>
3237
#import <react/renderer/runtimescheduler/RuntimeScheduler.h>
3338
#import <react/renderer/runtimescheduler/RuntimeSchedulerCallInvoker.h>
3439
#import <react/runtime/JSRuntimeFactory.h>
40+
#import <react/utils/ManagedObjectWrapper.h>
41+
42+
using namespace facebook::react;
3543

3644
@implementation RCTRootViewFactoryConfiguration
3745

@@ -151,6 +159,18 @@ - (RCTPlatformView *)viewWithModuleName:(NSString *)moduleName // [macOS]
151159
#if !TARGET_OS_OSX // [macOS]
152160
surfaceHostingProxyRootView.backgroundColor = [UIColor systemBackgroundColor];
153161
#endif // [macOS]
162+
163+
[surfaceHostingProxyRootView setContextContainer:_contextContainer]; // [macOS]
164+
165+
#if TARGET_OS_OSX && __has_include("RCTDevMenu.h") && RCT_DEV
166+
// Insert dev menu for macOS context menu access in bridgeless mode
167+
RCTDevMenu *devMenu = [self.reactHost.moduleRegistry moduleForClass:[RCTDevMenu class]];
168+
if (devMenu) {
169+
_contextContainer->erase("RCTDevMenu");
170+
_contextContainer->insert("RCTDevMenu", wrapManagedObject(devMenu));
171+
}
172+
#endif
173+
154174
if (_configuration.customizeRootView != nil) {
155175
_configuration.customizeRootView(surfaceHostingProxyRootView);
156176
}
@@ -186,6 +206,12 @@ - (RCTPlatformView *)createRootViewWithBridge:(RCTBridge *)bridge
186206
#if !TARGET_OS_OSX // [macOS]
187207
rootView.backgroundColor = [UIColor systemBackgroundColor];
188208
#endif // [macOS]
209+
210+
// Set context container if this is a Fabric-enabled RCTSurfaceHostingView (or subclass)
211+
if (enableFabric && [rootView isKindOfClass:[RCTSurfaceHostingView class]]) {
212+
[(RCTSurfaceHostingView *)rootView setContextContainer:_contextContainer];
213+
}
214+
189215
return rootView;
190216
}
191217

@@ -202,6 +228,16 @@ - (RCTPlatformView *)createRootViewWithBridge:(RCTBridge *)bridge
202228
jsInvoker:callInvoker];
203229
_contextContainer->erase("RuntimeScheduler");
204230
_contextContainer->insert("RuntimeScheduler", _runtimeScheduler);
231+
232+
#if TARGET_OS_OSX && __has_include("RCTDevMenu.h") && RCT_DEV
233+
// Insert dev menu for macOS context menu access using proper moduleForClass pattern
234+
RCTDevMenu *devMenu = [bridge moduleForClass:[RCTDevMenu class]];
235+
if (devMenu) {
236+
_contextContainer->erase("RCTDevMenu");
237+
_contextContainer->insert("RCTDevMenu", wrapManagedObject(devMenu));
238+
}
239+
#endif
240+
205241
return RCTAppSetupDefaultJsExecutorFactory(bridge, turboModuleManager, _runtimeScheduler);
206242
} else {
207243
return RCTAppSetupJsExecutorFactoryForOldArch(bridge, _runtimeScheduler);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import "RCTSurfaceHostingView.h"
9+
10+
#ifdef __cplusplus
11+
#import <react/utils/ContextContainer.h>
12+
#endif
13+
14+
NS_ASSUME_NONNULL_BEGIN
15+
16+
@interface RCTSurfaceHostingView (Private)
17+
18+
#ifdef __cplusplus
19+
/** [macOS]
20+
* Context container that provides access to the React Native runtime and TurboModule registry.
21+
* This C++ interface is used internally by React Native for bridgeless mode.
22+
*/
23+
@property (nonatomic, assign, nullable) facebook::react::ContextContainer::Shared contextContainer;
24+
#endif
25+
26+
@end
27+
28+
NS_ASSUME_NONNULL_END

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#import <React/RCTSurfaceProtocol.h>
1212
#import <React/RCTSurfaceSizeMeasureMode.h>
1313
#import <React/RCTSurfaceStage.h>
14-
#import <react/utils/ContextContainer.h>
14+
// #import <react/utils/ContextContainer.h> // [macOS]
1515

1616
@class RCTBridge;
1717
@class RCTSurface;
@@ -49,12 +49,6 @@ NS_ASSUME_NONNULL_BEGIN
4949
*/
5050
@property (nonatomic, assign) RCTSurfaceSizeMeasureMode sizeMeasureMode;
5151

52-
/** [macOS
53-
* Context container that provides access to the React Native runtime and TurboModule registry.
54-
* Primarily used to get access to the dev menu.
55-
*/
56-
@property (nonatomic, assign, nullable) facebook::react::ContextContainer::Shared contextContainer; // macOS]
57-
5852
/**
5953
* Activity indicator factory.
6054
* A hosting view may use this block to instantiate and display custom activity

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66
*/
77

88
#import "RCTSurfaceHostingView.h"
9+
#import "RCTSurfaceHostingView+Private.h" // [macOS]
910
#import "RCTConstants.h"
1011
#import "RCTDefines.h"
1112
#import "RCTSurface.h"
1213
#import "RCTSurfaceDelegate.h"
1314
#import "RCTSurfaceView.h"
1415
#import "RCTUtils.h"
1516

16-
#import <react/utils/ContextContainer.h> // [macOS]
17+
#if TARGET_OS_OSX && __has_include("RCTDevMenu.h") // [macOS]
18+
#import "RCTDevMenu.h"
19+
#import "RCTBridgeProxy.h"
20+
#import <react/utils/ManagedObjectWrapper.h>
21+
22+
using namespace facebook::react;
23+
#endif // [macOS]
1724

1825
@interface RCTSurfaceHostingView ()
1926

@@ -294,4 +301,44 @@ - (void)surface:(__unused RCTSurface *)surface didChangeIntrinsicSize:(__unused
294301
});
295302
}
296303

304+
#pragma mark - Context Menu
305+
306+
#if TARGET_OS_OSX // [macOS]
307+
- (NSMenu *)menuForEvent:(NSEvent *)event
308+
{
309+
NSMenu *menu = nil;
310+
311+
#if __has_include("RCTDevMenu.h") && RCT_DEV
312+
// Try to get dev menu from context container
313+
if (_contextContainer) {
314+
auto devMenuOptional = _contextContainer->find<std::shared_ptr<void>>("RCTDevMenu");
315+
if (devMenuOptional.has_value()) {
316+
RCTDevMenu *devMenu = unwrapManagedObject(*devMenuOptional);
317+
if (devMenu && [devMenu respondsToSelector:@selector(menu)]) {
318+
menu = [devMenu menu];
319+
}
320+
}
321+
}
322+
323+
// Fallback: try legacy bridge access if surface has bridge
324+
if (menu == nil && [_surface respondsToSelector:@selector(bridge)]) {
325+
RCTBridge *bridge = [_surface performSelector:@selector(bridge)];
326+
if (bridge && [bridge respondsToSelector:@selector(devMenu)]) {
327+
RCTDevMenu *devMenu = [bridge devMenu];
328+
if (devMenu && [devMenu respondsToSelector:@selector(menu)]) {
329+
menu = [devMenu menu];
330+
}
331+
}
332+
}
333+
#endif
334+
335+
// Fall back to super's menu if no dev menu available
336+
if (menu == nil) {
337+
menu = [super menuForEvent:event];
338+
}
339+
340+
return menu;
341+
}
342+
#endif // [macOS]
343+
297344
@end

0 commit comments

Comments
 (0)