Moved screenshot functions into Device's class.

Summary:
I have moved the screenshot functions from ScreenCaptureButtons to the Device classes. I have slightly rewritten them so that they return a Promise which resolves to a Buffer. The Buffer can then be saved to a file or converted to a data Blob.

I have removed streaming and simply loaded the image into memory. Once the image is in memory it can be manipulated for various tasks i.e. written to a file, or displayed in the app.

iOS screenshots had to be rewritten. I now save the image to a temp folder, load it into the apps memory, and then remove the temp image.

Reviewed By: jknoxville

Differential Revision: D16939901

fbshipit-source-id: 3e39a5aeda8d48829ac5a8ff912a98f110341c07
This commit is contained in:
Benjamin Elo
2019-08-22 05:26:46 -07:00
committed by Facebook Github Bot
parent 263b47f82f
commit 7def9bb681
6 changed files with 81 additions and 46 deletions

View File

@@ -5,7 +5,7 @@
* @format
*/
import {Button, ButtonGroup} from 'flipper';
import {Button, ButtonGroup, writeBufferToFile} from 'flipper';
import React, {Component} from 'react';
import {connect} from 'react-redux';
import AndroidDevice from '../devices/AndroidDevice';
@@ -26,17 +26,10 @@ const CAPTURE_LOCATION = expandTilde(
config().screenCapturePath || remote.app.getPath('desktop'),
);
type PullTransfer = any;
type OwnProps = {};
type StateFromProps = {
selectedDevice:
| BaseDevice
| typeof AndroidDevice
| typeof IOSDevice
| null
| undefined;
selectedDevice: BaseDevice | null | undefined;
};
type DispatchFromProps = {};
@@ -73,17 +66,6 @@ function getFileName(extension: 'png' | 'mp4'): string {
return `Screen Capture ${new Date().toISOString()}.${extension}`;
}
function writePngStreamToFile(stream: PullTransfer): Promise<string> {
return new Promise((resolve, reject) => {
const pngPath = path.join(CAPTURE_LOCATION, getFileName('png'));
stream.on('end', () => {
resolve(pngPath);
});
stream.on('error', reject);
stream.pipe(fs.createWriteStream(pngPath));
});
}
type Props = OwnProps & StateFromProps & DispatchFromProps;
class ScreenCaptureButtons extends Component<Props, State> {
iOSRecorder: any | null | undefined;
@@ -132,32 +114,14 @@ class ScreenCaptureButtons extends Component<Props, State> {
captureScreenshot: Promise<void> | any = () => {
const {selectedDevice} = this.props;
if (selectedDevice instanceof AndroidDevice) {
return reportPlatformFailures(
selectedDevice.adb
.screencap(selectedDevice.serial)
.then(writePngStreamToFile)
.then(openFile),
'captureScreenshotAndroid',
).catch(console.error);
} else if (selectedDevice instanceof IOSDevice) {
const screenshotPath = path.join(CAPTURE_LOCATION, getFileName('png'));
return reportPlatformFailures(
new Promise((resolve, reject) => {
exec(
`xcrun simctl io booted screenshot "${screenshotPath}"`,
async err => {
if (err) {
reject(err);
} else {
openFile(screenshotPath);
resolve();
}
},
);
}),
'captureScreenshotIos',
const pngPath = path.join(CAPTURE_LOCATION, getFileName('png'));
if (selectedDevice != null) {
reportPlatformFailures(
selectedDevice
.screenshot()
.then((buffer: Buffer) => writeBufferToFile(pngPath, buffer))
.then((path: string) => openFile(path)),
'captureScreenshot',
);
}
};