Skip to content

Commit e87426f

Browse files
committed
Enable ScreenshotManager for macOS
1 parent 3e24c19 commit e87426f

File tree

5 files changed

+88
-50
lines changed

5 files changed

+88
-50
lines changed

packages/rn-tester/NativeModuleExample/Screenshot.m

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ @implementation ScreenshotManager
2020
: (RCTPromiseRejectBlock)reject)
2121
{
2222
[self.bridge.uiManager addUIBlock:^(
23-
__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
23+
__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTPlatformView *> *viewRegistry) { // [macOS]
24+
#if !TARGET_OS_OSX // [macOS]
2425
// Get view
2526
UIView *view;
2627
if (target == nil || [target isEqual:@"window"]) {
@@ -81,6 +82,40 @@ @implementation ScreenshotManager
8182
// If we reached here, something went wrong
8283
reject(RCTErrorUnspecified, error.localizedDescription, error);
8384
});
85+
#else // [macOS
86+
// find the key window
87+
NSWindow *keyWindow;
88+
for (NSWindow *window in NSApp.windows) {
89+
if (window.keyWindow) {
90+
keyWindow = window;
91+
break;
92+
}
93+
}
94+
95+
// take a snapshot of the key window
96+
CGWindowID windowID = (CGWindowID)[keyWindow windowNumber];
97+
CGWindowImageOption imageOptions = kCGWindowImageDefault;
98+
CGWindowListOption listOptions = kCGWindowListOptionIncludingWindow;
99+
CGRect imageBounds = CGRectNull;
100+
CGImageRef windowImage = CGWindowListCreateImage(imageBounds, listOptions, windowID, imageOptions);
101+
NSImage *image = [[NSImage alloc] initWithCGImage:windowImage size:[keyWindow frame].size];
102+
CGImageRelease(windowImage);
103+
104+
// save to a temp file
105+
NSError *error = nil;
106+
NSString *tempFilePath = RCTTempFilePath(@"jpeg", &error);
107+
NSData *imageData = [image TIFFRepresentation];
108+
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
109+
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:@0.8 forKey:NSImageCompressionFactor];
110+
imageData = [imageRep representationUsingType:NSBitmapImageFileTypeJPEG properties:imageProps];
111+
BOOL success = [imageData writeToFile:tempFilePath atomically:NO];
112+
113+
if (success) {
114+
resolve(tempFilePath);
115+
} else {
116+
reject(RCTErrorUnspecified, error.localizedDescription, error);
117+
}
118+
#endif // macOS]
84119
}];
85120
}
86121

packages/rn-tester/NativeModuleExample/ScreenshotManager.podspec

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,13 @@ Pod::Spec.new do |s|
2222
s.source = { :git => "https://github.com/facebook/react-native.git", :tag => "#{s.version}" }
2323

2424
s.source_files = "**/*.{h,m,mm,swift}"
25-
# [macOS
26-
s.ios.exclude_files = "ScreenshotMacOS.{h,mm}"
27-
s.osx.exclude_files = "Screenshot.{h,m}"
28-
# macOS]
25+
# [macOS Github#1734: Disable React-TurboModuleCxx RNW implementation as React Native now has C++ sharing support and examples
26+
s.exclude_files = "ScreenshotMacOS.{h,mm}"
27+
# macOS]
2928
s.requires_arc = true
3029

3130
install_modules_dependencies(s)
3231

33-
# [macOS
34-
s.osx.dependency "React-TurboModuleCxx-RNW"
35-
s.osx.pod_target_xcconfig = { "USE_HEADERMAP" => "YES",
36-
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
37-
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17",
38-
}
39-
# macOS]
40-
4132
# s.dependency "..."
4233

4334
# Enable codegen for this library

