forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make "Snapshot/Screenshot" test page works (#546)
* Add ScreenshotManagerTurboModule, but it is not called. * ... * Update AppDelegate.mm * Fix code due to facebook API rename * Update AppDelegate.mm * [Pods] Expose boost headers to consumer of RCT-Folly (#1) * Update AppDelegate.mm * ScreenshotManagerTurboModule gets called * reject the promise in takeScreenshot * ... * Put TurboModuleRegistry.get call at the right place * ... * Successfully took the screenshot * Remove unnecessary code * Fix code review comments * Fix code review comments * ... * Fix code review comments * Fix build break * Fix build break * Fix build break * Update NativeScreenshotManager.js * Fix build break * Fix yarn lint errors Co-authored-by: Eloy Durán <[email protected]>
- Loading branch information
1 parent
92b68a5
commit e4bc8c4
Showing
8 changed files
with
233 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import <React/RCTViewManager.h> | ||
#import <ReactCommon/RCTTurboModuleManager.h> | ||
|
||
@interface ScreenshotManagerTurboModuleManagerDelegate : NSObject<RCTTurboModuleManagerDelegate> | ||
- (std::shared_ptr<facebook::react::TurboModule>) | ||
getTurboModule:(const std::string &)name | ||
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker; | ||
|
||
- (std::shared_ptr<facebook::react::TurboModule>) | ||
getTurboModule:(const std::string &)name | ||
instance:(id<RCTTurboModule>)instance | ||
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import "ScreenshotMacOS.h" | ||
#import <React/RCTUIManager.h> | ||
#import <React/RCTUtils.h> | ||
#import <ReactCommon/RCTTurboModuleManager.h> | ||
#import <ReactCommon/TurboModuleUtils.h> | ||
|
||
static NSImage* TakeScreenshot() | ||
{ | ||
// find the key window | ||
NSWindow* keyWindow; | ||
for (NSWindow* window in NSApp.windows) { | ||
if (window.keyWindow) { | ||
keyWindow = window; | ||
break; | ||
} | ||
} | ||
|
||
// take a snapshot of the key window | ||
CGWindowID windowID = (CGWindowID)[keyWindow windowNumber]; | ||
CGWindowImageOption imageOptions = kCGWindowImageDefault; | ||
CGWindowListOption listOptions = kCGWindowListOptionIncludingWindow; | ||
CGRect imageBounds = CGRectNull; | ||
CGImageRef windowImage = CGWindowListCreateImage( | ||
imageBounds, | ||
listOptions, | ||
windowID, | ||
imageOptions); | ||
NSImage* image = [[NSImage alloc] initWithCGImage:windowImage size:[keyWindow frame].size]; | ||
|
||
return image; | ||
} | ||
|
||
static NSString* SaveScreenshotToTempFile(NSImage* image) | ||
{ | ||
// save to a temp file | ||
NSError *error = nil; | ||
NSString *tempFilePath = RCTTempFilePath(@"jpeg", &error); | ||
NSData* imageData = [image TIFFRepresentation]; | ||
NSBitmapImageRep* imageRep = [NSBitmapImageRep imageRepWithData:imageData]; | ||
NSDictionary* imageProps = | ||
[NSDictionary | ||
dictionaryWithObject:@0.8 | ||
forKey:NSImageCompressionFactor | ||
]; | ||
imageData = [imageRep representationUsingType:NSBitmapImageFileTypeJPEG properties:imageProps]; | ||
[imageData writeToFile:tempFilePath atomically:NO]; | ||
|
||
return tempFilePath; | ||
} | ||
|
||
class ScreenshotManagerTurboModule : public facebook::react::TurboModule | ||
{ | ||
public: | ||
ScreenshotManagerTurboModule(std::shared_ptr<facebook::react::CallInvoker> jsInvoker) | ||
:facebook::react::TurboModule("ScreenshotManager", jsInvoker) | ||
{ | ||
} | ||
|
||
facebook::jsi::Value get( | ||
facebook::jsi::Runtime& runtime, | ||
const facebook::jsi::PropNameID& propName | ||
) override | ||
{ | ||
auto jsInvoker = jsInvoker_; | ||
auto key = propName.utf8(runtime); | ||
if (key == "takeScreenshot") | ||
{ | ||
return facebook::jsi::Function::createFromHostFunction( | ||
runtime, | ||
propName, | ||
0, | ||
[jsInvoker]( | ||
facebook::jsi::Runtime& runtime, | ||
const facebook::jsi::Value& thisVal, | ||
const facebook::jsi::Value *args, | ||
size_t count) | ||
{ | ||
return facebook::react::createPromiseAsJSIValue( | ||
runtime, | ||
[jsInvoker](facebook::jsi::Runtime& runtime, std::shared_ptr<facebook::react::Promise> promise) | ||
{ | ||
// ignore arguments, assume to be ('window', {format: 'jpeg', quality: 0.8}) | ||
|
||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
NSImage* screenshotImage = TakeScreenshot(); | ||
jsInvoker->invokeAsync([screenshotImage, &runtime, promise]() | ||
{ | ||
NSString* tempFilePath = SaveScreenshotToTempFile(screenshotImage); | ||
promise->resolve(facebook::jsi::Value( | ||
runtime, | ||
facebook::jsi::String::createFromUtf8( | ||
runtime, | ||
std::string([tempFilePath UTF8String]) | ||
) | ||
)); | ||
}); | ||
}); | ||
} | ||
); | ||
} | ||
); | ||
} | ||
else | ||
{ | ||
return facebook::jsi::Value::undefined(); | ||
} | ||
} | ||
}; | ||
|
||
@implementation ScreenshotManagerTurboModuleManagerDelegate | ||
|
||
- (std::shared_ptr<facebook::react::TurboModule>) | ||
getTurboModule:(const std::string &)name | ||
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker | ||
{ | ||
if (name == "ScreenshotManager") | ||
{ | ||
return std::make_shared<ScreenshotManagerTurboModule>(jsInvoker); | ||
} | ||
return nullptr; | ||
} | ||
|
||
|
||
- (std::shared_ptr<facebook::react::TurboModule>) | ||
getTurboModule:(const std::string &)name | ||
instance:(id<RCTTurboModule>)instance | ||
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker | ||
{ | ||
if (name == "ScreenshotManager") | ||
{ | ||
return std::make_shared<ScreenshotManagerTurboModule>(jsInvoker); | ||
} | ||
return nullptr; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters