Files
flipper/desktop/flipper-ui-core/src/utils/screenshot.tsx
Andrey Goncharov 6c74f2dd18 Set device features on device initialization
Summary:
1. Identify if device supports screenshots/screen recording when it is created.
2. Disable screen recording/screenshot buttons when they are not supported

Reviewed By: passy

Differential Revision: D34611133

fbshipit-source-id: 82ad2d67e4af482d9becf7995187667b5d99bc36
2022-03-04 02:00:23 -08:00

54 lines
1.8 KiB
TypeScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import BaseDevice from '../devices/BaseDevice';
import {reportPlatformFailures} from 'flipper-common';
import {getRenderHostInstance} from '../RenderHost';
import {getFlipperLib, path} from 'flipper-plugin';
import {assertNotNull} from './assertNotNull';
export function getCaptureLocation() {
return (
getRenderHostInstance().serverConfig.processConfig.screenCapturePath ||
getRenderHostInstance().serverConfig.paths.desktopPath
);
}
// TODO: refactor so this doesn't need to be exported
export function getFileName(extension: 'png' | 'mp4'): string {
// Windows does not like `:` in its filenames. Yes, I know ...
return `screencap-${new Date().toISOString().replace(/:/g, '')}.${extension}`;
}
export async function capture(device: BaseDevice): Promise<string> {
if (!device.connected.get()) {
console.info('Skipping screenshot for disconnected device');
return '';
}
const pngPath = path.join(getCaptureLocation(), getFileName('png'));
return reportPlatformFailures(
// TODO: there is no reason to read the screenshot first, grab it over the websocket, than send it back
// again to write in a file, probably easier to change screenshot api to `device.screenshot(): path`
device
.screenshot()
.then((buffer) => {
assertNotNull(
buffer,
`Device ${device.description.deviceType}:${device.description.os} does not support taking screenshots`,
);
return getFlipperLib().remoteServerContext.fs.writeFileBinary(
pngPath,
buffer,
);
})
.then(() => pngPath),
'captureScreenshot',
);
}