packages/rn-tester/Podfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ def pods(target_name, options = {}, use_flipper: !IN_CI && !USE_FRAMEWORKS)
4747
# Additional Pods which aren't included in the default Podfile
4848
pod 'React-RCTPushNotification', :path => "#{@prefix_path}/Libraries/PushNotificationIOS"
4949
pod 'Yoga', :path => "#{@prefix_path}/ReactCommon/yoga", :modular_headers => true
50-
# pod 'React-TurboModuleCxx-WinRTPort', :path => "#{@prefix_path}/ReactTurboModuleCxx" # [macOS]
51-
# pod 'React-TurboModuleCxx-RNW', :podspec => "#{@prefix_path}/ReactTurboModuleCxx/React-TurboModuleCxx-RNW.podspec" # [macOS]
5250
# Additional Pods which are classed as unstable
5351

52+
# [macOS Github#1734: Disable React-TurboModuleCxx RNW implementation as React Native now has C++ sharing support and examples
53+
# pod 'React-TurboModuleCxx-WinRTPort', :path => "#{@prefix_path}/ReactTurboModuleCxx"
54+
# pod 'React-TurboModuleCxx-RNW', :podspec => "#{@prefix_path}/ReactTurboModuleCxx/React-TurboModuleCxx-RNW.podspec"
55+
# macOS]
56+
5457
# RNTester native modules and components
55-
# [macOS] Github#1734: Disable ScreenshotManager once reconcile the codegen strategy for it
56-
# pod 'ScreenshotManager', :path => "NativeModuleExample"
58+
pod 'ScreenshotManager', :path => "NativeModuleExample"
5759
if ENV['RCT_NEW_ARCH_ENABLED'] == '1'
5860
pod 'NativeCxxModuleExample', :path => "NativeCxxModuleExample"
5961
end

packages/rn-tester/Podfile.lock

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ PODS:
368368
- React-logger (= 1000.0.0)
369369
- React-perflogger (= 1000.0.0)
370370
- ReactCommon/turbomodule/core (= 1000.0.0)
371+
- ScreenshotManager (0.0.1):
372+
- RCT-Folly (= 2021.07.22.00)
373+
- React-Core
371374
- SocketRocket (0.6.0)
372375
- Yoga (1.14.0)
373376

@@ -411,6 +414,7 @@ DEPENDENCIES:
411414
- React-runtimeexecutor (from `../../ReactCommon/runtimeexecutor`)
412415
- ReactCommon/turbomodule/core (from `../../ReactCommon`)
413416
- ReactCommon/turbomodule/samples (from `../../ReactCommon`)
417+
- ScreenshotManager (from `NativeModuleExample`)
414418
- Yoga (from `../../ReactCommon/yoga`)
415419

416420
SPEC REPOS:
@@ -493,52 +497,55 @@ EXTERNAL SOURCES:
493497
:path: "../../ReactCommon/runtimeexecutor"
494498
ReactCommon:
495499
:path: "../../ReactCommon"
500+
ScreenshotManager:
501+
:path: NativeModuleExample
496502
Yoga:
497503
:path: "../../ReactCommon/yoga"
498504

499505
SPEC CHECKSUMS:
500506
boost: 8fa3cd00fa17ef6c3221e5fd283fa93e81d23017
501507
DoubleConversion: acaf5db79676d2e9119015819153f0f99191de12
502-
FBLazyVector: 5f27d829b3da40b758f6977b008cf00a90618dca
503-
FBReactNativeSpec: 367a663ab38943195b50fba4262002033e2e7f8f
508+
FBLazyVector: cbfb8f8b7dfba058db2cc0b8591d1bd29955e0d3
509+
FBReactNativeSpec: 0d27e62fc4277373a85f16e258a0af6d2b3c748e
504510
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
505511
glog: 6df0a3d6e2750a50609471fd1a01fd2948d405b5
506512
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
507513
OCMock: 9491e4bec59e0b267d52a9184ff5605995e74be8
508514
RCT-Folly: bf7b4921a91932051ebf1c5c2d297154b1fa3c8c
509-
RCTRequired: 0c10f3c599eace0f47ec2fbdf35b16302acbf5ff
510-
RCTTypeSafety: cece106e998c939f5741a0f4f5205ec5802b3a56
511-
React: b9fbe862b3c5405af1a21019da32f02e00bbc253
512-
React-bridging: 04e8ab79c66a5a2eb6f12b63392a33ea348409e4
513-
React-callinvoker: b1cd1b385243650ac8173f640d73f060d55f6d3f
515+
RCTRequired: 1114ffca53adc4a15dfb12089cef37e65a0f90bc
516+
RCTTypeSafety: 9588858c3f2d0b8eda0ccbbb019b29c733d20a4a
517+
React: c458bbb9d111c9f651fabd6dbb5db243992cd9f4
518+
React-bridging: 3dbf23b128a2539a528625ac1f0d459698dc204e
519+
React-callinvoker: c44b52f149123627b079565c4f71dd2bba8b63c1
514520
React-Codegen: e1a5e78a10d564627176af4cb4f73e683297887c
515-
React-Core: fa70d438f57324c2a52f4a0d3c093eb35682ecb8
516-
React-CoreModules: d5011a429eaa9c9db968b6805cdfd0b7e37f4d72
517-
React-cxxreact: dfd5ae7d01ea6ed3ca55f8f6f1a59b2f6e87aa65
518-
React-jsc: da0fa12ac20303713967bfa33acaa1d028cf4ed6
519-
React-jsi: a3397329548cd9bf9e682beb581d8f6a66a4dd3e
520-
React-jsidynamic: 43df2ccad662172f0a2e034333345b305c975e03
521-
React-jsiexecutor: bafcaf94deb047215600d259d46aae13acdb0910
522-
React-jsinspector: effe66a5001d6c9025a917284ef5ac48abdf1b8c
523-
React-logger: aadc23f62b86336a687436bdb674b002357de6a7
524-
React-perflogger: 7b6d3c17c68db2e0b9a662ab94d0453ec342e2db
525-
React-RCTActionSheet: a8a6a19b4020a35a5ab5e9ba7bcea537869a1264
526-
React-RCTAnimation: f433f281a9ac5375ec6bbd77e5f8ceeb9cbab369
527-
React-RCTAppDelegate: eb10f96c39e7f03a9e84c05eaceb8650a0dd81f7
528-
React-RCTBlob: 7515552be4803fc25a031d53f79a22d5923f5627
529-
React-RCTImage: 6b1e40d41aea9653d94f2d225d1e528ffd17207f
530-
React-RCTLinking: 4a11086e04c93d7e95136c2560d4ffb62a1a3f2c
531-
React-RCTNetwork: 7b3aaff86b4f4ac705f21af6d962b74bc6680e15
532-
React-RCTPushNotification: 1a82c36e11dd0801b0c32c718a7ede1deda7de63
533-
React-RCTSettings: 4c40e69eb42616534306c116d3adc264e3c2cca1
534-
React-RCTTest: 48cdc6937fc8192a1811be9e7e2ea1dab24852ba
535-
React-RCTText: 7f8996f909584ad561e51292b8fa6032ad33ee85
536-
React-RCTVibration: f428ba71e88aec1bb4806f5f3de8c15ee7cc9e01
537-
React-runtimeexecutor: 28a1c2e3e87654b6af9eba16e69e0205c0c53665
538-
ReactCommon: d14531b62f6088cd1747dd3612b24e592f4ac538
521+
React-Core: 5f14f4f1a9d7b4a05c5037a6dfdb85b612a72d22
522+
React-CoreModules: adae19cd0d00575f55a44b243f6788e14e9ad80a
523+
React-cxxreact: 515b7c17cf7dc849d7e141dad33e406fcd229b33
524+
React-jsc: c20053ea234eb4fe533637d3f9dacef8a85c88ef
525+
React-jsi: 0f804e07353e32174ad004056172ed568ee66e66
526+
React-jsidynamic: 0524d578a13a6a4d1b8c7c38c6d46a41a7267521
527+
React-jsiexecutor: 12f385a072db73331c9f6be5f63ec8a9b8fb7fe9
528+
React-jsinspector: d429d7a486b9e3609ee107bf603d19d1c20c2e54
529+
React-logger: d6f3d897d2a7adc73cd397fed5d1bf461bd3440e
530+
React-perflogger: a280925b7ceaac26e15a9486bf9c174c12fbb7e8
531+
React-RCTActionSheet: d53f7e4d1d6e2902a19e03226d1d8a268439ac0f
532+
React-RCTAnimation: fb35350c5542232fb18ef5390b770d29f3b60f59
533+
React-RCTAppDelegate: 1d1e54d15cc6b207f1dacf739fce585e7b07a73c
534+
React-RCTBlob: 80baa34d8cc0a73a37692bd7acf2ae8323bc3017
535+
React-RCTImage: 6e8918b9e1e25e8e79dafebef3dd00a790df3df8
536+
React-RCTLinking: 067f7a6df17de7aa772a51bf7c3507a3d30e77e1
537+
React-RCTNetwork: 5880bebf9660a0973602a9d31a6a272964aa085a
538+
React-RCTPushNotification: a7bba99765d80dc501c57981100cefcac8d482a9
539+
React-RCTSettings: 984b207f8399a8e0b0123c32991b2b343a32003b
540+
React-RCTTest: f6c2b68143a48ff30299e3114cfd316f9accd966
541+
React-RCTText: 6ad9974b84f10dc1484269f3ee960848771ad5f0
542+
React-RCTVibration: 9f8714e20cc0748e85cefc4d40d074c1743a2a96
543+
React-runtimeexecutor: a49e4dbd198ca2d546881cf12f2e3a3be42da352
544+
ReactCommon: 54ad531f131a8b2fff2abd05420898302bd3ab19
545+
ScreenshotManager: f9f16ef094bef209f0e748d46acf566d25dd7de5
539546
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
540-
Yoga: 180194e730ba4fac02f568a11ce84dfdffc7ad9c
547+
Yoga: 35d5639406a3778a0c532628242abd71bb5dd990
541548

542-
PODFILE CHECKSUM: 07f9bc8111506452540b5b2a46ba58f745825b21
549+
PODFILE CHECKSUM: cb331bb85b088563f2ae234f77b8a34492e24201
543550

544551
COCOAPODS: 1.11.3

packages/rn-tester/RNTester/AppDelegate.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ @interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate,
7878

7979
RCTTurboModuleManager *_turboModuleManager;
8080
#if TARGET_OS_OSX // [macOS
81+
// Github#1734: Disable React-TurboModuleCxx RNW implementation as React Native now has C++ sharing support and examples
8182
// std::shared_ptr<winrt::Microsoft::ReactNative::TurboModulesProvider> _turboModulesProvider;
8283
#endif // macOS]
8384
}
@@ -256,6 +257,7 @@ - (Class)getModuleClassFromName:(const char *)name
256257
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
257258
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
258259
{
260+
// [macOS Github#1734: Disable React-TurboModuleCxx RNW implementation as React Native now has C++ sharing support and examples
259261
// if (!_turboModulesProvider) {
260262
// _turboModulesProvider = std::make_shared<winrt::Microsoft::ReactNative::TurboModulesProvider>();
261263
// _turboModulesProvider->SetReactContext(winrt::Microsoft::ReactNative::CreateMacOSReactContext(jsInvoker));
@@ -264,6 +266,7 @@ - (Class)getModuleClassFromName:(const char *)name
264266
// L"ScreenshotManager", winrt::Microsoft::ReactNative::MakeModuleProvider<ScreenshotManagerCxx>());
265267
// }
266268
// return _turboModulesProvider->getModule(name, jsInvoker);
269+
// macOS]
267270
return facebook::react::RNTesterTurboModuleProvider(name, jsInvoker);
268271
}
269272

0 commit comments

Comments
 (0)